Files
foxhunt/crates/ml-alpha/Cargo.toml
jgrusewski 7e7c8b5d90 feat(rl): R6 — replace LobEnv with kernel-driven GPU-pure env step
Replaces the flawed Phase F+G LobEnv trait + LobSimEnvAdapter +
MockLobEnv fixture with a GPU-pure env-interaction path.

DELETED:
- LobEnv trait (host-method-per-batch surface)
- MockLobEnv fixture (the toy-bandit pattern that hid the production defects)

ADDED:
- RlLobBackend trait (src/rl/reward.rs): narrow device-oriented
  surface (apply_snapshot, pos_and_market_targets_mut, pos_d,
  pos_bytes, step_fill_from_market_targets, n_backtests). Single
  purpose: break the ml-alpha ↔ ml-backtesting dep cycle. No
  mocking layer.

- 3 new GPU-pure kernels (cuda/):
  * extract_realized_pnl_delta.cu — reads pos.realized_pnl +
    position_lots from device Pos array, writes per-batch
    (reward delta, done flag), updates trainer's prev_* buffers.
  * apply_reward_scale.cu — element-wise rewards *= ISV[406].
  * actions_to_market_targets.cu — 9-action grid → market_targets
    {side, size} on device, reading current position_lots for
    conditional Flat-from-Long/Short.

- LobSimCuda impls RlLobBackend (ml-backtesting/src/sim/mod.rs)
  plus a new step_fill_from_market_targets entry that runs
  submit_market_immediate + step_pnl_track without the host-side
  targets-vec build. pos_and_market_targets_mut returns disjoint
  field borrows (&pos_d, &mut market_targets_d).

- IntegratedTrainer: 3 cubin includes, 3 module/function fields,
  launch_apply_reward_scale launcher, prev_realized_pnl_d /
  prev_position_lots_d / rewards_d / dones_d buffers,
  step_with_lobsim signature switched to RlLobBackend, body's
  Step 5 rewritten as kernel-driven (no per-batch host loop, no
  individual submit_action calls).

R6 PARTIAL — work R7 lifts:
- Thompson + log_pi stay host (R7 uses R4 kernels)
- mean_abs_pnl EMA stays host (R7 uses R3 ema_update_on_done)
- Advantage/return stays host (R7 uses R3 compute_advantage_return)
- 4 final DtoH copies for step_synthetic's host-slice signature
  (R7 lifts step_after_encoder_forward to device-buffer args)
The "DtoH the device rewards/dones at end" comment in
step_with_lobsim documents the boundary. After R7, hot path has
zero host loops other than the now-unused Thompson host loop
(which R7 retires in favour of rl_action_kernel from R4).

Tests:
- integrated_trainer_smoke.rs: rewritten — real LobSimCuda with
  synthetic book, one step_with_lobsim call, asserts finite losses
  + λs sum to 1. Smoke gate, not a convergence test.
- dqn_toy.rs, ppo_toy.rs: retired (empty stubs documenting
  rationale). Files preserved so rename history survives. The
  MockLobEnv toy-bandit pattern intrinsically couldn't catch the
  production defects #1, #2, #5 that motivated this rebuild.

ml-backtesting added as ml-alpha dev-dep (cycle-safe: production
direction is ml-backtesting → ml-alpha; dev edge only loads when
building ml-alpha's own tests/examples).

Build cache-bust v29. cargo check + cargo build for all R-phase
tests green (pre-existing heads_bit_equiv index-OOB persists).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-23 10:39:25 +02:00

61 lines
2.3 KiB
TOML

[package]
name = "ml-alpha"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
description = "CfC perception + multi-horizon alpha heads (Phase A foundation for CfC+PPO greenfield)"
publish = false
# Phase A scope: snapshot-level CfC trunk + 5 multi-horizon BCE heads,
# pre-compiled CUDA cubins, GPU-resident weights. The hard validation gate
# (CfC vs Mamba2 AUC at every horizon) sits at the end of Phase A. Phase B
# (PPO) extends this crate; live integration lives in trading_agent_service.
[features]
default = ["cuda"]
cuda = []
kernel-step-trace = []
[dependencies]
ml-core = { workspace = true }
# NOTE: cannot depend on `ml` (would cycle — ml depends on ml-alpha for the
# mamba2_block gate reference). MappedF32Buffer is duplicated locally in
# `src/pinned_mem.rs`; eventually we should move mapped-pinned to ml-core.
ml-features = { workspace = true }
data = { workspace = true }
cudarc = { version = "0.19", default-features = false, features = ["driver", "cublas", "dynamic-linking", "std", "cuda-version-from-build-system", "f16"] }
# Standard async + error + logging plumbing
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "fs", "io-util"] }
anyhow.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
bincode.workspace = true
thiserror.workspace = true
clap = { workspace = true, features = ["derive"] }
# Arrow IPC for fxcache reading (gate reference path).
arrow = { workspace = true, features = ["ipc"] }
# Deterministic RNG for weight init, shuffling.
rand = { workspace = true }
rand_chacha = "0.3"
# Phase A data path: mmap predecoded MBP-10 sidecars.
memmap2 = { workspace = true }
# SPEED-A (2026-05-22): parallel per-file load + label generation in
# MultiHorizonLoader::new gives ~4-8x speedup on typical 4-8 core hosts.
rayon = { workspace = true }
[dev-dependencies]
tempfile = { workspace = true }
approx = { workspace = true }
# Phase R6: convergence-gate fixtures + alpha_rl_train CLI need
# LobSimCuda from ml-backtesting. Dev-dep avoids the cycle —
# ml-backtesting → ml-alpha is the production direction; this
# dev-only edge only loads when building ml-alpha's own tests/examples.
ml-backtesting = { path = "../ml-backtesting" }