Skip to content

Fix: serialize block range claims - #1331

Merged
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
ChaoWao:codex/serialize-next-block-claims-main
Jul 11, 2026
Merged

ChaoWao merged 1 commit into
hw-native-sys:mainfrom
ChaoWao:codex/serialize-next-block-claims-main

Conversation

@ChaoWao

@ChaoWao ChaoWao commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add one CAS-based claim_block_range() helper for task block ranges.
  • Use it in normal ready dispatch, early-dispatch staging, and sync-start
    drain dispatch.
  • Cover concurrent claims and partial early-staged release routing.

Root cause

After a partially pre-staged task is released, normal ready dispatch can run
at the same time as a late early-dispatch stager. The stager already claims
through CAS, but normal dispatch used a relaxed load followed by a relaxed
store:

normal: load start=2
stager: CAS 2 -> 7, owns [2,7)
normal: store 5, owns [2,5)

That both overlaps a block range and moves next_block_idx backwards.
Routing every claimant through the same CAS loop makes ranges disjoint.

This race exists on main independently of #1304, so it is intentionally
submitted separately.

Testing

@coderabbitai

coderabbitai Bot commented Jul 11, 2026 •

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 133b67d9-6dc0-4f40-b5be-4b29e9df85fa

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The change adds an atomic block-range claim helper to task slots, updates scheduler dispatch paths to use it, revises staging comments, and adds tests for concurrent claims and partially staged early-dispatch routing.

Changes

Concurrent block claiming

Layer / File(s) Summary
Atomic block-range claim helper
src/a2a3/runtime/tensormap_and_ringbuffer/runtime/pto_runtime2_types.h
Adds CAS-based bounded range reservation through claim_block_range() and documents concurrent cursor advancement.
Scheduler dispatch integration
src/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_completion.cpp, src/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_dispatch.cpp, src/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_context.h
Routes worker, normal, and early dispatch through the shared claim helper while updating staging-order comments.
Concurrent and partial-release validation
tests/ut/cpp/a2a3/test_wiring.cpp
Tests non-overlapping concurrent claims and remainder routing after partial staging.

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

Sequence Diagram(s)

sequenceDiagram
  participant WorkerDispatch
  participant SchedulerDispatch
  participant PTO2TaskSlotState
  WorkerDispatch->>PTO2TaskSlotState: claim_block_range(block_num, valid.count(), start)
  SchedulerDispatch->>PTO2TaskSlotState: claim_block_range(logical_block_num, available, start)
  PTO2TaskSlotState-->>SchedulerDispatch: claimed contiguous range
  SchedulerDispatch->>SchedulerDispatch: publish or stage claimed blocks
Loading

Possibly related PRs

Poem

I’m a rabbit claiming blocks in a row,
With CAS hops quick as the winds that blow.
No overlap, no muddle, the ranges align,
Partial dispatch leaves a tail just fine.
Hop, scheduler—your queues now rhyme!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: serializing block range claims via a shared CAS-based helper.
Description check ✅ Passed The description accurately describes the CAS helper, affected dispatch paths, race condition, and added tests.
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.

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a thread-safe 'claim_block_range' helper method in 'PTO2TaskSlotState' using a CAS loop to manage concurrent block claims. It refactors the scheduler dispatch and completion logic to use this helper, ensuring safe concurrent execution, and adds corresponding unit tests. The feedback suggests a minor optimization in 'claim_block_range' to directly assign 'start' to 'current' upon a successful CAS, simplifying the logic and avoiding unnecessary arithmetic.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/a2a3/runtime/tensormap_and_ringbuffer/runtime/pto_runtime2_types.h Outdated
@ChaoWao
ChaoWao force-pushed the codex/serialize-next-block-claims-main branch from 0097c66 to 72833e0 Compare July 11, 2026 16:04
Normal ready dispatch can overlap a late early-dispatch stager
after a partial release. Its relaxed load/store could overwrite a CAS
claim and dispatch overlapping block ranges.

