gausplat_renderer/render/gaussian_3d/jit/kernel/scan/add/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Scanning and adding the values exclusively.

pub use super::*;

use burn::tensor::ops::IntTensorOps;

/// Inputs.
#[derive(Clone, Debug)]
pub struct Inputs<R: JitRuntime> {
    /// The values to scan.
    pub values: JitTensor<R>,
}

/// Outputs.
#[derive(Clone, Debug)]
pub struct Outputs<R: JitRuntime> {
    /// The exclusively scanned values.
    pub values: JitTensor<R>,
    /// The total of scanned values.
    pub total: JitTensor<R>,
}

/// `N / N'`
pub const GROUP_SIZE: u32 = 256;

/// Scanning and adding the values exclusively.
pub fn main<R: JitRuntime, F: FloatElement, I: IntElement, B: BoolElement>(
    inputs: Inputs<R>
) -> Outputs<R> {
    impl_kernel_source!(Kernel1, "kernel.1.wgsl");
    impl_kernel_source!(Kernel2, "kernel.2.wgsl");

    // Specifying the parameters

    // [N]
    let values = inputs.values;
    let client = &values.client;
    let device = &values.device;
    // N
    let count = values.shape.dims[0] as u32;
    // N'
    let count_next = count.div_ceil(GROUP_SIZE);

    // [N']
    let values_next =
        JitBackend::<R, F, I, B>::int_empty([count_next as usize].into(), device);

    // Launching the kernel 1

    client.execute(
        Box::new(SourceKernel::new(
            Kernel1,
            CubeDim {
                x: GROUP_SIZE,
                y: 1,
                z: 1,
            },
        )),
        CubeCount::Static(count_next, 1, 1),
        vec![
            values.handle.to_owned().binding(),
            values_next.handle.to_owned().binding(),
        ],
    );

    let total = if count_next > 1 {
        // Recursing if there is more than one remaining group

        let Outputs {
            total,
            values: values_next,
        } = main::<R, F, I, B>(Inputs {
            values: values_next,
        });

        // Launching the kernel 2

        client.execute(
            Box::new(SourceKernel::new(
                Kernel2,
                CubeDim {
                    x: GROUP_SIZE,
                    y: 1,
                    z: 1,
                },
            )),
            CubeCount::Static(count_next, 1, 1),
            vec![
                values_next.handle.binding(),
                values.handle.to_owned().binding(),
            ],
        );

        total
    } else {
        // Returning the next values if there is only one group
        assert_eq!(count_next, 1);
        values_next
    };

    Outputs { total, values }
}

#[cfg(test)]
mod tests {
    #[test]
    fn scan_add_small() {
        use super::*;
        use crate::backend::{Wgpu, WgpuDevice, WgpuRuntime};
        use burn::tensor::TensorData;
        use bytemuck::{cast_slice, from_bytes};

        type B = Wgpu;
        type R = WgpuRuntime;
        type F = f32;
        type I = i32;
        let device = &WgpuDevice::default();

        let count = 9;
        let values_source = vec![0, 3, 0, 2, 4, 1, 3, 2, 9];

        let values_target = vec![0, 0, 3, 3, 5, 9, 10, 13, 15];
        let total_target = 24;

        let values = B::int_from_data(TensorData::new(values_source, [count]), device);
        let Outputs { total, values } = main::<R, F, I, u32>(Inputs { values });
        let total_output = *from_bytes::<u32>(
            &total.client.read(vec![total.handle.to_owned().binding()])[0],
        );
        let values_output = total.client.read(vec![values.handle.to_owned().binding()]);
        let values_output = cast_slice::<u8, u32>(&values_output[0]);

        assert_eq!(total_output, total_target);
        values_output
            .iter()
            .zip(&values_target)
            .enumerate()
            .for_each(|(index, (output, target))| {
                assert_eq!(output, target, "index: {index}");
            });
    }

    #[test]
    fn scan_add_random() {
        use super::*;
        use crate::backend::{Wgpu, WgpuDevice, WgpuRuntime};
        use burn::tensor::TensorData;
        use bytemuck::{cast_slice, from_bytes};
        use rand::{rngs::StdRng, Rng, SeedableRng};

        type B = Wgpu;
        type R = WgpuRuntime;
        type F = f32;
        type I = i32;
        let device = &WgpuDevice::default();

        let count = (1 << 22) - 1;
        let values_source = StdRng::from_entropy()
            .sample_iter(rand_distr::Uniform::new(0, 1 << 8))
            .take(count)
            .collect::<Vec<_>>();

        let values_target = values_source
            .iter()
            .scan(0, |state, &sum| {
                let output = *state;
                *state += sum;
                Some(output)
            })
            .collect::<Vec<_>>();
        let total_target = values_target.last().unwrap() + values_source.last().unwrap();

        let values = B::int_from_data(TensorData::new(values_source, [count]), device);
        let Outputs { total, values } = main::<R, F, I, u32>(Inputs { values });
        let total_output = *from_bytes::<u32>(
            &total.client.read(vec![total.handle.to_owned().binding()])[0],
        );
        let values_output = total.client.read(vec![values.handle.to_owned().binding()]);
        let values_output = cast_slice::<u8, u32>(&values_output[0]);

        assert_eq!(total_output, total_target);
        values_output
            .iter()
            .zip(&values_target)
            .enumerate()
            .for_each(|(index, (output, target))| {
                assert_eq!(output, target, "index: {index}");
            });
    }
}