aboutsummaryrefslogtreecommitdiff
path: root/src/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.cpp
blob: 62b2531daff800e70b1c73e829d04b5a9fd306b0 (plain)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * Copyright (c) 2019-2020 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * 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 "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h"

#include "arm_compute/core/AccessWindowStatic.h"
#include "arm_compute/core/CPP/Validate.h"
#include "arm_compute/core/NEON/wrapper/traits.h"
#include "arm_compute/core/NEON/wrapper/wrapper.h"
#include "arm_compute/core/utils/misc/ShapeCalculator.h"
#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
#include "src/core/NEON/kernels/convolution/depthwise/impl_qa8_qa8.hpp"
#include "support/ToolchainSupport.h"

namespace arm_compute
{
namespace
{
void pad_vectors(std::vector<int> &mult, std::vector<int> &shift, int vec_size)
{
    ARM_COMPUTE_ERROR_ON(mult.size() != shift.size());
    while(mult.size() % vec_size != 0)
    {
        mult.push_back(0);
        shift.push_back(0);
    }
}

template <typename T, int S>
void depthwise_loop_multiplier1_fp(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
                                   const Size2D &dilation, const Window &window, bool has_biases)
{
    using VectorType = typename wrapper::traits::neon_vector<T, S>::type;
    using TagType    = typename wrapper::traits::neon_vector<T, S>::tag_type;

    const size_t input_stride_y   = input->info()->strides_in_bytes().y();
    const size_t input_stride_z   = input->info()->strides_in_bytes().z();
    const size_t input_max_offset = input->info()->strides_in_bytes().z() * input->info()->dimension(2) - (input->info()->padding().bottom + input->info()->padding().top) *
                                    input->info()->strides_in_bytes().y();
    const size_t weights_width    = weights->info()->dimension(1);
    const size_t weights_height   = weights->info()->dimension(2);
    const size_t weights_stride_y = weights->info()->strides_in_bytes().y();
    const size_t weights_stride_z = weights->info()->strides_in_bytes().z();
    const size_t conv_stride_x    = conv_info.stride().first;
    const size_t conv_stride_y    = conv_info.stride().second;
    const size_t conv_pad_left    = conv_info.pad_left();
    const size_t conv_pad_top     = conv_info.pad_top();

    Window win_input = window;
    win_input.set(Window::DimY, Window::Dimension(0, 0, 0));
    win_input.set(Window::DimZ, Window::Dimension(0, 0, 0));

    Window win_weights = win_input;
    win_weights.set(3, Window::Dimension(0, 0, 0));

    Iterator input_it(input, win_input);
    Iterator weights_it(weights, win_weights);
    Iterator output_it(output, window);
    Iterator biases_it{};

    if(has_biases)
    {
        biases_it = Iterator(biases, win_weights);
    }

    execute_window_loop(window, [&](const Coordinates & id)
    {
        VectorType acc = wrapper::vdup_n(static_cast<T>(0), TagType{});

        const int input_y      = id.y() * conv_stride_x - conv_pad_left;
        const int input_z      = id.z() * conv_stride_y - conv_pad_top;
        int       input_offset = input_y * input_stride_y + input_z * input_stride_z;

        auto weights_ptr = weights_it.ptr();
        for(size_t h = 0; h < weights_height; ++h)
        {
            int offs = input_offset;
            for(size_t w = 0; w < weights_width; ++w)
            {
                const auto input_vals   = wrapper::vload(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), input_max_offset)));
                const auto weights_vals = wrapper::vload(reinterpret_cast<T *>(weights_ptr + w * weights_stride_y));

                acc = wrapper::vmla(acc, weights_vals, input_vals);
                offs += dilation.x() * input_stride_y;
            }

            weights_ptr += weights_stride_z;
            input_offset += dilation.y() * input_stride_z;
        }

        if(has_biases)
        {
            const auto biases_vals = wrapper::vload(reinterpret_cast<T *>(biases_it.ptr()));
            acc                    = wrapper::vadd(acc, biases_vals);
        }

        wrapper::vstore(reinterpret_cast<T *>(output_it.ptr()), acc);
    },
    input_it, weights_it, biases_it, output_it);
}

template <typename T>
void depthwise_loop_generic_fp(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
                               const Size2D &dilation, unsigned int depth_multiplier, const Window &window, bool has_biases)
{
    const size_t input_stride_y   = input->info()->strides_in_bytes().y();
    const size_t input_stride_z   = input->info()->strides_in_bytes().z();
    const size_t input_max_offset = input->info()->strides_in_bytes().z() * input->info()->dimension(2) - (input->info()->padding().bottom + input->info()->padding().top) *
                                    input->info()->strides_in_bytes().y();
    const size_t weights_width    = weights->info()->dimension(1);
    const size_t weights_height   = weights->info()->dimension(2);
    const size_t weights_stride_y = weights->info()->strides_in_bytes().y();
    const size_t weights_stride_z = weights->info()->strides_in_bytes().z();
    const size_t conv_stride_x    = conv_info.stride().first;
    const size_t conv_stride_y    = conv_info.stride().second;
    const size_t conv_pad_left    = conv_info.pad_left();
    const size_t conv_pad_top     = conv_info.pad_top();

    Window win_input = window;
    win_input.set(Window::DimY, Window::Dimension(0, 0, 0));
    win_input.set(Window::DimZ, Window::Dimension(0, 0, 0));

    Window win_weights = win_input;
    win_weights.set(3, Window::Dimension(0, 0, 0));

    win_input.set_dimension_step(Window::DimX, 1);

    Iterator input_it(input, win_input);
    Iterator weights_it(weights, win_weights);
    Iterator output_it(output, window);
    Iterator biases_it{};

    if(has_biases)
    {
        biases_it = Iterator(biases, win_weights);
    }

    execute_window_loop(window, [&](const Coordinates & id)
    {
        std::vector<T> acc(depth_multiplier, static_cast<T>(0));

        const int input_y      = id.y() * conv_stride_x - conv_pad_left;
        const int input_z      = id.z() * conv_stride_y - conv_pad_top;
        int       input_offset = input_y * input_stride_y + input_z * input_stride_z;

        auto weights_ptr = weights_it.ptr();
        for(size_t h = 0; h < weights_height; ++h)
        {
            int offs = input_offset;
            for(size_t w = 0; w < weights_width; ++w)
            {
                const auto input_val = *(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), input_max_offset)));

                for(size_t m = 0; m < depth_multiplier; ++m)
                {
                    const auto weights_val = *(reinterpret_cast<T *>(weights_ptr + m * sizeof(T) + w * weights_stride_y));
                    acc.at(m)              = support::cpp11::fma(weights_val, input_val, acc.at(m));
                }

                offs += dilation.x() * input_stride_y;
            }

            weights_ptr += weights_stride_z;
            input_offset += dilation.y() * input_stride_z;
        }

        if(has_biases)
        {
            for(size_t m = 0; m < depth_multiplier; ++m)
            {
                const auto biases_val                                     = *(reinterpret_cast<T *>(biases_it.ptr() + m * sizeof(T)));
                *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m) + biases_val;
            }
        }
        else
        {
            for(size_t m = 0; m < depth_multiplier; ++m)
            {
                *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m);
            }
        }
    },
    input_it, weights_it, biases_it, output_it);
}

