Skip to content

fix(tensor): return fewer chunks instead of panicking in chunk - #6263

Merged
laggui merged 2 commits into
tracel-ai:mainfrom
b84330808:fix-chunk-uneven-panic
Oct 28, 2026
Merged

laggui merged 2 commits into
tracel-ai:mainfrom
b84330808:fix-chunk-uneven-panic

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

Checklist

  • I have reviewed and understand the changes in this PR.
  • Confirmed that cargo run-checks has been executed.
  • Made sure the book is up to date with changes in this PR.
  • Added or updated changelog entries for user-visible changes, if applicable, following the changelog policy in CONTRIBUTING.md.

Related Issues/PRs

None, found while testing edge cases of chunk.

Changes

Tensor::chunk panics in narrow when fewer chunks than requested already cover the dimension: arange(0..6).chunk(4, 0) (the last chunk would be empty) and arange(0..7).chunk(5, 0) (it would run past the end). It built chunks - 1 chunks of ceil(size / chunks) and gave the last one size % chunk_size elements.

The docs already say chunk may return fewer chunks, and PyTorch gives [2, 2, 2] and [2, 2, 2, 1] here. chunk now uses ceil(size / chunks) as the chunk size and delegates to split. Inputs that worked before give the same result.

Testing

  • New test_chunk_fewer_chunks_without_remainder (6 into 4) and test_chunk_fewer_chunks_with_remainder (7 into 5): both panic on main, pass with the fix.
  • cargo test --no-default-features --features flex,std --test tensor (Flex) passes.
  • cargo fmt --check and clippy on burn-tensor are clean. I didn't run the full cargo run-checks or the GPU backends.

AI Usage

Claude Code (an AI agent) found this while probing tensor edge cases, wrote the fix, tests, changelog entry and this description, and opened this PR, mostly autonomously.

`chunk` built `chunks - 1` chunks of `ceil(size / chunks)` and gave the
last one `size % chunk_size` elements. When fewer chunks are enough to
cover the dimension, that last `narrow` is empty (6 elements into 4
chunks) or runs past the end (7 into 5), and both panic.

Use `ceil(size / chunks)` as the chunk size and let `split` produce the
chunks, so the last chunk holds the rest and fewer chunks are returned,
as the documentation already says.

@laggui laggui left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

@laggui
laggui merged commit cc5f3ef into tracel-ai:main Oct 28, 2026
12 of 13 checks passed
@codecov

codecov Bot commented Oct 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.63%. Comparing base (5856e07) to head (f0041ec).
⚠️ Report is 13 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6263   +/-   ##
=======================================
  Coverage   66.63%   66.63%           
=======================================
  Files         875      875           
  Lines      129889   129903   +14     
=======================================
+ Hits        86547    86561   +14     
  Misses      43342    43342           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants