Skip to content

Add: select A5 AICPU cores on verified 9599 and 9579 devices - #1643

Open
yanghaoran29 wants to merge 1 commit into
hw-native-sys:mainfrom
yanghaoran29:add/a5-fg-pg-core-selection
Open

Add: select A5 AICPU cores on verified 9599 and 9579 devices#1643
yanghaoran29 wants to merge 1 commit into
hw-native-sys:mainfrom
yanghaoran29:add/a5-fg-pg-core-selection

Conversation

@yanghaoran29

@yanghaoran29 yanghaoran29 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Scope

This change is scoped to the A5 devices whose topology inputs were verified:

  • Ascend950PR_9599: use device-side occupancy plus the documented
    9-logical-CPU topology to select the FG 1O+4S affinity.
  • x86 Ascend950PR_9579 with OCCUPY=0x3e: when CPU_TOPO is unavailable,
    treat the five occupied bits as the complete schedulable pool and select
    all five threads without assuming SMT relationships.

Other complete A5 topology shapes use the structural FG, FG+SMT, PG1, and
PG2 policies covered by C++ unit tests. Unknown shapes retain the
topology-ordered fallback; this PR does not claim hardware validation for
unlisted device shapes.

Changes

  • Query the authoritative device-side AICPU scheduler pool before launch.
  • Prefer live CPU_TOPO and use the embedded Ascend950PR_9599 table only
    when the driver topology query is unavailable.
  • Keep the fallback data fixed in the repository; no environment variable
    changes runtime topology behavior.
  • Let aicpu-device-query choose the a2a3 or A5 dispatcher with
    --platform, defaulting to A5. JSON topology classification remains A5-only.
  • Cover topology classification, placement, JSON formatting, and the
    five-thread OCCUPY fallback in C++ unit tests.

Verification

  • ctest --test-dir tests/ut/cpp/build-pr1643 -LE requires_hardware --output-on-failure (75/75 passed)
  • pre-commit run --files <changed files>

@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: 4354ca49-2e42-4b81-8537-38c9cd5c1c5e

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 PR adds a production AICPU topology query, occupancy-aware host probing, scenario-specific CPU selection, unknown-topology fallback, runtime thread-count adjustment, and diagnostic JSON output for A5.

Changes

A5 AICPU topology flow

