Commit Graph

4015 Commits

Author SHA1 Message Date
jgrusewski
51ee07a6bc feat(ml-dqn): implement all 18 todo stubs — zero stubs, 354 tests pass
Real GPU implementations for every stub:

noisy_layers.rs: Factorized Gaussian noise generation, NoisyLinear
  forward with mu+sigma*epsilon weight composition
quantile_regression.rs: IQN forward (cosine embedding + projection),
  expected Q reduction, CVaR, quantile Huber loss
rainbow_network.rs: Full Rainbow forward (NoisyLinear + dueling +
  distributional softmax), categorical Q-values
distributional_dueling.rs: Distributional dueling forward with
  LeakyReLU + RMSNorm + mean-subtracted advantage
branching.rs: Constructor (GpuVarStore + GpuLinear + NoisyLinear),
  forward_branches, forward_distributional with C51 support
agent.rs: train(), compute_loss (Huber TD), update_target_network
  (hard copy), load_from_safetensors (checkpoint restore)

354 pass, 0 fail, 5 ignored. Zero todo!() stubs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 15:04:34 +01:00
jgrusewski
813358944e fix(ml-core): make GPU capability test device-agnostic
test_max_shared_memory_kb_h100: was calling driver query path which
returns actual GPU's shared memory (100KB on RTX 3050, not 228KB).
Changed to test name-based heuristic directly via max_shared_memory_kb_by_name().

Added test_max_shared_memory_kb_queries_real_device with range assertion
(48-256 KB) for device-agnostic driver query test.

ml-core: 301 pass, 0 fail.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 14:59:57 +01:00
jgrusewski
7e49d3c08e fix(ml): migrate all test code to GPU types — 125 compile errors fixed
17 files: smoke_tests (gpu_residency, training_stability, performance,
helpers), inference, multi_timeframe, tlob, validation/adapters,
tft/tests, ensemble/adapters/dqn, flash_attention, hyperopt/tft,
mamba/trainable, online_learning, liquid/adapter, transformers/attention

- Tensor→CudaSlice direct ops in smoke tests (no tensor_to_cuda_slice)
- Device→MlDevice throughout
- GpuLinear::forward 4-arg signature (input, store, cublas, stream)
- rand::rng()→thread_rng() (rand 0.8)
- StreamTensor .shape field access (not .dims() method)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 14:47:33 +01:00
jgrusewski
80058eaa16 fix(ml-dqn): migrate test code to GPU types — 22 compile errors fixed
- dqn.rs: readback() stream param, GpuTensor::randn signature,
  host-side assertions for max/eq (download result only for assert)
- target_update.rs: store.names()→param_names()
- ensemble_network.rs: Debug trait bound workaround

322 tests pass, 32 runtime failures (GPU memory / API — separate fix).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 14:12:50 +01:00
jgrusewski
2d703a2f9b fix(cuda): reduction kernel bugs — missing shmem fold + count reduction
Bug 1: Tree reduction stopped at s>32, leaving shmem[32..63] unfolded
before warp shuffle. Added explicit fold: thread i merges shmem[i+32]
before __shfl_down_sync. Affected all 5 kernels (stats, argmax, sum,
argmax_rows, col_sum). Caused max=96 instead of 100 for N=100.

Bug 2: fused_stats_reduce count only atomicAdd'd thread 0's local_count.
Added 5th shared memory slot for count through full tree reduction.

All 5 reduction tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 14:01:50 +01:00
jgrusewski
3da65f2804 feat(cuda): add 6 unary CUDA ops + delete 455 lines of host helpers from dqn.rs
elementwise.rs: added abs, neg, exp, log, le, affine CUDA kernel ops
(cases 5-10 in elementwise_unary). All GPU-native, zero host downloads.

gpu_tensor.rs: 7 new methods — abs(), neg(), exp(), log(), le(),
affine(), broadcast_sub(). All dispatch to CUDA kernels.

dqn.rs: DELETED 30+ host-side helper functions (~455 lines) that
downloaded to CPU, computed, re-uploaded. ALL replaced with direct
GpuTensor methods: affine(), clamp(), abs(), neg(), exp(), log(),
broadcast_mul(), broadcast_sub(), broadcast_as(), index_select(),
dim(), sqr(), flatten_all(), gpu_clone(), ActivationKernels.

Zero host-side math remaining in dqn.rs hot paths.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 13:48:26 +01:00
jgrusewski
643b9505a4 feat: WORKSPACE COMPILES — zero candle, zero errors, GPU-native
Final 58 compile errors fixed + GPU violation analysis:
- 23 files across ml, ml-dqn, ml-supervised, services
- flash_attention: CudaBlas + stream fields, GPU matmul/transpose
- ensemble adapters (tggn, tlob, mamba2, tft, ppo): StreamTensor/GpuLinear
- diffusion/xlstm/mamba trainable: StreamTensor ↔ GpuTensor conversion
- hyperopt adapters: fixed API signatures
- trainers (liquid, mamba2): checkpoint save via safetensors
- benchmarks: fixed to_scalar, RainbowAgent API
- services: MlDevice, PPO::load_checkpoint, Mamba2SSM constructor

