trueno/backends/gpu/
mod.rs1#![allow(missing_docs)]
2#[cfg(any(feature = "gpu", feature = "gpu-wasm"))]
31mod batch;
32
33#[cfg(any(feature = "gpu", feature = "gpu-wasm"))]
34mod device;
35
36#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
37mod pool;
38
39#[cfg(any(feature = "gpu", feature = "gpu-wasm"))]
40pub mod shaders;
41
42#[cfg(any(feature = "gpu", feature = "gpu-wasm"))]
43pub mod runtime;
44
45mod partition_view;
47mod tensor_view;
48mod tiled_reduction;
49
50pub use partition_view::{PartitionView, TileInfo};
51pub use tensor_view::{MemoryLayout, TensorView};
52pub use tiled_reduction::{
53 tiled_max_2d, tiled_min_2d, tiled_reduce_2d, tiled_reduce_partial, tiled_sum_2d, MaxOp, MinOp,
54 ReduceOp, SumOp, TILE_SIZE,
55};
56
57#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
58pub use batch::{BufferId, GpuCommandBatch, PipelineCache};
59
60#[cfg(any(feature = "gpu", feature = "gpu-wasm"))]
62pub use device::GpuDevice;
63
64#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
67pub(crate) use device::shared_instance;
68
69#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
73pub(crate) use device::gpu_backends;
74
75#[cfg(any(feature = "gpu", feature = "gpu-wasm"))]
78pub use wgpu;
79
80#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
81pub use pool::GpuDevicePool;
82
83#[cfg(any(feature = "gpu", feature = "gpu-wasm"))]
85pub use device::linalg::cached_matmul::GpuMatmulCache;
86
87#[cfg(any(feature = "gpu", feature = "gpu-wasm"))]
89pub use device::linalg::wgsl_forward::{QkvLoRA, WgslForwardPass};
90
91#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
92mod backend_ops;
93
94#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
96#[derive(Clone)]
97pub struct GpuBackend {
98 device: Option<GpuDevice>,
99}
100
101#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
102impl GpuBackend {
103 pub fn new() -> Self {
105 Self { device: None }
106 }
107
108 fn ensure_device(&mut self) -> Result<&GpuDevice, String> {
110 if self.device.is_none() {
111 self.device = Some(GpuDevice::new()?);
112 }
113 Ok(self.device.as_ref().expect("device initialized above"))
114 }
115
116 pub fn is_available() -> bool {
118 GpuDevice::is_available()
119 }
120}
121
122#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
123impl Default for GpuBackend {
124 fn default() -> Self {
125 Self::new()
126 }
127}
128
129#[cfg(any(not(feature = "gpu"), target_arch = "wasm32"))]
131#[derive(Clone)]
132pub struct GpuBackend;
133
134#[cfg(any(not(feature = "gpu"), target_arch = "wasm32"))]
135impl GpuBackend {
136 pub fn new() -> Self {
137 Self
138 }
139
140 pub fn is_available() -> bool {
141 false
142 }
143}
144
145#[cfg(any(not(feature = "gpu"), target_arch = "wasm32"))]
146impl Default for GpuBackend {
147 fn default() -> Self {
148 Self
149 }
150}
151
152#[cfg(test)]
154#[cfg(not(feature = "gpu"))]
155mod stub_tests {
156 use super::*;
157
158 #[test]
159 fn test_gpu_backend_stub_new() {
160 let _backend = GpuBackend::new();
161 }
162
163 #[test]
164 fn test_gpu_backend_stub_is_available() {
165 assert!(!GpuBackend::is_available());
166 }
167
168 #[test]
169 fn test_gpu_backend_stub_default() {
170 let _ = GpuBackend;
171 }
172
173 #[test]
174 fn test_gpu_backend_stub_clone() {
175 let backend = GpuBackend::new();
176 let _cloned = backend.clone();
177 }
178}
179
180#[cfg(test)]
181#[cfg(feature = "gpu")]
182mod tests_gpu;