feat(layers): implement custom shape-aligned attention and MoE primit…#4200
Open
katyaoussar wants to merge 1 commit into
Open
feat(layers): implement custom shape-aligned attention and MoE primit…#4200katyaoussar wants to merge 1 commit into
katyaoussar wants to merge 1 commit into
Conversation
…ives for DeepSeek-V4
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.
This PR implements a custom, JAX/Flax NNX integration of the DeepSeek-V4 attention mechanism and core primitives.
Here is a concise summary of what was done to the four core components:
1- RoPE (Rotary Embeddings): Implemented custom interleaved channel frequency pairing ([-x1, x0, -x3, x2]) and partial dimension rotation for precise token position encoding.
2- Grouped Linear: Created parallel, multi-group projection layers to efficiently mix attention head outputs in a single compilable step.
3- MoE (Mixture of Experts): Built the learned Top-K expert routing mechanism along with the custom SqrtSoftplus load-balancing loss to ensure stable training routing.
4- Attention Block: Engineered a unified, TPU-optimized module combining local sliding window attention, overlapping compressed sparse attention (CSA) with a causal indexer, and heavily compressed history attention (HCA) — using block-bias masking to avoid dynamic gather memory stalls.