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
use super::{
    adaptive_avgpool::{adaptive_avg_pool2d, adaptive_avg_pool2d_backward},
    avgpool::{avg_pool2d, avg_pool2d_backward},
    conv::{conv2d, conv3d, conv_transpose2d, conv_transpose3d},
    interpolate::{bicubic_interpolate, bilinear_interpolate, nearest_interpolate},
    maxpool::{max_pool2d, max_pool2d_backward, max_pool2d_with_indices},
};
use crate::{element::FloatNdArrayElement, tensor::NdArrayTensor, NdArray};
use crate::{element::QuantElement, ops::interpolate::nearest_interpolate_backward};
use burn_tensor::ops::*;

impl<E: FloatNdArrayElement, Q: QuantElement> ModuleOps<Self> for NdArray<E, Q> {
    fn conv2d(
        x: NdArrayTensor<E, 4>,
        weight: NdArrayTensor<E, 4>,
        bias: Option<NdArrayTensor<E, 1>>,
        options: ConvOptions<2>,
    ) -> NdArrayTensor<E, 4> {
        conv2d::<E, Q>(x, weight, bias, options)
    }

    fn conv_transpose2d(
        x: NdArrayTensor<E, 4>,
        weight: NdArrayTensor<E, 4>,
        bias: Option<NdArrayTensor<E, 1>>,
        options: ConvTransposeOptions<2>,
    ) -> NdArrayTensor<E, 4> {
        conv_transpose2d(x, weight, bias, options)
    }

    fn avg_pool2d(
        x: NdArrayTensor<E, 4>,
        kernel_size: [usize; 2],
        stride: [usize; 2],
        padding: [usize; 2],
        count_include_pad: bool,
    ) -> NdArrayTensor<E, 4> {
        avg_pool2d(x, kernel_size, stride, padding, count_include_pad)
    }

    fn avg_pool2d_backward(
        x: NdArrayTensor<E, 4>,
        grad: NdArrayTensor<E, 4>,
        kernel_size: [usize; 2],
        stride: [usize; 2],
        padding: [usize; 2],
        count_include_pad: bool,
    ) -> NdArrayTensor<E, 4> {
        avg_pool2d_backward(x, grad, kernel_size, stride, padding, count_include_pad)
    }

    fn max_pool2d(
        x: NdArrayTensor<E, 4>,
        kernel_size: [usize; 2],
        stride: [usize; 2],
        padding: [usize; 2],
        dilation: [usize; 2],
    ) -> NdArrayTensor<E, 4> {
        max_pool2d::<E, Q>(x, kernel_size, stride, padding, dilation)
    }

    fn max_pool2d_with_indices(
        x: NdArrayTensor<E, 4>,
        kernel_size: [usize; 2],
        stride: [usize; 2],
        padding: [usize; 2],
        dilation: [usize; 2],
    ) -> MaxPool2dWithIndices<NdArray<E, Q>> {
        let (output, indices) =
            max_pool2d_with_indices::<E, Q>(x, kernel_size, stride, padding, dilation);

        MaxPool2dWithIndices::new(output, indices)
    }

    fn max_pool2d_with_indices_backward(
        x: NdArrayTensor<E, 4>,
        kernel_size: [usize; 2],
        stride: [usize; 2],
        padding: [usize; 2],
        dilation: [usize; 2],
        output_grad: NdArrayTensor<E, 4>,
        indices: NdArrayTensor<i64, 4>,
    ) -> MaxPool2dBackward<NdArray<E, Q>> {
        MaxPool2dBackward::new(max_pool2d_backward(
            x,
            kernel_size,
            stride,
            padding,
            dilation,
            output_grad,
            indices,
        ))
    }

    fn adaptive_avg_pool2d(x: NdArrayTensor<E, 4>, output_size: [usize; 2]) -> NdArrayTensor<E, 4> {
        adaptive_avg_pool2d(x, output_size)
    }

    fn adaptive_avg_pool2d_backward(
        x: NdArrayTensor<E, 4>,
        grad: NdArrayTensor<E, 4>,
    ) -> NdArrayTensor<E, 4> {
        adaptive_avg_pool2d_backward(x, grad)
    }

    fn interpolate(
        x: NdArrayTensor<E, 4>,
        output_size: [usize; 2],
        options: InterpolateOptions,
    ) -> NdArrayTensor<E, 4> {
        match options.mode {
            InterpolateMode::Nearest => nearest_interpolate(x, output_size),
            InterpolateMode::Bilinear => bilinear_interpolate(x, output_size),
            InterpolateMode::Bicubic => bicubic_interpolate(x, output_size),
        }
    }

    fn interpolate_backward(
        x: NdArrayTensor<E, 4>,
        grad: NdArrayTensor<E, 4>,
        output_size: [usize; 2],
        options: InterpolateOptions,
    ) -> NdArrayTensor<E, 4> {
        match options.mode {
            InterpolateMode::Nearest => nearest_interpolate_backward(x, grad, output_size),
            InterpolateMode::Bilinear => {
                panic!("bilinear interpolation backward is not supported for ndarray backend")
            }
            InterpolateMode::Bicubic => {
                panic!("bicubic interpolation backward is not supported for ndarray backend")
            }
        }
    }

    fn conv3d(
        x: NdArrayTensor<E, 5>,
        weight: NdArrayTensor<E, 5>,
        bias: Option<NdArrayTensor<E, 1>>,
        options: ConvOptions<3>,
    ) -> NdArrayTensor<E, 5> {
        conv3d::<E, Q>(x, weight, bias, options)
    }

    fn conv_transpose3d(
        x: NdArrayTensor<E, 5>,
        weight: NdArrayTensor<E, 5>,
        bias: Option<NdArrayTensor<E, 1>>,
        options: ConvTransposeOptions<3>,
    ) -> NdArrayTensor<E, 5> {
        conv_transpose3d(x, weight, bias, options)
    }
}