jgrusewski 91c4e499d2 feat(rl): wire 3 of 6 missing EMA inputs (Phase A — entropy, adv_var, td_kurt)
R9 cluster smoke alpha-rl-qzstj diag exposed that 6 of 7 controllers
held at bootstrap for the entire 1000-step run because their input
EMAs were never populated. Only `mean_abs_pnl_ema` was wired (via
ema_update_on_done on reward_abs_d). The other 6 EMA producers
existed as generic kernels (ema_update_per_step / ema_update_on_done)
but nothing computed the per-step input signals to feed them.

This commit wires the 3 EMAs whose source signals are ALREADY
computed and live in trainer per-step buffers (Phase A — cheapest
to wire):

  * `entropy_observed_ema` (ISV[420] → rl_entropy_coef controller)
    ← per-batch entropy `entropy_d` from PPO surrogate forward.
    `ema_update_per_step` does mean-reduce internally, so this is
    a single launch with entropy_d as input (b_size native).

  * `advantage_var_ratio_ema` (ISV[421] → rl_rollout_steps)
    ← `var(advantages) / max(|mean(advantages)|, 1e-6)` reduction
    on advantages_d. New kernel `rl_var_over_abs_mean_b`
    (two-pass shared-mem tree-reduce) writes scalar to trainer-
    owned `ema_input_scratch_d[1]`, then `ema_update_per_step`
    consumes with b_size=1.

  * `td_kurtosis_ema` (ISV[422] → rl_per_alpha)
    ← `E[(x-μ)⁴] / σ⁴` kurtosis reduction on td_per_sample_d
    (R7d's per-sample CE loss from dqn_distributional_q_bwd).
    New kernel `rl_kurtosis_b` (three-pass shared-mem tree-reduce)
    writes scalar to ema_input_scratch_d, then ema_update_per_step
    with b_size=1.

## Wiring placement

* `advantage_var_ratio` update: in `step_with_lobsim` immediately
  after `compute_advantage_return` populates `advantages_d`. Fires
  BEFORE the next step's controllers, so the controller sees the
  fresh signal one step later.

* `entropy_observed` + `td_kurtosis` updates: in `step_synthetic`
  AFTER the encoder backward (deferred from their natural in-place
  locations to avoid a borrow-checker conflict with `h_t_borrow`
  which holds `&self.perception` through the entire forward chain).
  One-step lag — same as advantage_var_ratio for the same reason
  (controllers fire in the NEXT step_with_lobsim).

## Trainer-owned scratch

Single `ema_input_scratch_d: CudaSlice<f32>` of length 1. Reused
across the var-over-abs-mean and kurtosis launches in any given
step — they're stream-serialised, so the second reducer's write
to slot 0 strictly follows the first reducer's consumer (the
corresponding ema_update_per_step). Cheap (4 bytes); avoids
two separate scratches.

## Why a 1-float scratch + b_size=1 ema_update

`ema_update_per_step` expects `obs_d[b_size]` and computes per-step
mean as `Σobs / b_size`. Passing a 1-element buffer gives
mean = obs[0] = the reduce kernel's scalar output. The EMA then
blends `prev` toward that scalar via Wiener-α (or bootstraps on
first non-zero per `pearl_first_observation_bootstrap`).

This pattern lets the existing per-step EMA kernel handle scalar
inputs without modification — the alternative (a dedicated
"ema_scalar_per_step") would duplicate logic per
`feedback_single_source_of_truth_no_duplicates`.

## Verified gates (post-fix, local sm_86)

  G1  isv_bootstrap                
  G3  controllers_emit              (test pre-seeds inputs, so
                                       wiring path not exercised)
  G4  target_soft_update           
  G6  r7d_per_wiring               
  R3, R4, smoke                    

Local smoke at b_size=1 won't exercise kurtosis (kernel returns 0
at b_size<2 → cold-start gate holds per_α at bootstrap). var_over_
abs_mean does fire because b_size=1 has a well-defined (degenerate)
variance of 0. Cluster smoke at b_size=1 will mostly exercise
entropy_observed.