Host-side softmax/distributional functions analyzed — operating on
legitimately-downloaded small output tensors at computation endpoints.
Not GPU violations (cold paths, <50 elements).

FULL WORKSPACE: 0 errors, 56 warnings, 0 candle dependencies.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 13:31:35 +01:00
jgrusewski
7102c0998a fix(ml): DQN trainer chain — 276 errors fixed, GPU-native APIs
10 files: metrics.rs, train_step.rs, config.rs, training_loop.rs,
fused_training.rs, data_loading.rs, online_learning.rs,
gpu_dqn_trainer.rs, dqn/trainable_adapter.rs, constructor.rs

- All GpuTensor ops use stream arg (add, mul, narrow, cat, etc.)
- Checkpoint save via safetensors TensorView (no VarMap)
- EWC: uniform Fisher approximation (no per-param backward)
- PER priority: CPU-side max Q (replacing nonexistent .max() chain)
- GpuTrainingGuard: from_device→from_stream
- HER: index_select with u32 indices

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 12:53:34 +01:00
jgrusewski
42e6e1053f fix(ml): validation, inference, hyperopt, PPO — GPU-native APIs
- validation/ppo_adapter.rs: complete rewrite, host-side PPO validation
- inference.rs: Arc<CudaStream> + CudaBlas + ActivationKernels
- hyperopt/adapters: 8 adapters migrated to UnifiedTrainable forward_loss
- trainers/ppo.rs: GPU collector VarStore, checkpoint, weight sync
- ppo/trainable_adapter.rs: PPO::new() constructor
- trainers/tft/trainer.rs: GpuTensor↔StreamTensor conversion helpers

334→65 errors (81% reduction). Remaining: StreamTensor vs GpuTensor
type mismatches in ensemble/trainable adapters.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 12:48:09 +01:00
jgrusewski
42e13803c7 feat(cuda): rewrite 18 GpuTensor methods as proper CUDA kernels
ZERO host downloads in any GpuTensor operation. All ops GPU-native:

elementwise.rs (NEW): 7 CUDA kernels compiled via compile_ptx_for_device
- elementwise_binary: add/sub/mul/div (parameterized op)
- elementwise_unary: powf/sqr/floor/relu/clamp (parameterized op)
- broadcast_scalar_binary: scalar broadcast with any op
- broadcast_row_binary: row broadcast [1,N] op [M,N]
- expand_broadcast: general N-D broadcast with GPU stride tables
- transpose_2d: [rows,cols] → [cols,rows]
- gather_select: index_select along any dimension

gpu_tensor.rs: 18 methods rewritten from host-roundtrip to GPU-native
- gpu_clone: cuMemcpyDtoDAsync (was DtoH+HtoD)
- add/sub/mul: elementwise_binary kernel (was CPU zip)
- broadcast_mul/div: broadcast kernels (was CPU map)
- narrow(dim=0): DtoD slice view (was CPU slice)
- narrow(dim>0): gather_select kernel
- argmax: ReductionKernels (was CPU scan)
- mean_all/sum_all: ReductionKernels (was CPU sum)
- powf/sqr/floor/relu/clamp: elementwise_unary kernel
- transpose: transpose_2d kernel
- expand/broadcast_as: expand_broadcast kernel
- index_select: gather_select kernel

3 reduction tests have init bug (max not -INF) — fix follows.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 11:34:03 +01:00
jgrusewski
84aaa2d7ac fix(ml): trainable adapters — correct UnifiedTrainable impl, GPU-native forward
7 adapters: KAN, TGNN, TLOB, TFT, xLSTM, Mamba, Liquid
- Correct trait methods: device_name(), forward_loss(&[f32], &[f32])
- GPU forward: GpuTensor::from_host → GpuLinear::forward → LossKernels::mse
- CudaContext→CudaStream→CudaBlas→GpuVarStore→GpuLinear→GpuAdamW init chain
- Backward: todo!() stubs (need GPU autograd — will fix in follow-up)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:47:24 +01:00
jgrusewski
e8dd460898 feat(ml-core): complete GpuTensor/GpuLinear/GpuVarStore API surface
GpuTensor: Clone derive (ref-counted CudaSlice), dims/dim/dims2/dims3,
from_vec/from_slice aliases, detach (no-op clone), powf/sqr/floor,
to_vec0/to_vec1, backward (no-op stub), relu/sum_all/flatten_all,
transpose/clamp/expand/broadcast_as/index_select. All host-side with
TODO: CUDA kernel markers.

GpuLinear: new(in, out, stream) standalone constructor,
weight_to_vec/bias_to_vec for checkpoint only.

GpuVarStore: cuda_stream(), data(), with_device(&MlDevice).

