Skip to content

[Code Health] Warn when task fanin/fanout exceeds 16 during dependency construction#1298

Open
yanghaoran29 wants to merge 1 commit into
hw-native-sys:mainfrom
yanghaoran29:codehealth/warn-dense-fanin-fanout
Open

[Code Health] Warn when task fanin/fanout exceeds 16 during dependency construction#1298
yanghaoran29 wants to merge 1 commit into
hw-native-sys:mainfrom
yanghaoran29:codehealth/warn-dense-fanin-fanout

Conversation

@yanghaoran29

Copy link
Copy Markdown
Contributor

Summary

Closes #1261. Very large fanin/fanout is a code-health smell (cf. #959) — it makes the dependency graph hard to reason about and can hide an unexpectedly dense wiring shape. Nothing flagged it before.

This adds a two-part, bounded diagnostic, split by the phase where each degree is naturally constructed:

1. Immediate WARN during execution (high-water gated)

Per codestyle rule 7 (never flood the AICPU device log on the hot path), each metric warns only on a new high-water max above the threshold:

  • Fanoutpto_orchestrator.cpp::append_fanin_or_fail, where the orchestrator increments each producer's fanout_count per claimed consumer. The value is captured under fanout_lock and logged after unlock (no logging in the critical section).
  • Faninpto_scheduler.h::wire_task, where scheduler thread 0 wires a task's fanin edges (wfanin), gated on a per-ring high-water.

2. End-of-run summary

Every task/producer over the threshold is counted silently during the run, then one line per metric is emitted at teardown:

  • === [Orchestrator] tasks with fanout>16: N === in mark_done()
  • === [Scheduler] tasks with fanin>16: N === in SchedulerContext::shutdown() (thread 0)

Counters reset per run. Threshold PTO2_DEP_DEGREE_WARN_THRESHOLD = 16. Purely diagnostic — no behavior change, no new env/macro gate. Mirrored to a2a3 and a5.

Test

Adds a dummy_task st case (DenseFanoutFanin, case=4): one producer fanned out to 18 dummy barriers, then one consumer depending on all 18, so both degrees exceed the threshold.

Verified on a2a3sim + a5sim (both pass):

[WARN] append_fanin_or_fail: dense dependency: task ring=0 id=0 fanout=17 (>16) [orch submit]
[WARN] append_fanin_or_fail: dense dependency: task ring=0 id=0 fanout=18 (>16) [orch submit]
[WARN] append_fanin_or_fail: dense dependency: task ring=0 id=0 fanout=19 (>16) [orch submit]
[INFO_V0] mark_done: === [Orchestrator] tasks with fanout>16: 1 ===
[WARN] wire_task: dense dependency: task ring=0 id=19 fanin=19 (>16) [scheduler wiring]
[INFO_V0] shutdown: === [Scheduler] tasks with fanin>16: 1 ===

18 consumers → only 3 WARN lines (one per new max) confirms the flood guard. Summaries need --log-level v0; the WARNs show at the default level.

Notes

  • Fires at 17/18/19 (not 16) because "greater than 16" is strict and the case-4 consumer also reads X, making the producer's true fanout 19. PTO2_FANIN_INLINE_CAP is 64, so 16 is an advisory policy threshold, not a structural boundary.
  • Onboard hardware verification wasn't possible in this environment (NPU dcmi init fails, arch precheck refuses); the sim runs exercise the identical orchestrator/scheduler code paths.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Jul 8, 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: e784973f-d597-4292-a0d1-848d49e9cdd9

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

Adds a PTO2_DEP_DEGREE_WARN_THRESHOLD (16) diagnostic across the a2a3 and a5 runtime variants: orchestrator fanout counters, scheduler fanin counters, ring-reset initialization, and shutdown-time summary logging when dependency degree exceeds the threshold. New DenseFanoutFanin test cases exercise producer-fanout/consumer-fanin scenarios in both variants.

Changes

a2a3 dense fanin/fanout diagnostics

Layer / File(s) Summary
Threshold constant
src/a2a3/.../pto_runtime2_types.h
Adds PTO2_DEP_DEGREE_WARN_THRESHOLD = 16.
Orchestrator fanout tracking
src/a2a3/.../pto_orchestrator.cpp
Adds high-water/over-threshold counters, updates them in append_fanin_or_fail on newly claimed producers, logs and resets in mark_done.
Scheduler fanin tracking and shutdown summary
src/a2a3/.../pto_scheduler.h, src/a2a3/.../scheduler_cold_path.cpp, src/a2a3/.../pto_runtime2_init.cpp
Adds fanin_high_water/fanin_over_count fields, updates them in wire_task, resets them on ring reuse, and sums/logs them at scheduler shutdown.
Dense fanout/fanin test case
tests/st/a2a3/.../dummy_task_orch.cpp, tests/st/a2a3/.../test_dummy_task.py
Adds case_id==4 with DENSE_DEP_COUNT=18 producer fanout / consumer fanin pattern and a DenseFanoutFanin test entry.

a5 dense fanin/fanout diagnostics

Layer / File(s) Summary
Threshold constant
src/a5/.../pto_runtime2_types.h
Adds PTO2_DEP_DEGREE_WARN_THRESHOLD = 16.
Orchestrator fanout tracking
src/a5/.../pto_orchestrator.cpp
Adds high-water/over-threshold counters, updates them in append_fanin_or_fail on newly claimed producers, logs and resets in mark_done.
Scheduler fanin tracking and shutdown summary
src/a5/.../pto_scheduler.h, src/a5/.../scheduler_cold_path.cpp, src/a5/.../pto_runtime2_init.cpp
Adds fanin_high_water/fanin_over_count fields, updates them in wire_task, resets them on ring reuse, and sums/logs them at scheduler shutdown.
Dense fanout/fanin test case
tests/st/a5/.../dummy_task_orch.cpp, tests/st/a5/.../test_dummy_task.py
Adds case_id==4 with DENSE_DEP_COUNT=18 producer fanout / consumer fanin pattern and a DenseFanoutFanin test entry.

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

