MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / get_2d_sincos_pos_embed

Function get_2d_sincos_pos_embed

diffusers/src/diffusers/models/embeddings.py:128–147  ·  view source on GitHub ↗

grid_size: int of the grid height and width return: pos_embed: [grid_size*grid_size, embed_dim] or [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token)

(
    embed_dim, grid_size, cls_token=False, extra_tokens=0, interpolation_scale=1.0, base_size=16
)

Source from the content-addressed store, hash-verified

126
127
128def get_2d_sincos_pos_embed(
129 embed_dim, grid_size, cls_token=False, extra_tokens=0, interpolation_scale=1.0, base_size=16
130):
131 """
132 grid_size: int of the grid height and width return: pos_embed: [grid_size*grid_size, embed_dim] or
133 [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token)
134 """
135 if isinstance(grid_size, int):
136 grid_size = (grid_size, grid_size)
137
138 grid_h = np.arange(grid_size[0], dtype=np.float32) / (grid_size[0] / base_size) / interpolation_scale
139 grid_w = np.arange(grid_size[1], dtype=np.float32) / (grid_size[1] / base_size) / interpolation_scale
140 grid = np.meshgrid(grid_w, grid_h) # here w goes first
141 grid = np.stack(grid, axis=0)
142
143 grid = grid.reshape([2, 1, grid_size[1], grid_size[0]])
144 pos_embed = get_2d_sincos_pos_embed_from_grid(embed_dim, grid)
145 if cls_token and extra_tokens > 0:
146 pos_embed = np.concatenate([np.zeros([extra_tokens, embed_dim]), pos_embed], axis=0)
147 return pos_embed
148
149
150def get_2d_sincos_pos_embed_from_grid(embed_dim, grid):

Callers 3

__init__Method · 0.85
__init__Method · 0.85
forwardMethod · 0.85

Calls 1

Tested by

no test coverage detected