Skip to content

fix(compile): preserve unused gradients and validate collective usage - #16

Open
yu-chen-code wants to merge 1 commit into
masterfrom
feat-graph-unused-gradients
Open

yu-chen-code wants to merge 1 commit into
masterfrom
feat-graph-unused-gradients

Conversation

@yu-chen-code

@yu-chen-code yu-chen-code commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

What type of PR is this?
/kind bug

What does this PR do / why do we need it:

Refreshed on 2026-11-09 against fork/upstream master a6be99d3. PR1421 is now merged upstream, so its duplicate prerequisite has been removed. Current head: 4c52a3e1922032fd9f58887c29ecaeab8d637486; retained feature/dependency commits: 4c52a3e1. Git range-diff confirms unchanged feature patches. Focused CPU regression on this exact head: 114 JUnit records, 0 failures, 0 errors. The isolated refresh environments now include master-required PEFT 0.21.2 and accelerate 1.13.0; pip check passes. All numerical runs and older source hashes below are pre-refresh evidence. NPU precision training and formal upstream CI were not rerun for this rebase.

Keep truly unused trainable parameters out of AdamW updates when replaying a joint graph. The tracer previously replaced every None gradient with a zero tensor. A parameter absent from the loss therefore acquired Adam moments and weight decay in graph mode, although eager training left its gradient and optimizer state absent. With learning rate 0.01 and weight decay 0.1, ten updates changed an unused weight from 0.5 by 0.00497749, a relative error of 0.00995499.

Preserve each None in its original gradient-output slot. The existing FSDP pass already skips literal non-node gradients without compacting parameter indices, so later parameters and trailing loss metrics retain their correct positions. The compiler skips None during accumulation and leaves any earlier microbatch gradient intact. A used parameter with a tensor-valued zero gradient still follows the usual optimizer state creation and decay; zero-token expert-bank gradients remain tensors.

When graph FSDP or PP is enabled in distributed training, validate the captured state schema and each trainable FQN/unused flag once across all participating world ranks before parallel graph lowering. All ranks participate, including ranks with no unused parameter. This is a conservative uniform-usage admission check, not a gradient union between distinct EP owners. Rank-dependent trainable sets or usage fail explicitly instead of emitting different collective sequences. PP with unused gradients is explicitly rejected; dynamic unused-parameter recapture and owner-group union are outside this change.

Validation:

  • Reproduced the original error on CPU and four-card native HyperParallel FSDP with a never-used parameter, a used zero-gradient parameter and distinct used parameters before/after the unused slot.
  • Five new CPU tests cover AdamW skipping versus zero-gradient decay/state creation, ten updates and microbatch accumulation, preservation of an earlier gradient, FSDP output-slot identities and loss metrics, mismatched rank usage/trainable FQNs, and PP rejection.
  • Pre-refresh complete compile UT: 354 passed and 315 subtests passed. Two existing full/regional CPU Inductor strict-equality subcases still fail with 0.701689600944519 versus 0.7016897201538086 on this Torch nightly, the same two-FP32-ULP baseline reproduced on the prerequisite. They were not skipped or relaxed; an all-tests-passed result is not claimed.
  • Ascend 910B2, Torch 2.15.0.dev20261005 / torch-npu 2.15, pre-refresh head e7ed6f4f: native eager FSDP4 versus graph, ten AdamW updates and two microbatches, 720 tensor comparisons exact. Unused gradients and moments remain absent; used tensor-zero gradients retain moments and decay.
  • Native eager FSDP4+EP2+EDP2 versus graph, different initial expert weights for each EP owner, real NPU GMM and alternating empty/nonempty expert routes: another 1320 tensor comparisons exact. Forty zero-token expert-gradient observations were tensor-valued zeros, distinct from the truly unused parameter. The fixed limit remained 1e-3 with denominator floor 1e-8; all nonfinite counts were zero.
  • Two separate four-rank negative cases used different local usage masks and different frozen/trainable identities. All ranks rejected before parallel graph lowering or training replay, with no mismatched collective hang. In these base-prerequisite probes physical storage was also unchanged; storage-ready compositions may already have prepared storage before this validation.
  • The existing FP32 ragged FSDP+EP ten-step regression passed. Final Python 3.9 and 3.12 public-CI pylint configurations both had zero findings. Before/after source hashes, editable imports and dependency checks passed. No performance claim is made.

Source manifests, full tensors, commands, numerical summaries and original failures are retained in T020 runs/20261106-graph-capability-spikes/unused*; the final summary is unused/final-verification.json. No formal upstream gate is requested or claimed.

Which issue(s) this PR fixes:

None.

Code review checklist [illustration]:

  • whether to verify the function's return value (It is forbidden to use void to mask the return values of security functions and self-developed functions. C++ STL functions can be masked if there is no problem)
  • Whether to comply with SOLID principle / Demeter's law
  • Whether there is UT test case && the test case is a valid (if there is no test case, please explain the reason)
  • Whether the API change is involved
  • Whether official document modification is involved

@yu-chen-code
yu-chen-code force-pushed the feat-graph-unused-gradients branch from e7ed6f4 to 4c52a3e Compare November 9, 2026 11:25
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