fix: fused_experts observer shape bug and router tuple crash#12
Open
naoufelito wants to merge 1 commit intoCerebrasResearch:mainfrom
Open
fix: fused_experts observer shape bug and router tuple crash#12naoufelito wants to merge 1 commit intoCerebrasResearch:mainfrom
naoufelito wants to merge 1 commit intoCerebrasResearch:mainfrom
Conversation
a7e6d5b to
dc7d47b
Compare
Fix two bugs in the fused_experts branch of MoETransformerObserver: 1. module.router(flat_input) returns a tuple for some routers (e.g. Llama4Router returns (scores, logits)), causing torch.topk to fail with a TypeError. Replace with F.linear(flat_input, router.weight, router.bias) to get raw logits directly. 2. router_scores.size(0) equals total_tokens, not num_experts, causing routed_in to have shape (total_tokens^2, hidden_dim) instead of (num_experts * total_tokens, hidden_dim). The subsequent .view() then crashes with a RuntimeError. Replace the broken gather-based construction with flat_input.repeat(num_experts, 1). Fixes CerebrasResearch#11
dc7d47b to
af642b2
Compare
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.
Summary
Fixes two bugs in the
fused_expertsbranch ofMoETransformerObserver._hook_fn:Router tuple crash:
module.router(flat_input)may return a tuple (e.g.Llama4Routerreturns(scores, logits)), causingtorch.topkto fail with aTypeError. Now handled by checkingisinstance(router_output, tuple)and extracting the last element.Shape mismatch:
router_scores.size(0)equalstotal_tokens, notnum_experts(router output shape is(total_tokens, num_experts), not(num_experts, total_tokens)as the comment claimed). This causedrouted_into have shape(total_tokens², hidden_dim)instead of(num_experts * total_tokens, hidden_dim), and the subsequent.view(num_experts, ...)crashes with aRuntimeError. Replaced the broken gather-based construction withflat_input.repeat(num_experts, 1).Fixes #11
Test plan
tests/test_fused_observer_shape_bug.pywithtotal_tokens=10andnum_experts=4(crashes before fix, passes after)