{"record":{"id":"4a25706ac57abdf3","repo":"huggingface/candle","slug":"invalid-audio-position-for-tensor-shape","errorCode":null,"errorMessage":"Invalid audio position: ({}, {}) for tensor shape ({}, {}, {})","messagePattern":"Invalid audio position: \\((.+?), (.+?)\\) for tensor shape \\((.+?), (.+?), (.+?)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-transformers/src/models/voxtral/model.rs","lineNumber":197,"sourceCode":"            audio_embeds.i(0..num_audio_tokens)?\n        }\n    } else {\n        candle::bail!(\n            \"Not enough audio embeddings: need {}, got {}. Input sequence should have {} audio tokens.\",\n            num_audio_tokens,\n            total_audio_embeds,\n            total_audio_embeds\n        );\n    };\n\n    // Create result tensor starting with text embeddings\n    let mut result = inputs_embeds.clone();\n\n    // Replace audio tokens with audio embeddings\n    // Since we don't have scatter operations, we'll do this manually\n    for (idx, &(batch_idx, seq_idx)) in audio_positions.iter().enumerate() {\n        if batch_idx >= batch_size || seq_idx >= seq_len {\n            candle::bail!(\n                \"Invalid audio position: ({}, {}) for tensor shape ({}, {}, {})\",\n                batch_idx,\n                seq_idx,\n                batch_size,\n                seq_len,\n                hidden_size\n            );\n        }\n\n        // Get the audio embedding for this position\n        let audio_embed = audio_embeds.i(idx)?;\n\n        // Create a mask for this specific position\n        let mut position_mask = vec![0f32; batch_size * seq_len];\n        position_mask[batch_idx * seq_len + seq_idx] = 1.0;\n        let position_mask = Tensor::new(position_mask.as_slice(), device)?\n            .reshape((batch_size, seq_len, 1))?\n            .to_dtype(inputs_embeds.dtype())?;","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-transformers/src/models/voxtral/model.rs#L179-L215","documentation":"While replacing audio placeholder tokens with embeddings, each (batch_idx, seq_idx) position is checked against the (batch_size, seq_len) of inputs_embeds. An out-of-range position bails with the offending coordinates and tensor shape (batch, seq, hidden). This guards against indexing a tensor out of bounds due to a bad audio position mapping.","triggerScenarios":"Calling forward with audio_positions containing coordinates beyond the inputs_embeds batch or sequence dimension — e.g. positions computed from a tokenized sequence longer/shorter than the embedding tensor passed in.","commonSituations":"Building audio_positions from input_ids but passing a truncated/padded inputs_embeds; batching mismatch (positions computed for a different batch item); pre/post processing bug inserting fewer or more tokens than positions reference.","solutions":["Regenerate audio_positions from the exact same inputs_embeds tensor passed to forward","Check padding/truncation in preprocessing so positions and embeddings stay aligned","Add an assertion on max(audio_positions) vs (batch_size, seq_len) before calling forward"],"exampleFix":"// before\nlet positions = positions_from_tokens(&input_ids);\nmodel.forward(&inputs_embeds_truncated, &positions, ...)?;\n// after\nassert!(positions.iter().all(|&(b, s)| b < batch && s < seq));\nmodel.forward(&inputs_embeds, &positions, ...)?;","handlingStrategy":"validation","validationCode":"let (b, s) = inputs_embeds.dims2()?; // or dims3\nif let Some(&(bi, si)) = audio_positions.iter().max_by_key(|&&(b, s)| (b, s)) {\n    if bi >= b || si >= s { return Err(anyhow::anyhow!(\"audio position ({bi},{si}) out of shape ({b},{s})\")); }\n}","typeGuard":null,"tryCatchPattern":"match model.forward_with_audio(&embeds, &positions, &audio) {\n    Err(e) if e.to_string().contains(\"Invalid audio position\") => {\n        anyhow::bail!(\"audio_positions misaligned with inputs_embeds; regenerate them\")\n    }\n    r => r?,\n}","preventionTips":["Generate audio_positions from the exact tensors passed to forward","Avoid truncating/padding inputs after positions are computed","Unit-test position generation against batched inputs"],"tags":["rust","candle","voxtral","audio","index-out-of-range"],"backgroundTag":"index-out-of-bounds","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}