modelscope / modelscope/DiffSynth-Studio

Details on direct distillation

Open
#1,181 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
13.1k
Forks
1.3k
Avg merge
13h 12m
Merged PRs (30d)
45

Description

Hi, thanks for your great contributions to the community! I’ve been using DiffSynth for a long time. When I look up the code of loss.py, I'm confuse about the algorithm of the direct distillation.

Code:

def DirectDistillLoss(pipe: BasePipeline, **inputs):
    pipe.scheduler.set_timesteps(inputs["num_inference_steps"])
    pipe.scheduler.training = True
    models = {name: getattr(pipe, name) for name in pipe.in_iteration_models}
    for progress_id, timestep in enumerate(pipe.scheduler.timesteps):
        timestep = timestep.unsqueeze(0).to(dtype=pipe.torch_dtype, device=pipe.device)
        noise_pred = pipe.model_fn(**models, **inputs, timestep=timestep, progress_id=progress_id)
        inputs["latents"] = pipe.step(pipe.scheduler, progress_id=progress_id, noise_pred=noise_pred, **inputs)
    loss = torch.nn.functional.mse_loss(inputs["latents"].float(), inputs["input_latents"].float())
    return loss

The loss function and pseudo code:

$$ \mathcal{L}{\text{direct}} = \text{MSE}(\boldsymbol{z}{\text{student}}, \boldsymbol{z}_{\text{teacher}}) $$

seed = xxx
with torch.no_grad():
    image_1 = pipe(prompt, steps=50, seed=seed, cfg=4)
image_2 = pipe(prompt, steps=4, seed=seed, cfg=1)
loss = torch.nn.functional.mse_loss(image_1, image_2)

So I got noise as inputs["latents"] and image latents as inputs["input_latents"] after QwenImageUnit_InputImageEmbedder stage. Why it could be considered as Student-Teacher model ? Why do you consider random noise as student model instead of few step inference result How can we set NFE (few steps like 4 or 8) with training? Thank you for your time, and thanks again for all your work on this project!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read diffsynth/diffusion/loss.py and the DirectDistillLoss entry point, then trace the scheduler, model_fn, step, and QwenImageUnit_InputImageEmbedder context named in the issue. A useful resolution should explain the student and teacher roles and how NFE is selected during training, with the relevant code locations identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.