Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

accum-batches can't be used with autoencoder

Open
#201 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python, pytorch

Research direction

Start in the AutoencoderTrainingWrapper class and trace the manual-optimization path used when model_type is "autoencoder". Compare it with Trainer(accumulate_grad_batches=32) and verify that the configured batch count is applied to both discriminator and generator updates without raising the reported exception. Done means autoencoder training accepts accum-batches and performs the expected accumulated optimizer steps.

Written by the indexing model from the issue text.

Description

When we set "model_type": "autoencoder" in the configuration of the model, the argument accum-batches can't be used because automatic_optimization is disabled.

MisconfigurationException: Automatic gradient accumulation is not supported for manual optimization. Remove `Trainer(accumulate_grad_batches=32)` or switch to automatic optimization.

To fix the issue, I have written the code to accumulate the batch in the AutoencoderTrainingWrapper class.

if use_disc:
            loss, losses = self.losses_disc(loss_info)

            log_dict["train/disc_lr"] = opt_disc.param_groups[0]["lr"]

            # Gradient accumulation
            if self.gradient_accumulation is not None:
                loss = loss / self.gradient_accumulation

            self.manual_backward(loss)
            if self.clip_grad_norm > 0.0:
                torch.nn.utils.clip_grad_norm_(
                    self.discriminator.parameters(), self.clip_grad_norm
                )

            # accumulate gradients of N batches
            if (batch_idx + 1) % self.gradient_accumulation == 0:
                opt_disc.step()

                if sched_disc is not None:
                    # sched step every step
                    sched_disc.step()

                opt_disc.zero_grad()

        # Train the generator
        else:
            loss, losses = self.losses_gen(loss_info)

            if self.use_ema:
                self.autoencoder_ema.update()

            log_dict["train/loss"] = loss.detach().item()
            log_dict["train/latent_std"] = latents.std().detach().item()
            log_dict["train/data_std"] = data_std.detach().item()
            log_dict["train/gen_lr"] = opt_gen.param_groups[0]["lr"]

            # Gradient accumulation
            if self.gradient_accumulation is not None:
                loss = loss / self.gradient_accumulation

            self.manual_backward(loss)
            if self.clip_grad_norm > 0.0:
                torch.nn.utils.clip_grad_norm_(
                    self.autoencoder.parameters(), self.clip_grad_norm
                )

            # accumulate gradients of N batches
            if (batch_idx + 1) % self.gradient_accumulation == 0:
                opt_gen.step()

                if sched_gen is not None:
                    # scheduler step every step
                    sched_gen.step()

                opt_gen.zero_grad()

Dominant language
Python
Stars
3.9k
Forks
488
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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.

More from Stability-AI/stable-audio-tools

All issues in Stability-AI/stable-audio-tools

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.