Commit Graph

5446 Commits

Author SHA1 Message Date
jgrusewski
30db01ccc8 perf(loader): parallel per-file load via rayon par_iter (~4-8x speedup)
Each file's load_or_predecode + label generation is pure CPU work over
disjoint inputs (snapshots, cfg.horizons, cfg.outcome_label_cost). The
sequential for loop was the parallel-friendly bottleneck — converting
to rayon::par_iter gives ~4-8x speedup on typical 4-8 core hosts.

Combined with SPEED-C's ~400x speedup on the inner Welford loop, total
preload throughput is ~1600-3200x faster than the prior single-threaded
O(W) recompute path.

File order is preserved by par_iter's collect contract. Too-few-snapshots
skips emit a warn during load and resolve to None at collection.
2026-05-22 21:13:37 +02:00
jgrusewski
955613d02d perf(ml-alpha): online Welford in generate_outcome_labels_ab (~400x speedup)
Replaces O(W=1000) per-step mean+var recompute with f64 running
sum_x + sum_x2 updated incrementally on window push/pop. Per-snapshot
cost drops from ~2000 to ~5 float ops. On a 5M-snapshot file across
3 horizons, total hot-loop ops drop from ~30B to ~75M.

f64 accumulators contain ~16 decimal digits - over a 5M-step file
the accumulated rounding error stays well below the f32 output
precision. Every RECOMPUTE_PERIOD=10000 pops, we still do a full
window sweep to reset the accumulators as defense in depth against
pathological drift.

New parity test online_sigma_matches_naive_full_window_recompute_within_tolerance
asserts the fast path matches the naive O(W) algorithm within 1e-4
relative on a 30k-snapshot synthetic stream.

Per pearl_cooperative_staging_eliminates_redundant_reads (CPU analog):
running sums eliminate the redundant window reads that dominated
preload time.
2026-05-22 21:09:16 +02:00
jgrusewski
458d678e9f docs(plans): multi-resolution input architecture migration plan
Greenfield Phase 1 plan addressing temporal receptive field mismatch
(auc_h1000=0.576 plateau): replace seq_len=32 raw-tick input with
3-scale aggregation [(1,10), (30,10), (100,12)] covering 1510 ticks.

10 tasks total, executed via subagent-driven development. Falsifiable
gate: auc_h1000 >= 0.65 on clean front-month data.
2026-05-22 20:48:05 +02:00
jgrusewski
a06abf60df test(ml-alpha): synthetic multi-resolution pipeline test
Four pure-CPU tests confirming aggregate_window emits the right
ts_ns - prev_ts_ns span per scale, so the existing snap-feature Δt
Fourier encoder gets correctly-scaled inputs without any CUDA kernel
changes.
2026-05-22 20:46:55 +02:00
jgrusewski
b1bfae2367 feat(argo): replace --seq-len with --multi-resolution in alpha-perception
Default '1:10,30:10,100:12' (32 positions, 1510-tick context).
Greenfield migration — no legacy seq-len fallback. Operators
override per-run via './scripts/argo-alpha-perception.sh --multi-resolution 1:32'
for the parity-check config.
2026-05-22 20:45:21 +02:00
jgrusewski
7e438aeba0 feat(alpha_train): --multi-resolution CLI flag
Replaces --seq-len. Default '1:10,30:10,100:12' = 32 positions
covering 1510 ticks of context, the Phase 1 fix for temporal
receptive field mismatch. Logs the active config at preload start
so Argo logs surface the per-run choice.
2026-05-22 20:42:40 +02:00
jgrusewski
8d72c14a8b fix(ml): migrate test uploads from clone_htod to mapped-pinned
Per feedback_no_htod_htoh_only_mapped_pinned: mapped-pinned only for
CPU↔GPU; tests not exempt. Previous commit ab64c0412 was a shallow
deprecated-API swap (memcpy_stod -> clone_htod) — both still HTOD
copies. This commit replaces all 5 uploads and the 1 readback in
test_eval_action_select_thompson_picks_proportionally with the
production mapped-pinned + memcpy_dtod_async pattern (mirrors lines
433-448 of the same file).

Closure 'upload_pinned' DRYs the 5 upload sites; readback uses
MappedI32Buffer + DtoD into staging.host_slice_mut().to_vec().
Zero HTOD/HTOH copies remain in this file. Documented in
dqn-wire-up-audit.md.
2026-05-22 20:39:32 +02:00
jgrusewski
ab64c0412b chore(ml): migrate deprecated cudarc memcpy_* calls
cudarc 0.19 deprecated memcpy_stod (use clone_htod) and memcpy_dtov
(use clone_dtoh). Six call sites in cuda_pipeline/mod.rs migrated.
Pre-existing warnings surfaced now because the recent file-level
allow(unsafe_code) on this file no longer per-line-suppresses the
deprecated lint.

Per feedback_no_legacy_aliases: rename call sites directly, no
deprecated wrappers. Documented in dqn-wire-up-audit.md.
2026-05-22 20:34:59 +02:00
jgrusewski
7abfd3b0b6 chore(ml-alpha-tests): migrate test fixtures to MultiResolutionConfig
multi_horizon_loader.rs constructs MultiHorizonLoaderConfig with
default_three_scale() (matching alpha_train's production default
and yielding total_positions()=32). The cfg.seq_len=32 override
in loader_stride_4_yields_correct_spacing is now a no-op since
the constructor already provides 32, so it's removed with a comment.

perception_overfit.rs was untouched — it doesn't construct
MultiHorizonLoaderConfig, only PerceptionTrainerConfig (whose own
seq_len field is unrelated to the loader migration).
2026-05-22 20:31:44 +02:00
jgrusewski
99982cc8fb chore(ml-backtesting): migrate to MultiResolutionConfig
Backtest harness + tests use default_three_scale config (1:10,30:10,
100:12 = 1510 ticks of context). Replaces the previous seq_len=32
single-scale path which is now deleted.
2026-05-22 20:28:24 +02:00
jgrusewski
dcbc51f432 fix(loader): anchor sampler enforces lookback lower bound
Multi-resolution sequence builder reads from
[anchor + total - lookback, anchor + total), so anchor must be in
[max(0, lookback - total), snapshots.len() - (total + max_horizon)).
The previous gen_range(0..max_anchor) underflowed for default
3-scale (lookback=1510, total=32) at small anchors. Compute
min_anchor = lookback.saturating_sub(total) and sample
gen_range(min_anchor..max_anchor) with the ensure! upgraded to
check max_anchor > min_anchor.

Caught by implementer self-review of SDD-MR Task 3.
2026-05-22 20:22:16 +02:00
jgrusewski
3b1ed72eaa feat(ml-alpha): replace seq_len with MultiResolutionConfig in loader
Sequence builder now walks fine→coarse scales from the anchor's
forward edge, aggregating each window into one pseudo-snapshot
via aggregate_window. seq_len field deleted (total_positions()
replaces all reads). Greenfield — no legacy single-scale path.
External callers updated in subsequent tasks.
2026-05-22 20:20:11 +02:00
jgrusewski
cc40780b80 chore(ml): file-level allow(unsafe_code) on 12 CUDA-launch files
CUDA kernel launches via cudarc::launch_builder and MappedF32::new
(cuMemHostAlloc DEVICEMAP FFI) inherently require unsafe blocks. The
workspace-wide '-W unsafe-code' lint produced ~80 warnings across these
files, all structurally identical. Match the established pattern from
ml-backtesting/src/harness.rs and ml-backtesting/src/sim/mod.rs: single
file-level allow with a comment explaining the rationale.