Layer / File(s) Summary
Topology query contract and entry point
src/a5/platform/include/common/kernel_args.h, src/a5/platform/onboard/aicpu/*, src/common/aicpu_loader/*, src/a5/runtime/*
Adds the topology query payload, HAL-backed simpler_aicpu_query_topology entry point, build linkage, and runtime symbol exports.
Topology classification and CPU selection
src/a5/platform/onboard/host/aicpu_topology_probe.*, tests/ut/cpp/a5/test_aicpu_topology_fallback.cpp
Adds occupancy-aware probing, FG/PG classification, known-scenario affinity policies, unknown-topology fallback selection, JSON serialization, and unit tests.
Runtime occupancy and launch integration
src/a5/platform/onboard/host/device_runner.*, src/a5/docs/hardware.md
Queries and caches device occupancy, applies topology selection, adjusts effective runtime thread counts, preserves the full launch population, and resets cached state.
Diagnostic JSON flow and documentation
tools/cann-examples/aicpu-device-query/*, docs/design/a5-fg-pg-core-selection.en.md
Adds --json topology output, required build and link settings, and documentation for production querying and fallback behavior.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant DeviceRunner
  participant AICPUQuery
  participant AscendHAL
  participant TopologyProbe
  participant AICPUThreads
  DeviceRunner->>AICPUQuery: query device occupancy
  AICPUQuery->>AscendHAL: request occupancy metrics
  AscendHAL-->>AICPUQuery: return topology values
  AICPUQuery-->>DeviceRunner: return occupancy result
  DeviceRunner->>TopologyProbe: classify topology and select CPUs
  TopologyProbe-->>DeviceRunner: return affinity and effective count
  DeviceRunner->>AICPUThreads: launch full OCCUPY population
Loading

Possibly related PRs

Poem

A rabbit queries cores in a row,
Through HAL paths where occupancy values flow.
Five CPUs hop into place,
With O at the end of the race.
JSON rests clean, warnings stay low.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 24.14% 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
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.
Title check ✅ Passed The title clearly summarizes the main change: selecting A5 AICPU cores on the specified devices.
Description check ✅ Passed The description directly explains A5 AICPU core selection, topology classification, fallback behavior, diagnostics, and tests.

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: 2

🧹 Nitpick comments (4)
src/a5/platform/onboard/host/aicpu_topology_probe.cpp (2)

483-485: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Return a value tuple from topology_key.

std::tie returns std::tuple<const int32_t&, ...> bound to the members of cpu. All current callers compare the result inside the same full expression, so the references stay valid. The signature is still fragile. A future caller that stores the result, or that passes a temporary AicpuLogicalCpu, gets dangling references with no compiler diagnostic. The members are five int32_t, so a value tuple costs nothing.

♻️ Proposed change to return a value tuple
 auto topology_key(const AicpuLogicalCpu &cpu) {
-    return std::tie(cpu.die_id, cpu.cluster_id, cpu.phy_cpu_id, cpu.hyperthread_id, cpu.cpu_id);
+    return std::make_tuple(cpu.die_id, cpu.cluster_id, cpu.phy_cpu_id, cpu.hyperthread_id, cpu.cpu_id);
 }
🤖 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/a5/platform/onboard/host/aicpu_topology_probe.cpp` around lines 483 -
485, Update topology_key to return a value tuple containing the five int32_t
topology fields instead of using std::tie, ensuring results remain valid when
stored or when the input is temporary.

676-694: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Escape soc_name before you write it into the JSON string.

Line 683 interpolates topology.soc_name directly between quotes. The value comes from aclrtGetSocName() through query_soc_name(), so this code does not control its content. A " or \ in that value produces malformed JSON for every consumer of --json output. Observed SoC names are alphanumeric, so this is a robustness gap and not a current failure.

A related point on line 679-681: the ternary chain uses "sequential_fallback" as the catch-all. If a fourth AicpuSelectionPolicy enumerator is added, the output silently reports the wrong policy. A switch gives you a compiler warning instead.

♻️ Proposed fix for escaping and the policy mapping
+std::string json_escape(const std::string &value) {
+    std::string out;
+    out.reserve(value.size());
+    for (char c : value) {
+        if (c == '"' || c == '\\') {
+            out += '\\';
+            out += c;
+        } else if (static_cast<unsigned char>(c) < 0x20) {
+            char buf[7];
+            std::snprintf(buf, sizeof(buf), "\\u%04x", static_cast<unsigned char>(c));
+            out += buf;
+        } else {
+            out += c;
+        }
+    }
+    return out;
+}
+
 std::string format_aicpu_topology_json(
     const AicpuTopology &topology, AicpuSelectionPolicy policy, const std::vector<int32_t> &allowed_cpus
 ) {
-    const char *policy_name = policy == AicpuSelectionPolicy::kScenario ? "scenario" :
-                              policy == AicpuSelectionPolicy::kGeneric  ? "generic" :
-                                                                          "sequential_fallback";
+    const char *policy_name = "sequential_fallback";
+    switch (policy) {
+    case AicpuSelectionPolicy::kScenario:
+        policy_name = "scenario";
+        break;
+    case AicpuSelectionPolicy::kGeneric:
+        policy_name = "generic";
+        break;
+    case AicpuSelectionPolicy::kSequentialFallback:
+        policy_name = "sequential_fallback";
+        break;
+    }
     std::ostringstream out;
-    out << "{\n  \"architecture\": \"a5\",\n  \"soc_name\": \"" << topology.soc_name << "\",\n  \"scenario_type\": \""
+    out << "{\n  \"architecture\": \"a5\",\n  \"soc_name\": \"" << json_escape(topology.soc_name)
+        << "\",\n  \"scenario_type\": \""
🤖 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/a5/platform/onboard/host/aicpu_topology_probe.cpp` around lines 676 -
694, Update format_aicpu_topology_json to JSON-escape topology.soc_name before
inserting it into the quoted "soc_name" field, including quotes, backslashes,
and other required control characters. Replace the policy_name ternary in
format_aicpu_topology_json with an exhaustive switch over AicpuSelectionPolicy
so newly added enumerators are diagnosed rather than silently mapped to
sequential_fallback.
tools/cann-examples/aicpu-device-query/host/CMakeLists.txt (1)

44-48: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Make the driver library directory overridable.

Line 47 hardcodes /usr/local/Ascend/driver/lib64/driver while every other search path in this file derives from ${ASCEND_HOME_PATH}. On a host with a non-default driver install, the ascend_hal link on line 52 fails and the tool cannot be built. A cache variable keeps the default and lets the builder override it.

♻️ Proposed change
+set(ASCEND_DRIVER_LIB_DIR "/usr/local/Ascend/driver/lib64/driver"
+    CACHE PATH "Directory containing libascend_hal.so")
+
 target_link_directories(query_device_hal PRIVATE
     ${ASCEND_HOME_PATH}/lib64
     ${ASCEND_HOME_PATH}/runtime/lib64
-    /usr/local/Ascend/driver/lib64/driver
+    ${ASCEND_DRIVER_LIB_DIR}
 )
🤖 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 `@tools/cann-examples/aicpu-device-query/host/CMakeLists.txt` around lines 44 -
48, Update the target_link_directories configuration for query_device_hal to
replace the hardcoded driver path with a CMake cache variable that defaults to
/usr/local/Ascend/driver/lib64/driver, allowing builders to override the driver
library directory while preserving the current default.
src/a5/platform/onboard/host/device_runner.cpp (1)

282-289: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Name the 5 that selects the 4S+1O policy.

The literal couples this dispatch to compute_scenario_allowed_cpus, which always returns exactly five CPUs in [S0, S1, S2, S3, O] order. The relationship is not visible at the call site. A named constant documents why any other requested count falls through to the generic compute_allowed_cpus path.

♻️ Proposed change
+// compute_scenario_allowed_cpus implements the fixed 4-scheduler + 1-orchestrator
+// policy, so it applies only when the caller requests exactly that many threads.
+constexpr int kScenarioPolicyThreadCount = 5;
-        } else if (requested_aicpu_num == 5) {
+        } else if (requested_aicpu_num == kScenarioPolicyThreadCount) {
🤖 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/a5/platform/onboard/host/device_runner.cpp` around lines 282 - 289,
Replace the literal requested_aicpu_num == 5 check in the device runner dispatch
with a named constant representing the 4S+1O policy CPU count, defined in the
appropriate nearby scope. Use that constant when selecting
compute_scenario_allowed_cpus so the fixed five-CPU relationship is explicit
while leaving the generic fallback unchanged.
🤖 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/a5/platform/onboard/host/device_runner.cpp`:
- Around line 162-172: Update the preflight AICPU launch and synchronization
failure branches in the device occupancy query to call
recover_device_or_mark_unusable(rc) before returning. Apply this to both
launch_aicpu_payload and aclrtSynchronizeStreamWithTimeout failures, preserving
the existing error logging and return behavior.

In `@tools/cann-examples/aicpu-device-query/host/query_device_hal.cpp`:
- Around line 498-505: Replace the magic indices used in the JSON occupancy
mapping within the query-device flow with named constants representing the
OS_SCHED, OCCUPY, and PF_OCCUPY request positions. Use those constants
consistently for both value and validity assignments, and define them alongside
the requests list so changes to request ordering remain explicit and
synchronized.

---

Nitpick comments:
In `@src/a5/platform/onboard/host/aicpu_topology_probe.cpp`:
- Around line 483-485: Update topology_key to return a value tuple containing
the five int32_t topology fields instead of using std::tie, ensuring results
remain valid when stored or when the input is temporary.
- Around line 676-694: Update format_aicpu_topology_json to JSON-escape
topology.soc_name before inserting it into the quoted "soc_name" field,
including quotes, backslashes, and other required control characters. Replace
the policy_name ternary in format_aicpu_topology_json with an exhaustive switch
over AicpuSelectionPolicy so newly added enumerators are diagnosed rather than
silently mapped to sequential_fallback.

In `@src/a5/platform/onboard/host/device_runner.cpp`:
- Around line 282-289: Replace the literal requested_aicpu_num == 5 check in the
device runner dispatch with a named constant representing the 4S+1O policy CPU
count, defined in the appropriate nearby scope. Use that constant when selecting
compute_scenario_allowed_cpus so the fixed five-CPU relationship is explicit
while leaving the generic fallback unchanged.

In `@tools/cann-examples/aicpu-device-query/host/CMakeLists.txt`:
- Around line 44-48: Update the target_link_directories configuration for
query_device_hal to replace the hardcoded driver path with a CMake cache
variable that defaults to /usr/local/Ascend/driver/lib64/driver, allowing
builders to override the driver library directory while preserving the current
default.
🪄 Autofix (Beta)

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: 82f4f0d8-f319-482a-9e81-bffd2ebdee27

📥 Commits

Reviewing files that changed from the base of the PR and between 04e0733 and 388d7ad.

📒 Files selected for processing (16)
  • docs/design/a5-fg-pg-core-selection.en.md
  • src/a5/docs/hardware.md
  • src/a5/platform/include/common/kernel_args.h
  • src/a5/platform/onboard/aicpu/CMakeLists.txt
  • src/a5/platform/onboard/aicpu/kernel.cpp
  • src/a5/platform/onboard/host/aicpu_topology_probe.cpp
  • src/a5/platform/onboard/host/aicpu_topology_probe.h
  • src/a5/platform/onboard/host/device_runner.cpp
  • src/a5/platform/onboard/host/device_runner.h
  • src/a5/runtime/host_build_graph/host/runtime_maker.cpp
  • src/a5/runtime/tensormap_and_ringbuffer/host/runtime_maker.cpp
  • src/common/aicpu_loader/host/load_aicpu_op.h
  • tests/ut/cpp/a5/test_aicpu_topology_fallback.cpp
  • tools/cann-examples/aicpu-device-query/README.md
  • tools/cann-examples/aicpu-device-query/host/CMakeLists.txt
  • tools/cann-examples/aicpu-device-query/host/query_device_hal.cpp

Comment thread src/a5/platform/onboard/host/device_runner.cpp
Comment thread tools/cann-examples/aicpu-device-query/host/query_device_hal.cpp Outdated
@yanghaoran29
yanghaoran29 force-pushed the add/a5-fg-pg-core-selection branch 9 times, most recently from 2b60f55 to b704cbd Compare August 3, 2026 11:35
@yanghaoran29 yanghaoran29 changed the title Add: implement A5 FG/PG AICPU core selection Add: select A5 AICPU cores on verified 9599 and 9579 devices Aug 3, 2026
Select the default 1O+4S affinity from device-side occupancy and host
CPU_TOPO for A5 layouts covered by the probe.

- Apply FG, FG+SMT, PG1, and PG2 placement when complete topology
  metadata is available; keep unknown layouts on a CPU-ID-ordered
  OCCUPY-only fallback.
- Use the embedded Ascend950PR_9599 topology when live CPU_TOPO is
  unavailable, without a behavior-changing environment override.
- Recover the device after topology-query launch or synchronization
  failures so the persistent stream cannot remain poisoned.
- Emit robust diagnostic JSON and allow the driver library directory
  to be overridden when building aicpu-device-query.
@yanghaoran29
yanghaoran29 force-pushed the add/a5-fg-pg-core-selection branch from b704cbd to dcbf182 Compare August 3, 2026 13:13
@yanghaoran29

Copy link
Copy Markdown
Contributor Author

@coderabbitai Addressed the remaining review suggestions:

  • topology_key now returns a value tuple.
  • topology JSON escapes the SoC name and maps selection policies with an exhaustive switch.
  • the fixed 4S+1O thread count has a named constant.
  • the driver library directory is an overridable CMake cache path.
  • JSON fallback helpers are grouped at the end of aicpu_topology_probe.cpp.

Validation passed: the 16 A5 topology unit tests, pre-commit, and a separate clang-tidy run.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Your plan includes PR reviews subject to rate limits. Reviews are available now.

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