MlDevice: cuda_if_available(), new_cuda(), device() identity.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:26:34 +01:00
jgrusewski
a26478eca3 fix(ml): cuda_pipeline + trainers infra — CudaSlice throughout
- cuda_pipeline/mod.rs: all Tensor→CudaSlice, DqnGpuData/PpoGpuData GPU-native,
  d2d_subrange helper, build_state_tensor host-interleave + single HtoD
- portfolio_transformer.rs: complete rewrite with GpuLinear + cuBLAS
- trainers/tlob.rs: GpuVarStore + GpuLinear, host-side MSE/MAE
- trainers/tft: StreamTensor trait, MlDevice constructors
- trainers/liquid.rs, mamba2.rs: GpuTensor pairs, f64 loss accumulation
- training/orchestrator.rs: forward_loss(&[f32]) trait interface
- hyperopt/adapters/ppo.rs: MlDevice, PPO::new() constructor
- ml-ppo/lib.rs: fixed cuda_compat→cuda_compile re-export

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:02:18 +01:00
jgrusewski
c89a1dbf29 fix(ml): migrate remaining files — hyperopt, validation, data_loaders, inference
25+ files fixed:
- hyperopt/adapters: duplicate Arc imports, Device→MlDevice
- validation: regime_analysis rewritten for GpuTensor, ppo_adapter NativeDType
- data_loaders: all 3 loaders migrated to GpuTensor::from_host
- tft/training: MlDevice, GpuAdamW, StreamTensor, CPU loss tracking
- training_pipeline: AdamW→GpuAdamW, NativeDevice fixes
- features/multi_timeframe: removed to_candle_tensor
- inference: ModelForward trait replaces candle_nn::Module
- lib.rs: removed cuda_compat re-export
- ppo/mod.rs: fixed re-export

~95 errors remain in: inference.rs, flash_attention, validation/ppo_adapter,
hyperopt adapter method signatures (blocked on trainable adapter trait).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 09:57:24 +01:00
jgrusewski
0af1567c94 fix(ml): migrate DQN trainers — GpuTensor/MlDevice/GpuVarStore throughout
11 files: config.rs, action.rs, metrics.rs, training_loop.rs, train_step.rs,
mod.rs, state.rs, constructor.rs, fused_training.rs, data_loading.rs,
online_learning.rs

- Tensor→GpuTensor, Device→MlDevice, VarMap→GpuVarStore
- LinearGrads→BTreeMap<String, GpuTensor> for gradient accumulation
- Checkpoint save via safetensors + GpuVarStore::export_to_host()
- EWC (online_learning) fully migrated

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 09:13:43 +01:00
jgrusewski
2bbc80cf46 feat(cuda): PPO GPU-resident pipeline — 123 MB/epoch roundtrip eliminated
PpoExperienceBatch: all 6 fields Vec<T>→CudaSlice<T>. Data stays on GPU
from kernel collection through training. Zero DtoH for batch data.

- collect_experiences(): returns CudaSlice via D2D copy, no memcpy_dtoh
- gpu_batch_to_trajectory_batch(): DELETED (was CPU conversion)
- PPO::update_gpu(): reads states via D2D mini-batch, forward on GPU
- compute_metrics_from_gpu(): downloads only scalars (returns/advantages/
  actions), NOT the 123 MB state tensor
- download_*() methods for debug/checkpoint only

States (123 MB/epoch) never leave GPU. Only 2 scalar losses come to CPU.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 09:09:40 +01:00
jgrusewski
3e3cc27b30 refactor(cuda): delete CPU ExperienceBatch — GPU-only path
Deleted ExperienceBatch (CPU Vec<f32>) struct and collect_experiences()
method (120 lines of memcpy_dtoh + CPU next_state computation).

Production DQN training already uses collect_experiences_gpu() which
returns GpuExperienceBatch (CudaSlice, zero CPU download).

Tests rewritten as pure index-math validation without GPU types.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 09:09:01 +01:00
jgrusewski
0fc8b64b0b fix(ml): migrate 10 trainable adapters — GpuAdamW, Arc<CudaStream> constructors
KAN, TGNN, TLOB, Liquid, xLSTM, TFT, Diffusion, Mamba adapters:
- Candle AdamW/ParamsAdamW → GpuAdamW/AdamWConfig
- NativeDevice constructors → &Arc<CudaStream>
- Trait methods adapted: forward_loss(&[f32],&[f32])→f64, backward(f64)→f64
- GPU tensor ops moved to inherent forward_gpu/compute_loss_gpu methods

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 09:00:05 +01:00
jgrusewski
d39d191fbb feat(cuda): GPU fused reduction kernels + DQN trainer wiring
New ml-core/cuda_autograd/reductions.rs:
- fused_stats_reduce: min/max/sum/sum_sq/count in single pass with
  warp-level __shfl_down_sync + atomicCAS (20 bytes DtoH)
- argmax_flat: single u32 result (4 bytes DtoH)
- argmax_rows: per-row argmax for 2D data
- sum_reduce: standard tree reduction
- col_sum_reduce: per-column sum along rows