template <typename T, typename TW, int S>
void depthwise_loop_multiplier1_quantized(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
                                          const Size2D &dilation, std::vector<int> output_multiplier, std::vector<int> output_shift, const Window &window, bool has_biases)
{
    using VectorType = typename wrapper::traits::neon_vector<T, S>::type;
    using TagType    = typename wrapper::traits::neon_vector<T, S>::tag_type;

    const size_t input_stride_y   = input->info()->strides_in_bytes().y();
    const size_t input_stride_z   = input->info()->strides_in_bytes().z();
    const size_t input_max_offset = input->info()->strides_in_bytes().z() * input->info()->dimension(2) - (input->info()->padding().bottom + input->info()->padding().top) *
                                    input->info()->strides_in_bytes().y();
    const size_t weights_width    = weights->info()->dimension(1);
    const size_t weights_height   = weights->info()->dimension(2);
    const size_t weights_stride_y = weights->info()->strides_in_bytes().y();
    const size_t weights_stride_z = weights->info()->strides_in_bytes().z();
    const size_t conv_stride_x    = conv_info.stride().first;
    const size_t conv_stride_y    = conv_info.stride().second;
    const size_t conv_pad_left    = conv_info.pad_left();
    const size_t conv_pad_top     = conv_info.pad_top();

    const int32_t input_qoffset   = input->info()->quantization_info().uniform().offset;
    const int32_t weights_qoffset = weights->info()->quantization_info().uniform().offset;
    const int32_t output_qoffset  = output->info()->quantization_info().uniform().offset;
    const int32_t k_offset        = weights_width * weights_height * input_qoffset * weights_qoffset;

    Window win_input = window;
    win_input.set(Window::DimY, Window::Dimension(0, 0, 0));
    win_input.set(Window::DimZ, Window::Dimension(0, 0, 0));

    Window win_weights = win_input;
    win_weights.set(3, Window::Dimension(0, 0, 0));

    Iterator input_it(input, win_input);
    Iterator weights_it(weights, win_weights);
    Iterator output_it(output, window);
    Iterator biases_it{};

    if(has_biases)
    {
        biases_it = Iterator(biases, win_weights);
    }

    execute_window_loop(window, [&](const Coordinates & id)
    {
        std::vector<int32_t> acc(S, 0);
        std::vector<int32_t> in_sum(S, 0);
        std::vector<int32_t> we_sum(S, 0);

        const int input_y      = id.y() * conv_stride_x - conv_pad_left;
        const int input_z      = id.z() * conv_stride_y - conv_pad_top;
        int       input_offset = input_y * input_stride_y + input_z * input_stride_z;

        auto weights_ptr = weights_it.ptr();
        for(size_t h = 0; h < weights_height; ++h)
        {
            int offs = input_offset;
            for(size_t w = 0; w < weights_width; ++w)
            {
                const auto input_vals   = wrapper::vload(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), input_max_offset)));
                const auto weights_vals = wrapper::vload(reinterpret_cast<TW *>(weights_ptr + w * weights_stride_y));

                for(int i = 0; i < S; ++i)
                {
                    acc.at(i) += input_vals[i] * weights_vals[i];
                    in_sum.at(i) += input_vals[i];
                    we_sum.at(i) += weights_vals[i];
                }

                offs += dilation.x() * input_stride_y;
            }

            weights_ptr += weights_stride_z;
            input_offset += dilation.y() * input_stride_z;
        }

        VectorType out_vals = wrapper::vdup_n(static_cast<T>(0), TagType{});
        for(int i = 0; i < S; ++i)
        {
            acc.at(i) -= in_sum.at(i) * weights_qoffset;
            acc.at(i) -= we_sum.at(i) * input_qoffset;
            acc.at(i) += k_offset;

            if(has_biases)
            {
                acc.at(i) += *reinterpret_cast<int32_t *>(biases_it.ptr() + i * sizeof(int32_t));
            }

            const int out_mul   = output_multiplier.at(id.x() + i);
            const int out_shift = output_shift.at(id.x() + i);
            if(out_shift < 0)
            {
                acc.at(i) = saturating_doubling_high_mul(acc.at(i) * (1 << (-out_shift)), out_mul) + output_qoffset;
            }
            else
            {
                acc.at(i) = rounding_divide_by_exp2(saturating_doubling_high_mul(acc.at(i), out_mul), out_shift) + output_qoffset;
            }
            out_vals[i] = static_cast<T>(utility::clamp<int32_t, T>(acc.at(i)));
        }

        wrapper::vstore(reinterpret_cast<T *>(output_it.ptr()), out_vals);
    },
    input_it, weights_it, biases_it, output_it);
}