Files: alpha_dqn_h600_smoke, alpha_baseline, cublaslt_debug,
gpu_walk_forward, cuda_pipeline/mod, hyperopt adapters (mamba2, ppo),
DQN smoke_tests (helpers, td_propagation), trainers/ppo, and two
ml/tests files. Documented in dqn-wire-up-audit.md.
2026-05-22 20:13:09 +02:00
jgrusewski
326fa526ce feat(ml-alpha): aggregate_window helper
Builds one Mbp10RawInput from a window of consecutive snapshots.
Book levels meaned, flows summed, ts_ns spans the window so the
snap-feature Δt Fourier encoder gets correct dt without any CUDA
kernel changes.
2026-05-22 20:06:51 +02:00
jgrusewski
bd60aa6afe feat(ml-alpha): MultiResolutionConfig for multi-scale input aggregation
Adds a config struct + CLI grammar for stacking multiple time-scale
aggregations into the input window. default_three_scale() returns
(1,10) + (30,10) + (100,12) = 32 positions covering 1510 ticks of
context. Greenfield design — no legacy single-scale default.
2026-05-22 19:59:53 +02:00
jgrusewski
20aa345a7c fix(loader): soft-validate FrontMonth symbol when no streaming SymbolMapping
Databento historical bulk DBN files emit symbol mappings only in the metadata
header (date-range form), not as in-stream SymbolMappingMsg records. The
strict 'no mapping = error' branch from the previous commit blocked the
front-month smoke (alpha-perception-vstrr) at Q1 2024 where the dominant id
5602 has no streaming mapping entry.

Volume-leader detection itself doesn't depend on the symbol; the regex check
was defense-in-depth. Log-warn on symbol-mismatch and proceed with id=metadata-only
when no streaming mapping exists. Calendar-spread anomalies still surface in
logs without blocking training.
2026-05-22 18:08:36 +02:00
jgrusewski
78a9e08358 feat(loader): InstrumentFilter::FrontMonth for cross-quarter ES.FUT data
Replaces Option<u32> instrument_id_filter with InstrumentFilter enum {All,
Id(u32), FrontMonth}. FrontMonth runs a two-pass detect over the DBN
stream: pass 1 counts instrument_ids and collects SymbolMapping records,
picks the dominant id, validates it resolves to an ES contract via regex
ES[FGHJKMNQUVXZ]\d{1,2}; pass 2 streams the filtered records.

Motivated by alpha-perception-k54wd: a single-id filter on parent-symbol
ES.FUT data caught Q1 2024 (kept=73M) but kept=0 for Q2-Q9 because ES
front-month rolls quarterly (ESH4 -> ESM4 -> ESU4 -> ESZ4 ...). FrontMonth
self-tunes across the rolls without needing a per-file id table.

Sidecar keys distinguish modes: mbp10 / mbp10_instr<id> / mbp10_front_month.
CLI flag renamed --instrument-id -> --instrument-mode {all,id=N,front-month}
with matching parameter rename in argo-alpha-perception.sh + template.
2026-05-22 17:38:41 +02:00
jgrusewski
11c658d3fb feat(argo): plumb --instrument-id flag through alpha-perception script + template
Adds new workflow parameter instrument-id (default empty string = no filter).
Script forwards via --instrument-id CLI flag to alpha_train when non-empty.
EXTRA_FLAGS template logic appends --instrument-id only when set.

kubectl apply must run first (per feedback_argo_template_must_apply) so the
cluster CRD reflects the new parameter before argo submit.
2026-05-22 15:57:47 +02:00
jgrusewski
783297e002 feat(loader): instrument_id filter + outdated test fix + sp18 fingerprint 2026-05-22 15:56:31 +02:00
jgrusewski
1764cc394b revert(aux): F2 mid_price_f32 NaN-on-one-sided was a wrong hypothesis
Step 2 of aux-diagnosis-deeper plan (NumPy on local ES data) falsified
the F2 hypothesis:

1. Local data has 0% zero-bid/zero-ask records. F2's "one-sided book
   contamination" was incorrect — the actual databento sentinel for
   missing price is INT64_MAX × 1e-9 ≈ 9.22e9 (a HUGE POSITIVE number
   that passes the `> 0` check). F2 was a no-op on real data.

2. Sentinel rate on local Q1 is 0.006% — negligible.

3. Local pos_fraction at K=10 is 19.59%, cluster reports 35.07%. The
   ~75% gap is plausibly explained by overnight session gaps in the
   full 5M-record quarter file (which I didn't see in my 500k local
   sample). These are real price discontinuities, not "contamination".

4. F2 introduced an epoch-4 NaN explosion (alpha-perception-7shgw)
   because NaN cascaded through the σ_K Welford rolling computation.

Reverting:
- crates/ml-alpha/src/data/loader.rs::mid_price_f32 → blind average
- crates/ml-alpha/src/multi_horizon_labels.rs:218 → is_finite only

KEEPING:
- F1 dir_acc fix in perception.rs:3647 — empirically working (metric
  hovers ~0.5 instead of pinned sub-chance)

cargo check --workspace --all-targets clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 14:56:50 +02:00
jgrusewski
85d3ca5c72 fix(aux): three bugs causing systematic anti-correlation (F1+F2)
F-series investigations triangulated TWO real bugs that together
explained: aux BCE > ln(2) chance baseline, sub-chance dir_acc with
balanced labels, and bit-identical training across vastly different
POS_WEIGHT_MAX values.

BUG #1 (F2) — One-sided book contamination in mid_price_f32:
  - crates/ml-alpha/src/data/loader.rs::mid_price_f32 blindly averaged
    bid_px[0] + ask_px[0] without checking validity. MBP-10 snapshots
    at session boundaries / halts / stale level-0 vacancies have
    bid_px=0 or ask_px=0; result was mid = real_price/2 (~2750 for ES)
    or mid = 0 (both sides empty).
  - generate_outcome_labels_ab:218-220 only guarded is_finite() (NOT > 0).
    Sister fn generate_labels:75 does check both — asymmetry between
    parallel generators.
  - Transitions like mid=0 → mid=5500 produced ΔP=5500, instantly
    satisfying delta > 2×cost → spuriously triggering y_prof=1. Each
    contaminated snapshot tainted up to 2K downstream labels (appears
    as p_t for K positions and as p_kt for K positions).
  - With ~10-20% one-sided books in real ES, ~35-45% of K=10 labels
    spuriously positive — exactly matching the observed pos_fraction
    (0.35, 0.39, 0.46 for long).

  Fix (broader): mid_price_f32 returns NaN for one-sided/empty books
  at the source. Cascades to ALL downstream consumers (regime features,
  snap_features encoder, labels). Defensive guard also added in
  multi_horizon_labels.rs:218 for direct-test callers bypassing the
  loader.

BUG #2 (F1+F3) — dir_acc metric structural bias:
  - perception.rs:3647 match condition for flat-true bucket required
    float-EXACT equality on raw logits (`pred_diff == 0.0`) — essentially
    unreachable for continuous-valued logits.
  - On real ES, ~30-50% of samples are flat (neither direction
    profitable at 2×cost threshold). ALL counted as misses → random
    baseline depressed to ~0.35 (matching observed 0.32-0.45). BCE
    can DROP while dir_acc DROPS — decoupled metrics.
  - aux_lift_dir_acc_threshold=0.85 was structurally UNREACHABLE under
    this metric regardless of model quality.

  Fix: skip flat-true samples (`if true_diff == 0 { continue; }`).
  Converts dir_acc into "given a directional outcome, did we get the
  direction right?" with proper random baseline 0.5.

F3 + F4 confirmed: aux head wiring (8 head Adam optimizers, fwd/bwd
kernel arg order, BCE+sigmoid gradient sign, reduce_axis0 reduction)
is correct. The synthetic perception_overfit test stays in chance
regime because it constructs constant prof_long=1, prof_short=0 →
no zero-priced snapshots, no flat-true bucket. Production-data path
divergence is the canonical pattern in
pearl_canary_input_freshness_launch_order.

cargo check --workspace --all-targets: clean.
cargo test -p ml-alpha --lib multi_horizon_labels: 15 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 13:19:42 +02:00
jgrusewski
f065cfbaa2 diag(aux): log pos_fraction once at epoch=0, step=0 (diagnose pw bit-identity) 2026-05-22 12:39:38 +02:00
jgrusewski
24289f40a5 tune(aux-bce): lower POS_WEIGHT_MAX 50→10 (gentler class-weight clamp)
A+B Smoke 1 (alpha-perception-79rbn) showed pos_weight=50 caused
overshooting: BCE=0.83 (worse than chance ln(2)=0.69) AND sub-chance
dir_acc (0.28-0.45) at all horizons. The 50× cap pushed the model
toward the rare positive class hard enough to blow up loss on 99.99%
of negative samples.

Lower MAX clamp to 10× lets per-horizon imbalance still scale the loss
but caps the runaway gradient on extreme-rare positive classes (like
K=100's 0.01% positive rate where pos_weight would otherwise be ~9999).

Synthetic test still passes (clamp doesn't matter on constant signal
where pos_fraction=1.0).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 12:19:31 +02:00
jgrusewski
0f5d5c7b4a feat(aux-supervision): wire BCE + conditional-Huber actual calls (CB5)
CB3+CB4 shipped the kernels with a holding-pattern (grad-zero) call site.
CB5 wires the real calls + per-loss ISV signals.

perception.rs:
- step_batched body: pos_weight computed host-side from
  self.last_pos_fraction (n_neg/n_pos clamped to [1.0, 50.0] per E3),
  uploaded mapped-pinned to stg_aux_pos_weight_{long,short}
- 4 kernel calls per step (slab mode, K*B*N_AUX_HORIZONS):
  * aux_bce_loss_gpu × 2 (prof_long, prof_short) — class-weighted
  * aux_huber_masked_loss_gpu × 2 (size_long, size_short) — NaN-mask
    from CB1's y_size=NaN at y_prof=0 gives conditional-Huber for free
- Per-loss EMAs replace single aux_huber_ema:
  * aux_prof_bce_ema_per_h (BCE EMA per horizon)
  * aux_size_huber_ema_per_h (Huber EMA per horizon)
  * aux_dir_acc_ema_per_h (unchanged)
- Stop-grad lift condition: aux_prof_bce_ema < 0.4 AND aux_dir_acc > 0.85
  for ALL horizons (uses BCE not Huber per E3 — size Huber is
  observability-only since the regression scale varies more than the
  binary classification quality)
- aux_lift_huber_threshold renamed to aux_lift_prof_bce_threshold

alpha_train.rs:
- AlphaTrainSummary: final_aux_huber_ema_per_h split into
  final_aux_prof_bce_ema_per_h + final_aux_size_huber_ema_per_h
- Per-epoch tracing: aux_prof_bce_h{100,300,1000} + aux_size_huber_h{...}
  + aux_dir_acc_h{...} + stop_grad_aux_to_encoder

perception_overfit.rs: synthetic test asserts BCE finite + below ln(2)
chance baseline, size Huber finite, dir_acc >= 0.5.

Synthetic test on RTX 3050:
  aux_prof_bce_ema    = [0.0142, 0.0142, 0.0142]  (30× below threshold)
  aux_size_huber_ema  = [0.1213, 0.1213, 0.1213]  (small)
  aux_dir_acc_ema     = [1.0, 1.0, 1.0]            (perfect)
  stop_grad lifted    = false                      (lift fired)

Known limitation (follow-up CB6): both kernels return single joint scalar
over the [K × B × N_AUX_HORIZONS] slab — all per-horizon EMA entries
carry the broadcast joint mean. Per-h gating would require kernel split.

cargo check --workspace --all-targets clean.
cargo test -p ml-alpha --lib: 43 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 11:32:19 +02:00
jgrusewski
842e90aeb0 feat(aux-heads): rewrite for A+B paired (BCE + conditional-Huber) (CB3+CB4)
CB1+CB2 swapped labels D→A+B; this swaps the kernels to match.

aux_heads.cu — 4-output structure:
- Forward: 12 outputs per snapshot = 4 per direction × N_HORIZONS
  (prof_long_logit, size_long_pred, prof_short_logit, size_short_pred,
   each [N_HORIZONS]). Linear projections; sigmoid applied in BCE kernel.
- Backward: accepts 4 grad_y inputs, produces 8 grad_W + 8 grad_b +
  grad_h_aux. Cooperative h_aux staging in shmem once per block.
- 8 weight matrices total, Xavier × 0.1 init under scoped_init_seed.

aux_loss.cu — 2 kernels:
- aux_bce_loss_fwd_bwd: class-weighted BCE+sigmoid fused. pos_weight in
  shared mem; scales positive-class gradient. NaN-mask y_true.
- aux_huber_masked_fwd_bwd: Huber w/ NaN-mask. CB1's y_size=NaN at
  y_prof=0 provides the conditional-Huber semantics naturally — no
  separate mask buffer needed.

aux_heads.rs:
- AuxHeads + AuxHeadsWeights: 8 buffer fields (4 W + 4 b)
- AuxBceLoss + AuxMaskedHuberLoss wrappers replace AuxHuberLoss
- POS_WEIGHT_MIN/MAX = [1.0, 50.0] clamps per E3
- aux_heads_fwd_gpu/aux_heads_bwd_gpu/aux_bce_loss_gpu/aux_huber_masked_loss_gpu

perception.rs (minimal compile-keeping signature updates only):
- Renamed/added buffers: 4 prediction (prof/size × long/short),
  4 label staging, 4 grad_y per-K, 8 head grad scratches, 8 head Adam
  optimizers, 2 pos_weight buffers (device + staging)
- HOLDING PATTERN: bwd zeroes the 4 grad_y_per_K buffers each step so
  the head Adam updates are no-ops on grad=0 (no aux gradient signal
  this commit). CB5 wires the actual aux_bce + aux_huber_masked calls.
- BCE direction signal + dir_acc readouts updated to use the new
  prof_long/prof_short prediction buffers (so existing perception_overfit
  aux test still passes).

7 GPU oracle tests on RTX 3050 sm_86, all pass in 2.43s:
- fwd_matches_naive_reference, bwd_finite_diff_matches_bias_sample,
  aux_bce_loss_matches_naive_reference, aux_bce_pos_weight_scales_positive_gradient
  (verified: pos_weight=10 → 10× gradient ratio within 1e-4),
  aux_huber_masked_does_not_propagate, aux_huber_masked_covers_both_branches,
  aux_bce_nan_mask_does_not_propagate

Cubins rebuilt: aux_heads (8736→10528 bytes, +20% for 4-head fwd/bwd),
aux_loss (6944→13344 bytes, +92% for 2 kernels).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 11:16:29 +02:00
jgrusewski
9ed882e740 feat(aux-labels): replace D with A+B paired labels + loader/trainer migration (CB1+CB2)
Smoke 1 v2 empirically falsified the D-style labels: 99.99% of K=100 D-labels
were negative on real ES MBP-10 (cost+1.5×MaxDD dominates every typical
100-tick move). Aux head couldn't escape predicting the mean.

CB1 — replace label generator:
- generate_outcome_labels_d → generate_outcome_labels_ab returning
  OutcomeLabelsAB { y_prof_{long,short}, y_size_{long,short}, sigma_k,
  cost_price_units, pos_fraction }
- y_prof = 1 iff signed_pnl > 2×cost (binary, class-weighted BCE target)
- y_size = signed_pnl / σ_K (σ-normalized regression target,
  conditional-masked when y_prof=0)
- σ_K: rolling 1000-bar Welford std of K-step log-returns, floored at
  cost/4 per pearl_trade_level_vol_for_stop_distance
- pos_fraction: per-(direction, horizon) positive class fraction for
  downstream BCE pos_weight balancing
- 10 unit tests validating sign-correctness, NaN edges, balance,
  cost-threshold, sigma-floor, per-horizon independence, error paths

CB2 — atomic caller migration:
- LabeledSequence + LoadedFile: outcome_long/short (2 arrays) → 5 arrays
  (prof_long, prof_short, size_long, size_short, sigma_k) + pos_fraction
- Loader splits new generator's outputs + propagates pos_fraction
  file-level into each yielded LabeledSequence
- step / step_batched signatures widened to 7 params + pos_fraction
- alpha_train.rs: 4 separate batches + per-snapshot row build for each
- last_pos_fraction stashed on PerceptionTrainer
- aux test fixture (synthetic_aux_outcomes) updated to emit 4 arrays +
  pos_fraction matching the constant-up-ramp test signal
- HOLDING PATTERN: step_batched body validates new arrays + stages
  prof-binary through the existing D-era Huber kernel so training
  produces a real gradient. CB3 rewrites the kernel; CB4 widens head
  outputs; CB5 wires BCE+Huber proper loss with class weighting.

cargo check --workspace --all-targets: clean (only pre-existing cudarc
cupti + ml insert_batch unrelated errors).
cargo test -p ml-alpha --lib: 43 passed (15 multi_horizon_labels).
cargo test -p ml-backtesting --lib: 33 passed.
stacked_trainer_aux_supervision RTX 3050: pass (huber=0.0004, dir_acc=1.0,
stop_grad lifted).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 10:52:20 +02:00
jgrusewski
2e06297671 feat(aux-supervision): per-epoch aux observability in alpha_train (B6.5)
Smoke 1 at HEAD 21e7dfd63 showed BCE bit-identical to baseline
(asymmetric stop-grad isolation working) but zero aux signal in logs.
Adds:

- Per-epoch tracing 'aux snapshot' line with aux_huber_h{10,100,1000},
  aux_dir_acc_h{10,100,1000}, stop_grad_aux_to_encoder
- AlphaTrainSummary JSON fields: final_aux_huber_ema_per_h,
  final_aux_dir_acc_ema_per_h, final_stop_grad_aux_to_encoder

Reads PerceptionTrainer's pub fields directly (no accessor noise).

After re-running Smoke 1 we'll have empirical evidence whether aux is
learning on REAL ES MBP-10 data (vs the synthetic constant-direction
test where dir_acc trivially hits 1.0). This informs B7's gate threshold
design.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 10:12:55 +02:00
jgrusewski
21e7dfd63c feat(aux-supervision): wire AuxTrunk + AuxHeads + Huber loss into PerceptionTrainer (B5)
Wires the aux supervision path parallel to BCE:
- AuxTrunk (64-hidden single-bucket CfC) consumes the same encoder output
  as the main BCE trunk
- AuxHeads (linear regression long/short) maps aux_trunk output to
  per-(direction, horizon) predicted outcomes
- AuxHuberLoss supervises against D-style labels from MultiHorizonLoader

Backward path with asymmetric stop-grad at encoder boundary:
- aux_trunk gets gradient signal into its OWN params at all times
- aux_trunk's encoder-boundary gradient is INITIALLY blocked
  (stop_grad_aux_to_encoder = true)
- Conditional lift per E3 design: if aux_huber_ema < 0.4 AND
  aux_dir_acc_ema > 0.85 within 200 steps, lift the stop-grad
- When lifted, aux_vec_add kernel folds aux's grad_x into the main
  grad_h_enriched_seq slot (element-wise += per feedback_no_atomicadd)

ISV signals added: aux_huber_per_h, aux_dir_acc_per_h (per pearl).

Per-trunk scratch + reduced grad buffers (no Adam state sharing per
pearl_adam_normalizes_loss_weights — opt_aux is its own Adam group).

New helper kernel cuda/aux_vec_add.cu: position-local dst += src for
the asymmetric stop-grad lift accumulation.

New synthetic test stacked_trainer_aux_supervision_converges_on_constant_signal
validates end-to-end:
  aux_huber_ema_per_h    = [0.087, 0.087, 0.087]  (converged)
  aux_dir_acc_ema_per_h  = [1.0, 1.0, 1.0]        (perfect on constant)
  stop_grad_aux_to_encoder = false                (lift fired)

All 5 stacked_trainer tests pass on RTX 3050 (lib still converges, no
regression from parallel aux wiring).

Not yet consumed by decision policy (B7) — aux output flows through
training only.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 09:45:29 +02:00
jgrusewski
ff70734802 feat(aux-heads): linear regression heads + Huber loss for aux supervision (B4)
Adds the aux heads that map AuxTrunk's output (64-dim) to per-(direction,
horizon) predicted outcomes, plus the Huber loss kernel that supervises
them against the D-style labels generated in B1.

Files (1212 LOC):
- cuda/aux_heads.cu (197 LOC): fused fwd+bwd for linear projection
  h_aux[B, 64] → y_long_hat[B, N_HORIZONS] + y_short_hat[B, N_HORIZONS].
  Single launch for both directions, per-batch grad scratch + caller-side
  reduce_axis0, cooperative h_aux staging in shmem.
- cuda/aux_loss.cu (143 LOC): Huber loss fwd+bwd with NaN-masking. Per
  direction call; returns Σ Huber + valid_count separately so caller picks
  reduction policy.
- src/aux_heads.rs (412 LOC): AuxHeads + AuxHuberLoss wrappers, weight
  structs, Xavier init under scoped_init_seed.
- tests/aux_heads.rs (460 LOC): 4 #[ignore]'d GPU oracle tests
  (fwd_matches_naive, bwd_finite_diff_matches_bias, huber_loss_matches_
  naive, huber_loss_nan_mask_does_not_propagate). 4/4 PASS on RTX 3050.
- build.rs: KERNELS += aux_heads, aux_loss; cache-bust bumped to v17.
- src/lib.rs: pub mod aux_heads.

Design decisions (full notes in subagent report):
- Linear heads (no GRN) — D-labels already encode asymmetric loss-aversion
- Per-direction Huber launches (cleaner per-direction telemetry)
- NaN-masking in loss kernel (single NaN label can't poison batch grad)
- Unreduced sum + valid_count separately (caller picks mean policy)

Not yet wired into trainer (B5) or used in policy (B7).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 09:11:02 +02:00
jgrusewski
d6a020ad39 feat(aux-trunk): smaller single-bucket CfC for aux supervision (Layer B3)
New 64-hidden single-bucket CfC trunk to serve as the parallel
parameter group for D-style aux supervision (Layer B of anti-cal plan).
Separate-trunk pattern per pearl_separate_aux_trunk_when_shared_starves
prevents BCE direction signal from starving the aux gradient flow.

Architecture:
- AUX_HIDDEN = 64 (vs main trunk 128) — keeps params/compute reasonable
- Single bucket — no per-horizon channel splitting; aux supervision is
  per-K at the head (B4), not at the trunk state
- Independent parameters: own w_in, w_rec, b, tau weights
- Same CfC step math as cfc_step_per_branch, parameterized for single-block

Files:
- cuda/aux_trunk.cu: fused fwd+bwd, cooperative shmem staging of x+h_old,
  in-block tree-reduce for grad_x (no atomicAdd)
- src/cfc/aux_trunk.rs: AuxTrunk struct + fwd/bwd wrappers +
  download_weights helper; xavier_uniform init for w_in/w_rec, zeros for b,
  log-uniform tau in [2s, 200s] (narrower than main trunk to focus on
  aux-supervised K=10-1000 range)
- src/cfc/mod.rs: pub mod aux_trunk + re-exports
- build.rs: KERNELS += "aux_trunk"
- tests/aux_trunk.rs: 3 #[ignore]'d GPU oracle tests
  (fwd_matches_naive_reference, bwd_finite_diff_matches_bias_sample,
  fwd_smoke_large_batch_no_nan_no_oom) — 3/3 pass on RTX 3050 sm_86

Design decisions documented in subagent report:
- Runtime feat_dim parameter (not compile-time #define) for single-cubin
  reuse across raw-snap (40) and post-encoder (128) inputs
- grad_x reduced INSIDE bwd kernel via shmem tree-reduce (caller doesn't
  need separate reduction pass) — matches no-atomicAdd discipline
- grad_h_old carries only direct dh*decay; cross-channel BPTT term left
  for caller (B5) when K>1 unroll is wired

Not yet wired into trainer (B5) or supervised by aux head (B4).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 09:03:00 +02:00
jgrusewski
859d0c738b feat(aux-labels): wire D-style outcome labels into MultiHorizonLoader
Extends MultiHorizonLoaderConfig with `outcome_label_cost` (default
DEFAULT_OUTCOME_LABEL_COST_ES = 0.5 = 2 ticks for ES round-trip).
LabeledSequence + LoadedFile gain outcome_long + outcome_short arrays
of [Vec<f32>; N_HORIZONS] populated by generate_outcome_labels_d during
LoadedFile construction (same !inference_only branch as BCE labels).

CLI exposure: alpha_train.rs gains --outcome-label-cost flag with the
ES default. All 6 MultiHorizonLoaderConfig consumers updated atomically
per feedback_no_partial_refactor: alpha_train (train + val loaders),
in-file test fixtures (×2), multi_horizon_loader.rs (×2), harness.rs,
trainer_parity.rs, ring3_replay.rs.

cargo check --workspace clean; ml-alpha lib tests 41 passed.

B3+ tasks not yet touched: outcome labels are computed and propagated to
LabeledSequence but no consumer reads them yet (aux trunk Tasks B3-B5
will wire that).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 08:53:27 +02:00
jgrusewski
aa2b706346 feat(aux-labels): D-style asymmetric loss-aversion label generator
Adds generate_outcome_labels_d for the future aux supervision head
(Layer B of the anti-calibration plan, docs/superpowers/plans/2026-05-22-
horizon-rebase-n3-100-300-1000-and-aux-d-labels.md).

Per (snapshot t, horizon K, direction d ∈ {long, short}):
  profit = signed_pnl(d) - cost
  dd_against = max adverse excursion in [t, t+K] holding direction d
  y[t, K, d] = profit - 1.5 × |dd_against|

The 1.5× drawdown penalty encodes loss-aversion per behavioral finance —
trades with deep drawdowns are penalized even if final outcome is positive.

Implementation: monotonic-deque sliding-window min/max per horizon,
O(N · N_HORIZONS) amortized. NaN at right-edge positions (t+K >= n).
Input validation rejects zero horizon and non-finite/negative cost.

13 tests pass: 5 hand-derived value checks (simple up, drawdown
penalty, NaN edge, sliding-window correctness, per-horizon independence),
3 additional edge tests (constant-series invariant, zero-horizon
validation, negative-cost validation), plus the 5 pre-existing tests
unchanged.

Not yet wired into the loader (Task B2) or used as supervision target
(Tasks B3-B5). New function only.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 08:47:24 +02:00
jgrusewski
6d772a5d68 chore(sweep): update sweep_smoke_perhoriz_cfc to N=3 checkpoint + D1 anti-cal gate
Point at alpha-perception-9h5xc's trunk_best_h1000.bin (commit 2efedcd6b,
Smoke 1 validated auc_h1000=0.7137 >> 0.65 gate).

Reframe the sweep gate: primary is now anti-calibration check (validates
D1's signed-EMA fix at 0384f7674). Per-horizon mean_run_len
differentiation is informational only — the per-horizon-differentiation
thesis was falsified earlier this session.
2026-05-22 01:59:36 +02:00
jgrusewski
2efedcd6b8 refactor(per-horizon): N_HORIZONS 5→3 — remaining ml-alpha tests
Five test files migrated to clear the last cargo check --all-targets errors:

- tests/multi_horizon_loader.rs:22,64 — hardcoded [usize;5] horizons literal
  → ml_alpha::heads::HORIZONS; for-loop bounds 0..5 → 0..N_HORIZONS
- tests/output_smoothness_grad_finite_diff.rs:198 — [0.1, 0.3, 1.0, 3.0, 10.0]
  → [0.1, 1.0, 10.0] preserving 100× span across horizons
- src/data/loader.rs:560,640 (inline lib tests) — hardcoded [30,100,300,
  1000,6000] literal → crate::heads::HORIZONS
- tests/perception_overfit.rs:318,358 (audit-discovered 5-isms) —
  cfg.seq_len * 5 → cfg.seq_len * N_HORIZONS
- tests/gpu_log_ring_invariants.rs:173 (audit-discovered) — payload field
  name v["payload"]["raw_h30"] → "raw_h10" (matches gpu_log.rs schema
  migrated in Task 5)

cargo check --workspace --all-targets: clean (only pre-existing third-party
cudarc cupti example error, unrelated).
cargo test -p ml-alpha --lib: 33 passed, 0 failed, 6 ignored — baseline.

Golden fixtures deferred to runtime regeneration:
- tests/fixtures/perception_forward_golden.bin (644 → 388 bytes post-rebase).
  Test is #[ignore]-d and rewrites if missing; regenerate during Task 9
  local validation by deleting the .bin and re-running with --ignored.

gpu_log.rs migration verified complete by Task 5 (no remnant 5-horizon
field names in payload_json decoders for RT_INPUT/RT_STATE/RT_OUTPUT).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 01:45:45 +02:00
jgrusewski
0d9fbc16b0 refactor(per-horizon): N_HORIZONS 5→3 — sweep configs + generator script
5 sweep YAMLs updated to reference the new checkpoint filename:
- config/ml/sweep_smoke.yaml: trunk_best_h6000.bin → trunk_best_h1000.bin
- config/ml/sweep_perhoriz_diag.yaml: same
- config/ml/sweep_threshold_tuning.yaml: same
- config/ml/sweep_deployability.yaml: same
- config/ml/sweep_smoke_perhoriz_cfc.yaml: same + 3 comment updates
  (WIN-gate criteria, header doc, best-checkpoint annotation)

scripts/generate_sweep_variants.py:61 updated atomically — without this
the next regeneration of sweep_deployability.yaml would silently
re-introduce trunk_best_h6000.bin.

Argo workflow templates (alpha-perception, alpha-cv, lob-backtest-sweep)
did NOT need text changes — they're already horizon-agnostic:
- They forward CLI flags via {{workflow.parameters.*}} to binaries
- Don't grep alpha_train_summary.json inline
- Don't reference per-horizon field names
- early-stop-metric default is "mean_auc" (horizon-agnostic)

Intentionally left:
- alpha-cv-template.yaml stacker-horizon: "6000" (unrelated TFT lookback)
- alpha-cv-template.yaml horizon: "1200" (DQN execution horizon in bars)
- lob-backtest-sweep-template.yaml ci-training-h100 (GPU pool name)

NOTE: kubectl apply of workflow templates is deferred to Task 10 (push
+ dispatch). Verified all 5 sweep YAMLs parse with python3 yaml.safe_load.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 01:40:13 +02:00
jgrusewski
0c8b4b5608 refactor(per-horizon): N_HORIZONS 5→3 — ml-backtesting full propagation
Source migration:
- crates/ml-backtesting/src/lob/mod.rs:5 + policy/mod.rs:18: local
  const N_HORIZONS 5→3 via re-export from ml_alpha::heads::N_HORIZONS
  (single source of truth across crates)
- crates/ml-backtesting/src/sim/mod.rs:1751,1773,1784: [IsvKellyStateHost; 5]
  array literals → ; N_HORIZONS]
- crates/ml-backtesting/cuda/lob_state.cuh:9: #define N_HORIZONS 5→3
  (this triggers cubin rebuild of decision_policy + 4 other ml-backtesting
  kernels that #include this header)

Test migration (8 test files + 2 JSON fixtures):
- threshold_and_cost.rs, decision_floor_coldstart.rs, parallel_sim_
  correctness.rs, stop_controller.rs (37+10+1 broadcast_alpha calls),
  lob_sim_integrated_fuzz.rs, lob_sim_fixtures.rs: hardcoded [f32; 5]
  alpha-probs and [IsvKellyStateHost; 5] arrays → N_HORIZONS-sized via
  std::array::from_fn or [v; N_HORIZONS] literals
- trainer_parity.rs:34 + ring3_replay.rs:47: horizons literal
  [30,100,300,1000,6000] → ml_alpha::heads::HORIZONS
- fixtures/decision_alpha_buy_close.json + decision_program_h4_only.json:
  5-element warm_start_isv_kelly / alpha_probs / expected_isv_kelly_after
  trimmed to 3 elements; active horizon relocated to N_HORIZONS-1

Library lib-test rewrite (per pearl_tests_must_prove_not_lock_observations):
- crates/ml-backtesting/src/policy/mod.rs:197-233: lib tests
  default_strategy_has_5_horizon_leaves + ..._flattens_to_5_emits...
  renamed to N_HORIZONS-parametric form (observed-value 5 and 7
  were bug-locks).

cargo check -p ml-backtesting --all-targets: clean.
cargo test -p ml-backtesting --lib: 33 passed.
cargo test -p ml-backtesting --tests (non-CUDA, non-fixture-data): 2 passed,
53 ignored (CUDA-gated or FOXHUNT_TEST_DATA-gated).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 01:37:23 +02:00
jgrusewski
afb6d73396 refactor(per-horizon): N_HORIZONS 5→3 — bucket-coupled CUDA kernels
Five CUDA kernels + their Rust caller migrated atomically to prevent
silent memory layout corruption between Rust-side bucket geometry
(MAX_BUCKET_DIM=96, BUCKET_DIM_K=[43,43,42], BUCKET_CHANNEL_OFFSET=
[0,43,86,128]) and kernel-side hardcoded constants.

Kernels:
- bucket_transition_kernels.cu: N_HORIZONS 5→3, MAX_BUCKET_DIM 28→96,
  BUCKET_DIM_LAST 28→42, BUCKET_OFFSETS {0,25,50,75,100,128}→{0,43,86,128};
  bucket_assign_kernel quintile→tercile rewire.
- cfc_step_per_branch.cu: defines + doc.
- heads_block_diagonal_fwd.cu: same; REDUCE_PAD 32→128 (next pow2 ≥ 96).
- multi_horizon_heads.cu: N_HORIZONS_H 5→3 covers fwd, bwd, batched, and
  the GRN/2-layer variants via the single define + shared mem [N_HORIZONS_H]
  + loop bounds.
- output_smoothness.cu: OS_N_HORIZONS 5→3.

Rust caller (silent-corruption fix found during audit):
- crates/ml-alpha/src/cfc/step.rs:135,192 had local hardcoded
  N_HORIZONS=5 / MAX_BUCKET_DIM=28 constants — replaced with
  crate::heads::N_HORIZONS / crate::cfc::bucket_routing::MAX_BUCKET_DIM
  (single source of truth via the Rust SoT constants).

cargo build -p ml-alpha: all 5 cubins rebuild PASS.
GPU oracle tests on RTX 3050 sm_86: 5/19 pass; 14 failures are test-side
fixtures hardcoding old 5×28 layout — owned by SDD Task 8 (not regression).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 01:26:37 +02:00
jgrusewski
89a5d0b203 refactor(per-horizon): N_HORIZONS 5→3 — horizon_lambda + bce_loss + smoothness kernels
Three CUDA kernels + atomically-coupled Rust consumer:
- horizon_lambda.cu: N_HORIZONS_LAMBDA 5→3
- bce_loss_multi_horizon.cu: BCS_N_HORIZONS 5→3
- smoothness_lambda_controller.cu: SLC_N_HORIZONS 5→3 AND TARGET_K_RATIO
  rebased from old-horizon {30,100,300,1000,6000} sqrt formula to new
  {10,100,1000} → {1.0, 0.3162, 0.1}. Payload size 10 → 6 (2×N_HORIZONS).
- gpu_log.rs: payload_json decoders for RT_INPUT, RT_STATE, RT_OUTPUT
  records updated to 3-horizon field names (h30..h6000 → h10/h100/h1000)
  per feedback_no_partial_refactor.

Bucket-coupled kernels (bucket_transition, cfc_step_per_branch,
heads_block_diagonal_fwd, multi_horizon_heads) STILL HAVE N_HORIZONS=5
and bucket geometry constants. Next commit migrates those — they share
memory layout with Rust-side bucket_routing.rs which is already at
N_HORIZONS=3 / MAX_BUCKET_DIM=96, so kernel-side mismatch would be
silent data corruption at runtime.

decision_policy.cu N_HORIZONS comes from lob_state.cuh — Task 6 scope.

cargo build -p ml-alpha and -p ml-backtesting: cubins rebuild PASS.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 01:14:09 +02:00
jgrusewski
3f8e1fb553 refactor(per-horizon): rewrite smoothness controller test for N_HORIZONS=3
Re-derive 3-element fixtures for [10, 100, 1000] preserving geometric
decay invariant. Rename excess_at_h6000_lifts_lambda_proportionally to
excess_at_h1000_lifts_lambda_proportionally.

Critical correction during execution: the kernel uses SQRT-anchored
TARGET_K_RATIO (since commit b5bed9f80 "sqrt K-ratio") not linear ratio.
The lifted-fixture computation mirrors the kernel's sqrt constant
(TARGET_K_RATIO_H2 = sqrt(10/1000) ≈ 0.3162) so the test fires the
lambda = 10 × base invariant under the actual kernel math.

Side-discovery (flagged for Task 5 scope expansion):
- cuda/smoothness_lambda_controller.cu:30 still has SLC_N_HORIZONS = 5
- TARGET_K_RATIO at lines 45-51 uses old-horizon formula {30/30, 30/100,
  30/300, 30/1000, 30/6000}. With N_HORIZONS=3 the kernel reads only
  slots [0..3] = {1.0, 0.5477, 0.3162} — those correspond to old
  30/30, 30/100, 30/300 ratios. h1000's smoothness target is currently
  anchored to OLD h300 ratio (functional bug requiring kernel update).

cargo test --test smoothness_lambda_controller_invariants: 4 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 01:07:33 +02:00
jgrusewski
81e9970d4c refactor(per-horizon): N_HORIZONS 5→3 — alpha_train example
Renames all *_h6000 references to *_h1000 (new longest horizon):
- AlphaTrainSummary struct fields: best_auc_h6000_* → best_auc_h1000_*,
  best_h6000_ckpt_path → best_h1000_ckpt_path
- State vars: best_auc_h6000*, h6000_no_improvement → h1000 equivalents
- Checkpoint filename: trunk_best_h6000.bin → trunk_best_h1000.bin
- CLI early-stop metric: "auc_h6000" → "auc_h1000"
- Tracing log fields: w_h30..w_h6000 → w_h10/w_h100/w_h1000
- 4 hardcoded [seq.labels[0..4][k]] literals → std::array::from_fn
- MultiHorizonLoaderConfig.horizons + AlphaTrainSummary.horizons:
  literal [30,100,300,1000,6000] → HORIZONS constant
- Doc comments: "5 horizons", "h6000-snapshot" → "N_HORIZONS", "longest-horizon"

KNOWN FOLLOW-UP (Task 7): 5 sweep YAMLs reference trunk_best_h6000.bin
filename — must update atomically when applying workflow template changes.

KNOWN FOLLOW-UP (Task 8): crates/ml-alpha/src/gpu_log.rs:176-192 still
decodes 5-horizon JSON fields (raw_h30..h6000, ema_h30..h6000, etc.).

cargo check --example alpha_train: PASS.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 01:01:18 +02:00
jgrusewski
1e8188c947 refactor(per-horizon): N_HORIZONS 5→3, HORIZONS=[10,100,1000] — library + data layer
First commit of the horizon-rebase refactor (full plan at
docs/superpowers/plans/2026-05-22-horizon-rebase-n3.md). Motivated by
empirical evidence from this session's investigations:

- C1: ES MBP-10 input ACF dies by L=300; K=1000/6000 are below noise floor
- D3: binarized labels ρ(30,6000)=0.06; K=6000 carries no signal
- Heads_w1 mask experiment: h6000 AUC collapsed 0.73→0.54, confirming
  long-horizon prediction needs cross-channel integration that 5-horizon
  bucket structure couldn't provide

New horizon set [10, 100, 1000] matches C1's empirical autocorrelation
elbows (~50ms, ~3.5s, ~45s wall-clock at 74µs median Δt).

Scope of this commit:
- crates/ml-alpha/src/heads.rs: N_HORIZONS 5→3, HORIZONS=[10,100,1000]
- crates/ml-alpha/src/cfc/bucket_routing.rs: MAX_BUCKET_DIM 28→96,
  BUCKET_DIM_K=[43,43,42], BUCKET_CHANNEL_OFFSET=[0,43,86,128]
- crates/ml-alpha/src/data/loader.rs: 5 hardcoded [T;5] types migrated
- crates/ml-backtesting/src/sim/mod.rs: broadcast_alpha signature,
  local N_HORIZONS re-exports ml_alpha::heads::N_HORIZONS
- crates/ml-backtesting/src/harness.rs: local N_HORIZONS re-export,
  per-horizon log labels, MultiHorizonLoaderConfig.horizons

Tests + examples + CUDA kernels still need migration (subsequent commits
per plan tasks 2-8).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 00:56:16 +02:00
jgrusewski
0384f76743 fix(decision-policy): use signed-conviction EMA, not magnitude-only EMA
Anti-calibration evidence (smoke ckpts across architecture variants
showed higher reported conviction → MORE negative PnL, -15.6 → -145.2)
traced to a magnitude/direction mismatch in the sizing formula:

  conv_ema = EMA(|conviction_signed|)         # magnitude smoothing
  target_lots = sign(conviction_signed)       # INSTANTANEOUS sign
              × conv_ema × max_lots           # × SMOOTHED magnitude

When per-horizon directions disagree event-to-event, the magnitude EMA
stays high (|x| EMA can't cancel) but the sign flips on noise. Result:
big trades in random direction whenever the 5 horizons disagree.

Replace with a single signed-conviction EMA:

  conv_signed_ema = EMA(conviction_signed)
  target_lots = sign(conv_signed_ema)
              × |conv_signed_ema| × max_lots

Now BOTH sign AND magnitude come from the same smoothed signal. When
directions disagree, signed EMA → 0 collapses size to zero naturally.

Atomic migration across decision_policy.cu (2 kernels), pnl_track.cu
(open-branch reads the SIGNED buffer and takes fabsf for the magnitude-
semantics open_trade_state offsets that composite_exit_check forms ratios
from), and src/sim/mod.rs (renamed device buffers + accessor + 4 launch
sites). No legacy aliases per feedback_no_legacy_aliases; no parallel
buffers per feedback_single_source_of_truth_no_duplicates; α floor 0.4
preserved per pearl_wiener_alpha_floor_for_nonstationary; first-
observation bootstrap preserved per pearl_first_observation_bootstrap.

ml-backtesting lib: 33 passed.
GPU oracle: signed_conviction_ema_collapses_on_sign_flips passes —
observed steady-state shows |signed_ema| ≈ 0.205 (post-fix) producing
|target_lots| = 2 every tail event, vs the pre-fix bug's predicted
|target_lots| = 7 every tail event. Existing
multi_horizon_conviction_cancels_on_disagreement still passes (zero-
cancellation regime is even cleaner under signed EMA).
ml-alpha lib: 33 passed (no regression).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 00:26:22 +02:00
jgrusewski
fe5d7a1ad7 fix(loader): rename EMA bandwidth constants to reflect actual half-lives
Empirical ES MBP-10 median inter-event Δt is 74µs (not 250ms as
documented in loader.rs:25-26). The constants ALPHA_MED=0.02 and
ALPHA_SLOW=0.0005 were named/commented as "9s @ 250ms" and "6min @
250ms" but at the actual 74µs Δt their half-lives are ~2.6ms and ~100ms
— a 3000× scale mismatch.

Rename for truth-in-naming (values unchanged to preserve all trained
checkpoints):
- ALPHA_MED → ALPHA_FAST_2P6MS (half-life ~2.6ms @ 74µs Δt)
- ALPHA_SLOW → ALPHA_MED_100MS (half-life ~100ms @ 74µs Δt)

The regime[0..5] feature naming in snap_features.rs reflects the
actual microstructure-burst and 1-2-second-clustering time scales, NOT
macro 9s/6min regimes. True macro-regime EMAs (if needed) are a
separate addition.

ml-alpha lib: 33 passed, unchanged from baseline.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 00:09:25 +02:00
jgrusewski
e18325aaf1 Revert "fix(per-horizon-cfc): extend block-diagonal mask to heads_w1 (Smoke 2 fix)"
This reverts commit 4c1ed1d953.
2026-05-21 23:48:15 +02:00
jgrusewski
4c1ed1d953 fix(per-horizon-cfc): extend block-diagonal mask to heads_w1 (Smoke 2 fix)
Diagnostic Smoke 2 (lob-backtest-sweep-kw7f6 at d2d34b6d4) confirmed
empirically:
- Per-horizon logit distributions near-uniform (mean spread 0.05, std
  spread 0.02), explaining mean_run_len ratio = 1.04x (vs target >=10x)
- heads_w1 inspection: 100% dense at off-bucket positions (32,768/32,768
  nonzero), magnitude ratio off/in ~= 1.0
- GRN kernel grep confirmed heads_w1 is the SOLE remaining HIDDEN_DIM-
  reading path bypassing bucket-routing (heads_w_skip already restricted;
  heads_w2/gate/main operate within HEAD_MID_DIM only)

Extends the existing heads_w_skip block-diagonal 3-layer defense to
heads_w1 with mask shape [N_HORIZONS x HEAD_MID_DIM x HIDDEN_DIM]:

- heads_w1_mask_init_kernel at transition: zero off-bucket params +
  build mask
- heads_w1_zero_off_bucket_kernel on Adam (m, v) moments at transition
  (prevents momentum from re-introducing off-bucket weights)
- heads_w1_grad_mask_apply_kernel per-step: zero off-bucket gradient
  before Adam
- heads_w1_zero_off_bucket_kernel per-step post-Adam: belt+suspenders
  for eps drift

GPU oracle tests verify mask init, grad mask apply, and post-Adam
invariance under simulated training (3 new tests for the 3-D heads_w1
shape; mirrors the existing heads_w_skip oracle tests).

ml-alpha lib: 33 passed.
GPU oracle tests on RTX 3050 sm_86: 19 passed (16 prior + 3 new).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 23:08:36 +02:00
jgrusewski
ff9207e7bb feat(per-horizon-cfc): plumb kernel-step-trace into fxt-backtest inference path
Mirrors the alpha_train CLI flag (--kernel-step-trace <path>) into
fxt-backtest. The PerceptionTrainerConfig already accepts the path;
this commit just exposes the CLI surface and propagates through:

  fxt-backtest run --kernel-step-trace <path>
  -> BacktestHarnessConfig.kernel_step_trace_path
  -> PerceptionTrainerConfig.kernel_step_trace_path

Sweep YAML gains a `kernel_step_trace: Option<PathBuf>` base field.
Argo lob-backtest-sweep-template gains a `kernel-step-trace` workflow
parameter (default disabled; when set, --features kernel-step-trace
is passed to cargo build AND --kernel-step-trace to fxt-backtest).

Gated behind the `kernel-step-trace` Cargo feature (matching alpha_train).
When disabled (default), zero overhead.

Enables per-step JSONL diagnostic emission from inference kernels --
needed for Smoke 2 deep-dive after sampling histograms (in parallel
investigation) localize the per-horizon dynamics bottleneck.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 22:33:44 +02:00
jgrusewski
d2d34b6d4e diag(per-horizon-cfc): per-horizon alpha_probs sampling for Smoke 2 investigation
Adds --per-horizon-logit-sampling-stride CLI flag to fxt-backtest run/sweep
+ harness instrumentation that DtoHs alpha_probs every N events and
accumulates per-horizon mean/stddev/min/max + 10-bin histogram.

Emitted at end of CRT.diag block as:
  per_horizon_logit_diag h{N}: count=... mean=... std=... min=... max=... hist=[...]

Goal: distinguish whether Smoke 2's uniform mean_run_len 1.04x across
horizons is caused by uniform per-horizon LOGITS (-> Task 11 scope
incomplete; heads_w1/w2/gate/main need block-diagonal too) or by
DIFFERENTIATED logits with collapsed downstream (-> different fix).

Default stride=None (disabled, zero overhead). With stride=1000 on
2M events, ~2000 DtoD stops x ~50us = ~100ms total overhead per run.
Per feedback_no_htod_htoh_only_mapped_pinned: uses mapped-pinned buffer
(single 5xf32 allocation reused on every sampling tick).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 22:20:42 +02:00
jgrusewski
8b2ac1e577 fix(per-horizon-cfc): ALPHA — skip reorder, channels_in_bucket lookup, Adam moment masking
URGENT correctness fix surfaced by checkpoint deep-dive after Smoke 2 failed
the WIN gate (mean_run_len ratio = 1.0× vs target ≥10×; all 5 horizons
uniformly ~2.4 events).

THREE INDEXING BUGS identified:

1. tau_reorder produces bucket-grouped tau_all_d, but Controller B's
   tau_clamp_kernel reads bucket_id_per_channel[c] where c is the
   POSITION in the reordered buffer (not the original channel index)
   → wrong bucket-IQR lookup → τ never constrained → buckets collapse
   (deep-dive showed all 5 buckets had nearly identical τ ranges
   [0.07, 74] except bucket 4 reaching 878).

2. heads_w_skip grad mask ran but Adam (m, v) momentum from BEFORE the
   transition re-introduced gradient signal across the transition →
   off-bucket positions stayed nonzero throughout training
   (deep-dive: 512/512 = 100% of off-bucket positions nonzero in
   trunk_best_h6000.bin).

3. per-branch CfC kernel read w_in[c * HIDDEN_DIM + k] with c =
   bucket-grouped position, but W_in rows are indexed by ORIGINAL
   channel → kernel read wrong rows for each output → outputs were
   essentially random per-channel.

ALPHA FIX: skip the reorder entirely, use bucket-filter throughout:
- Removed tau_reorder_kernel; cfc.tau_d stays in original-channel layout.
- Added channels_in_bucket_kernel that populates a
  [N_HORIZONS × MAX_BUCKET_DIM] lookup (original channel index per
  (bucket, within-bucket-position)).
- Per-branch CfC fwd+bwd now reads channels_in_bucket[branch][tid]
  → original_c, then uses original_c for w_in/w_rec indexing. All
  weights stay in original layout consistently.
- Controller B's tau_clamp_kernel now correctly operates on original-
  channel cfc.tau_d with bucket_id_per_channel[c] lookup (no position-
  vs-channel confusion).
- Added zero_off_bucket_kernel + three-layer defense for heads_w_skip
  block-diagonal invariant:
    (a) At transition: zero off-bucket params + zero Adam (m, v)
        moments via opt_heads_w_skip.m_mut() / v_mut() accessors.
    (b) Per-step: heads_w_skip_grad_mask_apply_kernel zeros off-bucket
        gradients before Adam step (unchanged from prior follow-up).
    (c) Per-step: zero_off_bucket_kernel zeros off-bucket params after
        Adam step, catching any drift from Adam's ε denominator or
        weight decay.

New AdamW::m_mut()/v_mut() accessors enable the projection at transition.

GPU oracle tests: 19 total (16 in bucket_transition + 3 in cfc_step_per_branch).
New tests verify:
- channels_in_bucket_kernel correctness under non-contiguous bucket assignment
- zero_off_bucket maintains invariant after many mock Adam steps
- fwd kernel writes only to bucket-assigned channels under arbitrary mapping

Per `feedback_no_partial_refactor`: all 3 indexing bugs + Adam momentum
defense land in one commit.

ml-alpha lib: 33 passed.
GPU oracle tests on RTX 3050 sm_86: 19 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 20:43:51 +02:00
jgrusewski
bf4d0c899f fix(per-horizon-cfc): sync trunk bucket metadata from transition output
URGENT correctness fix surfaced by Task 13 implementer.

Bug: training-time Phase 2 dispatch sites (cfc_step_per_branch_fwd at
~3139, h_mag_per_bucket for Controller D at ~3211-3212) read
self.trunk.bucket_*_d which are zero-initialized at trunk construction
and only populated via load_checkpoint. The Phase 1→2 transition
populates a SEPARATE BucketRoutingMetadata owned by the trainer
(self.bucket_routing_metadata) but never syncs it into the trunk fields.
With zero-init bucket_dim_k, the per-branch kernel's uniform predicate
`tid >= bucket_dim_k[branch]` early-returns ALL threads -> Phase 2
silently no-ops during training, Smoke 1 fails before producing signal.

Fix: at transition, DtoD-copy metadata buffers into trunk fields BEFORE
the `Some(metadata)` move (so `metadata.*` borrows remain valid):
- metadata.bucket_channel_offset_d -> trunk.bucket_channel_offset_d
- metadata.bucket_dim_k_d          -> trunk.bucket_dim_k_d
- metadata.bucket_id_per_channel_d -> trunk.bucket_id_per_channel_d
- metadata.heads_w_skip_offset_d   -> trunk.heads_w_skip_offset_d

Trunk fields remain the single source of truth for both training-time
dispatch AND checkpoint serialization. Total sync cost: 4 DtoD memcpys,
~256 bytes total at the one-shot transition (off the hot path).

Verification:
- `cargo check -p ml-alpha --all-targets`: clean
- `cargo test -p ml-alpha --lib`: 33 passed, 0 failed
- 3 read sites (perception.rs:3139, 3211-3212, 4725-4726) verified
  unchanged; all 4 new memcpy_dtod_async calls bracketed in the
  transition block (perception.rs:2224, 2234, 2244, 2254).
- Block-diagonal heads grad-mask init (step 7) continues to read
  `metadata.bucket_id_per_channel_d` via `as_ref()` after the move;
  ordering preserved per pearl_canary_input_freshness_launch_order.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 19:16:27 +02:00
jgrusewski
ed8b53b474 feat(per-horizon-cfc): forward_step_into uses Phase 2 fused dispatch
Per spec §2.4 and Task 13 of the plan.

Replace the single-CfC `cfc_step_batched` dispatch in `forward_step_into`
with `cfc_step_per_branch_fwd_gpu`. Production checkpoints have Phase 2
routing frozen at the training-time Phase 1→2 transition, so inference
unconditionally takes the per-branch path. The fused kernel reads
`bucket_channel_offset_d` / `bucket_dim_k_d` from the trunk; in
deployment these are populated via `CfcTrunk::load_checkpoint`. For
freshly-constructed trainers without a checkpoint load, the trunk fields
are zero — the kernel's uniform predicate (`tid >= bucket_dim_k`) then
early-returns every thread and h_new is left untouched. That matches the
"Phase 2 only at inference" contract.

Heads dispatch unchanged: the existing GRN kernel reads from
`trunk.heads_w_skip_d` which has off-bucket entries zeroed at the
transition (sparsification per the prior block-diagonal-grad-mask
follow-up commit). Mathematically the full-buffer read is equivalent to
a compact-only per-bucket read; no inference-time kernel change required.

forward_step_golden's convergence test is now architecturally
incompatible with the new dispatch — `forward_only` runs Phase 1
single-CfC math while `forward_step_into` requires populated Phase 2
routing. Annotated `#[ignore]` with a clear divergence note; the
deterministic + reset semantics tests remain valid invariants per
`pearl_training_smoothness_does_not_transfer_to_inference`.

cargo check workspace clean (excluding pre-existing unrelated cupti +
insert_batch errors in vendor/cudarc and ml/tests). ml-alpha + ml-
backtesting lib tests pass (33 + 33).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 19:10:22 +02:00