DQN trainer integration:
- collect_qvalue_statistics: 4 DtoH → 1 (single stats() call)
- compute_q_diagnostics_fused: replaces Candle sort/narrow pipeline
  with argmax_rows + stats + col_sums (3 kernels, 3 readbacks)
- ReductionKernels lazy-initialized in training loop

All kernels compiled via compile_ptx_for_device (native cubin + disk cache).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 08:40:28 +01:00
jgrusewski
2d426b5a4f perf(ppo): eliminate GPU roundtrip in metrics + fix MlDevice type
- Replaced compute_metrics_tensors (CPU→GPU→CPU roundtrip: 3 HtoD +
  8 kernels + 4 DtoH) with compute_metrics_cpu (pure f64 two-pass,
  data already on host). Zero GPU transfers for metrics.
- Fixed Device→MlDevice type, cuda_stream() extraction pattern
- Removed dead sample_action method (referenced non-existent Tensor type)
- Fixed GpuVarStore::new() to pass stream arg

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 08:31:06 +01:00
jgrusewski
d572dc466a perf(cuda): batch DQN metrics readbacks — 15 DtoH transfers → 4
metrics.rs: 4 fixes batching individual to_scalar() GPU syncs:
- Q-value stats: 4 transfers → 1 (cat [min,max,mean,var])
- Q diagnostics: 8 transfers → 1 (cat [gaps + per-action avgs])
- get_q_values: N transfers → 1 (bulk to_vec1)
- Validation Sharpe: 2 transfers → 1 (cat [mean,var])

Zero per-step DtoH leaks in training_loop.rs or metrics.rs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 08:29:26 +01:00
jgrusewski
b53df16b5f fix(ml-dqn): resolve final 24 compile errors — ml-dqn builds clean
- entropy_regularization: rand::rng()→thread_rng() (rand 0.8)
- agent: f32/f64 type casts for learning rate
- branching: cudarc 0.19 memcpy_htod 2-arg form, memcpy_dtoh deref
- distributional_dueling: RMSNorm::new_gpu→new_default
- noisy_layers: GpuTensor.data→into_parts()
- replay_buffer_type: GpuBatchSlices→GpuBatch conversion
- gpu_replay_buffer: bf16/u32 slice→GpuTensor helpers

ml-dqn: 0 errors, 44 warnings. Full candle elimination complete.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 08:23:30 +01:00
jgrusewski
3ed3594193 fix(ml-dqn): migrate dqn.rs — 187 compile errors resolved
Massive rewrite of DQNAgent core (3400+ lines):
- 30+ GPU helper functions added (gpu_clamp, gpu_abs, gpu_gather_3d, etc.)
- Forward pass: Candle ops→GpuTensor + host-side helpers
- Loss computation: simplified to host-side Bellman TD + Huber
  (fused CUDA trainer handles GPU-native path)
- Optimizer: ParamsAdam→AdamWConfig, Adam→GpuAdamW
- Target updates: polyak via copy_weights_from fallback
- Action selection: argmax returns Vec<u32>, Gumbel-max host-side
- Checkpoint: safetensors direct deserialization

24 errors remain across 12 other files (small counts, cascading type changes).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 08:12:00 +01:00
jgrusewski
2ffaf42197 fix(ml-dqn): migrate branching + regime_conditional — zero candle errors
branching.rs: MlDevice→Arc<CudaStream>, Candle gather/unsqueeze→host-side,
MaybeNoisyLinear::noisy_sigma_slices(), copy_weights via memcpy pattern.

regime_conditional.rs: device→stream field, all GpuTensor ops get &stream,
batch_softmax_actions via host-side Gumbel-max, gradient merge via BTreeMap.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 08:06:08 +01:00
jgrusewski
d6fa11ec6c fix: cudarc 0.19 API fixes in tests — CudaDevice→CudaContext, no Arc double-wrap
Test code used CudaDevice (cudarc 0.17) instead of CudaContext (0.19),
and wrapped Arc::new(CudaContext::new().new_stream()) which double-wraps
since new_stream() already returns Arc<CudaStream>.

740 tests pass across ml-core, ml-ppo, ml-supervised, ml-ensemble.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 08:05:32 +01:00
jgrusewski
f607335820 perf(cuda): eliminate NVRTC runtime compilation — all kernels now cubin
Replaced 4 cudarc::nvrtc::compile_ptx() calls in ml-ppo cuda_nn with
compile_ptx_for_device() — native cubin via nvcc -O3 with disk cache.

Before: virtual PTX → driver JIT (no -O3, no arch targeting, ~100ms first launch)
After: native SASS for exact sm_XX, cached to disk, <10ms load

Files: lstm.rs (2 kernels), linear.rs (1), softmax.rs (1)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 07:55:11 +01:00
jgrusewski
94b968d800 perf(cuda): pinned DtoH memory + NVRTC/device-attr caching
Experience collector: PinnedHostBuf<T> via cuMemHostAlloc(PORTABLE) for
6 DtoH buffers — ~25-30% faster transfers vs pageable memory.

