Zero candle_core/candle_nn imports in ml/src/. Three-agent parallel migration: - cuda_pipeline/ (19 files): Tensor→CudaSlice, Device→Arc<CudaStream>, VarMap→GpuVarStore, cudarc import path fixed - trainers/ + adapters (45 files): DQN/PPO/TFT trainers, 10 ensemble adapters, 11 hyperopt adapters — all migrated to MlDevice, GpuTensor, GpuVarStore, GpuAdamW - model dirs + infra (40 files): 10 trainable adapters, preprocessing, inference, transformers, validation, benchmarks 61 test/example files still reference candle — next commit. candle-nn still in Cargo.toml (needed by tests until migrated). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
80 lines
2.6 KiB
Rust
80 lines
2.6 KiB
Rust
//! # Performance Benchmarks for HFT Transformers
|
|
//!
|
|
//! This module provides comprehensive benchmarking for transformer models
|
|
//! optimized for high-frequency trading, validating sub-100μs inference targets.
|
|
//!
|
|
//! ## Benchmark Categories
|
|
//!
|
|
//! - **Latency Benchmarks**: End-to-end inference timing
|
|
//! - **Throughput Benchmarks**: Predictions per second capacity
|
|
//! - **Memory Benchmarks**: GPU memory usage and efficiency
|
|
//! - **Accuracy Benchmarks**: Model performance on financial data
|
|
//! - **Hardware Benchmarks**: GPU vs CPU performance comparison
|
|
|
|
use std::fs::File;
|
|
use std::fs;
|
|
use std::io::Write;
|
|
use std::process;
|
|
use std::time::{Duration, Instant};
|
|
|
|
use ml_core::native_types::{NativeDevice, NativeDType, NativeTensor};
|
|
use ml_core::cuda_autograd::GpuTensor;
|
|
use chrono::Utc;
|
|
use criterion::{BenchmarkGroup, BenchmarkId, Criterion, measurement::WallTime};
|
|
use tokio::runtime::Runtime;
|
|
|
|
use crate::traits::MLModel; // Import MLModel trait for predict method
|
|
use crate::transformers::{
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_benchmark_config() {
|
|
let config = BenchmarkConfig::default();
|
|
assert_eq!(config.target_latency_us, 100);
|
|
assert!(config.gpu_enabled);
|
|
assert!(!config.batch_sizes.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn test_benchmark_result() {
|
|
let result = BenchmarkResult {
|
|
name: "test".to_owned(),
|
|
mean_latency_us: 50.0,
|
|
std_latency_us: 5.0,
|
|
min_latency_us: 40.0,
|
|
max_latency_us: 60.0,
|
|
p50_latency_us: 50.0,
|
|
p95_latency_us: 58.0,
|
|
p99_latency_us: 59.0,
|
|
throughput_pps: 20000.0,
|
|
memory_usage_bytes: 1024,
|
|
meets_target: true,
|
|
metadata: std::collections::HashMap::new(),
|
|
};
|
|
|
|
assert!(result.meets_hft_requirements(100));
|
|
assert!(result.summary().contains("test"));
|
|
assert!(result.summary().contains("50.0μs"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_benchmark_suite() {
|
|
let config = BenchmarkConfig {
|
|
warmup_iterations: 5,
|
|
measurement_iterations: 10,
|
|
batch_sizes: vec![1],
|
|
sequence_lengths: vec![16],
|
|
model_sizes: vec![ModelSize::Nano],
|
|
gpu_enabled: false, // Use CPU for testing
|
|
target_latency_us: 100,
|
|
};
|
|
|
|
let mut suite = TransformerBenchmarkSuite::new(config);
|
|
|
|
// This would run a minimal benchmark for testing
|
|
// In practice, we'd need actual model weights loaded
|
|
// For now, just test that the suite initializes correctly
|
|
assert_eq!(suite.results.len(), 0);
|
|
}
|
|
}
|