Repository navigation
Conversation
zcbenz
approved these changes
Nov 2, 2026
Co-authored-by: Cheng <zcbenz@gmail.com>
This branch has not been deployed
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.
Observation:
For a linear layer we do matrix multiplication in the following way:
x @ W.T, x \in [M, K], W \in [N, K], K is fast moving dim, x is the input, W is the weight.In vjp we compute the gradient of the loss with respect of the weights as:
dW = x^T @ dY(dY incoming gradient \in [M, N]) -> [K, N] gradient, N is fast moving. Then Transpose::vjp just changing the strides, shape is [N, K], N is still fast moving.=> there is a mismatch between the layout of dW (row major) and layout of W that it column major
=> we use suboptimal strided kernel to do optimizer step W-= dW (simplified).
Fix:
This PR proposes to write gradients in a matching layout to the second operand: if B was produced using Transpose primitive we compute
dW = dY^T @ x, else the opposite.Results:
For 7B pretraining on 1 B200 with batch size 1, context 8192 this change results in 17500 tok/s -> 19400 tok/s throughput change.