Skip to content

Fix: gate the device-phase timing buffer's per-run transfers on capture - #1657

Merged
ChaoZheng109 merged 1 commit into
hw-native-sys:mainfrom
ChaoZheng109:fix/issue-1642-gate-device-phase-transfers
Aug 5, 2026
Merged

ChaoZheng109 merged 1 commit into
hw-native-sys:mainfrom
ChaoZheng109:fix/issue-1642-gate-device-phase-transfers

Conversation

@ChaoZheng109

Copy link
Copy Markdown
Collaborator

Fixes #1642.

Problem

Every simpler_run performed three device↔host transfers around the AICPU device-phase / task-timing buffer, all unconditional:

# Op Dir a2a3 / a5
1 ensure_device_wall_buffer() H2D reset H→D 2496 B / 5824 B
2 read_device_wall_ns() phase-region D2H D→H 960 B / 2240 B
3 read_device_wall_ns() task-timing tail D2H D→H 1536 B / 3584 B

The only consumer of the readback is emit_device_phase_markers(), which is double-gated (compile-time SIMPLER_HOST_STRACE — STRACE_DEV_SPAN_AT compiles to nothing when off — and runtime SIMPLER_DEVICE_STRACE_ENABLE). The producers were not gated: the only guard was "is the buffer allocated", which the lazy allocator makes true for the process lifetime after the first run. No other code reads last_device_wall_ns / last_device_phase_ns / last_task_slot_*. So with device strace disabled, all three transfers ran every run and the data was discarded — per-run latency on the dispatch/decode hot path.

Fix

Gate the producer on the same condition as the consumer. device_phase_capture_enabled() (compile gate ∧ runtime env) is now the single predicate both sides share:

When capture is on, the code path is byte-identical to before (allocate + reset + read + emit).

Scope

Verification

  • Compile: full from-scratch rebuild of both a2a3 and a5 onboard runtimes (host_build_graph + tensormap_and_ringbuffer) — clean. Pre-commit clang-format / clang-tidy / cpplint pass.
  • Onboard hardware: this box currently reports no detectable silicon (npu-smi returns no chip), so the device-behavior check (markers still emitted with capture on; absent with capture off; no regression) is left to CI's st-onboard-a2a3 / st-onboard-a5 jobs. The change is correct by construction: with capture on the path is unchanged, and the AICPU null-base no-op is an existing, documented guarantee.

@coderabbitai

coderabbitai Bot commented Aug 3, 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: 5b690f66-4d3f-4677-a087-86885f3ddd49

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

Changes

Device phase capture

Layer / File(s) Summary
Define and cache the capture gate
src/common/platform/onboard/host/device_runner_base.h, src/common/platform/onboard/host/device_runner_base.cpp
The host runtime declares device_phase_capture_enabled() and caches the SIMPLER_DEVICE_STRACE_ENABLE runtime gate.
Skip disabled capture operations
src/common/platform/onboard/host/device_runner_base.cpp, src/common/platform/onboard/host/c_api_shared.cpp
Disabled capture skips buffer reset and readbacks. Marker emission uses the shared capture gate.

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

Sequence Diagram(s)

sequenceDiagram
  participant DeviceRunnerBase
  participant DeviceWallBuffer
  participant CApiShared
  DeviceRunnerBase->>DeviceWallBuffer: reset and read timing data when capture is enabled
  DeviceRunnerBase->>DeviceWallBuffer: skip transfers when capture is disabled
  CApiShared->>DeviceRunnerBase: query device_phase_capture_enabled()
  DeviceRunnerBase-->>CApiShared: capture state
  CApiShared->>CApiShared: emit markers when enabled
Loading

Possibly related issues

Possibly related PRs

Poem

A rabbit guards the timing gate,
No needless buffers wait or wake.
When strace calls, the markers shine;
When disabled, transfers decline.
Hop, hop—the host now skips the load!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies gating per-run device-phase timing transfers on capture.
Description check ✅ Passed The description directly explains the problem, implementation, scope, and verification for the device-phase transfer gating changes.
Linked Issues check ✅ Passed The changes satisfy issue #1642 by gating allocation, H2D reset, and both D2H readbacks with the shared capture predicate.
Out of Scope Changes check ✅ Passed All changes are limited to onboard device-phase capture gating and its shared predicate; no unrelated code changes are identified.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

@ChaoZheng109
ChaoZheng109 force-pushed the fix/issue-1642-gate-device-phase-transfers branch from 6811a1d to bc5d045 Compare August 4, 2026 09:05
Fixes hw-native-sys#1642

Every simpler_run reset the onboard device-phase buffer over H2D and
read the phase region and task-timing tail back over D2H. The readback
exists only for device-domain [STRACE] markers, so runs that could not
emit those markers still paid three marker-only transfers.

Use one capture predicate for buffer preparation, readback, and marker
emission. It requires SIMPLER_HOST_STRACE, an enabled
SIMPLER_DEVICE_STRACE_ENABLE setting, and a log threshold that admits
LOG_TIMING. Disabled capture publishes a null device buffer base, so
AICPU stamping is also skipped; re-enabling capture restores the
existing allocation.

Add no-hardware coverage for compile-time, environment, and log-level
gates, and align the DFX documentation with the onboard behavior. The
sim path retains its emission-only environment gate.

The task-timing tail still transfers when capture is enabled but no task
is tagged; that is observation 2 and remains follow-up work.

Co-authored-by: ChaoZheng109 <zhengchao47@huawei.com>
@zmnobug
zmnobug force-pushed the fix/issue-1642-gate-device-phase-transfers branch from bc5d045 to a2ef4cf Compare August 5, 2026 07:55
@ChaoZheng109
ChaoZheng109 merged commit 7fd34fe into hw-native-sys:main Aug 5, 2026
18 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Aug 6, 2026
5 of 6 tasks
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] Device-phase/strace timing buffer is copied device↔host on every simpler_run, unconditionally and unaffected by its own enable gates

1 participant