[BUG] TiledFusedLogitsLoss ignores external loss scaling for head gradients

オープン
#9,087 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@taking-lying-flat がすでに取り組んでいます。

2026年11月16日 から。

  • #9096 @taking-lying-flat による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, pytorch

調査の方向性

Start in deepspeed/runtime/sequence_parallel/ulysses_sp.py: read TiledFusedLogitsLoss.forward and its backward, then compare with SequenceTiledCompute's deferred-gradient path. Run the issue's repro.py under CPU FP64 to confirm the 11x head-gradient ratio and the loss * 0 and 1/4-scaling controls. Done means head parameter gradients match the reference for output_reduction="sum" with token normalization while loss and hidden gradients stay correct; the ZeRO-hook interaction still needs maintainer input.

索引モデルが issue の本文から書いたものです。

説明

TiledFusedLogitsLoss silently produces incorrectly scaled LM-head gradients when its returned loss is normalized or weighted. The loss value and hidden-state gradients remain correct. This includes the documented output_reduction="sum" followed by valid-token normalization.

System info

DeepSpeed master 7caa7a2267bc5 (0.20.1 source), Python 3.12.13, PyTorch 2.13.0+cu130; Linux, CPU FP64. Direct Python, without a distributed launcher. ds_report confirms the source checkout and no compiled DeepSpeed ops.

Minimal reproduction

Run against that checkout with DS_ACCELERATOR=cpu PYTHONPATH=. python repro.py:

import copy
import torch
import torch.nn.functional as F
from deepspeed.runtime.sequence_parallel.ulysses_sp import TiledFusedLogitsLoss

torch.manual_seed(0)
torch.set_num_threads(1)
head = torch.nn.Linear(4, 5, bias=False).double()
ref_head = copy.deepcopy(head)
x = torch.randn(2, 7, 4, dtype=torch.float64, requires_grad=True)
ref_x = x.detach().clone().requires_grad_()
y = torch.tensor([[0, 1, -100, 2, 3, 4, 0], [1, 2, 3, -100, 4, 0, -100]])

def ce_sum(model, hidden, labels):
    logits = model(hidden)
    return F.cross_entropy(logits.reshape(-1, 5), labels.reshape(-1), reduction="sum")

n = (y != -100).sum()  # 11 valid tokens, uneven tiles
loss = TiledFusedLogitsLoss.apply(ce_sum, head, x, y, None, 3, list(head.parameters()), "sum") / n
ref_loss = ce_sum(ref_head, ref_x, y) / n
loss.backward()
ref_loss.backward()

print("loss error:", (loss - ref_loss).abs().item())
print("hidden gradient error:", (x.grad - ref_x.grad).abs().max().item())
print("head gradient norm ratio:", (head.weight.grad.norm() / ref_head.weight.grad.norm()).item())
torch.testing.assert_close(head.weight.grad, ref_head.weight.grad)

Actual output:

loss error: 0.0
hidden gradient error: 1.0408340855860843e-17
head gradient norm ratio: 10.999999999999996
AssertionError: Tensor-likes are not close!
Mismatched elements: 20 / 20 (100.0%)

The assertion exposes a silent training error; the loss computation itself does not raise. The expected head-gradient ratio is 1, but it is 11, the valid-token count.

Root cause

Forward uses a synthetic incoming gradient of 1 and calls backward inside each tile's forward, already accumulating the head's parameter gradients. When the real upstream gradient arrives, backward only scales the saved hidden gradient.

For J = alpha * L, the head therefore receives dL/dW instead of alpha * dL/dW.

I also reproduced nonzero head gradients for loss * 0, and head gradients 4× the reference after four microbatches each scaled by 1/4. The unscaled control passes. These are local autograd tests; full ZeRO/distributed and AMP training were not run.

Suggested fix

Defer each tile's gradient computation to backward so both hidden and parameter gradients use the actual upstream gradient, following SequenceTiledCompute. Its sum-reduction path matches the reference in all six CPU checks. This requires recomputing tiles; throughput was not measured. Multiplying param.grad afterward would also rescale contributions already accumulated from other calls.

Related PR #8960 fixes in-place mutation of an incoming gradient in SequenceTiledCompute; this is a different root cause.

I would appreciate the maintainers' feedback on this fix direction, particularly its interaction with ZeRO hooks.

主要言語
Python
スター
43.3k
フォーク
5.1k
平均マージ
5日 10時間
マージ済み PR(30日)
85

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

deepspeedai/DeepSpeed のほかの issue

deepspeedai/DeepSpeed の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。