Skip to content

[CUDA][Reduce] Restore exact thread image counting - #2825

Open
KellyFrog wants to merge 1 commit into
tile-ai:mainfrom
KellyFrog:fix/reduce-z3-counting
Open

[CUDA][Reduce] Restore exact thread image counting#2825
KellyFrog wants to merge 1 commit into
tile-ai:mainfrom
KellyFrog:fix/reduce-z3-counting

Conversation

@KellyFrog

@KellyFrog KellyFrog commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

First see & merge tile-ai/tvm#61 .

Created safe fallback for counting threads in src/backend/common/op/reduce.h when determinating named barrier count.

Summary

  • Restores exact CUDA thread-image counting for reduction named barriers.
  • Uses Z3 to count distinct participating thread IDs.
  • Falls back to the bounding span when Z3 returns unknown.
  • Rejects unsatisfiable or non-contiguous thread images.
  • Updates the 3rdparty/tvm submodule. Merge tile-ai/tvm#61 first.

C++ style / lint notes

  • The PR changes C++ code in src/backend/common/op/reduce.h.
  • It does not change the rules documented in docs/developer_guide/cpp_style.md.
  • The “C++ API Style Audit (warning only)” CI step is not affected.
  • The changes introduce no known correctness, build, or test issues. Advisory style warnings remain non-blocking.

@github-actions

Copy link
Copy Markdown

👋 Hi! Thank you for contributing to the TileLang project.

Please remember to run pre-commit run --all-files in the root directory of the project to ensure your changes are properly linted and formatted. This will help ensure your contribution passes the format check.

We appreciate you taking this step! Our team will review your contribution, and we look forward to your awesome work! 🚀

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR updates the TVM submodule reference and changes scalar AllReduce thread-range resolution to use Z3 for exact, contiguous thread participation checks, with a bounding-span fallback when the prover returns unknown.

Changes

AllReduce thread resolution

Layer / File(s) Summary
TVM reference update
3rdparty/tvm
The TVM submodule pointer now references commit 6bc7a8f0b22c1f493af4a0ccd661f282c478dc74.
Z3-based thread analysis
src/backend/common/op/reduce.h
ResolveAllReduceThreadRange constrains and enumerates thread expressions with Z3. It validates nonempty, contiguous thread images and falls back to the bounding span when Z3 returns unknown.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ResolveAllReduceThreadRange
  participant ReduceLayout
  participant Z3
  ResolveAllReduceThreadRange->>ReduceLayout: read thread expression and bounds
  ResolveAllReduceThreadRange->>Z3: constrain and enumerate thread expression
  Z3-->>ResolveAllReduceThreadRange: distinct thread count or unknown
  ResolveAllReduceThreadRange->>ResolveAllReduceThreadRange: validate contiguous image or use bounding span
Loading

Possibly related PRs

Suggested reviewers: penguin-wwy

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: restoring exact thread-image counting for CUDA reductions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/backend/common/op/reduce.h`:
- Around line 148-160: The scalar AllReduce lowering must not treat an unknown
distinct-thread count as proof that the bounding range is contiguous: update the
count handling around the existing count validation to fail lowering when the
result is unknown, unless an independent proof establishes every thread in
[base, end] participates. At src/backend/common/op/reduce.h lines 148-160,
remove the fallback that assigns span for unknown results; at lines 82-88,
update the documentation only if the implementation is intentionally changed to
accept an unproven bounding span.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ae4ffb4c-b7ff-4f99-ac85-64590556d169

📥 Commits

Reviewing files that changed from the base of the PR and between bdb769a and 9f49c08.

📒 Files selected for processing (2)
  • 3rdparty/tvm
  • src/backend/common/op/reduce.h

Comment on lines +148 to +160
if (count < 0) {
LOG(WARNING) << "tl.reduce: Z3 returned unknown while counting scalar "
"AllReduce participating threads; falling back to the "
"bounding range ["
<< base << ", " << end << "].";
count = span;
}
ICHECK_GT(count, 0)
<< "tl.reduce: scalar AllReduce participating thread constraints are "
"unsatisfiable";
ICHECK_EQ(count, span)
<< "tl.reduce: partial scalar AllReduce requires one contiguous thread "
"range, but got "

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.

🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

Reject an unknown result unless contiguity is proven.

Line 153 replaces the unknown distinct-thread count with span. Line 158 then always succeeds on this path. A sparse image, such as {base, base + 2}, can therefore configure a named barrier for three threads while only two threads execute the guarded reduction. The kernel can deadlock.

  • src/backend/common/op/reduce.h#L148-L160: Do not use span as proof of contiguity. Fail lowering on an unknown result, or add an independent proof that every thread in [base, end] executes the reduction.
  • src/backend/common/op/reduce.h#L82-L88: Update the documentation if the implementation can accept an unproven bounding span.
📍 Affects 1 file
  • src/backend/common/op/reduce.h#L148-L160 (this comment)
  • src/backend/common/op/reduce.h#L82-L88
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/backend/common/op/reduce.h` around lines 148 - 160, The scalar AllReduce
lowering must not treat an unknown distinct-thread count as proof that the
bounding range is contiguous: update the count handling around the existing
count validation to fail lowering when the result is unknown, unless an
independent proof establishes every thread in [base, end] participates. At
src/backend/common/op/reduce.h lines 148-160, remove the fallback that assigns
span for unknown results; at lines 82-88, update the documentation only if the
implementation is intentionally changed to accept an unproven bounding span.

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.

1 participant