huggingface / huggingface/candle

Additional Tensor Operations?

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

Description

I am trying to perform the following tensor operations [torch.cumsum()](https://pytorch.org/docs/stable/generated/torch.cumsum.html), and the quickest way i can think of is to convert the tensor to a vec and perform the operation using

```rust
fn cumsum_2d(mask: &Tensor, dim: u8, device: &Device) -> Result {
let mask = mask.to_vec2::()?;

let rows = mask.len();
let cols = mask[0].len();

let mut result = mask.clone();

match dim {
0 => {
// Cumulative sum along rows
for i in 0..rows {
for j in 1..cols {
result[i][j] += result[i][j - 1];
}
}
}
1 => {
// Cumulative sum along columns
for j in 0..cols {
for i in 1..rows {
result[i][j] += result[i - 1][j];
}
}
}
_ => panic!("Dimension not supported"),
}

let result = Tensor::new(result, &device)?;

Ok(result)
}
```

@LaurentMazare could you recommend a more effective way to perform this operation? and maybe `torch.split(tensor, split_size_or_sections, dim=0)` too..
Thanks!

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the Tensor API around to_vec2 and Tensor::new, then compare the linked torch.cumsum documentation. Clarify whether cumsum and torch.split are both in scope; done should mean these operations work without converting tensors to Vec values and their behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.