perf(helios): replace strided RoPE with stack+flatten for contiguous memory (#2474)
Signed-off-by: willamhou <willamhou@ceresman.com> Co-authored-by: willamhou <willamhou@ceresman.com>
This commit is contained in:
1 parent
26edc7f2fb
commit
15684518d0
2 files changed
+122
-4
No files matched your search
@@ -62,10 +62,16 @@ def apply_rotary_emb_helios(
|
||||
"""
|
||||
x_1, x_2 = hidden_states.unflatten(-1, (-1, 2)).unbind(-1)
|
||||
cos, sin = freqs_cis.unsqueeze(-2).chunk(2, dim=-1)
|
||||
out = torch.empty_like(hidden_states)
|
||||
out[..., 0::2] = x_1 * cos[..., 0::2] - x_2 * sin[..., 1::2]
|
||||
out[..., 1::2] = x_1 * sin[..., 1::2] + x_2 * cos[..., 0::2]
|
||||
return out.type_as(hidden_states)
|
||||
# Use stack+flatten instead of strided slice assignment for contiguous
|
||||
# memory layout and better performance on GPU/NPU (#2436, cf. PR #2393).
|
||||
rotated = torch.stack(
|
||||
(
|
||||
x_1 * cos[..., 0::2] - x_2 * sin[..., 1::2],
|
||||
x_1 * sin[..., 1::2] + x_2 * cos[..., 0::2],
|
||||
),
|
||||
dim=-1,
|
||||
)
|
||||
return rotated.flatten(-2, -1).type_as(hidden_states)
|
||||
|
||||
|
||||
class DistributedRMSNorm(nn.Module):
|
||||
|
||||
Reference in new issue
Block a user