huggingface / huggingface/candle

bug: index_select() does not work on CUDA if index array has zero elements

Open
#2,673 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
21.1k
Forks
1.8k
Avg merge
16h 42m
Merged PRs (30d)
25

Description

Here's a minimal reproduction:

```rust
use candle_core::{Device, Tensor};

pub fn main() -> candle_core::Result<()> {
let device = Device::Cpu;
let indices = Tensor::from_vec::<_, u32>(vec![], 0, &device)?;
let sample = Tensor::randn(0., 1., (3, 3, 3), &device)?;

// works fine, produces a result with shape (0, 3, 3)
let result = sample.index_select(&indices, 0)?;
dbg!(result);

let device = Device::new_cuda(0)?;
let indices = Tensor::from_vec::<_, u32>(vec![], 0, &device)?;
let sample = Tensor::randn(0., 1., (3, 3, 3), &device)?;

// crashes with Cuda(Cuda(DriverError(CUDA_ERROR_INVALID_VALUE, "invalid argument")))
let result = sample.index_select(&indices, 0)?;
dbg!(result);

Ok(())
}
```

`candle-core` version is 0.8.1

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the issue with the Rust example in the report, then trace the CUDA implementation of Tensor::index_select and its handling of zero-length index tensors. Compare it with the CPU behavior and run the relevant CUDA tests or add coverage for an empty index array; done means the operation returns a tensor shaped (0, 3, 3) without a CUDA error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend, machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.