Fix zero_to_fp32 --safe_serialization on models with tied weights (#8376)
`zero_to_fp32 --safe_serialization` aborts on any checkpoint whose model
ties weights, which is the usual embedding / `lm_head` pair, so most
decoder-only models are affected:
```
RuntimeError:
Some tensors share memory, this will lead to duplicate memory on disk and
potential differences when loading them again: [{'lm_head.weight', 'model.embed_tokens.weight'}].
```
## Root cause
`_get_fp32_state_dict_from_zero{2,3}_checkpoint` recovers a tied
parameter by pointing it at the tensor it is tied to:
```python
for pair in zero_model_states[0].shared_params:
if pair[1] in state_dict:
state_dict[pair[0]] = state_dict[pair[1]]
```
`to_torch_tensor` then deliberately keeps that aliasing so the pair
costs one tensor rather than two:
```python
if tensor_id in converted_tensors: # shared tensors
shared_tensor = torch_state_dict[converted_tensors[tensor_id]]
torch_state_dict[name] = shared_tensor
```
That is exactly what `safetensors.torch.save_file` refuses. The pickle
path is fine because `torch.save` stores shared storage once and
restores the aliasing on load.
Note that ordinary (untied) parameters are *not* affected even though
they are all views into one flat vector: safetensors only rejects
tensors that overlap, and sibling `narrow()` slices are disjoint. Only
the tied pair, which is the same region twice, trips it.
## Fix
`to_torch_tensor` grows a `share_tensors` flag, and
`convert_zero_checkpoint_to_state_dict` passes `share_tensors=not
safe_serialization` at both call sites, so the tied entry gets its own
copy on the safetensors path and nowhere else. Passing it to the
`return_empty_tensor=True` call as well means
`split_torch_state_dict_into_shards` sees two distinct storages and
sizes the shards for what is actually written.
Cloning rather than dropping the duplicate key keeps the documented
contract that the output loads with `load_state_dict()`: a tied module
still lists both names in its `state_dict()`, so a strict load needs
both. The pickle path is untouched and still shares the tensor, which
`test_output_dtype_conversion_preserves_shared_tensors` and
`test_checkpoint_file_output_dtype` already assert.
## Test
`tests/unit/checkpoint/test_convert_checkpoint.py::test_safe_serialization_with_tied_weights`,
parametrized over `max_shard_size` `None` and `"1GB"` so both the
single-file and the sharded writer are covered. It is a CPU test in the
same monkeypatched style as the existing
`test_checkpoint_file_output_dtype`.
**2 failed before the change, 2 passed after**; the 4 existing CPU tests
in the file pass on both sides.
Separately I exercised the change against synthesized ZeRO-2 and ZeRO-3
checkpoints on disk (world sizes 1 to 4, odd numels, several parameter
groups, frozen parameters, buffers, tied parameters, fp16/bf16 output,
sharded and unsharded, safetensors and pickle), reconstructing the state
dict and comparing it to the weights the checkpoint was built from.
Everything round-trips, and the only failures before the change were the
tied-weight-plus-safetensors combinations.
---------
Signed-off-by: Vineeth Sai <vineethsai4444@gmail.com>
Co-authored-by: Masahiro Tanaka <81312776+tohtana@users.noreply.github.com>
Co-authored-by: Olatunji Ruwase <tunji.ruwase@snowflake.com>