Skip to content

[graph_trainer] Pass global_valid_tokens into loss_fn instead of dividing externally - #3244

Merged
ydwu4 merged 10 commits into
mainfrom
gh/ydwu4/1/head
May 8, 2026
Merged

ydwu4 merged 10 commits into
mainfrom
gh/ydwu4/1/head

Conversation

@ydwu4

@ydwu4 ydwu4 commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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.

…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 added 7 commits May 6, 2026 21:50
…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]
@ydwu4 ydwu4 added the ciflow/h100.8 Trigger H100.8 CI label May 7, 2026
ydwu4 added 2 commits May 8, 2026 09:40
…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]
@ydwu4
ydwu4 changed the base branch from gh/ydwu4/1/base to main May 8, 2026 16:42
@ydwu4
ydwu4 merged commit ecd117e into main May 8, 2026
6 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants