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
use std::sync::Arc;

use crate::{
    compute::StaticKernel,
    element::WgpuElement,
    kernel::{elemwise_workgroup, KernelSettings, StaticKernelSource, WORKGROUP_DEFAULT},
    kernel_wgsl,
    tensor::WgpuTensor,
};

kernel_wgsl!(ComparisonElemRaw, "../../template/comparison/elem.wgsl");
kernel_wgsl!(
    ComparisonElemInplaceRaw,
    "../../template/comparison/elem_inplace.wgsl"
);

/// Creates a comparison elementwise kernel.
#[macro_export]
macro_rules! comparison_elem {
    (
        $struct:ident,
        $ops:expr
    ) => {
        pub struct $struct;

        impl $crate::kernel::StaticKernelSource for $struct {
            fn source() -> $crate::kernel::SourceTemplate {
                $crate::kernel::ComparisonElemRaw::source()
                    .register("body", format!("output[id] = u32(lhs[id] {} rhs);", $ops))
            }
        }
    };
}

/// Creates a comparison elementwise inplace kernel.
#[macro_export]
macro_rules! comparison_elem_inplace {
    (
        $struct:ident,
        $ops:expr
    ) => {
        pub struct $struct;

        impl $crate::kernel::StaticKernelSource for $struct {
            fn source() -> $crate::kernel::SourceTemplate {
                $crate::kernel::ComparisonElemInplaceRaw::source()
                    .register("body", "lhs[id] = compare(lhs[id], rhs);")
                    .add_template(format!(
                        "{}return {{{{ elem }}}}(lhs {} rhs);{}",
                        "fn compare(lhs: {{ elem }}, rhs: {{ elem }}) -> {{ elem }} {\n",
                        $ops,
                        "\n}\n"
                    ))
            }
        }
    };
}

pub fn comparison_elem<K: StaticKernelSource, E: WgpuElement, const D: usize>(
    lhs: WgpuTensor<E, D>,
    rhs: E,
) -> WgpuTensor<u32, D> {
    let num_elems = lhs.shape.num_elements();

    let handle = lhs.client.empty(num_elems * core::mem::size_of::<u32>());
    let rhs_handle = lhs.client.create(E::as_bytes(&[rhs]));
    let kernel =
        StaticKernel::<KernelSettings<K, E, i32, WORKGROUP_DEFAULT, WORKGROUP_DEFAULT, 1>>::new(
            elemwise_workgroup(num_elems, WORKGROUP_DEFAULT),
        );

    lhs.client
        .execute(Arc::new(kernel), &[&lhs.handle, &rhs_handle, &handle]);

    WgpuTensor::new(lhs.client, lhs.device, lhs.shape, handle)
}

pub fn comparison_elem_inplace<K: StaticKernelSource, E: WgpuElement, const D: usize>(
    lhs: WgpuTensor<E, D>,
    rhs: E,
) -> WgpuTensor<u32, D> {
    let kernel =
        StaticKernel::<KernelSettings<K, E, i32, WORKGROUP_DEFAULT, WORKGROUP_DEFAULT, 1>>::new(
            elemwise_workgroup(lhs.shape.num_elements(), WORKGROUP_DEFAULT),
        );
    let rhs_handle = lhs.client.create(E::as_bytes(&[rhs]));
    lhs.client
        .execute(Arc::new(kernel), &[&lhs.handle, &rhs_handle]);

    WgpuTensor::new(lhs.client, lhs.device, lhs.shape, lhs.handle)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tests::{ReferenceBackend, TestBackend};
    use burn_tensor::{backend::Backend, Bool, Distribution, Tensor};

    comparison_elem!(LowerEqual, "<=");
    comparison_elem_inplace!(LowerEqualInplace, "<=");

    #[test]
    fn comparison_elem_should_work_with_multiple_invocations() {
        let (lhs, lhs_ref, rhs) = inputs();

        let value =
            Tensor::<TestBackend, 3, Bool>::from_primitive(comparison_elem::<LowerEqual, f32, 3>(
                lhs.into_primitive(),
                rhs,
            ));

        let value_ref = lhs_ref.lower_equal_elem(rhs);
        value
            .into_data()
            .assert_approx_eq(&value_ref.into_data(), 3);
    }

    #[test]
    fn comparison_elem_inplace_should_work_with_multiple_invocations() {
        let (lhs, lhs_ref, rhs) = inputs();

        let value =
            Tensor::<TestBackend, 3, Bool>::from_primitive(comparison_elem_inplace::<
                LowerEqualInplace,
                f32,
                3,
            >(lhs.into_primitive(), rhs));

        let value_ref = lhs_ref.lower_equal_elem(rhs);
        value
            .into_data()
            .assert_approx_eq(&value_ref.into_data(), 3);
    }

    #[allow(clippy::type_complexity)]
    fn inputs() -> (Tensor<TestBackend, 3>, Tensor<ReferenceBackend, 3>, f32) {
        TestBackend::seed(0);
        let lhs = Tensor::<TestBackend, 3>::random([2, 6, 256], Distribution::Uniform(0.0, 1.0));
        let lhs_ref = Tensor::<ReferenceBackend, 3>::from_data(lhs.to_data());

        (lhs, lhs_ref, 5.0)
    }
}