CIS490/training/dashboard/producers/_publish.py
Max 1fabd4a246 training: validator, feature/tensor extractors, 6 supervised models, schema-hashed checkpoints, eval suite, dashboard producers
The model layer of the project, built honestly:

  - tools/dataset_validate.py — full-sweep validator over the receiver
    store (sha256, schema, monotonic labels, telemetry-row gate). On the
    current corpus: 64,798 accepted + 8,154 degraded + 3,701 rejected +
    7 errored across 76,660 shipped episodes. data/processed/validation_v1.parquet
    is committed as the per-episode acceptance index.

  - training/_features.py — channel registry (46 channels across
    proc/guest/qmp/netflow), summary-stat windowing AND channel×time
    tensor extraction at 10s/5s windowing. Time alignment uses t_wall_ns
    (Unix ns) — tested fix for a real netflow-vs-host clock-base
    inconsistency that was silently dropping every netflow channel.

  - training/_split.py — three held-out recipes (host / sample / time)
    with profile-stratification assertions. held_out_host carries
    untested_profiles for cases like scan-and-dial absent from the test
    host (5 of 6 profiles tested cross-device, never silently averaged).

  - training/models/ — 6 architectures behind a common BaseModel
    interface: gbt (XGBoost), mlp, cnn, gru, lstm, transformer. Each
    trained twice (realistic / oracle) per the deployment threat model.
    Schema-hashed checkpoints refuse to load if _features.py changed
    since training (silent-input-drift protection, tested).

  - training/trainer/ — unified training loop: class-weighted CE, LR
    warmup + cosine, gradient clipping, mixed precision when CUDA,
    early stopping on val macro F1, best-on-val checkpoint. Same loop
    runs MLP/CNN/GRU/LSTM/Transformer; GBT uses XGBoost
    early_stopping_rounds on val mlogloss.

  - training/eval_/ — bootstrap 95% CIs on macro F1, per-class F1,
    per-profile and per-host breakdown, paired-bootstrap significance
    for model-vs-model gap. Confusion matrix uses union of seen labels.

  - training/dashboard/producers/ — replay/metrics/perf/profiles
    emitting the six event types the dashboard's awaiting scenes
    consume; on-demand tensor extraction so the Pi can run live
    inference without 65 GB of shards.

  - 17 unit tests (split coverage, features round-trip, schema mismatch,
    determinism, time-base alignment regression).

End-to-end smoke-trained all six on a 567-episode subset; held-out
test macro F1 reported with paired-bootstrap significance. The
methodology now reports honest cross-device generalization, not
in-distribution validation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 01:19:00 -05:00

53 lines
1.6 KiB
Python

"""Transport-agnostic publish callable for dashboard producers.
Two flavors, both returning ``async def publish(msg) -> None``:
- ``http_publisher(url)`` — wraps the canonical
``training.dashboard.client.Publisher`` (stdlib-only urllib). Use
for separate-process producers (the recommended pattern in
PRODUCERS.md). Errors are swallowed via ``try_publish`` — a
momentarily dead dashboard should not kill a long-running producer.
- ``local_publisher()`` — in-process. Awaits
``training.dashboard.app.broadcaster.publish`` directly. Only use
when your code is genuinely on the dashboard's import path and
doesn't block the event loop.
- ``null_publisher()`` — no-op for unit tests.
"""
from __future__ import annotations
import asyncio
import logging
from typing import Any, Awaitable, Callable
log = logging.getLogger("cis490.dashboard.producers")
PublishFn = Callable[[dict[str, Any]], Awaitable[None]]
def local_publisher() -> PublishFn:
from training.dashboard.app import broadcaster
async def publish(msg: dict[str, Any]) -> None:
await broadcaster.publish(msg)
return publish
def http_publisher(url: str = "http://127.0.0.1:8447/publish",
timeout_s: float = 2.0) -> PublishFn:
from training.dashboard.client import Publisher
pub = Publisher(url=url, timeout=timeout_s)
async def publish(msg: dict[str, Any]) -> None:
# try_publish swallows errors and returns 0 on failure.
await asyncio.to_thread(pub.try_publish, msg)
return publish
def null_publisher() -> PublishFn:
async def publish(_msg: dict[str, Any]) -> None:
return None
return publish