Mrope accuracy fix for qwen#1437
Merged
Merged
Conversation
When mrope_interleaved is enabled, HPUMRotaryEmbedding was still using the non-interleaved split/concat section mapping for cos/sin. This produced incorrect rotary channel ordering for multimodal MRoPE inputs and could cause sample-level mismatches against upstream vLLM behavior. Use apply_interleaved_rope for the interleaved branch, and preserve the existing split/concat logic for non-interleaved layouts. Co-authored-by: Jimin Ha <jimin.ha@intel.com> Signed-off-by: Harish Subramony <harish.subramony@intel.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the HPU implementation of MRotaryEmbedding to improve multimodal MRoPE correctness/accuracy (notably for Qwen-style 3-axis positions) by changing how cos/sin values are assembled for the rotary kernel.
Changes:
- Add an optional
prepare_mrope_cachemode to precompute three sparse cos/sin caches (per T/H/W axis) that can be combined by addition. - Normalize/reshape incoming
positionsmore defensively (handling[3, seq_len, 1],[1, seq_len], and flattened forms). - Switch the multimodal
[3, seq_len]path from per-step split/concat to cache-sum assembly.
Comment on lines
+639
to
+654
| assert self.mrope_section | ||
|
|
||
| sin_start_idx = self.rotary_dim // 2 | ||
| if getattr(self, "mrope_interleaved", False): | ||
| mrope1_slice = ( | ||
| list(range(1, self.mrope_section[1] * 3, 3)) | ||
| + list(range(sin_start_idx + 1, sin_start_idx + self.mrope_section[1] * 3, 3))) | ||
| mrope2_slice = ( | ||
| list(range(2, self.mrope_section[2] * 3, 3)) | ||
| + list(range(sin_start_idx + 2, sin_start_idx + self.mrope_section[2] * 3, 3))) | ||
| else: | ||
| c0 = self.mrope_section[0] | ||
| c1 = c0 + self.mrope_section[1] | ||
| c2 = c1 + self.mrope_section[2] | ||
| mrope1_slice = list(range(c0, c1)) + list(range(sin_start_idx + c0, sin_start_idx + c1)) | ||
| mrope2_slice = list(range(c1, c2)) + list(range(sin_start_idx + c1, sin_start_idx + c2)) |
| cos = torch.cat((cos, cos), dim=-1).unsqueeze(-2) | ||
| sin = torch.cat((sin, sin), dim=-1).unsqueeze(-2) | ||
| if offsets is not None: | ||
| offsets = offsets.view(positions.shape[0], -1) |
Comment on lines
+713
to
+714
| if positions.shape[0] != num_tokens: | ||
| positions = positions.view(-1, num_tokens) |
Comment on lines
+656
to
+672
| self.cos_sin_cache_mrope1 = torch.zeros_like(self.cos_sin_cache) | ||
| self.cos_sin_cache_mrope2 = torch.zeros_like(self.cos_sin_cache) | ||
| self.cos_sin_cache_mrope1[..., mrope1_slice] = self.cos_sin_cache[..., mrope1_slice] | ||
| self.cos_sin_cache_mrope2[..., mrope2_slice] = self.cos_sin_cache[..., mrope2_slice] | ||
| self.cos_sin_cache_mrope0 = self.cos_sin_cache.clone() | ||
| self.cos_sin_cache_mrope0[..., mrope1_slice] = 0 | ||
| self.cos_sin_cache_mrope0[..., mrope2_slice] = 0 | ||
|
|
||
| def repeat_cache(cos_sin_cache: torch.Tensor) -> torch.Tensor: | ||
| if self.is_neox_style: | ||
| cos, sin = cos_sin_cache.chunk(2, dim=-1) | ||
| return torch.cat((cos, cos, sin, sin), dim=-1) | ||
| return torch.repeat_interleave(cos_sin_cache, 2, dim=-1) | ||
|
|
||
| self.cos_sin_cache_mrope0 = repeat_cache(self.cos_sin_cache_mrope0) | ||
| self.cos_sin_cache_mrope1 = repeat_cache(self.cos_sin_cache_mrope1) | ||
| self.cos_sin_cache_mrope2 = repeat_cache(self.cos_sin_cache_mrope2) |
Comment on lines
+656
to
+673
| self.cos_sin_cache_mrope1 = torch.zeros_like(self.cos_sin_cache) | ||
| self.cos_sin_cache_mrope2 = torch.zeros_like(self.cos_sin_cache) | ||
| self.cos_sin_cache_mrope1[..., mrope1_slice] = self.cos_sin_cache[..., mrope1_slice] | ||
| self.cos_sin_cache_mrope2[..., mrope2_slice] = self.cos_sin_cache[..., mrope2_slice] | ||
| self.cos_sin_cache_mrope0 = self.cos_sin_cache.clone() | ||
| self.cos_sin_cache_mrope0[..., mrope1_slice] = 0 | ||
| self.cos_sin_cache_mrope0[..., mrope2_slice] = 0 | ||
|
|
||
| def repeat_cache(cos_sin_cache: torch.Tensor) -> torch.Tensor: | ||
| if self.is_neox_style: | ||
| cos, sin = cos_sin_cache.chunk(2, dim=-1) | ||
| return torch.cat((cos, cos, sin, sin), dim=-1) | ||
| return torch.repeat_interleave(cos_sin_cache, 2, dim=-1) | ||
|
|
||
| self.cos_sin_cache_mrope0 = repeat_cache(self.cos_sin_cache_mrope0) | ||
| self.cos_sin_cache_mrope1 = repeat_cache(self.cos_sin_cache_mrope1) | ||
| self.cos_sin_cache_mrope2 = repeat_cache(self.cos_sin_cache_mrope2) | ||
| self._mrope_hpu_cache_prepared = True |
Comment on lines
+718
to
+723
| use_mrope_cache_sum = positions.ndim == 2 and positions.shape[0] == 3 | ||
| if use_mrope_cache_sum: | ||
| if not getattr(self, "_mrope_hpu_cache_prepared", False): | ||
| self.prepare_cos_sin(positions, offsets, prepare_mrope_cache=True) | ||
| cos_sin = (self.cos_sin_cache_mrope0[positions[0]] + self.cos_sin_cache_mrope1[positions[1]] + | ||
| self.cos_sin_cache_mrope2[positions[2]]) |
🚧 CI BlockedThe main CI workflow was not started for the following reason:
|
✅ CI PassedAll checks passed successfully against the following vllm commit: |
adobrzyn
approved these changes
May 13, 2026
✅ CI PassedAll checks passed successfully against the following vllm commit: |
iboiko-habana
pushed a commit
that referenced
this pull request
May 18, 2026
When mrope_interleaved is enabled, HPUMRotaryEmbedding was still using the non-interleaved split/concat section mapping for cos/sin. This produced incorrect rotary channel ordering for multimodal MRoPE inputs and could cause sample-level mismatches against upstream vLLM behavior. Use apply_interleaved_rope for the interleaved branch, and preserve the existing split/concat logic for non-interleaved layouts. Signed-off-by: Harish Subramony <harish.subramony@intel.com> Co-authored-by: Jimin Ha <jimin.ha@intel.com> Co-authored-by: Agata Dobrzyniewicz <160237065+adobrzyn@users.noreply.github.com> Co-authored-by: Seunghyuk Park (shepark) <separk@habana.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When mrope_interleaved is enabled, HPUMRotaryEmbedding was still using the non-interleaved split/concat section mapping for cos/sin.
This produced incorrect rotary channel ordering for multimodal MRoPE inputs and could cause sample-level mismatches against upstream vLLM behavior.
Use apply_interleaved_rope for the interleaved branch, and preserve the existing split/concat logic for non-interleaved layouts.