Skip to main content

lyndon_cl/
lib.rs

1#[cfg(test)]
2mod test {
3    use std::marker::PhantomData;
4
5    use cubecl::{
6        benchmark::{Benchmark, TimingMethod},
7        future,
8        prelude::*,
9        server::Handle,
10        std::tensor::compact_strides,
11        wgpu::{WgpuDevice, WgpuRuntime},
12        zspace::Strides,
13    };
14    pub struct CpuTensor {
15        pub data: Vec<f32>,
16        pub strides: Strides,
17        pub shape: Vec<usize>,
18    }
19
20    impl CpuTensor {
21        pub fn arange(shape: Vec<usize>) -> Self {
22            let size = shape.iter().product();
23            let data = (0..size).map(|i| i as f32).collect();
24            let strides = compact_strides(&shape);
25            Self {
26                data,
27                strides,
28                shape,
29            }
30        }
31
32        pub fn empty(shape: Vec<usize>) -> Self {
33            let size = shape.iter().product();
34            let data = vec![0.0; size];
35            let strides = compact_strides(&shape);
36            Self {
37                data,
38                strides,
39                shape,
40            }
41        }
42
43        pub fn read(self) -> Vec<f32> {
44            self.data
45        }
46    }
47
48    pub fn reduce_matrix(input: &CpuTensor, output: &mut CpuTensor) {
49        for i in 0..input.shape[0] {
50            let mut acc = 0.0f32;
51            for j in 0..input.shape[1] {
52                acc += input.data[i * input.strides[0] + j];
53            }
54            output.data[i] = acc;
55        }
56    }
57
58    #[derive(Debug)]
59    pub struct GpuTensor<R: Runtime, F: Float + CubeElement> {
60        data: Handle,
61        shape: Vec<usize>,
62        strides: Strides,
63        _r: PhantomData<R>,
64        _f: PhantomData<F>,
65    }
66
67    impl<R: Runtime, F: Float + CubeElement> Clone for GpuTensor<R, F> {
68        fn clone(&self) -> Self {
69            Self {
70                data: self.data.clone(), // Handle is a pointer to the data, so cloning it is cheap
71                shape: self.shape.clone(),
72                strides: self.strides.clone(),
73                _r: PhantomData,
74                _f: PhantomData,
75            }
76        }
77    }
78
79    impl<R: Runtime, F: Float + CubeElement> GpuTensor<R, F> {
80        /// Create a `GpuTensor` with a shape filled by number in order
81        pub fn arange(shape: Vec<usize>, client: &ComputeClient<R>) -> Self {
82            let size = shape.iter().product();
83            let data: Vec<F> = (0..size).map(|i| F::from_int(i as i64)).collect();
84            let data = client.create_from_slice(F::as_bytes(&data));
85
86            let strides = compact_strides(&shape);
87            Self {
88                data,
89                shape,
90                strides,
91                _r: PhantomData,
92                _f: PhantomData,
93            }
94        }
95
96        /// Create an empty GpuTensor with a shape
97        pub fn empty(shape: Vec<usize>, client: &ComputeClient<R>) -> Self {
98            let size = shape.iter().product::<usize>() * core::mem::size_of::<F>();
99            let data = client.empty(size);
100
101            let strides = compact_strides(&shape);
102            Self {
103                data,
104                shape,
105                strides,
106                _r: PhantomData,
107                _f: PhantomData,
108            }
109        }
110
111        /// Create a TensorArg to pass to a kernel
112        pub fn into_tensor_arg(&self) -> TensorArg<R> {
113            unsafe {
114                TensorArg::from_raw_parts(
115                    self.data.clone(),
116                    self.strides.clone(),
117                    self.shape.clone().into(),
118                )
119            }
120        }
121
122        /// Return the data from the client
123        pub fn read(self, client: &ComputeClient<R>) -> Vec<F> {
124            let bytes = client.read_one(self.data).unwrap();
125            F::from_bytes(&bytes).to_vec()
126        }
127    }
128
129    pub struct ReductionBench<R: Runtime, F: Float + CubeElement> {
130        input_shape: Vec<usize>,
131        client: ComputeClient<R>,
132        _f: PhantomData<F>,
133    }
134
135    const LINE_SIZE: u32 = 4;
136
137    impl<R: Runtime, F: Float + CubeElement> Benchmark for ReductionBench<R, F> {
138        type Input = GpuTensor<R, F>;
139        type Output = GpuTensor<R, F>;
140
141        fn prepare(&self) -> Self::Input {
142            GpuTensor::<R, F>::arange(self.input_shape.clone(), &self.client)
143        }
144
145        fn name(&self) -> String {
146            format!("{}-reduction-{:?}", R::name(&self.client), self.input_shape).to_lowercase()
147        }
148
149        fn sync(&self) {
150            let _ = future::block_on(self.client.sync());
151        }
152
153        fn execute(&self, input: Self::Input) -> Result<Self::Output, String> {
154            let output_shape: Vec<usize> = vec![self.input_shape[0]];
155            let output = GpuTensor::<R, F>::empty(output_shape, &self.client);
156
157            unsafe {
158                reduce_matrix_gpu::launch_unchecked::<F, R>(
159                    &self.client,
160                    CubeCount::Static(self.input_shape[0] as u32, 1, 1),
161                    CubeDim::new_3d(self.input_shape[1] as u32, 1, 1),
162                    LINE_SIZE as usize,
163                    input.into_tensor_arg(),
164                    output.into_tensor_arg(),
165                );
166            }
167
168            Ok(output)
169        }
170    }
171
172    #[cube(launch_unchecked)]
173    pub fn reduce_matrix_gpu<F: Float, N: Size>(
174        input: &Tensor<Vector<F, N>>,
175        output: &mut Tensor<Vector<F, N>>,
176    ) {
177        let mut acc = Vector::new(F::new(0.0f32));
178        for i in 0..input.shape(2) / LINE_SIZE as usize {
179            acc = acc
180                + input[CUBE_POS_X as usize * input.stride(0)
181                    + UNIT_POS_X as usize * input.stride(1)
182                    + i];
183        }
184        output[CUBE_POS_X as usize * output.stride(0) + UNIT_POS_X as usize] = acc;
185    }
186
187    #[test]
188    fn test_doing_stuff_on_the_cpu() {
189        let input_shape = vec![3, 3];
190        let output_shape = vec![3];
191        let input = CpuTensor::arange(input_shape);
192        let mut output = CpuTensor::empty(output_shape);
193
194        reduce_matrix(&input, &mut output);
195
196        println!("Executed reduction => {:?}", output.read());
197    }
198
199    #[test]
200    fn test_doing_stuff_on_the_gpu() {
201        let client = WgpuRuntime::client(&WgpuDevice::default());
202        let input = GpuTensor::<WgpuRuntime, f32>::arange(vec![3, 3], &client);
203        let output = GpuTensor::<WgpuRuntime, f32>::empty(vec![3, 3], &client);
204
205        unsafe {
206            reduce_matrix_gpu::launch_unchecked::<f32, WgpuRuntime>(
207                &client,
208                CubeCount::Static(1, 1, 1),
209                CubeDim::new_3d(1, 1, 1),
210                LINE_SIZE as usize,
211                input.into_tensor_arg(),
212                output.into_tensor_arg(),
213            );
214        };
215
216        println!(
217            "Executed reduction with runtime {:?} => {:?}",
218            WgpuRuntime::name(&client),
219            output.read(&client)
220        );
221    }
222
223    #[test]
224    fn test_benchmark() {
225        let client = WgpuRuntime::client(&WgpuDevice::default());
226        let bench1 = ReductionBench::<WgpuRuntime, f32> {
227            input_shape: vec![64, 256, 1024],
228            client: client.clone(),
229            _f: PhantomData,
230        };
231
232        let bench2 = ReductionBench::<WgpuRuntime, f32> {
233            input_shape: vec![64, 64, 4096],
234            client: client.clone(),
235            _f: PhantomData,
236        };
237
238        for bench in [bench1, bench2] {
239            println!("{}", bench.name());
240            println!("{}", bench.run(TimingMethod::System).unwrap());
241        }
242    }
243}