{"record":{"id":"fd4eb6b606086872","repo":"CorentinJ/Real-Time-Voice-Cloning","slug":"hparams-synthesis-batch-size-must-be-evenly-divi","errorCode":null,"errorMessage":"`hparams.synthesis_batch_size` must be evenly divisible by n_gpus!","messagePattern":"`hparams\\.synthesis_batch_size` must be evenly divisible by n_gpus!","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"synthesizer/synthesize.py","lineNumber":27,"sourceCode":"\nfrom synthesizer.hparams import hparams_debug_string\nfrom synthesizer.models.tacotron import Tacotron\nfrom synthesizer.synthesizer_dataset import SynthesizerDataset, collate_synthesizer\nfrom synthesizer.utils import data_parallel_workaround\nfrom synthesizer.utils.symbols import symbols\n\n\ndef run_synthesis(in_dir: Path, out_dir: Path, syn_model_fpath: Path, hparams):\n    # This generates ground truth-aligned mels for vocoder training\n    synth_dir = out_dir / \"mels_gta\"\n    synth_dir.mkdir(exist_ok=True, parents=True)\n    print(hparams_debug_string())\n\n    # Check for GPU\n    if torch.cuda.is_available():\n        device = torch.device(\"cuda\")\n        if hparams.synthesis_batch_size % torch.cuda.device_count() != 0:\n            raise ValueError(\"`hparams.synthesis_batch_size` must be evenly divisible by n_gpus!\")\n    else:\n        device = torch.device(\"cpu\")\n    print(\"Synthesizer using device:\", device)\n\n    # Instantiate Tacotron model\n    model = Tacotron(embed_dims=hparams.tts_embed_dims,\n                     num_chars=len(symbols),\n                     encoder_dims=hparams.tts_encoder_dims,\n                     decoder_dims=hparams.tts_decoder_dims,\n                     n_mels=hparams.num_mels,\n                     fft_bins=hparams.num_mels,\n                     postnet_dims=hparams.tts_postnet_dims,\n                     encoder_K=hparams.tts_encoder_K,\n                     lstm_dims=hparams.tts_lstm_dims,\n                     postnet_K=hparams.tts_postnet_K,\n                     num_highways=hparams.tts_num_highways,\n                     dropout=0., # Use zero dropout for gta mels\n                     stop_threshold=hparams.tts_stop_threshold,","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/CorentinJ/Real-Time-Voice-Cloning/blob/890f3a03187195b9829db2079b75c2ba2ab0405c/synthesizer/synthesize.py#L9-L45","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["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.","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.","Check for stray GPUs being visible (CUDA_VISIBLE_DEVICES=\"\" would make it CPU-only, avoiding the check entirely — only if CPU synthesis is acceptable)."],"exampleFix":"# before: 2 GPUs visible, batch size 11\nhparams.synthesis_batch_size = 11  # 11 % 2 != 0 -> ValueError\n\n# after\nimport torch\nhparams.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","handlingStrategy":"validation","validationCode":"import torch\n\ndef validate_batch_size(batch_size: int) -> int:\n    n = torch.cuda.device_count() if torch.cuda.is_available() else 1\n    return batch_size if n == 1 or batch_size % n == 0 else batch_size + (n - batch_size % n)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Choose batch sizes with many divisors (16, 24, 48, 96) so they survive 1/2/4/8-GPU machines.","Print torch.cuda.device_count() next to the batch size at job start.","Set CUDA_VISIBLE_DEVICES explicitly per run instead of relying on the node default."],"tags":["gpu","multi-gpu","synthesizer","hparams","training"],"backgroundTag":null,"analyzedSha":"890f3a03187195b9829db2079b75c2ba2ab0405c","analyzedAt":"2026-08-15T02:15:13.202Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}