File: lrn_test.cpp

package info (click to toggle)
miopen 6.4.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 66,788 kB
  • sloc: cpp: 300,511; lisp: 29,731; ansic: 2,683; sh: 471; python: 323; makefile: 155
file content (341 lines) | stat: -rw-r--r-- 12,445 bytes parent folder | download
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*******************************************************************************
 *
 * MIT License
 *
 * Copyright (c) 2017 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *******************************************************************************/
#include "driver.hpp"
#include "test.hpp"
#include "verify.hpp"
#include "get_handle.hpp"
#include "tensor_holder.hpp"
#include <miopen/miopen.h>
#include <miopen/tensor.hpp>
#include <miopen/stringutils.hpp>
#include <miopen/lrn.hpp>
#include <random>
#include <algorithm>
#include <iterator>
#include <limits>
#include <iostream>

template <class T>
struct verify_lrn_foward
{
    miopen::LRNDescriptor lrn;
    tensor<T> input;

    verify_lrn_foward(const miopen::LRNDescriptor& plrnDesc, const tensor<T>& pinput)
    {
        lrn   = plrnDesc;
        input = pinput;
    }

    tensor<T> cpu() const
    {
        auto output = tensor<T>{input.desc.GetLengths()};
        int n_batch, channels, height, width;
        std::tie(n_batch, channels, height, width) = miopen::tien<4>(input.desc.GetLengths());

        auto alpha       = lrn.GetAlpha();
        auto beta        = lrn.GetBeta();
        auto K           = lrn.GetK();
        auto lrn_n       = lrn.GetN();
        int radius_lower = static_cast<int>((lrn_n - 1) / 2);
        int radius_upper = static_cast<int>(lrn_n / 2);
        auto mode        = lrn.GetMode();

        if(mode == miopenLRNCrossChannel)
        {
            auto alphaoverarea = alpha / lrn_n;
            par_ford(n_batch, channels, height, width)([&](int b, int c, int h, int w) {
                int start = c < radius_lower ? 0 : (c - radius_lower);
                int end   = (c + radius_upper + 1) > channels ? channels : (c + radius_upper + 1);

                double scale = 0;
                for(int k = start; k < end; k++)
                {
                    scale += std::pow(input(b, k, h, w), 2);
                }

                scale *= alphaoverarea;
                scale += K;
                scale = std::pow(scale, -beta);

                output(b, c, h, w) = static_cast<T>(scale * input(b, c, h, w));
            });
        }
        else
        {
            double alphaoverarea = radius_upper == 0 ? 1 : alpha / (lrn_n * lrn_n);
            par_ford(n_batch, channels, height, width)([&](int b, int c, int h, int w) {
                double scale = 0;
                int left     = (w - radius_lower) < 0 ? 0 : (w - radius_lower);
                int right    = (w + radius_upper + 1) > width ? width : (w + radius_upper + 1);
                int top      = (h - radius_lower) < 0 ? 0 : (h - radius_lower);
                int bottom   = (h + radius_upper + 1) > height ? height : (h + radius_upper + 1);

                for(int i = left; i < right; i++)
                {
                    for(int j = top; j < bottom; j++)
                    {
                        scale += std::pow(input(b, c, j, i), 2);
                    }
                }
                scale *= alphaoverarea;
                scale += K;
                scale              = std::pow(scale, -beta);
                output(b, c, h, w) = static_cast<T>(scale * input(b, c, h, w));
            });
        }

        return output;
    }

    tensor<T> gpu() const
    {
        auto&& handle = get_handle();
        auto out      = tensor<T>{input.desc.GetLengths()};
        auto in_dev   = handle.Write(input.data);
        auto out_dev  = handle.Write(out.data);
        auto alpha    = lrn.GetAlpha();
        auto beta     = lrn.GetBeta();
        auto bDoBwd   = false;

        lrn.Forward(handle,
                    &alpha,
                    input.desc,
                    in_dev.get(),
                    &beta,
                    out.desc,
                    out_dev.get(),
                    bDoBwd,
                    nullptr);

        out.data = handle.Read<T>(out_dev, out.data.size());
        return out;
    }

