fix(trainer): contiguous routed_experts slice for glm4_moe under compile - #2860
Merged
mikasenghaas merged 1 commit intoJun 24, 2026
Merged
Conversation
Router replay passes routed_experts[:, :, layer_idx, :] (a strided view of [tokens, layers, topk], dim-1 stride = layers*topk) into the MoE forward. Under torch.compile the inductor kernel asserts a contiguous input, so it crashed with `assert_size_stride ... stride 8==368` on GLM-4.5-Air. Make the slice contiguous. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
mikasenghaas
marked this pull request as ready for review
June 24, 2026 00:28
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.
Problem
With router replay (
trainer.enable_router_replay), the trainer replays inference's expert choices by passingrouted_experts(shape[batch, seq, num_layers, top_k]) into the MoE forward, slicing one layer at a time:That slice is a non-contiguous view (dim-1 stride =
num_layers * top_k). Undertorch.compile, the inductor MoE kernel is compiled expecting a contiguous input, so at runtime it asserts and the trainer crashes on every rank:(
368 = 46 layers × 8 top_kfor GLM-4.5-Air.) Hit on a GLM-4.5-Air (glm4_moe) RL run withenable_router_replay=true+cp=8+ compile.Fix
Make the per-layer slice contiguous before it enters the compiled kernel:
Validation
GLM-4.5-Air RL run reached step 0 with no stride assert after this change (
Mismatch KL 0.0002,Peak Mem 105.6 GiB), and rollouts trained normally. Scoped toglm4_moe; the same slice pattern exists in other MoE models but onlyglm4_moehas been observed to trip the assert.🤖 Generated with Claude Code
Note
Low Risk
Single-line tensor layout fix on an optional router-replay path; no auth, data, or routing logic changes, with a small per-layer copy cost when replay is on.
Overview
Fixes router replay crashes on GLM-4 MoE when
torch.compileis enabled: the per-layerrouted_experts[:, :, layer_idx, :]slice is now.contiguous()before each decoder layer’s MoE forward.Without this, that slice stays a strided view over
[batch, seq, layers, top_k](stride on the token dimension scales withlayers × top_k), which trips the compiled inductor MoE kernel’sassert_size_strideand fails training on every rank. The change is limited tomodeling_glm4_moe.py; other MoE models still use the same slicing pattern unchanged.Reviewed by Cursor Bugbot for commit 7e50017. Bugbot is set up for automated code reviews on this repo. Configure here.