aboutsummaryrefslogtreecommitdiff
path: root/src/core/NEON/kernels/convolution/depthwise/impl_qa8_qa8.hpp
blob: be73065b00ea786beff12a71ff5234de90c88d8a (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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
/*
 * Copyright (c) 2019 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.
 */

/*
 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 *
 *          NOTE: Header to be included by implementation files only.
 *
 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 */

#include <limits>

#include "arm_compute/core/NEON/kernels/convolution/common/arm.hpp"
#include "arm_compute/core/NEON/kernels/convolution/depthwise/impl_base.hpp"
#include "arm_compute/core/NEON/kernels/convolution/depthwise/depthwise_quantized.hpp"

#pragma once

// Comment the following to use floating-point based quantisation, leave
// uncommented to use fixed-point.
#define FIXED_POINT_REQUANTISATION 1

using namespace neon_convolution_kernels;
using namespace qasymm8;

template <typename T>
struct clamp_to_limits
{
  template <typename U>
  static inline U clamp(const U& v)
  {
    const std::numeric_limits<T> limits;
    const U min = static_cast<U>(limits.min());
    const U max = static_cast<U>(limits.max());
    return std::min(std::max(v, min), max);
  }

  template <typename U>
  static inline T clamp_and_cast(const U& v)
  {
    return static_cast<U>(clamp(v));
  }
};

template <typename T>
inline T saturating_doubling_high_mul(const T&, const int32_t&);

template <>
inline int32x4_t saturating_doubling_high_mul(const int32x4_t& a, const int32_t& b)
{
  return vqrdmulhq_n_s32(a, b);
}

template <>
inline int32_t saturating_doubling_high_mul(const int32_t& a, const int32_t& b)
{
  return vget_lane_s32(vqrdmulh_n_s32(vdup_n_s32(a), b), 0);
}

template <typename T>
inline T rounding_divide_by_exp2(const T& x, const int exponent);

template <>
inline int32x4_t rounding_divide_by_exp2(const int32x4_t& x, const int exponent)
{
  const int32x4_t shift = vdupq_n_s32(-exponent);
  const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift), 31);
  const int32x4_t fixed = vqaddq_s32(x, fixup);
  return vrshlq_s32(fixed, shift);
}

template <>
inline int32x2_t rounding_divide_by_exp2(const int32x2_t& x, const int exponent)
{
  const int32x2_t shift = vdup_n_s32(-exponent);
  const int32x2_t fixup = vshr_n_s32(vand_s32(x, shift), 31);
  const int32x2_t fixed = vqadd_s32(x, fixup);
  return vrshl_s32(fixed, shift);
}

template <>
inline int32_t rounding_divide_by_exp2(const int32_t& x, const int exponent)
{
  const int32x2_t xs = vdup_n_s32(x);
  return vget_lane_s32(rounding_divide_by_exp2(xs, exponent), 0);
}

