jgrusewski c34650a241 refactor(rl): R7b — host Thompson/argmax/log_pi → GPU; V stays on device
Closes the last host orchestration in `step_with_lobsim`'s policy path
per `feedback_cpu_is_read_only` + `pearl_no_host_branches_in_captured_graph`.
The flawed Phase F+G shipped three host loops (Thompson sampling over
C51 atoms, argmax over expected Q, log-softmax for log π_old) that each
required a DtoH staging copy of the relevant Q/V/π device buffer per
step. R7b deletes them all in favour of three R4 kernels:

  * `rl_action_kernel`     — Thompson sampler with per-batch xorshift32
                             PRNG state (device-resident, no salt-based
                             host RNG seeding per step).
  * `argmax_expected_q`    — deterministic Bellman-target argmax over
                             expected Q per action; distinct selector
                             from Thompson per
                             `pearl_thompson_for_distributional_action_selection`.
  * `log_pi_at_action`     — per-batch log π(action_b) via log-softmax
                             + lookup; feeds the PPO importance ratio.

Side effects of the lift:

  * The host `read_slice_d` DtoH calls for `q_logits_host`,
    `v_pred_host`, `pi_logits_host` are deleted (each was 4 × b_size ×
    Q_N_ATOMS or b_size × N_ACTIONS floats per step). Three full
    device→host→device roundtrips per step → zero.
  * The host γ readback (ISV[400] mirror DtoH + bootstrap fallback
    that the flawed branch kept "just in case") is also gone:
    `compute_advantage_return` already reads γ on-device from
    ISV[400] (R3). The fallback was a smell that R7a planned to
    eliminate; R7b actually removes it.
  * V(s_t) stays in `v_pred_d` throughout the hot path. The host
    upload via the (now-deleted) `upload_f32` helper that round-tripped
    V back to device for `compute_advantage_return` is gone. V(s_{t+1})
    still reuses `v_pred_d` (h_t bootstrap) — true V(s_{t+1}) via
    `forward_encoder(next_snapshots)` is explicitly scoped to R7c.
  * Step-counter increment retained (drives the Thompson-vs-argmax
    diagnostic ordering in the trainer's stats record), but the
    ChaCha8Rng-from-step_counter that seeded the host loop is gone.

Per `feedback_no_hiding` the three retired helper functions
(`upload_f32`, `upload_i32`, `argmax_f32`) are DELETED rather than
`#[allow(dead_code)]`'d. The matching unused imports
(`MappedI32Buffer`, `DevicePtrMut`) are removed in the same commit.

The `actions_d` allocation that used to live in step_with_lobsim's
local scope (used only to bridge the host Thompson loop → the
GPU `actions_to_market_targets` kernel) is gone too — that kernel now
reads directly from `self.actions_d` written by `rl_action_kernel`,
closing the last action-path host upload.

What's still HtoD in the hot path (intentional, boundary data):
  * `apply_snapshot(last_snap)` — the trainer-fed MBP-10 input
    crosses the I/O boundary; mapped-pinned per
    `feedback_no_htod_htoh_only_mapped_pinned`.
  * HEALTH_DIAG ISV readback inside `step_synthetic` — explicit
    diagnostic surface, not hot-path orchestration.

Falsifiability gate G4 (R4 kernel oracle tests + R5 controller +
soft-update tests) already passing on this branch. The R6/R7a smoke
(`integrated_trainer_step_with_lobsim_runs_without_panic`) re-builds
clean and the only host code remaining in `step_with_lobsim` is the
bounded `LaunchConfig`/`b_size_i` setup before each kernel launch.

R7c (next): PER buffer wiring (push transitions, sample, update
priorities) per `feedback_always_per`, and `next_snapshots` +
`forward_encoder(next_snapshots)` for true V(s_{t+1}) Bellman target
(replaces the h_t bootstrap reuse above).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-23 12:15:41 +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%