[BUG] TiledFusedLogitsLoss ignores external loss scaling for head gradients
メンテナーはふだん 2 日以内に返信
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 25/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
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
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
deepspeedai/DeepSpeed のほかの issue
-
[Bug] DeepSpeedEngine.step leaves _running_engine_step=True permanently on exception, corrupting is_gradient_accumulation_boundary()対応中かも @its-amann が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
deepspeedai/DeepSpeed#9190 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
[Bug] PipelineEngine._exec_reduce_grads and _exec_optimizer_step leave _force_grad_boundary=True permanently after any exception対応中かも @bantianyue が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
deepspeedai/DeepSpeed#9183 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
deepspeedai/DeepSpeed#9019 · コメント 2 件 ·
メンテナーはふだん 2 日以内に返信
-
fp16 `hysteresis: 0` (or negative) makes DynamicLossScaler never lower the loss scale, so every overflowing step is skipped forever対応中かも @damienen が 26 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
deepspeedai/DeepSpeed#8982 ·
メンテナーはふだん 2 日以内に返信
-
[BUG] OneCycle applies the minimum momentum to the first Adam update対応中かも @ai-yang が 30 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
deepspeedai/DeepSpeed#8951 ·
メンテナーはふだん 2 日以内に返信
deepspeedai/DeepSpeed の issue をすべて見る
似ている issue
-
area/auth comp/agent P3 type/bug
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
NousResearch/hermes-agent#161417 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
Dao-AILab/flash-attention#3251 ·
メンテナーはふだん 2 日以内に返信
-
[New Check]: sqlserver_public_network_access_disabled対応中かも @pouria-l が今日担当しました。 オープンfeature-request status/needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
prowler-cloud/prowler#13607 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
external langchain
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
langchain-ai/docs#8184 ·
メンテナーはふだん 1 日以内に返信