Skip to content

Fix more cases for 0 sized tensors.#4878

Open
ArthurBrussee wants to merge 3 commits into
tracel-ai:mainfrom
ArthurBrussee:fix/slice-assign-empty-value
Open

Fix more cases for 0 sized tensors.#4878
ArthurBrussee wants to merge 3 commits into
tracel-ai:mainfrom
ArthurBrussee:fix/slice-assign-empty-value

Conversation

@ArthurBrussee
Copy link
Copy Markdown
Contributor

@ArthurBrussee ArthurBrussee commented Apr 25, 2026

shape == 0, the subtraction underflows and panics under overflow checks (e.g. debug builds, wasm tests).

On native this is hidden because ChannelDeviceHandle swallows worker-thread panics with catch_unwind. on wasm ReentrantMutexDeviceHandle propagates the panic and additionally poisons the device handle (the service cell stays None after the panic skips its replace), turning every later test into "State ... is already borrowed by the current thread". Fix: an empty tensor has no elements that can overlap, so return true early when any dim is zero.

For slice assign, the kernel does no useful work when value has 0 elements anyway, so just early-return. After tracel-ai/cubecl#1297 this wouldn't panic anymore anyway, but, might as well skip the work here entirely.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2026

Codecov Report

❌ Patch coverage is 0% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.62%. Comparing base (5c3669f) to head (2c4a5d9).
⚠️ Report is 101 commits behind head on main.

Files with missing lines Patch % Lines
...rates/burn-cubecl/src/kernel/index/slice_assign.rs 0.00% 9 Missing ⚠️
crates/burn-cubecl/src/tensor/base.rs 0.00% 3 Missing ⚠️

❌ Your patch check has failed because the patch coverage (0.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.
❌ Your project check has failed because the head coverage (65.62%) is below the target coverage (80.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4878      +/-   ##
==========================================
- Coverage   65.62%   65.62%   -0.01%     
==========================================
  Files        1161     1161              
  Lines      170422   170432      +10     
==========================================
  Hits       111839   111839              
- Misses      58583    58593      +10     

☔ 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.

@ArthurBrussee ArthurBrussee changed the title Skip empty slice_assign kernel launches Fix more cases for 0 sized tensors. Apr 25, 2026
Copy link
Copy Markdown
Member

@laggui laggui left a comment

Choose a reason for hiding this comment

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

Instead of being backend-specific logic in cubecl, we could add the early return check in the high-level API? The contract should be the same for all.

We should probably add more tests to cover 0 sized tensor operations.

/edit: going over pending PRs, I just realized this shouldn't trigger for slice_assign in any case as it currently stands. If the value tensor is empty, the slice range should be empty, and it's a no-op:

// Check if any slice produces 0 elements (empty assignment).
// Empty assignments are no-ops and would cause issues in backend implementations.
let is_empty_assignment = slices
.iter()
.enumerate()
.any(|(i, slice)| slice.output_size(shape[i]) == 0);
if is_empty_assignment {
return self;
}

If the slice range is not empty but the value tensor is, it would panic with a tensor operation error due to the mismatch.

But other kernels like select and select_assign don't have this check for 0 sized tensors which should be handled.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 6, 2026

This PR has been marked as stale because it has not been updated for over a month

@github-actions github-actions Bot added the stale The issue or pr has been open for too long label Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale The issue or pr has been open for too long

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants