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
use burn_tensor::Element;

/// The base element trait for the wgou backend.
pub trait WgpuElement:
    burn_tensor::Element + core::fmt::Debug + Send + Sync + 'static + Clone + bytemuck::Pod
where
    Self: Sized,
{
    fn type_name() -> &'static str;
    fn as_bytes(slice: &[Self]) -> &[u8];
    fn from_bytes(bytes: &[u8]) -> &[Self];
    #[cfg(any(feature = "fusion", test))]
    fn elem_type() -> crate::fusion::codegen::Elem;
}

/// The float element type for the wgpu backend.
pub trait FloatElement: WgpuElement + Element {}

/// The int element type for the wgpu backend.
pub trait IntElement: WgpuElement + Element {}

impl WgpuElement for u32 {
    fn type_name() -> &'static str {
        "u32"
    }
    fn as_bytes(slice: &[Self]) -> &[u8] {
        bytemuck::cast_slice(slice)
    }
    fn from_bytes(bytes: &[u8]) -> &[Self] {
        bytemuck::cast_slice(bytes)
    }
    #[cfg(any(feature = "fusion", test))]
    fn elem_type() -> crate::fusion::codegen::Elem {
        crate::fusion::codegen::Elem::U32
    }
}

impl WgpuElement for i32 {
    fn type_name() -> &'static str {
        "i32"
    }
    fn as_bytes(slice: &[Self]) -> &[u8] {
        bytemuck::cast_slice(slice)
    }
    fn from_bytes(bytes: &[u8]) -> &[Self] {
        bytemuck::cast_slice(bytes)
    }
    #[cfg(any(feature = "fusion", test))]
    fn elem_type() -> crate::fusion::codegen::Elem {
        crate::fusion::codegen::Elem::I32
    }
}

impl WgpuElement for f32 {
    fn type_name() -> &'static str {
        "f32"
    }
    fn as_bytes(slice: &[Self]) -> &[u8] {
        bytemuck::cast_slice(slice)
    }
    fn from_bytes(bytes: &[u8]) -> &[Self] {
        bytemuck::cast_slice(bytes)
    }

    #[cfg(any(feature = "fusion", test))]
    fn elem_type() -> crate::fusion::codegen::Elem {
        crate::fusion::codegen::Elem::F32
    }
}

impl FloatElement for f32 {}
impl IntElement for i32 {}