## What's NOT in this commit (Phase B — 3 EMAs left)

  * `kl_pi_ema` (ISV[419] → rl_ppo_clip)
    needs: D_KL approximation between log_pi_old and log_pi_new.
    Both buffers exist in trainer; need a small subtract-and-mean
    kernel OR extend PPO surrogate forward to emit kl_per_batch.

  * `q_divergence_ema` (ISV[418] → rl_target_tau)
    needs: `‖W_online − W_target‖₂`. Both DQN weight buffers
    accessible via dqn_head fields; need a small L2-diff-norm
    kernel called after soft_update_target.

  * `trade_duration_ema` (ISV[417] → rl_gamma)
    needs: per-batch step counter (i32, b_size, trainer-owned)
    that increments each step and emits its value on done. Needs
    a small `step_counter_update` kernel + the counter buffer.

These three need NEW signal-derivation kernels (not just reductions
over existing buffers). Separate commit.

## Cluster smoke expected diag change

Before this commit: 6 of 7 EMA input slots stuck at 0.0 for all
1000 steps. After: ISV[420], ISV[421], ISV[422] populated each
step. The corresponding controllers (coef, n_roll, per_α) should
visibly adapt after the first few non-zero observations.

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

Foxhunt

Production HFT trading system in Rust.

Architecture

The workspace contains 32 crates organized as follows:

Core Libraries (16)

Crate Purpose
trading_engine Order processing, FIX 4.4, IB TWS, SIMD, RDTSC timing
risk VaR, Kelly, circuit breakers, kill switches, compliance
risk-data Risk data types and shared structures
trading-data Trading data types
ml DQN Rainbow, PPO, TFT, Mamba2, ensemble inference
ml-data ML data types and feature definitions
data Market data ingestion and storage
backtesting Replay engine, strategy tester
adaptive-strategy Ensemble execution, microstructure analysis
common Shared types, resilience, error handling
storage S3 and local model storage
model_loader Model serialization and loading
market-data Market data feed handlers
database PostgreSQL access layer (SQLx)
config Configuration management
tli CLI commands and tooling

Services (8)

Service Purpose
backtesting_service gRPC backtesting service
broker_gateway_service FIX routing, broker connectivity
trading_service Core trading operations
ml_training_service Model training orchestration
data_acquisition_service Market data acquisition
trading_agent_service Autonomous trading agents
api_gateway gRPC API gateway with auth
web-gateway Axum REST + WebSocket gateway

Frontend

web-dashboard/ -- React 19 + TypeScript + Vite + TradingView charts.

Building

# Check compilation (no PostgreSQL required)
SQLX_OFFLINE=true cargo check --workspace

# Run tests for a specific crate
SQLX_OFFLINE=true cargo test -p <crate> --lib

# Clippy
SQLX_OFFLINE=true cargo clippy --workspace

ML Models

Four production model architectures on Candle v0.9.1 with CUDA:

  • DQN Rainbow -- Deep Q-Network with prioritized replay, dueling heads, noisy nets
  • PPO -- Proximal Policy Optimization with GAE and LSTM policies
  • TFT -- Temporal Fusion Transformer for multi-horizon forecasting
  • Mamba2 -- State space model for sequence prediction

Each model has a standalone trainer and a UnifiedTrainable adapter for the hyperopt pipeline.

Infrastructure

  • Git: Gitea at git.fxhnt.ai (Tailscale-only), Scaleway DEV1-S
  • Observability: OpenTelemetry OTLP (env OTEL_EXPORTER_OTLP_ENDPOINT)
  • Database: PostgreSQL with SQLx offline mode for CI

License

Proprietary. All rights reserved.

Description
No description provided
Readme 849 MiB
Languages
Rust 88.2%
Cuda 7.7%
Python 1.3%
Shell 1.1%
PLpgSQL 0.8%
Other 0.8%