Repository navigation
Conversation
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.
ivy-zhou
force-pushed
the
pr4277
branch
2 times, most recently
from
October 9, 2026 00:30
039da62 to
4cc00df
Compare
This was referenced Nov 27, 2026
Draft
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
TorchCheckpointingManagerloads a Hugging Face export as themodelitem, sotorch_checkpointingreadsmodel.safetensors.index.jsonormodel.safetensors. Exports named after another item, such as diffusers'diffusion_pytorch_model.safetensors.index.jsonfor Flux, did not load.BaseStateDictAdaptergainshf_index_file: the indexfqn_to_index_mappingwas read from inhf_assets_path, orNonewithout one.StateDictAdapterrecordsmodel.safetensors.index.jsonwhen it finds it, andFluxStateDictAdapterrecords whichever of its two index names it finds. The HF load names its one item withtorch_checkpointing'shf_item_keyapplied to that index file, and falls back tomodel, 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 undermodel.This needs
torch_checkpointing.hf.metadata.hf_item_key, which is not yet in a releasedtorch_checkpointing.Test Plan:
52 passed, with
torch_checkpointingfrom source onPYTHONPATH.test_hf_load_reads_a_real_safetensors_exportloads real indexedmodelanddiffusion_pytorch_modelexports into annn.Linearwith no mocks.FluxStateDictAdapterTestcheckshf_index_fileis the index found for both names. pre-commit: clean apart from pyrefly's missingtorch_checkpointingimports.Stack created with Sapling. Best reviewed with ReviewStack.