template <typename T, typename TW>
void depthwise_loop_generic_quantized(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
                                      const Size2D &dilation, unsigned int depth_multiplier, std::vector<int> output_multiplier, std::vector<int> output_shift, const Window &window, bool has_biases)
{
    const size_t input_stride_y   = input->info()->strides_in_bytes().y();
    const size_t input_stride_z   = input->info()->strides_in_bytes().z();
    const size_t input_max_offset = input->info()->strides_in_bytes().z() * input->info()->dimension(2) - (input->info()->padding().bottom + input->info()->padding().top) *
                                    input->info()->strides_in_bytes().y();
    const size_t weights_width    = weights->info()->dimension(1);
    const size_t weights_height   = weights->info()->dimension(2);
    const size_t weights_stride_y = weights->info()->strides_in_bytes().y();
    const size_t weights_stride_z = weights->info()->strides_in_bytes().z();
    const size_t conv_stride_x    = conv_info.stride().first;
    const size_t conv_stride_y    = conv_info.stride().second;
    const size_t conv_pad_left    = conv_info.pad_left();
    const size_t conv_pad_top     = conv_info.pad_top();

    const int32_t input_qoffset   = input->info()->quantization_info().uniform().offset;
    const int32_t weights_qoffset = weights->info()->quantization_info().uniform().offset;
    const int32_t output_qoffset  = output->info()->quantization_info().uniform().offset;
    const int32_t k_offset        = weights_width * weights_height * input_qoffset * weights_qoffset;

    Window win_input = window;
    win_input.set(Window::DimY, Window::Dimension(0, 0, 0));
    win_input.set(Window::DimZ, Window::Dimension(0, 0, 0));

    Window win_weights = win_input;
    win_weights.set(3, Window::Dimension(0, 0, 0));

    win_input.set_dimension_step(Window::DimX, 1);

    Iterator input_it(input, win_input);
    Iterator weights_it(weights, win_weights);
    Iterator output_it(output, window);
    Iterator biases_it{};

    if(has_biases)
    {
        biases_it = Iterator(biases, win_weights);
    }

    execute_window_loop(window, [&](const Coordinates & id)
    {
        std::vector<int32_t> acc(depth_multiplier, 0);
        std::vector<int32_t> we_sum(depth_multiplier, 0);
        int32_t              in_sum = 0;

        const int input_y      = id.y() * conv_stride_x - conv_pad_left;
        const int input_z      = id.z() * conv_stride_y - conv_pad_top;
        int       input_offset = input_y * input_stride_y + input_z * input_stride_z;

        auto weights_ptr = weights_it.ptr();
        for(size_t h = 0; h < weights_height; ++h)
        {
            int offs = input_offset;
            for(size_t w = 0; w < weights_width; ++w)
            {
                const auto input_val = *(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), input_max_offset)));

                for(size_t m = 0; m < depth_multiplier; ++m)
                {
                    const auto weights_val = *(reinterpret_cast<TW *>(weights_ptr + m * sizeof(T) + w * weights_stride_y));
                    acc.at(m) += input_val * weights_val;

                    we_sum.at(m) += weights_val;
                }

                offs += dilation.x() * input_stride_y;
                in_sum += input_val;
            }

            weights_ptr += weights_stride_z;
            input_offset += dilation.y() * input_stride_z;
        }

        for(size_t m = 0; m < depth_multiplier; ++m)
        {
            acc.at(m) -= in_sum * weights_qoffset;
            acc.at(m) -= we_sum.at(m) * input_qoffset;
            acc.at(m) += k_offset;

            if(has_biases)
            {
                acc.at(m) += *(reinterpret_cast<int32_t *>(biases_it.ptr() + m * sizeof(int32_t)));
            }

            const int out_mul   = output_multiplier.at(id.x() + m);
            const int out_shift = output_shift.at(id.x() + m);
            if(out_shift < 0)
            {
                acc.at(m) = saturating_doubling_high_mul(acc.at(m) * (1 << (-out_shift)), out_mul) + output_qoffset;
            }
            else
            {
                acc.at(m) = rounding_divide_by_exp2(saturating_doubling_high_mul(acc.at(m), out_mul), out_shift) + output_qoffset;
            }
            *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = static_cast<T>(utility::clamp<int32_t, T>(acc.at(m)));
        }
    },
    input_it, weights_it, biases_it, output_it);
}

Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
                          const Size2D &dilation)
{
    ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
    ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
    ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
    ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
    ARM_COMPUTE_RETURN_ERROR_ON(depth_multiplier == 0);
    ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(1) + (weights->dimension(1) - 1) * (dilation.x() - 1) > input->dimension(1) + conv_info.pad_left() + conv_info.pad_right());
    ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) + (weights->dimension(2) - 1) * (dilation.y() - 1) > input->dimension(2) + conv_info.pad_top() + conv_info.pad_bottom());
    ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(0) * depth_multiplier) != weights->dimension(0));
    ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
    ARM_COMPUTE_RETURN_ERROR_ON((conv_info.stride().first < 1) || (conv_info.stride().second < 1));

    if(is_data_type_quantized_per_channel(weights->data_type()))
    {
        ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL);
        ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(0) != weights->quantization_info().scale().size());
    }
    else
    {
        ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
    }

    if(biases != nullptr)
    {
        ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
        ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(0));

        if(is_data_type_quantized_asymmetric(input->data_type()))
        {
            ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
        }
        else
        {
            ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
        }
    }

    if(output->total_size() != 0)
    {
        const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
        ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
        ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
    }

    return Status{};
}

