Repository navigation
[graph_trainer] Pass global_valid_tokens into loss_fn instead of dividing externally - #3244
Merged
Merged
Conversation
…ding externally graph_trainer's compute_loss previously did ``loss_fn(pred, labels) / global_valid_tokens`` — calling the loss without gvt, then dividing the result. This breaks the "loss is the autograd endpoint" mental model and forces every loss-side autograd.Function backward to chain-rule the external division through grad_output, which interacts poorly with custom Functions whose returned grads live on a different DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss on (tp), as we'd hit with chunked CE plumbing). global_valid_tokens is already part of the BaseLoss.__call__ contract — ``CrossEntropyLoss``, ``MSELoss``, and ``ChunkedCELoss`` all accept it and divide internally if provided. Passing gvt through restores "loss is the endpoint": no external scaling on the autograd path, downstream grad_output is trivially 1.0, and the loss tensor lands on a single mesh determined by the loss computation rather than the gvt operand's mesh. Numerically equivalent: BaseLoss.__call__ does ``loss = self.fn(...); if gvt: loss = loss / gvt`` — identical to the prior external divide for the existing CrossEntropyLoss path that to_graph_trainer_config swaps to. [ghstack-poisoned]
ydwu4
requested review from
SherlockNoMad,
aditvenk,
sanketpurandare,
tianyu-l,
xmfan and
yiming0416
as code owners
May 6, 2026 19:31
This was referenced May 6, 2026
SherlockNoMad
approved these changes
May 6, 2026
…ead of dividing externally" graph_trainer's compute_loss previously did ``loss_fn(pred, labels) / global_valid_tokens`` — calling the loss without gvt, then dividing the result. This breaks the "loss is the autograd endpoint" mental model and forces every loss-side autograd.Function backward to chain-rule the external division through grad_output, which interacts poorly with custom Functions whose returned grads live on a different DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss on (tp), as we'd hit with chunked CE plumbing). global_valid_tokens is already part of the BaseLoss.__call__ contract — ``CrossEntropyLoss``, ``MSELoss``, and ``ChunkedCELoss`` all accept it and divide internally if provided. Passing gvt through restores "loss is the endpoint": no external scaling on the autograd path, downstream grad_output is trivially 1.0, and the loss tensor lands on a single mesh determined by the loss computation rather than the gvt operand's mesh. Numerically equivalent: BaseLoss.__call__ does ``loss = self.fn(...); if gvt: loss = loss / gvt`` — identical to the prior external divide for the existing CrossEntropyLoss path that to_graph_trainer_config swaps to. [ghstack-poisoned]
…nto loss_fn instead of dividing externally" graph_trainer's compute_loss previously did ``loss_fn(pred, labels) / global_valid_tokens`` — calling the loss without gvt, then dividing the result. This breaks the "loss is the autograd endpoint" mental model and forces every loss-side autograd.Function backward to chain-rule the external division through grad_output, which interacts poorly with custom Functions whose returned grads live on a different DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss on (tp), as we'd hit with chunked CE plumbing). global_valid_tokens is already part of the BaseLoss.__call__ contract — ``CrossEntropyLoss``, ``MSELoss``, and ``ChunkedCELoss`` all accept it and divide internally if provided. Passing gvt through restores "loss is the endpoint": no external scaling on the autograd path, downstream grad_output is trivially 1.0, and the loss tensor lands on a single mesh determined by the loss computation rather than the gvt operand's mesh. Numerically equivalent: BaseLoss.__call__ does ``loss = self.fn(...); if gvt: loss = loss / gvt`` — identical to the prior external divide for the existing CrossEntropyLoss path that to_graph_trainer_config swaps to. [ghstack-poisoned]
…ead of dividing externally" graph_trainer's compute_loss previously did ``loss_fn(pred, labels) / global_valid_tokens`` — calling the loss without gvt, then dividing the result. This breaks the "loss is the autograd endpoint" mental model and forces every loss-side autograd.Function backward to chain-rule the external division through grad_output, which interacts poorly with custom Functions whose returned grads live on a different DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss on (tp), as we'd hit with chunked CE plumbing). global_valid_tokens is already part of the BaseLoss.__call__ contract — ``CrossEntropyLoss``, ``MSELoss``, and ``ChunkedCELoss`` all accept it and divide internally if provided. Passing gvt through restores "loss is the endpoint": no external scaling on the autograd path, downstream grad_output is trivially 1.0, and the loss tensor lands on a single mesh determined by the loss computation rather than the gvt operand's mesh. Numerically equivalent: BaseLoss.__call__ does ``loss = self.fn(...); if gvt: loss = loss / gvt`` — identical to the prior external divide for the existing CrossEntropyLoss path that to_graph_trainer_config swaps to. [ghstack-poisoned]
…nto loss_fn instead of dividing externally" graph_trainer's compute_loss previously did ``loss_fn(pred, labels) / global_valid_tokens`` — calling the loss without gvt, then dividing the result. This breaks the "loss is the autograd endpoint" mental model and forces every loss-side autograd.Function backward to chain-rule the external division through grad_output, which interacts poorly with custom Functions whose returned grads live on a different DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss on (tp), as we'd hit with chunked CE plumbing). global_valid_tokens is already part of the BaseLoss.__call__ contract — ``CrossEntropyLoss``, ``MSELoss``, and ``ChunkedCELoss`` all accept it and divide internally if provided. Passing gvt through restores "loss is the endpoint": no external scaling on the autograd path, downstream grad_output is trivially 1.0, and the loss tensor lands on a single mesh determined by the loss computation rather than the gvt operand's mesh. Numerically equivalent: BaseLoss.__call__ does ``loss = self.fn(...); if gvt: loss = loss / gvt`` — identical to the prior external divide for the existing CrossEntropyLoss path that to_graph_trainer_config swaps to. [ghstack-poisoned]
…ead of dividing externally" graph_trainer's compute_loss previously did ``loss_fn(pred, labels) / global_valid_tokens`` — calling the loss without gvt, then dividing the result. This breaks the "loss is the autograd endpoint" mental model and forces every loss-side autograd.Function backward to chain-rule the external division through grad_output, which interacts poorly with custom Functions whose returned grads live on a different DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss on (tp), as we'd hit with chunked CE plumbing). global_valid_tokens is already part of the BaseLoss.__call__ contract — ``CrossEntropyLoss``, ``MSELoss``, and ``ChunkedCELoss`` all accept it and divide internally if provided. Passing gvt through restores "loss is the endpoint": no external scaling on the autograd path, downstream grad_output is trivially 1.0, and the loss tensor lands on a single mesh determined by the loss computation rather than the gvt operand's mesh. Numerically equivalent: BaseLoss.__call__ does ``loss = self.fn(...); if gvt: loss = loss / gvt`` — identical to the prior external divide for the existing CrossEntropyLoss path that to_graph_trainer_config swaps to. [ghstack-poisoned]
…nto loss_fn instead of dividing externally" graph_trainer's compute_loss previously did ``loss_fn(pred, labels) / global_valid_tokens`` — calling the loss without gvt, then dividing the result. This breaks the "loss is the autograd endpoint" mental model and forces every loss-side autograd.Function backward to chain-rule the external division through grad_output, which interacts poorly with custom Functions whose returned grads live on a different DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss on (tp), as we'd hit with chunked CE plumbing). global_valid_tokens is already part of the BaseLoss.__call__ contract — ``CrossEntropyLoss``, ``MSELoss``, and ``ChunkedCELoss`` all accept it and divide internally if provided. Passing gvt through restores "loss is the endpoint": no external scaling on the autograd path, downstream grad_output is trivially 1.0, and the loss tensor lands on a single mesh determined by the loss computation rather than the gvt operand's mesh. Numerically equivalent: BaseLoss.__call__ does ``loss = self.fn(...); if gvt: loss = loss / gvt`` — identical to the prior external divide for the existing CrossEntropyLoss path that to_graph_trainer_config swaps to. [ghstack-poisoned]
…ead of dividing externally" graph_trainer's compute_loss previously did ``loss_fn(pred, labels) / global_valid_tokens`` — calling the loss without gvt, then dividing the result. This breaks the "loss is the autograd endpoint" mental model and forces every loss-side autograd.Function backward to chain-rule the external division through grad_output, which interacts poorly with custom Functions whose returned grads live on a different DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss on (tp), as we'd hit with chunked CE plumbing). global_valid_tokens is already part of the BaseLoss.__call__ contract — ``CrossEntropyLoss``, ``MSELoss``, and ``ChunkedCELoss`` all accept it and divide internally if provided. Passing gvt through restores "loss is the endpoint": no external scaling on the autograd path, downstream grad_output is trivially 1.0, and the loss tensor lands on a single mesh determined by the loss computation rather than the gvt operand's mesh. Numerically equivalent: BaseLoss.__call__ does ``loss = self.fn(...); if gvt: loss = loss / gvt`` — identical to the prior external divide for the existing CrossEntropyLoss path that to_graph_trainer_config swaps to. [ghstack-poisoned]
…nto loss_fn instead of dividing externally" graph_trainer's compute_loss previously did ``loss_fn(pred, labels) / global_valid_tokens`` — calling the loss without gvt, then dividing the result. This breaks the "loss is the autograd endpoint" mental model and forces every loss-side autograd.Function backward to chain-rule the external division through grad_output, which interacts poorly with custom Functions whose returned grads live on a different DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss on (tp), as we'd hit with chunked CE plumbing). global_valid_tokens is already part of the BaseLoss.__call__ contract — ``CrossEntropyLoss``, ``MSELoss``, and ``ChunkedCELoss`` all accept it and divide internally if provided. Passing gvt through restores "loss is the endpoint": no external scaling on the autograd path, downstream grad_output is trivially 1.0, and the loss tensor lands on a single mesh determined by the loss computation rather than the gvt operand's mesh. Numerically equivalent: BaseLoss.__call__ does ``loss = self.fn(...); if gvt: loss = loss / gvt`` — identical to the prior external divide for the existing CrossEntropyLoss path that to_graph_trainer_config swaps to. [ghstack-poisoned]
…ead of dividing externally" graph_trainer's compute_loss previously did ``loss_fn(pred, labels) / global_valid_tokens`` — calling the loss without gvt, then dividing the result. This breaks the "loss is the autograd endpoint" mental model and forces every loss-side autograd.Function backward to chain-rule the external division through grad_output, which interacts poorly with custom Functions whose returned grads live on a different DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss on (tp), as we'd hit with chunked CE plumbing). global_valid_tokens is already part of the BaseLoss.__call__ contract — ``CrossEntropyLoss``, ``MSELoss``, and ``ChunkedCELoss`` all accept it and divide internally if provided. Passing gvt through restores "loss is the endpoint": no external scaling on the autograd path, downstream grad_output is trivially 1.0, and the loss tensor lands on a single mesh determined by the loss computation rather than the gvt operand's mesh. Numerically equivalent: BaseLoss.__call__ does ``loss = self.fn(...); if gvt: loss = loss / gvt`` — identical to the prior external divide for the existing CrossEntropyLoss path that to_graph_trainer_config swaps to. [ghstack-poisoned]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
graph_trainer's compute_loss previously did
loss_fn(pred, labels) / global_valid_tokens— calling the loss withoutgvt, then dividing the result. This breaks the "loss is the autograd
endpoint" mental model and forces every loss-side autograd.Function backward
to chain-rule the external division through grad_output, which interacts
poorly with custom Functions whose returned grads live on a different
DeviceMesh than the loss output (e.g. lm_head params on (fsdp, tp) vs loss
on (tp), as we'd hit with chunked CE plumbing).
global_valid_tokens is already part of the BaseLoss.call contract —
CrossEntropyLoss,MSELoss, andChunkedCELossall accept it anddivide internally if provided. Passing gvt through restores "loss is the
endpoint": no external scaling on the autograd path, downstream grad_output
is trivially 1.0, and the loss tensor lands on a single mesh determined by
the loss computation rather than the gvt operand's mesh.
Numerically equivalent: BaseLoss.call does
loss = self.fn(...); if gvt: loss = loss / gvt— identical to the priorexternal divide for the existing CrossEntropyLoss path that
to_graph_trainer_config swaps to.