namespace depthwise
{
template <
  unsigned int OutputTileRows, unsigned int OutputTileCols,
  unsigned int KernelRows, unsigned int KernelCols,
  unsigned int StrideRows, unsigned int StrideCols
>
QAsymm8DepthwiseConvolution<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, StrideRows, StrideCols
>::QAsymm8DepthwiseConvolution(
  int n_batches, int n_input_rows, int n_input_cols, int n_channels,
  const ActivationFunction activation,
  const QAsymm8Params& weight_quantisation,
  const QAsymm8Params& input_quantisation,
  const QAsymm8Params& output_quantisation,
  unsigned int padding_top,
  unsigned int padding_left,
  unsigned int padding_bottom,
  unsigned int padding_right
) : QAsymm8DepthwiseConvolution(
    n_batches, n_input_rows, n_input_cols, n_channels,
    activation, weight_quantisation, input_quantisation, output_quantisation,
    QAsymm8RescaleParams::make_rescale_params(weight_quantisation, input_quantisation, output_quantisation),
    padding_top, padding_left, padding_bottom, padding_right
  )
{
}

template <
  unsigned int OutputTileRows, unsigned int OutputTileCols,
  unsigned int KernelRows, unsigned int KernelCols,
  unsigned int StrideRows, unsigned int StrideCols
>
QAsymm8DepthwiseConvolution<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, StrideRows, StrideCols
>::QAsymm8DepthwiseConvolution(
  int n_batches, int n_input_rows, int n_input_cols, int n_channels,
  const ActivationFunction activation,
  const QAsymm8Params& weight_quantisation,
  const QAsymm8Params& input_quantisation,
  const QAsymm8Params& output_quantisation,
  const QAsymm8RescaleParams& rescale_params,
  unsigned int padding_top,
  unsigned int padding_left,
  unsigned int padding_bottom,
  unsigned int padding_right
) : Base(
    n_batches, n_input_rows, n_input_cols, n_channels,
    get_activation_fn(activation, output_quantisation),
    padding_top, padding_left, padding_bottom, padding_right
  ),
  _weights_quant(weight_quantisation),
  _inputs_quant(input_quantisation),
  _output_quant(output_quantisation),
  rescale_parameters(rescale_params)
{
}

template <
  unsigned int OutputTileRows, unsigned int OutputTileCols,
  unsigned int KernelRows, unsigned int KernelCols,
  unsigned int StrideRows, unsigned int StrideCols
>
ActivationFunction QAsymm8DepthwiseConvolution<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, StrideRows, StrideCols
>::get_activation_fn(
  const ActivationFunction activation,
  const QAsymm8Params& output_quant
)
{
  if (
    (activation == ActivationFunction::ReLU &&
     output_quant.quantize(0) == 0) ||
    (activation == ActivationFunction::ReLU6 &&
     output_quant.quantize(0) == 0 &&
     output_quant.dequantize(255) <= 6.0f)
  )
  {
    // If the range of values which can be represented by a quantized value are
    // within the range that would be produced by the activation function, then
    // the activation function is redundant and can be skipped.
    return ActivationFunction::None;
  }
  else if(
    activation == ActivationFunction::ReLU6 &&
    output_quant.dequantize(255) <= 6.0f
  )
  {
    // If the largest value that can be represented by a quantized value is
    // lower than the upper boundary, then the activation function can be
    // relaxed to a ReLU.
    return ActivationFunction::ReLU;
  }

  return activation;
}

template <
  unsigned int OutputTileRows, unsigned int OutputTileCols,
  unsigned int KernelRows, unsigned int KernelCols,
  unsigned int StrideRows, unsigned int StrideCols
>
uint8_t QAsymm8DepthwiseConvolution<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, StrideRows, StrideCols
>::_input_padding_value(void) const
{
  return _inputs_quant.offset;
}

template <
  unsigned int OutputTileRows, unsigned int OutputTileCols,
  unsigned int KernelRows, unsigned int KernelCols,
  unsigned int StrideRows, unsigned int StrideCols
>
void QAsymm8DepthwiseConvolution<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, StrideRows, StrideCols
>::_pack_params(
  void * const buffer,
  const void * const weights,
  const unsigned int weight_row_stride,
  const unsigned int weight_col_stride,
  const void * const biases
) const
{
  const uint8_t *wptr = static_cast<const uint8_t *>(weights);
  const int32_t *bptr = static_cast<const int32_t *>(biases);
  uint8_t *outptr = static_cast<uint8_t *>(buffer);

  // We set the vector length to use quad registers on Aarch64 and only doubles
  // on Aarch32. NOTE For SVE set this to the actual vector length.
#if defined(__aarch64__)
  unsigned int veclen = 16;
#else
#if defined(__arm__)
  unsigned int veclen = 8;
#endif
#endif

  // Compute the rank 0 offset arising from the quantisation parameters.
  const int32_t rank0_offset = (KernelRows * KernelCols *
                                static_cast<int32_t>(_weights_quant.offset) *
                                static_cast<int32_t>(_inputs_quant.offset));

  // While there are channels left to process, pack a vector length of them at
  // a time and reduce the size of vector used as the size of the tensor
  // decreases.
  for (
    unsigned int n_channels = this->n_channels(); n_channels;
    n_channels -= veclen,
    outptr += veclen*(sizeof(int32_t) + this->kernel_rows*this->kernel_cols)
  )
  {
    // NOTE Ignore this section if using SVE, the vector length remains the
    // same and we just don't fill a full register for the tail.
    while (n_channels < veclen)
    {
      // Reduce the vector length to either 8 or 1 (scalar)
      // TODO Support more vector lengths in `execute_tile`.
      veclen = (veclen == 16) ? 8 : 1;
    }

    // Get pointers to bias and weight portions of the output structure.
    int32_t *out_bptr = reinterpret_cast<int32_t *>(outptr);
    uint8_t *out_wptr = outptr + veclen*sizeof(int32_t);

    // Copy a vector length of elements
    for (unsigned int n = 0; n < veclen && n < n_channels; n++)
    {
      int32_t bias = (bptr != nullptr) ? *(bptr++) : 0;
      uint32_t weight_sum = 0;

      for (unsigned int i = 0; i < KernelRows; i++)
      {
        uint8_t *row_outptr = out_wptr + i*KernelCols*veclen;
        for (unsigned int j = 0; j < KernelCols; j++)
        {
          uint8_t w = *(wptr + i*weight_row_stride + j*weight_col_stride);
          row_outptr[j*veclen + n] = w;
          weight_sum += static_cast<uint32_t>(w);
        }
      }
      wptr++;

      // Include in the bias contributions from the quantisation offset
      int32_t rank1_offset = static_cast<int32_t>(
        static_cast<uint32_t>(_inputs_quant.offset) * weight_sum
      );
      out_bptr[n] = bias + rank0_offset - rank1_offset;
    }
  }
}

template <
  unsigned int OutputTileRows, unsigned int OutputTileCols,
  unsigned int KernelRows, unsigned int KernelCols,
  unsigned int StrideRows, unsigned int StrideCols
>
template<ActivationFunction Activation>
void QAsymm8DepthwiseConvolution<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, StrideRows, StrideCols
>::execute_tile(
  int n_channels,
  const void* packed_params,
  const uint8_t* inptr,
  const unsigned int in_row_stride,
  const unsigned int in_col_stride,
  uint8_t* outptr,
  const unsigned int out_row_stride,
  const unsigned int out_col_stride
)
{
  // Activation parameters (unused if Activation is None)
  const uint8_t aqmin = _output_quant.offset;
  const uint8_t aqmax = (Activation == ActivationFunction::ReLU6) ?
    std::min<uint8_t>(255u, _output_quant.quantize(6.0f)) : 255u;

  // Byte type pointer to weights and biases
  const uint8_t *wbptr = static_cast<const uint8_t *>(packed_params);

#if defined(__aarch64__)  // Under Aarch64 only use quad registers
  for (; n_channels >= 16; n_channels -= 16)
  {
    // Load biases
    const int32x4_t biases[4] = {
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr)),
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr) + 4),
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr) + 8),
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr) + 12)
    };
    wbptr += 16*sizeof(int32_t);

    // Load weights
    uint8x16_t weights[KernelRows][KernelCols];
    for (unsigned int i = 0; i < KernelRows; i++)
    {
      for (unsigned int j = 0; j < KernelCols; j++)
      {
        weights[i][j] = vld1q_u8(wbptr);
        wbptr += 16;
      }
    }

    // Load the input activations
    uint8x16_t inputs[Base::inner_tile_rows][Base::inner_tile_cols];
    for (unsigned int i = 0; i < Base::inner_tile_rows; i++)
    {
      for (unsigned int j = 0; j < Base::inner_tile_cols; j++)
      {
        inputs[i][j] = vld1q_u8(inptr + i*in_row_stride + j*in_col_stride);
      }
    }
    inptr += 16;

    // Perform the convolution
    for (unsigned int oi = 0; oi < OutputTileRows; oi++)
    {
      for (unsigned int oj = 0; oj < OutputTileCols; oj++)
      {
        // Two sets of operations are required, we perform the
        // multiply-accumulates for the convolution proper but must also sum
        // the tile elements to account for the _weight_ offset.
        uint32x4_t accs[4];
        for (unsigned int i = 0; i < 4; i++)
        {
          accs[i] = reinterpret_cast<uint32x4_t>(biases[i]);
        }

        for (unsigned int wi = 0; wi < KernelRows; wi++)
        {
          for (unsigned int wj = 0; wj < KernelCols; wj++)
          {
            // Get relevant weight and activation pixel
            const uint8x16_t w = weights[wi][wj];
            const uint8x16_t x = inputs[oi*StrideRows + wi][oj*StrideCols + wj];

            // Perform multiplication and accumulation
            const uint16x8_t muls[2] = {
              vmull_u8(vget_low_u8(w), vget_low_u8(x)),
              vmull_u8(vget_high_u8(w), vget_high_u8(x))
            };

            const uint8x8_t woffset = vdup_n_u8(_weights_quant.offset);
            const uint16x8_t sum_elems[2] = {
              vmull_u8(vget_low_u8(x), woffset),
              vmull_u8(vget_high_u8(x), woffset)
            };

            const uint32x4_t tmps[4] = {
              vsubl_u16(vget_low_u16(muls[0]), vget_low_u16(sum_elems[0])),
              vsubl_u16(vget_high_u16(muls[0]), vget_high_u16(sum_elems[0])),
              vsubl_u16(vget_low_u16(muls[1]), vget_low_u16(sum_elems[1])),
              vsubl_u16(vget_high_u16(muls[1]), vget_high_u16(sum_elems[1])),
            };
            for (unsigned int i = 0; i < 4; i++)
            {
              accs[i] = vaddq_u32(accs[i], tmps[i]);
            }
          }
        }

        // Rescale the accumulator and add in the new offset.
        uint32x4_t final_accs[4];
        for (unsigned int i = 0; i < 4; i++)
        {
#ifdef FIXED_POINT_REQUANTISATION
          const int32x4_t y = rounding_divide_by_exp2(
            saturating_doubling_high_mul(
              reinterpret_cast<int32x4_t>(accs[i]), rescale_parameters.multiplier
            ),
            rescale_parameters.shift
          );
          const int32x4_t offset = reinterpret_cast<int32x4_t>(vdupq_n_u32(_output_quant.offset));
          final_accs[i] = reinterpret_cast<uint32x4_t>(vmaxq_s32(vaddq_s32(y, offset), vdupq_n_s32(0)));
#else  // floating point requantisation
          float32x4_t fp_acc = vcvtq_f32_s32(reinterpret_cast<int32x4_t>(accs[i]));
          fp_acc = vmulq_f32(fp_acc, vdupq_n_f32(rescale_parameters.rescale));
          fp_acc = vaddq_f32(fp_acc, vdupq_n_f32(static_cast<float>(_output_quant.offset)));
          fp_acc = vmaxq_f32(fp_acc, vdupq_n_f32(0.0f));
          final_accs[i] = vcvtq_u32_f32(fp_acc);
#endif
        }

        uint8x16_t output = vcombine_u8(
          vqmovn_u16(vcombine_u16(vqmovn_u32(final_accs[0]), vqmovn_u32(final_accs[1]))),
          vqmovn_u16(vcombine_u16(vqmovn_u32(final_accs[2]), vqmovn_u32(final_accs[3])))
        );

        // Apply the activation function
        if (Activation == ActivationFunction::ReLU ||
            Activation == ActivationFunction::ReLU6)
        {
          output = vmaxq_u8(output, vdupq_n_u8(aqmin));
        }
        if (Activation == ActivationFunction::ReLU6)
        {
          output = vminq_u8(output, vdupq_n_u8(aqmax));
        }

        vst1q_u8(outptr + oi*out_row_stride + oj*out_col_stride, output);
      }
    }
    outptr += 16;
  }