std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases,
                                                        ITensorInfo *output, const PadStrideInfo &conv_info,
                                                        unsigned int depth_multiplier, const Size2D &dilation)
{
    // Get convolved dimensions
    const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);

    // Output auto inizialitation if not yet initialized
    auto_init_if_empty(*output, input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_quantization_info(output->quantization_info()));

    // Configure kernel window (generic)
    const unsigned int num_elems_read_per_iteration    = (depth_multiplier == 1) ? 8 / element_size_from_data_type(input->data_type()) : 1;
    const unsigned int num_elems_written_per_iteration = num_elems_read_per_iteration * depth_multiplier;

    // Configure kernel window
    Window win = calculate_max_window(*output, Steps(num_elems_written_per_iteration));

    AccessWindowStatic input_access(input, 0, -conv_info.pad_left(), ceil_to_multiple(num_elems_read_per_iteration, input->dimension(0)),
                                    input->dimension(1) + std::max(std::max(conv_info.pad_right(), conv_info.pad_bottom()), conv_info.pad_top()));
    AccessWindowHorizontal weights_access(weights, 0, num_elems_written_per_iteration);
    AccessWindowHorizontal output_access(output, 0, num_elems_written_per_iteration);

    bool window_changed = update_window_and_padding(win, input_access, weights_access, output_access);

    if(biases != nullptr)
    {
        AccessWindowHorizontal biases_access(biases, 0, num_elems_written_per_iteration);
        window_changed |= update_window_and_padding(win, biases_access);
    }

    output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));

    Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
    return std::make_pair(err, win);
}
} // namespace

NEDepthwiseConvolutionLayerNativeKernel::NEDepthwiseConvolutionLayerNativeKernel()
    : _func(), _border_size(0), _input(), _weights(), _biases(), _output(), _conv_info(), _depth_multiplier(1), _dilation(), _output_multiplier(), _output_shift(), _has_biases()
{
}

BorderSize NEDepthwiseConvolutionLayerNativeKernel::border_size() const
{
    return _border_size;
}

void NEDepthwiseConvolutionLayerNativeKernel::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
                                                        const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
{
    ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
    ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, depth_multiplier, dilation));

    _input            = input;
    _weights          = weights;
    _biases           = biases;
    _output           = output;
    _conv_info        = conv_info;
    _depth_multiplier = depth_multiplier;
    _border_size      = BorderSize(_conv_info.pad_left(), 0, std::max(std::max(conv_info.pad_right(), conv_info.pad_bottom()), conv_info.pad_top()), 0);
    _dilation         = dilation;
    _has_biases       = (biases != nullptr);

    if(is_data_type_quantized(_input->info()->data_type()))
    {
        const auto input_scale  = input->info()->quantization_info().uniform().scale;
        const auto output_scale = output->info()->quantization_info().uniform().scale;

        auto weights_scale = weights->info()->quantization_info().scale();
        if(!is_data_type_quantized_per_channel(_weights->info()->data_type()))
        {
            for(size_t i = 1; i < _weights->info()->dimension(0); ++i)
            {
                weights_scale.push_back(weights_scale.front());
            }
        }

        for(size_t i = 0; i < weights_scale.size(); ++i)
        {
            int32_t     out_mult   = 0;
            int32_t     out_shift  = 0;
            const float multiplier = input_scale * weights_scale.at(i) / output_scale;
            arm_compute::quantization::calculate_quantized_multiplier(multiplier, &out_mult, &out_shift);

            _output_multiplier.push_back(out_mult);
            _output_shift.push_back(out_shift);
        }
    }

    switch(_weights->info()->data_type())
    {
        case DataType::QASYMM8:
            _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<uint8_t, uint8_t, 8>;
            pad_vectors(_output_multiplier, _output_shift, 8);
            break;
        case DataType::QASYMM8_SIGNED:
            _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<int8_t, int8_t, 8>;
            pad_vectors(_output_multiplier, _output_shift, 8);
            break;
        case DataType::QSYMM8_PER_CHANNEL:
            if(_input->info()->data_type() == DataType::QASYMM8)
            {
                _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<uint8_t, int8_t, 8>;
            }
            else
            {
                _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<int8_t, int8_t, 8>;
            }
            pad_vectors(_output_multiplier, _output_shift, 8);
            break;
#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
        case DataType::F16:
            _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<float16_t, float16_t, 4>;
            pad_vectors(_output_multiplier, _output_shift, 4);
            break;
#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
        case DataType::F32:
            _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<float, float, 2>;
            pad_vectors(_output_multiplier, _output_shift, 2);
            break;
        default:
            ARM_COMPUTE_ERROR("Data type not supported");
            break;
    }

    auto win_config = validate_and_configure_window(_input->info(), _weights->info(), (biases != nullptr) ? biases->info() : nullptr, _output->info(), _conv_info, _depth_multiplier, dilation);
    ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
    INEKernel::configure(win_config.second);
}

Status NEDepthwiseConvolutionLayerNativeKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
                                                         unsigned int  depth_multiplier,
                                                         const Size2D &dilation)
{
    ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info, depth_multiplier, dilation));
    ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), weights->clone().get(), (biases != nullptr) ? biases->clone().get() : nullptr, output->clone().get(), conv_info,
                                                              depth_multiplier, dilation)
                                .first);
    return Status{};
}

void NEDepthwiseConvolutionLayerNativeKernel::run(const Window &window, const ThreadInfo &info)
{
    ARM_COMPUTE_UNUSED(info);
    ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
    ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);

    (this->*_func)(window, _has_biases);
}

template < typename T, typename TW, int S, typename std::enable_if < std::is_same<T, float>::value
#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
                                                                     || std::is_same<T, float16_t>::value
#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
                                                                     ,
                                                                     int >::type >
void NEDepthwiseConvolutionLayerNativeKernel::run_depthwise(const Window &window, bool has_biases)
{
    ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
    ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);

    if(_depth_multiplier == 1)
    {
        depthwise_loop_multiplier1_fp<T, S>(_input, _weights, _biases, _output, _conv_info, _dilation, window, has_biases);
    }
    else
    {
        depthwise_loop_generic_fp<T>(_input, _weights, _biases, _output, _conv_info, _dilation, _depth_multiplier, window, has_biases);
    }
}

template <typename T, typename TW, int S, typename>
void NEDepthwiseConvolutionLayerNativeKernel::run_depthwise(const Window &window, bool has_biases)
{
    ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
    ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);

    if(_depth_multiplier == 1)
    {
        depthwise_loop_multiplier1_quantized<T, TW, S>(_input, _weights, _biases, _output, _conv_info, _dilation, _output_multiplier, _output_shift, window, has_biases);
    }
    else
    {
        depthwise_loop_generic_quantized<T, TW>(_input, _weights, _biases, _output, _conv_info, _dilation, _depth_multiplier, _output_multiplier, _output_shift, window, has_biases);
    }
}
} // namespace arm_compute