Skip to content

Commit 7f35e51

Browse files
committed
Add unified tool to inspect checkpoint structures
1 parent 98c79e4 commit 7f35e51

3 files changed

Lines changed: 412 additions & 11 deletions

File tree

docs/guides/checkpointing_solutions/convert_checkpoint.md

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,41 @@ Max KL divergence for a single token in the set: 0.003497
236236

237237
______________________________________________________________________
238238

239-
## Troubleshooting and Development
239+
## Development and Troubleshooting
240+
241+
### Inspection Tools
242+
243+
We provide a unified inspection tool In [`inspect_checkpoint.py`](https://github.com/AI-Hypercomputer/maxtext/blob/main/src/maxtext/checkpoint_conversion/inspect_checkpoint.py) to help you view model structures. This is highly useful for both development and troubleshooting.
244+
245+
- **Memory-efficient:** Operates without requiring heavy compute or massive RAM allocation.
246+
- **Detailed outputs:** Prints tensor keys and shapes. Optional: data types with `--check_dtype=True` flag.
247+
248+
**Tool 1 (HF Inspector)**. To inspect the HuggingFace checkpoint structure, from either `.safetensors` or `.pth` files:
249+
250+
```
251+
python src/maxtext/checkpoint_conversion/inspect_checkpoint.py hf --path <local_hf_path> --format <safetensors | pth>
252+
```
253+
254+
**Tool 2 (MaxText Inspector)**. To inspect the MaxText model structure:
255+
256+
```
257+
python src/maxtext/checkpoint_conversion/inspect_checkpoint.py maxtext model_name=<maxtext_model_name> scan_layers=<True | False>
258+
```
259+
260+
**Tool 3 (Orbax Inspector)**. To inspect the Orbax checkpoint:
261+
262+
```
263+
python src/maxtext/checkpoint_conversion/inspect_checkpoint.py orbax --path <local_orbax_path | gcs_orbax_path>
264+
```
240265

241266
### Adding New Models
242267

243268
To extend conversion support to a new model architecture, you must define its specific parameter and configuration mappings. The conversion logic is decoupled, so you only need to modify the mapping files.
244269

245270
1. **Add parameter mappings**:
246271

272+
- As the first step, we inspect the checkpoint structures. To see the HuggingFace checkpoint structure, use **Tool 1 (HF Inspector)**. To see the MaxText model structure, use **Tool 2 (MaxText Inspector)**.
273+
247274
- In [`utils/param_mapping.py`](https://github.com/AI-Hypercomputer/maxtext/blob/main/src/maxtext/checkpoint_conversion/utils/param_mapping.py), add the parameter name mappings(`def {MODEL}_MAXTEXT_TO_HF_PARAM_MAPPING`). This is the 1-to-1 mappings of parameters names per layer.
248275

249276
- In [`utils/param_mapping.py`](https://github.com/AI-Hypercomputer/maxtext/blob/main/src/maxtext/checkpoint_conversion/utils/param_mapping.py), add the `hook_fn` logic (`def {MODEL}_MAXTEXT_TO_HF_PARAM_HOOK_FN`). This is the transformation needed per layer.
@@ -258,11 +285,22 @@ Here is an example [PR to add support for gemma3 multi-modal model](https://gith
258285

259286
### Common Errors
260287

261-
- "Type ShapeDtypeStruct is not a valid JAX type" or generic **PyTree structure/shape mismatches** (e.g., Orbax reporting `"X/Y paths matched"`, such as `143/145 paths`):
262-
This is almost always caused by a mismatch in the `scan_layers` configuration between the checkpoint conversion script (e.g., `to_maxtext.py` or `to_huggingface.py`) and the trainer/inference runner (e.g., `train.py`).
288+
- **Error:** When loading a converted checkpoint, `Type ShapeDtypeStruct is not a valid JAX type`.
289+
290+
- **Cause (most common): Structure mismatch** between the converted checkpoint and the MaxText model.
291+
292+
- **Solution:** To see the MaxText model structure, use **Tool 2 (MaxText Inspector)**. To inspect the checkpoint, use **Tool 3 (Orbax Inspector)**.
293+
294+
- **Error:** `Type ShapeDtypeStruct is not a valid JAX type` or generic **PyTree structure/shape mismatches** (e.g., Orbax reporting `"X/Y paths matched"`, such as `143/145 paths`).
295+
296+
- **Cause: Configuration mismatch** (e.g., `scan_layers`) between the checkpoint conversion script (e.g., `to_maxtext.py` or `to_huggingface.py`) and the trainer/inference runner (e.g., `train.py`).
263297

264298
- **Solution:** Ensure the `scan_layers` flag is set to the exact same value (`True` or `False`) in both the conversion command and your training/execution command.
265299

266-
- If the converted checkpoint loads without errors but produces nonsensical output, likely an error in the Q/K/V weight reshaping logic during conversion.
300+
- **Error:** The converted checkpoint loads without errors but produces nonsensical output.
301+
302+
- **Cause:** There is likely an error in the Q/K/V weight reshaping logic during conversion.
303+
304+
- **Error:** The model generates repetitive text sequences.
267305

268-
- If the model generates repetitive text sequences, check if layer normalization parameters were mapped correctly.
306+
- **Cause:** Layer normalization parameters were likely not mapped correctly.

docs/guides/model_bringup.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,40 @@ This step can be bypassed if the current MaxText codebase already supports all c
4242

4343
## 3. Checkpoint Conversion
4444

45-
While most open-source models are distributed in Safetensors or PyTorch formats, MaxText requires conversion to the [Orbax](https://orbax.readthedocs.io/en/latest/) format.
45+
While most open-source models are distributed in Safetensors or PyTorch formats, MaxText requires conversion to the [Orbax](https://orbax.readthedocs.io/en/latest) format.
4646

47-
There are [two primary formats](../reference/core_concepts/checkpoints.md) for Orbax checkpoints within MaxText, and while both are technically compatible with training and inference, we recommend following these performance-optimized guidelines:
47+
There are [two primary formats](../reference/core_concepts/checkpoints.md) for Orbax checkpoints within MaxText. While both are technically compatible with training and inference, we recommend following these performance-optimized guidelines:
4848

4949
- **Scanned Format**: Recommended for **training** as it stacks layers for efficient processing via `jax.lax.scan`. To enable this, set `scan_layers=True`.
5050
- **Unscanned Format**: Recommended for **inference** to simplify loading individual layer parameters. To enable this, set `scan_layers=False`.
5151

5252
### 3.1 Create Mapping
5353

54-
Success starts with a clear map. You must align the parameter names from your source checkpoints (Safetensors/PyTorch) with the corresponding MaxText internal names.
54+
To successfully convert a model, you must define the exact mapping between the parameter names in your source checkpoints (Safetensors/PyTorch) and the corresponding MaxText internal names.
5555

56-
- You can print out the keys and shapes of your original `.safetensors` or `.pth` files.
57-
- To see the target structure, you can initiate a pre-training run to save a randomly initialized checkpoint for inspection.
56+
We provide a unified utility [`inspect_checkpoint.py`](https://github.com/AI-Hypercomputer/maxtext/blob/main/src/maxtext/checkpoint_conversion/inspect_checkpoint.py) to help you view and compare these structures.
57+
58+
(1) **HF Inspector**. To see the HuggingFace checkpoint structure, you can print out the keys and shapes of your original `.safetensors` or `.pth` files.
59+
60+
```
61+
python src/maxtext/checkpoint_conversion/inspect_checkpoint.py hf --path <local_hf_path> --format <safetensors | pth>
62+
```
63+
64+
(2) **MaxText Inspector**. View the expected parameter structure of the target MaxText model:
65+
66+
```
67+
python src/maxtext/checkpoint_conversion/inspect_checkpoint.py maxtext model_name=<maxtext_model_name> scan_layers=<True | False>
68+
```
69+
70+
(3) **Orbax Inspector** (Optional). If you have already saved an Orbax checkpoint during pretraining, you can inspect its structure directly:
71+
72+
```
73+
python src/maxtext/checkpoint_conversion/inspect_checkpoint.py orbax --path <local_orbax_path | gcs_orbax_path>
74+
```
5875

5976
### 3.2 Write Script
6077

61-
Use existing model scripts within the repository as templates to tailor the conversion logic for your specific architecture. We strongly recommended to use the [checkpoint conversion utility](checkpointing_solutions/convert_checkpoint.md) rather than [standalone scripts](https://github.com/AI-Hypercomputer/maxtext/tree/main/src/maxtext/checkpoint_conversion/standalone_scripts).
78+
Use existing model scripts within the repository as templates to tailor the conversion logic for your specific architecture. We strongly recommended to use the [checkpoint conversion utility](checkpointing_solutions/convert_checkpoint.md), rather than [standalone scripts](https://github.com/AI-Hypercomputer/maxtext/tree/main/src/maxtext/checkpoint_conversion/standalone_scripts).
6279

6380
### 3.3 Verify Compatibility
6481

0 commit comments

Comments
 (0)