Sequence Diagram(s)

sequenceDiagram
  participant Producer as Producer Task
  participant Orchestrator as append_fanin_or_fail
  participant Counters as Fanout/Fanin Counters
  participant Scheduler as wire_task
  participant Shutdown as SchedulerContext::shutdown

  Producer->>Orchestrator: claim producer fanout_count
  Orchestrator->>Counters: compute fanout_now, update high-water/over_count
  Orchestrator-->>Orchestrator: LOG_WARN if threshold crossed
  Scheduler->>Counters: check wfanin vs threshold
  Scheduler-->>Scheduler: LOG_WARN on new high-water
  Shutdown->>Counters: sum fanin_over_count across rings
  Shutdown-->>Shutdown: log summary if total > 0
Loading

Possibly related issues

Possibly related PRs

  • hw-native-sys/simpler#1066: Both modify append_fanin_or_fail in pto_orchestrator.cpp, with this PR adding diagnostics on top of the deduplication logic changed there.
  • hw-native-sys/simpler#1132: Both touch fanout_count/PTO2_FANOUT_SCOPE_BIT semantics in the same fanin/fanout claiming path.
  • hw-native-sys/simpler#1135: Both modify the fanout_lock-protected fanout claiming path in append_fanin_or_fail.

Poem

A rabbit counts the threads that bind,
sixteen deps and one to find—
"Too dense!" I thump, my warning clear,
high-water marks I hold so dear.
Hop, log, reset, then run again,
dense fanout tamed by a bunny's ken. 🐰📊

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: adding warnings when fanin/fanout exceeds 16 during dependency construction.
Description check ✅ Passed The description accurately explains the new diagnostics, thresholds, tests, and mirrored A2A3/A5 changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 dense-dependency diagnostics for both fanin and fanout across the a2a3 and a5 runtimes, logging warnings when dependency degrees exceed a threshold of 16 and printing summaries upon completion. It also adds a new test case DenseFanoutFanin to verify this behavior. The reviewer feedback highlights two main issues: first, the global static variables used for fanout diagnostics are only reset in mark_done(), which can cause state leakage across runs if a run aborts or fails; second, inline logging (LOG_WARN) within header-inline methods compiled for device should be avoided and delegated to cold-path helpers to prevent code bloat on the hot path.

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/runtime/tensormap_and_ringbuffer/runtime/pto_orchestrator.cpp Outdated
Comment thread src/a5/runtime/tensormap_and_ringbuffer/runtime/pto_orchestrator.cpp Outdated
Comment thread src/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/pto_scheduler.h Outdated
Comment thread src/a5/runtime/tensormap_and_ringbuffer/runtime/scheduler/pto_scheduler.h Outdated
@yanghaoran29 yanghaoran29 force-pushed the codehealth/warn-dense-fanin-fanout branch from d0803a8 to 1350ecc Compare July 9, 2026 01:41
…ruction

Very large fanin/fanout is a code-health smell (see hw-native-sys#959): it makes the
dependency graph hard to reason about and can hide an unexpectedly dense
wiring shape. Nothing flagged it before. Closes hw-native-sys#1261.

Add a two-part, bounded diagnostic, split by the phase where each degree
is naturally constructed:

- Fanout (pto_orchestrator.cpp, append_fanin_or_fail): the orchestrator
  increments each producer's fanout_count per claimed consumer. Emit an
  immediate WARN only on a new high-water above the threshold (computed
  under fanout_lock, logged after unlock — no logging in the critical
  section), and count each producer once at the 16->17 crossing.
- Fanin (pto_scheduler.h, wire_task): scheduler thread 0 wires the task's
  fanin edges; warn on a new per-ring high-water and count each task. The
  WARN is emitted through report_dense_fanin_warning(), defined out-of-line
  in pto_scheduler.cpp, so the device-compiled, header-inline wire_task hot
  path carries no logging code.

At program end, emit one summary line per metric:
- "=== [Orchestrator] tasks with fanout>16: N ===" in mark_done()
- "=== [Scheduler] tasks with fanin>16: N ===" in shutdown() (thread 0)

The high-water gate keeps the AICPU device log from flooding on the hot
path (codestyle rule 7). Both degree counters live as per-run state
(fanout on PTO2OrchestratorState, fanin on RingSchedState) and are reset
at the start of each run in reset_for_reuse — so an aborted run that skips
mark_done/shutdown cannot leak them into the next run on the same loaded
AICPU binary. Threshold PTO2_DEP_DEGREE_WARN_THRESHOLD=16. Purely
diagnostic — no behavior change, no new env/macro gate. Mirrored to a2a3
and a5.

Add a dummy_task st case (DenseFanoutFanin / case=4): one producer fanned
out to 18 dummy barriers, then one consumer depending on all 18, so both
degrees exceed the threshold. Verified on a2a3sim + a5sim that the WARNs
fire only on new maxima (17/18/19) and the summaries report 1/1.
@yanghaoran29 yanghaoran29 force-pushed the codehealth/warn-dense-fanin-fanout branch from 1350ecc to 33ce682 Compare July 9, 2026 02:06
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] Warn when task fanin/fanout exceeds 16 during dependency construction

1 participant