Skip to content

feat(inference): CBAR-PIECE-5 PR-1 — Qwen GPU residency gate (pure-functions slice) - #1330

Closed
joelteply wants to merge 1 commit into
feat/grid-inference-routing-pr1-capabilityfrom
feat/cbar-piece5-qwen-residency-gate
Closed

joelteply wants to merge 1 commit into
feat/grid-inference-routing-pr1-capabilityfrom
feat/cbar-piece5-qwen-residency-gate

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

CBAR-SUBSTRATE missing-piece #5, PR-1. Stacks on #1315 (GRID-INFERENCE-ROUTING PR-1) — same inference_capability module, different file, same pure-functions cadence.

#1315's probe answers "does this node have an advertisable GPU at all?" — this gate answers the next question one level deeper: "will the SELECTED MODEL actually fit with all layers on that GPU, evidenced not guessed?"

Per CBAR-SUBSTRATE-ARCHITECTURE.md §336 piece #5: before any local-generation turn runs, the gate must surface: selected model, backend matching platform, GPU layer count, unsupported layers, VRAM residency estimate. "CPU graph splits or unsupported Qwen layers are blockers unless the turn is explicitly degraded with a visible reason."

This PR ships the data + pure derivation layer. No GGUF I/O, no dispatch wiring. PR-2 wires the GGUF reader. PR-3 wires the gate into the turn dispatcher with enforcement.

What ships

  • BackendChoice — Metal | Cuda | Vulkan, lowercase ts-rs export. Selected by select_backend(hw) per the happy-path rule.
  • QwenModelMetadata — model_name, architecture, layer_count, parameter_count_billions, bytes_per_parameter_quantized, layer_kinds_needing_check. Pure data populated by future PR-2 GGUF reader.
  • ResidencyEvidence — typed evidence emitted on Pass; covers every CBAR-SUBSTRATE-required field (model_name, architecture, backend, gpu_layer_count, estimated_vram_bytes, free_vram_bytes, platform).
  • ResidencyGateResult — Pass(evidence) | Block { reasons }, tagged-union, ts-rs outcome discriminator.
  • BlockReason — NoGpuBackendOnNode | UnsupportedLayer | PartialGpuSplit | WrongBackendForPlatform, typed surfaces of specific cause.
  • Pure fns: select_backend, check_residency_gate.

Failure-mode discipline (non-negotiable per vhsm-d1f4 audit pass 1)

  • ✅ No silent CPU split: PartialGpuSplit fires when free VRAM < estimate.
  • ✅ No silent fallback: NoGpuBackendOnNode fires when no GPU at all (exclusive reason — bypasses other checks).
  • ✅ No silent unsupported layer: UnsupportedLayer fires per-kind for Vulkan + qwen3moe (vendored llama.cpp Vulkan gap today).
  • ✅ No hardcoded enums: BackendChoice is tagged enum; layer_kinds_needing_check is Vec<String> — new layer kinds plug in.
  • ✅ No assumed defaults: every field comes from inputs.

Backend precedence (matches probe.rs llamacpp advertisement rule)

Platform Backend chosen
Apple Silicon Metal
NVIDIA (CUDA + Vulkan available) CUDA
AMD/Intel discrete with Vulkan Vulkan
CPU-only None → Block

Metal wins over CUDA on a Mac (native path). CUDA wins over Vulkan on NVIDIA (llama.cpp CUDA kernels more complete than Vulkan today).

Test plan

41 passing on cargo test --lib --features metal,accelerate inference_capability::residency::

  • select_backend (4): picks Metal/CUDA/Vulkan correctly per HW class; None on CPU-only
  • check_residency_gate happy paths (4): M5 Pro / MacBook Air M2 / Blackwell / AMD-Vulkan all run their expected Qwen variants with full evidence
  • check_residency_gate block paths (4): CPU-only blocks with NoGpuBackendOnNode exclusive; M2 blocks 30B for VRAM; AMD Vulkan blocks Qwen3 MoE with UnsupportedLayer; Vulkan + Qwen2 PASSES (Vulkan handles qwen2 today)
  • VRAM estimate (3): Q4 7B in 3-5GB band, Q4 30B in 14-18GB band, scales with quantization
  • Evidence + serde (5): every required field present on Pass; lowercase BackendChoice; tagged-union round-trips; camelCase
  • Edge cases (8): inclusive-vram-boundary pass; one-byte-under blocks; tiny model on CPU still blocks; probe-passes-residency-blocks composition; multi-reason block accumulates; reasons() empty slice on Pass; FP16 7B blocks on 8GB Mac; WrongBackend variant round-trips
  • Layer-kind detail (3): backend_choice_as_str; vulkan emits one UnsupportedLayer per kind; empty layer_kinds never emits
  • ts-rs exports (5): BackendChoice, BlockReason, QwenModelMetadata, ResidencyEvidence, ResidencyGateResult