    void fail(int) const
    {
        std::cout << "verify_lrn_foward" << std::endl;
        std::cout << "Input Tensor"
                  << " " << input.desc.ToString() << std::endl;
    }
};

template <class T>
struct verify_lrn_bwd
{

    miopen::LRNDescriptor lrn;
    tensor<T> inputY;
    tensor<T> inputDY;
    tensor<T> inputX;
    tensor<T> scale;

    verify_lrn_bwd(const miopen::LRNDescriptor& plrn,
                   const tensor<T>& pout,
                   const tensor<T>& pdout,
                   const tensor<T>& pin,
                   const tensor<T>& pscale)
    {
        lrn     = plrn;
        inputY  = pout;
        inputDY = pdout;
        inputX  = pin;
        scale   = pscale;
    }

    tensor<T> cpu() const
    {
        auto routputDX = tensor<T>{inputX.desc.GetLengths()};
        int n_batch, channels, height, width;
        std::tie(n_batch, channels, height, width) = miopen::tien<4>(inputY.desc.GetLengths());

        auto alpha       = lrn.GetAlpha();
        auto beta        = lrn.GetBeta();
        auto lrn_n       = lrn.GetN();
        auto mode        = lrn.GetMode();
        int radius_lower = static_cast<int>((lrn_n - 1) / 2);
        int radius_upper = static_cast<int>(lrn_n / 2);

        if(mode == miopenLRNWithinChannel)
        {
            auto adjust_area       = lrn_n * lrn_n;
            auto cache_ratio_value = 2 * alpha * beta / adjust_area;

            par_ford(n_batch, channels, height, width)([&](int b, int c, int h, int w) {
                int left   = w < radius_upper ? 0 : (w - radius_upper);
                int right  = (w + radius_lower + 1) > width ? width : (w + radius_lower + 1);
                int top    = h < radius_upper ? 0 : (h - radius_upper);
                int bottom = (h + radius_lower + 1) > height ? height : (h + radius_lower + 1);

                double ydy = 0;
                for(int i = left; i < right; i++)
                {
                    for(int j = top; j < bottom; j++)
                    {
                        ydy += (double(inputY(b, c, j, i) * inputDY(b, c, j, i)) /
                                double(scale(b, c, j, i)));
                    }
                }

                routputDX(b, c, h, w) = static_cast<T>(
                    std::pow(static_cast<double>(scale(b, c, h, w)), -beta) * inputDY(b, c, h, w) -
                    cache_ratio_value * inputX(b, c, h, w) * ydy);
            });
        }
        else
        {
            auto cache_ratio_value = 2 * alpha * beta / lrn_n;

            par_ford(n_batch, channels, height, width)([&](int b, int c, int h, int w) {
                int start = c < radius_upper ? 0 : (c - radius_upper);
                int end   = (c + radius_lower + 1) > channels ? channels : (c + radius_lower + 1);

                double ydy = 0;
                for(auto k = start; k < end; k++)
                {
                    ydy += (double(inputY(b, k, h, w) * inputDY(b, k, h, w)) /
                            double(scale(b, k, h, w)));
                }

                routputDX(b, c, h, w) = static_cast<T>(
                    std::pow(static_cast<double>(scale(b, c, h, w)), -beta) * inputDY(b, c, h, w) -
                    cache_ratio_value * inputX(b, c, h, w) * ydy);
            });
        }

        return routputDX;
    }

    tensor<T> gpu() const
    {
        auto&& handle     = get_handle();
        auto routputDX    = tensor<T>{inputX.desc.GetLengths()};
        auto inputY_dev   = handle.Write(inputY.data);
        auto inputDY_dev  = handle.Write(inputDY.data);
        auto inputX_dev   = handle.Write(inputX.data);
        auto outputDX_dev = handle.Create<T>(routputDX.data.size());
        auto scale_dev    = handle.Write(scale.data);

        auto alpha = lrn.GetAlpha(), beta = lrn.GetBeta();
        lrn.Backward(handle,
                     &alpha,
                     inputY.desc, // Y
                     inputY_dev.get(),
                     inputDY.desc, // DY
                     inputDY_dev.get(),
                     inputX.desc, // X
                     inputX_dev.get(),
                     &beta,
                     routputDX.desc, // DX
                     outputDX_dev.get(),
                     scale_dev.get());

        routputDX.data = handle.Read<T>(outputDX_dev, routputDX.data.size());
        return routputDX;
    }

    void fail(int) const
    {
        std::cout << "verify_lrn_bwd" << std::endl;
        std::cout << "Input Tensor Y"
                  << " " << inputY.desc.ToString() << std::endl;
        std::cout << "Input Tensor DY"
                  << " " << inputDY.desc.ToString() << std::endl;
        std::cout << "Input Tensor X"
                  << " " << scale.desc.ToString() << std::endl;
    }
};

template <class T>
struct lrn_driver : test_driver
{
    tensor<T> input;

    unsigned int n = 1;
    double alpha   = 1;
    double beta    = 1;
    double k       = 1;
    std::string mode;

    std::unordered_map<std::string, miopenLRNMode_t> mode_lookup = {
        {"WITHIN_CHANNEL", miopenLRNWithinChannel}, {"ACROSS_CHANNEL", miopenLRNCrossChannel}};

    lrn_driver()
    {
        auto gen_value = [](auto... is) {
            return tensor_elem_gen_integer{miopen_type<T>{} == miopenHalf ? 5 : 17}() *
                   tensor_elem_gen_checkboard_sign{}(is...);
        };

        add(input, "input", get_input_tensor(gen_value));
        add(n, "N", generate_data({1, 4, 5}));
        add(alpha, "alpha", generate_data({double(1)}));
        add(beta, "beta", generate_data({double(1)}));
        add(k, "K", generate_data({double(1)}));
        add(mode, "mode", generate_data({"Within_Channel", "Across_Channel"}));
    }

    void run()
    {
        std::size_t n_batch, channels, height, width;
        std::tie(n_batch, channels, height, width) = miopen::tien<4>(input.desc.GetLengths());
        size_t total_mem  = 5 * input.desc.GetNumBytes(); // estimate based on backward pass
        size_t device_mem = get_handle().GetGlobalMemorySize();
        if(total_mem >= device_mem)
        {
            show_command();
            std::cout << "Config requires " << total_mem
                      << " Bytes to write all necessary tensors to GPU. GPU has " << device_mem
                      << " Bytes of memory." << std::endl;
            return;
        }

        miopen::LRNDescriptor lrn{mode_lookup.at(miopen::ToUpper(mode)), n, {alpha, beta, k}};

        auto out           = verify(verify_lrn_foward<T>{lrn, input});
        uint64_t max_value = miopen_type<T>{} == miopenHalf ? 5 : 17;

        auto scale = tensor<T>{n_batch, channels, height, width}.generate(
            tensor_elem_gen_integer{max_value});
        auto dout = tensor<T>{n_batch, channels, height, width}.generate(
            tensor_elem_gen_integer{max_value});
        par_ford(n_batch, channels, height, width)(
            [&](int b, int c, int h, int w) { scale(b, c, h, w) += 1; });

        verify(verify_lrn_bwd<T>{lrn, out.first, dout, input, scale});
    };
};

// To address compiler issue for bfloat1 type in SWDEV-202752
// creating explicit instance of lrn_driver with bfloat16 with noop
template <>
struct lrn_driver<bfloat16> : test_driver
{
    lrn_driver() {}
    void run() { std::cout << "bfloat16 is not supported in lrn" << std::endl; };
};

int main(int argc, const char* argv[]) { test_drive<lrn_driver>(argc, argv); };