NVRTC caching: 4 OnceLock additions in ml-ppo cuda_nn:
- softmax.rs: was re-compiling NVRTC on EVERY forward pass batch (!)
- linear.rs, lstm.rs: cached for multi-layer construction

Device attribute: OnceLock<u32> for max_threads in gpu_replay_buffer
pfx_sum — eliminates ~5µs driver query per call.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 07:52:44 +01:00
jgrusewski
53382ad692 perf(cuda): unify dual-stream to single stream + event-based sync_all
gpu_backtest_evaluator: removed env_stream, env_event, main_event.
Sequential single-stream loop eliminates 4 event sync calls per step.
H100 forward pass occupies 100+ SMs — env_step overlap was not profitable.

cuda_streams: sync_all() reduced from N blocking cuStreamSynchronize
calls to 1 via cuEventRecord fan-in on stream[0]. 118 tests pass.

Estimated 15-20% walltime reduction in backtest evaluation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 07:50:00 +01:00
jgrusewski
bc0f1f2bd8 perf(cuda): pre-allocate GPU buffers in PPO collector — eliminate per-epoch malloc
reset_episodes() was allocating 5 CPU Vecs per epoch for HtoD zero-fill.
Replaced with:
- memset_zeros() for barrier/diversity buffers (async GPU-side, no HtoD)
- Pre-allocated host staging vectors for portfolio_init and RNG seeds

gpu_curiosity_trainer already optimized (fused kernel zeros internally).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 07:46:38 +01:00
jgrusewski
08eb6d5cc2 refactor(dedup): consolidate activations + AdamW kernels — PPO→ml-core
Activations: ml-ppo/cuda_nn/activations.rs 208→60 lines (-71%).
Deleted 4 inline CUDA kernels, replaced with thin wrappers delegating
to ml-core::cuda_autograd::ActivationKernels via new _raw methods.

AdamW: Unified kernel source between ml-ppo and ml-core. Switched to
architecture-aware SASS compilation (compile_ptx_for_device). Fixed
weight decay from L2-regularization to true decoupled AdamW formula.

Added GpuTensor::into_parts() for zero-copy CudaSlice extraction.
Added ActivationKernels::relu_fwd_raw/bwd_raw/tanh_fwd_raw/sigmoid_fwd_raw
for direct CudaSlice operation without GpuTensor wrapping.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 07:41:22 +01:00
jgrusewski
e068224830 refactor(dedup): consolidate GpuTensor/GpuLinear — ml-supervised→ml-core
Moved ml-supervised's duplicate GpuTensor (stream-carrying) + GpuLinear +
50 free functions to ml-core/cuda_autograd/stream_ops.rs as StreamTensor
and StreamLinear. ml-supervised/gpu_tensor.rs → 53 lines of re-exports.

Two tensor flavors now canonical in ml-core:
- GpuTensor: takes &Arc<CudaStream> per-call (autograd integration)
- StreamTensor: carries Arc<CudaStream> internally (self-contained ops)

Zero consumer import changes. Both crates compile clean.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 07:37:49 +01:00
jgrusewski
6d8ba0708c fix(ml-supervised): resolve all 104 compile errors — clean build
- mamba/mod.rs: ~90 errors fixed — _candle suffixed functions replaced,
  operator overloads→free functions, autograd→pseudo-gradients,
  checkpoint→JSON serialization
- gpu_tensor.rs: added gpu_eye, gpu_cat_dim0, gpu_stack_tensors
- TFT/SSD: unused imports cleaned, type mismatches fixed
- ml-core: GpuTensor algebra methods (17 new), cuda_compat.rs deleted,
  GpuVarStore::vars/all_vars/linear_xavier added

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 07:26:56 +01:00
jgrusewski
dd62f3fcfd refactor: eliminate candle from entire workspace — tests, examples, Cargo.toml
Final cleanup:
- 61 test files + 5 example files: candle imports replaced
- 8 testing/integration files: migrated to cudarc/ml-core types
- 3 services/trading_service test files: migrated
- Root Cargo.toml: candle-core, candle-nn removed from [workspace.dependencies]
- crates/ml/Cargo.toml: candle-nn dependency removed
- testing/e2e/Cargo.toml: candle-core dependency removed

Zero active candle_core/candle_nn/candle_optimisers code references remain.
Zero candle dependency declarations in any Cargo.toml.
Remaining "candle" strings are exclusively in doc comments.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 00:53:47 +01:00
jgrusewski
0d62bdba2e refactor(ml): eliminate candle from all 104 src files
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>
2026-03-18 00:24:35 +01:00
jgrusewski
20319618b6 refactor(ml-supervised): eliminate candle — GpuTensor/GpuLinear throughout
Zero candle_core/candle_nn imports remain. All 13 source files migrated:

