".github/vscode:/vscode.git/clone" did not exist on "0919b08ec8761e1416259a146d5a02a3ab7b7fd1"
conv_bwd_data_driver.cpp 9.07 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <numeric>
#include <initializer_list>
#include <cstdlib>
#include <stdlib.h>
#include "config.hpp"
#include "tensor_descriptor.hpp"
#include "tensor_descriptor_helper.hpp"
#include "print_array.hpp"
#include "print_sequence.hpp"
#include "device.hpp"
Chao Liu's avatar
Chao Liu committed
12
#include "host_tensor_generator.hpp"
Chao Liu's avatar
Chao Liu committed
13
14
15
16
17
#include "device_tensor.hpp"
#include "conv_common.hpp"
#include "host_conv_bwd_data.hpp"
#include "device_convolution_backward_data_implicit_gemm_v1r1_nchw_kcyx_nkhw.hpp"
#include "device_convolution_backward_data_implicit_gemm_v1r2_nchw_kcyx_nkhw.hpp"
18
#include "device_convolution_backward_data_implicit_gemm_v4r1_nchw_kcyx_nkhw.hpp"
carlushuang's avatar
carlushuang committed
19
20
#include "device_convolution_backward_data_implicit_gemm_v4r2_nchw_kcyx_nkhw.hpp"
#include "device_convolution_backward_data_implicit_gemm_v4r3_nchw_kcyx_nkhw.hpp"
Chao Liu's avatar
Chao Liu committed
21
#include "device_convolution_backward_data_implicit_gemm_v5r1_nhwc_kyxc_nhwk.hpp"
Chao Liu's avatar
Chao Liu committed
22
23
24

int main(int argc, char* argv[])
{
25
    using namespace launcher;
Chao Liu's avatar
Chao Liu committed
26

Chao Liu's avatar
Chao Liu committed
27
#if 0
Chao Liu's avatar
Chao Liu committed
28
29
30
31
32
33
34
    constexpr index_t N  = 64;
    constexpr index_t C  = 256;
    constexpr index_t HI = 56;
    constexpr index_t WI = 56;
    constexpr index_t K  = 256;
    constexpr index_t Y  = 1;
    constexpr index_t X  = 1;
Chao Liu's avatar
Chao Liu committed
35

Chao Liu's avatar
Chao Liu committed
36
    using ConvStrides   = Sequence<1, 1>;
37
    using ConvDilations = Sequence<1, 1>;
Chao Liu's avatar
Chao Liu committed
38
39
40
41
42
43
44
45
46

    using LeftPads  = Sequence<0, 0>;
    using RightPads = Sequence<0, 0>;
#elif 0
    // 3x3, 34x34
    constexpr index_t N  = 64;
    constexpr index_t C  = 256;
    constexpr index_t HI = 34;
    constexpr index_t WI = 34;
47
    constexpr index_t K  = 256;
Chao Liu's avatar
Chao Liu committed
48
49
50
51
52
53
54
55
56
    constexpr index_t Y  = 3;
    constexpr index_t X  = 3;

    using ConvStrides   = Sequence<1, 1>;
    using ConvDilations = Sequence<1, 1>;

    using LeftPads  = Sequence<0, 0>;
    using RightPads = Sequence<0, 0>;
#elif 0
57
58
    // 3x3, 28x28
    constexpr index_t N  = 128;
Chao Liu's avatar
Chao Liu committed
59
    constexpr index_t C  = 256;
60
61
62
63
64
    constexpr index_t HI = 28;
    constexpr index_t WI = 28;
    constexpr index_t K  = 1024;
    constexpr index_t Y  = 3;
    constexpr index_t X  = 3;
Chao Liu's avatar
Chao Liu committed
65
66
67
68

    using ConvStrides   = Sequence<1, 1>;
    using ConvDilations = Sequence<1, 1>;

69
70
    using LeftPads  = Sequence<1, 1>;
    using RightPads = Sequence<1, 1>;
Chao Liu's avatar
Chao Liu committed
71
72
#elif 0
    // 1x1 filter, 8x8 image
73
74
    constexpr index_t N  = 256;
    constexpr index_t C  = 1024;
Chao Liu's avatar
Chao Liu committed
75
76
    constexpr index_t HI = 8;
    constexpr index_t WI = 8;
77
    constexpr index_t K  = 1024;
Chao Liu's avatar
Chao Liu committed
78
79
80
81
82
83
84
85
86
87
88
    constexpr index_t Y  = 1;
    constexpr index_t X  = 1;

    using ConvStrides   = Sequence<1, 1>;
    using ConvDilations = Sequence<1, 1>;

    using LeftPads  = Sequence<0, 0>;
    using RightPads = Sequence<0, 0>;
#elif 0
    // 1x1 filter, 7x7 image
    constexpr index_t N  = 128;
89
    constexpr index_t C  = 1024;
Chao Liu's avatar
Chao Liu committed
90
91
    constexpr index_t HI = 7;
    constexpr index_t WI = 7;
92
    constexpr index_t K  = 1024;
Chao Liu's avatar
Chao Liu committed
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
    constexpr index_t Y  = 1;
    constexpr index_t X  = 1;

    using ConvStrides   = Sequence<1, 1>;
    using ConvDilations = Sequence<1, 1>;

    using LeftPads  = Sequence<0, 0>;
    using RightPads = Sequence<0, 0>;
#elif 0
    // 1x1 filter, 14x14 image
    constexpr index_t N  = 128;
    constexpr index_t C  = 512;
    constexpr index_t HI = 14;
    constexpr index_t WI = 14;
    constexpr index_t K  = 128;
    constexpr index_t Y  = 1;
    constexpr index_t X  = 1;

    using ConvStrides   = Sequence<1, 1>;
    using ConvDilations = Sequence<1, 1>;

    using LeftPads  = Sequence<0, 0>;
    using RightPads = Sequence<0, 0>;
#elif 0
    // 1x1 filter, 28x28 image
    constexpr index_t N  = 128;
119
    constexpr index_t C  = 128;
Chao Liu's avatar
Chao Liu committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    constexpr index_t HI = 28;
    constexpr index_t WI = 28;
    constexpr index_t K  = 128;
    constexpr index_t Y  = 1;
    constexpr index_t X  = 1;

    using ConvStrides   = Sequence<1, 1>;
    using ConvDilations = Sequence<1, 1>;

    using LeftPads  = Sequence<0, 0>;
    using RightPads = Sequence<0, 0>;
#elif 0
    // 1x1 filter, 17x17 input
    constexpr index_t N  = 128;
134
    constexpr index_t C  = 1024;
Chao Liu's avatar
Chao Liu committed
135
136
    constexpr index_t HI = 17;
    constexpr index_t WI = 17;
137
    constexpr index_t K  = 1024;
Chao Liu's avatar
Chao Liu committed
138
139
140
141
142
143
144
145
146
    constexpr index_t Y  = 1;
    constexpr index_t X  = 1;

    using ConvStrides   = Sequence<1, 1>;
    using ConvDilations = Sequence<1, 1>;

    using LeftPads  = Sequence<0, 0>;
    using RightPads = Sequence<0, 0>;
#elif 0
147
    // 5x5 filter, 2x2 pad, 7x7 input
Chao Liu's avatar
Chao Liu committed
148
    constexpr index_t N  = 128;
149
150
151
152
153
154
    constexpr index_t C  = 1024;
    constexpr index_t HI = 7;
    constexpr index_t WI = 7;
    constexpr index_t K  = 1024;
    constexpr index_t Y  = 5;
    constexpr index_t X  = 5;
Chao Liu's avatar
Chao Liu committed
155
156
157
158

    using ConvStrides   = Sequence<1, 1>;
    using ConvDilations = Sequence<1, 1>;

159
160
    using LeftPads  = Sequence<2, 2>;
    using RightPads = Sequence<2, 2>;
Chao Liu's avatar
Chao Liu committed
161
#elif 0
162
    // 1x7 filter, 0x3 pad, 17x17 input
Chao Liu's avatar
Chao Liu committed
163
    constexpr index_t N  = 128;
Chao Liu's avatar
Chao Liu committed
164
    constexpr index_t C  = 256;
165
166
    constexpr index_t HI = 17;
    constexpr index_t WI = 17;
Chao Liu's avatar
Chao Liu committed
167
    constexpr index_t K  = 1024;
Chao Liu's avatar
Chao Liu committed
168
    constexpr index_t Y  = 1;
169
    constexpr index_t X  = 7;
Chao Liu's avatar
Chao Liu committed
170
171
172
173

    using ConvStrides   = Sequence<1, 1>;
    using ConvDilations = Sequence<1, 1>;

174
175
    using LeftPads  = Sequence<0, 3>;
    using RightPads = Sequence<0, 3>;
Chao Liu's avatar
Chao Liu committed
176
#elif 0
177
    // 7x1 filter, 3x0 pad, 17x17 input
Chao Liu's avatar
Chao Liu committed
178
    constexpr index_t N  = 128;
Chao Liu's avatar
Chao Liu committed
179
    constexpr index_t C  = 256;
180
181
182
183
    constexpr index_t HI = 17;
    constexpr index_t WI = 17;
    constexpr index_t K  = 1024;
    constexpr index_t Y  = 7;
Chao Liu's avatar
Chao Liu committed
184
185
186
187
188
    constexpr index_t X  = 1;

    using ConvStrides   = Sequence<1, 1>;
    using ConvDilations = Sequence<1, 1>;

189
190
    using LeftPads  = Sequence<3, 0>;
    using RightPads = Sequence<3, 0>;
Chao Liu's avatar
Chao Liu committed
191
#elif 1
Chao Liu's avatar
Chao Liu committed
192
193
    // 3x3 filter, 2x2 stride, 35x35 input, 17x17 output
    constexpr index_t N  = 128;
Chao Liu's avatar
Chao Liu committed
194
    constexpr index_t C  = 256;
Chao Liu's avatar
Chao Liu committed
195
196
    constexpr index_t HI = 35;
    constexpr index_t WI = 35;
Chao Liu's avatar
Chao Liu committed
197
    constexpr index_t K  = 1280;
Chao Liu's avatar
Chao Liu committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    constexpr index_t Y  = 3;
    constexpr index_t X  = 3;

    using ConvStrides   = Sequence<2, 2>;
    using ConvDilations = Sequence<1, 1>;

    using LeftPads  = Sequence<0, 0>;
    using RightPads = Sequence<0, 0>;
#endif

    constexpr auto in_nchw_desc  = make_native_tensor_descriptor_packed(Sequence<N, C, HI, WI>{});
    constexpr auto wei_kcyx_desc = make_native_tensor_descriptor_packed(Sequence<K, C, Y, X>{});
    constexpr auto out_nkhw_desc = get_convolution_output_default_4d_tensor_descriptor(
        in_nchw_desc, wei_kcyx_desc, ConvStrides{}, ConvDilations{}, LeftPads{}, RightPads{});

Chao Liu's avatar
Chao Liu committed
213
214
215
    ostream_tensor_descriptor(in_nchw_desc, std::cout << "in_nchw_desc: ");
    ostream_tensor_descriptor(wei_kcyx_desc, std::cout << "wei_kcyx_desc: ");
    ostream_tensor_descriptor(out_nkhw_desc, std::cout << "out_nkhw_desc: ");
Chao Liu's avatar
Chao Liu committed
216
217
218
219
220
221
    print_sequence("LeftPads", LeftPads{});
    print_sequence("LeftPads", LeftPads{});
    print_sequence("RightPads", RightPads{});
    print_sequence("ConvStrides", ConvStrides{});
    print_sequence("ConvDilations", ConvDilations{});

Chao Liu's avatar
Chao Liu committed
222
223
224
225
    Tensor<float> in_nchw_device(make_HostTensorDescriptor(in_nchw_desc));
    Tensor<float> in_nchw_host(make_HostTensorDescriptor(in_nchw_desc));
    Tensor<float> wei_kcyx(make_HostTensorDescriptor(wei_kcyx_desc));
    Tensor<float> out_nkhw(make_HostTensorDescriptor(out_nkhw_desc));
Chao Liu's avatar
Chao Liu committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

    std::size_t num_thread = std::thread::hardware_concurrency();

    if(argc != 3)
    {
        printf("arg1: do_verification, arg2: nrepeat\n");
        exit(1);
    }

    bool do_verification = atoi(argv[1]);
    std::size_t nrepeat  = atoi(argv[2]);

    if(do_verification)
    {
#if 0
        wei_kcyx.GenerateTensorValue(GeneratorTensor_1{1}, num_thread);
        out_nkhw.GenerateTensorValue(GeneratorTensor_1{1}, num_thread);
#else
        wei_kcyx.GenerateTensorValue(GeneratorTensor_2{-5, 5}, num_thread);
        out_nkhw.GenerateTensorValue(GeneratorTensor_2{-5, 5}, num_thread);
#endif
    }

Chao Liu's avatar
Chao Liu committed
249
#if 0
Chao Liu's avatar
Chao Liu committed
250
    device_convolution_backward_data_implicit_gemm_v1r1_nchw_kcyx_nkhw
251
#elif 0
Chao Liu's avatar
Chao Liu committed
252
    device_convolution_backward_data_implicit_gemm_v1r2_nchw_kcyx_nkhw
253
254
#elif 0
    device_convolution_backward_data_implicit_gemm_v4r1_nchw_kcyx_nkhw
carlushuang's avatar
carlushuang committed
255
256
257
258
#elif 0
    device_convolution_backward_data_implicit_gemm_v4r2_nchw_kcyx_nkhw
#elif 1
    device_convolution_backward_data_implicit_gemm_v4r3_nchw_kcyx_nkhw
Chao Liu's avatar
Chao Liu committed
259
260
#elif 1
    device_convolution_backward_data_implicit_gemm_v5r1_nhwc_kyxc_nhwk
Chao Liu's avatar
Chao Liu committed
261
#endif
Chao Liu's avatar
Chao Liu committed
262
263
264
265
266
267
268
269
270
271
272
    (in_nchw_desc,
     in_nchw_device,
     wei_kcyx_desc,
     wei_kcyx,
     out_nkhw_desc,
     out_nkhw,
     ConvStrides{},
     ConvDilations{},
     LeftPads{},
     RightPads{},
     nrepeat);
Chao Liu's avatar
Chao Liu committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

    if(do_verification)
    {
        host_direct_convolution_backward_data(in_nchw_host,
                                              wei_kcyx,
                                              out_nkhw,
                                              ConvStrides{},
                                              ConvDilations{},
                                              LeftPads{},
                                              RightPads{});

        check_error(in_nchw_host, in_nchw_device);

#if 0
        LogRange(std::cout << "out_nkhw : ", out_nkhw.mData, ",") << std::endl;
        LogRange(std::cout << "wei_kcyx : ", wei_kcyx.mData, ",") << std::endl;
        LogRange(std::cout << "in_nchw_host : ", in_nchw_host.mData, ",") << std::endl;
        LogRange(std::cout << "in_nchw_device : ", in_nchw_device.mData, ",") << std::endl;
#endif
    }
}