Skip to content

hw-gate: every GPU step takes flock ~/npu.lock, so the gate cannot run while an NPU serve is up (c16 gate cut 3x by its own 20-min hold) #682

Closed

Description

@bong-water-water-bong

Summary

scripts/hw-gate.sh takes flock ~/npu.lock before every GPU step, and that lock is the one an NPU
serve holds for its entire lifetime. So while any NPU model is up, the hardware gate cannot run: it sits in
flock at 0 % CPU until the caller's own hold expires, is killed, re-queues, and sits again. Tonight that
cost c16's gate three consecutive attempts without a single step executing.

Now that GPU and NPU concurrency has been measured safe (#673), the gate should take the NPU lock only for
its npu phase
, which is the only phase that touches the device and already does exactly that.

The code, at origin/main

line what it does
scripts/hw-gate.sh:94 LOCK=${HW_GATE_LOCK:-$HOME/npu.lock} — the lock is the NPU device lock
scripts/hw-gate.sh:95 LOCK_WAIT=${HW_GATE_LOCK_WAIT:-5400} — a step will wait 90 minutes for it
scripts/hw-gate.sh:303 gpu() — "hold the GPU token, wait for coder-llm, hold the lock, mem-guard + thermal-run"
scripts/hw-gate.sh:316 flock -w "$LOCK_WAIT" -E 75 "$LOCK" "$0" --cool --mem "${GPU_MODEL:--}" … mem-guard.sh "$MEM_FLOOR" "$THERMAL" "$@"
scripts/hw-gate.sh:580 flock -w "$LOCK_WAIT" -E 75 "$LOCK" sh -c "$HW_GATE_NPU_CHECK" — the npu phase, correct today
scripts/hw-gate.sh:569-570 its own comment: the npu phase "runs inside the gate's GPU claim and holds ~/npu.lock, and is NOT wrapped in thermal-run"

So every GPU step — build, ops, kat, kld, needle, placement — serialises against the NPU device lock, and
gpu() treats a lock timeout as infra=1 (:335), i.e. "the box could not run the gate", after up to
LOCK_WAIT.

The behaviour is not unknown to the docs; it is written down as a cost: docs/hw-gate.md:199-201 —
"Waiting for ~/npu.lock costs more than the gate itself: flock is not first-come-first-served, and with
about ten jobs queueing, each acquisition waited 10 to 60 minutes. The summary reports both numbers (waiting,
holding)."
That was tolerable when the queue was GPU jobs. It is not tolerable when the holder is an NPU
serve that holds the lock for hours by design.

Evidence: three cuts, no steps run

From ~/briefs/TRELEASE-3-STATE.md, entry 31 Oct 00:18 (line 1849 of that file):

  • the ops unit was cut by its own 20-minute gpu-hold twice — attempt 2 token 23:44:42 → cut 00:04:42,
    attempt 3 token 00:07:43 — and a third attempt followed the same way; the unit failed with 92;
  • the holder was the owner's Bonsai serve
    (/home/bcloud/wt/w-587-build/1bit forward-serve -m …/bonsai2-27b-NPU/model.q4nx, pid 3272533),
    fuser -v ~/npu.lock showing it F.... through six samples over a minute;
  • the gate sat in flock at 0 % CPU for the whole window, so the 20-minute hold was spent waiting, not
    measuring — and the same attempt re-queued into the same wait.

The lane's own reading of the options (same entry) concluded that pointing HW_GATE_LOCK at a private file
was not acceptable, because "the lock is what keeps GPU and NPU work from colliding on heat and memory".
That premise is what #673 changed.

Why the lock is no longer needed on the GPU steps

  • docs(hybrid): NPU and GPU run at the same time, measured — no hazard, ~13 % stream cost (engine#612 item 5) #673 (docs, open PR) measures the NPU and the GPU running at the same time on this box: no hazard,
    ~13 % stream cost
    . That retires the collision argument for correctness.
  • The GPU steps keep every guard they need without npu.lock:
    • ~/GPU-TOKEN fair queue, taken per step (:304, take_token), still serialises GPU work against other
      GPU jobs and against Hyperloom;
    • scripts/mem-guard.sh (MEM_FLOOR 12 GiB) and the tools/mem_budget.py pre-check that refuses a step
      which would take GTT to 95 GiB or past MemAvailable (:318-322, rc=76);
    • thermal-run.sh cooling and the cool() gate (:578 for the npu phase, per step for GPU);
    • the post-step amdgpu ring-timeout check (:331-334).
  • The npu phase keeps the lock at :580, unchanged — that is the phase whose device the lock protects.

Smallest fix

In gpu() (:316), stop wrapping the step in flock; keep the self re-exec that does the memory pre-check
and the cooling, and leave LOCK/LOCK_WAIT in place for the npu phase:

-    flock -w "$LOCK_WAIT" -E 75 "$LOCK" "$0" --cool --mem "${GPU_MODEL:--}" "$ROOT/scripts/mem-guard.sh" "$MEM_FLOOR" "$THERMAL" "$@"
+    "$0" --cool --mem "${GPU_MODEL:--}" "$ROOT/scripts/mem-guard.sh" "$MEM_FLOOR" "$THERMAL" "$@"

plus the doc lines that describe the old behaviour (docs/hw-gate.md:129 "It holds ~/npu.lock (flock) only
while it runs…", and :199-201), and the header comment at scripts/hw-gate.sh:63-64. rc=75
(:335, lock timeout) then applies to the npu phase only, where it is meaningful.

Acceptance

  1. With an NPU serve running and holding ~/npu.lock, hw-gate.sh completes its GPU phases (build, ops,
    kat, kld, needle, placement) and reports its timing.tsv "waiting" split as GPU-TOKEN + heat + memory
    only — no lock wait.
  2. Two concurrent gate runs still serialise on ~/GPU-TOKEN (no overlap in their GPU steps).
  3. mem-guard.sh still refuses a step that would cross the 12 GiB MemAvailable floor / 95 GiB GTT.
  4. The npu phase still waits for ~/npu.lock and still returns infra=1 on rc=75.
  5. A run with the NPU serve up shows no new amdgpu ring timeout (the existing post-step check).

New exposure, stated plainly

GPU steps and the NPU serve will now share the box's memory and heat without the lock as an interlock. The
GPU side is still budgeted (mem_budget.py, mem-guard.sh); the NPU side is not — nothing in that path
consults MemAvailable before it grows. If a fix here is followed by a combined-memory incident, the next step
is to have the NPU serve publish its footprint in a file the gate's pre-check can read, not to re-introduce a
global lock. #680 (page faults at GTT 106.5 of 120 GiB) is the shape of what that interlock would have to
prevent.

Scope

Small-fix candidate, deliberately after the release: one script file plus two doc paragraphs, no
behaviour change when no NPU serve is running, and it can be tested on this box without a window once #673
has landed.

Related

Activity

  1. added
    bug
    on Oct 31, 2026
  2. bong-water-water-bong commented on Nov 1, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Merged — closing. PR #716 → merge commit cd83e1202 (main, 2026-11-01 02:29:55Z).

    What is verified, and what is not:

    CI on the merged commit build pass (4m34s), pr_agent pass, CodeQL (actions/c-cpp/python/rust) pass, hw_gate skipped (no hw-gate label)
    the change gpu() no longer wraps the step in flock ~/npu.lock; the NPU phase still takes it (an NPU dispatch must not overlap another). GPU token, coder-llm idling, cool-down, mem-guard and the 76-retry untouched; rc 75 is no longer produced by a GPU step, so the three callers now treat 76 only
    the regression guard new invariant in tests/hw_gate_test.py (ctest hw_gate_judges): gpu() must contain no flock, the NPU phase must — verified both ways, PASS as merged and FAIL with the old line put back
    premise engine#673: 160/160 byte-exact, zero resets, NPU stream keeps ~87 % of its bandwidth with the GPU busy (7.40 GB/s vs 8.2–8.5 idle)
    still to be seen on hardware the gate has not run since the merge. The proof is the next gate run (c19): its summary should show waiting only for heat/memory, no NPU-lock wait, and it must run while an NPU serve is up — that is the whole point of the issue

    Run that gate capped and alone (systemd-run --user -p MemoryMax=<cap>, free -g first, nothing else loading):
    it loads the 27B row, and the 23:38 OOM is what an uncapped gate plus a neighbour looks like. If a new amdgpu ring
    timeout appears in it, this diff is the first suspect and the comment goes here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions