huggingface / huggingface/candle
linear_split in candle-metal-kernels cannot handle empty tensors
- Dominant language
- Rust
- Stars
- 21k
- Forks
- 1.8k
- Avg merge
- 16h 42m
- Merged PRs (30d)
- 25
Description
Error:
`attempt to divide by zero`
Due to `width=0` in the linear_split function for embedding lookups on (1, 0) shaped tensors
```
pub(crate) fn linear_split(pipeline: &ComputePipelineState, length: usize) -> (MTLSize, MTLSize) {
let size = length as u64;
let width = std::cmp::min(pipeline.max_total_threads_per_threadgroup(), size);
let count = size.div_ceil(width);
// ...
}
```
`let count = size.div_ceil(width);` ← CRASH: Division by zero when width=0
When length = 0 (empty tensor)
- size = 0
- width = min(max_threads, 0) = 0
- count = 0.div_ceil(0)
Simple reproducer (on M1):
```
use candle_core::{DType, Device, Result, Tensor};
use candle_nn::{Embedding, Module};
fn main() -> Result<()> {
let device = Device::new_metal(0)?;
let empty = Tensor::zeros((1, 0), DType::I64, &device)?;
let embeddings = Tensor::randn(0f32, 1f32, (100, 512), &device)?;
let layer = Embedding::new(embeddings, 512);
layer.forward(&empty)?; // Panics here
Ok(())
}
```
Run above code with `RUST_BACKTRACE=full cargo run` command.
Got an error trace like this:
```
thread 'main' (3186775) panicked at /Users/skrohit/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/candle-metal-kernels-0.9.2/src/utils.rs:14:22:
attempt to divide by zero
stack backtrace:
0: 0x100ef8864 - ::fmt::h2808c764b780ef77
1: 0x100f05384 - core::fmt::write::h164e2e78980a3344
2: 0x100edcd08 - std::io::Write::write_fmt::hc5b2a4d160c11c86
3: 0x100ee0db8 - std::sys::backtrace::BacktraceLock::print::h765d16e49e83c7b3
4: 0x100ee2b28 - std::panicking::default_hook::{{closure}}::hf5d7ef6cefa10586
5: 0x100ee29dc - std::panicking::default_hook::h8781bd6c5c54e4cc
6: 0x100ee3268 - std::panicking::panic_with_hook::h93c775fc227522dd
7: 0x100ee2f28 - std::panicking::panic_handler::{{closure}}::h8561e58c9391724a
8: 0x100ee0ec4 - std::sys::backtrace::__rust_end_short_backtrace::h55629ed76aaa9d26
9: 0x100ed6070 - __rustc[d9b87f19e823c0ef]::rust_begin_unwind
10: 0x100f115b8 - core::panicking::panic_fmt::hfe9cfd57c06d80d9
11: 0x100f112d0 - core::panicking::panic_const::panic_const_div_by_zero::hc8ccfb6d43a5a054
12: 0x100ed3020 - candle_metal_kernels::utils::linear_split::h659a79ce8fb08644
13: 0x100ec91e8 - candle_metal_kernels::kernels::indexing::call_index_select::h72b8a493376ea6ea
14: 0x100ec2dd4 - ::index_select::hfc09bcd6d9db136a
15: 0x100ec5d6c - candle_core::storage::Storage::index_select::hd9aa41fe5905707c
16: 0x100eab064 - candle_core::tensor::Tensor::index_select::h95192d753d272ee2
17: 0x100eac9ac - ::forward::h7af583031bf6a3ab
18: 0x100ea8f90 - linear_split_test::main::h3ef7f9d49c370c77
19: 0x100ea9218 - std::sys::backtrace::__rust_begin_short_backtrace::h5fdfdae1d80bb671
20: 0x100ea9168 - std::rt::lang_start::{{closure}}::h51279ed7833ca87d
21: 0x100ede044 - std::rt::lang_start_internal::h1e9a2dfabf97dcbe
22: 0x100eaa960 - _main
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in candle-metal-kernels/src/utils.rs at linear_split, then follow the indexing path shown by the stack trace through call_index_select and Embedding::forward. Run the provided M1 reproducer with RUST_BACKTRACE=full and verify that forwarding an empty (1, 0) tensor no longer panics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100