MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / PositionEmbeddingLearned

Class PositionEmbeddingLearned

models/aios/position_encoding.py:118–143  ·  view source on GitHub ↗

Absolute pos embedding, learned.

Source from the content-addressed store, hash-verified

116
117
118class PositionEmbeddingLearned(nn.Module):
119 """Absolute pos embedding, learned."""
120 def __init__(self, num_pos_feats=256):
121 super().__init__()
122 self.row_embed = nn.Embedding(50, num_pos_feats)
123 self.col_embed = nn.Embedding(50, num_pos_feats)
124 self.reset_parameters()
125
126 def reset_parameters(self):
127 nn.init.uniform_(self.row_embed.weight)
128 nn.init.uniform_(self.col_embed.weight)
129
130 def forward(self, tensor_list: NestedTensor):
131 x = tensor_list.tensors
132 h, w = x.shape[-2:]
133 i = torch.arange(w, device=x.device)
134 j = torch.arange(h, device=x.device)
135 x_emb = self.col_embed(i)
136 y_emb = self.row_embed(j)
137 pos = torch.cat([
138 x_emb.unsqueeze(0).repeat(h, 1, 1),
139 y_emb.unsqueeze(1).repeat(1, w, 1),
140 ],
141 dim=-1).permute(2, 0, 1).unsqueeze(0).repeat(
142 x.shape[0], 1, 1, 1)
143 return pos
144
145
146def build_position_encoding(args):

Callers 1

build_position_encodingFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected