Skip to content

feat(conversion): add HF embedding row repair script - #5005

Merged
yaoyu-33 merged 1 commit into
mainfrom
yuya/repair-hf-embedding-rows
Jul 22, 2026
Merged

yaoyu-33 merged 1 commit into
mainfrom
yuya/repair-hf-embedding-rows

Conversation

@yaoyu-33

@yaoyu-33 yaoyu-33 commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add an offline HF safetensors checkpoint repair script for near-zero input embedding rows.
  • Repair damaged input rows from matching lm_head directions scaled to healthy input embedding RMS norm.
  • Preserve HF checkpoint layout, write an audit manifest, and document the conversion example.

Nano 4B Check

Checked nvidia/NVIDIA-Nemotron-3-Nano-4B-BF16 model.safetensors via HTTP range reads of only:

  • backbone.embeddings.weight: BF16 [131072, 3136]
  • lm_head.weight: BF16 [131072, 3136]

With min_norm=1.0e-4:

  • num rows that would be repaired: 79
  • min input row norm before repair: 0.0
  • healthy input RMS target norm: 0.7767779671842665
  • corresponding lm_head source rows are healthy: min norm 0.2580839991569519, max norm 0.25814253091812134, unusable source rows 0

Repaired row IDs:
1192, 1193, 1245, 1246, 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 25304, 42819, 51067, 55014, 56286, 67044, 77579, 81422, 81691, 81695, 81772, 81815, 81819, 82005, 82267, 82312, 82500, 82690, 82725, 82737, 82977, 83362, 83855, 84006, 84105, 84115, 84121, 84521, 84794, 85015, 86060, 86366, 86409, 87020, 87411, 88346, 89406, 89412, 90101, 90294, 90320, 90703, 91368, 91515, 91608, 91762, 92529, 96374, 96385, 98573, 101707, 104097, 105465, 106921, 108262, 109896, 112147, 112277, 112327, 112497, 114442, 114755, 116654, 117123, 117744, 118025

Testing

  • uvx ruff check examples/conversion/repair_hf_embedding_rows.py
  • uvx ruff format --check examples/conversion/repair_hf_embedding_rows.py

Note: uv run pre-commit run --all-files was attempted after initializing 3rdparty/Megatron-LM, but cannot resolve on this macOS host because nvidia-resiliency-ext==0.6.0 only publishes manylinux wheels.

@copy-pr-bot

copy-pr-bot Bot commented Jul 22, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test 109a44b

@claude

claude Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Light review - LGTM

Self-contained offline utility with clear contracts, good safety guards (max_rows abort, damaged-output-source rejection, post-repair norm verification, output-path nesting check), and reasonable README docs. No correctness or logic issues found, no typos, and no perf/recipe configs are touched.

Minor, non-blocking observations:

Test-coverage gaps. The three unit tests cover the sharded-repair, dry-run, and damaged-output-source paths, but several guarded branches are untested:

  • Single-file (model.safetensors, no index) checkpoint path in _load_weight_map.
  • max_rows exceeded raising ValueError.
  • No damaged rows: returns a clone and still writes a manifest with target_norm=null.
  • Non-finite (NaN/inf) input rows via ~torch.isfinite, not just the <= min_norm path (current fixtures only use zero/tiny rows).
  • Explicit --input-embedding-name / --output-embedding-name, and the _resolve_tensor_name failure message.
  • --overwrite vs FileExistsError when the output dir already exists.

README nit: the new repair_hf_embedding_rows.py section is inserted just above the numbered '1. hf_megatron_roundtrip.py' entry, so an unnumbered tool now precedes the numbered list. Harmless, but slightly odd ordering.

Suggested test cases

  • No perf tests impacted.
  • Unit: single-file (non-sharded) model.safetensors checkpoint repair path.
  • Unit: max_rows exceeded raises ValueError.
  • Unit: no damaged rows results in output written unchanged plus manifest with target_norm=null and empty repaired_row_ids.
  • Unit: non-finite (NaN/inf) input embedding rows are detected and repaired.
  • Unit: explicit --input-embedding-name / --output-embedding-name resolution, plus _resolve_tensor_name failure error.
  • Unit: existing output dir raises FileExistsError without --overwrite, and is replaced with --overwrite.

Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
@yaoyu-33
yaoyu-33 force-pushed the yuya/repair-hf-embedding-rows branch from 109a44b to 14a6307 Compare July 22, 2026 02:58
@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test 14a6307

@yaoyu-33 yaoyu-33 added area:ckpt Checkpoint conversion, loading, export, and save paths feature New capabilities, enhancements, or enablement work needs-more-tests Requires additional L0 and L1 test coverage before merge needs-review PR is ready for code review and waiting on a reviewer labels Jul 22, 2026
@yaoyu-33
yaoyu-33 merged commit 40a49d8 into main Jul 22, 2026
77 of 82 checks passed
@yaoyu-33
yaoyu-33 deleted the yuya/repair-hf-embedding-rows branch July 22, 2026 17:20

This branch had an error being deployed

1 failed and 1 inactive deployments
test β€” 14a6307e Deployed Jul 22, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #18458
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ckpt Checkpoint conversion, loading, export, and save paths feature New capabilities, enhancements, or enablement work needs-more-tests Requires additional L0 and L1 test coverage before merge needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant