jgrusewski e09757419c fix(tlob): align backward dW_Q/K/V layout with forward W_Q/K/V (Fix 20)
Forward W_Q SGEMM stored col-major [K, M] (lda=K) while backward dW_Q
wrote col-major [M, K] (ldc=M). When M ≠ K (TLOB: M=16, K=32), Adam's
element-wise update applied gradients computed at position (m, k) to
weights stored at position (k, m) — silent learning corruption at
every flat index ≠ 0 (511 of 512 W_Q slots updated using wrong-position
gradients, matched in W_K/V; W_O is square so unaffected).

Standardised backward dW_Q/K/V SGEMM output to col-major [K, M] (ldc=K)
matching the forward layout (Strategy A from the audit brainstorm — the
forward layout is the definitive weight storage; Adam's flat layout
follows forward's allocation). The fix flips the cuBLAS strided-batched
operands: backward now computes `dW^T = ofi @ d_proj^T` instead of
`dW = d_proj @ ofi^T`. Same gradient values, just re-laid-out so flat
indexing matches `params`. No new kernel; no kernel-internal layout
change (the SDP forward/backward kernels still read `proj_qkv_buf` /
`d_proj_qkv_buf` as [M, B] col-major — those buffers are untouched).
The QKV-fusion `cublasSgemmStridedBatched(batch=3)` semantics are
preserved: ofi is the new shared operand (strideA=0), d_proj is the
per-batch operand (strideB=M·B), strideC=M·K=512 unchanged.

Phase-1 reproduction (`tlob_dw_layout_alignment_repro`,
#[ignore = "requires GPU"]) ran the broken and fixed cuBLAS dispatches
side-by-side on identical sentinel inputs (`d_proj[m=0,b=0]=1`,
`ofi[k=1,b=0]=1`, all else 0); broken `[M, K]` placed the `1.0`
gradient at flat 16, fixed `[K, M]` placed it at flat 1 — O(1)
cross-layout delta exactly matching the audit prediction. Pre-fix Adam
would have updated `W_Q[m=0, k=16]` (the forward layout's flat-16 slot)
using the gradient computed for `W_Q[m=0, k=1]` — the silent
corruption.

Phase-3 regression (`tlob_dw_layout_alignment_regression_full_chain`,
#[ignore = "requires GPU"]) exercises the full forward → backward →
Adam → forward chain with random Xavier-init weights (W_O seeded to
break the production-zero-init that would collapse the gradient chain
to all-zero in a synthetic test). Asserts (1) GPU dW_Q matches a CPU
reference computed in the post-fix [K, M] layout within TF32 tolerance,
and (2) the second forward Q matches the analytical [K, M]
interpretation of the post-Adam W_Q — locks in cross-step layout
agreement and would fail if any future refactor accidentally
re-permutes `params` between Adam and the next forward.

Existing inline `tlob_sgemm_parity_with_cpu_reference` still passes
(its CPU dW_Q/K/V reference was updated in lockstep to the [K, M]
layout per `feedback_no_partial_refactor`; pre-fix the GPU produced
[M, K] and the new CPU reference would diverge element-wise — a clean
no-skip parity check that locks the layout convention end-to-end).
`tlob_qkv_fusion_equivalence` unchanged (the fix only touches the
backward call, forward QKV fusion is bit-identical pre/post).

Local verification (RTX 3050 Ti, batch=256 for fusion test):
  tlob_dw_layout_alignment_repro:                      PASS
  tlob_dw_layout_alignment_regression_full_chain:      PASS
  tlob_qkv_fusion_equivalence:    PASS (3.79× speedup retained)
  tlob_sgemm_parity_with_cpu_reference:                PASS

Fix 20 in docs/dqn-gpu-hot-path-audit.md updated FIXED with verdict
+ strategy + test list. Forward SGEMM call site got an inline comment
block documenting the [K, M] convention and pointing at the
`tlob_dw_layout_alignment_*` regression coverage.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 12:15:34 +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%