#endif  // defined(__aarch64__)
  for (; n_channels >= 8; n_channels -= 8)
  {
    const int32x4_t biases[2] = {
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr)),
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr) + 4),
    };
    wbptr += 8*sizeof(int32_t);

    uint8x8_t weights[KernelRows][KernelCols];
    for (unsigned int i = 0; i < KernelRows; i++)
    {
      for (unsigned int j = 0; j < KernelCols; j++)
      {
        weights[i][j] = vld1_u8(wbptr);
        wbptr += 8;
      }
    }

    uint8x8_t inputs[Base::inner_tile_rows][Base::inner_tile_cols];
    for (unsigned int i = 0; i < Base::inner_tile_rows; i++)
    {
      for (unsigned int j = 0; j < Base::inner_tile_cols; j++)
      {
        inputs[i][j] = vld1_u8(inptr + i*in_row_stride + j*in_col_stride);
      }
    }
    inptr += 8;

    for (unsigned int oi = 0; oi < OutputTileRows; oi++)
    {
      for (unsigned int oj = 0; oj < OutputTileCols; oj++)
      {
        uint32x4_t accs[2];
        for (unsigned int i = 0; i < 2; i++)
        {
          accs[i] = reinterpret_cast<uint32x4_t>(biases[i]);
        }

        for (unsigned int wi = 0; wi < KernelRows; wi++)
        {
          for (unsigned int wj = 0; wj < KernelCols; wj++)
          {
            const uint8x8_t w = weights[wi][wj];
            const uint8x8_t x = inputs[oi*StrideRows + wi][oj*StrideCols + wj];

            const uint16x8_t muls = vmull_u8(w, x);
            const uint8x8_t woffset = vdup_n_u8(_weights_quant.offset);
            const uint16x8_t sum_elems = vmull_u8(x, woffset);

            const uint32x4_t tmps[2] = {
              vsubl_u16(vget_low_u16(muls), vget_low_u16(sum_elems)),
              vsubl_u16(vget_high_u16(muls), vget_high_u16(sum_elems)),
            };
            for (unsigned int i = 0; i < 2; i++)
            {
              accs[i] = vaddq_u32(accs[i], tmps[i]);
            }
          }
        }

        uint32x4_t final_accs[2];
        for (unsigned int i = 0; i < 2; i++)
        {
#ifdef FIXED_POINT_REQUANTISATION
          const int32x4_t y = rounding_divide_by_exp2(
            saturating_doubling_high_mul(
              reinterpret_cast<int32x4_t>(accs[i]), rescale_parameters.multiplier
            ),
            rescale_parameters.shift
          );
          const int32x4_t offset = reinterpret_cast<int32x4_t>(vdupq_n_u32(_output_quant.offset));
          final_accs[i] = reinterpret_cast<uint32x4_t>(vmaxq_s32(vaddq_s32(y, offset), vdupq_n_s32(0)));
#else  // floating point requantisation
          float32x4_t fp_acc = vcvtq_f32_s32(reinterpret_cast<int32x4_t>(accs[i]));
          fp_acc = vmulq_f32(fp_acc, vdupq_n_f32(rescale_parameters.rescale));
          fp_acc = vaddq_f32(fp_acc, vdupq_n_f32(static_cast<float>(_output_quant.offset)));
          fp_acc = vmaxq_f32(fp_acc, vdupq_n_f32(0.0f));
          final_accs[i] = vcvtq_u32_f32(fp_acc);
#endif
        }

        uint8x8_t output = vqmovn_u16(vcombine_u16(vqmovn_u32(final_accs[0]), vqmovn_u32(final_accs[1])));

        // Apply the activation function
        if (Activation == ActivationFunction::ReLU ||
            Activation == ActivationFunction::ReLU6)
        {
          output = vmax_u8(output, vdup_n_u8(aqmin));
        }
        if (Activation == ActivationFunction::ReLU6)
        {
          output = vmin_u8(output, vdup_n_u8(aqmax));
        }

        vst1_u8(outptr + oi*out_row_stride + oj*out_col_stride, output);
      }
    }
    outptr += 8;
  }
  for (; n_channels; n_channels--)
  {
    // Load bias
    const int32_t bias = *reinterpret_cast<const int32_t *>(wbptr);
    wbptr += sizeof(int32_t);

    // Load weights
    uint8_t weights[KernelRows][KernelCols];
    for (unsigned int i = 0; i < KernelRows; i++)
    {
      for (unsigned int j = 0; j < KernelCols; j++)
      {
        weights[i][j] = *(wbptr++);
      }
    }

    // Load the input activations
    uint8_t inputs[Base::inner_tile_rows][Base::inner_tile_cols];
    for (unsigned int i = 0; i < Base::inner_tile_rows; i++)
    {
      for (unsigned int j = 0; j < Base::inner_tile_cols; j++)
      {
        inputs[i][j] = *(inptr + i*in_row_stride + j*in_col_stride);
      }
    }
    inptr++;

    // Perform the convolution
    for (unsigned int oi = 0; oi < OutputTileRows; oi++)
    {
      for (unsigned int oj = 0; oj < OutputTileCols; oj++)
      {
        int32_t acc = bias;
        uint32_t element_sum = 0;

        for (unsigned int wi = 0; wi < KernelRows; wi++)
        {
          for (unsigned int wj = 0; wj < KernelCols; wj++)
          {
            const auto w = weights[wi][wj], x = inputs[oi*StrideRows + wi][oj*StrideCols + wj];
            acc += static_cast<int32_t>(static_cast<uint32_t>(w) * static_cast<uint32_t>(x));
            element_sum += static_cast<uint32_t>(x);
          }
        }

        acc -= static_cast<int32_t>(element_sum) * static_cast<int32_t>(_weights_quant.offset);

        // Requantize
#ifdef FIXED_POINT_REQUANTISATION
        acc = rounding_divide_by_exp2(
            saturating_doubling_high_mul(acc, rescale_parameters.multiplier),
            rescale_parameters.shift
        );
        acc += _output_quant.offset;
        uint8_t output = clamp_to_limits<uint8_t>::clamp_and_cast<int32_t>(acc);
#else  // floating point requantization
        float fp_acc = static_cast<float>(acc);
        fp_acc *= rescale_parameters.rescale;
        fp_acc += static_cast<float>(_output_quant.offset);
        fp_acc = std::max<float>(fp_acc, 0.0f);
        uint8_t output = static_cast<uint8_t>(std::min<int32_t>(static_cast<int32_t>(fp_acc), 255));
#endif

        // Apply the activation function
        if (Activation == ActivationFunction::ReLU ||
            Activation == ActivationFunction::ReLU6)
        {
          output = std::max(output, aqmin);
        }
        if (Activation == ActivationFunction::ReLU6)
        {
          output = std::min(output, aqmax);
        }

        *(outptr + oi*out_row_stride + oj*out_col_stride) = output;
      }
    }
    outptr++;
  }
}