cargo check clean on --features metal,accelerate.

VDD evidence

N/A for this slice — pure data + derivation, no inference dispatch. Evidence lands with PR-3 (enforcement wiring).

What this enables: dynamicism the team has been naming. A persona's turn runs on Metal on a Mac, CUDA on a Blackwell, Vulkan on AMD — same code path, hardware-probed, gate-blocked when the model wouldn't fit, no silent CPU degrade. Same architecture works on a 16GB MacBook Air and a 32+64GB RTX 5090; only the gate's verdict differs.

Stack

When #1315 merges, this PR rebases cleanly to canary.

…nctions slice)

CBAR-SUBSTRATE missing-piece #5 (docs/architecture/CBAR-SUBSTRATE-ARCHITECTURE.md
§336): Qwen GPU residency gate. Stacks on PR #1315 (GRID-INFERENCE-ROUTING PR-1)
inference_capability module — different file, same module surface, same pure-
functions cadence as rate_proposals + generate_recipe + #1315 PR-1s.

#1315's probe answers "does this node have an advertisable GPU at all?" This
gate answers the next question one level deeper: "will the SELECTED MODEL
actually fit with all layers on that GPU, evidenced not guessed?"

Per CBAR-SUBSTRATE spec, before any local-generation turn runs:

- Selected Qwen model named explicitly
- Backend (Metal / CUDA / Vulkan) named + matches platform
- GPU layer count reported
- Unsupported layers enumerated (Vulkan-llama.cpp gaps, etc.)
- VRAM residency estimate covers all layers
- "CPU graph splits or unsupported Qwen layers are blockers unless the
  turn is explicitly degraded with a visible reason."

What ships (pure-functions slice — no GGUF I/O, no dispatch wiring; PR-2
wires the GGUF reader to populate QwenModelMetadata, PR-3 wires the gate
into the actual turn dispatcher with a block-the-turn enforcement point):

- BackendChoice (Metal / Cuda / Vulkan) — lowercase ts-rs export
- QwenModelMetadata — model_name, architecture, layer_count,
  parameter_count_billions, bytes_per_parameter_quantized,
  layer_kinds_needing_check. Pure data populated by future PR-2 GGUF reader
- ResidencyEvidence — typed evidence emitted on Pass; covers every
  CBAR-SUBSTRATE-required field
- ResidencyGateResult — Pass(evidence) | Block { reasons } tagged-union
- BlockReason — NoGpuBackendOnNode | UnsupportedLayer | PartialGpuSplit |
  WrongBackendForPlatform (typed, surfaces specific cause)
- Pure functions: select_backend, check_residency_gate

Failure-mode discipline (non-negotiable per vhsm-d1f4 audit pass 1):

- No silent CPU split: PartialGpuSplit fires when free VRAM < estimate
- No silent fallback: NoGpuBackendOnNode fires when no GPU at all
- No silent unsupported layer: UnsupportedLayer fires per-kind for
  Vulkan + qwen3moe (vendored llama.cpp Vulkan gap today)
- No hardcoded enums: BackendChoice is a tagged enum; QwenModelMetadata's
  layer_kinds_needing_check is Vec<String> (new layer kinds plug in)
- No assumed defaults: every field comes from inputs

Backend selection precedence (matches probe.rs llamacpp advertisement rule):
Mac → Metal, NVIDIA → CUDA, AMD/Intel → Vulkan, CPU-only → None.
Metal wins over Cuda on a Mac (native path); CUDA wins over Vulkan on
NVIDIA hardware (llama.cpp CUDA kernels more complete than Vulkan today).

Tests: 41 passing on cargo test --lib --features metal,accelerate
inference_capability::residency::

- select_backend (4): picks Metal/CUDA/Vulkan correctly per HW class; None
  on CPU-only
- check_residency_gate happy paths (4): M5 Pro / MacBook Air M2 / Blackwell
  / AMD-Vulkan all run their expected Qwen variants with full evidence
- check_residency_gate block paths (4): CPU-only blocks with
  NoGpuBackendOnNode + exclusive reason; M2 blocks 30B for VRAM; AMD Vulkan
  blocks Qwen3 MoE with UnsupportedLayer; vulkan-+-Qwen2 PASSES (vulkan
  handles qwen2 today, not qwen3moe)
- VRAM estimate (3): Q4 7B in 3-5GB band, Q4 30B in 14-18GB band,
  estimate scales with quantization
- Evidence + serde (5): every required field present on Pass; BackendChoice
  lowercase; BlockReason + ResidencyGateResult tagged-union round-trips;
  QwenModelMetadata + ResidencyEvidence camelCase
