Repository navigation
Conversation
| @abstractmethod | ||
| def from_hf(self, hf_state_dict: dict[str, Any]) -> dict[str, Any]: | ||
| def from_hf( | ||
| self, hf_state_dict: dict[str, Any], quantized: bool = False |
There was a problem hiding this comment.
again see if you agree with #4996 (comment)
here I recommend using from_quantized
- to align with the arg of
get_hf_storage_readerbelow - to make it not ambiguous
There was a problem hiding this comment.
Yes I think @Zhuoxi2000 and I are basically proposing the same thing here, I will update the naming, thanks for flagging it's the same issue!
| @abstractmethod | ||
| def to_hf(self, state_dict: dict[str, Any]) -> dict[str, Any]: | ||
| def to_hf( | ||
| self, state_dict: dict[str, Any], quantized: bool = False |
There was a problem hiding this comment.
how would we use this quantized field? I assume it means to_quantized. If so, could we generate quantized HF state dict, as I didn't see something like QuantizedHFWriter class before.
There was a problem hiding this comment.
This one is used during HF loads actually, there's no quantized checkpoint writing in Titan. The basic flow is that you construct a TorchTitan model / state dict, then you convert to HF side names to load HF state dict into it, and then that allows the QuantizedHuggingFaceStorageReader to read specific quantization keys (e.g. FP8 *.weight with *.weight_scale_inv, or MXFP4 *_blocks with *_scales) so that it can actually do the decoding to full size tensor.
| def to_hf( | ||
| self, state_dict: dict[str, Any], from_quantized: bool = False | ||
| ) -> dict[str, Any]: |
There was a problem hiding this comment.
should we assert from_quantized=False in the function body, as HF doesn't provide quantized checkpoints and we don't support the feature?
| @abstractmethod | ||
| def to_hf(self, state_dict: dict[str, Any]) -> dict[str, Any]: | ||
| def to_hf( | ||
| self, state_dict: dict[str, Any], from_quantized: bool = False |
There was a problem hiding this comment.
to be precise, shall it be
| self, state_dict: dict[str, Any], from_quantized: bool = False | |
| self, state_dict: dict[str, Any], to_quantized: bool = False |
There was a problem hiding this comment.
sounds good, Claude preferred from_quantized because it thought we should always be using it with the from_hf(..., from_quantized=True) function, but I think you're right it doesn't make sense. I will rename.
|
|
||
| def to_hf(self, state_dict: dict[str, Any]) -> dict[str, Any]: | ||
| def to_hf( | ||
| self, state_dict: dict[str, Any], to_quantized_keys: bool = False |
There was a problem hiding this comment.
Did claude explain why it's "quantized keys"? Although quantized dict and non-quantized dict use different keys, ultimately it's the tensor who gets quantized, not keys.
Is to_quantized and from_quantized not accurate?
There was a problem hiding this comment.
Oh sorry, this is actually my preference--I was thinking that basically to_hf actually just generates a state dict with the correct quantization state dict keys--it leaves the actual dequantization to the checkpoint loader, which implements the quantization decoding as a part of loading. So the to_hf'd state_dict has the quantization state dict keys, with the correct dequantized tensor shapes, but not the actual dequantized tensors.
To be honest this isn't the best API, you might imagine that you'd want to provide the quantized keys in one step, and then run the dequantization in a separate step. So for example, you might want to load a quantized checkpoint and then you run the dequant(state_dict). This would separate the concerns a bit better but sacrifice some more memory overhead (because you have the full dequantized tensors allocated at the at same time as the quantized tensors).
There was a problem hiding this comment.
I see. Yeah it's not clear to me what would be the best names. I'll stamp but I'll let claude comment one more time.
update: how do you like claude's suggestions?
def to_hf(self, state_dict, *, hf_quantized: bool = False) -> dict[str, Any]: ...
def from_hf(self, hf_state_dict, *, hf_quantized: bool = False) -> dict[str, Any]: ...
There was a problem hiding this comment.
I think it should be:
def to_hf(self, state_dict, *, to_quantized_layout: bool = False) -> dict[str, Any]: ...
def from_hf(self, hf_state_dict, *, is_quantized_layout: bool = False) -> dict[str, Any]: ...
I think this is clearer because in to_hf, we have not yet applied the quantization layout, and without the to_* prefix it might sounds like we are doing to_hf on some state dict with a quantization layout keys. And then for from_hf, the is_quantized_layout is clear that it's about the hf_state_dict.
Claude really prefers the names to be the same so that you know to use the same value w/ the functions, but I don't buy this.
|
@claude could you give suggestions on the naming of args for both |
|
Claude finished @tianyu-l's task in 37s —— View job Naming suggestion for the
|
Summary: An HF load calls `to_hf` on the destination state dict to get the keys to read. Some quantized checkpoints store a tensor under a different name than the unquantized checkpoint does: compressed-tensors MXFP4 (Kimi K3) stores `X.weight_packed` and `X.weight_scale` where the bf16 checkpoint has `X.weight`. The adapter has no way to tell which one is being read. This adds keyword-only `to_hf(state_dict, *, to_quantized_layout=False)` and `from_hf(hf_state_dict, *, is_quantized_layout=False)` to the state dict adapter protocol, and the DCP load passes its `from_quantized` to both. `to_quantized_layout` makes `to_hf` return the quantized checkpoint's key names; only the names change, and no values are quantized. The storage reader then fills those entries with dequantized tensors, so the dict `from_hf` receives is in the quantized checkpoint's layout without being quantized, and `is_quantized_layout` tells `from_hf` to read the names `to_hf` chose instead of inferring the format from the keys. These are per-call arguments rather than adapter state because the same adapter also converts state dicts for HF saves, RL weight sync, and the conversion scripts, which always want unquantized names. Every in-tree adapter accepts both arguments: - DeepSeek V4 and Kimi K2.7 wrap DeepSeek V3 and forward them. - DeepSeek V3 and GPT-OSS, which already load quantized checkpoints, ignore them for now. - Adapters that cannot load a quantized checkpoint (Flux, Llama 3, Qwen3, Qwen3.5, Muse Glimmer, Kimi K3 and the HF transformers backend) raise `ValueError`. Before, the base storage reader only logged a warning and read the checkpoint as unquantized. The `torch_checkpointing` HF load path is left alone for now. Test Plan: ``` pytest -q tests/unit_tests/cpu/components/checkpointer/test_checkpoint.py tests/unit_tests/cpu/models/test_state_dict_adapter.py tests/unit_tests/cpu/components/checkpointer/test_torch_checkpointing.py SKIP=lychee-link-checker pre-commit run --files <changed files> ``` 128 passed, and pre-commit passes. The new `test_quantized_hf_load_requests_quantized_names` checks that a quantized DCP load calls `to_hf(..., to_quantized_layout=True)` and `from_hf(..., is_quantized_layout=True)`. The new `test_adapters_without_quantized_loads_reject_quantized_layout` checks that the Flux, Llama 3 and Qwen3 adapters raise for both, and fails if the Flux check is removed. This change was written with the help of an AI assistant.
Summary:
An HF load calls
to_hfon the destination state dict to get the keys toread. Some quantized checkpoints store a tensor under a different name than
the unquantized checkpoint does: compressed-tensors MXFP4 (Kimi K3) stores
X.weight_packedandX.weight_scalewhere the bf16 checkpoint hasX.weight. The adapter has no way to tell which one is being read.This adds keyword-only
to_hf(state_dict, *, to_quantized_layout=False)andfrom_hf(hf_state_dict, *, is_quantized_layout=False)to the state dictadapter protocol, and the DCP load passes its
from_quantizedto both.to_quantized_layoutmakesto_hfreturn the quantized checkpoint's keynames; only the names change, and no values are quantized. The storage reader
then fills those entries with dequantized tensors, so the dict
from_hfreceives is in the quantized checkpoint's layout without being quantized, and
is_quantized_layouttellsfrom_hfto read the namesto_hfchose insteadof inferring the format from the keys. These are per-call
arguments rather than adapter state because the same adapter also converts
state dicts for HF saves, RL weight sync, and the conversion scripts, which
always want unquantized names.
Every in-tree adapter accepts both arguments:
them for now.
Qwen3.5, Muse Glimmer, Kimi K3 and the HF transformers backend) raise
ValueError. Before, the base storage reader only logged a warning andread the checkpoint as unquantized.
The
torch_checkpointingHF load path is left alone for now.Test Plan:
128 passed, and pre-commit passes. The new
test_quantized_hf_load_requests_quantized_nameschecks that a quantized DCPload calls
to_hf(..., to_quantized_layout=True)andfrom_hf(..., is_quantized_layout=True). The newtest_adapters_without_quantized_loads_reject_quantized_layoutchecks that theFlux, Llama 3 and Qwen3 adapters raise for both, and fails if the Flux check
is removed.
This change was written with the help of an AI assistant.
Stack created with Sapling. Best reviewed with ReviewStack.