Skip to content

Refactor: derive tmr scheduler thread/core dims from platform_config - #1735

Merged
ChaoZheng109 merged 1 commit into
hw-native-sys:mainfrom
ChaoZheng109:fix/issue-1734-derive-scheduler-dims-from-platform-config
Aug 10, 2026
Merged

Refactor: derive tmr scheduler thread/core dims from platform_config#1735
ChaoZheng109 merged 1 commit into
hw-native-sys:mainfrom
ChaoZheng109:fix/issue-1734-derive-scheduler-dims-from-platform-config

Conversation

@ChaoZheng109

Copy link
Copy Markdown
Collaborator

Fixes #1734

Three hardware-derived dimensions in the tensormap_and_ringbuffer scheduler were written as bare numeric literals even though platform_config.h already defines the exact named constant they equal. The literals obscured that a2a3 and a5 share one rule, read as divergences when they are not, and would go silently wrong if a platform constant changed. Landed across both trees in one commit per codestyle.md rule 10.

No behavior change — every derived value equals the current literal on both arches today (PLATFORM_MAX_AICPU_THREADS = 4/5, PLATFORM_CORES_PER_BLOCKDIM = 3, PLATFORM_MAX_CORES = 72/108).

Changes

G — dummy-drain thread gate (scheduler_dispatch.cpp)
thread_idx < 3 (a2a3) / thread_idx < 4 (a5) → thread_idx < PLATFORM_MAX_AICPU_THREADS - 1 — "every scheduler thread except the last".

M1 — RUNTIME_MAX_WORKER (runtime.h + scheduler_context.h)
72 / 108PLATFORM_MAX_CORES. Both the authoritative unguarded define in runtime.h and the #ifndef fallback in scheduler_context.h are changed: the runtime.h define is unguarded, so leaving it a literal while the fallback expands to the constant is a mismatched macro redefinition that -Werror rejects. (The issue's Location listed only scheduler_context.h; runtime.h had to move with it for the tree to compile.)

M2 — cores-per-cluster 3 (scheduler_types.h + scheduler_cold_path.cpp)
The semantic cluster_count_ * 3 (core_num()), the two cores_total_num_ / 3, and the % 3 != 0 validity check → PLATFORM_CORES_PER_BLOCKDIM; the "multiple of 3" error text now interpolates the constant with %d. The self-documenting bit(i*3+k) / core_id_map_[cluster_idx*3+k] bitmask-layout arithmetic is left as-is, per the issue's scope note.

Both trees now read identically at every changed site.

Testing

Rebuilt both runtimes (pip install --no-build-isolation -e .) and ran tmr sim scene tests on both arches:

Test a2a3sim a5sim Exercises
dummy_task G dummy-drain gate
available_aicore_counts M2 core-count / multiple-of-3 validation
mixed_example general scheduling / dispatch

@coderabbitai

coderabbitai Bot commented Aug 7, 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 Plus

Run ID: 787c851e-f836-404b-be27-062c803b0466

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 scheduler runtime now derives worker capacity, core-layout calculations, validation, and dummy-drain thread eligibility from platform configuration constants in both the a2a3 and a5 trees.

Changes

Scheduler dimension alignment

Layer / File(s) Summary
Platform-derived worker capacity
src/a2a3/.../runtime.h, src/a2a3/.../scheduler/scheduler_context.h, src/a5/.../runtime.h, src/a5/.../scheduler/scheduler_context.h
RUNTIME_MAX_WORKER now uses PLATFORM_MAX_CORES instead of fixed platform-specific values.
Platform-derived core layout
src/{a2a3,a5}/.../scheduler/scheduler_cold_path.cpp, src/{a2a3,a5}/.../scheduler/scheduler_types.h
Core counts, cluster sizing, and divisibility validation now use PLATFORM_CORES_PER_BLOCKDIM.
Platform-derived dummy-drain gate
src/{a2a3,a5}/.../scheduler/scheduler_dispatch.cpp
Dummy completion draining now runs on every AICPU scheduler thread except the final platform thread.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Poem

