jgrusewski 9170d24fe3 refactor(ml-alpha): replace legacy attention_pool with MultiHorizonAttention [Stage 2]
Single source of truth for the attention path. Deletes the legacy
single-Q `attention_pool.cu` and all `attn_*` fields from
`PerceptionTrainer`; wires `MultiHorizonAttention` (the bundle
introduced in Stage 1) into `step_batched` + `evaluate_batched` as
THE attention summary that seeds CfC's `h_old` at k=0.

Deletions:
  cuda/attention_pool.cu                      (244 lines)
  perception.rs::attn_q_d/attn_context_d/
    attn_weights_d/grad_attn_q_d/opt_attn_q/
    attn_fwd_fn/attn_bwd_fn/_attn_module/
    attn_grad_q_scratch_d                     (all struct fields)
  perception.rs::ATTENTION_POOL_CUBIN         (include_bytes constant)
  Their corresponding init + struct-construction lines.
  build.rs::KERNELS                           (drops "attention_pool")

New kernel + binding:
  cuda/horizon_mean_collapse.cu               (53 lines)
    - `horizon_mean_collapse_fwd/_bwd`: collapses [B, N_H, H] → [B, H]
      by averaging over the horizon axis. Single-pass, no reductions.
  src/horizon_mean_collapse.rs                 (host binding)

MHA additions:
  - `collapse` field + `ctx_mean_d` + `grad_ctx_mean_d` for the seed.
  - `grad_ctx_h_d` scratch (split from grad_horizon_tokens_scratch to
    avoid aliasing when MoE bwd writes d_ctx_h while pool bwd writes
    d_horizon_tokens).
  - `forward(ln_b_out)`: horizon-token pool → inverted pool → MoE
    dispatch → mean-collapse → ctx_mean_d.
  - `backward(ln_b_out, grad_ctx_mean, grad_ln_out)`: full reverse
    chain.
  - `apply_anchor()`: launches anchor_l2 on horizon_tokens, Q,
    experts_w.
  - `adamw_step()`: steps all 6 owned optimizer groups.

PerceptionTrainer integration:
  - Section 2d (forward): `self.mha.forward(&self.ln_out_d)` replaces
    the legacy attention_pool launch. CfC's h_old at k=0 now reads
    `self.mha.ctx_mean_d.device_ptr` (was `self.attn_context_d`).
  - Section 7c-pre (backward): `self.mha.backward(ln_out, grad_h_carry,
    grad_h_enriched_seq)` replaces the legacy attn_bwd_fn launch.
  - Four `reduce_axis0` launches collapse MHA's per-batch scratches
    into shared gradient buffers: grad_horizon_tokens, grad_q,
    grad_experts_w, grad_experts_b.
  - `self.mha.apply_anchor()` adds L2 anchor grad contributions.
  - Section 9 (AdamW): `self.mha.adamw_step()` replaces opt_attn_q.
  - `evaluate_batched`: `self.mha.forward` replaces the legacy fwd
    launch; h_old at k=0 reads `mha.ctx_mean_d`.
  - `self.mha.zero_grads()` at step start (capture-safe memset_zeros).

BUG CAUGHT DURING WIRING (NVIDIA-grade discipline): first wiring
attempt mis-sized the reduce_axis0 launches for the MoE
`grad_w_scratch_d` ([B, N_H, N_E, H, H]). Initial `n_tail = N_H * N_E
* H * H = 327680` would have made reduce_axis0 read 5× past the end
of the buffer → CUDA_ERROR_ILLEGAL_ADDRESS. Fix: `n_tail = N_E * H *
H = 65536` with `n_batch = B * N_H`, treating the leading two axes
together as the reduction dimension. Caught by stacked_trainer test
on RTX 3050; would have caused silent corruption then a hard fault
on L40S/H100 later.

LOCAL VERIFICATION (RTX 3050 sm_86):
  - ml-alpha builds clean (cuda feature).
  - All 38+ tests PASS serially with --test-threads=1:
      perception_overfit (8 tests incl. loss-shrinks)
      trunk_forward (5)
      stacked_loss_shrinks (multiple)
      bce_grad_finite_diff (4)
      snap_feature_assemble (9)
      ... (full suite green)
  - Numgrad parity for the 4 new MHA kernels (horizon_token, inv_attn,
    regime_moe_gate, anchor_l2) PASSES at 5e-2 rel.

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