Skip to content

Add unified tool to inspect checkpoint structures#3139

Merged
copybara-service[bot] merged 1 commit into
mainfrom
shuningjin-ckpt-structure
Jun 18, 2026
Merged

Add unified tool to inspect checkpoint structures#3139
copybara-service[bot] merged 1 commit into
mainfrom
shuningjin-ckpt-structure

Conversation

@shuningjin

@shuningjin shuningjin commented Feb 13, 2026

Copy link
Copy Markdown
Collaborator

Description

Unified script to inspect checkpoint structures for HF/MaxText/Orbax, to help with bringup and debugging.

Fix: b/484416862

Feature: Inspection Tool

A unified command-line tool to inspect checkpoint structures and tensor shapes. It is designed to be lightweight and minimize memory/compute costs across three supported modes:

1. HuggingFace Checkpoints (Locally downloaded file)

  • Input: local path (safetensors or pth).
  • Cost - safetensors: Lightweight with near-zero cost. Parses JSON file headers to read metadata instantly without allocating RAM.
  • Cost - pth: Per-tensor load with small cost. Deserializes the file to load weights per-tensor (via mmap). Incurs a time cost for deserialization, but the host memory footprint is strictly constrained.
  • Prints: Param key / shape.

2. MaxText Model Architecture (On-the-fly)

  • Input: model name, scan mode. Run on CPU or TPU.
  • Cost: Lightweight with near-zero cost. Traces JAX shapes abstractly to evaluate theoretical parameters dynamically without executing compute or allocating VRAM/HBM. Mechanism: jax.eval_shape
  • Prints: Param key / shape + total parameter count.

3. Orbax Checkpoints (Pre-saved disk/GCS file)

  • Input: GCS path or local path. Run on CPU or TPU.
  • Cost: Lightweight with near-zero cost. Reads the structural metadata tree instantly without pulling the underlying heavy TensorStore data chunks. Mechanism: Read structural metadata.
  • Prints: Param key / shape.

Usage Examples

[Mode 1: HuggingFace]   
  python src/maxtext/checkpoint_conversion/inspect_checkpoint.py hf \
      --path <local_hf_path> --format <safetensors | pth>

[Mode 2: MaxText Architecture] 
  python src/maxtext/checkpoint_conversion/inspect_checkpoint.py maxtext \
      model_name=<maxtext_model_name> scan_layers=<True | False>
      (Optional: other maxtext config)

[Mode 3: Orbax]        
  python src/maxtext/checkpoint_conversion/inspect_checkpoint.py orbax \
      --path <local_orbax_path | gcs_orbax_path>

Additional Features:

  • --check_dtype: bool, default to False. Appends the data type of each tensor to the output.
  • --output_file: str, default to empty. Path to save the output structure.

Add Documentation: When to use Tool

  1. docs/guides/checkpointing_solutions/convert_checkpoint.md
  • In Section Development and Troubleshooting, introduced a new ### Inspection Tools section documenting the inspect_checkpoint.py utility. It provides clear command-line examples for checking the three supported checkpoint formats.
  • In Section Adding New Models, introduced using Inspection Tools to create mappings.
  • In Section Common Errors, introduced using Inspection Tools to debug. The most common error (Type ShapeDtypeStruct is not a valid JAX type) is caused by structural mismatches.
  1. docs/guides/model_bringup.md
  • In Section 3.1 Create Mapping, introduced using Inspection Tools to create mappings.

Tests

1 HF checkpoint, locally downloaded

safetensors: no cost

python -m maxtext.checkpoint_conversion.inspect_checkpoint hf \
--path ~/deepseek2-16b/hf-bf16 \
--format safetensors \
--check_dtype True --output_file ~/checkpoint/test.txt  # Optional
python -m maxtext.checkpoint_conversion.inspect_checkpoint hf \
--path /mnt/disks/ds32/deepseek3.2/hf-bf16 \
--format safetensors \
--check_dtype True --output_file ~/checkpoint/test.txt  # Optional

pth: low cost with per-tensor load

python -m maxtext.checkpoint_conversion.inspect_checkpoint hf \
--path ~/llama2-7b/hf-bf16 \
--format pth \
--check_dtype True --output_file ~/checkpoint/test.txt  # Optional

2 MaxText model, on-fly, no cost

