use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_1D;
use crate::device::compute_pipeline::ComputeDispatch;
use crate::error::Result;
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct TreeParamsGpu {
n_samples: u32,
n_features: u32,
n_nodes_max: u32,
n_trees: u32,
max_depth: u32,
_pad0: u32,
_pad1: u32,
_pad2: u32,
}
pub struct FlatForest {
pub feature_idx: Vec<u32>,
pub thresholds: Vec<f64>,
pub left_child: Vec<i32>,
pub right_child: Vec<i32>,
pub predictions: Vec<u32>,
pub tree_offsets: Vec<u32>,
}
impl FlatForest {
#[must_use]
pub fn single_tree(
feature_idx: Vec<u32>,
thresholds: Vec<f64>,
left_child: Vec<i32>,
right_child: Vec<i32>,
predictions: Vec<u32>,
) -> Self {
Self {
feature_idx,
thresholds,
left_child,
right_child,
predictions,
tree_offsets: vec![0],
}
}
#[must_use]
pub fn n_trees(&self) -> usize {
self.tree_offsets.len()
}
#[must_use]
pub fn n_nodes(&self) -> usize {
self.left_child.len()
}
}
pub struct TreeInferenceGpu {
device: Arc<WgpuDevice>,
}
impl TreeInferenceGpu {
#[must_use]
pub fn new(device: &WgpuDevice) -> Self {
Self::with_device(Arc::new(device.clone()))
}
#[must_use]
pub fn with_device(device: Arc<WgpuDevice>) -> Self {
Self { device }
}
pub fn predict(
&self,
forest: &FlatForest,
samples: &[f64],
n_samples: usize,
) -> Result<Vec<u32>> {
let dev = &self.device;
let n_trees = forest.n_trees();
let n_nodes = forest.n_nodes();
let n_feat = samples.len() / n_samples;
let upload = |data: &[u8], label: &str| {
dev.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some(label),
contents: data,
usage: wgpu::BufferUsages::STORAGE,
})
};
let samples_buf = upload(bytemuck::cast_slice(samples), "Tree samples");
let feat_buf = upload(bytemuck::cast_slice(&forest.feature_idx), "Tree feat_idx");
let thresh_buf = upload(bytemuck::cast_slice(&forest.thresholds), "Tree thresholds");
let left_buf = upload(bytemuck::cast_slice(&forest.left_child), "Tree left");
let right_buf = upload(bytemuck::cast_slice(&forest.right_child), "Tree right");
let pred_buf = upload(bytemuck::cast_slice(&forest.predictions), "Tree preds");
let offsets_buf = upload(bytemuck::cast_slice(&forest.tree_offsets), "Tree offsets");
let output_buf = dev.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Tree output"),
size: (n_samples * n_trees * std::mem::size_of::<u32>()) as u64,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let params = TreeParamsGpu {
n_samples: n_samples as u32,
n_features: n_feat as u32,
n_nodes_max: n_nodes as u32,
n_trees: n_trees as u32,
max_depth: 32,
_pad0: 0,
_pad1: 0,
_pad2: 0,
};
let params_buf = dev
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Tree params"),
contents: bytemuck::bytes_of(¶ms),
usage: wgpu::BufferUsages::UNIFORM,
});
let total_threads = (n_samples * n_trees) as u32;
ComputeDispatch::new(dev, "tree_inference")
.shader(
include_str!("../../shaders/bio/tree_inference_f64.wgsl"),
"main",
)
.f64()
.uniform(0, ¶ms_buf)
.storage_read(1, &samples_buf)
.storage_read(2, &feat_buf)
.storage_read(3, &thresh_buf)
.storage_read(4, &left_buf)
.storage_read(5, &right_buf)
.storage_read(6, &pred_buf)
.storage_read(7, &offsets_buf)
.storage_rw(8, &output_buf)
.dispatch(total_threads.div_ceil(WORKGROUP_SIZE_1D), 1, 1)
.submit()?;
dev.read_buffer_u32(&output_buf, n_samples * n_trees)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::device::test_pool;
fn simple_stump() -> FlatForest {
FlatForest::single_tree(
vec![0, 0, 0], vec![0.5, 0.0, 0.0], vec![1, -1, -1], vec![2, -1, -1], vec![99, 0, 1], )
}
#[tokio::test]
async fn test_stump_two_samples() {
let Some(device) = test_pool::get_test_device_if_f64_gpu_available().await else {
return;
};
let infer = TreeInferenceGpu::new(&device);
let forest = simple_stump();
let samples = vec![0.3_f64, 0.8];
let output = infer.predict(&forest, &samples, 2).unwrap();
assert_eq!(output[0], 0, "sample 0 should be class 0");
assert_eq!(output[1], 1, "sample 1 should be class 1");
}
#[tokio::test]
async fn test_deeper_tree() {
let Some(device) = test_pool::get_test_device_if_f64_gpu_available().await else {
return;
};
let infer = TreeInferenceGpu::new(&device);
let forest = FlatForest::single_tree(
vec![0, 1, 0, 0, 1, 0, 0], vec![0.5, 0.5, 0.0, 0.0, 0.5, 0.0, 0.0],
vec![1, 2, -1, -1, 5, -1, -1],
vec![4, 3, -1, -1, 6, -1, -1],
vec![99, 99, 0, 1, 99, 2, 3],
);
let samples = vec![0.3_f64, 0.3, 0.3, 0.8, 0.8, 0.3, 0.8, 0.8];
let output = infer.predict(&forest, &samples, 4).unwrap();
assert_eq!(output[0], 0, "left-left → class 0");
assert_eq!(output[1], 1, "left-right → class 1");
assert_eq!(output[2], 2, "right-left → class 2");
assert_eq!(output[3], 3, "right-right → class 3");
}
}