jgrusewski e72885e8b6 gem(G12): finish predictive_coding — write backward + wire into training loop
Found by V7-gem audit: predictive_coding_loss kernel existed (forward only,
per-sample MSE on consecutive trunk activations), and compute_predictive_coding_loss
Rust wrapper existed, but no backward and no caller. Pure scaffolding.

Self-supervised temporal smoothness on the enriched trunk h_s2 IS in the
"better form" by the V7 methodology — it operates at the gradient level on
the network representation, not as a reward shaping term. Worth wiring up.

Three changes:

1) New CUDA kernel `predictive_coding_backward` (experience_kernels.cu)

   Loss:    L = sum_{i=0..N-2} (lambda/SH2) * sum_j (h[i,j] - h[i+1,j])^2
   Grad:    dL/dh[i,j] = (2*lambda/SH2) * sum-of-affected-loss-terms

   Each h[i,j] appears in TWO loss terms (interior i): "current" of term i
   and "next" of term i-1. Boundaries (i=0, i=N-1) have one. Closed-form
   gradient written directly with no atomicAdd — one thread per (sample,
   feature) cell, each writes to a unique slot, plain += accumulates into
   bw_d_h_s2. Bit-deterministic.

2) Rust glue (gpu_dqn_trainer.rs)

   - Load `predictive_coding_backward` kernel via existing exp_module_for_mag
   - New field on DQNTrainer (predictive_coding_backward_kernel)
   - Extend `compute_predictive_coding_loss` to also launch backward as
     step 3 (after forward + reduce). Now the function name accurately
     describes what it does — both compute and accumulate gradient.

3) Integration (fused_training.rs::submit_aux_ops)

   Inserted the call right after `launch_recursive_confidence_backward`,
   before regime_scale_td_errors. Both spots accumulate into bw_d_h_s2 via
   plain +=, so ordering is irrelevant for correctness — what matters is
   that this runs INSIDE the aux_child CUDA-graph capture window AND
   BEFORE the trunk W_s2 → W_s1 backward GEMMs read bw_d_h_s2.

Why not also G6 (branch_independence) and G10 (temporal_consistency)?
Per V7-gem methodology — check for redundancy first:
  - G10 wants Lipschitz on Q for similar states. Spectral normalization
    (already wired on all 12 weight tensors) achieves *global* Lipschitz.
    G10 adds *local-pair* Lipschitz on top. Possibly redundant — needs
    a measurement before wiring blindly.
  - G6 wants the 4 advantage branches to stay diverse. NoisyNets already
    adds different parameter noise per layer → naturally diverse heads.
    Ensemble KL gradient pushes ensemble heads apart (different mechanism
    but similar intent). Possibly redundant for the 4-branch case.

G12 is unambiguously useful — trunk smoothness is a genuine gem with no
existing equivalent in the codebase. G6/G10 land separately if measurement
shows they add real value beyond spectral_norm + NoisyNets + ensemble KL.

Files touched:
  crates/ml/src/cuda_pipeline/experience_kernels.cu      (+45)
  crates/ml/src/cuda_pipeline/gpu_dqn_trainer.rs         (+33 / -3)
  crates/ml/src/trainers/dqn/fused_training.rs           (+9)

Verified: cargo check passes. Smoke test running to verify training is
stable with G12 active (lambda_pred=0.1 — small enough that any regression
is from a real bug, not dominance over the C51/IQN gradient).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 09:23:28 +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%