A rabbit hops through cores in line,
With platform constants keeping time.
Three becomes a named decree,
Threads drain neatly, wild and free.
Worker bounds now fit the design—
Thump, thump, the schedulers align!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the refactor to derive scheduler thread and core dimensions from platform configuration.
Description check ✅ Passed The description directly explains the platform-constant refactor, affected files, preserved behavior, and validation results.
Linked Issues check ✅ Passed The changes satisfy issue #1734 by replacing the specified scheduler magic literals across both architecture trees while preserving bitmask arithmetic.
Out of Scope Changes check ✅ Passed All changes support issue #1734; the runtime.h updates are necessary to keep the authoritative worker-limit definitions consistent.
Docstring Coverage ✅ Passed Docstring coverage is 85.71% which is sufficient. The required threshold is 80.00%.

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

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/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_cold_path.cpp`:
- Line 903: Make semantic cluster-width handling consistent with
PLATFORM_CORES_PER_BLOCKDIM in assign_own_clusters() and
assign_cores_to_threads() across both files:
src/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_cold_path.cpp
at lines 903, 803, and 1154-1158, and
src/a5/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_cold_path.cpp
at lines 900, 800, and 1135-1139. Replace remaining c * 3, sub < 3, N/3, and
multiple-of-3 logic and diagnostics with the platform-derived width, or isolate
any intentional fixed bitmask offsets behind a named constant with an explicit
invariant; keep AIC-count division and downstream traversal aligned.
🪄 Autofix

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: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 441f9e08-fd2d-4706-a164-d54d3d15babd

📥 Commits

Reviewing files that changed from the base of the PR and between 2990441 and eaa120d.

📒 Files selected for processing (10)
  • src/a2a3/runtime/tensormap_and_ringbuffer/runtime/runtime.h
  • src/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_cold_path.cpp
  • src/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_context.h
  • src/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_dispatch.cpp
  • src/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_types.h
  • src/a5/runtime/tensormap_and_ringbuffer/runtime/runtime.h
  • src/a5/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_cold_path.cpp
  • src/a5/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_context.h
  • src/a5/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_dispatch.cpp
  • src/a5/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_types.h

@ChaoZheng109
ChaoZheng109 force-pushed the fix/issue-1734-derive-scheduler-dims-from-platform-config branch from eaa120d to 3d6a279 Compare August 10, 2026 02:31
Fixes hw-native-sys#1734

Three hardware-derived dimensions in the tensormap_and_ringbuffer
scheduler were written as bare numeric literals even though
platform_config.h already defines the exact named constant they equal.
The literals obscured that a2a3 and a5 share one rule, read as
divergences when they are not, and would go silently wrong if a platform
constant changed. Every value below is identical to the prior literal on
both arches, so there is no behavior change.

- Dummy-drain gate: `thread_idx < 3` (a2a3) / `thread_idx < 4` (a5) ->
  `thread_idx < PLATFORM_MAX_AICPU_THREADS - 1` ("every scheduler thread
  except the last"; MAX is 4 / 5).
- RUNTIME_MAX_WORKER: `72` / `108` -> `PLATFORM_MAX_CORES`. Changed in
  both the authoritative runtime.h define and the scheduler_context.h
  `#ifndef` fallback: the runtime.h define is unguarded, so leaving it a
  literal while the fallback expands to the constant is a mismatched
  macro redefinition that -Werror rejects.
- Cores-per-cluster: every semantic cluster-width site ->
  PLATFORM_CORES_PER_BLOCKDIM (== 3). This covers `core_num()`'s
  `cluster_count_ * 3`, the `cores_total_num_ / 3` AIC-count divisions,
  the `% 3 != 0` validity check (its error text now interpolates the
  constant), the CoreTracker cluster strides (`cli * 3`, `c * 3`,
  `c * 3 + sub`, `max_clusters_per_thread * 3`) and the per-cluster loop
  bound (`sub < 3`). The within-cluster core offsets (`+ 1` / `+ 2`) and
  the fixed 1:2 AIC:AIV ratio (`aic_n + 2 * ci`, sub_block_id 0/0/1/1)
  stay literal -- they encode the block structure, not the block width.

Both trees now read identically at every changed site.
@ChaoZheng109
ChaoZheng109 merged commit a8d7ce1 into hw-native-sys:main Aug 10, 2026
19 checks passed
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.

[Code Health] Derive tmr scheduler thread/core dimensions from platform_config constants instead of magic literals

1 participant