{"record":{"id":"b0fc06149fe094b7","repo":"microsoft/VibeVoice","slug":"sample-index-sample-idx-exceeds-batch-size-self","errorCode":null,"errorMessage":"Sample index {sample_idx} exceeds batch size {self.batch_size}","messagePattern":"Sample index (.+?) exceeds batch size (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"vibevoice/modular/streamer.py","lineNumber":85,"sourceCode":"                if not self.finished_flags[idx]:\n                    self.audio_queues[idx].put(self.stop_signal, timeout=self.timeout)\n                    self.finished_flags[idx] = True\n        else:\n            # End specific samples\n            for sample_idx in sample_indices:\n                idx = sample_idx.item() if torch.is_tensor(sample_idx) else sample_idx\n                if idx < self.batch_size and not self.finished_flags[idx]:\n                    self.audio_queues[idx].put(self.stop_signal, timeout=self.timeout)\n                    self.finished_flags[idx] = True\n    \n    def __iter__(self):\n        \"\"\"Returns an iterator over the batch of audio streams.\"\"\"\n        return AudioBatchIterator(self)\n    \n    def get_stream(self, sample_idx: int):\n        \"\"\"Get the audio stream for a specific sample.\"\"\"\n        if sample_idx >= self.batch_size:\n            raise ValueError(f\"Sample index {sample_idx} exceeds batch size {self.batch_size}\")\n        return AudioSampleIterator(self, sample_idx)\n\n\nclass AudioSampleIterator:\n    \"\"\"Iterator for a single audio stream from the batch.\"\"\"\n    \n    def __init__(self, streamer: AudioStreamer, sample_idx: int):\n        self.streamer = streamer\n        self.sample_idx = sample_idx\n        \n    def __iter__(self):\n        return self\n    \n    def __next__(self):\n        value = self.streamer.audio_queues[self.sample_idx].get(timeout=self.streamer.timeout)\n        if value == self.streamer.stop_signal:\n            raise StopIteration()\n        return value","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/microsoft/VibeVoice/blob/94da20d98b2fa7688e9cbfaf7692ddb4954f7600/vibevoice/modular/streamer.py#L67-L103","documentation":"The synchronous AudioStreamer distributes generated audio into one queue per batch element. get_stream(sample_idx) returns a single sample's iterator and validates the index against the streamer's batch size. A zero-or-greater index equal to or above batch_size fails immediately; note that negative indices are NOT caught by this check even though they will misbehave.","triggerScenarios":"Calling streamer.get_stream(i) where i >= streamer.batch_size, e.g. iterating range(len(audio_files)) when the model was actually run with a smaller batch (batch dim collapsed by batching/padding logic), or hardcoding an index for a batch-1 run.","commonSituations":"User runs generation with batch size 1 but loops over multiple prompts calling get_stream(idx); or the batch dimension was squeezed out upstream so the streamer was constructed with batch_size=1 while the caller assumes N streams.","solutions":["Use streamer.batch_size as the loop bound: for i in range(streamer.batch_size): stream = streamer.get_stream(i).","Verify the batch dimension actually passed to the model matches the number of prompts you expect (print the input tensor's shape[0]).","For a single prompt, use get_stream(0) or iterate the streamer directly (AudioBatchIterator)."],"exampleFix":"# before\nfor i in range(num_prompts):  # num_prompts > batch_size\n    stream = streamer.get_stream(i)\n\n# after\nfor i in range(streamer.batch_size):\n    stream = streamer.get_stream(i)","handlingStrategy":"validation","validationCode":"idx = int(idx)\nif not (0 <= idx < streamer.batch_size):\n    raise IndexError(f'stream index {idx} outside [0, {streamer.batch_size})')\nstream = streamer.get_stream(idx)","typeGuard":"def is_valid_stream_index(streamer, idx) -> bool:\n    return isinstance(idx, int) and 0 <= idx < streamer.batch_size","tryCatchPattern":"try:\n    stream = streamer.get_stream(idx)\nexcept ValueError as e:\n    if 'exceeds batch size' in str(e):\n        logger.warning('batch shrank; clamping to last stream')\n        stream = streamer.get_stream(streamer.batch_size - 1)\n    else:\n        raise","preventionTips":["Always iterate range(streamer.batch_size), never a separately tracked prompt count.","Log the model input's shape[0] when constructing the streamer to keep batch size traceable.","Remember negative indices pass this check but are invalid — assert non-negativity yourself."],"tags":["streaming","indexing","batch","audio"],"backgroundTag":null,"analyzedSha":"94da20d98b2fa7688e9cbfaf7692ddb4954f7600","analyzedAt":"2026-08-15T04:12:07.418Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}