jgrusewski 64bbbe4181 fix(sp20): WR_EMA pinning bug — wire segment-level is_win_per_env producer + kernel-body read
Closes the fix gap left by the failing-test commit. The aggregation
kernel body now reads `is_win_per_env[env]` (when non-NULL) in the
`tc != 0` branch in place of the broken per-bar `step_ret > 0`
predicate; the producer site at `experience_kernels.cu::segment_
complete` writes the segment-level `(segment_return > 0.0f) ? 1 : 0`
flag at the same location as `alpha_per_env[i] = alpha`.

Atomic changes:
- sp20_aggregate_inputs_kernel.cu: when is_win_per_env != NULL, read
  the per-env i32 flag for wins_count; legacy `sr > 0` predicate
  preserved as NULL-tolerant fallback for oracle-test scaffolds.
- experience_kernels.cu: new `int* is_win_per_env` kernel arg;
  segment_complete branch writes `(segment_return > 0.0f) ? 1 : 0`
  at the same site as `alpha_per_env[i] = alpha`. Race-free per-i
  write (mirrors the alpha producer pattern). NULL-tolerant.
- gpu_experience_collector.rs: `is_win_per_env: CudaSlice<i32>`
  `[alloc_episodes]` field, alloc_zeros at construction, kernel-arg
  pass at experience_env_step launch site, raw_ptr() pass at
  sp20_aggregate_inputs launch site.
- state_reset_registry.rs: FoldReset entry + assertion test
  (sp20_is_win_per_env_registered_fold_reset). The
  every_fold_and_soft_reset_entry_has_dispatch_arm test passing
  confirms the dispatch arm is present.
- training_loop.rs: `"is_win_per_env" => memset_zeros` arm.
- docs/dqn-wire-up-audit.md: 2026-05-10 fix-commit entry.

Bug pattern for the memory pearl: per-bar mark-to-market step_return
is NOT the trade's segment-level win/loss outcome at close bars
because tx_cost (deducted via `*cash -= cost` in
trade_physics.cuh::execute_trade) plus the per-bar Δprice tick
swamp out segment-accumulated unrealized gains. The segment-level
realized P&L (`(realized_pnl + unrealized_at_exit -
trade_start_pnl) / prev_equity`) is the correct outcome scalar;
the producer in segment_complete already had it (used by
sp20_compute_event_reward's sign check for alpha), the aggregation
kernel just wasn't reading the right thing. Bug latent since SP20
Phase 1.4 / 2.2 landed on 2026-05-09; caught 2026-05-10 by
HEALTH_DIAG observation in a multi-seed L40S smoke (alpha_ema
populated correctly while wr_ema pinned at 0.0000 across 17 epochs).

Verification:
- SQLX_OFFLINE=true cargo check -p ml --tests passes
- 21 SP20 lib tests pass; 9 state_reset_registry tests pass
- The new GPU oracle test
  wr_ema_uses_segment_pnl_via_is_win_per_env_predicate passes
  after this commit (would FAIL on prior commit's kernel-body)
- The NULL-tolerance pin
  null_is_win_per_env_falls_back_to_legacy_step_ret_predicate
  preserves the pre-fix oracle-test contract

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 01:27:36 +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%