|
| 1 | +"""GUI-facing public API: verify + estimate in one call, adapter |
| 2 | +suggestion lookup, ergonomic dim_env builders. |
| 3 | +
|
| 4 | +This module is what the visual-builder front-end actually imports. It |
| 5 | +wires together Stages A-D into the three operations the GUI cares about: |
| 6 | +
|
| 7 | + - :func:`verify_and_estimate` — on every graph mutation, return both |
| 8 | + the diagnostics list (red/yellow/green per edge) and the memory |
| 9 | + report (bar at the top: ``18.2 / 80 GB``). |
| 10 | + - :func:`suggest_adapters` — when the user hovers a red edge, propose |
| 11 | + the adapter chain that would close it. |
| 12 | + - :func:`suggest_dim_env` — sensible default named-dim env for a given |
| 13 | + preset name, so the GUI doesn't have to ship a full editor before |
| 14 | + the first verify lands. |
| 15 | +
|
| 16 | +Designed to run in <50 ms per call for any of the 12 architecture |
| 17 | +presets at production-scale dim envs (B=1, S=4096, H=4096) — the |
| 18 | +GUI workflow test in tests/v4/test_vbspec_stage_e.py enforces this. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import time |
| 24 | +from collections.abc import Mapping, Sequence |
| 25 | +from dataclasses import dataclass, field |
| 26 | +from typing import Any |
| 27 | + |
| 28 | +from cppmega_v4.fusion import FusionRegionPlan, plan_fusion_regions |
| 29 | +from cppmega_v4.fusion.brick_graph import BrickGraph, BrickNode |
| 30 | +from cppmega_v4.spec.adapters import ( |
| 31 | + AdapterSuggestion, |
| 32 | + suggest_adapter_chain, |
| 33 | +) |
| 34 | +from cppmega_v4.spec.memory_report import ( |
| 35 | + MemoryReport, |
| 36 | + estimate_memory, |
| 37 | +) |
| 38 | +from cppmega_v4.spec.resolver import ( |
| 39 | + ResolvedBrickGraph, |
| 40 | + resolve_shapes, |
| 41 | +) |
| 42 | + |
| 43 | + |
| 44 | +# --------------------------------------------------------------------------- |
| 45 | +# Default named-dim envs per preset |
| 46 | +# --------------------------------------------------------------------------- |
| 47 | + |
| 48 | + |
| 49 | +# Production-scale defaults. The GUI ships these so a fresh-open model |
| 50 | +# already has a sensible env without the user touching dim sliders. |
| 51 | +# Override by passing your own ``dim_env`` to verify_and_estimate. |
| 52 | +_PROD_BASE: Mapping[str, int] = { |
| 53 | + "B": 1, "S": 4096, "H": 4096, |
| 54 | + "nh": 32, "nkv": 4, "head_dim": 128, |
| 55 | + "num_experts": 8, "top_k": 2, |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +_PROD_MLA_EXTRAS: Mapping[str, int] = { |
| 60 | + "q_lora_rank": 1536, "kv_lora_rank": 512, |
| 61 | + "qk_rope_head_dim": 64, "qk_nope_head_dim": 128, "v_head_dim": 128, |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +_PROD_GEMMA_EXTRAS = {"sliding_window_size": 1024} |
| 66 | +_PROD_NEMOTRON_EXTRAS = {"d_state": 64} |
| 67 | +_PROD_ZAYA_EXTRAS = {"fine_window": 256, "coarse_block_size": 16} |
| 68 | + |
| 69 | + |
| 70 | +_PRESET_DIM_ENVS: Mapping[str, Mapping[str, int]] = { |
| 71 | + "qwen3_next": dict(_PROD_BASE), |
| 72 | + "kimi_linear": {**_PROD_BASE, **_PROD_MLA_EXTRAS}, |
| 73 | + "kimi_k2": {**_PROD_BASE, **_PROD_MLA_EXTRAS}, |
| 74 | + "deepseek_v3": {**_PROD_BASE, **_PROD_MLA_EXTRAS}, |
| 75 | + "deepseek_v4_flash": dict(_PROD_BASE), |
| 76 | + "gemma4": {**_PROD_BASE, **_PROD_GEMMA_EXTRAS}, |
| 77 | + "mistral4": {**_PROD_BASE, **_PROD_MLA_EXTRAS}, |
| 78 | + "ling26": {**_PROD_BASE, **_PROD_MLA_EXTRAS}, |
| 79 | + "longcat": {**_PROD_BASE, **_PROD_MLA_EXTRAS}, |
| 80 | + "nemotron3": {**_PROD_BASE, **_PROD_NEMOTRON_EXTRAS}, |
| 81 | + "zaya1": {**_PROD_BASE, **_PROD_ZAYA_EXTRAS}, |
| 82 | + "arcee_trinity": {**_PROD_BASE, **_PROD_GEMMA_EXTRAS}, |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +def suggest_dim_env(preset_name: str | None = None) -> dict[str, int]: |
| 87 | + """Return a sensible default named-dim env. |
| 88 | +
|
| 89 | + With ``preset_name=None``, returns the generic production-scale |
| 90 | + base env (B=1, S=4096, H=4096, nh=32, nkv=4, head_dim=128, |
| 91 | + num_experts=8, top_k=2). With a preset name, returns the env that |
| 92 | + includes any preset-specific extra dims (MLA LoRA ranks, sliding |
| 93 | + window size, etc.). |
| 94 | + """ |
| 95 | + if preset_name is None: |
| 96 | + return dict(_PROD_BASE) |
| 97 | + if preset_name not in _PRESET_DIM_ENVS: |
| 98 | + raise KeyError( |
| 99 | + f"unknown preset {preset_name!r}; " |
| 100 | + f"available: {sorted(_PRESET_DIM_ENVS)}" |
| 101 | + ) |
| 102 | + return dict(_PRESET_DIM_ENVS[preset_name]) |
| 103 | + |
| 104 | + |
| 105 | +# --------------------------------------------------------------------------- |
| 106 | +# verify_and_estimate |
| 107 | +# --------------------------------------------------------------------------- |
| 108 | + |
| 109 | + |
| 110 | +_DEFAULT_SIDE_CHANNELS = frozenset({"doc_ids", "token_ids"}) |
| 111 | + |
| 112 | + |
| 113 | +@dataclass(frozen=True) |
| 114 | +class VerificationResult: |
| 115 | + """One-shot result of :func:`verify_and_estimate`.""" |
| 116 | + |
| 117 | + resolved: ResolvedBrickGraph |
| 118 | + fusion_plan: tuple[FusionRegionPlan, ...] |
| 119 | + memory: MemoryReport |
| 120 | + elapsed_ms: float |
| 121 | + fits_on_device: bool | None = None |
| 122 | + |
| 123 | + @property |
| 124 | + def has_errors(self) -> bool: |
| 125 | + return self.resolved.has_errors |
| 126 | + |
| 127 | + def summary(self) -> dict[str, Any]: |
| 128 | + """Compact dict the GUI can render directly.""" |
| 129 | + return { |
| 130 | + "errors": len(self.resolved.errors), |
| 131 | + "warnings": len(self.resolved.warnings), |
| 132 | + "regions": len(self.fusion_plan), |
| 133 | + "memory": self.memory.summary(), |
| 134 | + "elapsed_ms": round(self.elapsed_ms, 2), |
| 135 | + "fits_on_device": self.fits_on_device, |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | +def verify_and_estimate( |
| 140 | + graph: BrickGraph, |
| 141 | + dim_env: Mapping[str, int] | None = None, |
| 142 | + *, |
| 143 | + preset_name: str | None = None, |
| 144 | + device_hbm_bytes: int | None = None, |
| 145 | + headroom: float = 0.9, |
| 146 | + training: bool = True, |
| 147 | + optimizer: str = "adamw", |
| 148 | + dtype_bytes: int = 2, |
| 149 | + kv_cache_dtype_bytes: int = 1, |
| 150 | + available_side_channels: frozenset[str] = _DEFAULT_SIDE_CHANNELS, |
| 151 | + strict: bool = False, |
| 152 | +) -> VerificationResult: |
| 153 | + """Resolve shapes, plan fusion regions, estimate memory — one shot. |
| 154 | +
|
| 155 | + Args: |
| 156 | + graph: the BrickGraph to verify. |
| 157 | + dim_env: concrete int values per named dim. If None, falls back |
| 158 | + to :func:`suggest_dim_env(preset_name)`. |
| 159 | + preset_name: lookup key for the default dim_env if dim_env=None. |
| 160 | + device_hbm_bytes: when supplied, the result includes |
| 161 | + ``fits_on_device`` (Memory.fits_on(device, headroom)). |
| 162 | + headroom: HBM headroom fraction for fits_on (default 0.9). |
| 163 | + training: forwarded to estimate_memory. |
| 164 | + optimizer, dtype_bytes, kv_cache_dtype_bytes: forwarded. |
| 165 | + available_side_channels: forwarded to resolve_shapes; defaults to |
| 166 | + ``{"doc_ids", "token_ids"}`` (the most common case for our bricks). |
| 167 | + strict: when True, resolver raises on first ERROR instead of |
| 168 | + collecting diagnostics. |
| 169 | + """ |
| 170 | + if dim_env is None: |
| 171 | + dim_env = suggest_dim_env(preset_name) |
| 172 | + |
| 173 | + t0 = time.perf_counter() |
| 174 | + resolved = resolve_shapes( |
| 175 | + graph, dim_env, |
| 176 | + strict=strict, |
| 177 | + available_side_channels=available_side_channels, |
| 178 | + ) |
| 179 | + plans = tuple(plan_fusion_regions(graph)) |
| 180 | + memory = estimate_memory( |
| 181 | + resolved, |
| 182 | + fusion_plan=plans, |
| 183 | + training=training, |
| 184 | + optimizer=optimizer, |
| 185 | + dtype_bytes=dtype_bytes, |
| 186 | + kv_cache_dtype_bytes=kv_cache_dtype_bytes, |
| 187 | + ) |
| 188 | + elapsed_ms = (time.perf_counter() - t0) * 1000.0 |
| 189 | + |
| 190 | + fits = ( |
| 191 | + memory.fits_on(device_hbm_bytes, headroom=headroom) |
| 192 | + if device_hbm_bytes is not None else None |
| 193 | + ) |
| 194 | + return VerificationResult( |
| 195 | + resolved=resolved, |
| 196 | + fusion_plan=plans, |
| 197 | + memory=memory, |
| 198 | + elapsed_ms=elapsed_ms, |
| 199 | + fits_on_device=fits, |
| 200 | + ) |
| 201 | + |
| 202 | + |
| 203 | +# --------------------------------------------------------------------------- |
| 204 | +# suggest_adapters |
| 205 | +# --------------------------------------------------------------------------- |
| 206 | + |
| 207 | + |
| 208 | +@dataclass(frozen=True) |
| 209 | +class AdapterProposal: |
| 210 | + """Bundle returned by :func:`suggest_adapters`. |
| 211 | +
|
| 212 | + Fields: |
| 213 | + producer / consumer: the names of the edge endpoints. |
| 214 | + producer_shape / consumer_shape: resolved shapes on the edge. |
| 215 | + chain: ordered list of AdapterSuggestion steps; empty when shapes |
| 216 | + already match; None when no chain ≤ max_steps exists. |
| 217 | + reason: human-readable explanation for the GUI tooltip. |
| 218 | + """ |
| 219 | + |
| 220 | + producer: str |
| 221 | + consumer: str |
| 222 | + producer_shape: tuple[int, ...] |
| 223 | + consumer_shape: tuple[int, ...] |
| 224 | + chain: list[AdapterSuggestion] | None |
| 225 | + reason: str |
| 226 | + |
| 227 | + |
| 228 | +def suggest_adapters( |
| 229 | + resolved: ResolvedBrickGraph, |
| 230 | + producer: str, |
| 231 | + consumer: str, |
| 232 | + *, |
| 233 | + max_steps: int = 4, |
| 234 | +) -> AdapterProposal: |
| 235 | + """Look up the resolved edge ``producer -> consumer`` and produce |
| 236 | + an :class:`AdapterProposal`. Raises KeyError if no such edge.""" |
| 237 | + edge = resolved.edge(producer, consumer) |
| 238 | + chain = suggest_adapter_chain( |
| 239 | + edge.producer_shape, edge.consumer_shape, max_steps=max_steps, |
| 240 | + ) |
| 241 | + if edge.matched: |
| 242 | + reason = "shapes already match — no adapter needed" |
| 243 | + elif chain is None: |
| 244 | + reason = ( |
| 245 | + f"no adapter chain (≤ {max_steps} hops) bridges " |
| 246 | + f"{edge.producer_shape} -> {edge.consumer_shape}" |
| 247 | + ) |
| 248 | + elif not chain: |
| 249 | + reason = "shapes match by value but resolver disagreed — no adapter needed" |
| 250 | + else: |
| 251 | + reason = f"chain of {len(chain)} adapter step(s): " + " -> ".join( |
| 252 | + s.kind for s in chain |
| 253 | + ) |
| 254 | + return AdapterProposal( |
| 255 | + producer=producer, |
| 256 | + consumer=consumer, |
| 257 | + producer_shape=edge.producer_shape, |
| 258 | + consumer_shape=edge.consumer_shape, |
| 259 | + chain=chain, |
| 260 | + reason=reason, |
| 261 | + ) |
| 262 | + |
| 263 | + |
| 264 | +__all__ = [ |
| 265 | + "AdapterProposal", |
| 266 | + "VerificationResult", |
| 267 | + "suggest_adapters", |
| 268 | + "suggest_dim_env", |
| 269 | + "verify_and_estimate", |
| 270 | +] |
0 commit comments