|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Integration test for checkpoint conversion.""" |
| 16 | + |
| 17 | +import os |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | +import tempfile |
| 21 | +import unittest |
| 22 | +import pytest |
| 23 | + |
| 24 | +pytestmark = [pytest.mark.integration_test] |
| 25 | + |
| 26 | + |
| 27 | +class Qwen3CheckpointConversionTest(unittest.TestCase): |
| 28 | + """Tests HuggingFace to Orbax checkpoint conversion.""" |
| 29 | + |
| 30 | + @pytest.mark.cpu_only |
| 31 | + def test_qwen3_30b_a3b_roundtrip_conversion(self): |
| 32 | + model_name = "qwen3-30b-a3b" |
| 33 | + base_num_decoder_layers = 2 |
| 34 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 35 | + to_mt_cmd = [ |
| 36 | + sys.executable, |
| 37 | + "-m", |
| 38 | + "maxtext.checkpoint_conversion.to_maxtext", |
| 39 | + f"model_name={model_name}", |
| 40 | + f"base_output_directory={tmpdir}", |
| 41 | + f"base_num_decoder_layers={base_num_decoder_layers}", |
| 42 | + "override_model_config=True", |
| 43 | + "scan_layers=False", |
| 44 | + "hardware=cpu", |
| 45 | + "skip_jax_distributed_system=True", |
| 46 | + "checkpoint_storage_use_ocdbt=False", |
| 47 | + "checkpoint_storage_use_zarr3=False", |
| 48 | + "--save_dtype=bfloat16", |
| 49 | + "--lazy_load_tensors=True", |
| 50 | + ] |
| 51 | + env = os.environ.copy() |
| 52 | + env["JAX_PLATFORMS"] = "cpu" |
| 53 | + env["HF_HOME"] = tmpdir |
| 54 | + |
| 55 | + print("Running checkpoint conversion command:", " ".join(to_mt_cmd)) |
| 56 | + # Inherit stdout and stderr from the parent process to stream logs in real time |
| 57 | + subprocess.run(to_mt_cmd, env=env, check=True) |
| 58 | + |
| 59 | + # Verify output directory exists and contains items |
| 60 | + # Output structure: tmpdir/0/items |
| 61 | + expected_dir = os.path.join(tmpdir, "0", "items") |
| 62 | + self.assertTrue(os.path.exists(expected_dir), f"Expected checkpoint directory {expected_dir} does not exist.") |
| 63 | + self.assertTrue(len(os.listdir(expected_dir)) > 0, f"Checkpoint directory {expected_dir} is empty.") |
| 64 | + |
| 65 | + # Roundtrip conversion back to HuggingFace format |
| 66 | + expected_hf_dir = os.path.join(tmpdir, "hf_safetensor", "qwen3-30b-a3b") |
| 67 | + to_hf_cmd = [ |
| 68 | + sys.executable, |
| 69 | + "-m", |
| 70 | + "maxtext.checkpoint_conversion.to_huggingface", |
| 71 | + f"model_name={model_name}", |
| 72 | + f"load_parameters_path={expected_dir}", |
| 73 | + f"base_output_directory={expected_hf_dir}", |
| 74 | + f"base_num_decoder_layers={base_num_decoder_layers}", |
| 75 | + "override_model_config=True", |
| 76 | + "scan_layers=false", |
| 77 | + "weight_dtype=bfloat16", |
| 78 | + "hardware=cpu", |
| 79 | + "skip_jax_distributed_system=True", |
| 80 | + "--override_model_architecture=True", |
| 81 | + ] |
| 82 | + print("Running roundtrip checkpoint conversion command (to HF):", " ".join(to_hf_cmd)) |
| 83 | + subprocess.run(to_hf_cmd, env=env, check=True) |
| 84 | + |
| 85 | + # Verify HF output directory exists and contains safetensors/config files |
| 86 | + self.assertTrue( |
| 87 | + os.path.exists(expected_hf_dir), f"Expected HF checkpoint directory {expected_hf_dir} does not exist." |
| 88 | + ) |
| 89 | + self.assertTrue(len(os.listdir(expected_hf_dir)) > 0, f"HF checkpoint directory {expected_hf_dir} is empty.") |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + unittest.main() |
0 commit comments