MCPcopy
hub / github.com/lm-sys/FastChat / apply_rotary_pos_emb

Function apply_rotary_pos_emb

fastchat/model/monkey_patch_non_inplace.py:20–27  ·  view source on GitHub ↗
(q, k, cos, sin, position_ids)

Source from the content-addressed store, hash-verified

18
19
20def apply_rotary_pos_emb(q, k, cos, sin, position_ids):
21 gather_indices = position_ids[:, None, :, None] # [bs, 1, seq_len, 1]
22 gather_indices = gather_indices.repeat(1, cos.shape[1], 1, cos.shape[3])
23 cos = torch.gather(cos.repeat(gather_indices.shape[0], 1, 1, 1), 2, gather_indices)
24 sin = torch.gather(sin.repeat(gather_indices.shape[0], 1, 1, 1), 2, gather_indices)
25 q_embed = (q * cos) + (rotate_half(q) * sin)
26 k_embed = (k * cos) + (rotate_half(k) * sin)
27 return q_embed, k_embed
28
29
30def forward(

Callers 1

forwardFunction · 0.70

Calls 1

rotate_halfFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…