- TFT: GatedResidualNetwork, LSTMEncoder, TemporalAttention → GpuLinear
- Mamba2: SSM layers → GpuLinear + local GpuTensor ops, grad norm host-side
- Liquid CfC: Training rewritten with perturbation-based gradient estimation
- SSD layer: Constructor takes Arc<CudaStream> not VarBuilder
- gpu_tensor.rs: from_candle/to_candle bridge methods deleted

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 23:32:39 +01:00
jgrusewski
bf838976eb refactor(ml-dqn): eliminate candle — pure cudarc + GpuTensor/GpuLinear
Zero candle_core/candle_nn/candle_optimisers imports remain. All 25 source
files migrated to ml-core cuda_autograd types:

- Network structs: Vec<Linear> → Vec<GpuLinear>
- DQNAgent: VarMap→GpuVarStore, Adam→GpuAdamW, Device→MlDevice
- GpuReplayBuffer: Candle Tensor wrappers deleted, returns CudaSlice directly
- NoisyLinear: cudarc-native noise buffers
- All softmax/logit/entropy functions: &Tensor → &[f32]
- Module trait impls deleted, forward() is direct method

316 implementation-level errors remain (missing GpuTensor algebra methods:
argmax, gather, unsqueeze, etc.) — these are next-layer work, not candle deps.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 23:12:18 +01:00
jgrusewski
22004a7368 refactor(cuda): eliminate candle from ml-core, ml-ppo, and 4 thin crates
Hard refactor — no shims, no compat layers. Candle removed from Cargo.toml
and all source files in 6 crates:

- ml-core: MlDevice enum, checkpoint.rs (safetensors direct), cudarc imports
  fixed from candle re-export to direct, AdamWConfig lr_decay, cuda_compat
  gutted. Net -7,341 lines.
- ml-ppo: All 16 files rewritten. LSTM→CudaLSTM, VarMap→GpuVarStore,
  PPOAgent 2306→700 lines, checkpoint→binary format.
- ml-ensemble: GPU-resident sigmoid via custom CUDA kernel.
- ml-explainability: Integrated gradients via GPU finite-difference kernels.
- ml-labeling: Device→MlDevice.
- ml-hyperopt: Cargo.toml only.

Remaining: ml-dqn (24 files), ml-supervised (4 files), ml crate (104 files).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 22:27:56 +01:00
jgrusewski
622d7cd757 refactor(cuda): replace Candle internals with GpuLinear/GpuTensor in model adapters
Convert forward paths of TGGN, Liquid CfC, and multi-timeframe LSTM
encoder from Candle Linear/Module/ops to cuBLAS-backed GpuLinear and
gpu_* element-wise ops from ml-supervised. Candle Tensor remains at
the UnifiedTrainable trait boundary; VarMap/AdamW retained for autograd.

- TGGN: forward uses GpuLinear + gpu_relu instead of candle_nn::Linear
- Liquid CfC: RNN loop uses GpuLinear + gpu_sigmoid/gpu_tanh/gpu_mul
- MultiTimeframeEncoder: LSTM gates use gpu_matmul + gpu_sigmoid/gpu_tanh
- Checkpoints save GpuLinear weights as JSON instead of safetensors
- Remove candle_nn::Module, candle_nn::Linear from adapter imports

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 19:08:45 +01:00
jgrusewski
8a6bca399c refactor(cuda): replace Tensor→CudaSlice conversions with direct accessors in trainers/
Add CudaSlice<f32> accessor methods to GpuTrainResult and GradientResult
(loss_cuda_slice, grad_norm_cuda_slice) in ml-dqn, eliminating redundant
tensor_to_cuda_slice_f32 calls at every training guard check site.

- GpuTrainResult: add from_fused_scalars() constructor and CudaSlice extractors
- GradientResult: add CudaSlice extractors that delegate to GpuTrainResult
- fused_training.rs: use from_fused_scalars() instead of Tensor::new() wrapping
- train_step.rs: use result.loss_cuda_slice() instead of tensor_to_cuda_slice_f32
- training_loop.rs: same CudaSlice accessor pattern for both fused and accum paths

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 19:01:02 +01:00
jgrusewski
31a7c1b171 refactor(cuda): remove Candle intermediaries from cuda_pipeline weight extraction
- gpu_weights.rs: Remove slow-path Candle fallback (flatten/cast/contiguous)
  from extract_one() and sync_one(). F32-contiguous is now a hard requirement
  enforced by ensure_f32_contiguous() at network construction. Non-F32 or
  non-contiguous tensors produce a clear error instead of silently falling
  back to the Candle conversion pipeline.

- gpu_action_selector.rs: Remove unused ensure_contiguous_f32(). Keep
  stream_from_device/cuda_u32_to_tensor/cuda_f32_to_tensor as boundary
  converters (they have external callers).

- gpu_experience_collector.rs: Clean up doc comments referencing Candle
  where only cudarc CudaSlice is used.

- mod.rs: Update module docs to clarify Candle Tensor/Device are only
  at the upload boundary (DqnGpuData, PpoGpuData, tensor_to_cuda_slice).

