spark_model/
mistral_loader.rs1use anyhow::Result;
10use spark_runtime::gpu::{DevicePtr, GpuBackend};
11
12use crate::layers::ops;
13use crate::weight_map::DenseWeight;
14
15pub struct MistralWeightLoader;
16
17pub(crate) fn gpu_alloc_or_managed(gpu: &dyn GpuBackend, bytes: usize) -> Result<DevicePtr> {
22 if gpu.op_cache().alloc_fell_back() {
27 return gpu.alloc_managed(bytes);
28 }
29 match gpu.alloc(bytes) {
30 Ok(p) => Ok(p),
31 Err(_) => {
32 tracing::warn!(
33 "GPU alloc failed ({bytes} bytes) — switching to managed for remaining allocations"
34 );
35 gpu.op_cache().note_alloc_fallback();
36 gpu.alloc_managed(bytes)
37 }
38 }
39}
40
41#[allow(dead_code)]
42fn gpu_matmul(
43 a: DevicePtr,
44 b: DevicePtr,
45 m: usize,
46 n: usize,
47 k: usize,
48 gpu: &dyn GpuBackend,
49) -> Result<DevicePtr> {
50 let bf16 = 2usize;
51 let c = gpu_alloc_or_managed(gpu, m * n * bf16)?;
52 let stream = gpu.default_stream();
53 let gemm_k = gpu.kernel("gemm", "dense_gemm_bf16")?;
54 let b_dense = DenseWeight { weight: b };
55 ops::dense_gemm(
56 gpu, gemm_k, a, &b_dense, c, m as u32, n as u32, k as u32, stream,
57 )?;
58 gpu.synchronize(stream)?;
59 Ok(c)
60}
61
62pub(crate) mod loader_impl;