Route normal, early, and drain claims through one CAS helper. Cover
concurrent range claims and partial staged release to the ready queue.
@ChaoWao
ChaoWao force-pushed the codex/serialize-next-block-claims-main branch from 72833e0 to 2996448 Compare July 11, 2026 16:07
@ChaoWao
ChaoWao merged commit b2d1613 into hw-native-sys:main Jul 11, 2026
15 checks passed
@ChaoWao
ChaoWao deleted the codex/serialize-next-block-claims-main branch July 11, 2026 16:21
ChaoWao added a commit that referenced this pull request Jul 11, 2026
…gbuffer (#1335)

Follows PR #1327 (which synced hbg's scheduler to tmr's #1319 state). tmr then
landed five more early-dispatch changes on the same subsystem; this ports them
into host_build_graph's copies, preserving hbg's host-orchestration invariants
(single ring, no execution-time reclaim, flat Runtime, no ACK-gate, host-side
orchestrator).

Absorbed from tmr:
- #1304 Add early-dispatch for require_sync_start SPMD cohorts, incl. the
  drain_stage_cores parallel-drain refactor (handle_drain_mode two-stage).
- #1326 Defer early dispatch until producer publication (correctness/race):
  published_block_count + record_published_blocks; propagate_dispatch_fanin is
  gated on published_block_count >= logical_block_num; the published_list ledger
  in dispatch and the stage_consumer_blocks two-sided publication/release
  handshake.
- #1328 Shrink dispatch cold-write cost via a folded-gate src_payload
  (PTO2DispatchPayload: not_ready -> volatile uint64_t src_payload, 0 = ready,
  non-zero = gated source PTO2TaskPayload address; global_context to the tail;
  the PTO2_TASKPAYLOAD_*_OFFSET constants + static_asserts). build_payload writes
  args on the ready path and only src_payload on the gated path; the idle AICore
  fills args[] from src_payload during its doorbell wait; init-time prefill of
  async_ctx slab pointers/capacity + context-ptr args; deferred_slab reset moved
  off the dispatch path; software-pipelined prefetch of the next block.
- #1329 Make early-dispatch doorbell ownership exclusive (correctness).
- #1331 Serialize block range claims (correctness): every block-range claim now
  goes through slot_state->claim_block_range(...) instead of an inline
  next_block_idx CAS.

Preserved host-orch divergences (not overwritten by tmr): PTO2_MAX_RING_DEPTH==1
with rings[]/ring_id removed; no advance_ring_pointers / reset_for_reuse /
check_and_handle_consumed (completion via completed_tasks_, consumer wait keys on
fanout_refcount); flat runtime->workers / runtime->func_id_to_addr_ (not
runtime->dev.*); pto_runtime2_types.h keeps its dropped pto2_dispatch_payload.h
include; hbg's AICore FIN-vs-swimlane-record ordering and its no-ACK-gate
completion path (drains via completion-before-dispatch + backstop flushes); no
device orchestrator (orchestrator_done_ absent, scheduler threads gate on
runtime_init_ready_).

Out of scope (unrelated tmr changes hbg still lacks, tracked for a follow-up):
per-device scheduler timeout (get_scheduler_timeout_ms), PTO2_TENSOR_DATA_TIMEOUT
MS-vs-cycles (#1189), stall sub-classification (#1182), runtime scope_tasks_cap
(#1188).

Verified: all 8 runtime targets build -Werror; a2a3sim host_build_graph scene
suite 10/10; a2a3 onboard host_build_graph suite 10 passed / 1 skipped
(paged_attention exercises the new claim_block_range serialization, drain_stage_cores
drain, src_payload gated arg-fill, and published-block gating). tensormap_and_ringbuffer
and the a5 runtimes are untouched.
doraemonmj pushed a commit to doraemonmj/simpler_wc that referenced this pull request Jul 14, 2026
Normal ready dispatch can overlap a late early-dispatch stager
after a partial release. Its relaxed load/store could overwrite a CAS
claim and dispatch overlapping block ranges.

Route normal, early, and drain claims through one CAS helper. Cover
concurrent range claims and partial staged release to the ready queue.
doraemonmj pushed a commit to doraemonmj/simpler_wc that referenced this pull request Jul 14, 2026
…gbuffer (hw-native-sys#1335)

Follows PR hw-native-sys#1327 (which synced hbg's scheduler to tmr's hw-native-sys#1319 state). tmr then
landed five more early-dispatch changes on the same subsystem; this ports them
into host_build_graph's copies, preserving hbg's host-orchestration invariants
(single ring, no execution-time reclaim, flat Runtime, no ACK-gate, host-side
orchestrator).

Absorbed from tmr:
- hw-native-sys#1304 Add early-dispatch for require_sync_start SPMD cohorts, incl. the
  drain_stage_cores parallel-drain refactor (handle_drain_mode two-stage).
- hw-native-sys#1326 Defer early dispatch until producer publication (correctness/race):
  published_block_count + record_published_blocks; propagate_dispatch_fanin is
  gated on published_block_count >= logical_block_num; the published_list ledger
  in dispatch and the stage_consumer_blocks two-sided publication/release
  handshake.
- hw-native-sys#1328 Shrink dispatch cold-write cost via a folded-gate src_payload
  (PTO2DispatchPayload: not_ready -> volatile uint64_t src_payload, 0 = ready,
  non-zero = gated source PTO2TaskPayload address; global_context to the tail;
  the PTO2_TASKPAYLOAD_*_OFFSET constants + static_asserts). build_payload writes
  args on the ready path and only src_payload on the gated path; the idle AICore
  fills args[] from src_payload during its doorbell wait; init-time prefill of
  async_ctx slab pointers/capacity + context-ptr args; deferred_slab reset moved
  off the dispatch path; software-pipelined prefetch of the next block.
- hw-native-sys#1329 Make early-dispatch doorbell ownership exclusive (correctness).
- hw-native-sys#1331 Serialize block range claims (correctness): every block-range claim now
  goes through slot_state->claim_block_range(...) instead of an inline
  next_block_idx CAS.

Preserved host-orch divergences (not overwritten by tmr): PTO2_MAX_RING_DEPTH==1
with rings[]/ring_id removed; no advance_ring_pointers / reset_for_reuse /
check_and_handle_consumed (completion via completed_tasks_, consumer wait keys on
fanout_refcount); flat runtime->workers / runtime->func_id_to_addr_ (not
runtime->dev.*); pto_runtime2_types.h keeps its dropped pto2_dispatch_payload.h
include; hbg's AICore FIN-vs-swimlane-record ordering and its no-ACK-gate
completion path (drains via completion-before-dispatch + backstop flushes); no
device orchestrator (orchestrator_done_ absent, scheduler threads gate on
runtime_init_ready_).

Out of scope (unrelated tmr changes hbg still lacks, tracked for a follow-up):
per-device scheduler timeout (get_scheduler_timeout_ms), PTO2_TENSOR_DATA_TIMEOUT
MS-vs-cycles (hw-native-sys#1189), stall sub-classification (hw-native-sys#1182), runtime scope_tasks_cap
(hw-native-sys#1188).

Verified: all 8 runtime targets build -Werror; a2a3sim host_build_graph scene
suite 10/10; a2a3 onboard host_build_graph suite 10 passed / 1 skipped
(paged_attention exercises the new claim_block_range serialization, drain_stage_cores
drain, src_payload gated arg-fill, and published-block gating). tensormap_and_ringbuffer
and the a5 runtimes are untouched.
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