Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
mindspeed · 1 changed files
Base commit: df2ea7fad8e43016b0e2edbf7ce16026496df2ae
diff --git a/mindspeed/core/fusions/fused_rope.py b/mindspeed/core/fusions/fused_rope.pyindex 00a3d66e41411ac63b1e52536194bc47fa38d397..21d72ade79c8f47ac00e0f02c39ae14506fc79d1 100644--- a/mindspeed/core/fusions/fused_rope.py+++ b/mindspeed/core/fusions/fused_rope.py@@ -62,8 +62,7 @@ def apply_rotary_pos_emb_bshd( rot_dim = freqs.shape[-1] t, t_pass = t[..., :rot_dim], t[..., rot_dim:]- cos_ = (torch.cos(freqs) * _mscale).to(t.dtype)- sin_ = (torch.sin(freqs) * _mscale).to(t.dtype)+ cos_, sin_ = _cos_sin_cache_lookup(freqs, t.dtype, _mscale) if getattr(args, "use_fused_rotary_pos_emb"): mode = 1 if rotary_interleaved else 0@@ -71,7 +70,28 @@ def apply_rotary_pos_emb_bshd( else: t = (t * cos_) + (_rotate_half(t, rotary_interleaved) * sin_) - return torch.cat((t, t_pass), dim=-1)+ if t_pass.numel() > 0:+ return torch.cat((t, t_pass), dim=-1)+ return t+++# Every layer of a micro-batch shares one `freqs` tensor, so a single-entry cache+# removes the repeated cos/sin/mul/cast work that used to run per attention layer.+# The cached `freqs` reference is kept alive so its identity cannot be recycled.+_COS_SIN_CACHE: dict[str, tuple] = {}+++def _cos_sin_cache_lookup(freqs: Tensor, dtype: torch.dtype, mscale: float):+ key = f"{dtype}:{mscale!r}"+ entry = _COS_SIN_CACHE.get(key)+ if entry is not None and entry[0] is freqs:+ return entry[1], entry[2]+ cos_ = (torch.cos(freqs) * mscale).to(dtype)+ sin_ = (torch.sin(freqs) * mscale).to(dtype)+ if len(_COS_SIN_CACHE) > 8:+ _COS_SIN_CACHE.clear()+ _COS_SIN_CACHE[key] = (freqs, cos_, sin_)+ return cos_, sin_ def transformer_config_post_init_wrapper(fn):