MCPcopy Create free account
hub / github.com/huggingface/transformers / positional_encoding

Function positional_encoding

src/transformers/modeling_ctrl.py:46–58  ·  view source on GitHub ↗
(position, d_model_size, dtype)

Source from the content-addressed store, hash-verified

44
45
46def positional_encoding(position, d_model_size, dtype):
47 # create the sinusoidal pattern for the positional encoding
48 angle_rads = angle_defn(
49 torch.arange(position, dtype=dtype).unsqueeze(1),
50 torch.arange(d_model_size, dtype=dtype).unsqueeze(0),
51 d_model_size,
52 )
53
54 sines = torch.sin(angle_rads[:, 0::2])
55 cosines = torch.cos(angle_rads[:, 1::2])
56
57 pos_encoding = torch.cat([sines, cosines], dim=-1)
58 return pos_encoding
59
60
61def scaled_dot_product_attention(q, k, v, mask, attention_mask=None, head_mask=None):

Callers 1

__init__Method · 0.70

Calls 1

angle_defnFunction · 0.70

Tested by

no test coverage detected