transformers
8979c57d - Install the TP gradient all-reduce only when grad is enabled (#49975)

Commit
27 days ago
Install the TP gradient all-reduce only when grad is enabled (#49975) `ReplicatedWithGradAllReduce.install_forward` registered a module-level backward hook on every per-head norm at TP setup time. Dynamo breaks the graph at each call of a module carrying a full backward hook, including under `torch.no_grad()`, so `tp_plan="auto"` cost every affected model a graph break per norm per layer in inference. Reported for Qwen3, where decoding slowed from 37.6 ms to 140 ms per token. Register the all-reduce from the module's forward instead, and only when `torch.is_grad_enabled()`: inference installs nothing. Doing it at forward time rather than install time also means the parameters the loader actually installed are the ones hooked, which is what the module-level hook was working around, so a `register_post_accumulate_grad_hook` is safe here. That also removes a latent bug. A full backward hook fires on grad w.r.t. outputs when no input requires grad, at which point `param.grad` is still None and the all-reduce was silently skipped; a post-accumulate hook cannot run before the gradient exists.
Author
Parents
Loading