{"record":{"id":"4b566159dd1fbd18","repo":"huggingface/candle","slug":"internal-error-unexpected-current-position-tenso","errorCode":null,"errorMessage":"internal error, unexpected current position {tensor_start_pos} {offset} {pos}","messagePattern":"internal error, unexpected current position (.+?) (.+?) (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/quantized/gguf_file.rs","lineNumber":627,"sourceCode":"        w.write_u32::<LittleEndian>(dims.len() as u32)?;\n        for &dim in dims.iter().rev() {\n            w.write_u64::<LittleEndian>(dim as u64)?;\n        }\n        w.write_u32::<LittleEndian>(tensor.dtype().to_u32())?;\n        w.write_u64::<LittleEndian>(offset as u64)?;\n        offsets.push(offset);\n        let size_in_bytes = tensor.storage_size_in_bytes();\n        let padding = 31 - (31 + size_in_bytes) % 32;\n        offset += size_in_bytes + padding;\n    }\n    let pos = w.stream_position()? as usize;\n    let padding = 31 - (31 + pos) % 32;\n    w.write_all(&vec![0u8; padding])?;\n    let tensor_start_pos = w.stream_position()? as usize;\n    for (offset, (_name, tensor)) in offsets.iter().zip(tensors.iter()) {\n        let pos = w.stream_position()? as usize;\n        if tensor_start_pos + offset != pos {\n            crate::bail!(\n                \"internal error, unexpected current position {tensor_start_pos} {offset} {pos}\"\n            )\n        }\n        let data = tensor.data()?;\n        let size_in_bytes = data.len();\n        w.write_all(&data)?;\n        let padding = 31 - (31 + size_in_bytes) % 32;\n        w.write_all(&vec![0u8; padding])?;\n    }\n    Ok(())\n}\n","sourceCodeStart":609,"sourceCodeEnd":639,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/quantized/gguf_file.rs#L609-L639","documentation":"During GGUF writing, candle computes exact byte offsets for each tensor's data and asserts the writer's stream position matches the expected offset before writing each tensor. A mismatch indicates an internal invariant violation (a bug in offset calculation or an I/O layer that skipped/inserted bytes), not user error.","triggerScenarios":"GgufFile::write when the underlying writer's position deviates from the computed tensor_start_pos + offset — e.g., a non-seekable or misbehaving writer adapter, or a genuine candle bug.","commonSituations":"Writing through custom wrapper writers that buffer or transform bytes; hitting a candle version bug in GGUF serialization offset alignment.","solutions":["Write directly to a File or BufWriter<File> instead of a custom wrapper writer","Update candle-core; if reproducible, file a bug with a minimal reproduction","Ensure no concurrent writes to the same underlying stream"],"exampleFix":"// before\nlet file = CustomFilteringWriter::new(std::fs::File::create(\"out.gguf\")?); // custom wrapper\n// after\nlet file = std::io::BufWriter::new(std::fs::File::create(\"out.gguf\")?);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match GgufFile::write(tensors, &mut w) {\n    Ok(_) => {},\n    Err(e) if e.to_string().contains(\"unexpected current position\") => {\n        eprintln!(\"gguf write offset invariant failed; retry with a plain File/BufWriter and report if reproducible\");\n        return Err(e.into());\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Write gguf output through std File/BufWriter, not custom transforming writers","Keep candle-core updated","Never share one writer across threads"],"tags":["gguf","internal-invariant","serialization"],"backgroundTag":"internal-invariant-violation","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}