Skip to content

[Fix] Support origin HF weight backfilling in parallel converter - #2270

Open
albaNnaksqr wants to merge 3 commits into
THUDM:mainfrom
albaNnaksqr:fix/parallel-add-missing-weights
Open

[Fix] Support origin HF weight backfilling in parallel converter#2270
albaNnaksqr wants to merge 3 commits into
THUDM:mainfrom
albaNnaksqr:fix/parallel-add-missing-weights

Conversation

@albaNnaksqr

@albaNnaksqr albaNnaksqr commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Motivation

convert_torch_dist_to_hf.py supports --add-missing-from-origin-hf, which restores tensors that are present in the original Hugging Face checkpoint but absent from the Megatron torch_dist checkpoint.

This is useful when the Megatron checkpoint intentionally contains only part of the model. For example, the checkpoint may contain the trained language model weights but omit frozen visual encoder or MTP weights.

However, convert_torch_dist_to_hf_parallel.py did not support this option. Switching from the regular converter to the parallel converter could therefore produce an incomplete Hugging Face checkpoint.

This is related to:

Changes

This PR adds --add-missing-from-origin-hf support to convert_torch_dist_to_hf_parallel.py.

When enabled, the parallel converter:

  • collects the complete set of tensors produced by all conversion workers;
  • reads tensors missing from the converted result from --origin-hf-dir;
  • writes the missing tensors into additional safetensors shards;
  • includes the additional tensors in the final weight_map, shard numbering, and total_size;
  • requires --origin-hf-dir;
  • verifies that every converted tensor is represented in the final weight map.

The option remains opt-in, matching the behavior of the regular converter.

Conversion error handling

Previously, an exception raised while converting an individual Megatron parameter was printed and replaced with an empty result:

except Exception as e:
    print(f"Error processing {name}: {e}")
    return []

With origin-HF backfilling enabled, this could hide a real conversion failure. The missing output tensors could be copied from the original checkpoint, producing an apparently complete checkpoint containing stale pretrained weights.

This PR now collects conversion errors from all parallel workers and raises a combined error before:

  • creating any output tensor shard;
  • writing the final checkpoint index;
  • backfilling any tensor from the original HF checkpoint.

Therefore, origin-HF backfilling cannot mask a parameter conversion exception.

Validation

A zero-training Qwen3.5-27B round trip was tested:

HF -> torch_dist -> HF

using the parallel converter with --add-missing-from-origin-hf.

Checkpoint validation:

  • original HF tensors: 1,199;
  • tensors converted from torch_dist: 851;
  • tensors restored from the original HF checkpoint: 348;
  • final HF tensors: 1,199;
  • missing keys: 0;
  • unexpected keys: 0;
  • shape mismatches: 0;
  • non-finite tensors: 0.

The 348 restored tensors consisted of:

  • 333 model.visual.* tensors;
  • 15 mtp.* tensors.

The resulting checkpoint was served with SGLang v0.5.16 and tested using short, medium, and long tool-calling prompts:

TP=1: 9/9 passed
TP=2: 9/9 passed
TP=4: 9/9 passed
Total: 27/27 passed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant