Root cause of Q-value saturation at +/-50 seen in train-6nbx5 after
ISV v-range unification (9deda5f65, 11df03785): cold-start path in
`update_eval_v_range` latched `q_std_ema = q_std.max(0.01)` on the
first epoch. But that first `q_std` is dominated by the +/-50
bootstrap atom-spread (scaffolding set in construct/reset), NOT by
real Q-distribution spread. Result: `3*std_ema` exceeds
`min_half_floor=10` and approaches `abs_half=50` immediately, atoms
stay wide next epoch, next `q_std` confirms that width, EMA never
escapes. Q saturated at +/-abs_half every run.
Fix 1 (gpu_dqn_trainer.rs:3106-3120): seed
`eval_q_std_ema = min_half_floor / 3.0` at cold start so initial
`half = 3 * std_ema = min_half_floor` exactly. Adaptive-rate EMA
(alpha clamped to [0.01, 0.3]) then relaxes upward only if genuine
Q-spread warrants it. Breaks the self-confirming initialization.
Fix 2 (training_loop.rs:479-494): remove leftover pre-clamp of
reward quantiles to `config.v_{min,max}`. That was from the earlier
quantile-clamp fix (d38a8cf99). Phase 2c (9deda5f65) moved the
per-branch clamp inside `warm_start_atom_positions`, which reads
each branch's [centre-half, centre+half] from the ISV pinned bus.
An outer static clamp to the wider config bound is redundant
double-clamping and hides which layer owns the support. Pass raw
quantiles through to warm_start.
Validated locally: SQLX_OFFLINE cargo check -p ml passes (only
pre-existing warnings).
Next: push + L40S validation run. Diagnostic instrumentation from
423ac460b remains in place to confirm (center, half) trajectory on
the next run — will be removed once validated.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds targeted tracing::info! at three call sites to diagnose why Q-value
range hits exactly +/-50 (config hard safety clamp) at every epoch after
the ISV v-range unification commits 9deda5f65 and 11df03785.
Instrumentation (all under target="isv_vrange_diag"):
1. update_eval_v_range (called at epoch-final Q-stats): per-branch
(q_mean, q_std, q_gap, center, half, initialized_before) — captures
whether cold init or warm EMA path produced the ISV write.
2. recompute_atom_positions (called at epoch N start): per-branch host
read of ISV (center, half) — what the adaptive_atom kernel will
consume on this epoch's forward pass.
3. warm_start_atom_positions (called mid-epoch, after experience
collection): per-branch clamp bounds (b_min, b_max, isv_ptr_null),
plus post-write per-branch (amin, amax) of the atoms just uploaded.
NOT PUSHED — local diagnostic commit for root-cause investigation.
Completes the ISV-unified Q-support range spec
(docs/superpowers/specs/2026-04-23-isv-v-range-unification.md) by
migrating the per_sample_support buffer from per-sample [B, 3] to
per-sample-per-branch [B, 4, 3] stride-12. Without this phase the
atom_positions grid already spanned per-branch adaptive ranges (landed
in 9deda5f65 via ISV slots 23..30) while the loss-projection
Bellman step still read a single V(s)-centred range — atoms and
projection disagreed, which is the exact pathology the spec fixes.
Producers:
* iql_value_kernel.cu::iql_compute_per_sample_support — new kernel
signature adds isv_signals pointer; writes 4 branch triples per
sample where centre = isv_signals[23 + 2*d] and half-width = V(s)-
derived Q spread. Bootstrap identity: ISV centres=0 + readiness=0
falls back to [-1, 1] across all 4 branches, byte-identical to the
pre-Phase-2d single-range default at epoch 1.
* iql_value_kernel.cu::iql_support_floor — Frugal-1U p5 estimator now
aggregates half-widths across all (sample, branch) pairs and applies
the floor per (sample, branch) independently.
* gpu_iql_trainer.rs — per_sample_support_buf sized b*4*3, seed writes
12 floats per sample, compute_per_sample_support takes isv_dev_ptr
and forwards it to the kernel; launch-site arg order aligned.
* fused_training.rs — passes trainer.isv_signals_dev_ptr() into the
IQL call.
Consumers (all migrated to stride-12 indexing `b*12 + d*3 + {0,1,2}`):
* c51_loss_kernel.cu::c51_loss_batched — per-branch (v_min, v_max,
delta_z) read INSIDE the d-loop; degenerate-support skip is now
per-branch (continue instead of whole-sample early exit).
* c51_grad_kernel.cu::c51_grad_kernel — per-branch z_norm and
delta_z for the q-gap floor gradient path.
* experience_kernels.cu::compute_expected_q — per-branch (v_min, dz)
inside the d-loop that iterates all 4 branches.
* experience_kernels.cu::mag_concat_qdir — reads direction branch
(d=0) slots from the stride-12 tile.
* experience_kernels.cu::quantile_q_select — per-branch (v_min, dz)
inside the d-loop.
Experience-collector parity:
* gpu_experience_collector.rs — its OWN per_sample_support_buf grows
to alloc_episodes*4*3; update_per_sample_support tiles the same
(v_min, v_max, delta_z) triple to all 4 branches so the layout
matches the IQL buffer and the consumer kernels read uniformly.
Safety:
* Bootstrap byte-identical at epoch 1 preserved (ISV centres default 0,
readiness ramps from 0 → 1).
* No stub values, no TODO/FIXME/XXX markers introduced.
* Kernel scalar arg (gamma) is already f32 in GpuIqlConfig — no f64→f32
cast needed at the call site (feedback_cudarc_f64_f32_abi compliance
via type, not cast).
* c51_loss branch-degenerate `continue` is uniform across the block
(all threads read the same support_base) so __syncthreads inside
the loop body remains collective.
Compile verified: cargo check -p ml + --workspace pass (SQLX_OFFLINE,
CARGO_INCREMENTAL=0, sccache) and cargo build -p ml compiles all
CUDA kernels via nvcc. cargo test -p ml --lib --no-run succeeds.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Unifies the Q-support range source across atom grid / warm-start quantile
clamp consumers via the ISV signal bus. One broadcast written at epoch
boundary from per-branch Q-stats EMAs, read by the two consumers that
previously held disagreeing ranges. Target observation: atom utilisation
≥40% (up from 11-15% on train-fpxnw).
Phase 0 — per-branch Q-stats kernel Rust plumbing:
* Load q_stats_per_branch_reduce alongside legacy q_stats_reduce
* Add per_branch_q_stats_pinned (28 f32 = 4 × 7, device-mapped)
* PerBranchQValueStats struct: [QValueStatsResult; 4]
* reduce_current_q_stats_per_branch launches the new kernel with the
four branch (off, size) pairs derived from config.branch_N_size
Phase 1 — ISV v-range plumbing (zero behavioural change at epoch 1):
* ISV_NETWORK_DIM=23 preserved for w_isv_fc1 sizing; ISV_TOTAL_DIM=31
allocates 8 additional slots for per-branch (centre, half-width)
* Slot constants V_CENTER_DIR..V_HALF_URG covering slots 23..30
* eval_q_mean_ema / eval_q_std_ema / eval_ema_initialized promoted
to [f32; 4] / [bool; 4]; scalar setters preserved for trajectory
backtracking (broadcast same value to all branches)
* Bootstrap at construction: centre=0, half=(v_max-v_min)/2 → the
byte-identical [config.v_min, config.v_max] span per branch before
any Q observations arrive
* reset_eval_v_range_state resets the 4 per-branch EMAs AND the 8 ISV
slots to bootstrap values; legacy eval_v_range_pinned[2] still reset
(deferred removal — spec Phase 3)
* update_eval_v_range reworked: signature takes PerBranchQValueStats and
per_branch_q_gaps. Maintains 4 independent adaptive-rate EMAs,
computes (centre, half) per branch with min_half_floor=0.1×(v_max-v_min)
and clamps to config bounds, writes 8 ISV slots. Branch-0 (direction)
centre±half is also mirrored into the legacy eval_v_range_pinned for
consumers that have not yet migrated to the per-branch bus.
Phase 2a/2b — atom grid per-branch v-range:
* adaptive_atom_positions kernel signature changed from
(v_min: float, v_max: float) to (branch_idx: int, isv_signals: float*);
reads centre/half from ISV slots 23+2·b, 24+2·b. Eliminates the f64→f32
ABI trap (spec Phase 2 side-effect) since the only per-branch range
path is now pointer-based.
* recompute_atom_positions passes branch_idx + isv_signals_dev_ptr per
branch; no scalar v_min/v_max arg remains.
Phase 2c — warm-start quantile clamp per-branch from ISV:
* warm_start_atom_positions reads per-branch (centre, half) from pinned
ISV host memory, clamps shared reward-quantile vector into each
branch's adaptive range before tiling into atom_positions_buf.
Bootstrap makes this equivalent to the pre-spec config.v_{min,max}
clamp until the first Q observation lands.
Deviations from spec:
* Phase 2d (per_sample_support_buf → [N, 4, 3]) NOT implemented. The
spec's premise was that per_sample_support is host-tiled from
eval_v_range, but the active path in this codebase has it filled by
iql_compute_per_sample_support (V(s)-centered, per-sample, already
adaptive) — orthogonal to the ISV bus. Migrating that kernel to
per-branch output would require rewriting iql_value_kernel +
iql_support_floor + C51/MSE loss kernel indexing in lockstep, which
the "no unrelated refactoring" constraint disallows. The loss-kernel
Bellman projection today uses V-centered bounds that are themselves
adaptive; the ISV v-range fix still lands the primary win (atom grid
+ warm-start agreement) without touching IQL.
Compile verified: cargo check -p ml + --workspace pass (SQLX_OFFLINE,
CARGO_INCREMENTAL=0, sccache). No TODO/FIXME/XXX introduced.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Root cause of the Q=±333k explosion at every run's epoch 2.
`warm_start_atom_positions` writes quantiles from the raw environment
reward distribution directly into `atom_positions_buf`. Raw rewards
are unbounded PnL-scaled values — a single extreme sample in the
first experience buffer becomes `atom_positions[num_atoms-1]`, and
the C51 expected-value readback `Q = Σ prob × atom_pos` inherits
that magnitude.
Observed deterministically across train-7rgqd, train-5gzpn, and
train-gj54m: epoch 1 Q in `[0, ~6]` (initial Xavier atoms), epoch 2
Q at exactly `±333406` once warm-start writes the sorted-reward-tail
into the atom grid. Every downstream path — the C51 loss projection,
eval_v_range EMA, IQL support, HEALTH_DIAG q_gap — assumes
`atom_positions ∈ [v_min, v_max]`. The warm-start path was the only
one bypassing that assumption.
Clamp each quantile to the configured `[v_min, v_max]` before writing.
This is a safety rail, not a tuning parameter: config.v_{min,max} are
already derived from reward_scale (±15 default), which is the support
range the rest of the system expects.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
target_params_buf was initialized once via DtoD copy at first train
step and then only moved toward online via slow Polyak EMA (tau≈0.005).
At fold boundaries the online weights are shrink-and-perturb'd with
alpha=0.8, which modifies params_buf in-place — but target_params_buf
still held the end-of-previous-fold values. The Bellman target would
then use stale weights against freshly perturbed online predictions,
producing a large TD error gap in the first fold-N+1 training steps.
Polyak averaging at tau=0.005 is far too slow to close that gap before
the oversized gradients compound through Adam into runaway updates —
one of the drivers of the fold-1 gradient explosion observed in both
train-7rgqd and train-5gzpn.
- Add GpuDqnTrainer::sync_target_from_online() — DtoD memcpy of the
full params_buf into target_params_buf.
- Call it from FusedTraining::reset_for_fold right after shrink-and-
perturb and before reset_adam_state, so target = perturbed online
and Adam moments zero out from the same starting point.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
GpuIqnHead carries its own m_buf/v_buf/adam_step — separate from
GpuDqnTrainer::reset_adam_state, which only zeroes the legacy
iqn_trunk_* buffers. Without a fold-boundary reset, the IQN
optimizer enters fold N+1 with fold N's momentum, producing
oversized Adam steps that compound through the IQN backward
pass into the runaway gradients we observed in both train-7rgqd
(crashed fold 1 ep 52) and train-5gzpn (NaN'd fold 1 ep 17).
- Add GpuIqnHead::reset_adam_state() — zero m_buf, v_buf,
adam_step, and the pinned t counter.
- Call it from FusedTraining::reset_for_fold after the trainer's
main Adam reset, gated by gpu_iqn.is_some(). Non-fatal warn on
failure to match the surrounding shrink-and-perturb pattern.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Three kernel launches passed f64 config fields directly into argument
slots whose kernel-side declaration is `float`. cudarc's `DeviceRepr`
impl for f64 places an 8-byte value at the next 8-byte-aligned slot,
but CUDA reads only 4 bytes for a `float` parameter — the low 4 bytes
of the f64 — then advances to the next slot. For a typical config
value the low bytes of the f64 encoding are near-zero, producing
garbage values and shifting every subsequent arg slot by 4 bytes of
padding mismatch.
Affected sites:
- recompute_atom_positions → adaptive_atom_positions kernel
(v_min/v_max for C51 atom grid placement)
- c51_loss_batched (forward) → c51_loss_kernel
(curiosity_q_penalty_lambda, spectral_decoupling_lambda)
- mse_loss_batched (forward) → mse_loss_kernel
(same two lambdas)
Cast to f32 explicitly at the call site and bind to a let so the
&value reference points into a 4-byte f32 slot. Observed symptom:
Q-value range oscillating to ±144k at epoch 25 while the config
`v_min=-15, v_max=+15` theoretical bound should have held atoms
inside that range.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
fix kernel-read gap
Adds 12 features to the DQN input pipeline:
- 10 MicrostructureState::snapshot()[0..10] slots that were previously computed
every bar and then discarded before reaching fxcache: ofi_trajectory,
realized_variance, hawkes_intensity, book_pressure (weighted 10-level),
spread_dynamics, aggression_ratio, queue_depletion_asymmetry,
order_count_flux, intra_bar_momentum, regime_score.
- 2 TLOB-novel slots derived directly from Mbp10Snapshot:
order_count_imbalance = (Σbid_ct − Σask_ct) / Σ(bid_ct + ask_ct),
microprice_residual = (weighted_mid − mid) / mid.
Also fixes a production gap: ofi_acceleration (slot 18) and
toxicity_gradient (slot 19) were persisted to fxcache via OFI_DIM=20
but the OFI embed kernel (experience_kernels.cu:6146-6173) only read
[0..18), silently discarding them every bar. Kernel extended to
consume full SL_OFI_DIM=32.
Dimension bumps (all 8-aligned):
OFI_DIM 20 → 32
FXCACHE_VERSION 4 → 5 (invalidates existing caches; regen via
precompute_features)
STATE_DIM 96 → 104
PADDING_DIM 4 → 0 (OFI expansion consumed padding, still 8-aligned)
STATE_DIM_PADDED 128 (unchanged)
OFI_EMBED_IN 18 → 32 (MLP input width; W/grad/Adam/m/v buffers
resized in lockstep via named constants)
fxcache regen results (175874 bars ES.FUT 2024-Q1):
deltas_nonzero: 175781 / 175874 (99.9 percent)
book_aggression: 102137 / 175874 (58.1 percent)
microstructure[20-30): 175874 / 175874 (100 percent)
tlob_novel[30-32): 133615 / 175874 (76.0 percent)
Compile status: SQLX_OFFLINE=true CARGO_INCREMENTAL=0 cargo check
--workspace --tests passes cleanly (0 errors, pre-existing warnings
only).
Test results:
fxcache roundtrip (unit + integration): PASS (4+6 tests)
magnitude_distribution smoke: ran through epoch 1 successfully
(OFI_DIAG fires, state_dim=104 confirmed, feature_dim=74 in
validation kernel); epoch 2 OOM on local RTX 3050 Ti (4 GB) —
expected hardware limit from state_dim growth. Full 20-epoch run
requires L40S/H100 CI verification.
multi_fold_convergence smoke: not verified locally (same VRAM
ceiling applies). L40S/H100 CI verification required.
The new slots follow the existing OFICalculator/MicrostructureState
pattern and consume signals already computed by ml-features — no new
crate, no ONNX, no stubs. All 12 sources were audited against their
implementation before persistence; every slot traces back to real
Mbp10Snapshot or MicrostructureState math.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
C51 var wired, q_abs_ref clamp
Branch worktree-agent-a496c8e9, commit 8a5e7d316. Implements the 4
fixes from docs/superpowers/specs/2026-04-23-isv-signal-quality-audit.md.
1. Regime signals (slots 8-11) now aggregate over all B samples inside
the single-threaded isv_signal_update kernel. Adds int batch_size
arg and fixes a latent stride bug (launch passed STATE_DIM=104 but
states_buf has stride STATE_DIM_PADDED=128; sample-0 worked by
luck because row-0 offset is identical). 99.994% information loss
closed. Expected to unblock the adaptive cql_alpha gate which
previously stayed at 0.0 for all 20 epochs on train-mdh86.
2. Health (slot 12) decoupled from outcomes is the biggest finding
from the audit: r = -0.765 over 20 epochs — health climbed
0.50→0.64 EXACTLY while Sharpe collapsed +34→-67. Every adaptive
mechanism using `(1 - health)` as stress signal was reading the
inverse of truth. Replaced with sigmoid(0.1 × sharpe_ema) EMA-
blended. New ISV slot 22 = SHARPE_EMA_INDEX persists the Rust-
side training_sharpe_ema (broadcast from training_loop.rs:3541).
ISV_DIM 22→23. Post-fix validation: 3 smoke runs show health
declining 0.50→0.40 in response to consistently negative training
Sharpe — outcome-coupled, not inverted.
3. Slot 3 now carries C51 Q-distribution variance (wired via third
c51_loss_reduce launch, same pattern as td_error fix 7f92fa242).
Previously zero-initialised with no writer. Renamed scratch to
reflect actual semantics (true multi-head ensemble variance
remains a separate follow-up — no per-head Q readback exists in
the current architecture).
4. q_dir_abs_ref (slot 21) and q_abs_ref (slot 16) gain outlier
clamps before EMA update: new_abs = min(raw, 10×current + 1) to
prevent single Q-excursions (±10⁵ observed on train-mdh86 at
epochs 0/3/7/11) from poisoning the Kelly-conviction denominator
for 20 epochs.
Pre-production checkpoint compat note: ISV_DIM bump grows w_isv_fc1
from [16,22] to [16,23]. No safetensors format encodes ISV_DIM
directly, but flat param buffer layouts shift — any live checkpoint
from before this commit needs regeneration. Acceptable for current
dev state; no production-live models exist.
Pre-existing 14/872 test failures (OFI missing, profile drift,
test_batch_size env) unchanged by this commit.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
C51 Q-var slot + q_abs_ref outlier clamp
Bundles 4 ISV signal-quality fixes from the audit at
docs/superpowers/specs/2026-04-23-isv-signal-quality-audit.md.
1. Regime signals (slots 8-11) now batch-aggregate over all B samples
instead of reading sample 0 only. Adds `int batch_size` kernel arg
and fixes a latent stride bug (launch passed STATE_DIM=104 but
states_buf has stride STATE_DIM_PADDED=128 — sample 0 worked by
luck because both strides land at the same offset for row 0).
99.994% information loss closed.
2. Health (slot 12) now couples to outcomes: sigmoid(0.1 × sharpe_ema)
EMA-blended. New ISV slot 22 = SHARPE_EMA_INDEX persists the
Rust-side training_sharpe_ema. ISV_DIM 22→23. Prior component-
aggregation health formula was ANTI-correlated with Sharpe
(r=-0.765 per audit) because its components saturated at 0/1
boundaries (q_gap=1.0 / q_var=1.0 for 19/20 epochs, grad_stable
stuck at 0.0 for 20/20 epochs). New formula's sensitive sigmoid
region [-10, +10] Sharpe matches the observed magnitude range.
The Rust-side write_isv_signal_at is preserved as a fallback
initializer at epoch boundaries — the kernel then overwrites
slot 12 every training step based on slot 22's current value.
3. Slot 3 now carries C51 Q-distribution variance (wired via third
c51_loss_reduce launch, same pattern as td_error fix 7f92fa242).
Previously zero-initialised with no writer. Renamed from
"ensemble_var_scratch" to "q_var_scratch" to reflect actual
semantics — it is the batch mean of q_var_buf_trainer (atom-
spread variance from the C51 distributional head), NOT multi-head
ensemble disagreement. True multi-head ensemble variance remains
a separate follow-up if/when a per-head ensemble is wired.
Slot 4 (velocity derivative of slot 3) becomes meaningful
automatically.
4. q_dir_abs_ref (slot 21) and q_abs_ref (slot 16) gain outlier
clamps before EMA update: clamped = min(raw, 10×current + 1) to
prevent single ±10⁵ Q-excursions from poisoning 20 epochs. The
+1 floor handles the cold-start case where current EMA is near 0.
Slot 21 is especially load-bearing because it feeds the Kelly
conviction denominator (q_range / q_dir_abs_ref).
ISV_DIM bump 22→23 changes the flat-param buffer size (w_isv_fc1
tensor [68] grows from [16,22] to [16,23] → +16 floats). Xavier
init at gpu_dqn_trainer.rs:13819 picks up the new dimension
automatically. Pinned allocs scale via ISV_DIM * size_of::<f32>()
expressions. No safetensors / checkpoint format currently encodes
ISV layout directly — checkpoint_state_dim/num_actions/hidden_dims
are the only hashed architectural fields — but the flat param
buffer content differs, so existing live checkpoints will require
rebase. Acceptable for the current pre-production dev state.
Tests: workspace cargo check --workspace --tests passes clean. Ran
magnitude_distribution smoke 3 times locally (RTX 3050 Ti); health
in HEALTH_DIAG now tracks negative Sharpe regime (0.50→0.38
trajectory over 20 epochs with mean sharpe_raw=-7) rather than
climbing monotonically as in the audit's train-mdh86 logs
(0.50→0.64 against Sharpe +34→-67). The magnitude_distribution
eval-dist H10 gate sometimes passes, sometimes fails depending on
the training seed — the pre-existing H10 flakiness persists. The
pre-existing 14 ml-lib test failures are unrelated (OFI features
missing / production profile config mismatch / test_batch_size
test environment issues — all fail on HEAD as well).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Branch worktree-agent-ace613af, 3 commits (f47af9d2 / 82dca76d / 5f95cad4).
1. DQN::load_from_safetensors is no longer a no-op. Adds
BranchingDuelingQNetwork::load_from_named_slices (D2D memcpy,
validates names + shapes, errors on drift). Adds
NoisyLinear::{weight_mu_mut, bias_mu_mut} for in-place mu restore.
Rewrites DQN::load_from_safetensors to actually restore weights;
handles both plain and `trending__`-prefixed checkpoint layouts.
Resyncs target network. Un-ignores the ensemble adapter's
test_dqn_checkpoint_round_trip (now deterministic with NoisyNet
disabled).
2. New post-hoc IG diagnostic CLI at crates/ml-explainability/src/bin/
ig_diag.rs. Gated by Cargo feature `ig-diag-cli` to avoid cyclic
dep (ml already depends on ml-explainability). CLI args:
--checkpoint --states auto|PATH --feature-names --num-steps
--output --auto-samples --seed. Forward target: mean(Q[direction,
0..4]) → V(s) via dueling identity; NoisyNet disabled → deterministic.
Fxcache reader inlined (~80 LOC) to avoid ml-crate dep.
Integration test measured 1.6% IG completeness error at
num_steps=64.
3. Latent bug caught while wiring checkpoint loading: ensemble DQN
adapter tests used vec![0.1; 56] but STATE_DIM=96, so cuBLAS
gemm_ex was reading 40 bytes of uninitialised memory past the
CudaSlice. That was the source of flaky argmax in
test_dqn_adapter_deterministic. Replaced hardcoded 56 with
ml_core::state_layout::STATE_DIM in 3 test sites.
Tests (ml-dqn --lib): 270→274 pass / 16→15 fail (+4 pass, -1 fail).
All new tests pass. Remaining 15 failures are pre-existing flaky
tests (NoisyNet non-determinism, small state_dim GPU alloc edges)
not introduced by this work.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The DQN inference adapter tests hardcoded `vec![0.1; 56]` as the
feature vector, but the DQN model's first shared layer expects
STATE_DIM=96 inputs. cuBLAS gemm_ex was reading 40 elements past the
end of the 56-allocated CudaSlice — uninitialized memory that
happened to give consistent-enough values for the deterministic test
to pass on the pre-change heap layout, and for other tests not to
notice the out-of-bounds read.
Surfaced by the Part 1 checkpoint-load work: adding the CUDA_LOCK
mutex and a new weight_mu_mut method to NoisyLinear shifted
allocation patterns enough that the uninitialized tail now reads
different values between the two predict() calls in
test_dqn_adapter_deterministic, flipping the argmax.
Replace all three `vec![0.1|0.3; 56]` occurrences with
`vec![...; ml_core::state_layout::STATE_DIM]` so the tests actually
exercise the model with in-bounds memory.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds `ig_diag` binary under ml-explainability/src/bin/ that runs
Integrated Gradients on a trained DQN safetensors checkpoint and
writes a per-feature attribution report as JSON. Designed for offline
model inspection, not the inference hot path.
Forward target: mean(Q[direction, 0..4]), which simplifies to V(s)
under the dueling identity (mean of centered advantages is zero by
the identifiability constraint). Smooth, no argmax discontinuity,
well-posed for IG.
Regime-head handling: loads only the `trending__`-prefixed weights
(matches RegimeConditionalDQN::load_from_merged_safetensors). Full
multi-head attribution is a future extension.
CLI args:
--checkpoint PATH safetensors file
--states auto|PATH `auto` samples from test_data/feature-cache/
*.fxcache; otherwise a JSON file containing
{ "states": [[f32; STATE_DIM], ...] }
--feature-names PATH JSON array of STATE_DIM names (optional)
--num-steps N IG Riemann steps (default 50)
--output PATH output JSON (default ig_report.json)
--auto-samples N fxcache sample count (default 16)
--seed N LCG seed for reproducibility (default 42)
Output JSON schema:
{
"schema_version": 1,
"checkpoint", "num_steps", "state_dim", "num_states",
"forward_target", "regime_head",
"features": [ { "name", "mean_abs", "stddev", "mean_signed" } ],
"top_10_by_mean_abs": [...],
"completeness": {
"worst_relative_error": f64,
"per_state": [ { "state_idx", "sum_attributions",
"f_input_minus_f_baseline", "relative_error" } ]
}
}
NoisyNet is disabled on both the Q-network and target network before
IG runs so attributions are deterministic (same checkpoint + states +
num_steps = bit-identical attributions).
Gated behind the `ig-diag-cli` Cargo feature (optional Cargo binary
feature, not a runtime flag) because the binary depends on ml-dqn.
Cannot depend on `ml` due to a cyclic dependency (ml already depends
on ml-explainability). To avoid pulling in the heavy `ml` crate, the
fxcache header parser is reimplemented inline in the binary (~80 LOC,
matches ml/src/fxcache.rs byte-for-byte for v4 files).
Tests:
- tests/ig_diag_cli_integration.rs: end-to-end test that builds a
DQN, saves a checkpoint, writes a states JSON, invokes the binary
via std::process::Command, parses the output, asserts schema +
completeness axiom (worst relative error < 5%). Gated with
#[cfg(all(feature = "cuda", feature = "ig-diag-cli"))] + #[ignore]
because it needs CUDA + the binary pre-built.
Build/run:
cargo build --release -p ml-explainability \
--features ig-diag-cli --bin ig_diag
cargo test -p ml-explainability --features ig-diag-cli \
--test ig_diag_cli_integration -- --ignored --nocapture
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
`DQN::load_from_safetensors` was a documented no-op that only checked
file existence and returned Ok(()). Real weight restoration happened
only via the fused trainer's params_buf path; any caller loading a
checkpoint via the DQN struct itself (e.g., DqnInferenceAdapter::
from_checkpoint used by the ensemble) got a fresh untrained DQN with
no error raised — a silent production bug.
Adds BranchingDuelingQNetwork::load_from_named_slices (symmetric
counterpart of existing named_weight_slices) using the same D2D
memcpy primitive as copy_weights_from. Validates names AND shapes;
returns Err on mismatch. Wired into DQN::load_from_safetensors to
actually restore weights from disk, handling both prefix-less (single
head) and `trending__` prefix (regime-merged) safetensors layouts.
Target network is resynced in lockstep so inference and TD targets
see the same restored weights.
Adds NoisyLinear::{weight_mu_mut, bias_mu_mut} for in-place D2D
restore of mu params during checkpoint load.
Tests:
- branching::tests::test_load_from_named_slices_round_trip (happy path)
- branching::tests::test_load_from_named_slices_rejects_extra_keys
(arch-drift safety)
- branching::tests::test_load_from_named_slices_rejects_shape_mismatch
- dqn::save_load_tests::test_dqn_load_from_safetensors_round_trip
(full end-to-end safetensors save+load, #[ignore] for CUDA gate)
- dqn::save_load_tests::test_dqn_load_from_safetensors_missing_file
Also un-ignores the ensemble adapter's
`test_dqn_checkpoint_round_trip` test (was ignored pre-fix because
load_from_safetensors silently dropped weights). Disables NoisyNet
on both sides for deterministic argmax comparison. Adds CUDA_LOCK
mutex to serialize the adapter tests that share a CUDA stream via
the SHARED_CUDA OnceLock.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Diagnostic-only audit of all 22 ISV slots using train-mdh86 HEALTH_DIAG
(20 epochs, same oscillation pattern as train-rq6n8). Identifies three
dead writers (slots 2, 3, 4), one strongly anti-correlated signal
(slot 12 learning_health, r=-0.765 vs Sharpe), sample-0 bias in four
regime slots (8-11), and EMA-pollution risk on the Q-scale EMAs
(16, 21) that feed Kelly conviction + C51 bin weighting.
Produces a per-slot table with writer/reader citations and an ordered
fix list. Hypothesises the observed +34 → -67 Sharpe swing is driven
by health mis-reporting "improving" while policy diverges, q_dir_abs_ref
contamination collapsing Kelly, and the zero-writer TD-error EMA
disabling micro-reward regime awareness.
No code changes — reconnaissance only.
Supervised Mamba2's dropout_rate field was configured and stored but
never applied — comments said "simulated" but the arithmetic was
absent. An operator tuning dropout_rate upward got zero regularisation.
New dropout_kernel.cu with a forward-only Bernoulli dropout (Philox-
seeded, deterministic, in-place). New build.rs matching ml-dqn's
pattern. Applied in forward_with_gradients (training path) only; the
`forward()` path used by validate/predict/SPSA stays deterministic so
SPSA's ±ε finite-difference estimator is not destabilised.
NOT a DQN fix: DQN has its own regime_dropout kernel in
cuda_pipeline/experience_kernels.cu and its own native mamba2_step
in gpu_dqn_trainer.rs; DQN does not call into Mamba2SSM. This commit
affects only the supervised Mamba2 trainer and its hyperopt adapter.
Tests: determinism, eval-mode identity, ctr-increment divergence.
DQN smoke tests (magnitude_distribution, multi_fold_convergence) are
unaffected by this change — ran them for regression assurance only.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Two ghost-feature fixes from the pre-L40S cleanup audit (tasks #66
and #68 tracked internally).
### #66 — delete apply_accumulated_gradients (dead code from removed path)
The agent-audit confirmed this function is residue from a prior
Candle-gradient-tracking training path that was REMOVED (see the
now-deleted test crates/ml/tests/test_var_source_gradients.rs which
was `#[ignore]`'d with the comment "Candle gradient tracking removed
-- DQN/PPO use custom CUDA backward passes"). Evidence:
- `DQN::optimizer: Option<GpuAdamW>` is always `None` — never
initialised anywhere in the codebase.
- `DQN` uses `OwnedGpuLinear` + `NoisyLinear` with standalone
`CudaSlice<f32>` BY DESIGN (see comment at branching.rs:988-989
"this layout exists to avoid a GpuVarStore intermediate"). There
is no GpuVarStore to feed GpuAdamW.
- Zero external callers for `DqnTrainer::apply_accumulated_gradients`,
`DQN::apply_accumulated_gradients`, `RegimeConditionalDQN::
apply_accumulated_gradients`, or the various `optimizer_vars()`
wrappers.
- The only test exercising this path was `#[ignore]`'d with the
"Candle gradient tracking removed" rationale.
- Production training goes through the fused CUDA trainer
(`trainers/dqn/fused_training.rs`) which applies gradients into a
flat `params_buf` — a separate, live path.
Deleted: 6 functions across 3 files + the stale test. Preserving a
ghost that has zero callers, zero initialisation path, and a design
direction explicitly chosen AWAY from its premise is not "keep and
wire" — it's accumulating fiction. Per feedback_no_functionality_
removal.md the rule preserves FUNCTIONAL features; this wasn't one.
### #68 — TFT honest error message
Audit found TFT is architecturally incompatible with GpuAdamW in its
current form (not a wiring gap, an architectural absence):
- `TemporalFusionTransformer` has no GpuVarStore, no `parameters()`,
no `named_parameters()` accessor
- Internal layers use `StreamLinear` which has NO `backward()`
- `TFTModel` trait only exposes `forward()`, `get_config()`,
`clear_cache()` — no gradient accessor
- `TemporalFusionTransformer::train()` runs forward + accumulates
loss but never calls backward or optimizer step
- `TrainableTFT` adapter's `backward()` explicitly returns "not
supported — use TFTTrainer::train() instead"
The previous error string "TFT optimizer not yet migrated to
GpuAdamW" implied 99% done and a simple constructor wiring would
finish it. Actual gap: GpuVarStore threading through 5+ layers +
StreamLinear→GpuLinear migration + backward ops for softmax
attention / layer norm 3D / quantile monotonicity chain / stack +
trait extension for var_store accessor + train-loop gradient
assembly. ~3-7 day dedicated architectural project.
New error message states this explicitly so no one wastes time
chasing an "almost migrated" fiction. Full-lift tracked separately.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Branch worktree-agent-aadd27f0, commit 9b27428de. Replaces the stub
{value:0.5, confidence:0.0} with dispatch via Arc<dyn
ModelInferenceAdapter>, wrapping any of the 10 concrete per-model
adapters in crates/ml/src/ensemble/adapters/. prediction_value
derived from inner direction∈[-1,1] via (d+1)/2 bullish probability.
confidence forwarded from inner adapter (softmax-max / quantile IQR
per model), clamped [0,1]. Returns Err (not fake 0-confidence) when
inner is_ready=false, inner predict fails, output non-finite, or
features empty.
build_production_strategy signature now Vec<(String, Arc<...>)>; sole
caller ml_strategy_engine.rs passes empty vec (not 10 ghost adapters)
with a note to wire real from_checkpoint when backtesting-side
checkpoint loading is ready.
# Conflicts:
# crates/ml/src/ensemble/model_adapter.rs
The adapter's `predict` returned
`{ prediction_value: 0.5, confidence: 0.0 }` as a "neutral" stub.
Downstream ensemble filtering dropped any 0-confidence prediction,
so the adapter silently contributed nothing -- a stub that survived
only because the caller discarded it.
Now: `EnsembleModelAdapter` holds an
`Arc<dyn ModelInferenceAdapter>` and dispatches `predict` to the
wrapped per-model GPU adapter (`DqnInferenceAdapter`,
`PpoInferenceAdapter`, `TftInferenceAdapter`, `Mamba2InferenceAdapter`,
`LiquidInferenceAdapter`, `KanInferenceAdapter`,
`XlstmInferenceAdapter`, `TggnInferenceAdapter`,
`TlobInferenceAdapter`, `DiffusionInferenceAdapter`). The inner
adapter already runs a full forward pass on its model and returns a
normalized `(direction, confidence)` pair; the bridge maps
`direction in [-1, 1]` -> `prediction_value in [0, 1]` via
`(direction + 1) / 2` to match `MLPrediction`'s bullish-probability
contract (> 0.5 = bullish), clamps confidence to [0, 1], and
propagates `metadata.latency_us` as the inference latency.
The bridge returns `Err` (never a faked 0-confidence success) when
the inner model reports `is_ready() == false`, the inner `predict`
fails, the output is non-finite, or the feature slice is empty.
`build_production_strategy` no longer fabricates ten zero-confidence
ghost adapters. It now accepts
`Vec<(String, Arc<dyn ModelInferenceAdapter>)>` -- the caller owns
real model construction (checkpoint loading, device selection). The
only current caller (backtesting service `MLPoweredStrategy::new`)
passes an empty vec; that yields an empty ensemble, which is an
honest "no models loaded" signal rather than ten stubs that exist
only to be filtered.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
services/trading_service/src/services/risk.rs compute_sharpe_ratio /
compute_sortino_ratio previously returned `0.0` as an
"insufficient-samples" fallback. Plain zero is indistinguishable
from a legitimate "Sharpe happened to equal 0" result; callers
gating on `sharpe > threshold` silently pass/fail on the fallback,
and downstream monitoring logs it as a real value.
Now: returns `Option<f64>` — `Some(value)` for valid samples,
`None` when:
* returns.len() < MIN_RETURN_OBSERVATIONS (threshold at line 30
unchanged — only the return-shape changes)
* daily_std < 1e-12 (degenerate zero-volatility series; ratio
mathematically undefined, not zero)
* downside_count < 2 (Sortino only; too few sub-risk-free
observations to estimate downside variance)
* downside_dev < 1e-12 (Sortino only)
Production caller (get_risk_metrics, the only call site) updated:
* On Some/Some: debug-log as before
* On either None: WARN-log with `[insufficient-samples]` tag,
formatting each ratio as "None" or "{:.4}" so operators can
distinguish missing data from a real zero
* Protobuf RiskMetrics { sharpe_ratio, sortino_ratio } is a
non-optional `double` in risk.proto — export `f64::NAN` rather
than a fake 0, so consumers can detect the undefined case
without silently failing threshold gates.
Tests: each function now has happy-path `Some(_)`,
insufficient-samples `None`, and empty-input `None` cases;
existing `_zero_volatility` / `_no_downside` tests re-asserted
against `None` (previously asserted against 0.0).
compute_volatility and compute_current_drawdown retain `f64`
return — their 0.0 outputs are legitimate (no position → 0%
drawdown; constant returns → 0% volatility).
Callers touched:
- services/trading_service/src/services/risk.rs:570-596
(get_risk_metrics — only production caller)
- services/trading_service/src/services/risk.rs:1585-1671
(tests)
Cross-file grep (compute_sharpe|compute_sortino|RiskServiceImpl::)
confirms no other callers in crates/, services/, bin/.
position_manager.rs line 772 references the function in a comment
only.
Compile: SQLX_OFFLINE=true CARGO_INCREMENTAL=0 cargo check
--workspace --tests — clean on risk.rs (pre-existing ml-crate
warnings unchanged).
Tests: cargo test -p trading-service --lib risk — 59 passed,
0 failed, including 2 new empty-input tests.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Branch worktree-agent-a510b4c9, commit 6cb2257163. Caps any branch's
weight-gradient L2 norm at `num_branches × median(branch_norms)` via a
new single-pass reduce+rescale CUDA kernel, wired into both the
ungraphed fallback and the `adam_grad_child` graph capture path.
Motivation: L40S train-mdh86 (terminated at epoch 20 after Sharpe
regression) showed grad_ratio_mag_dir ∈ {14793, 11934, 12858, 16406,
15141} for the first five epochs, then collapsed to 111× in a single
step at epoch 7 and destabilised learning for the remaining epochs.
HEALTH_DIAG forensics at /tmp/l40s_diag/health.log confirmed the
imbalance as the probable trigger.
Post-fix grad_ratio_mag_dir on the equivalent local smoke: 55, 78, 35,
11, 10 — a 268–1503× reduction in ratio magnitude.
Architectural rule (no tuned knobs, per feedback_adaptive_not_tuned.md):
cap = num_branches × median(branch_norms)
num_branches=4 is the factored-action axis count (architectural)
median is a per-step statistical reference (adaptive)
product is fully signal-driven
Smokes (local RTX 3050 Ti):
magnitude_distribution: PASS — EVAL_DIST Q=0.153 H=0.255 F=0.592,
3 internal folds Sharpe +16.7 / +38.8 / +49.1
multi_fold_convergence: PASS — 3/3 folds Sharpe +57.8 / +55.6 / +119.3
Expected to resolve the L40S regression when validated with the next
argo-train.sh run.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Caps any branch's weight-gradient L2 norm at num_branches × median
(median across the 4 branches' norms). Scales the offending branch's
gradient down to the cap; healthy branches pass through unchanged.
Fixes the observed pathology in L40S train-mdh86: grad_ratio_mag_dir
was 15k–26k× for 6 consecutive epochs, then collapsed to ~100× in a
single step at epoch 7 and destabilised learning (Sharpe flipped +34
→ -67, never recovered). Symmetric per-branch capping at
`num_branches × median` prevents the swing at both ends without
requiring a global ratio bound.
No tuned knobs: `num_branches = 4` is architectural (factored action
space: direction × magnitude × order × urgency), `median_branch_norm`
is a per-step statistical reference that tracks the current gradient
regime, and the product is fully adaptive. Per
feedback_adaptive_not_tuned.md, the only static value is the
architectural axis count; medians and derived caps are signal-driven.
Implementation — two CUDA kernel launches in
`branch_grad_balance_kernel.cu`:
branch_grad_norm_reduce: grid=(4,1,1), block=(256,1,1). One block
per branch; sum-of-squares via shared-mem
tree reduce writes `branch_norms_dev[4]`.
No atomicAdd (one-block-per-branch, single
writer per slot).
branch_grad_rescale: grid=(max_blocks, 4, 1), block=(256,1,1).
Each block caches the 4 branch norms into
shared memory, computes the median via a
5-comparator sorting network + two-element
average (branch-deterministic, no reduction
primitive), derives the 4 per-branch scales
`scale[d] = min(1, 4×median/norm[d])`, then
threads multiply their slice element by the
owning branch's scale. No atomicAdd (each
thread writes one distinct element).
Insertion point: inside the `adam_grad_child` graph between the aux
phase and `compute_grad_norm_for_adam`, so Adam's global clip and the
Adam update both observe the rebalanced gradient. Also wired into the
ungraphed fallback paths so no code path can skip the cap. The kernels
have fixed launch configs, no host syncs, no dynamic allocations —
safe to capture.
Per-branch slice metadata (starts/lens for each of the 4 contiguous
4-tensor branch slices in `grad_buf`) is precomputed from
`compute_param_sizes` at trainer construction and uploaded once to
device i32 buffers, matching the existing `grad_decomp_kernel` layout
convention.
Smoke tests (local RTX 3050 Ti, 4 GB):
magnitude_distribution: PASS (MAG_DIST Q=0.637 H=0.114 F=0.249,
EVAL_DIST Q=0.153 H=0.255 F=0.592)
multi_fold_convergence: PASS (3/3 folds produce best-checkpoint;
fold Sharpes +57.8 / +55.6 / +119.3)
grad_ratio_mag_dir trajectory (mag_dist smoke, first fold, first 5
epochs) — pre-fix values from /tmp/l40s_diag/health.log (L40S
train-mdh86):
pre-fix: 14793, 11934, 12858, 16406, 15141 (×1000 regime)
post-fix: 55, 78, 35, 11, 10 (×10-100 regime)
Three+ orders of magnitude reduction. The residual ratio can still
exceed `num_branches = 4` when the direction branch's norm sits below
the median — the cap bounds each branch's absolute norm (≤ 4×median),
not the pairwise ratio, by design (direction-outlier smallness is a
separate pathology that would be masked by a ratio bound).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Branch worktree-agent-a7a1d9df, commit 746b8b675. Documents the
diff between TLOB's 51-dim feature set and Foxhunt's current 20-dim
OFI + 42-dim market features + 12-dim MicrostructureState.
Key findings:
- 23 of TLOB's 51 slots are placeholders (hardcoded constants, sine
waves, time_since_update=0.5). Unusable as-is.
- 12 duplicate Foxhunt's existing slots (VPIN, Kyle's λ, depth
imbalance variants). Already persisted.
- 10 initially "novel," collapsing to ~4 after removing intra-TLOB
redundancy.
- Critical insight: most of those 10 are ALREADY COMPUTED in
Foxhunt's own ofi_calculator.rs::MicrostructureState::snapshot()
at slots [0..10] — realized variance, Hawkes intensity, weighted
book pressure, spread dynamics, aggression ratio, queue-depletion
asymmetry, order-count flux, intra-bar momentum, regime score,
OFI trajectory. They are discarded before reaching fxcache.
Bonus production bug flagged: OFI slots [18..20) (ofi_acceleration,
toxicity_gradient) are written to fxcache but never consumed by
the OFI embed kernel (experience_kernels.cu:6148-6173 reads only
[0..18)). Dead data every bar.
Phase B (persist the already-computed features + fix the [18..20)
gap) is a vastly smaller scope than importing TLOB would have been.
Phase B agent dispatched separately.
Compares the 51-feature TLOBFeatureExtractor (crates/ml-supervised/src/tlob/
features.rs + mbp10_feature_extractor.rs) against Foxhunt's 20-slot OFI
vector persisted in .fxcache.
Findings: of TLOB's 51 slots, 23 are placeholders (sine waves / hardcoded
constants), 12 duplicate existing OFI or 42-dim market features, and only
~10 carry genuinely novel information. Meanwhile the existing
MicrostructureState struct in ml-features already computes 10 features
(realized variance, Hawkes intensity, weighted book pressure, spread
dynamics, aggression ratio, queue-depletion asymmetry, order-count flux,
intra-bar momentum, regime score, OFI trajectory) that are dropped before
reaching fxcache — these dominate the TLOB novel candidates and require
zero new math.
Phase B plan (drafted inline): bump OFI_DIM 20→28, bump FXCACHE_VERSION
4→5, prioritize persisting the 10 Foxhunt-internal MicrostructureState
features before any TLOB-derived additions, sweep all hard-coded 18/20
constants across CUDA kernels + gpu_dqn_trainer.rs, and rely on the
ensure-fxcache Argo step for cache regeneration.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The TLOBTrainer in this file was:
- Never referenced by any caller in crates/ /services/ /bin/
(grep confirms zero hits beyond the string literal "TLOB" in
ml-ensemble's adaptive weight maps, which does not depend on the
trainer type).
- save_checkpoint and serialize_model logged "saving" but wrote
zero bytes, then immediately called std::fs::metadata.len() and
std::fs::read() on the missing file → ENOENT every time. Ghost
feature masquerading as a trainer (flagged during the pre-L40S
bulk-TODO sweep, commit 155c079fa).
- Depended on an ONNX-Runtime path in crates/ml-supervised/src/tlob/
transformer.rs (Environment / Session / Value), incompatible with
Foxhunt's native cudarc + cuBLAS GPU path used everywhere else.
TLOB feature-extractors in crates/ml-supervised/src/tlob/{features,
mbp10_feature_extractor,analytics,performance}.rs ARE retained —
they produce a 51-dim order-book feature representation that is
potentially worth integrating into the DQN data pipeline as a richer
alternative to our current 20-dim OFI. That integration is tracked
as a separate follow-up, NOT attempted here.
Also removes:
- `pub mod tlob;` in crates/ml/src/trainers/mod.rs:80
- `pub use tlob::{TLOBHyperparameters, TLOBTrainer, TLOBTrainingMetrics};`
re-export in trainers/mod.rs:109
Full workspace check: cargo check --workspace passes, no new warnings.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
migrations:
- 001_trading_events.sql, 003_audit_system.sql: the hard-coded
node_id literals (`trading-node-01`, `audit-node-01`,
`ml-node-01`, `system-node-01`, `change-tracker-01`) are
overridden per-deployment by later migrations rather than read
from the environment. Describe that in the inline comment.
- 004_compliance_views.sql: `generate_compliance_report` is a log
stub — actual report generation is performed by the compliance
service. Say so explicitly.
services:
- ml_training_service/tests/orchestrator_225_features_test.rs: the
empty `#[ignore]`d placeholder for the 225-feature orchestrator
loader has been removed; it held no assertions and only tracked
a TODO (feedback_no_stubs.md).
- trading_agent_service/src/service.rs: portfolio volatility uses
the diagonal-only approximation because cross-asset return
correlations are not maintained in this service. Document that.
- trading_service/src/services/risk.rs: `get_risk_metrics` uses
`calculate_marginal_var` + asset-class fallback; describe why
`calculate_comprehensive_var` is not wired at this boundary.
- trading_service/tests/auth_comprehensive.rs: delete the entire
commented-out legacy BackupCodeValidator test block — the old
`generate_backup_codes` / `store_backup_code` /
`verify_backup_code` surface no longer exists, and MFA
integration tests already cover the new API.
testing:
- harness/grpc_clients.rs: no BacktestingServiceClient proto
exists; reword the stale TODO import line.
- chaos/*: reword the family of "TODO: Implement ..." stubs as
"Currently a no-op / synthetic result" descriptions so readers
know exactly how much of the chaos framework is live.
- compliance_automation_tests.rs: delete the file; it was a giant
/* ... */ block referencing a nonexistent compliance module
and was not wired into any Cargo target.
- framework.rs: describe why `setup()` uses `println!` instead of
`tracing_subscriber` (tracing_subscriber is not a dep of this
integration crate).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- ml/tests/dqn_training_pipeline_test.rs: the inline TODO speculated
about a future `load_checkpoint` hook; loader round-trip coverage
already lives in dqn_checkpoint_tests. Reword to point there.
- ml/tests/ppo_lstm_training_loop_tests.rs: the assertion on
`hidden_state_manager.is_some()` is the public-surface proxy for
"LSTM path active"; deeper introspection isn't exposed. Say so.
- ml/tests/ppo_recurrent_integration_tests.rs: the test is already
`#[ignore]`d; rewrite the inline TODO as a description of the
missing `from_varbuilder` constructors on LSTMPolicyNetwork /
LSTMValueNetwork.
- risk/risk_engine.rs: VarEngine receives a default asset-class
config because the schema-to-config conversion is not wired.
Reword the TODO to describe that plainly.
- trading_engine/types/errors.rs: `common::ConversionError` does
not exist; keep ConversionError local and drop the aspirational
re-export comment.
- trading_engine/tests/audit_persistence_tests.rs: describe why
the query assertion only checks the Ok shape (row-to-event
mapping not wired) rather than pointing at a nonexistent line
number.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Branch worktree-agent-af5e15a7, commit 952302149. Full GPU IG
implementation — replaces the compute_gpu stub in
integrated_gradients.rs with a real kernel-driven loop. New
ig_kernels.cu (interpolate_input + perturb_dimension), new
crates/ml-explainability/build.rs (matches the crates/ml/build.rs
nvcc pattern for sm_89 L40S / sm_90 H100). Scratch buffers
(d_interpolated, d_x_plus, d_x_minus) allocated once outside the
step loop and reused across num_steps × num_features perturbations.
forward_fn scalar output pulled via GpuTensor::to_scalar; no
full-tensor dtoh inside the hot loop. No atomicAdd, deterministic
launch config.
4/4 existing CPU tests pass unchanged. New GPU smoke test
(test_ig_compute_gpu_linear_model) validates completeness axiom
within 1% and GPU↔CPU parity within 5% on local RTX 3050 Ti.
Motivation: next-run diagnosis of the L40S train-mdh86 regression
(see /tmp/l40s_diag/health.log). IG will let us compare per-feature
attributions at checkpoints ep 6 (healthy, Sharpe +34), ep 8
(pre-collapse, grad_ratio_mag_dir=115 down from 23754), and ep 10
(Sharpe regressed to -66) to determine whether the magnitude-vs-
direction gradient imbalance is driven by genuine feature weighting
or by a gradient-shape pathology that a multi-task balancer can fix.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- ml/Cargo.toml: describe why ndarray's blas feature stays disabled
(CI compile pool has no libopenblas-dev; GPU cuBLAS handles the
hot path) rather than labelling it a TODO.
- dbn_sequence_loader.rs: Wave C regime-detection branch emits zeros
when only that flag is enabled; the live feed lives under WaveD.
Reword from "TODO Wave C" to a description of that superseding.
- ensemble/adapters/{liquid,tggn,tlob,xlstm}.rs: checkpoint loading
currently constructs fresh GpuLinear weights and logs a runtime
warning so the ignored checkpoint path is visible. No new
functionality, just reword the repeated TODO.
- ensemble/model_adapter.rs: the neutral-prediction adapter is
guarded by the ensemble's confidence threshold (0.0 = filtered),
making it a no-op stub used for end-to-end wiring. Describe that
contract explicitly.
- hyperopt/adapters/tft.rs: the input_dim=51 line is load-bearing
(5 static + 10 known + 36 unknown matches the CUDA layout). Drop
the "should be 42" aside.
- trainers/tft/trainer.rs: initialize_optimizer returns Err until
GpuAdamW is wired; the `let _ = &self.optimizer;` anchor in the
training loop keeps the migration target visible.
- trainers/tlob.rs: save_checkpoint / serialize_model both surface
errors until GpuVarStore safetensors serialisation lands. Mark
the gap declaratively rather than as a TODO.
- transformers/mod.rs: only AttentionMask is implemented in the
attention submodule; drop the aspirational re-export list.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The three TODO comments claiming \"TrainingPipelineConfig needs a new
struct\" were stale — the config already exposes `storage.base_directory`
(test_process_features_full_workflow_success already uses it). Wire
up the two previously-neutered tests so they actually exercise their
intended behaviour:
- `test_pipeline_creation_storage_dir_is_file_fails` now sets
base_directory to a regular file and asserts pipeline creation
returns Err (create_dir_all on a file path fails with ENOTDIR).
- `test_process_features_dataset_not_found` now points storage at a
fresh tempdir so the NotFound assertion runs against an isolated
state rather than the default on-disk location.
- The inline \"fixed from TODO comment\" note in the third test is
replaced with a plain description of why the tempdir override is
needed.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- common/sqlx_test.rs: the entire module was a single /* ... */ block
behind a TODO because the identifier types (`OrderId`, `TradeId`,
`Symbol`, `AccountId`, `OrderSide`) do not derive `sqlx::Type`. The
file has been a dead placeholder behind `#[cfg(all(test,
feature=\"database\"))]` for a long time; delete it and drop the
`mod sqlx_test;` declaration.
- data/brokers/mod.rs: re-enable the `pub type BrokerAdapter = Box<dyn
BrokerClient>;` alias — the trait is already implemented by
`InteractiveBrokersAdapter` and re-exported from the module. Delete
the commented-out `BrokerFactory::create_client` block: it
referenced `ICMarketsClient`, which does not exist in this crate.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- ml-dqn/dqn.rs: `apply_accumulated_gradients` is a scaffolding method
whose real optimizer step lives in the fused CUDA trainer. The
`grads` map was already being dropped silently; reword the comment
to describe that split explicitly (incidental: see trainer path for
the live gradient application).
- ml-features/mbp10_loader.rs: strip the "TODO optimize with binary
search" parenthetical from the docstring. Linear search over the
sorted snapshot slice is the intended behaviour for current call
sites.
- ml-hyperopt/optimizer.rs: `optimize_two_phase` short-circuits after
Phase A because `DQNTrainer` is not `Clone`. Describe that limit
and point callers at `optimize_parallel` (which requires `M: Clone`)
rather than a hypothetical Phase B.
- ml-checkpoint/signer.rs: `fetch_key_from_vault` is currently an
env-var resolver. Reword to say so plainly — no Vault client is
wired into this crate, production uses K8s secrets injected as env.
- backtesting/dbn_replay.rs: `DbnReplayEngine::from_bytes` remains an
Err stub because `DbnParser` is gated behind the `databento`
feature which this crate does not enable. Replace the pseudocode
block with a declarative comment.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Implements the CUDA kernels (`interpolate_input`, `perturb_dimension`)
and wires `compute_gpu` in `integrated_gradients.rs` to run the full
IG algorithm on-device. Replaces the stub that previously returned
`MLError::ModelError("IG GPU kernels not available: cubins not yet
wired")` and fell back to CPU.
Algorithm: for each of `num_steps` interpolation points along the
baseline->input path, compute central-finite-difference gradients for
all `num_features` dimensions via two GPU forward passes per feature.
Single cubin (`ig_kernels.cubin`) compiled via build.rs (following the
crates/ml/build.rs pattern — nvcc, `-arch=sm_\${CUDA_COMPUTE_CAP}`, O3,
f32) and embedded with `include_bytes!`. The `forward_fn` consumers
operate on `GpuTensor`, so no dtoh round-trips inside the inner loop —
only the scalar `[1]` tensor output of each forward pass is pulled to
host (via `to_scalar`) per gradient sample. The three scratch buffers
(`interpolated`, `x_plus`, `x_minus`) are allocated once outside the
step loop and reused across all steps and features. No atomicAdd —
the kernels are trivial 1-D element-wise writes.
Tests: existing CPU tests pass unchanged. Added GPU smoke test
`test_ig_compute_gpu_linear_model` (gated `#[cfg(feature = \"cuda\")]`
+ `#[ignore]`) that builds a linear model as a `GpuTensor`-native
forward (elementwise mul + mean), verifies the completeness axiom
within 1%, and cross-checks GPU attributions against the CPU path
within 5%. Passes locally on RTX 3050 Ti (sm_86).
Removes 3 TODO markers and the embedded `_IG_CUDA_SRC` const that
were awaiting this work, along with the `#[allow(unused_variables)]`
stub attribute on `compute_gpu`.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- ml-explainability/integrated_gradients.rs: the GPU IG path is a
stub that returns an error so callers fall through to CPU. Drop
the three TODO markers and describe the situation declaratively —
the reference CUDA source is kept as a follow-up anchor.
- ml-supervised/mamba/mod.rs: dropout in both inference and training
paths is simulated via the `1 - dropout_rate` scalar bake-in; no
dedicated GPU dropout kernel is wired on the supervised path. The
hidden-state carry-over in the SSD scan requires a 3-D narrow the
GpuTensor API does not expose, and inference only consumes the
last timestep, so the update is intentionally elided.
- ml-core/cuda_autograd/gpu_tensor.rs: the `cat` docstring claimed a
host round-trip; the implementation is already fully DtoD via
`memcpy_dtod` for dim=0 and per-row DtoD for dim>0. Rewrite the
comment to describe the real implementation.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
client_performance.rs, configuration_benchmarks.rs and
serialization_benchmarks.rs were wholly commented-out /* ... */
bodies with a `fn main()` placeholder and a TODO explaining that
either the types or the crate deps they referenced never existed.
They never produced measurements and benchmarks do not ship, so
delete them and drop their `[[bench]]` entries from Cargo.toml.
The remaining `encryption_performance` bench (auth token storage)
is kept as-is.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The non-interactive `login_with_credentials` path already speaks to
`AuthServiceClient` via gRPC. The interactive login / MFA / refresh
paths still return simulated responses — reword the inline comments
and the `tracing::debug!` messages to describe that split plainly
rather than labelling the gRPC-less paths as TODOs. The SECURITY
warn!() lines are untouched so the runtime signal remains.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Removes commented-out ProviderMetrics tests (struct was replaced by
ConnectionStatus long ago) and reword the Parquet-reader integration
tests so they stop claiming the reader is a "placeholder" — the Parquet
reader is fully implemented and surfaces `File::open` errors via
anyhow. Also tightens test_12_invalid_file_handling to assert the real
Err behaviour rather than the stale "Ok(vec![])" expectation.
No production code change; tests still compile and run under the same
ignore gates.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Part A continuation of the TODO sweep.
Deletes 6 commented-out test blocks that referenced the removed
`ProviderMetrics` struct (now replaced by `ConnectionStatus`). The
blocks sat behind `TODO: ... needs rewrite for new ConnectionStatus`
markers since the API change and were never revived. Per
feedback_no_todo_fixme.md, dead code stays deleted.
Also rewrites the module docstrings that named the outdated
migration, and removes the stale import-commented TODO at the top of
provider_error_path_tests.rs.
Rewrites the first of five aspirational "TODO: Once reader is fully
implemented, validate:" comments in real_data_integration_tests.rs
as a declarative note about the stub reader. The remaining four in
that file and the rest of the repo-wide TODO sweep are being done in
a parallel agent worktree.
Part A of pre-L40S cleanup.
1. Wire td_error batch mean into ISV scratch (gpu_dqn_trainer.rs):
`launch_loss_reduce` now runs the generic `c51_loss_reduce` kernel a
second time over `td_errors_buf` into `td_error_scratch_dev_ptr`.
ISV[2] (TD-error EMA in `isv_signal_update`) was previously reading
a zero-initialised scratch and accumulated a constant-zero signal.
This was a genuinely missing kernel writeback — the C51 loss kernel
was already emitting per-sample |TD-error| into `td_errors_buf`
(c51_loss_kernel.cu:1096), it just wasn't being batch-reduced.
Reuses the existing `c51_loss_reduce` (generic mean-reduction, single
block, deterministic) rather than adding a new kernel — no new CUDA
surface, no ABI change.
2. Remove 2 stale TODOs from batched_backward.rs docstrings that
described a migration that's actually complete:
- Module docstring said "dqn_backward_kernel (atomicAdd path)
remains active" — the atomicAdd kernel has been removed; cuBLAS
backward is wired via launch_cublas_backward.
- `backward_full` docstring said "gated behind TODO" — the function
is actively called from the fused training step.
3. Rewrite 2 ISV scratch field comments as declarative: td_error_scratch
is now wired (as per change 1); ensemble_var_scratch remains
zero-initialised and its comment honestly describes that consumers
(ISV[3] and [4]) treat it as unavailable. Per feedback_no_todo_fixme.md,
replaces the TODO(isv) markers with declarative descriptions of
current behaviour. Future wiring is tracked in the plan, not in code
aspirational markers.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Composes the Kelly safety_multiplier from TWO orthogonal adaptive
signals instead of one:
safety = max(health_safety, conviction)
where:
health_safety = 0.5 + 0.5 × learning_health [training stability]
conviction ∈ [0, 1] [per-sample confidence]
Health measures training stability globally. Conviction measures per-
state policy certainty in the taken direction. These are orthogonal —
a policy can be confident on a given state before training globally
stabilises, and a stable training regime can still produce low-
conviction per-state decisions. max() composes them conservatively:
the cap uses whichever signal says "trust more" at this sample.
Bounded to [0.5, 1.0] by the health floor.
Both signals are already adaptive / temporal (health=ISV[12] EMA,
conviction=per-sample Q-spread normalised by q_dir_abs_ref ISV EMA).
No static tuning knobs. Per feedback_adaptive_not_tuned.md.
Motivation (per project_magnitude_eval_collapse_kelly_capped.md): at
typical smoke-test health=0.49, health_safety = 0.745 sits coincid-
entally on the Half/Full decoder boundary (abs_pos < 0.75). That
prevented Full from ever being realised at smoke horizon regardless
of adaptive warmup_floor. Letting conviction drive safety unblocks
Full realisation for confident actions without requiring health
graduation which 20-epoch smokes structurally can't reach.
Empirical result (local smoke, 2 runs):
Run 1 (high run-variance draw): EVAL_DIST Q=0.911 H=0.057 F=0.032
— still fails H10 eh+ef≥0.30
Run 2: EVAL_DIST Q=0.350 H=0.121 F=0.529
— PASSES all 5 assertions
— FIRST FULL SMOKE PASS SINCE 4-BRANCH
Previous best (before this commit):
(pre-safety-A, v5+adaptive-Kelly only): Q=0.325 H=0.675 F=0.000
— passed H10 at line 134 but failed Task 2.X line 153 (ef < 0.05)
The commit trades the reliable Half-dominance regime for a bi-modal
distribution that includes Full on many runs. Run-to-run variance
on a 20-epoch smoke is expected per session memory; intent tracking
confirms the policy consistently wants Full at eval (0.73-0.85 across
runs), so the gap is purely in realised cap, not policy learning.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Rewrites the plan_isv_buf comment from an aspirational TODO to a
declarative doc note describing the accepted design gap: backtest
validation intentionally zero-fills plan/ISV state positions [86..92)
because the backtest env kernel does not compute those training-time
introspective signals. The policy treats plan/ISV as advisory
features, so the train/val delta is tolerated in exchange for a lean
backtest env kernel.
Per feedback_no_todo_fixme.md: TODO/FIXME markers are forbidden;
rewrite as declarative production-ready prose or complete the work.
Agent worktree ff683470e. Replaces static warmup_floor=0.5 in
kelly_position_cap with an adaptive signal derived from the policy's
normalised direction Q-spread (q_dir_spread / q_dir_abs_ref clamped
[0, 1]). High conviction → high floor (trust policy to size up). Low
conviction → safety dominates. Fixes the root cause of EVAL_DIST
Quarter=1.000 for cold-start Kelly runs.
Agent's 3 smoke runs: 1 PASSED with EVAL_DIST Q=0.573 H=0.325 F=0.102
(Full above 5% threshold). 2 failed on direction hyper-variance
(pre-existing, see feedback_adaptive_not_tuned.md).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
# Conflicts:
# crates/ml/src/cuda_pipeline/experience_kernels.cu
# crates/ml/src/cuda_pipeline/gpu_backtest_evaluator.rs
# crates/ml/src/cuda_pipeline/gpu_experience_collector.rs
Agent worktree f9a8a5aa9. Adds a parallel metric reporting the policy's
intended mag_idx BEFORE Kelly/margin caps and before Hold/Flat dir_idx
forcing. Vindicates the Kelly cold-start hypothesis — first smoke
showed intent Full=0.599 while realised Full=0.000, confirming the
magnitude Q-head is learning and the cap is the sole blocker.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replaces static warmup_floor=0.5f in kelly_position_cap (trade_physics.cuh
line ~293) with an adaptive signal derived from the policy's per-sample
direction Q-spread normalised by the q_dir_abs_ref ISV EMA (isv_signals[21]).
High conviction -> high floor (trust policy at cold start). Low conviction
-> low floor (safety dominates). Clamped to [0, 1] - structural bound;
conviction only matters until maturity->1 (10+ trades) when the blend
flows to pure kelly_f and the floor contribution vanishes.
Wiring:
1. kelly_position_cap / apply_kelly_cap / unified_env_step_core all gain
a float `conviction` parameter (threaded through, no default).
2. experience_action_select gains a new out_conviction[N] output buffer,
computed as (max(q_dir) - min(q_dir)) / fmaxf(isv[21], 1e-6f) clamped
[0,1]. Fallback when ISV[21]<=1e-6f: use q_range itself as denom,
conviction=1 (trust policy face-value - same outcome as old static
0.5 at health=1, but from a real signal shape).
3. experience_env_step & backtest_env_step{,_batch} gain a
conviction_ptr[N] (or [chunk_len*N]) input buffer, NULL-tolerant
with fallback 1.0.
4. Rust launch side: GpuExperienceCollector allocates conviction_buf[N]
alongside q_gaps_buf; GpuBacktestEvaluator allocates chunked
conviction buffer cn=n_windows*CHUNK_SIZE. Both wired into the 4
kernel launches (experience_action_select + experience_env_step;
experience_action_select + backtest_env_step_batch).
The previous static 0.5 pinned cold-start cap to <=0.375*max_pos at
health=0.5 (safety_multiplier=0.75), which combined with the
`abs_pos < 0.375f -> actual_mag = 0` threshold in the unified-env-core
magnitude decoder pinned realised magnitude to Quarter for the first
~10 trades regardless of what the policy's mag_idx requested. Smoke
test EVAL_DIST=[1.0, 0.0, 0.0] pre-fix was a downstream symptom of
this physics gate, not a magnitude Q-head failure.
Per feedback_adaptive_not_tuned.md: no hard-coded numeric knobs.
Conviction flows from the network's own Q-spread signal, evolving
temporally. Per feedback_no_functionality_removal.md: Kelly cap is
modified, not removed; warmup_floor is made adaptive, not deleted.
Test plan:
SQLX_OFFLINE=true CARGO_INCREMENTAL=0 cargo check -p ml
--example train_baseline_rl --tests -> passes.
Smoke (magnitude_distribution, 20 epochs) shows:
[MAG_DIST] Quarter~0.62-0.70 Half~0.15-0.19 Full~0.13-0.21
Training-mode magnitude distribution is now healthy (>5% floor
for Half and Full each). Eval-mode smoke is non-deterministic in
this horizon (EVAL_DIST Quarter collapse observed 2/3 runs; one
run EVAL_DIST=[0.573, 0.325, 0.102]). Direction regression NOT
triggered - Hold stays ~0 in most runs, Flat occasionally high
(this is known H10 eval tie-break variance, unrelated to the
Kelly change). q_dir_abs_ref observed in ISV_DIR_MEANS:
~0.14-0.60 across runs - conviction signal is flowing.
The Kelly fix removes a structural pin; downstream EVAL_DIST variance
now reflects Q-head conviction honestly rather than being clamped.