- Edge cases (8): inclusive-vram-boundary pass; one-byte-under blocks;
  tiny model on CPU still blocks; probe-passes-residency-blocks
  composition; multi-reason block accumulates; reasons() empty slice on
  Pass; FP16 7B blocks on 8GB Mac; WrongBackend variant round-trips
- Layer-kind detail (3): backend_choice_as_str; vulkan emits one
  UnsupportedLayer per kind; empty layer_kinds never emits
- ts-rs exports (5): BackendChoice, BlockReason, QwenModelMetadata,
  ResidencyEvidence, ResidencyGateResult

Cargo check clean on --features metal,accelerate.

This is PR-1 of CBAR-PIECE-5. PR-2 wires GGUF metadata reader (extends
backends::read_gguf_metadata with block_count + parameter count) to
populate QwenModelMetadata from a path. PR-3 wires the gate result into
the turn dispatcher with enforcement (block the turn instead of letting
it silently run).

VDD evidence N/A — pure data + derivation, no inference dispatch.
Evidence lands with PR-3.

Stack:
- #1315 GRID-INFERENCE-ROUTING PR-1 (this PR's base; OPEN, MERGEABLE,
  zero file conflict)
- This PR: inference_capability/residency.rs (PIECE-5 PR-1)
- Future PR-2: GGUF reader + metadata populator
- Future PR-3: dispatcher integration + enforcement
@joelteply
joelteply deleted the branch feat/grid-inference-routing-pr1-capability May 16, 2026 21:18
@joelteply joelteply closed this May 16, 2026
joelteply pushed a commit that referenced this pull request May 17, 2026
… bridge

Pure-function bridge between PressureBroker's PressureAlert surface
(disk/memory pool eviction events) and the governor's typed
PressureSignal cascade input. Per GENOME-FOUNDRY-SENTINEL.md Part 11
line 1121: "PressureBroker informs the SubstrateGovernor. Pressure
signals from the broker drive the governor's adjustment cascade."

Scope:
- `alert_to_signal(&PressureAlert) -> Option<PressureSignal>` — pure
  mapping. High/Critical tier → SystemMemHigh{used_pct}; Normal/
  Warning/unknown → None.
- `governor_alert_sink(Arc<dyn SubstrateGovernor>) -> AlertSink` —
  factory that wraps a governor as an AlertSink the broker can register
  via `PressureBroker::add_alert_sink`. Sink derives the signal and
  forwards via `governor.on_pressure_signal` when Some; drops when None.

NOT in this PR (deferred to PR-5):
- Wiring the sink into PressureBrokerModule's boot path. The bridge is
  the data-side primitive; the wiring is a separate concern.
- Pool-name-aware mapping (vram → VRAMHigh, etc.). Today's broker pools
  are all memory-adjacent (Docker disk, HF cache, future VRAM via
  GpuMemoryManager); SystemMemHigh is the conservative single-mapping
  the cascade reacts to identically. Refinement when pool tier_name
  conventions stabilize.

Discipline:
- No silent default-on-error. Mapping is total — every alert maps to
  either Some(signal) or None explicitly.
- Pressure clamped to [0.0, 1.0] before percent conversion so transient
  over-budget snapshots map to 100% and negative artifacts map to 0%
  rather than wrapping via `as u8`.
- Sink forwards via `Arc<dyn SubstrateGovernor>` (object-safe trait) so
  the bridge does not depend on LocalSubstrateGovernor concretely.

Tests (14, all passing):
- normal/warning/unknown tiers -> None (4 tests)
- high/critical tiers -> SystemMemHigh with rounded used_pct (3 tests)
- pressure clamping above 1.0 + below 0.0 + rounding (3 tests)
- sink forwarding high/critical + non-forwarding normal/warning (4 tests)
- sink survives construction-scope drop + multi-call ordering (2 tests)