- tlob/trainable_adapter.rs: Fix double candle_core:: path that broke build.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 18:57:53 +01:00
jgrusewski
2fb9220576 fix(dqn): F32 master weights across all DQN networks
All VarBuilder/NoisyLinear init changed from BF16 to F32.
The fused CUDA trainer's Adam kernel operates on F32 master weights
with separate BF16 mirrors for forward kernels. BF16 VarBuilder
caused dtype mismatches in Candle forward paths and broke ensure_f32.

381 ml-dqn tests pass, 0 errors, 0 warnings workspace-wide.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 18:32:25 +01:00
jgrusewski
c326b7f654 refactor(cuda): migrate Candle references to native cudarc in ml crate
Replace deprecated cudarc memcpy_stod with clone_htod across all GPU
upload paths (14 call sites in trainers, cuda_pipeline, hyperopt).
Replace deprecated memcpy_dtov with clone_dtoh in ml-supervised
gpu_tensor.rs.

Bridge GpuTensor-migrated submodules (xLSTM, Liquid CfC, TFT GRN)
with Candle Tensor callers via from_candle_tensor/to_candle_tensor
conversion utilities at API boundaries. Fix CudaDevice->CudaContext
in ml-dqn distributional_dueling.rs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 18:20:26 +01:00
jgrusewski
e459e108ba refactor(ml-dqn): migrate RMSNorm, LayerNorm, ResidualBlock from Candle to cuda_autograd
Replace VarMap/VarBuilder weight storage with GpuVarStore (native CUDA)
for RMSNorm, LayerNorm, and ResidualBlock. Linear layers in ResidualBlock
now use GpuLinear with cuBLAS sgemm. Cold-path forwards convert between
GpuTensor and Candle Tensor at the boundary since downstream ops (GELU,
LayerNorm, dropout, broadcast) still use Candle.

Changes:
- ml-core cuda_autograd: add GpuTensor::to_candle/from_candle interop,
  export GpuParam/AdamWConfig/ActivationKernels/LossKernels/LossResult,
  make init::upload_to_gpu public
- rmsnorm.rs: RMSNorm/LayerNorm now take (Arc<CudaStream>, Device, dim)
  instead of (VarBuilder, dim). Weights stored in GpuVarStore.
- residual.rs: ResidualBlock now takes (Arc<CudaStream>, Device, config, name).
  fc1/fc2 are GpuLinear with cuBLAS sgemm forward. LayerNorm params in GpuVarStore.
- distributional_dueling.rs: updated RMSNorm constructor calls to new API

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 18:14:46 +01:00
jgrusewski
8b11b7046f feat(ppo): wire cuda_nn into all PPO model code
Replace PolicyNetwork and ValueNetwork with CudaPolicyNetwork/CudaValueNetwork
backed implementations. Each struct now stores a cuda_nn GPU-native network
(cuBLAS sgemm + CUDA kernels) alongside shadow Candle layers for autograd
training compatibility. Adds forward_cuda() inference paths that bypass Candle
entirely. Wire CudaTrajectoryTensors into TrajectoryBatch with to_cuda_tensors().
Re-export CudaLSTM from lstm_networks and all cuda_nn types from crate root.
Import GpuContext into continuous_policy, continuous_action_masking, flow_policy,
coupling_layer, and adaptive_entropy for future GPU migration. Add CudaVec::to_tensor()
bridge for cuda_nn→Candle interop. Fix upload_host_to_gpu→upload_to_gpu rename
in ml-core cuda_autograd init.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 17:57:37 +01:00
jgrusewski
1627ca57a1 fix(dqn): init weights as F32, not BF16 — fixes ensure_f32 Var::set crash
VarBuilder and NoisyLinear were creating BF16 weights, then ensure_f32
tried Var::set() which rejects dtype changes. Fix: create F32 from the
start. BF16 mirrors are managed separately by GpuDqnTrainer.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 15:34:19 +01:00
jgrusewski
0e2f82ab54 feat(cuda): complete Candle elimination + cudarc 0.19.3 upgrade
Integration of 7 hive agents:
- gpu_replay_buffer: 103 Candle refs → 0 (14 new CUDA kernels)
- gpu_action_selector: 27 refs → CudaSlice API
- signal_adapter: 26 refs → 3 new CUDA kernels
- gpu_experience_collector: 5 refs → CudaSlice output
- gpu_weights+iql+guard: 13 refs eliminated
- DQN forward: new forward_only_kernel for inference
- VarMap: F32 contiguous enforcement, fast-path extraction

New modules:
- ml-core/cuda_autograd: GpuTensor, GpuVarStore, GpuLinear, GpuAdamW
- ml-ppo/cuda_nn: CudaLinear, CudaLSTM, CudaAdam, networks
- ml-supervised/gpu_tensor: GpuTensor + cuBLAS for KAN, Diffusion

cudarc 0.17.3 → 0.19.3 (via candle 0.9.1 → 0.9.2)
safetensors 0.4 → 0.7