python -m maxtext.checkpoint_conversion.inspect_checkpoint maxtext \
model_name=deepseek2-16b scan_layers=True \
--check_dtype True --output_file ~/checkpoint/test.txt  # Optional
python -m maxtext.checkpoint_conversion.inspect_checkpoint maxtext \
model_name=deepseek2-16b scan_layers=False \
--check_dtype True --output_file ~/checkpoint/test.txt  # Optional

3 Orbax checkpoint, pre-saved, no cost

python -m maxtext.checkpoint_conversion.inspect_checkpoint orbax  \
--path gs://maxtext-deepseek/deepseek3-671b/2025-03-31/scanned/0/items \
--check_dtype True --output_file ~/checkpoint/test.txt  # Optional

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@codecov

codecov Bot commented Feb 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 144 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...axtext/checkpoint_conversion/inspect_checkpoint.py 0.00% 144 Missing ⚠️

📢 Thoughts on this report? Let us know!

@RissyRan RissyRan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making our process better!

Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py Outdated
Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py Outdated
Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py Outdated
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Hi @RissyRan, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## 📋 Review Summary

This Pull Request introduces a unified checkpoint inspector tool for HuggingFace, MaxText architecture, and Orbax. The tool is a great addition for debugging model bring-ups. However, there is a significant bug in the MaxText mode where layer indices are ignored during path flattening, leading to incomplete output. Additionally, there are opportunities to improve memory efficiency when inspecting large checkpoints.

🔍 General Feedback

  • Bug Fix Required: The MaxText architecture inspection currently collapses all layers into a single set of keys because it ignores SequenceKey indices. This needs to be addressed to provide a complete view of the model structure.
  • Memory Efficiency: For safetensors, using get_slice instead of get_tensor avoids unnecessary data loading. For PyTorch checkpoints, better handling of large files and common state-dict wrappers would make the tool more robust.
  • Consistency: Standardizing separators across different modes (e.g., using . everywhere) would improve the user experience.

Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py Outdated
Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py Outdated
Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py
Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py
Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py
Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py
@shuningjin shuningjin marked this pull request as ready for review June 17, 2026 02:49
@shuningjin shuningjin force-pushed the shuningjin-ckpt-structure branch 2 times, most recently from 25dd2cf to cd8b454 Compare June 17, 2026 03:26
Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py
Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py Outdated
Comment thread src/maxtext/checkpoint_conversion/inspect_checkpoint.py Outdated

@parambole parambole left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. JAX path flattening currently ignores SequenceKey (layer indices in lists) and GetAttrKey because it filters strictly with hasattr(k, "key"). This collapses all layers (e.g., layer_0 and layer_1 become just layer), causing incorrect parameter counts and output. We should use the existing helper param_key_parts_from_path(path_tuple) to format keys correctly. When scan_layers=False
  2. "attention=dot_product" after remaining_args overrides any user-specified CLI flags (e.g., attention=flash). We should prepend defaults before remaining_args.
  3. Remove a leftover debug prints.

@shuningjin shuningjin force-pushed the shuningjin-ckpt-structure branch 2 times, most recently from 7f35e51 to dc8b3a6 Compare June 18, 2026 01:07
@shuningjin shuningjin requested a review from parambole June 18, 2026 01:22

@parambole parambole left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you adding this awesome tool.

@hengtaoguo hengtaoguo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome tool, thanks Shuning!

As a nice-to-have feature, I wonder if we could add a --save_to_txt=<path> flag. In addition to printing logs to the terminal, it would be great to dump them into a separate text file. This would allow both humans and agents to review the files and analyze the differences more easily later on. WDYT?

Does the tool currently support this? If not, we can definitely treat this as a follow-up item.

@shuningjin shuningjin force-pushed the shuningjin-ckpt-structure branch 2 times, most recently from f7c37b2 to c09fcb3 Compare June 18, 2026 11:50
@shuningjin

Copy link
Copy Markdown
Collaborator Author

As a nice-to-have feature, I wonder if we could add a --save_to_txt=<path> flag. In addition to printing logs to the terminal, it would be great to dump them into a separate text file. This would allow both humans and agents to review the files and analyze the differences more easily later on. WDYT?

Thanks @hengtaoguo for the suggestion! I have enabled saving to output, with flag --output_file.

Tests: I have rerun the tests and included the saved file in the PR description.

@shuningjin shuningjin force-pushed the shuningjin-ckpt-structure branch from d002758 to 9caffac Compare June 18, 2026 18:41
@copybara-service copybara-service Bot merged commit ecdecc3 into main Jun 18, 2026
32 of 33 checks passed
@copybara-service copybara-service Bot deleted the shuningjin-ckpt-structure branch June 18, 2026 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants