Skip to content

Add: HostApi register/unregister_device_memory_to_host (SVM host-map)#1324

Merged
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
ChaoWao:hostapi-map-device-to-host
Jul 11, 2026
Merged

Add: HostApi register/unregister_device_memory_to_host (SVM host-map)#1324
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
ChaoWao:hostapi-map-device-to-host

Conversation

@ChaoWao

@ChaoWao ChaoWao commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds a platform capability for mapping a device buffer into the host address space, returning a host-readable VA, exposed through the shared HostApi table. This lets a host-side orchestrator dereference control tensors whose buffer.addr is a device address.

  • DeviceRunnerBase (onboard) gains virtual register_device_memory_to_host / unregister_device_memory_from_host (base default: nullptr / no-op).
  • a2a3 onboard overrides via halHostRegister(DEV_SVM_MAP_HOST) / halHostUnregister.
  • a5 onboard uses the base default (no host-map path on a5).
  • sim implements them as identity / no-op (the sim "device" pointer is already host-readable).
  • HostApi (common/host_api.h) gains the two function pointers; both c_api_shared.cpp tables (onboard + sim) wire wrappers forwarding to current_runner().

Contract: the returned host VA may differ from dev_ptr — callers must use it (not dev_ptr) for host access, and pair every register with an unregister before free_tensor.

Context