Lane H 8-PR stack progress: PR-1 (#1330/1331) -> PR-2 (#1345) -> PR-3a
(#1352) -> PR-3b (#1354) -> PR-3c1 (#1356) -> PR-3c2 (#1360) -> PR-3c3
(#1364) -> PR-3c4 (#1365) -> **PR-4 (this PR)**. PR-3d governor file
watcher in flight from codex on parallel branch (no overlap).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
joelteply pushed a commit that referenced this pull request May 17, 2026
… bridge

Pure-function bridge between PressureBroker's PressureAlert surface
(disk/memory pool eviction events) and the governor's typed
PressureSignal cascade input. Per GENOME-FOUNDRY-SENTINEL.md Part 11
line 1121: "PressureBroker informs the SubstrateGovernor. Pressure
signals from the broker drive the governor's adjustment cascade."

Scope:
- `alert_to_signal(&PressureAlert) -> Option<PressureSignal>` — pure
  mapping. High/Critical tier → SystemMemHigh{used_pct}; Normal/
  Warning/unknown → None.
- `governor_alert_sink(Arc<dyn SubstrateGovernor>) -> AlertSink` —
  factory that wraps a governor as an AlertSink the broker can register
  via `PressureBroker::add_alert_sink`. Sink derives the signal and
  forwards via `governor.on_pressure_signal` when Some; drops when None.

NOT in this PR (deferred to PR-5):
- Wiring the sink into PressureBrokerModule's boot path. The bridge is
  the data-side primitive; the wiring is a separate concern.
- Pool-name-aware mapping (vram → VRAMHigh, etc.). Today's broker pools
  are all memory-adjacent (Docker disk, HF cache, future VRAM via
  GpuMemoryManager); SystemMemHigh is the conservative single-mapping
  the cascade reacts to identically. Refinement when pool tier_name
  conventions stabilize.

Discipline:
- No silent default-on-error. Mapping is total — every alert maps to
  either Some(signal) or None explicitly.
- Pressure clamped to [0.0, 1.0] before percent conversion so transient
  over-budget snapshots map to 100% and negative artifacts map to 0%
  rather than wrapping via `as u8`.
- Sink forwards via `Arc<dyn SubstrateGovernor>` (object-safe trait) so
  the bridge does not depend on LocalSubstrateGovernor concretely.

Tests (14, all passing):
- normal/warning/unknown tiers -> None (4 tests)
- high/critical tiers -> SystemMemHigh with rounded used_pct (3 tests)
- pressure clamping above 1.0 + below 0.0 + rounding (3 tests)
- sink forwarding high/critical + non-forwarding normal/warning (4 tests)
- sink survives construction-scope drop + multi-call ordering (2 tests)

Lane H 8-PR stack progress: PR-1 (#1330/1331) -> PR-2 (#1345) -> PR-3a
(#1352) -> PR-3b (#1354) -> PR-3c1 (#1356) -> PR-3c2 (#1360) -> PR-3c3
(#1364) -> PR-3c4 (#1365) -> **PR-4 (this PR)**. PR-3d governor file
watcher in flight from codex on parallel branch (no overlap).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
joelteply added a commit that referenced this pull request May 17, 2026
)

Pure-function bridge between PressureBroker's PressureAlert surface
(disk/memory pool eviction events) and the governor's typed
PressureSignal cascade input. Per GENOME-FOUNDRY-SENTINEL.md Part 11
line 1121: "PressureBroker informs the SubstrateGovernor. Pressure
signals from the broker drive the governor's adjustment cascade."

Scope:
- `alert_to_signal(&PressureAlert) -> Option<PressureSignal>` — pure
  mapping. High/Critical tier → SystemMemHigh{used_pct}; Normal/
  Warning/unknown → None.
- `governor_alert_sink(Arc<dyn SubstrateGovernor>) -> AlertSink` —
  factory that wraps a governor as an AlertSink the broker can register
  via `PressureBroker::add_alert_sink`. Sink derives the signal and
  forwards via `governor.on_pressure_signal` when Some; drops when None.

NOT in this PR (deferred to PR-5):
- Wiring the sink into PressureBrokerModule's boot path. The bridge is
  the data-side primitive; the wiring is a separate concern.
- Pool-name-aware mapping (vram → VRAMHigh, etc.). Today's broker pools
  are all memory-adjacent (Docker disk, HF cache, future VRAM via
  GpuMemoryManager); SystemMemHigh is the conservative single-mapping
  the cascade reacts to identically. Refinement when pool tier_name
  conventions stabilize.

Discipline:
- No silent default-on-error. Mapping is total — every alert maps to
  either Some(signal) or None explicitly.
- Pressure clamped to [0.0, 1.0] before percent conversion so transient
  over-budget snapshots map to 100% and negative artifacts map to 0%
  rather than wrapping via `as u8`.
- Sink forwards via `Arc<dyn SubstrateGovernor>` (object-safe trait) so
  the bridge does not depend on LocalSubstrateGovernor concretely.

Tests (14, all passing):
- normal/warning/unknown tiers -> None (4 tests)
- high/critical tiers -> SystemMemHigh with rounded used_pct (3 tests)
- pressure clamping above 1.0 + below 0.0 + rounding (3 tests)
- sink forwarding high/critical + non-forwarding normal/warning (4 tests)
- sink survives construction-scope drop + multi-call ordering (2 tests)

Lane H 8-PR stack progress: PR-1 (#1330/1331) -> PR-2 (#1345) -> PR-3a
(#1352) -> PR-3b (#1354) -> PR-3c1 (#1356) -> PR-3c2 (#1360) -> PR-3c3
(#1364) -> PR-3c4 (#1365) -> **PR-4 (this PR)**. PR-3d governor file
watcher in flight from codex on parallel branch (no overlap).

Co-authored-by: Test <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant