Add layer by layer hidden state testing support to forward_pass_logit_checker.py#4173
Draft
snehalv2002 wants to merge 1 commit into
Draft
Add layer by layer hidden state testing support to forward_pass_logit_checker.py#4173snehalv2002 wants to merge 1 commit into
snehalv2002 wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Description
forward_pass_logit_checker.pynow provides the option to compute the layer by layer diff for a model. It's quite common to have to debug issues in new model bringup by inspecting the layer by layer outputs so this change can save developers from writing individual implementations of the same thing. We use Flax Linen nn.Module.sow() which offers a native way to save intermediate tensors and is already in use around maxtext for saving things like moe load balancing losses. Currently we add support for the DeepSeek V2 16B model.How to Use the Verification Tool
Step 1: Generate Golden Reference Hidden States
Use generate_hf_golden_logits.py on a machine with PyTorch/Hugging Face installed to dump reference hidden states for the target prompts:
Step 2: Run MaxText Verification Checker
Run the verifier pointing to the generated golden dataset. Add the --compare_layerwise_hidden_states flag to enable
layerwise assertion:
How to Add Support for a New Model
1. Prerequisites in MaxText
Ensure the JAX model implementation sows its raw block outputs. In your model class (e.g. deepseek.py or your new model's block layers), add the following sowing logic right before returning the layer output:
Additionally, update get_layer_index in tests/utils/forward_pass_logit_checker.py to correctly map the sowed PyTree keys of your new model to linear layer indices.
2. Reference Hidden States Generation
The generator script generate_hf_golden_logits.py uses PyTorch forward hooks to capture hidden states. Depending on how the model is supported by Hugging Face, follow the appropriate case:
Case A: Model is Natively Supported by HF Transformers (e.g. Llama, Mistral, Gemma)
Zero changes are required in the generator script.
• The script uses AutoModelForCausalLM to load the model.
• The hook registration logic dynamically resolves standard repository structures by looking for model.model.layers or
model.layers . Since natively supported models follow this structure, hooks will register automatically.
• Simply run the generator command (Step 1) passing the Hugging Face model ID.
Case B: Model is a Custom Remote Model (e.g. DeepSeek-V4, custom architectures)
If the model uses custom codebase files downloaded dynamically from the Hub (via trust_remote_code=True ), ensure the following:
• The raw activation tensor directly.
• A tuple where the first element ( output[0] ) is the hidden state tensor (this is standard in Hugging Face to return
attention weights alongside activations).
• The script already automatically unpacks tuples.
If the change fixes a bug or a Github issue, please include a link, e.g.,:
FIXES: b/123456
FIXES: #123456
You can also provide a comma-separated list. If you don't want to close a bug but
simply to reference it, use BUGS, e.g.:
BUGS: b/123456
Notice 1: Once all tests pass, the "pull ready" label will automatically be assigned.
This label is used for administrative purposes. Please do not add it manually.
Notice 2: For external contributions, our settings currently require an approval from a MaxText maintainer to trigger CI tests.
Tests
Please describe how you tested this change, and include any instructions and/or
commands to reproduce.
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.