jgrusewski 104fe81ca8 fix(cuda): VSN stride mismatch — read window_tensor at stride 56
Root cause of the step-4 NaN that has blocked the Phase 2.1 smoke gate
since session start.

`window_tensor_d` is allocated [B, K, ENCODER_INPUT_DIM=56]:
- Features [0..40) are per-snapshot market features (snap_feature_assemble_batched)
- Features [40..56) are per-batch broadcast context (rl_encoder_context_broadcast
  writes via offset `idx * 56 + 40`)

But `variable_selection_fwd` and `_bwd` read x with stride
`VSN_FEATURE_DIM=40`. For row 0 the stride happens to align with the
[B, K, 56] layout. For row 1 onwards the kernel reads MIXED data
across row boundaries — broadcast context bleeds into VSN's "snap
features" view, and snap features bleed across K boundaries.

The bleed produces nonsense for most steps but is bounded enough to
train through. Around step 4, accumulated trade_context magnitudes
(time_in_trade, unrealized_R, position_lots) get large enough to
overflow VSN's softmax in the bleed-affected rows → NaN cascades
through the encoder forward → backward computes NaN gradients →
AdamW applies them → step 5's forward graph sees NaN weights → G8 abort.

Fix: introduce `VSN_X_ROW_STRIDE = 56` (= ENCODER_INPUT_DIM) and use it
for all input reads of `x` in both forward and backward:
- variable_selection_fwd:41 (x_row = x + row * 56)
- variable_selection_bwd:173 (x_i read at stride 56)
- variable_selection_bwd:204 (xj read at stride 56)

Output strides (gates_out, y, grad_W, grad_b, grad_x) remain at
VSN_FEATURE_DIM=40 because:
- gates_out / y feed Mamba2 L1 which reads at in_dim=40 stride
- grad_W / grad_b accumulate into [FEATURE_DIM, FEATURE_DIM] / [FEATURE_DIM]
- grad_x is write-only (audit: `vsn_grad_x_d` has zero downstream consumers
  per grep of crates/ml-alpha/)

Verification:
- 10/10 runs clean at seed=16962, n-steps=10 (was 4-7/10 NaN before)
- 1000-step smoke clean at seed=16962: l_q≤1.76, l_v≤1.18, wr climbing
  0.36→0.55 (the surfer pattern emerging as expected per
  pearl_dd049d9a4_surfer_baseline_verified)
- Phase 2.1 smoke gate (l_q<3.0, l_v<2.0, no NaN over 1000 steps) PASSED

Per pearl_atomicadd_masks_v_instability commits 60e96bf55..4e629830b for
the full diagnostic chain (49 nan_scan labels, 4 sub-agent dispatches,
13 commits localizing the bug from "somewhere in the trainer" down to
this single line).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-29 10:49:58 +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%