huggingface / huggingface/diffusers

Potential incorrect indentation for logging in train_dreambooth.py

Open
#12,641 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stale
Dominant language
Python
Stars
34.5k
Forks
7.3k
Avg merge
3d 3h
Merged PRs (30d)
91

Description

Description:

In the train_dreambooth.py script, the logging and progress bar updates appear to be executed on every training step, even when using gradient accumulation. This might lead to incorrect or redundant logging.

The relevant code is located around lines 1393-1395:

https://github.com/huggingface/diffusers/blob/093cd3f040ee4f44908df8e1b441954f3f25c214/examples/dreambooth/train_dreambooth.py#L1346-L1395

The global_step is only incremented when accelerator.sync_gradients is true. However, the logging calls (progress_bar.set_postfix and accelerator.log) are outside this block. This means that when gradient accumulation is used, these lines are executed for every batch, but the global_step value passed to accelerator.log does not change until an optimization step occurs. This could result in multiple log entries for the same global_step.

It seems more appropriate to move the logging logic inside the if accelerator.sync_gradients: block to ensure that logging only happens once per optimization step.

Proposed Change:

  if accelerator.sync_gradients: 
      progress_bar.update(1) 
      global_step += 1 

      if accelerator.is_main_process: 
          # ... checkpointing and validation logic ...
 
- logs = {"loss": loss.detach().item(), "lr": lr_scheduler.get_last_lr()[0]} 
- progress_bar.set_postfix(**logs) 
- accelerator.log(logs, step=global_step)

+    logs = {"loss": loss.detach().item(), "lr": lr_scheduler.get_last_lr()[0]} 
+    progress_bar.set_postfix(**logs)
+    accelerator.log(logs, step=global_step)

Could you please confirm if this is the intended behavior or if the indentation should be corrected? Thank you!

Contributor guide

Open the contributing guide

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

Start in examples/dreambooth/train_dreambooth.py around lines 1346-1395 and inspect how accelerator.sync_gradients controls global_step. Check whether progress_bar.set_postfix and accelerator.log run once per optimization step or on every batch; done means the intended logging behavior is confirmed and the indentation is corrected if needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.