template <
  unsigned int OutputTileRows, unsigned int OutputTileCols,
  unsigned int KernelRows, unsigned int KernelCols,
  unsigned int StrideRows, unsigned int StrideCols
>
template<ActivationFunction Activation>
void QAsymm8DepthwiseConvolution<
  OutputTileRows, OutputTileCols, KernelRows, KernelCols, StrideRows, StrideCols
>::execute_tile(
  int n_channels,
  const void* packed_params,
  const uint8_t* inptrs[Base::inner_tile_rows][Base::inner_tile_cols],
  uint8_t* outptrs[Base::output_tile_rows][Base::output_tile_cols]
)
{
  // Activation parameters (unused if Activation is None)
  const uint8_t aqmin = _output_quant.offset;
  const uint8_t aqmax = (Activation == ActivationFunction::ReLU6) ?
    std::min<uint8_t>(255u, _output_quant.quantize(6.0f)) : 255u;

  // Byte type pointer to weights and biases
  const uint8_t *wbptr = static_cast<const uint8_t *>(packed_params);

  // Offset into input/output tensors
  int n = 0;

#if defined(__aarch64__)  // Under Aarch64 only use quad registers
  for (; n_channels >= 16; n_channels -= 16, n += 16)
  {
    // Load biases
    const int32x4_t biases[4] = {
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr)),
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr) + 4),
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr) + 8),
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr) + 12)
    };
    wbptr += 16*sizeof(int32_t);

    // Load weights
    uint8x16_t weights[KernelRows][KernelCols];
    for (unsigned int i = 0; i < KernelRows; i++)
    {
      for (unsigned int j = 0; j < KernelCols; j++)
      {
        weights[i][j] = vld1q_u8(wbptr);
        wbptr += 16;
      }
    }

    // Load the input activations
    uint8x16_t inputs[Base::inner_tile_rows][Base::inner_tile_cols];
    for (unsigned int i = 0; i < Base::inner_tile_rows; i++)
    {
      for (unsigned int j = 0; j < Base::inner_tile_cols; j++)
      {
        inputs[i][j] = vld1q_u8(inptrs[i][j] + n);
      }
    }

    // Perform the convolution
    for (unsigned int oi = 0; oi < OutputTileRows; oi++)
    {
      for (unsigned int oj = 0; oj < OutputTileCols; oj++)
      {
        // Two sets of operations are required, we perform the
        // multiply-accumulates for the convolution proper but must also sum
        // the tile elements to account for the _weight_ offset.
        uint32x4_t accs[4];
        for (unsigned int i = 0; i < 4; i++)
        {
          accs[i] = reinterpret_cast<uint32x4_t>(biases[i]);
        }

        for (unsigned int wi = 0; wi < KernelRows; wi++)
        {
          for (unsigned int wj = 0; wj < KernelCols; wj++)
          {
            // Get relevant weight and activation pixel
            const uint8x16_t w = weights[wi][wj];
            const uint8x16_t x = inputs[oi*StrideRows + wi][oj*StrideCols + wj];

            // Perform multiplication and accumulation
            const uint16x8_t muls[2] = {
              vmull_u8(vget_low_u8(w), vget_low_u8(x)),
              vmull_u8(vget_high_u8(w), vget_high_u8(x))
            };

            const uint8x8_t woffset = vdup_n_u8(_weights_quant.offset);
            const uint16x8_t sum_elems[2] = {
              vmull_u8(vget_low_u8(x), woffset),
              vmull_u8(vget_high_u8(x), woffset)
            };

            const uint32x4_t tmps[4] = {
              vsubl_u16(vget_low_u16(muls[0]), vget_low_u16(sum_elems[0])),
              vsubl_u16(vget_high_u16(muls[0]), vget_high_u16(sum_elems[0])),
              vsubl_u16(vget_low_u16(muls[1]), vget_low_u16(sum_elems[1])),
              vsubl_u16(vget_high_u16(muls[1]), vget_high_u16(sum_elems[1])),
            };
            for (unsigned int i = 0; i < 4; i++)
            {
              accs[i] = vaddq_u32(accs[i], tmps[i]);
            }
          }
        }

        // Rescale the accumulator and add in the new offset.
        uint32x4_t final_accs[4];
        for (unsigned int i = 0; i < 4; i++)
        {
#ifdef FIXED_POINT_REQUANTISATION
          const int32x4_t y = rounding_divide_by_exp2(
            saturating_doubling_high_mul(
              reinterpret_cast<int32x4_t>(accs[i]), rescale_parameters.multiplier
            ),
            rescale_parameters.shift
          );
          const int32x4_t offset = reinterpret_cast<int32x4_t>(vdupq_n_u32(_output_quant.offset));
          final_accs[i] = reinterpret_cast<uint32x4_t>(vmaxq_s32(vaddq_s32(y, offset), vdupq_n_s32(0)));
#else  // floating point requantisation
          float32x4_t fp_acc = vcvtq_f32_s32(reinterpret_cast<int32x4_t>(accs[i]));
          fp_acc = vmulq_f32(fp_acc, vdupq_n_f32(rescale_parameters.rescale));
          fp_acc = vaddq_f32(fp_acc, vdupq_n_f32(static_cast<float>(_output_quant.offset)));
          fp_acc = vmaxq_f32(fp_acc, vdupq_n_f32(0.0f));
          final_accs[i] = vcvtq_u32_f32(fp_acc);
#endif
        }

        uint8x16_t output = vcombine_u8(
          vqmovn_u16(vcombine_u16(vqmovn_u32(final_accs[0]), vqmovn_u32(final_accs[1]))),
          vqmovn_u16(vcombine_u16(vqmovn_u32(final_accs[2]), vqmovn_u32(final_accs[3])))
        );

        // Apply the activation function
        if (Activation == ActivationFunction::ReLU ||
            Activation == ActivationFunction::ReLU6)
        {
          output = vmaxq_u8(output, vdupq_n_u8(aqmin));
        }
        if (Activation == ActivationFunction::ReLU6)
        {
          output = vminq_u8(output, vdupq_n_u8(aqmax));
        }

        vst1q_u8(outptrs[oi][oj] + n, output);
      }
    }
  }
