TrajectoryImitationLoss: the regularization term (loss_2) has no gradient path to the student

Offen
#1,875 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@shizuku-desir7e arbeitet bereits daran.

Seit 03.12.2026.

  • #1877 von @shizuku-desir7e — offen

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
72/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python, pytorch

Rechercherichtung

Start with diffsynth/diffusion/loss.py, especially TrajectoryImitationLoss.compute_regularization at the linked lines, then trace how forward adds loss_2. Reproduce the gradient comparison described in the issue and check whether the regularization term contributes gradients to the student. Done when the gradient behavior is corrected or its intended stop-gradient behavior is clarified, with a regression check covering the reported case.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Describe the bug

In TrajectoryImitationLoss.compute_regularization, the student rolls out from the teacher's starting latent and each step is taken with noise_pred.detach():

https://github.com/modelscope/DiffSynth-Studio/blob/59e064d03b9ddc3c6eeed71c8c3262a2049c5acf/diffsynth/diffusion/loss.py#L190-L206

The starting latent trajectory_teacher[0] comes from fetch_trajectory under torch.no_grad(). Every update then uses the detached prediction, so image_pred = pipe.vae_decoder(inputs_shared["latents"]) does not depend on the student's parameters. loss_2 is added to the training loss in forward (L215-L217), but it contributes no gradient to the student. Training still runs normally, because loss_1 (align_trajectory) is differentiable, so nothing signals that the regularizer is inactive.

If the stop-gradient is intended (e.g. to save memory), then loss_2 only changes the logged loss value, which is probably not what the image-space regularizer is for.

Reproduction

I ran the original TrajectoryImitationLoss.forward on CPU with:

  • a scalar "student" (0.7) and a frozen "teacher" (1.0) as model functions;
  • the real scheduler, pipe.step and serial CFG;
  • an identity decoder and an MSE consumer in place of LPIPS.
d(total loss)/d(student)
autograd, current code −6.036
central finite difference of the same loss −6.636
autograd, with .detach() removed only in compute_regularization −6.636

The missing −0.6 is exactly the regularization term's gradient. The same result holds with the actual LPIPS (AlexNet) architecture with random frozen weights in place of the MSE consumer.

Context

I am working on a research project that checks diffusion training code for silent gradient and convention problems. This came out of that check. #1192 asks about this loss in general but does not mention the gradient path.

Vorherrschende Sprache
Python
Sterne
13.5k
Forks
1.3k
Ø Merge
1 T. 13 Std.
Gemergte PRs (30 T.)
46

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus modelscope/DiffSynth-Studio

Alle Issues in modelscope/DiffSynth-Studio

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.