This is the platform half of the host_build_graph (host-orchestration) runtime: the host orchestrator needs to read control tensors (e.g. paged_attention's context_lens) that live in device memory. Landing the capability separately, with the two-function contract named for intent (register_device_memory_to_host) rather than the a2a3 HAL mechanism, keeps the interface clean and correct across all backends (a2a3 SVM, a5 shadow, sim identity).

No consumer in this change — the hbg runtime PR (#1185) consumes it and drops its previous ad-hoc bridge.

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: f2063f8e-b29e-4ed5-90a5-57c6e80bfbe0

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

Device-memory host mapping callbacks are added to the shared HostApi and runner interfaces. Onboard runners use dynamically loaded HAL registration functions, while simulation runners return the original pointer and perform no-op unregistration.

Changes

Device Memory Host Mapping

Layer / File(s) Summary
Mapping contracts and defaults
src/common/platform/include/common/host_api.h, src/common/platform/onboard/host/device_runner_base.h, src/a2a3/platform/onboard/host/device_runner.h
HostApi and runner interfaces add registration and unregistration callbacks, with default unsupported behavior in the base runner.
Onboard HAL mapping flow
src/a2a3/platform/onboard/host/device_runner.cpp, src/common/platform/onboard/host/c_api_shared.cpp
DeviceRunner resolves and calls halHostRegister and halHostUnregister, while onboard C API wrappers expose the operations through g_host_api.
Simulation mapping flow
src/common/platform/sim/host/device_runner_base.h, src/common/platform/sim/host/c_api_shared.cpp
Simulation registration returns the original pointer, unregistration is a no-op, and both callbacks are wired into the simulation HostApi table.

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

Sequence Diagram(s)

sequenceDiagram
  participant Runtime
  participant HostApi
  participant DeviceRunner
  participant ascend_hal
  Runtime->>HostApi: register_device_memory_to_host(dev_ptr, bytes)
  HostApi->>DeviceRunner: forward registration request
  DeviceRunner->>ascend_hal: halHostRegister with DEV_SVM_MAP_HOST
  ascend_hal-->>DeviceRunner: host virtual address or failure
  DeviceRunner-->>Runtime: host address or nullptr
  Runtime->>HostApi: unregister_device_memory_from_host(dev_ptr)
  HostApi->>DeviceRunner: forward unregistration request
  DeviceRunner->>ascend_hal: halHostUnregister
Loading

Possibly related PRs

Poem

A rabbit hops where device pointers gleam,
HAL maps memory like a moonlit dream.
Register, unregister, tidy and neat,
Simulation returns the same address sweet.
“Hop!” says the bunny—“the host path is complete!”

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: adding HostApi device-memory host mapping support.
Description check ✅ Passed The description clearly matches the changeset and explains the new HostApi host-mapping capability and backend behavior.
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 register_device_memory_to_host and unregister_device_memory_from_host APIs to map and unmap device buffers into the host address space. This is implemented across the onboard and simulator runners, with the onboard implementation wrapping the lazy-loaded HAL's halHostRegister and halHostUnregister functions. The review feedback suggests improving robustness by validating that device_id_ is non-negative before invoking HAL functions, and making the unregistration function symmetric to the registration function by ensuring the HAL is loaded and logging errors if the unregister symbol is missing.

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/platform/onboard/host/device_runner.cpp
Comment thread src/a2a3/platform/onboard/host/device_runner.cpp

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

🧹 Nitpick comments (1)
src/a2a3/platform/onboard/host/device_runner.cpp (1)

724-740: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Log when halHostUnregister symbol is missing but HAL is loaded.

unregister_device_memory_from_host silently does nothing when get_halHostUnregister() returns nullptr. If the HAL is loaded (g_hal_handle != nullptr) but the symbol is missing, this is a real resource leak — the paired register succeeded but unregister can't run. register_device_memory_to_host logs the analogous symbol-not-found case; unregister should do the same for symmetry and debuggability.

♻️ Proposed fix: add a warning log for symbol-missing case
     HalHostUnregisterFn fn = get_halHostUnregister();
     if (fn != nullptr) {
         int rc = fn(dev_ptr, device_id_);
         if (rc != 0) {
             LOG_ERROR(
                 "unregister_device_memory_from_host: halHostUnregister failed for dev_ptr %p (rc=%d)", dev_ptr, rc
             );
         }
+    } else if (g_hal_handle != nullptr) {
+        LOG_ERROR("unregister_device_memory_from_host: halHostUnregister symbol not found");
     }
🤖 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/a2a3/platform/onboard/host/device_runner.cpp` around lines 724 - 740, Add
an else branch in DeviceRunner::unregister_device_memory_from_host after
checking get_halHostUnregister(), logging a warning when the function pointer is
null while g_hal_handle is non-null. Match the symbol-not-found logging behavior
and context used by register_device_memory_to_host, while preserving the
existing null-pointer and HAL-call error handling.
🤖 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.

Nitpick comments:
In `@src/a2a3/platform/onboard/host/device_runner.cpp`:
- Around line 724-740: Add an else branch in
DeviceRunner::unregister_device_memory_from_host after checking
get_halHostUnregister(), logging a warning when the function pointer is null
while g_hal_handle is non-null. Match the symbol-not-found logging behavior and
context used by register_device_memory_to_host, while preserving the existing
null-pointer and HAL-call error handling.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 663ea4fe-25c1-4a22-9ebc-3333c3560347

📥 Commits

Reviewing files that changed from the base of the PR and between 53114ef and f08c307.

📒 Files selected for processing (7)
  • src/a2a3/platform/onboard/host/device_runner.cpp
  • src/a2a3/platform/onboard/host/device_runner.h
  • src/common/platform/include/common/host_api.h
  • src/common/platform/onboard/host/c_api_shared.cpp
  • src/common/platform/onboard/host/device_runner_base.h
  • src/common/platform/sim/host/c_api_shared.cpp
  • src/common/platform/sim/host/device_runner_base.h

@ChaoWao ChaoWao force-pushed the hostapi-map-device-to-host branch from f08c307 to e0f9841 Compare July 11, 2026 03:56
Adds a platform capability for mapping a device buffer into the host address
space and returning a host-readable VA, exposed through the shared HostApi
table so a host-side orchestrator can dereference control tensors whose
buffer.addr is a device address.

- DeviceRunnerBase (onboard) gains virtual register_device_memory_to_host /
  unregister_device_memory_from_host (base default: nullptr / no-op).
- a2a3 onboard overrides them via halHostRegister(DEV_SVM_MAP_HOST) /
  halHostUnregister; a5 onboard uses the base default (no host-map path).
- sim SimDeviceRunnerBase implements them as identity / no-op (its "device"
  pointer is already host-readable).
- HostApi (common/host_api.h) gains the two function pointers; both
  c_api_shared.cpp tables (onboard + sim) wire wrappers that forward to
  current_runner().

The returned host VA may differ from dev_ptr, so callers must use it for host
access, and pair every register with an unregister before free. No consumer in
this change; host_build_graph will use it. Builds clean on all 4 platforms x
2 runtimes with -Werror.
@ChaoWao ChaoWao merged commit 283f2f1 into hw-native-sys:main Jul 11, 2026
16 checks passed
ChaoWao added a commit to ChaoWao/simpler-fork that referenced this pull request Jul 11, 2026
Introduces the host_build_graph (hbg) runtime: the host-orchestration
variant of tensormap_and_ringbuffer. The orchestrator runs on the host to
completion — building the whole task graph, relocating it to device
addresses, and H2D'ing the image — then the device boots scheduler-only.
Only the orchestration timing/location differs from tensormap; the data
structures and dispatch are shared.

Pipeline (host): stage tensors (device alloc + H2D) and host-map them so the
host can read control tensors directly; dlopen the orchestration .so and run
it; wire fanout inline during submit (build dep_pool/fanout_head + seed the
ready queue on the host); relocate every cross-task pointer to its final
device address (range-based, two-delta: SM region + arena region); H2D the
populated SM + arena. Device: attach the already-device-addressed image and
dispatch from the seeded ready queue; no on-device orchestrator, no pointer
fixup, no execution-time reclaim. Completion is tracked by completed_tasks_,
independent of last_task_alive.

Simplifications enabled by host-orchestration:
- No device-orch path (the device boots scheduler-only).
- No execution-time reclaim: the whole graph is host-resident and runs once,
  so advance_ring_pointers / reset_for_reuse / the COMPLETED->CONSUMED flip
  are removed. Consumer waits key on fanout_refcount.
- Single ring: with no reclaim and a whole-graph-resident ring, the
  per-scope-depth multi-ring split is gone (PTO2_MAX_RING_DEPTH == 1);
  rings[]/ring_id are physically removed. All scope depths map to ring 0.
- Host reads device control tensors (e.g. paged_attention's context_lens /
  block_table) via the host-map (a2a3 halHostRegister identity map; sim is
  already a host pointer), so get_tensor_data dereferences buffer.addr
  directly.

The host-map is consumed through HostApi::register_device_memory_to_host /
unregister_device_memory_from_host (added in hw-native-sys#1324): a2a3 onboard wraps
halHostRegister(DEV_SVM_MAP_HOST), sim is identity, and a5 onboard uses the
base default (no host-map path). tensormap_and_ringbuffer and a5 runtimes are
not modified.

Verified on a2a3 onboard: paged_attention passes (host_build_graph suite,
reading control tensors via the HostApi host-map); a2a3sim hbg scene suite 10
passed; cpput 54/54; all 8 runtime targets build with -Werror.
@ChaoWao ChaoWao deleted the hostapi-map-device-to-host branch July 11, 2026 06:39
ChaoWao added a commit that referenced this pull request Jul 11, 2026
…p) (#1185)

Introduces the host_build_graph (hbg) runtime: the host-orchestration
variant of tensormap_and_ringbuffer. The orchestrator runs on the host to
completion — building the whole task graph, relocating it to device
addresses, and H2D'ing the image — then the device boots scheduler-only.
Only the orchestration timing/location differs from tensormap; the data
structures and dispatch are shared.

Pipeline (host): stage tensors (device alloc + H2D) and host-map them so the
host can read control tensors directly; dlopen the orchestration .so and run
it; wire fanout inline during submit (build dep_pool/fanout_head + seed the
ready queue on the host); relocate every cross-task pointer to its final
device address (range-based, two-delta: SM region + arena region); H2D the
populated SM + arena. Device: attach the already-device-addressed image and
dispatch from the seeded ready queue; no on-device orchestrator, no pointer
fixup, no execution-time reclaim. Completion is tracked by completed_tasks_,
independent of last_task_alive.

Simplifications enabled by host-orchestration:
- No device-orch path (the device boots scheduler-only).
- No execution-time reclaim: the whole graph is host-resident and runs once,
  so advance_ring_pointers / reset_for_reuse / the COMPLETED->CONSUMED flip
  are removed. Consumer waits key on fanout_refcount.
- Single ring: with no reclaim and a whole-graph-resident ring, the
  per-scope-depth multi-ring split is gone (PTO2_MAX_RING_DEPTH == 1);
  rings[]/ring_id are physically removed. All scope depths map to ring 0.
- Host reads device control tensors (e.g. paged_attention's context_lens /
  block_table) via the host-map (a2a3 halHostRegister identity map; sim is
  already a host pointer), so get_tensor_data dereferences buffer.addr
  directly.

The host-map is consumed through HostApi::register_device_memory_to_host /
unregister_device_memory_from_host (added in #1324): a2a3 onboard wraps
halHostRegister(DEV_SVM_MAP_HOST), sim is identity, and a5 onboard uses the
base default (no host-map path). tensormap_and_ringbuffer and a5 runtimes are
not modified.

Verified on a2a3 onboard: paged_attention passes (host_build_graph suite,
reading control tensors via the HostApi host-map); a2a3sim hbg scene suite 10
passed; cpput 54/54; all 8 runtime targets build with -Werror.
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