Skip to content

Refactor: decompose run() into shared head + tail helpers#924

Merged
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
hw-native-sys-bot:decompose-run
May 30, 2026
Merged

Refactor: decompose run() into shared head + tail helpers#924
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
hw-native-sys-bot:decompose-run

Conversation

@hw-native-sys-bot
Copy link
Copy Markdown
Collaborator

Summary

Pulls the byte-identical sub-sequences at the head and tail of both arches' run() into DeviceRunnerBase helpers. The heavily-divergent middle (register address setup, profiling-flag bits, init_*, collector start/teardown, dep_gen, ffts, kernel launches) stays on each subclass.

Net -17 lines across the four touched files, but each arch's run() is now noticeably shorter (a2a3 -65 lines, a5 -54 lines on the head/tail alone).

New base helpers

Helper What it covers
validate_launch_aicpu_num(int) range check vs PLATFORM_MAX_AICPU_THREADS
ensure_device_wall_buffer() lazy-alloc the 8-byte device wall buffer, idempotent
resolve_block_dim(int) auto-resolve via query_max_block_dim or pass through validate_block_dim; returns concrete block_dim
prepare_runtime_for_launch(Runtime&, int block_dim, int launch_aicpu_num) num_aicore range check + worker_count/aicpu_thread_num setup + handshake init loop + task function_bin_addr loop
sync_run_streams() sync AICPU then AICore with timeout + stream-id / device-id / block_dim context on the timeout sentinel
read_device_wall_ns() D2H readback into device_wall_ns_ with soft-warn fallback

Each helper is a verbatim move of code that was identical between the two arches.

What's left arch-specific in run()

  • a2a3: ffts base addr setup, dep_gen init/start/teardown, AIC Pmu register init (separate MMIO from Ctrl), 4-arg init_aicore_register_addresses(...AicoreRegKind...) API
  • a5: 3-arg init_aicore_register_addresses API, on-failure PMU disable-and-continue path
  • both: profiling-flag bit construction (a2a3 includes enable_dep_gen_), all collector start / teardown, init_l2_perf / init_tensor_dump / init_pmu / init_scope_stats calls (per-arch KernelArgs field assignments), init_runtime_args + log/device_id setup, init_device_kernel_args, kernel launches

These are real per-arch differences and would need virtuals or templating to unify — out of scope here.

Verification

  • Both arches built clean (onboard + sim, both runtimes).
  • Local a2a3 onboard smoke (dummy_task, alternating_matmul_add, prepared_callable suite, spmd_basic) — 9/9 passed in 19s.

Test plan

  • CI st-sim-a2a3 / st-sim-a5
  • CI st-onboard-a2a3 / st-onboard-a5
  • CI ut-a2a3 / ut-a5

Pull the byte-identical sub-sequences at the head and tail of both
arches' run() into DeviceRunnerBase helpers. The heavily-divergent
middle (register address setup, profiling flag bits, init_*,
collector start/teardown, dep_gen, ffts, kernel launches) stays on
the subclass. Net -17 lines.

New base helpers:
- validate_launch_aicpu_num(int) — range check vs PLATFORM_MAX_AICPU_THREADS
- ensure_device_wall_buffer() — lazy-alloc the 8-byte device wall buffer
- resolve_block_dim(int) — auto-resolve or validate, returns block_dim
- prepare_runtime_for_launch(Runtime&, int block_dim, int launch_aicpu_num) —
  num_aicore range check + worker_count/aicpu_thread_num setup +
  handshake init loop + task function_bin_addr loop
- sync_run_streams() — sync aicpu then aicore with timeout + good error
  context (stream id, device id, block_dim) on the timeout sentinel
- read_device_wall_ns() — D2H readback into device_wall_ns_ with soft-warn
  fallback on rtMemcpy failure

Each helper is a verbatim move of code that was identical between the
two arches. No behavior change.

Both arches built clean. a2a3 onboard smoke (dummy_task,
alternating_matmul_add, prepared_callable suite, spmd_basic) 9/9
passed in 19s.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 30, 2026

Warning

Review limit reached

@hw-native-sys-bot, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 54 minutes and 36 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d65f2d53-fde1-409a-ba1f-cfa60b8b1fd9

📥 Commits

Reviewing files that changed from the base of the PR and between d9cdf01 and 9c6c30a.

📒 Files selected for processing (4)
  • src/a2a3/platform/onboard/host/device_runner.cpp
  • src/a5/platform/onboard/host/device_runner.cpp
  • src/common/platform/onboard/host/device_runner_base.cpp
  • src/common/platform/onboard/host/device_runner_base.h

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 and usage tips.

Copy link
Copy Markdown

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

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 refactors the DeviceRunner::run method across the a2a3 and a5 architectures by extracting common, byte-identical setup and teardown sequences into helper methods in the DeviceRunnerBase class. These helper methods include validate_launch_aicpu_num, ensure_device_wall_buffer, resolve_block_dim, prepare_runtime_for_launch, sync_run_streams, and read_device_wall_ns. This change significantly reduces code duplication and improves maintainability. There are no review comments to address, and I have no additional feedback to provide.

@ChaoWao ChaoWao merged commit c830d3a into hw-native-sys:main May 30, 2026
16 checks passed
@ChaoWao ChaoWao deleted the decompose-run branch May 30, 2026 12:32
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