jgrusewski 91e2c5dc8a feat(rl): FRD layer-2 backward (dW2, db2, dhidden) — F.3b
Second of three FRD backward stages. Given dL/dlogits from F.3a's
softmax_ce_grad and the cached hidden activation from F.2's forward,
computes the layer-2 weight gradients via the standard chain rule
and emits the upstream gradient for layer-1 backward (F.3c).

Kernel `cuda/rl_frd_layer2_bwd.cu`:
  * grid_dim = (B, 1, 1), block_dim = (FRD_HIDDEN_DIM=64, 1, 1)
  * Phase 0: stage 63-slot grad_logits into shared (thread 63 idle)
  * Phase 1: each thread i (i < 64) computes one row of per-batch
    dW2 scratch: grad_w2_per_batch[b, i, 0..63] = h_bi × grad_logits[0..63]
    (63 writes per thread, no atomics)
  * Phase 2: each thread i computes dL/dhidden[b, i] = Σ_j W2[i, j] × grad_logits[j]
  * Phase 3: thread i (i < 63) writes grad_b2_per_batch[b, i] = grad_logits[b, i]
  * Per-batch scratch shape [B, FRD_HIDDEN_DIM, FRD_OUT_DIM] reduces
    across batch via existing reduce_axis0 infra (caller's job, same
    pattern as v_head_bwd / aux_heads_bwd)

Rust wiring `FrdHead::layer2_bwd`:
  * Takes hidden (forward cache), grad_logits (from softmax_ce_grad),
    self.w2_d
  * Writes grad_w2_per_batch, grad_b2_per_batch, grad_hidden — all
    sized to caller-allocated buffers
  * Sole &self method (Adam step is the caller's responsibility)

Tests (2 new, 8/8 file total):
  * frd_layer2_bwd_finite_diff_w2 — perturb W2[10, 5] by ±ε=1e-3,
    compare (L(+) - L(-))/(2ε) to per-batch grad scratch. rel_err
    = 6.27e-5 (better than F.3a's softmax-CE finite-diff because
    the gradient magnitude here is larger so rounding error is
    relatively smaller). Helper `ce_total_loss` re-uses
    `softmax_ce_grad` to compute total CE for the perturbed forward
    pass — pure GPU-oracle, no CPU softmax/CE reference impl.
  * frd_layer2_bwd_db2_equals_grad_logits — analytical invariant:
    db2_per_batch[b, j] must equal grad_logits[b, j] exactly (the
    bias gradient is the identity passthrough at this layer). Cheap
    structural check that catches dimension-shuffle bugs in the
    kernel before they corrupt the reduce_axis0 step.

The kernel restores W2 to its original values after the perturbation
to keep test isolation clean — `&mut head` access pattern (proper
Rust borrowing, no UB const→mut casts).

F.3c (layer-1 backward: dW1, db1, dh_t with ReLU mask via the
cached hidden activation) is next.
2026-05-24 18:36:29 +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%