Zero errors, zero warnings workspace-wide.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 15:13:04 +01:00
jgrusewski
f92c9d5f79 feat(cuda): replace Candle with cudarc CudaSlice + cuBLAS in KAN and Diffusion models
Eliminate candle-core/candle-nn from the KAN and Diffusion model forward
paths in ml-supervised. All dense layers now use cuBLAS sgemm via the new
gpu_tensor module. Element-wise ops (SiLU, sigmoid, tanh, exp) use host
roundtrips for now; fused CUDA kernels are a follow-up.

Changes:
- Add gpu_tensor.rs: GpuTensor (CudaSlice<f32> + shape), GpuLinear
  (cuBLAS sgemm), and ~30 element-wise GPU ops
- Rewrite kan/{spline,layer,network}.rs to use GpuTensor instead of
  candle_core::Tensor and candle_nn::{VarBuilder,Linear}
- Rewrite diffusion/{denoiser,noise,sampler}.rs to use GpuTensor and
  GpuLinear instead of candle_nn::Linear
- Update ml crate trainable adapters (kan/trainable.rs,
  diffusion/trainable.rs) to bridge Candle<->GpuTensor at the
  UnifiedTrainable interface boundary
- Update ensemble inference adapters for both models
- Add cudarc 0.17 with cublas feature to ml-supervised Cargo.toml
- Candle deps retained in ml-supervised for unconverted models (TFT,
  Liquid, Mamba, xLSTM) -- will be removed once all 8 models are
  converted

Net: -424 lines, 509 -> ~453 Candle refs remaining (KAN: 0, Diffusion: 0)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 14:58:44 +01:00
jgrusewski
ad63c67499 feat(cuda): add cuda_autograd module — cudarc-native replacement for Candle autograd
Replace Candle's backward()/GradStore/VarMap/VarBuilder system with CUDA-native
primitives in ml-core::cuda_autograd. This eliminates the Candle dispatch overhead
(~2100 kernel launches per batch) for models that adopt the new API.

Components:
- GpuTensor: CudaSlice<f32> wrapper with shape metadata (replaces candle Tensor)
- GpuVarStore: named parameter store with flatten/unflatten (replaces VarMap/VarBuilder)
- GpuLinear: cuBLAS sgemm forward + manual backward (replaces candle_nn::Linear)
- GpuAdamW: per-parameter CUDA kernel optimizer (replaces candle_optimisers::Adam)
- ActivationKernels: ReLU/LeakyReLU/GELU/Sigmoid/Tanh forward+backward CUDA kernels
- LossKernels: MSE/Huber loss with fused gradient computation
- init: Xavier/Kaiming/near-zero initialization via CPU generate + GPU upload

Added cublas feature to ml-core's cudarc dependency for sgemm support.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 14:57:54 +01:00
jgrusewski
0ed2277319 feat(cuda): add CudaTrajectoryTensors and finalize cuda_nn module
Adds CudaTrajectoryTensors — GPU-resident trajectory batch storage that
replaces the Candle-based TrajectoryTensors. All trajectory data (states,
actions, log_probs, advantages, returns) uploaded as CudaSlice<f32> with
zero Candle tensor overhead.

The cuda_nn module is now complete with all primitives needed to replace
Candle in the PPO training loop:
- CudaLinear (cuBLAS sgemm)
- CudaLSTM (fused gate kernel)
- CudaPolicyNetwork / CudaValueNetwork (MLP stacks)
- CudaAdam (GPU-resident optimizer)
- CudaTrajectoryTensors (GPU batch storage)
- Softmax / log-softmax / ReLU / tanh / sigmoid kernels

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 14:37:44 +01:00
jgrusewski
43e10c8887 feat(cuda): add forward-only CUDA kernel for DQN inference, mark Candle forward paths as cold
Add dqn_forward_only_kernel to dqn_training_kernel.cu — runs a single
branching forward pass (shared layers -> value head -> 3 branch heads ->
C51 distributional -> expected Q) and writes per-branch Q-values to
q_out[B, 11]. No loss computation, no activation saves, no backward.
~3x faster than forward_loss for pure inference.

Wire the kernel into GpuDqnTrainer via forward_only_q() which takes
CudaSlice<f32> states directly and returns &CudaSlice<f32> Q-values,
replacing the Candle BranchingDuelingQNetwork::forward_branches() call
chain (~160 Candle kernel dispatches -> 1 fused CUDA launch).

Mark all 6 ml-dqn Candle forward methods as #[cold] with documentation
that hot-path compute is handled by fused CUDA kernels:
- noisy_layers.rs: NoisyLinear::forward() -> dqn_experience_kernel.cu
- quantile_regression.rs: QuantileNetwork::forward() -> iqn_dual_head_kernel.cu
- distributional.rs: CategoricalDistribution::to_scalar() -> dqn_forward_only_kernel
- curiosity.rs: ForwardDynamicsModel::predict() -> curiosity_training_kernel.cu
- residual.rs: ResidualBlock::forward() -> dqn_forward_only_kernel
- rmsnorm.rs: RMSNorm::forward() / LayerNorm::forward() -> dqn_experience_kernel.cu

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 14:36:40 +01:00