Skip to content

fix(cubecl): race-free bool scatter_nd reductions with repeated targets - #6215

Open
HarpeLm wants to merge 2 commits into
tracel-ai:mainfrom
HarpeLm:fix/cubecl-bool-scatter-nd-duplicates
Open

HarpeLm wants to merge 2 commits into
tracel-ai:mainfrom
HarpeLm:fix/cubecl-bool-scatter-nd-duplicates

Conversation

@HarpeLm

@HarpeLm HarpeLm commented Oct 24, 2026 •

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. (No book change needed.)
  • Added or updated changelog entries for user-visible changes, if applicable, following the
    changelog policy in CONTRIBUTING.md.

Related Issues/PRs

Changes

scatter_nd_kernel runs one unit per update element and does a plain read-modify-write, so with repeated targets two units can read the same old value and one update is lost. On bool, Add/Max (or) and Mul/Min (and) went through that path as Max/Min.

Or and and have an absorbing value, so for bool the kernel now writes only that value and skips the rest (as suggested by @antimora in tracel-ai/burn-onnx#654): true for or, false for and. Every concurrent write to a position then stores the same value, so the result no longer depends on scheduling, without atomics. This is a #[comptime] ScatterNdWrite mode on the existing kernel (Reduce keeps the read-modify-write for numeric data and bool Assign); bool_scatter_nd selects it.

Testing

  • New test_bool_scatter_nd_{or,and}_with_repeated_targets: 2048 updates on each of two positions, with a single update that differs from the rest, for all four reductions.
    • On Metal (--features metal) before the fix, both fail on every run (20/20): or loses the true (Position 0: 0 != 1), and loses the false (Position 1: 1 != 0).
    • After the fix: 50/50 runs pass on Metal, and every scatter / scatter_nd test passes on Metal (77) and Flex (89).
  • cargo run-checks: passes. One earlier run failed on f16::module::attention::test_attention_prompt_transformer_heads (abs diff 1.26e-3 vs tol 1e-3); that test fails 2 runs out of 3 of --test tensor_f16 on main too, so it's unrelated.
  • Not run: CUDA/ROCm (same kernel, no device here).

AI Usage

I used Claude Code for most of the work: it reproduced the race on Metal, wrote the kernel change, the tests and this description, and ran the checks. I followed the work step by step and decided what to submit.

The scatter_nd kernel applies each update element with a plain read-modify-write, so
when indices repeat a target two units can read the same old value and one update is
lost. For bool, or (Add/Max) and and (Mul/Min) have an absorbing value: the kernel now
writes only that value (true for or, false for and) and skips the rest, so every
concurrent write to a position stores the same value and the result no longer depends
on scheduling. Numeric reductions keep the read-modify-write.

Part of tracel-ai#6213.
Copilot AI balanced review requested due to automatic review settings October 24, 2026 18:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 43.28358% with 38 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.16%. Comparing base (dd31726) to head (5e35f2e).

Files with missing lines Patch % Lines
crates/burn-cubecl/src/kernel/index/scatter_nd.rs 0.00% 37 Missing ⚠️
crates/burn-cubecl/src/ops/bool_tensor.rs 0.00% 1 Missing ⚠️

❌ Your patch check has failed because the patch coverage (43.28%) 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 (66.16%) 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    #6215      +/-   ##
==========================================
- Coverage   66.16%   66.16%   -0.01%     
==========================================
  Files         878      878              
  Lines      127748   127806      +58     
==========================================
+ Hits        84528    84557      +29     
- Misses      43220    43249      +29     

☔ 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