#endif  // defined(__aarch64__)
  for (; n_channels >= 8; n_channels -= 8, n += 8)
  {
    const int32x4_t biases[2] = {
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr)),
      vld1q_s32(reinterpret_cast<const int32_t *>(wbptr) + 4),
    };
    wbptr += 8*sizeof(int32_t);

    uint8x8_t weights[KernelRows][KernelCols];
    for (unsigned int i = 0; i < KernelRows; i++)
    {
      for (unsigned int j = 0; j < KernelCols; j++)
      {
        weights[i][j] = vld1_u8(wbptr);
        wbptr += 8;
      }
    }

    uint8x8_t inputs[Base::inner_tile_rows][Base::inner_tile_cols];
    for (unsigned int i = 0; i < Base::inner_tile_rows; i++)
    {
      for (unsigned int j = 0; j < Base::inner_tile_cols; j++)
      {
        inputs[i][j] = vld1_u8(inptrs[i][j] + n);
      }
    }

    for (unsigned int oi = 0; oi < OutputTileRows; oi++)
    {
      for (unsigned int oj = 0; oj < OutputTileCols; oj++)
      {
        uint32x4_t accs[2];
        for (unsigned int i = 0; i < 2; i++)
        {
          accs[i] = reinterpret_cast<uint32x4_t>(biases[i]);
        }

        for (unsigned int wi = 0; wi < KernelRows; wi++)
        {
          for (unsigned int wj = 0; wj < KernelCols; wj++)
          {
            const uint8x8_t w = weights[wi][wj];
            const uint8x8_t x = inputs[oi*StrideRows + wi][oj*StrideCols + wj];

            const uint16x8_t muls = vmull_u8(w, x);
            const uint8x8_t woffset = vdup_n_u8(_weights_quant.offset);
            const uint16x8_t sum_elems = vmull_u8(x, woffset);

            const uint32x4_t tmps[2] = {
              vsubl_u16(vget_low_u16(muls), vget_low_u16(sum_elems)),
              vsubl_u16(vget_high_u16(muls), vget_high_u16(sum_elems)),
            };
            for (unsigned int i = 0; i < 2; i++)
            {
              accs[i] = vaddq_u32(accs[i], tmps[i]);
            }
          }
        }

        uint32x4_t final_accs[2];
        for (unsigned int i = 0; i < 2; i++)
        {
#ifdef FIXED_POINT_REQUANTISATION
          const int32x4_t y = rounding_divide_by_exp2(
            saturating_doubling_high_mul(
              reinterpret_cast<int32x4_t>(accs[i]), rescale_parameters.multiplier
            ),
            rescale_parameters.shift
          );
          const int32x4_t offset = reinterpret_cast<int32x4_t>(vdupq_n_u32(_output_quant.offset));
          final_accs[i] = reinterpret_cast<uint32x4_t>(vmaxq_s32(vaddq_s32(y, offset), vdupq_n_s32(0)));
#else  // floating point requantisation
          float32x4_t fp_acc = vcvtq_f32_s32(reinterpret_cast<int32x4_t>(accs[i]));
          fp_acc = vmulq_f32(fp_acc, vdupq_n_f32(rescale_parameters.rescale));
          fp_acc = vaddq_f32(fp_acc, vdupq_n_f32(static_cast<float>(_output_quant.offset)));
          fp_acc = vmaxq_f32(fp_acc, vdupq_n_f32(0.0f));
          final_accs[i] = vcvtq_u32_f32(fp_acc);
#endif
        }

        uint8x8_t output = vqmovn_u16(vcombine_u16(vqmovn_u32(final_accs[0]), vqmovn_u32(final_accs[1])));

        // Apply the activation function
        if (Activation == ActivationFunction::ReLU ||
            Activation == ActivationFunction::ReLU6)
        {
          output = vmax_u8(output, vdup_n_u8(aqmin));
        }
        if (Activation == ActivationFunction::ReLU6)
        {
          output = vmin_u8(output, vdup_n_u8(aqmax));
        }

        vst1_u8(outptrs[oi][oj] + n, output);
      }
    }
  }
  for (; n_channels; n_channels--, n++)
  {
    // Load bias
    const int32_t bias = *reinterpret_cast<const int32_t *>(wbptr);
    wbptr += sizeof(int32_t);

    // Load weights
    uint8_t weights[KernelRows][KernelCols];
    for (unsigned int i = 0; i < KernelRows; i++)
    {
      for (unsigned int j = 0; j < KernelCols; j++)
      {
        weights[i][j] = *(wbptr++);
      }
    }

    // Load the input activations
    uint8_t inputs[Base::inner_tile_rows][Base::inner_tile_cols];
    for (unsigned int i = 0; i < Base::inner_tile_rows; i++)
    {
      for (unsigned int j = 0; j < Base::inner_tile_cols; j++)
      {
        inputs[i][j] = *(inptrs[i][j] + n);
      }
    }

    // Perform the convolution
    for (unsigned int oi = 0; oi < OutputTileRows; oi++)
    {
      for (unsigned int oj = 0; oj < OutputTileCols; oj++)
      {
        int32_t acc = bias;
        uint32_t element_sum = 0;

        for (unsigned int wi = 0; wi < KernelRows; wi++)
        {
          for (unsigned int wj = 0; wj < KernelCols; wj++)
          {
            const auto w = weights[wi][wj], x = inputs[oi*StrideRows + wi][oj*StrideCols + wj];
            acc += static_cast<int32_t>(static_cast<uint32_t>(w) * static_cast<uint32_t>(x));
            element_sum += static_cast<uint32_t>(x);
          }
        }

        acc -= static_cast<int32_t>(element_sum) * static_cast<int32_t>(_weights_quant.offset);

        // Requantize
#ifdef FIXED_POINT_REQUANTISATION
        acc = rounding_divide_by_exp2(
            saturating_doubling_high_mul(acc, rescale_parameters.multiplier),
            rescale_parameters.shift
        );
        acc += _output_quant.offset;
        uint8_t output = clamp_to_limits<uint8_t>::clamp_and_cast<int32_t>(acc);
#else  // floating point requantization
        float fp_acc = static_cast<float>(acc);
        fp_acc *= rescale_parameters.rescale;
        fp_acc += static_cast<float>(_output_quant.offset);
        fp_acc = std::max<float>(fp_acc, 0.0f);
        uint8_t output = static_cast<uint8_t>(std::min<int32_t>(static_cast<int32_t>(fp_acc), 255));
#endif

        // Apply the activation function
        if (Activation == ActivationFunction::ReLU ||
            Activation == ActivationFunction::ReLU6)
        {
          output = std::max(output, aqmin);
        }
        if (Activation == ActivationFunction::ReLU6)
        {
          output = std::min(output, aqmax);
        }

        *(outptrs[oi][oj] + n) = output;
      }
    }
  }
}

}  // namespace depthwise