|
| 1 | +# Checkpoint conversion utilities |
| 2 | + |
| 3 | +This guide provides instructions for using the scripts that convert model checkpoints bidirectionally between Hugging Face and MaxText formats. |
| 4 | + |
| 5 | +## Supported models |
| 6 | + |
| 7 | +The following models are supported: |
| 8 | + |
| 9 | +- Gemma2 (2B, 9B, 27B). |
| 10 | +- Gemma3 multimodal (4B, 12B, 27B). |
| 11 | +- Qwen3 (0.6B, 4B, 8B, 14B, 32B). |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | +- Hugging Face requires Pytorch. |
| 15 | +- Hugging Face model checkpoints require local disk space. |
| 16 | + - The model files are always downloaded to a disk cache first before being loaded into memory (for more info, please consult Hugging Face [docs](https://huggingface.co/docs/accelerate/en/concept_guides/big_model_inference)). The default local storage path for Hugging Face models is $HOME/.cache/huggingface/hub |
| 17 | + |
| 18 | +## Hugging Face to MaxText |
| 19 | + |
| 20 | +Use the `to_maxtext.py` script to convert a Hugging Face model into a MaxText checkpoint. The script will automatically download the specified model from the Hugging Face Hub, perform conversion, and save converted checkpoints to given output directory. |
| 21 | + |
| 22 | +\*\**For a complete example, see the test script at [`end_to_end/tpu/qwen3/4b/test_qwen3.sh`](../../../end_to_end/tpu/qwen3/4b/test_qwen3.sh) and [`end_to_end/tpu/gemma3/4b/test_gemma3_unified.sh`](../../../end_to_end/tpu/gemma3/4b/test_gemma3_unified.sh).* |
| 23 | + |
| 24 | +### Usage |
| 25 | + |
| 26 | +The following command demonstrates how to run the conversion. You must provide your Hugging Face token in the `MaxText/configs/base.yml` file (hf_access_token). |
| 27 | + |
| 28 | +```bash |
| 29 | +python -m MaxText.utils.ckpt_conversion.to_maxtext MaxText/configs/base.yml \ |
| 30 | + model_name=<model-name> \ |
| 31 | + base_output_directory=<gcs-path-to-save-checkpoint> \ |
| 32 | + hf_access_token=<your-hf-token> \ |
| 33 | + use_multimodal=false \ |
| 34 | + scan_layers=false |
| 35 | +``` |
| 36 | + |
| 37 | +**Key arguments:** |
| 38 | + |
| 39 | + * `model_name`: The model identifier, which should be defined in `MaxText/utils/utils.py`. |
| 40 | + * `scan_layers`: Indicates if the output checkpoint is [scanned](https://github.com/AI-Hypercomputer/maxtext/blob/main/getting_started/checkpoints.md) (scan_layers=true) or unscanned (scan_layers=false). |
| 41 | + * `use_multimodal`: Indicates if multimodality is used, important for Gemma3. |
| 42 | + * `hf_access_token`: Your Hugging Face token. |
| 43 | + * `base_output_directory`: The path where the converted Orbax checkpoint will be stored; it can be Googld Cloud Storage (GCS) or local. If not set, the default output directory is `Maxtext/tmp`. |
| 44 | + |
| 45 | +\*\**It only converts the official version of Hugging Face model. You can refer the supported official version in HF_IDS in `MaxText/utils/ckpt_conversion/utils/utils.py`* |
| 46 | + |
| 47 | +## MaxText to Hugging Face |
| 48 | + |
| 49 | +Use the `to_huggingface.py` script to convert a MaxText checkpoint into the Hugging Face format. This is useful for sharing your models or integrating them with the Hugging Face ecosystem. |
| 50 | +\*\**For a complete example, see the test script at [`end_to_end/tpu/qwen3/4b/test_qwen3_to_hf.sh`](../../../end_to_end/tpu/qwen3/4b/test_qwen3_to_hf.sh).* |
| 51 | + |
| 52 | +### Usage |
| 53 | + |
| 54 | +The following command converts a MaxText checkpoint and saves it locally, to GCS, or uploads it directly to the Hugging Face Hub. |
| 55 | + |
| 56 | +```bash |
| 57 | +python -m MaxText.utils.ckpt_conversion.to_huggingface MaxText/configs/base.yml \ |
| 58 | + model_name=<MODEL_NAME> \ |
| 59 | + load_parameters_path=<path-to-maxtext-checkpoint> \ |
| 60 | + base_output_directory=<path-to-save-converted-checkpoint> \ |
| 61 | + scan_layers=false \ |
| 62 | + use_multimodal=false \ |
| 63 | + hf_access_token=<your-hf-token> \ |
| 64 | +``` |
| 65 | + |
| 66 | +**Key arguments:** |
| 67 | + |
| 68 | + * `load_parameters_path`: The path to the source MaxText Orbax checkpoint (e.g., `gs://your-bucket/maxtext-checkpoint/0/items`). |
| 69 | + * `model_name`: The corresponding model name in the MaxText configuration (e.g., `qwen3-4b`). |
| 70 | + * `scan_layers`: Indicates if the output checkpoint is [scanned](https://github.com/AI-Hypercomputer/maxtext/blob/main/getting_started/checkpoints.md) (scan_layers=true) or unscanned (scan_layers=false). |
| 71 | + * `hf_access_token`: Your Hugging Face token. |
| 72 | + * `use_multimodal`: Indicates if multimodality is used, important for Gemma3. |
| 73 | + * `base_output_directory`: The path where the converted Orbax checkpoint will be stored; it can be Googld Cloud Storage (GCS), Hugging Face Hub or local. If not set, the default output directory is `Maxtext/tmp`. |
| 74 | + |
| 75 | + |
| 76 | +## Verifying conversion correctness |
| 77 | + |
| 78 | +To ensure the conversion was successful, you can use the `MaxText/tests/forward_pass_logit_checker.py` script. It runs a forward pass on both the original and converted models and compares the output logits to verify conversion. It is used to verify the bidirectional conversion. |
| 79 | + |
| 80 | +### Usage |
| 81 | + |
| 82 | +```bash |
| 83 | +python3 -m MaxText.tests.forward_pass_logit_checker MaxText/configs/base.yml \ |
| 84 | + tokenizer_path=assets/<tokenizer> \ |
| 85 | + load_parameters_path=<path-to-maxtext-checkpoint> \ |
| 86 | + model_name=<MODEL_NAME> \ |
| 87 | + scan_layers=false \ |
| 88 | + use_multimodal=false \ |
| 89 | + --run_hf_model=True \ |
| 90 | + --hf_model_path=<path-to-HF-checkpoint> \ |
| 91 | + --max_kl_div=0.015 \ |
| 92 | +``` |
| 93 | + |
| 94 | +**Key arguments:** |
| 95 | + |
| 96 | + * `load_parameters_path`: The path to the source MaxText Orbax checkpoint (e.g., `gs://your-bucket/maxtext-checkpoint/0/items`). |
| 97 | + * `model_name`: The corresponding model name in the MaxText configuration (e.g., `qwen3-4b`). |
| 98 | + * `scan_layers`: Indicates if the output checkpoint is scanned (scan_layers=true) or unscanned (scan_layers=false). |
| 99 | + * `use_multimodal`: Indicates if multimodality is used. |
| 100 | + * `--run_hf_model`: Indicates if loading Hugging Face model from the hf_model_path. If not set, it will compare the maxtext logits with pre-saved golden logits. |
| 101 | + * `--hf_model_path`: The path to the Hugging Face checkpoint. |
| 102 | + * `--max_kl_div`: Max KL divergence tolerance during comparisons. |
| 103 | + |
| 104 | +**Example successful conversion verification:** |
| 105 | + |
| 106 | +Here is part of the output of forward_pass_logit_checker for the gemma2-2b. |
| 107 | + |
| 108 | +``` |
| 109 | +--- Prompt: What is the --- |
| 110 | +
|
| 111 | +--- MaxText model top 10 tokens --- |
| 112 | +| Token ID | Token | Score | |
| 113 | +|------------|----------------------|------------| |
| 114 | +| 5830 | difference | 27.2500 | |
| 115 | +| 1963 | best | 26.6250 | |
| 116 | +| 5316 | average | 26.3750 | |
| 117 | +| 2669 | change | 26.1250 | |
| 118 | +| 12070 | percentage | 26.1250 | |
| 119 | +| 1618 | value | 25.8750 | |
| 120 | +| 1546 | most | 25.7500 | |
| 121 | +| 66202 | molar | 25.5000 | |
| 122 | +| 3051 | total | 25.5000 | |
| 123 | +| 1503 | name | 25.3750 | |
| 124 | +
|
| 125 | +
|
| 126 | +--- HF model top 10 tokens --- |
| 127 | +| Token ID | Token | Score | |
| 128 | +|------------|----------------------|------------| |
| 129 | +| 5830 | difference | 27.2500 | |
| 130 | +| 1963 | best | 26.6250 | |
| 131 | +| 5316 | average | 26.3750 | |
| 132 | +| 12070 | percentage | 26.1250 | |
| 133 | +| 2669 | change | 26.1250 | |
| 134 | +| 1618 | value | 25.8750 | |
| 135 | +| 1546 | most | 25.7500 | |
| 136 | +| 66202 | molar | 25.5000 | |
| 137 | +| 3051 | total | 25.5000 | |
| 138 | +| 6187 | purpose | 25.3750 | |
| 139 | +
|
| 140 | +
|
| 141 | +--- Similarity Metrics of Top Tokens --- |
| 142 | +| Metric | Value | |
| 143 | +|--------------------------------|----------------------| |
| 144 | +| overlap_count | 9/10 | |
| 145 | +| jaccard_similarity | 0.8181818181818182 | |
| 146 | +| rank_agreement_percentage | 70.0 | |
| 147 | +
|
| 148 | +
|
| 149 | +Average KL divergence per token (D_KL(P_golden || Q_model)): 0.000409 |
| 150 | +
|
| 151 | +Max KL divergence for a single token in the set: 0.003497 |
| 152 | +``` |
| 153 | +----- |
| 154 | + |
| 155 | +## Adding support for new models |
| 156 | +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. |
| 157 | + |
| 158 | +1. **Add parameter mappings**: |
| 159 | +- In [`utils/param_mapping.py`](./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. |
| 160 | +- In [`utils/param_mapping.py`](./utils/param_mapping.py), add the `hook_fn` logic (`def {MODEL}_MAXTEXT_TO_HF_PARAM_HOOK_FN`). This is the transformation needed per layer. |
| 161 | +2. **Add Hugging Face weights Shape**: In [`utils/hf_shape.py`](./utils/hf_shape.py), define the tensor shape of Hugging Face format (`def {MODEL}_HF_WEIGHTS_TO_SHAPE`). This is used to ensure the tensor shape is matched after to_huggingface conversion. |
| 162 | +3. **Register model key**: In [`utils/utils.py`](./utils/utils.py), add the new model key in `HF_IDS`. |
| 163 | +4. **Add transformer config**: In [`utils/hf_model_configs.py`](./utils/hf_model_configs.py), add the `transformers.Config` object, describing the Hugging Face model configuration (defined in ['MaxText/configs/models'](../configs/models)). **Note**: This configuration must precisely match the MaxText model's architecture. |
| 164 | + |
| 165 | +Here is an example [PR to add support for gemma3 multi-modal model](https://github.com/AI-Hypercomputer/maxtext/pull/1983) |
| 166 | + |
| 167 | +## Debugging tips |
| 168 | + |
| 169 | +If a converted checkpoint loads without errors but produces incorrect output, consider these common issues: |
| 170 | + |
| 171 | + * **Symptom**: The model generates garbage or nonsensical tokens. |
| 172 | + |
| 173 | + * **Potential Cause**: The query/key/value (Q/K/V) or Out vectors weights were likely reshaped incorrectly during conversion. |
| 174 | + |
| 175 | + * **Symptom**: The model generates repetitive text sequences. |
| 176 | + |
| 177 | + * **Potential Cause**: The layer normalization parameters may have been converted incorrectly. |
0 commit comments