CorentinJ/Real-Time-Voice-Cloning · error · ValueError

`hparams.synthesis_batch_size` must be evenly divisible by n

Error message

`hparams.synthesis_batch_size` must be evenly divisible by n_gpus!

What it means

Raised by run_synthesis() in synthesizer/synthesize.py when CUDA is available and hparams.synthesis_batch_size % torch.cuda.device_count() != 0. GTA (ground-truth-aligned) mel generation shards each batch across all visible GPUs via DataParallel, which requires the batch to split evenly; a remainder would crash or silently drop samples, so it is rejected up front. CPU-only runs never hit this branch.

Source

Thrown at synthesizer/synthesize.py:27

from synthesizer.hparams import hparams_debug_string
from synthesizer.models.tacotron import Tacotron
from synthesizer.synthesizer_dataset import SynthesizerDataset, collate_synthesizer
from synthesizer.utils import data_parallel_workaround
from synthesizer.utils.symbols import symbols


def run_synthesis(in_dir: Path, out_dir: Path, syn_model_fpath: Path, hparams):
    # This generates ground truth-aligned mels for vocoder training
    synth_dir = out_dir / "mels_gta"
    synth_dir.mkdir(exist_ok=True, parents=True)
    print(hparams_debug_string())

    # Check for GPU
    if torch.cuda.is_available():
        device = torch.device("cuda")
        if hparams.synthesis_batch_size % torch.cuda.device_count() != 0:
            raise ValueError("`hparams.synthesis_batch_size` must be evenly divisible by n_gpus!")
    else:
        device = torch.device("cpu")
    print("Synthesizer using device:", device)

    # Instantiate Tacotron model
    model = Tacotron(embed_dims=hparams.tts_embed_dims,
                     num_chars=len(symbols),
                     encoder_dims=hparams.tts_encoder_dims,
                     decoder_dims=hparams.tts_decoder_dims,
                     n_mels=hparams.num_mels,
                     fft_bins=hparams.num_mels,
                     postnet_dims=hparams.tts_postnet_dims,
                     encoder_K=hparams.tts_encoder_K,
                     lstm_dims=hparams.tts_lstm_dims,
                     postnet_K=hparams.tts_postnet_K,
                     num_highways=hparams.tts_num_highways,
                     dropout=0., # Use zero dropout for gta mels
                     stop_threshold=hparams.tts_stop_threshold,

View on GitHub (pinned to 890f3a0318)

Solutions

  1. Set hparams.synthesis_batch_size to a multiple of torch.cuda.device_count() (e.g. 16 or 32 on 2/4/8 GPUs) and re-run.
  2. Or restrict the run to a divisor-friendly GPU set, e.g. CUDA_VISIBLE_DEVICES=0 python synthesizer/synthesize.py ... so device_count()==1 and any batch size passes.
  3. Check for stray GPUs being visible (CUDA_VISIBLE_DEVICES="" would make it CPU-only, avoiding the check entirely — only if CPU synthesis is acceptable).

Example fix

# before: 2 GPUs visible, batch size 11
hparams.synthesis_batch_size = 11  # 11 % 2 != 0 -> ValueError

# after
import torch
hparams.synthesis_batch_size = max(1, (11 + torch.cuda.device_count() - 1) // torch.cuda.device_count()) * torch.cuda.device_count()  # rounds up to a multiple of n_gpus
Defensive patterns

Strategy: validation

Validate before calling

import torch

def validate_batch_size(batch_size: int) -> int:
    n = torch.cuda.device_count() if torch.cuda.is_available() else 1
    return batch_size if n == 1 or batch_size % n == 0 else batch_size + (n - batch_size % n)

Prevention

When it happens

Trigger: Running synthesizer/synthesize.py (GTA synthesis for vocoder training) on a multi-GPU machine where synthesis_batch_size is not a multiple of the GPU count — e.g. batch_size 11 with 2 GPUs, or a value set for a different machine's GPU count. The batch size comes from the synthesizer hparams file (tts_hparams.py / a saved hparams dict).

Common situations: Hyperparameter file tuned on 1 GPU then reused on a 2/4/8-GPU box; CUDA_VISIBLE_DEVICES changed after hparams were written; default batch_size coincidentally not divisible by the new device count.

Related errors


AI-assisted analysis of CorentinJ/Real-Time-Voice-Cloning@890f3a0318 (2026-08-15). Data as JSON: /api/errors/fd4eb6b606086872. Report an issue: GitHub.