//! CUDA-native autograd replacement for Candle's backward/GradStore/VarMap system.
//!
//! Provides GPU-resident weight storage, initialization, linear layers with
//! cuBLAS-backed forward and manual backward passes, activation functions,
//! loss functions, and an AdamW optimizer — all using raw `cudarc` primitives
//! with zero Candle tensor intermediaries.
//!
//! ## Architecture
//!
//! - [`GpuTensor`]: Thin wrapper around `CudaSlice<f32>` with shape metadata.
//! - [`GpuVarStore`]: Named parameter store replacing `VarMap`/`VarBuilder`.
//! - [`GpuLinear`]: Dense layer using cuBLAS sgemm for forward, manual backward.
//! - [`GpuAdamW`]: Fused AdamW optimizer running entirely on GPU.
//! - [`activations`]: ReLU, LeakyReLU, GELU, Sigmoid, Tanh as element-wise CUDA kernels.
//! - [`loss`]: MSE, Huber, CrossEntropy loss with gradient output.
//! - [`init`]: Xavier/Kaiming initialization via CPU generate + GPU upload.
//!
//! ## Design decisions
//!
//! 1. **No autograd tape**: Instead of recording a computation graph and replaying
//!    it backward (Candle's approach), each layer implements explicit `backward()`
//!    methods that compute `dL/dW`, `dL/db`, and `dL/dx` given saved activations.
//!    This is more memory-efficient and CUDA Graph-friendly.
//!
//! 2. **Flat parameter buffer**: `GpuVarStore` can optionally flatten all parameters
//!    into a contiguous `CudaSlice<f32>` for single-kernel Adam updates (matching
//!    the DQN fused trainer pattern).
//!
//! 3. **cuBLAS for matmul**: Linear forward/backward use cuBLAS sgemm rather than
//!    custom CUDA kernels — cuBLAS is already tuned for tensor core utilization.

#![allow(unsafe_code)] // CUDA FFI requires unsafe for kernel launches and cuBLAS calls.

pub mod gpu_tensor;
pub mod var_store;
pub mod optimizer;
pub mod init;
pub mod linear;
pub mod activations;
pub mod loss;
pub mod dropout;
pub mod layer_norm;
pub mod reductions;
pub mod elementwise;
pub mod stream_ops;

pub use gpu_tensor::GpuTensor;
pub use gpu_tensor::ScalarValue;
pub use var_store::GpuVarStore;
pub use var_store::GpuParam;
pub use optimizer::GpuAdamW;
pub use optimizer::AdamWConfig;
pub use linear::GpuLinear;
pub use linear::LinearActivations;
pub use linear::LinearGrads;
pub use linear::clone_gpu_tensor;
pub use activations::ActivationKernels;
pub use loss::LossKernels;
pub use loss::LossResult;
pub use dropout::GpuDropout;
pub use layer_norm::GpuLayerNorm;
pub use reductions::ReductionKernels;
pub use reductions::Stats as ReductionStats;
pub use elementwise::ElementwiseKernels;
pub use stream_ops::StreamTensor;
pub use stream_ops::StreamLinear;
