Fix causality violation: use per-token weights instead of full-sequence mean pooling#3
Open
sippycoder wants to merge 1 commit into
Open
Fix causality violation: use per-token weights instead of full-sequence mean pooling#3sippycoder wants to merge 1 commit into
sippycoder wants to merge 1 commit into
Conversation
…ce mean pooling Dynamic weights (H_pre, H_post, H_res) were computed from H.mean(dim=1), which averages over all sequence positions. For autoregressive LLMs this leaks future token information into the mixing weights applied at position t, breaking causality. Fix: replace H.mean(dim=1).reshape(batch, n*dim) with H.reshape(batch*seq, n*dim) so each token's weights are derived solely from its own hidden state. Weight shapes change from (batch, n) to (batch, seq, n) throughout, matching the paper's intent and reference implementations (tokenbender, VatsaDev). Changes: - module.py / _torch_baseline.py: all three _compute_weights paths (static, fused, separate-projections) now produce per-position weights (batch, seq, n) - _kernels.py: stream_mix and add_residual forward kernels index weights by pid_bs (b*seq+s) instead of b - _backward.py: same index fix in backward kernels; gradient reductions now sum only over d_blocks (dim=2) to preserve the per-position (batch, seq, n) shape - ops.py / _torch_baseline.py: updated einsum signatures and docstrings https://claude.ai/code/session_016YVdHfTQm3GA8aqcj8ws25
Contributor
Author
|
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.
Dynamic weights (H_pre, H_post, H_res) were computed from H.mean(dim=1), which
averages over all sequence positions. For autoregressive LLMs this leaks future
token information into the mixing weights applied at position t, breaking causality.
Fix: replace H.mean(dim=1).reshape(batch, ndim) with H.reshape(batchseq, n*dim)
so each token's weights are derived solely from its own hidden state. Weight shapes
change from (batch, n) to (batch, seq, n) throughout, matching the paper's intent
and reference implementations (tokenbender, VatsaDev).
Changes:
separate-projections) now produce per-position weights (batch, seq, n)
(b*seq+s) instead of b
only over d_blocks (dim=2) to preserve the per-position (batch, seq, n) shape
https://claude.ai/code/session_016YVdHfTQm3GA8aqcj8ws25