Skip to content

[checkpoint] Load Hugging Face exports named after their index - #5167

Draft
ivy-zhou wants to merge 2 commits into
pr4277from
pr5167
Draft

ivy-zhou wants to merge 2 commits into
pr4277from
pr5167

Conversation

@ivy-zhou

@ivy-zhou ivy-zhou commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary:
TorchCheckpointingManager loads a Hugging Face export as the model item, so torch_checkpointing reads model.safetensors.index.json or model.safetensors. Exports named after another item, such as diffusers' diffusion_pytorch_model.safetensors.index.json for Flux, did not load.

BaseStateDictAdapter gains hf_index_file: the index fqn_to_index_mapping was read from in hf_assets_path, or None without one. StateDictAdapter records model.safetensors.index.json when it finds it, and FluxStateDictAdapter records whichever of its two index names it finds. The HF load names its one item with torch_checkpointing's hf_item_key applied to that index file, and falls back to model, the name this checkpointer writes its own exports under. The probe still checks only the item's index and single-file names, with no directory listing. Exports keep writing under model.

This needs torch_checkpointing.hf.metadata.hf_item_key, which is not yet in a released torch_checkpointing.

Test Plan:

pytest -q tests/unit_tests/cpu/test_torch_checkpointing.py tests/unit_tests/cpu/test_state_dict_adapter.py
pre-commit run --files torchtitan/components/checkpointer/torch_checkpointing.py torchtitan/protocols/state_dict_adapter.py torchtitan/models/flux/model/state_dict_adapter.py tests/unit_tests/cpu/test_torch_checkpointing.py tests/unit_tests/cpu/test_state_dict_adapter.py

52 passed, with torch_checkpointing from source on PYTHONPATH. test_hf_load_reads_a_real_safetensors_export loads real indexed model and diffusion_pytorch_model exports into an nn.Linear with no mocks. FluxStateDictAdapterTest checks hf_index_file is the index found for both names. pre-commit: clean apart from pyrefly's missing torch_checkpointing imports.


Stack created with Sapling. Best reviewed with ReviewStack.

Summary:
Load an initial Hugging Face safetensors checkpoint (`checkpoint.initial_load_in_hf`) through a temporary, model-only `torch_checkpointing` manager.

The model state is converted with the state-dict adapter (`to_hf`), loaded in place, and restored with `from_hf`. The load names the export's format with `metadata_format=HuggingFaceSafetensorsDistributedMetadataFormat`: an export has no native metadata, and `torch_checkpointing` only reads a non-native format the caller names. The model item keeps its `DefaultResharder`, which reshards every safetensors source into the model's DTensors. The temporary manager loads the export as the `model` item, so `torch_checkpointing` reads `model.safetensors.index.json` or `model.safetensors`, and the probe checks those two names, with no directory listing. Exports named after another item, such as diffusers' `diffusion_pytorch_model.safetensors.index.json` for Flux, are left as a TODO for a follow-up. Retention's validity check uses `model`, the name this checkpointer writes its own exports under. The load is `strict`, but only over the weights `to_hf` maps, so parameters an export does not carry, such as new LoRA adapters, keep their initialized values. A native base checkpoint cannot yet initialize a LoRA model, since its strict load requires the adapter parameters; a TODO marks that. Quantized Hugging Face loads are rejected for now.

It also imports `METADATA_FILE_NAME` from `torch_checkpointing.metadata_serialization`, where it now lives.

This needs a `torch_checkpointing` whose `CheckpointManager.load` takes `metadata_format`, with `torch_checkpointing.hf.metadata` (including item-named Hugging Face files and `HF_SAFETENSORS_FILE_TEMPLATE`) and `METADATA_FILE_NAME` in `metadata_serialization`, which are not on `torch_checkpointing` main yet. Until they are, CI's pyrefly check and these tests cannot import them.

Test Plan:
Against a `torch_checkpointing` build that includes the Hugging Face metadata format:
```
pytest -q tests/unit_tests/cpu/test_torch_checkpointing.py tests/unit_tests/cpu/test_state_dict_adapter.py
pre-commit run --files torchtitan/components/checkpointer/torch_checkpointing.py tests/unit_tests/cpu/test_torch_checkpointing.py
```
52 passed. `test_hf_load_reads_a_real_safetensors_export` loads a real indexed `model` export into an `nn.Linear` with no mocks, and `test_hf_load_leaves_parameters_the_export_does_not_carry` loads one into a model with an extra LoRA-like parameter, which keeps its value; loading the export by plain path instead fails with "Missing files for checkpoint items ['model']". pre-commit: clean apart from pyrefly's missing `torch_checkpointing` imports above.
Summary:
`TorchCheckpointingManager` loads a Hugging Face export as the `model` item, so `torch_checkpointing` reads `model.safetensors.index.json` or `model.safetensors`. Exports named after another item, such as diffusers' `diffusion_pytorch_model.safetensors.index.json` for Flux, did not load.

`BaseStateDictAdapter` gains `hf_index_file`: the index `fqn_to_index_mapping` was read from in `hf_assets_path`, or `None` without one. `StateDictAdapter` records `model.safetensors.index.json` when it finds it, and `FluxStateDictAdapter` records whichever of its two index names it finds. The HF load names its one item with `torch_checkpointing`'s `hf_item_key` applied to that index file, and falls back to `model`, the name this checkpointer writes its own exports under. The probe still checks only the item's index and single-file names, with no directory listing. Exports keep writing under `model`.

This needs `torch_checkpointing.hf.metadata.hf_item_key`, which is not yet in a released `torch_checkpointing`.

Test Plan:
```
pytest -q tests/unit_tests/cpu/test_torch_checkpointing.py tests/unit_tests/cpu/test_state_dict_adapter.py
pre-commit run --files torchtitan/components/checkpointer/torch_checkpointing.py torchtitan/protocols/state_dict_adapter.py torchtitan/models/flux/model/state_dict_adapter.py tests/unit_tests/cpu/test_torch_checkpointing.py tests/unit_tests/cpu/test_state_dict_adapter.py
```
52 passed, with `torch_checkpointing` from source on `PYTHONPATH`. `test_hf_load_reads_a_real_safetensors_export` loads real indexed `model` and `diffusion_pytorch_model` exports into an `nn.Linear` with no mocks. `FluxStateDictAdapterTest` checks `hf_index_file` is the index found for both names. pre-commit: clean apart from pyrefly's missing `torch_checkpointing` imports.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant