jgrusewski edc449eecf feat(ml-alpha): per-horizon attention pool kernel + numgrad parity (C21)
First implementation slice of the per-horizon attention pool design
(docs/superpowers/specs/2026-05-18-per-horizon-attention-pool-design.md).
Lands the kernel + Rust binding + numgrad verification; downstream
wiring into PerceptionTrainer's captured graph + CheckpointV2 bump +
A/B sweep are follow-up commits gated on this proving correctness.

Kernel (cuda/per_horizon_attention_pool.cu):

  per_horizon_attention_pool_fwd
    Q_h[N_HORIZONS, HIDDEN_DIM] × LNb[B, K, HIDDEN_DIM]
      → context_h[B, N_HORIZONS, HIDDEN_DIM]
        attn_h_weights[B, N_HORIZONS, K]
    Per-block math identical to the single-Q variant, looped over
    N_HORIZONS sequentially within each batch's block. Grid stays
    (B, 1, 1) so backward grad_ln_out writes are race-free
    (per feedback_no_atomicadd.md — no cross-block contention).
    Per-batch shared mem ~k_seq + BLOCK + HIDDEN_DIM floats.

  per_horizon_attention_pool_bwd
    Same chain-rule pattern as attention_pool_bwd but with the horizon
    loop inside the block: each (b, h) slice updates grad_ln_out in
    place (sequential horizon accumulation), grad_Q_h is written as
    per-block scratch [B, N_HORIZONS, HIDDEN_DIM] for host reduce.
    Single-writer discipline preserved.

Rust binding (src/per_horizon_attention_pool.rs):
  PerHorizonAttentionPool::{new, forward, backward}. Self-contained;
  doesn't yet touch PerceptionTrainer or CfcTrunk. Loads the cubin
  via the standard env!("OUT_DIR") path. Dynamic shared-mem byte
  count computed per launch from k_seq.

Numgrad parity test (tests/per_horizon_attention_pool_numgrad.rs):
  - B=2, K=8, HIDDEN_DIM=128, N_HORIZONS=5 fixture.
  - Loss = Σ context_h (so d_context = 1 everywhere — clean analytical).
  - Backward kernel produces analytical grads; central-difference of
    forward kernel at ±eps=1e-2 across 8 random Q_h indices + 8 random
    LNb indices verifies analytical matches CD within 5e-2 rel-tol or
    5e-3 abs-floor.
  - Passes on RTX 3050.

build.rs picks up the new .cu file automatically via the existing
KERNELS list; cubin compiles cleanly at sm_86 + sm_89.

Same scope discipline as Phase 2D.2 (VSN numgrad) — kernel correctness
first, integration second. The follow-up commit set per the spec §3
appendix:
  C22: extend multi_horizon_heads.cu signature to accept per-horizon
       context input + bump head_w shape to [N_HORIZONS, 2*HIDDEN_DIM]
  C23: wire PerHorizonAttentionPool into PerceptionTrainer + CfcTrunk
       captured graph behind AttentionPoolVariant config flag
  C24: CheckpointV1 → V2 bump with discriminant + optional q_h field
  C25: smoke training (one epoch, no NaN, loss decreases)
  C26: 30-epoch × 3-fold A/B sweep (#204) — decision gate per spec §0

Closes the kernel-correctness portion of #203.

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