MCPcopy Create free account
hub / github.com/THUDM/GLM / PositionalEmbedding

Class PositionalEmbedding

mpu/transformer.py:38–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36
37
38class PositionalEmbedding(torch.nn.Module):
39 def __init__(self, hidden_size):
40 super(PositionalEmbedding, self).__init__()
41
42 self.hidden_size = hidden_size
43
44 inv_freq = 1 / (10000 ** (torch.arange(0.0, hidden_size, 2.0) / hidden_size))
45 self.register_buffer('inv_freq', inv_freq)
46
47 def forward(self, pos_seq, bsz=None):
48 sinusoid_inp = torch.ger(pos_seq, self.inv_freq)
49 pos_emb = torch.cat([sinusoid_inp.sin(), sinusoid_inp.cos()], dim=-1)
50
51 if bsz is not None:
52 return pos_emb[None, :, :].expand(bsz, -1, -1)
53 else:
54 return pos_emb[None, :, :]
55
56
57class ParallelCrossAttention(torch.nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected