Commit Graph

5144 Commits

Author SHA1 Message Date
jgrusewski
affb0e24cf infra(argo): plumb --batch-size + --auto-horizon-weights through template
Adds two new workflow parameters with backward-compatible defaults
(batch-size=1, auto-horizon-weights=false) so existing submissions
behave identically. Both flags are forwarded to alpha_train CLI.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 10:44:24 +02:00
jgrusewski
c3ee5e165a feat(ml-alpha): wire batched kernels through trainer + CLI (#8)
Plumbs the batched cfc + heads kernels added in 829ddfa62 through
PerceptionTrainer, evaluator, and the alpha_train CLI:

  PerceptionTrainerConfig.n_batch         — batch size, default 1
  PerceptionTrainer::step_batched         — process B sequences per
                                            optimizer step using
                                            cfc_step_batched / heads_batched
  PerceptionTrainer::evaluate_batched     — forward-only batched eval
  PerceptionTrainer::step / evaluate      — thin B=1 wrappers preserving
                                            existing single-sequence
                                            test/inference APIs (assert
                                            cfg.n_batch == 1)
  alpha_train CLI: --batch-size N         — accumulates B sequences per
                                            optimizer step in train loop;
                                            val loop also batches and uses
                                            evaluate_batched

Per-K scratch buffers all grow to [K, B, dim] layout (K-major, slot-k
contiguous). Mamba2's [B, K, H] output is transposed once after
forward via the new transpose_3d_swap_01 kernel, and grad_h_enriched_seq_t
is transposed back to [B, K, H] before Mamba2 backward. Two transposes
per training step; negligible (1.5MB at B=32).

Dead unbatched kernel handles removed from the trainer (step_fn,
step_bwd_fn, heads_fn, heads_bwd_fn, grad_x_d) — all training and
inference now go through the batched variants for B ≥ 1. The
single-sample kernels remain in CUDA for the standalone test helpers
in cfc/step.rs and heads.rs.

Local 2Q smoke (seq_len=32, B=4, --auto-horizon-weights, 800 train
seqs × 2 epochs):
  epoch 0: val_loss=0.7138 AUC h30/h100/h300/h1000/h6000 = .55/.55/.57/.61/.51
  epoch 1: val_loss=0.6558 AUC h30/h100/h300/h1000/h6000 = .72/.68/.75/.68/.65

vs the in-flight qf5mj baseline (B=1, K=96, no horizon weighting) which
had val_loss=0.6933 best and AUCs oscillating at ~0.50 — this batched
run hits AUC 0.75 (h300) and 0.72 (h30) in just 2 epochs of 200
optimizer updates. Batching + horizon-weighting unblocks the model.

77 ml-alpha tests pass.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 10:42:35 +02:00
jgrusewski
829ddfa62c feat(ml-alpha): add batched cfc + heads CUDA kernels (foundation for #8)
Adds 4 new kernel symbols alongside the existing single-sample ones —
zero changes to current call sites, so the in-flight qf5mj baseline is
unaffected. The next commit wires these into PerceptionTrainer's K
loop and exposes --batch-size in the CLI.

  cfc_step_batched              processes [n_batch, n_in/n_hid] tensors
  cfc_step_backward_batched     same; shared mem holds sd_pre[B, n_hid]
                                + sdecay[n_hid] (~16 KiB at B=32, well
                                under L40S 48 KiB block limit). Param
                                grads (grad_b/grad_w_in/grad_w_rec/
                                grad_tau) accumulated via += — thread i
                                is sole writer to its row across all
                                samples, so no atomicAdd and no per-
                                batch scratch buffer.

  multi_horizon_heads_batched    [n_batch, 5] sigmoid outputs from
                                 [n_batch, 128] hidden inputs.
  multi_horizon_heads_backward_batched
                                 shared mem holds sd_z[B, 5]. grad_w
                                 / grad_b += across batch (thread tid
                                 sole writer). grad_h carries the
                                 optional per-sample grad_h_carry
                                 (cfc recurrence chain).

Design notes:
  - Threading: one block of n_hid threads. Each thread loops over
    b ∈ 0..B internally. This avoids cross-block races on grad_*
    buffers and keeps the existing "no atomicAdd" discipline. Cost:
    less raw parallelism than grid-batching, but the bottleneck is
    Mamba2 (already batch-parallel via its own kernel grid).
  - Per-thread accumulators: grad_b / grad_tau land in registers,
    flushed once at end. grad_w_in / grad_w_rec written += per-b
    (thread sole writer to its row, safe).
  - All B samples processed in stream order inside one kernel launch
    — saves K * (B-1) launches per sequence vs serialising B
    independent calls.

77 ml-alpha tests pass (kernels not yet exercised — wiring is the
next commit).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 10:20:27 +02:00
jgrusewski
85ce295773 feat(ml-alpha): per-horizon BCE weighting (fixes label-correlation inflation)
For seq_len K and horizon h with h ≫ K, the K position-supervised
labels in a single sequence are near-identical (sequential positions'
forward windows overlap by ~(h-1)/h). Per-position BCE therefore
treats ~K highly-correlated labels as independent samples, inflating
gradient pressure on long horizons by a factor of K.

Concretely at K=96:
  h=30   → ~3 effective samples per seq (forward windows overlap ~97%)
  h=100  → ~1                          (~99%)
  h=6000 → ~1                          (~99.98%)

Per-position supervision was paying 96× the natural signal density on
h=6000, pulling the model toward fitting noise at long horizons.

Fix: the fused BCE kernel now accepts an optional
`loss_weights[N_HORIZONS]` (nullptr → uniform = no-op). Each (k, h)
loss + grad contribution is multiplied by w_h; the normaliser is the
sum of weighted valid entries instead of the raw valid count.

`auto_horizon_weights(K, horizons)` computes `w_h = min(1.0, K/h)` so
short horizons stay at full weight and long horizons collapse to
their independent-sample density. Exposed via CLI:
  --auto-horizon-weights              # K/h auto-derived
  --horizon-weights "1,1,0.5,0.1,0.02" # explicit floats

Default behaviour is uniform (1.0) — apples-to-apples with the
in-flight qf5mj baseline. Synthetic overfit still 0.6268 → 0.1144 in
250 steps (82% drop). 77 tests pass.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 10:17:38 +02:00
jgrusewski
248d8fe510 feat(ml-alpha): full architectural pass — recurrent CfC, GPU BCE, regime features, training discipline
Comprehensive fix for the issues identified after the BPTT-unroll cluster
run plateaued at val_loss ~0.692 with oscillating AUCs:

ARCHITECTURE
  - CfC h_old is now RECURRENT across positions. Previously reset to
    zero every step → CfC degenerated to a per-cell tanh-FC layer.
    New: h_old at step k IS h_new at step k-1. Heads still operate
    on h_new_k, but now the CfC actually carries state. Reverse-order
    backward through the K positions accumulates grad_h_old → grad_h_new
    via the new optional `grad_h_carry` arg on multi_horizon_heads_backward.
  - tau is TRAINED. cfc_step_backward now writes grad_tau (per-cell decay
    constant derivative), trainer gets a 7th AdamW group at 0.1× cfc lr.
  - 6 NEW regime features (EMA cascade computed loader-side per file)
    fill slots out[20..26] of snap_features. Gives the model multi-minute
    trend / volatility / liquidity context that is structurally unreachable
    inside the K-snapshot BPTT window. Slots: mid-z (med/slow), trend
    signal, log-vol slow, log-spread med, log-trade-rate med. All bounded
    via log1p / signed-log so no tuned constants leak in.

PERFORMANCE (NVIDIA-style)
  - GPU-fused multi-horizon BCE for the entire [K, N_HORIZONS] grid in
    ONE launch (was K host roundtrips). Native NaN-label masking.
  - K-loop is fully GPU-resident: pre-allocated per-K scratch
    (h_new_per_k, probs_per_k, labels_per_k, grad_probs_per_k), zero
    device allocs inside step(). Only TWO syncs per sequence (after
    forward, after backward) vs previously 2K+1.
  - Stream-ordered kernel launches with pointer-offset addressing into
    per-K buffers — host doesn't wait between K iterations.
  - cfc_step_backward / multi_horizon_heads_backward both use += grad
    semantics; trainer pre-zeroes accumulators once per step().
  - MAMBA2_ALPHA_MAX_K capped at 96 (was temporarily at 256). 96 covers
    h=30/100/300 with room; regime features handle h=1000/h=6000.

TRAINING DISCIPLINE
  - LR schedule: linear warmup (default 200 steps) + cosine decay to
    lr * lr_min_factor (default 0.1). Applied per training step to both
    CfC and Mamba2 AdamW groups via new set_lr_cfc/set_lr_mamba2.
  - Best-checkpoint tracking by val_loss; recorded in summary
    (best_epoch, best_val_loss, best_val_auc).
  - Early stopping on val_loss plateau (default patience = 3).
  - CRITICAL BUG FIX: validation now uses new `evaluate()` method
    (forward-only) instead of `step()`. Previous CLI called step()
    on val data, which ran the full backward + AdamW update on the
    validation set. With per-step BPTT that's ~K× more pressure than
    the old comment ("statistically negligible") assumed.

Synthetic overfit: 0.6442 → 0.1233 in 250 steps (81% drop, sharper
than the previous 70%). 77 ml-alpha tests pass.

Local 2Q smoke (seq_len=64, 600 train seqs/epoch, 4 epochs):
  val_loss 0.7011 → 0.6990, best epoch=1, h300 AUC 0.565 in epoch 0.

Phase E.3 callers (ml/examples/alpha_baseline.rs,
alpha_dqn_h600_smoke.rs) use the LEGACY Mamba2 forward_train +
backward_from_h_enriched path — unaffected by these changes (their
kernels are pre-zeroed via alloc_zeros, so the += grad semantics
remain correct in single-call mode).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 09:59:37 +02:00
jgrusewski
485150c7b7 feat(ml-alpha): lift Mamba2 kernel seq_len cap from 32 → 256
Previous BPTT-unroll run had val_loss trending (0.6941→0.6922 over 5
epochs) but AUCs oscillating around 0.50 — the architecture lacked
context for medium/long horizons (h300, h1000, h6000 ≫ seq_len=32).
Phase 1d.2 validated the SSM at seq_len=6000; this is a step toward
restoring useful sequence depth.

Bumps:
  - MAMBA2_ALPHA_MAX_K constant: 32 → 256
  - x_hist per-thread replay buffer: 2KB → 16KB (spills to
    DRAM-backed per-thread local memory; L2-cached, acceptable
    perf cost vs the 8x context gain)
  - Mamba2BlockConfig::validate updates the cap

Backward compat: legacy Phase E.3 callers (alpha_baseline,
alpha_dqn_h600_smoke) only use K=12 / K=32 — unaffected by the
larger compile-time max.

Synthetic overfit still converges 0.7135 → 0.2079 in 250 steps.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 09:13:48 +02:00
jgrusewski
16f5febf27 feat(ml-alpha): per-step supervision unrolls BPTT through full sequence
The final-step-only trainer (one BCE prediction per 32-snapshot
window) trained flat at chance on real ES data despite working on
synthetic overfit: train_loss=0.6953, val_loss=0.6943 across 40k
gradient steps. Gradient density was the bottleneck — one supervised
position per sequence × ~8K seqs/epoch isn't enough signal for the
SSM to find the alpha.

This commit supervises the model at EVERY position in the sequence:

  mamba2_alpha_scan_fwd_seq    — emits h_enriched at every t step
                                 ([N, K, sh2] instead of [N, sh2])
  mamba2_alpha_scan_bwd_seq    — accepts d_h_enriched_seq, injects
                                 gradient at each t before propagating
                                 d_state through the gate chain.
                                 d_w_c and d_h_s2 accumulate across t.

  PerceptionTrainer.step()    — loop k=0..K; cfc + heads + BCE at
                                each valid label; cfc/heads grads
                                accumulate via += in kernel writes.
                                One Mamba2 backward call consumes the
                                full grad_h_enriched_seq.

  cfc_step_backward            — grad_w_in/w_rec/b writes changed
                                 to += (callers MUST pre-zero).
  multi_horizon_heads_backward — grad_w/grad_b writes changed to +=.

  alpha_train.rs               — passes per-position label rows to
                                 step(); AUC still scored from
                                 last-position predictions.

Phase E.3 callers (alpha_baseline.rs, alpha_dqn_h600_smoke.rs) use
the LEGACY Mamba2 forward_train + backward path with `alloc_zeros`
grad buffers — unaffected.

Synthetic overfit still converges 0.6664 → 0.1976 in 250 steps.
Local 2-quarter ES.FUT smoke shows the val AUC at h300 climbing
0.513 → 0.566 over 3 epochs (was flat-at-chance before). First
gradient signal we've gotten through the new architecture.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 01:03:33 +02:00
jgrusewski
2289fa062a feat(ml-alpha): normalize snap features to O(1)-O(10) scale
First L40S run (alpha-perception-s6hqv, commit 586d1e782) trained flat
at chance: train_loss=0.6953, val_loss=0.6943, AUCs all near 0.50
across 5 epochs and 40k gradient steps. Synthetic overfit on the same
trainer hit 0.59→0.19 in 250 steps, so wiring was sound.

The signal-killer was feature scale: raw size deltas were ±100, dt_ms
could exceed 1e4, and log-returns sat at ~1e-4. Mamba2's W_in
projection saturates on those extremes, gradient bleeds out.

Now in the kernel:
  out[0]     = (mid - prev_mid) / tick_size           [tick-return]
  out[12..17]= sgn(d) * log1p(|d|)                    [signed-log OFI]
  out[18]    = sgn(v) * log1p(|v|)                    [signed-log vol]
  out[19]    = log1p(max(dt_ms, 0))                   [log dt]

No tuned constants — tick_size is a market quantity; log1p and
signed-log are monotone bounded transforms. Bit-equiv tests updated
to assert the new closed-form expressions (still GPU-only, no CPU
oracle).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 00:33:17 +02:00
jgrusewski
586d1e7820 perf(ml-alpha): cache loaded file across next_sequence calls
The MultiHorizonLoader was calling load_or_predecode_mbp10 on every
next_sequence() call, deserializing millions of MBP-10 snapshots per
sequence. With 8000 sequences and 9 files this gave ~50+ hour
training time on what should be IO-trivial work.

Now keep one file cached (LoadedFile { snapshots, labels_full }),
yield ceil(n_max_sequences / n_files) sequences from it before
advancing. Per-horizon labels are computed once per file load and
sliced cheaply per anchor. With 8000/9: ~890 loads → 9 loads.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 00:19:06 +02:00
jgrusewski
4514313793 infra(argo): use decimal seed value — clap u64 parser rejects hex
alpha_train --seed is clap-typed as u64, and clap's default u64
parser only accepts decimal digits. Previous default "0x4242"
hit "invalid digit found in string" at startup.

Replace with decimal equivalent 16962 (= 0x4242) in both the
submission script default and the workflow template default.

Long-term: could add a custom clap value_parser that accepts
hex/dec/oct prefixes, but for now decimal-only matches the
foxhunt convention in other CLIs (alpha_baseline, etc.).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 00:09:43 +02:00
jgrusewski
1f14b994e4 infra(argo): fix MBP-10 data path — futures-baseline-mbp10/ES.FUT
The training-data PVC actually has its MBP-10 .dbn.zst files at
/data/futures-baseline-mbp10/ES.FUT (9 files for ES futures), NOT at
/data/futures-baseline/mbp10. The latter path doesn't exist; the
prev run's bash check fired exit 1 with "MBP-10 data directory not
found".

The PVC root layout (from a probe pod):
  /data/bin/                              <- compiled binaries by SHA
  /data/feature-cache/                    <- predecoded sidecar cache
  /data/futures-baseline/{ES,NQ,ZN,6E}.FUT/  <- legacy multi-asset
  /data/futures-baseline-mbp10/ES.FUT/    <- ES MBP-10 (this is what we want)
  /data/futures-baseline-trades/ES.FUT/   <- ES trades
  /data/futures-baseline-1s/ES.FUT/       <- 1-second OHLCV
  /data/trained-models/                   <- model checkpoints

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 00:06:32 +02:00
jgrusewski
9f866fa369 infra(argo): fix train image — foxhunt-training-runtime not training-runtime
The CI pipeline (.gitlab-ci.yml stage `build-foxhunt-training-runtime`)
builds and pushes the image as foxhunt-training-runtime:latest (with
the foxhunt- prefix). Other consumers in the repo agree:
  - infra/k8s/training/image-prepuller.yaml
  - infra/k8s/training/job-template.yaml

The alpha-perception template (and the older alpha-cv template) used
training-runtime:latest without the prefix — broken since the image
never existed at that path. Kubelet kept hitting ErrImagePull /
ImagePullBackOff with "not found".

This was the next blocker after the CPU oversizing fix. Stopping the
in-flight workflow and resubmitting on the corrected template.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 00:00:28 +02:00
jgrusewski
d2bfeaa983 infra(argo): check-cache pre-stage — skip ensure-binary on cache hit
Adds a tiny alpine pod (check-cache) that runs first on the platform
pool (no autoscaler delay, ~3 sec end-to-end) and probes the
training-data PVC for /data/bin/$SHA/alpha_train. Outputs:
  - sha:   short SHA used for binary cache keying
  - cache: "hit" or "miss"

ensure-binary now has `when: cache == miss` — when the binary is
already cached for the current SHA, the entire ~4.8GB ci-builder
image pull + sccache compile cycle is skipped. Re-runs on the same
SHA now go straight from submission to training in ~30 seconds
instead of ~3 minutes.

train depends on check-cache + ensure-binary; sources the SHA from
check-cache's output (works whether ensure-binary ran or was
skipped — Argo treats `when:` skip as a satisfied dependency).

Submission script (scripts/argo-alpha-perception.sh) now pre-resolves
commit-sha=HEAD to an actual git SHA via `git rev-parse origin/<branch>`
before submission. This lets the alpine check-cache pod work without
installing git in the container.

Also removed the now-stale `ci-training-h100x2|ci-training-h100-sxm`
case branch from the SM-arch detection — those pools no longer exist
post-pool-cleanup commit a252119fd.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:56:19 +02:00
jgrusewski
3ad3971ae6 infra(argo): fix train pod CPU request — L40S has 8 vCPU not 16
Previous cpu request was 8 (limit 16), but L40S-1-48G allocatable cpu
is 7800m (8 vCPU total minus kubelet overhead). Pod couldn't fit on
the very node we wanted it on — autoscaler provisioned the L40S
cleanly but the scheduler then rejected the pod with
"Insufficient cpu" forever.

Fix: requests cpu=6 / mem=16Gi, limits cpu=7 / mem=64Gi. Leaves
~1.8 vCPU and ~27Gi memory headroom for the system daemonsets
(cilium, csi-node, nvidia driver/device-plugin/dcgm-exporter,
gpu-feature-discovery, node-bootstrap, prometheus-node-exporter,
promtail) that the L40S node hosts.

The trainer itself is GPU-bound (kernels do the work), so 6 vCPU is
plenty for the orchestration host process.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:50:40 +02:00
jgrusewski
a252119fd4 infra(kapsule): remove h100x2 + h100-sxm pools
Reasons:
- h100-sxm: Scaleway account quota is 0/0 for the SXM instance type
  (cp_servers_type_H100_SXM_2_80G). The pool was perpetually trying
  to maintain size=1 with a creation_error node, which JAMMED the
  cluster autoscaler. That blocked L40S scale-up entirely during
  today's alpha-perception cluster run.
- h100x2: more expensive than current workloads justify. Every
  training path (alpha perception, future PPO) fits on either the
  single-GPU H100 or L40S.

Removed:
- Two `scaleway_k8s_pool` resources from infra/modules/kapsule/main.tf
- Six variables (enable + type + max_size for each pool)
- Two outputs (pool_id for each)
- Corresponding inputs in infra/live/production/kapsule/terragrunt.hcl

The live cluster has the SXM pool stuck node manually deleted via
`scw k8s node delete` (this commit-session); the pool resource itself
will be destroyed on next `terragrunt apply`.

Post-cleanup pool inventory:
- platform (DEV1-L × 3)
- ci-training-h100  (H100-1-80G, max 1)  <- regular single-GPU
- ci-training-l40s  (L40S-1-48G, max 1)  <- primary training target
- ci-compile-cpu    (POP2-HC, max 4)
- ci-compile-cpu-hm (POP2-HM, max 1)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:43:12 +02:00
jgrusewski
3dc42a6827 infra(argo): correct warmup-gpu doc — cluster grace is 10+10=20m
The actual Kapsule autoscaler_config (infra/modules/kapsule/main.tf
lines 46-52) is scale_down_delay_after_add="10m" +
scale_down_unneeded_time="10m". Combined, a freshly-provisioned node
won't be eligible for scaledown for 10m + another 10m unneeded
before action, so effective grace window is ~20m.

Update the comment in the warmup-gpu template to cite the actual
config rather than the speculative "15m" value. Behavior is
unchanged — the warmup pod still exits immediately and relies on
the grace window to keep the node warm for train.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:32:33 +02:00
jgrusewski
29cf092551 infra(argo): warmup-gpu exits immediately — relies on Scaleway 15min scaledown grace
Removed the 30s sleep. The warmup pod's purpose is to make the L40S
pool autoscaler scale 0 → 1; once the pod lands on the new node
(Scheduled → Running → Succeeded), the node enters Scaleway Kapsule's
scaledown-grace window (~15 min). That single window covers the
entire range of ensure-binary durations (sccache-hit ~10s through
cold ~15 min), so train always lands on a hot node without holding
the warmup pod open.

Trimmed cpu request 100m → 50m and mem 64Mi → 32Mi: the pod runs
~one shell command then exits; tiny resource footprint = faster
scheduling and no kubelet noise.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:31:40 +02:00
jgrusewski
260b07c492 infra(argo): warmup-gpu DAG branch — pipeline compile + L40S provisioning
Adds a parallel warmup-gpu task that runs concurrently with
ensure-binary. The warmup pod is a tiny CPU-only alpine container
scheduled on the gpu-pool's nodeSelector — its presence triggers
cluster-autoscaler scale-up of the L40S pool. After a 30s sleep, the
warmup pod exits; the node enters Scaleway's scaledown grace window
(~10 min), so the train pod lands on a hot node without waiting for
autoscaler provisioning.

No GPU resource request on the warmup pod — that would serialise
warmup and train on the same GPU. nodeSelector + nvidia.com/gpu
toleration are sufficient to force placement on the L40S pool.

Expected savings: ~3-5 min per cold cluster submission. First run
(this session, alpha-perception-4vl7c) showed compile took 113s
(sccache warm) with serial GPU provisioning following; subsequent
submissions should overlap the two stages.

DAG topology:
  ensure-binary ──┐
                  ├──> train
  warmup-gpu  ────┘   (only depends on ensure-binary; warmup is
                       fire-and-forget infrastructure)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:29:16 +02:00
jgrusewski
ef35b10f3e refactor(ml-alpha): drop competitive-gate machinery — stacked is default
Per user direction "no gating, this is the new default": the stacked
Mamba2 -> CfC -> heads design is THE production architecture. There's
no competing-baseline comparison to run. Validation reduces to normal
training metrics (per-horizon val AUC, train loss curve, sanity floor
of >0.5 AUC).

Deletions:
  - crates/ml-alpha/src/gate/cfc_vs_mamba2.rs (gate verdict logic)
  - crates/ml-alpha/src/gate/mod.rs
  - crates/ml-alpha/examples/alpha_gate.rs (gate runner binary)

Renames:
  - crates/ml-alpha/src/gate/auc.rs -> crates/ml-alpha/src/eval/auc.rs
  - lib.rs: pub mod gate -> pub mod eval (gate implied comparison;
    eval doesn't)

Spec amendments:
  - Drop the "Gate baseline strategy" amendment (committed earlier
    this session)
  - Reframe the stacked-architecture amendment as a "decision" not a
    "gate"; production path is unambiguous
  - Reframe Section 4 "Validation gate: CfC must meet Mamba2" -> just
    "Validation: per-horizon val AUC" with the >0.5 sanity floor

Doc cleanups: stale "Mamba2 gate baseline" mentions in build.rs and
pinned_mem.rs replaced with neutral wording. The Argo template
comment about "downstream gate consumption" becomes "for monitoring".

Test status: all 26+ ml-alpha tests pass. AUC tests (6/6) still pass
under the eval:: namespace.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:17:33 +02:00
jgrusewski
6e868fd136 spec(ml-alpha): gate-baseline ablation strategy amendment
Defines the Mamba2-only baseline for the stacked-vs-baseline gate
verdict as an ablation of the SAME PerceptionTrainer (a --bypass-cfc
flag), not a separate model. Apples-to-apples; same data window,
same hyperparameters, same code path. The only difference is whether
the CfC step is in the loop.

Three ablation options evaluated:
  1. --bypass-cfc flag (recommended): Mamba2 -> heads directly
  2. --mamba2-state-dim 2 (crippled Mamba2, CfC stays)
  3. Frozen CfC initialized to identity (no code branch needed)

Option 1 wins on clarity: it answers "is CfC additive on top of
Mamba2" unambiguously, with the same Mamba2 capacity and same
training regime in both arms.

Concrete next-session work documented (1-2 hours):
  - PerceptionTrainerConfig.bypass_cfc: bool + step() branch
  - alpha_train --bypass-cfc CLI flag
  - alpha-perception-template.yaml workflow parameter + bash branch
  - submit both runs, fetch summaries, alpha_gate, commit verdict

gate_verdict logic unchanged — the cfc/mamba2 naming in the report
becomes stacked/bypass at the binding layer; the verdict math is
generic.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:12:34 +02:00
jgrusewski
522178b2a7 infra(argo): alpha-perception workflow + submission script
Argo WorkflowTemplate at infra/k8s/argo/alpha-perception-template.yaml
runs the stacked Mamba2 -> CfC -> heads PerceptionTrainer on a single
L40S in fr-par-2. Two-stage DAG:
  ensure-binary  (ci-compile-cpu pool, sccache-backed cargo build of
                  alpha_train example, SHA-keyed binary cache under
                  /data/bin/$SHORT_SHA/)
  train          (ci-training-l40s pool, runs the cached binary against
                  /data/futures-baseline/mbp10 with predecoded sidecar
                  cache at /feature-cache/predecoded, writes
                  alpha_train_summary.json to
                  /feature-cache/alpha-perception-runs/$SHA/)

Defaults mirror the validated synthetic-overfit smoke config:
  epochs=5, seq_len=32, mamba2_state_dim=16, lr_cfc=3e-3,
  lr_mamba2=1e-3, n_train_seqs=8000, n_val_seqs=1000, seed=0x4242

Submission script scripts/argo-alpha-perception.sh wraps argo submit
with the standard L40S/H100 cuda-compute-cap mapping. --watch
follows logs.

Workflow nodeSelector pinned to fr-par-2 (consistent with the cluster
topology constraint). ttlStrategy 1h after completion;
activeDeadlineSeconds 4h cap (well above expected ~30-90 min wall).

This is the cluster entrypoint for the stacked perception design.
Once it lands a summary on MinIO, the gate runner (alpha_gate, Task
17) can consume it.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:10:37 +02:00
jgrusewski
1dc1f3563c refactor(ml-alpha): consolidate trainers — stacked is THE perception trainer
The merged Mamba2 -> CfC -> heads design from the 2026-05-16 spec
amendment supersedes the CfC-alone path. Removing the old CfC-only
PerceptionTrainer and renaming the stacked Mamba2CfcTrainer to
PerceptionTrainer (one trainer, clean naming).

Deletions:
  - src/trainer/perception.rs (the OLD CfC-only trainer)
  - tests/perception_overfit.rs (CfC-only smoke)
  - tests/perception_debug_dump.rs (CfC-only trajectory print)
  - tests/stacked_overfit.rs (replaced by perception_overfit pointing
    at the renamed module)

Renames:
  - src/trainer/stacked.rs -> src/trainer/perception.rs
  - Mamba2CfcTrainer -> PerceptionTrainer
  - Mamba2CfcTrainerConfig -> PerceptionTrainerConfig
  - tests/stacked_overfit.rs content -> tests/perception_overfit.rs

CLI rewrite:
  examples/alpha_train.rs now drives the stacked PerceptionTrainer.
  Per-step inputs are sequences (Vec<Mbp10RawInput>) of length
  seq_len; labels come from the LAST position of the window
  (per-horizon). Flags: --seq-len, --mamba2-state-dim, --lr-cfc,
  --lr-mamba2 (no more --n-hid since hidden_dim is fixed at 128 to
  match Mamba2 and CfC by design).

Test status:
  - 64 GPU tests pass on local sm_86 (31 lib unit + 33 integration)
  - synthetic-overfit (rebranded perception_overfit): 250 steps,
    initial=0.5951 -> final=0.1917 (68% drop, well above 40% gate)
  - All bit-equiv / finite-diff / invariant tests still PASS
  - One ignored test: the gate_artifact integration (waiting for
    cluster-trained summary inputs)

The cluster gate (Task 18) now compares stacked-trained AUC vs a
Mamba2-baseline AUC (TBD: stacked vs a simpler "Mamba2 only" config
or an external reference baseline).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:08:25 +02:00
jgrusewski
45b203e31e feat(ml-alpha): Mamba2CfcTrainer — stacked Mamba2 -> CfC -> heads
Realizes the 2026-05-16 spec amendment merging Mamba2 + CfC into one
stacked architecture (vs the original "compete via gate" framing).

Forward chain:
  snap_features × seq_len
   -> window pack [1, seq_len, FEATURE_DIM]
   -> Mamba2Block.forward_train -> (logit, cache.h_enriched [1, hidden_dim])
   -> cfc_step(x=h_enriched, h_old=0) -> h_new
   -> heads -> probs [5]
   -> BCE(probs, labels)

Backward chain:
  BCE -> grad_probs
   -> heads_backward -> grad_h_new + grad_W_heads, grad_b_heads
   -> cfc_step_backward -> grad_W_in, grad_W_rec, grad_b + grad_x (=grad_h_enriched)
   -> Mamba2.backward_from_h_enriched(&cache, &grad_h_enriched_tensor)
      -> Mamba2BackwardGrads (full 9-tensor gradient set)

Optimizers (6 total):
  - 5 CfC AdamWs (W_in, W_rec, b, heads_w, heads_b) — reused from
    PerceptionTrainer's per-param-group pattern
  - 1 Mamba2AdamW for all 9 Mamba2 parameter tensors (existing
    implementation in mamba2_block.rs)

Synthetic-overfit on constant +1 direction (seq_len=16, state_dim=8,
lr_cfc=3e-3, lr_mamba2=1e-3, 250 steps):
  initial_avg=0.5951 → final_avg=0.1917 (68% drop, well past 40% gate).
  Monotone descent at all 5 progress checkpoints.

Architectural note (v1): CfC runs with h_old=0 each step (no inter-
step recurrence). With h_old=0, the CfC layer is effectively per-cell
tau-scaled tanh FC. Inter-step CfC state (h_old carrying between
calls) is a v2 extension once the cluster gate validates the v1
foundation.

The cluster gate (Task 18) now has the actual stacked production
trainer to deploy, not a CfC-alone-vs-Mamba2-alone bench.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 23:03:55 +02:00
jgrusewski
deed15b34e feat(ml-alpha): cfc_step_backward emits grad_x for upstream chain
Adds grad_x[k] = sum_i d_pre[i] * W_in[i,k] computed by thread 0 of
the cfc_step_backward kernel (after the existing __syncthreads in
the shared-mem sd_pre relay). Required by the stacked Mamba2 -> CfC
design: Mamba2.backward_from_h_enriched needs grad on h_enriched,
which is the CfC's "x" input in the stacked topology.

For the existing CfC-only PerceptionTrainer (x = snap_features, no
upstream learnable layer), grad_x is computed but discarded into a
preallocated buffer.

backward_finite_diff tests still pass (4/4) — the new arg is the
14th positional kernel arg; existing callers updated. perception_
overfit smoke still passes (loss 0.5669 -> 0.0665 in 200 steps).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 22:59:36 +02:00
jgrusewski
e29d06b282 feat(ml-alpha): CfC-vs-Mamba2 gate verdict + alpha_gate binary
GateReport + GateVerdict + load_summary in src/gate/cfc_vs_mamba2.rs.
Verdict criterion per spec Section 4 (with 2026-05-16 stacked
amendment): CfC AUC >= Mamba2 AUC - tolerance at every horizon.
Default tolerance 0.01.

alpha_gate binary takes two AlphaTrainSummary JSON paths (one per
backbone, generated by alpha_train), runs the verdict, emits
phase_a_gate.json with the full report + verdict, exits 0/1.

Tests (5/5 lib unit):
  - PASS when CfC >= Mamba2 everywhere
  - PASS within tolerance (CfC 0.005 below)
  - FAIL at long horizon (h=1000, 6000)
  - FAIL at short horizon (h=30)
  - delta signs correctly track relative performance

For the cluster gate run (Task 18), the Mamba2 baseline summary is
generated by an existing/separate Mamba2 trainer pass on the same
data window. Apples-to-apples comparison requires identical train +
val seeds and quarters.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 22:50:07 +02:00
jgrusewski
c2bfbfef18 feat(ml-alpha): alpha_train CLI binary
CLI wraps PerceptionTrainer + MultiHorizonLoader for end-to-end
training. Per-epoch loop:
  - train: stream sequences from MultiHorizonLoader, step per
    position with reset_hidden_state (K=1 BPTT), accumulate train
    loss
  - val: separate loader on disjoint seed, accumulate (probs,
    labels) per horizon, compute Mann-Whitney U AUC

Emits alpha_train_summary.json with final train loss + per-horizon
val AUC for downstream gate consumption.

Adds PerceptionTrainer::last_probs() — slow-path readback of the
most recent forward's probs. Used by the eval loop to capture
per-position predictions for AUC.

Args: --mbp10-data-dir --predecoded-dir --out --epochs --n-hid
--seq-len --lr --n-train-seqs --n-val-seqs --seed (all with sane
defaults from spec Section 4 Phase A).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 22:48:43 +02:00
jgrusewski
8d7360aac3 feat(ml-alpha): per-horizon AUC via Mann-Whitney U
Slow-path CPU scalar AUC over (probs, labels) vectors. Mann-Whitney U
formulation with average-rank tie handling. NaN labels filtered
(mirrors generate_labels masking semantics).

Tests (6/6 lib unit):
  - perfect separation -> 1.0
  - perfect anti-separation -> 0.0
  - random uniform pairs -> ~0.5 (within 0.05)
  - NaN labels filtered out before computation
  - empty class -> 0.5 (defensive fallback)
  - all-tied scores -> 0.5 (average-rank correctness)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 22:46:54 +02:00
jgrusewski
e01e66ac23 docs(ml-alpha): update PerceptionTrainer doc — drop 'Phase A' phrasing
Per user feedback no-phase-naming. Documents the validated-end-to-end
state (loss 0.5669 -> 0.0665 in 200 steps) and the init-sensitivity
caveat for tiny smoke models.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 22:44:30 +02:00
jgrusewski
1305d6531b feat(ml-alpha): PerceptionTrainer end-to-end PASS on synthetic overfit
Resolves Task 13 — the synthetic-overfit divergence I thought was a
wiring bug was actually init-sensitivity on the n_hid=32 toy. With
seed=0x4242 + lr=3e-2 + constant +1 direction + 200 steps + reset_
hidden_state per sample, the trainer converges loss 0.5669 -> 0.0665
(88% drop, well under the 60% gate threshold).

The 200-step weight trajectory (debug_long_horizon_weight_trajectory)
shows monotone descent:
  step 0:   loss=0.6932  hb[0]=0.030  hw[0,0]=-0.124
  step 50:  loss=0.2332  hb[0]=1.164  hw[0,0]= 0.997
  step 100: loss=0.1301  hb[0]=1.599  hw[0,0]= 1.412
  step 190: loss=0.0747  hb[0]=1.999  hw[0,0]= 1.777
Heads weights drive monotonically into the correct sigmoid tail.

The chain is sound:
  - heads_backward finite-diff at 1% relative
  - cfc_step_backward finite-diff at 5% relative
  - BCE forward+backward at 5% relative finite-diff
  - AdamW invariants (zero-grad + wd, descent on g=theta)
  - Graph A capture bit-identical to sequential
  - end-to-end overfit on constant +1 = 88% loss drop in 200 steps

Removed the #[ignore] + the speculative "wiring bug" doc comment.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 22:44:08 +02:00
jgrusewski
1bb878b6a7 wip(ml-alpha): PerceptionTrainer scaffold + rename drops phase_a naming
Per user feedback "no phase naming, give proper naming to files and
functions": renames src/trainer/phase_a.rs -> perception.rs,
src/data/phase_a_loader.rs -> data/loader.rs, and the corresponding
types (PhaseATrainer -> PerceptionTrainer, PhaseALoader ->
MultiHorizonLoader, PhaseAConfig -> MultiHorizonLoaderConfig,
PhaseASequence -> LabeledSequence). Test files renamed in lock-step.

Adds PerceptionTrainer.step() — full end-to-end forward + heads
backward + cfc_step_backward (K=1 truncated BPTT) + 5 AdamW param
groups (W_in, W_rec, b, heads_w, heads_b). reset_hidden_state()
zeros h_old between independent samples.

KNOWN ISSUE — synthetic-overfit smoke (tests/perception_overfit.rs)
does NOT yet show loss shrinkage on the 200-step budget:
  initial_avg=0.6914, final_avg=0.6955 (random-baseline ln(2)=0.693)

The kernels are individually correct (heads_bwd + cfc_bwd finite-diff
at 5% rel, AdamW invariant ‖θ‖ 40->1 in 200 steps). The end-to-end
chain doesn't converge — most likely due to half the CfC cells having
near-1 decay from log-uniform tau init on a zeroed hidden state, so
only the fast cells carry signal. Fix candidates for next session:
  - tau init narrower / per-task tuned for the smoke
  - longer step budget (1000+) with adjusted LR
  - validate end-to-end with explicit print of grad/probs across iters

Task 13 is NOT complete (the gate criterion isn't met). Subsequent
work continues from here.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 22:29:32 +02:00
jgrusewski
753284f2ae feat(ml-alpha): backward kernels — heads + cfc_step (K=1 BPTT)
multi_horizon_heads_backward: sigmoid + linear chain rule. One block,
HIDDEN_DIM=128 threads. Computes grad_w, grad_b, grad_h_in. No
atomicAdd; per-thread accumulation only.

cfc_step_backward: truncated K=1 BPTT through one CfC time step.
Forward pre/decay/tanh recomputed inside the kernel; emits grad_w_in,
grad_w_rec, grad_b, grad_h_old. tau is held frozen (structural
log-uniform init per Hasani 2022; backprop through tau deferred to
Phase A v2 if the gate needs it). Uses dynamic shared memory for the
d_pre relay between threads (size = 2 * n_hid * 4 bytes).

Tests (4/4 on sm_86) validate via on-GPU finite-difference:
  - heads grad_h vs forward(h±eps) → matches at eps=1e-3, rel<=1%
  - heads grad_b vs forward(b±eps) → matches at eps=1e-3, rel<=1%
  - cfc grad_b vs forward(b±eps) → matches at eps=1e-3, rel<=5%
  - cfc grad_h_old vs forward(h_old±eps) → matches at eps=1e-3, rel<=5%

CPU is not the reference (per feedback_no_cpu_test_fallbacks.md). The
kernel is the truth; numerical perturbation validates the analytic
gradient against the kernel's own forward.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 22:20:54 +02:00
jgrusewski
a6f4772617 feat(ml-alpha): Phase A data loader — predecoded MBP-10 -> seq + labels
Reuses ml-features::predecoded::load_or_predecode_mbp10 (no cycle —
ml-features doesn't depend on ml-alpha). Yields seq_len-sized windows
of Mbp10RawInput plus 5-horizon binary labels via
multi_horizon_labels::generate_labels.

Per-snapshot prev_mid / prev_ts_ns / trade_signed_vol come from the
prior snapshot in the source stream (not from the anchor), so the
CfC trunk sees a continuous-time signal across the entire seq.

Labels: NaN at edge positions (no forward window) or tied prices;
BCE kernel masks these (Task 10).

Tests:
  - loader_errors_on_missing_root: passes (1/1 inline)
  - loader_yields_seq_with_valid_labels: --ignored, runs at gate time
    with FOXHUNT_TEST_DATA set

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 22:17:51 +02:00
jgrusewski
3cb6992364 feat(ml-alpha): CUDA Graph A capture for perception forward
Captures snap_feature_assemble -> cfc_step -> heads -> projection into
a single replayable graph. Scalars (dt_s, ts_ns, prev_mid, ...) are
frozen at capture time per cudarc 0.19 semantics; the trunk
re-captures when those change. A follow-up task moves scalars into a
device-resident buffer for cross-step replay stability.

Key learning: cudarc's default event-tracking creates cross-stream
dependencies that begin_capture rejects with
CUDA_ERROR_STREAM_CAPTURE_ISOLATION. Pattern (from crates/ml/.../
fused_training.rs): bracket begin/end_capture with
context.disable_event_tracking() / enable_event_tracking(). Mode
remains CU_STREAM_CAPTURE_MODE_RELAXED. Pre-allocate MappedF32Buffer
staging slots as struct fields (host-malloc during/around capture is
also a trigger).

The captured forward writes h_pong directly (no ping-pong swap inside
the captured region — the swap mutates pointer identity which would
invalidate captured kernel args). Heads and projection both read
h_pong.

Tests (3/3 on sm_86):
  - graph_a_replay_matches_sequential: captured replay output equals
    sequential dispatch on same input at eps<=1e-5 (probs) / 1e-4 (proj)
  - graph_a_replay_is_deterministic: 3 consecutive replays produce
    bit-identical output
  - graph_a_replay_outputs_finite: probs in [0,1], proj finite

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 22:05:25 +02:00
jgrusewski
d472b2ac3a feat(ml-alpha): BCE multi-horizon + AdamW kernels
bce_loss_multi_horizon: fused forward+backward, block tree-reduce (no
atomicAdd), NaN labels masked (drop). Loss = mean over valid; grad =
(p-y)/(p(1-p)) scaled by 1/N_valid.

adamw_step: element-wise AdamW with weight decay; one thread per param.

Tests pass on sm_86:
  BCE (4/4): positive+finite loss, near-zero loss when probs match
    labels, analytic grad matches GPU-computed finite-difference at
    eps=1e-3 / max_relative=5e-2 across 5 perturbation points, NaN
    labels mask grad and contribute zero to loss/N_valid.
  AdamW (4/4): zero-grad moves param only by weight-decay, positive
    grad decreases param, step counter increments, repeated descent
    on grad=theta drives ‖θ‖ from 40 to <1 in 200 steps.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:57:26 +02:00
jgrusewski
1ed81cf6c0 feat(ml-alpha): CfcTrunk forward — sequential perception dispatch
CfcTrunk owns weights, ping-pong hidden buffers, and pre-allocated
per-step scratch (snap features, probs, projection output). Modules
and CudaFunction handles cached at new_random so the hot path
avoids reload. forward_snapshot dispatches snap_feature_assemble ->
cfc_step -> heads -> projection sequentially; Graph A capture (Task 11)
will fold these into a single launch.

The Mamba2 prefix (per 2026-05-16 spec amendment) is added in a
follow-up task before Graph A capture.

Tests (5/5 on sm_86):
  - probs in [0,1] across all 5 horizons
  - hidden state changes after forward
  - probs + proj are finite
  - layer-norm proj has near-zero mean
  - 50-step run leaves hidden finite

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:55:00 +02:00
jgrusewski
9ddde632b2 feat(ml-alpha): projection kernel (128->8 + layer-norm)
Single-block 8-thread kernel; thread j computes its own 128-dim dot
product, then thread 0 computes block-wide mean/var, then each thread
applies the per-output affine layer-norm. No atomicAdd; reductions are
single-thread (8 elements — negligible cost).

Tests (5/5 on sm_86) assert:
  - layer-norm zero-mean output under identity gain
  - layer-norm unit-variance output under identity gain
  - ln_bias shifts mean uniformly
  - ln_gain scales variance (var = gain^2)
  - finite output under zero input (variance clamp activates)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:52:57 +02:00
jgrusewski
d4e46aba94 feat(ml-alpha): multi_horizon_heads kernel (128->5 sigmoid)
Per-horizon P(up) at h ∈ {30, 100, 300, 1000, 6000} snapshots forward.
Single-block 5-thread kernel; each thread is its own 128-dim dot
product + sigmoid. No atomicAdd.

Tests (5/5 pass on sm_86) assert invariants only:
  - sigmoid output ∈ [0, 1] for all heads
  - zero weights + zero bias → 0.5 exactly
  - bias = +20 → saturates near 1
  - bias = -20 → saturates near 0
  - per-head independence (mixed-bias configuration)

Addendum updated to explicitly state no-CPU-mirror discipline per
feedback_no_cpu_test_fallbacks.md.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:51:48 +02:00
jgrusewski
f927469ed3 feat(ml-alpha): cfc_step kernel + invariant-based GPU validation
Hasani 2022 closed-form CfC recurrence; one thread per hidden unit, no
atomicAdd. Tests assert algebraic invariants (dt=0 -> identity, zero
weights -> h_old * decay, large tau -> h_old preserved, output bound).

Also removes src/cfc/oracle.rs and replaces snap_feature bit-equiv
test with property assertions per feedback_no_cpu_test_fallbacks.md.
CPU mirrors are bug-locks; validation is now via known synthetic
inputs + analytical relations on the GPU output.

12 tests pass on local sm_86 (7 snap_feature invariants + 5 cfc_step
invariants).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:50:20 +02:00
jgrusewski
611c82b4ed feat(ml-alpha): snap_feature_assemble kernel + CPU oracle
Per-snapshot 32-dim feature vector (mid log-return, spread, depth, OFI,
trade-flow, dt). Single-block single-thread kernel; uploads via
MappedF32Buffer DtoD into CudaSlice per the addendum Pattern 3.

Bit-equiv tested CPU vs GPU at eps<=1e-5 over (synthetic input,
reserved-slots-are-zero, zero-prev-mid edge case).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:46:58 +02:00
jgrusewski
3389a59281 spec(ml-alpha): stacked Mamba2 -> CfC amendment
Mid-execution architecture revision: Mamba2 stays as a sequence
encoder; CfC becomes the layer on top (replacing the Phase 1d.3 MLP
stacker). Gate becomes 'stacked AUC >= Mamba2-only stacker AUC at
every horizon' — proves the CfC layer is additive, rather than CfC
alone beating Mamba2 alone.

Plan 1 kernels (cfc_step, heads, projection, BCE, AdamW, Graph A)
are unchanged. Only CfcTrunk's forward path gains a Mamba2 prefix
that consumes the snapshot stream and emits a 128-dim h_mamba which
CfC reads. The Mamba2 kernel (mamba2_alpha_kernel.cubin) is already
in the build.

Option B (parallel + fused dual-stream) is documented as the Plan 2
fallback if the stacked gate fails.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:42:57 +02:00
jgrusewski
c098c5dae1 feat(ml-alpha): mapped-pinned ingress slots for snapshot + fill
Typed ABIs (#[repr(C)] SnapshotPayload, FillPayload) backed by
pinned_mem::MappedF32Buffer. write_volatile is the only hot-path
CPU->GPU pathway; GPU reads via device_ptr() with zero HtoD.

Tests pass on local sm_86 (3/3): snapshot round-trip, fill round-trip,
pre-allocation invariant (device pointer stable across writes).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:42:28 +02:00
jgrusewski
1dac3c3fc2 feat(ml-alpha): IsvBus — 32-slot device-resident f32 bus
Holds perception outputs (slots 0..13) on-device; slow-path write/snapshot
go through MappedF32Buffer DtoD per the htod/htoh discipline. Slot
semantics documented in design spec Section 7.

Also deletes examples/alpha_mamba_baseline.rs which Task 1 left orphaned
(used the deleted eval + training modules). Task 17 will rebuild the
Mamba2 baseline trainer path inside gate/cfc_vs_mamba2.rs against the
new Phase A loader.

Tests pass on local sm_86 (3/3): round-trip one slot, 32-slot capacity,
multi-slot independent writes.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:38:54 +02:00
jgrusewski
d3b60b2ef3 build(ml-alpha): multi-cubin build.rs + placeholder kernels + local MappedF32Buffer
Cargo.toml: drops gbdt; adds memmap2 + approx; keeps ml-core only
(cannot depend on ml: would cycle since ml depends on ml-alpha for
the Mamba2 gate baseline).

build.rs: compiles 7 cubins (mamba2_alpha + 6 new placeholders)
with -O3 --use_fast_math --ftz --fmad. Skips kernels whose source
isn't present yet so partial check-ins work. Every env::var paired
with rerun-if-env-changed per the canonical build pearl.

src/pinned_mem.rs: local copy of MappedF32Buffer (mirrors
ml::cuda_pipeline::mapped_pinned::MappedF32Buffer). Drives the only
permitted CPU<->GPU path per feedback_no_htod_htoh_only_mapped_pinned.
Eventually the move-to-ml-core refactor will deduplicate; out of
scope for the Phase A branch.

Addendum: updates the import path to ml_alpha::pinned_mem.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:35:35 +02:00
jgrusewski
2deec0cf91 plan(ml-alpha): Phase A API addendum — cudarc 0.19 canonical patterns
Plan 1 was written against an older cudarc device-centric API. cudarc 0.19
moved alloc/launch ownership to the stream (partly for CUDA Graph capture
hygiene). This addendum pins:

- MlDevice -> CudaContext -> CudaStream construction
- Cubin load + module + function caching
- MappedF32Buffer staging -> DtoD-async -> CudaSlice (canonical CPU->GPU)
- Slow-path readback via DtoD into a staging MappedF32Buffer
- launch_builder(&func).arg(...).launch(cfg) idiom
- IsvBus and MappedPinnedSnapshotSlot/FillSlot using the real
  MappedF32Buffer API (host_slice_mut, read_all, dev_ptr field)
- CUDA Graph A capture via stream.begin_capture / end_capture / instantiate

Plan 1 kernel .cu source, CPU oracles, finite-diff thresholds, smoke
criteria, and gate logic are unchanged. Only Rust binding code uses
these patterns.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:31:53 +02:00
jgrusewski
34739a53a9 refactor(ml-alpha): gut Phase 1a sources for greenfield CfC rebuild
Removes mlp/training/eval/backtest/metrics_detail/calibration and the
old example trainers. Preserves multi_horizon_labels, purged_split,
fxcache_reader (Phase A data path), mamba2_block (gate reference).
Subsequent commits populate cfc/heads/isv/pinned/trainer/data/gate.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 21:05:26 +02:00
jgrusewski
056b4abe52 plan(ml-alpha): three-plan rollout for CfC+PPO greenfield
Plan 1 (Phase A, ~3000 lines): ml-alpha library scaffold, ISV bus,
mapped-pinned slots, CUDA build.rs, six perception kernels with
bit-equiv tests, AdamW + BCE, Graph A capture, Phase A trainer +
binary, CfC-vs-Mamba2 gate. Bite-sized 5-step TDD across 18 tasks.

Plan 2 (Phase B, ~780 lines): build_state, policy_forward (CfC
actor+critic), sample_action, replay buffer, multi-env rollout, GAE,
advantage_normalize, fused PPO loss, EWC, Graphs B+C, seven ISV
controllers, kill-switch, atomic weights swap, walk-forward CV across
6 folds. 19 tasks; tasks 2+ use compressed Step 2-5 TDD cycle pending
re-detail at the Plan 1 gate boundary.

Plan 3 (live, ~560 lines): IBKR adapter audit, AlphaPpoStrategy in
trading_agent_service, cold-start 4-state FSM, disconnect/failure
handling, observability, alpha-control IPC + fxt CLI, restart
semantics, paper trading harness (5 days), $1k live deploy with
manual ack gate, 5-day live window. 10 tasks.

Each plan has gate-pass criteria gating the next. On failure: post-
mortem + spec delta, no advance.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 20:48:14 +02:00
jgrusewski
90d14aaba5 spec(ml-alpha): GPU/CPU contract, CUDA Graphs, cold-start, disconnect handling
Critical-review pass on the CfC+PPO design adds:
- Public API: mapped-pinned ingress slots, single DtoH per decision
- Section 5.5: CUDA Graph capture topology (perception / policy / training),
  kernel inventory, cuBLAS Lt epilogue fusion, persistent ring layouts,
  determinism mode, build-time cubin compilation, perf budget
- Section 5.6: Databento/IBKR disconnect + failure response, never-do list
- Section 6 cold-start: 4-state bootstrap, Phase B walk-forward stats as
  pre-deployment kill-switch baseline, always-live ISV controllers

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 20:27:25 +02:00
jgrusewski
d4ea54d780 spec(ml-alpha): CfC + PPO greenfield design
Approved design document for the ml-alpha rebuild. Seven sections plus
appendices, all 7 sections + ISV-driven auto-tuning addition approved
via the brainstorming-skill flow.

Key architectural decisions:
- Full closed-loop scope: training + offline inference + live IBKR
- Three components: ml-alpha library, alpha_train binary, AlphaPpoStrategy
  in trading_agent_service (reuses existing data_acquisition,
  trading_service, broker_gateway, and the 1177-LOC IBKR adapter that
  already exists in trading_engine)
- Snapshot-level CfC perception trunk (Closed-form Continuous-time, the
  trainable LTC variant from Hasani 2022) — chosen over Mamba2 for
  native multi-time-scale via per-cell learnable τ; hard validation
  gate requires CfC AUC ≥ Mamba2 baseline before continuing
- 5 multi-horizon heads at {30, 100, 300, 1000, 6000} snapshots forward
- Decision-stride 50 snapshots (~1 min); empirically validated this
  session (per-bar→stride=200 flipped 3-fold mean Sharpe from -4.29 to
  +1.78 at quarter-tick cost)
- Action: target_position categorical ∈ {-10, ..., +10}; max_train=10
  fixed at architecture level, max_live ≤ 10 runtime-cappable
- Reward in price units (position-size invariant): dense per-segment
  PnL − C_trade·|Δcontracts| − C_vol·vol_excess; C_trade=0.034 fixed
  (IBKR $1.70 RT ÷ $50/point), C_vol=0 default
- Full online learning with EWC anchoring + 80/20 offline/live replay
  buffer + kill-switch on σ-band metric deviations (loss, mean reward,
  hit-rate, KL, entropy)
- All hyperparameters that vary during training are ISV-driven
  (controller outputs, not magic numbers) per
  pearl_controller_anchors_isv_driven.md
- Fully GPU-resident on hot path per feedback_cpu_is_read_only.md
- $35k starting capital; conservative max_live ramp 1→2→3 contracts
  with manual config bumps gated on realized track record
- Documented IBKR-specific quirks (PDT, mid-session margin spikes,
  rollover, halts, rate limits) with concrete design responses

Supersedes the abandoned 2026-05-16-alpha-ppo-trainer.md (DQN-port plan)
which inherited the wrong architectural shape.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 19:59:51 +02:00
jgrusewski
10f4bcc15b feat(alpha): decision-stride + cluster 9-fold CV workflow
Two complementary additions to validate the minute-horizon alpha
hypothesis at IBKR-realistic costs:

1. `alpha_baseline --decision-stride N`: emits a new action every N
   steps; between decisions force action=0 (wait) so an open position
   is held rather than re-decided per bar. Cuts per-bar trade counts
   ~stride× and removes the coin-flip overtrading. Local 2Q sweep
   showed stride=200 + scaled training (8K episodes × 25 envs × H=1200)
   flipped Sharpe at ¼-tick from -4.29 (per-bar, 3-fold mean) to +1.78,
   with std collapsing from ±8.8 to ±1.15. Break-even cost moved from
   <¼-tick to ~1-tick — for the first time positive at IBKR-realistic
   passive-execution frictions.

2. `alpha_train_stacker --max-rows N`: optional cap on bars consumed
   from the fxcache. Used during local 2Q smoke (--max-rows 4M against
   the 17.8M-row 9Q fxcache) to fit Mamba2 training on a 4 GB consumer
   GPU; on the cluster (--no-cap) it sees all 9Q.

3. New Argo workflow `alpha-cv`: standalone template that compiles
   alpha_train_stacker + alpha_baseline + alpha_fill_coeffs.json,
   trains the stacker on the 9Q fxcache, then runs 9 sequential
   walk-forward folds of alpha_baseline on disjoint 1.9M-bar windows
   (one per quarter). Launcher script `scripts/argo-alpha-cv.sh`
   mirrors argo-train.sh conventions.

The local 2Q test that motivated this commit is summarised inline in
the alpha-cv template comments; the verdict was "framing was the bug —
once decision cadence matches the multi-minute alpha horizon, the
strategy is positive at IBKR commission".

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 18:04:34 +02:00
jgrusewski
737c2c1305 infra(argo): pin all workflow nodeSelectors to topology.kubernetes.io/zone=fr-par-2
Scaleway BSSD PVCs (cargo-target-*, sccache-*, feature-cache-pvc,
bin-cache, training-data-pvc, test-data-pvc, platform service PVCs) are
single-zone-locked at create time. The cluster's node pools in main.tf
set `region` but no `zone`, so each autoscale event picks a zone
arbitrarily. When the autoscaler spins up a node in a zone that doesn't
match an existing PVC, scheduling fails with
  "1 node(s) didn't match PersistentVolume's node affinity"
until the autoscaler eventually retries in the right zone. We hit this
on the HM pool creation (fixed manually with zone=fr-par-2) and again
on this commit's ensure-binary autoscale.

Track 1 (this commit): add `topology.kubernetes.io/zone: fr-par-2` to
every k8s.scaleway.com/pool-name nodeSelector entry across the 11 argo
workflow templates (34 entries total). The Kubernetes scheduler AND
cluster-autoscaler both honor topology keys when deciding placement /
provisioning — so future autoscale events will only spin up fr-par-2
nodes, and PVC binding is guaranteed.

Track 2 (future, structural): add `zone = "${var.region}-2"` to each
scaleway_k8s_pool in infra/modules/kapsule/main.tf so the pools never
provision in any other zone. Requires terraform-state cleanup (the
GitLab http backend currently has no states; the HM pool was created
out-of-band) and drain/recreate of any existing mixed-zone nodes —
deferred.

Verification: pool=N / paired=N coverage report shows 1:1 pool-name
to topology.kubernetes.io/zone entries in every file. kubectl apply -f
of all 11 templates returned "configured" for each.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-16 15:08:37 +02:00