forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeconvolution-operator-tester.h
2744 lines (2426 loc) · 129 KB
/
deconvolution-operator-tester.h
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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Facebook, Inc. and its affiliates.
// All rights reserved.
//
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#pragma once
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <memory>
#include <numeric>
#include <random>
#include <utility>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <fp16/fp16.h>
#include "xnnpack.h"
#include "xnnpack/cache.h"
#include "xnnpack/common.h"
#include "xnnpack/microparams.h"
#include "replicable_random_device.h"
class DeconvolutionOperatorTester {
public:
enum class WeightsType {
Default,
FP32,
};
enum class Activation {
MinMax, // Default activation used in tests. If tests do not specify
// qmin/qmax, it is equivalent to linear activation.
Relu,
};
DeconvolutionOperatorTester& padding(uint32_t padding) {
this->padding_top_ = padding;
this->padding_right_ = padding;
this->padding_bottom_ = padding;
this->padding_left_ = padding;
return *this;
}
DeconvolutionOperatorTester& padding_height(uint32_t padding_height) {
this->padding_top_ = padding_height;
this->padding_bottom_ = padding_height;
return *this;
}
uint32_t padding_height() const {
return this->padding_top_ + this->padding_bottom_;
}
DeconvolutionOperatorTester& padding_width(uint32_t padding_width) {
this->padding_right_ = padding_width;
this->padding_left_ = padding_width;
return *this;
}
uint32_t padding_width() const {
return this->padding_left_ + this->padding_right_;
}
DeconvolutionOperatorTester& padding_top(uint32_t padding_top) {
this->padding_top_ = padding_top;
return *this;
}
uint32_t padding_top() const { return this->padding_top_; }
DeconvolutionOperatorTester& padding_right(uint32_t padding_right) {
this->padding_right_ = padding_right;
return *this;
}
uint32_t padding_right() const { return this->padding_right_; }
DeconvolutionOperatorTester& padding_bottom(uint32_t padding_bottom) {
this->padding_bottom_ = padding_bottom;
return *this;
}
uint32_t padding_bottom() const { return this->padding_bottom_; }
DeconvolutionOperatorTester& padding_left(uint32_t padding_left) {
this->padding_left_ = padding_left;
return *this;
}
uint32_t padding_left() const { return this->padding_left_; }
DeconvolutionOperatorTester& adjustment_height(uint32_t adjustment_height) {
this->adjustment_height_ = adjustment_height;
return *this;
}
uint32_t adjustment_height() const {
return this->adjustment_height_;
}
DeconvolutionOperatorTester& adjustment_width(uint32_t adjustment_width) {
this->adjustment_width_ = adjustment_width;
return *this;
}
uint32_t adjustment_width() const {
return this->adjustment_width_;
}
DeconvolutionOperatorTester& input_size(uint32_t input_height, uint32_t input_width) {
assert(input_height >= 1);
assert(input_width >= 1);
this->input_height_ = input_height;
this->input_width_ = input_width;
return *this;
}
DeconvolutionOperatorTester& input_height(uint32_t input_height) {
assert(input_height >= 1);
this->input_height_ = input_height;
return *this;
}
uint32_t input_height() const {
return this->input_height_;
}
DeconvolutionOperatorTester& input_width(uint32_t input_width) {
assert(input_width >= 1);
this->input_width_ = input_width;
return *this;
}
uint32_t input_width() const {
return this->input_width_;
}
DeconvolutionOperatorTester& groups(uint32_t groups) {
assert(groups >= 1);
this->groups_ = groups;
return *this;
}
uint32_t groups() const {
return this->groups_;
}
DeconvolutionOperatorTester& group_input_channels(size_t group_input_channels) {
assert(group_input_channels >= 1);
this->group_input_channels_ = group_input_channels;
return *this;
}
size_t group_input_channels() const {
return this->group_input_channels_;
}
DeconvolutionOperatorTester& group_output_channels(size_t group_output_channels) {
assert(group_output_channels >= 1);
this->group_output_channels_ = group_output_channels;
return *this;
}
size_t group_output_channels() const {
return this->group_output_channels_;
}
DeconvolutionOperatorTester& batch_size(size_t batch_size) {
assert(batch_size >= 1);
this->batch_size_ = batch_size;
return *this;
}
size_t batch_size() const {
return this->batch_size_;
}
DeconvolutionOperatorTester& kernel_size(uint32_t kernel_size) {
assert(kernel_size >= 1);
this->kernel_height_ = kernel_size;
this->kernel_width_ = kernel_size;
return *this;
}
DeconvolutionOperatorTester& kernel_size(uint32_t kernel_height, uint32_t kernel_width) {
assert(kernel_height >= 1);
assert(kernel_width >= 1);
this->kernel_height_ = kernel_height;
this->kernel_width_ = kernel_width;
return *this;
}
DeconvolutionOperatorTester& kernel_height(uint32_t kernel_height) {
assert(kernel_height >= 1);
this->kernel_height_ = kernel_height;
return *this;
}
uint32_t kernel_height() const {
return this->kernel_height_;
}
DeconvolutionOperatorTester& kernel_width(uint32_t kernel_width) {
assert(kernel_width >= 1);
this->kernel_width_ = kernel_width;
return *this;
}
uint32_t kernel_width() const {
return this->kernel_width_;
}
DeconvolutionOperatorTester& dilation(uint32_t dilation) {
assert(dilation >= 1);
this->dilation_height_ = dilation;
this->dilation_width_ = dilation;
return *this;
}
DeconvolutionOperatorTester& dilation(uint32_t dilation_height, uint32_t dilation_width) {
assert(dilation_height >= 1);
assert(dilation_width >= 1);
this->dilation_height_ = dilation_height;
this->dilation_width_ = dilation_width;
return *this;
}
DeconvolutionOperatorTester& dilation_height(uint32_t dilation_height) {
assert(dilation_height >= 1);
this->dilation_height_ = dilation_height;
return *this;
}
uint32_t dilation_height() const {
return this->dilation_height_;
}
DeconvolutionOperatorTester& dilation_width(uint32_t dilation_width) {
assert(dilation_width >= 1);
this->dilation_width_ = dilation_width;
return *this;
}
uint32_t dilation_width() const {
return this->dilation_width_;
}
DeconvolutionOperatorTester& stride(uint32_t stride) {
assert(stride >= 1);
this->stride_height_ = stride;
this->stride_width_ = stride;
return *this;
}
DeconvolutionOperatorTester& stride(uint32_t stride_height, uint32_t stride_width) {
assert(stride_height >= 1);
assert(stride_width >= 1);
this->stride_height_ = stride_height;
this->stride_width_ = stride_width;
return *this;
}
DeconvolutionOperatorTester& stride_height(uint32_t stride_height) {
assert(stride_height >= 1);
this->stride_height_ = stride_height;
return *this;
}
uint32_t stride_height() const {
return this->stride_height_;
}
DeconvolutionOperatorTester& stride_width(uint32_t stride_width) {
assert(stride_width >= 1);
this->stride_width_ = stride_width;
return *this;
}
uint32_t stride_width() const {
return this->stride_width_;
}
DeconvolutionOperatorTester& input_pixel_stride(size_t input_pixel_stride) {
assert(input_pixel_stride >= 1);
this->input_pixel_stride_ = input_pixel_stride;
return *this;
}
size_t input_pixel_stride() const {
if (this->input_pixel_stride_ == 0) {
return group_input_channels() * groups();
} else {
assert(this->input_pixel_stride_ >= group_input_channels() * groups());
return this->input_pixel_stride_;
}
}
DeconvolutionOperatorTester& output_pixel_stride(size_t output_pixel_stride) {
assert(output_pixel_stride >= 1);
this->output_pixel_stride_ = output_pixel_stride;
return *this;
}
size_t output_pixel_stride() const {
if (this->output_pixel_stride_ == 0) {
return group_output_channels() * groups();
} else {
assert(this->output_pixel_stride_ >= group_output_channels() * groups());
return this->output_pixel_stride_;
}
}
uint32_t dilated_kernel_height() const {
return (kernel_height() - 1) * dilation_height() + 1;
}
uint32_t dilated_kernel_width() const {
return (kernel_width() - 1) * dilation_width() + 1;
}
size_t output_height() const {
return stride_height() * (input_height() - 1) + adjustment_height() + dilated_kernel_height() - padding_height();
}
size_t output_width() const {
return stride_width() * (input_width() - 1) + adjustment_width() + dilated_kernel_width() - padding_width();
}
DeconvolutionOperatorTester& next_input_size(uint32_t next_input_height, uint32_t next_input_width) {
assert(next_input_height >= 1);
assert(next_input_width >= 1);
this->next_input_height_ = next_input_height;
this->next_input_width_ = next_input_width;
return *this;
}
DeconvolutionOperatorTester& next_input_height(uint32_t next_input_height) {
assert(next_input_height >= 1);
this->next_input_height_ = next_input_height;
return *this;
}
uint32_t next_input_height() const {
if (this->next_input_height_ == 0) {
return input_height();
} else {
return this->next_input_height_;
}
}
DeconvolutionOperatorTester& next_input_width(uint32_t next_input_width) {
assert(next_input_width >= 1);
this->next_input_width_ = next_input_width;
return *this;
}
uint32_t next_input_width() const {
if (this->next_input_width_ == 0) {
return input_width();
} else {
return this->next_input_width_;
}
}
size_t next_output_height() const {
return stride_height() * (next_input_height() - 1) + adjustment_height() + dilated_kernel_height() - padding_height();
}
size_t next_output_width() const {
return stride_width() * (next_input_width() - 1) + adjustment_width() + dilated_kernel_width() - padding_width();
}
DeconvolutionOperatorTester& next_batch_size(size_t next_batch_size) {
assert(next_batch_size >= 1);
this->next_batch_size_ = next_batch_size;
return *this;
}
size_t next_batch_size() const {
if (this->next_batch_size_ == 0) {
return batch_size();
} else {
return this->next_batch_size_;
}
}
DeconvolutionOperatorTester& qmin(uint8_t qmin) {
this->qmin_ = qmin;
return *this;
}
uint8_t qmin() const {
return this->qmin_;
}
DeconvolutionOperatorTester& qmax(uint8_t qmax) {
this->qmax_ = qmax;
return *this;
}
uint8_t qmax() const {
return this->qmax_;
}
DeconvolutionOperatorTester& activation(Activation activation) {
this->activation_ = activation;
return *this;
}
Activation activation() const {
return this->activation_;
}
DeconvolutionOperatorTester& has_bias(bool has_bias) {
this->has_bias_ = has_bias;
return *this;
}
bool has_bias() const {
return this->has_bias_;
}
DeconvolutionOperatorTester& weights_type(WeightsType weights_type) {
this->weights_type_ = weights_type;
return *this;
}
WeightsType weights_type() const {
return this->weights_type_;
}
DeconvolutionOperatorTester& use_weights_cache(bool use_weights_cache) {
this->use_weights_cache_ = use_weights_cache;
return *this;
}
bool use_weights_cache() const {
return this->use_weights_cache_;
}
DeconvolutionOperatorTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
size_t iterations() const {
return this->iterations_;
}
void TestQC8() const {
ASSERT_EQ(weights_type(), WeightsType::Default);
xnnpack::ReplicableRandomDevice rng;
std::uniform_int_distribution<int32_t> i32dist(-10000, 10000);
std::uniform_int_distribution<int32_t> i8dist(
std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max());
std::uniform_int_distribution<int32_t> w8dist(
-std::numeric_limits<int8_t>::max(), std::numeric_limits<int8_t>::max());
std::uniform_real_distribution<float> f32dist(0.1f, 1.0f);
std::vector<int8_t> input(XNN_EXTRA_BYTES / sizeof(int8_t) +
(batch_size() * input_height() * input_width() - 1) * input_pixel_stride() + groups() * group_input_channels());
std::vector<int8_t> kernel(groups() * group_output_channels() * kernel_height() * kernel_width() * group_input_channels());
std::vector<int32_t> bias(groups() * group_output_channels());
std::vector<int8_t> output((batch_size() * output_height() * output_width() - 1) * output_pixel_stride() + groups() * group_output_channels());
std::vector<int32_t> accumulators(batch_size() * output_height() * output_width() * groups() * group_output_channels());
std::vector<double> output_ref(batch_size() * output_height() * output_width() * groups() * group_output_channels());
std::vector<float> requantization_scales(groups() * group_output_channels());
const int8_t input_zero_point = 1;
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return i8dist(rng); });
std::generate(kernel.begin(), kernel.end(), [&]() { return w8dist(rng); });
std::generate(bias.begin(), bias.end(), [&]() { return i32dist(rng); });
std::fill(output.begin(), output.end(), INT8_C(0xA5));
// Compute reference results, without renormalization.
if (has_bias()) {
for (size_t i = 0; i < batch_size(); i++) {
for (size_t oy = 0; oy < output_height(); oy++) {
for (size_t ox = 0; ox < output_width(); ox++) {
for (size_t g = 0; g < groups(); g++) {
for (size_t oc = 0; oc < group_output_channels(); oc++) {
accumulators[(((i * output_height() + oy) * output_width() + ox) * groups() + g) * group_output_channels() + oc] =
bias[g * group_output_channels() + oc];
}
}
}
}
}
} else {
std::fill(accumulators.begin(), accumulators.end(), 0);
}
for (size_t i = 0; i < batch_size(); i++) {
for (size_t oy = 0; oy < output_height(); oy++) {
for (size_t ox = 0; ox < output_width(); ox++) {
for (size_t ky = 0; ky < kernel_height(); ky++) {
const size_t y = oy + padding_top() - ky * dilation_height();
const size_t iy = y / stride_height();
if (iy * stride_height() == y && iy < input_height()) {
for (size_t kx = 0; kx < kernel_width(); kx++) {
const size_t x = ox + padding_left() - kx * dilation_width();
const size_t ix = x / stride_width();
if (ix * stride_width() == x && ix < input_width()) {
for (size_t g = 0; g < groups(); g++) {
for (size_t oc = 0; oc < group_output_channels(); oc++) {
for (size_t ic = 0; ic < group_input_channels(); ic++) {
accumulators[(((i * output_height() + oy) * output_width() + ox) * groups() + g) * group_output_channels() + oc] +=
(int32_t(input[((i * input_height() + iy) * input_width() + ix) * input_pixel_stride() + g * group_input_channels() + ic]) - int32_t(input_zero_point)) *
int32_t(kernel[(((g * group_output_channels() + oc) * kernel_height() + ky) * kernel_width() + kx) * group_input_channels() + ic]);
}
}
}
}
}
}
}
}
}
}
// Compute renormalization parameters.
const int8_t output_zero_point = -1;
for (size_t c = 0; c < groups() * group_output_channels(); c++) {
int32_t accumulated_min = accumulators[c];
int32_t accumulated_max = accumulators[c];
for (size_t px = 0; px < batch_size() * output_height() * output_width(); px++) {
accumulated_min = std::min(accumulated_min, accumulators[px * groups() * group_output_channels() + c]);
accumulated_max = std::max(accumulated_max, accumulators[px * groups() * group_output_channels() + c]);
}
float requantization_scale = 2.328e-10f; //0x1.0p-32f;
if (accumulated_max != 0) {
requantization_scale = std::max(requantization_scale,
float(int32_t(std::numeric_limits<int8_t>::max()) - int32_t(output_zero_point)) / float(accumulated_max));
}
if (accumulated_min != 0) {
requantization_scale = std::max(requantization_scale,
float(int32_t(std::numeric_limits<int8_t>::min()) - int32_t(output_zero_point)) / float(accumulated_min));
}
requantization_scale = std::min(requantization_scale, 1.f);
requantization_scales[c] = requantization_scale;
}
// Renormalize reference results.
for (size_t c = 0; c < groups() * group_output_channels(); c++) {
for (size_t px = 0; px < batch_size() * output_height() * output_width(); px++) {
output_ref[px * groups() * group_output_channels() + c] = double(int32_t(output_zero_point)) +
double(accumulators[px * groups() * group_output_channels() + c]) * double(requantization_scales[c]);
}
}
std::transform(output_ref.cbegin(), output_ref.cend(), output_ref.begin(),
[this](double x) -> double {
return std::max<double>(std::min<double>(x, double(qmax() - 0x80)), double(qmin() - 0x80));
});
// Create, setup, run, and destroy Deconvolution operator.
ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */));
xnn_operator_t deconvolution_op = nullptr;
struct xnn_internal_weights_cache* internal_weights_cache = nullptr;
std::unique_ptr<xnn_weights_cache_provider, decltype(&xnn_delete_weights_cache)> auto_weights_cache(
nullptr, xnn_delete_weights_cache);
if (use_weights_cache()) {
xnn_weights_cache_t weights_cache = nullptr;
xnn_create_weights_cache(&weights_cache);
auto_weights_cache.reset(weights_cache);
if (weights_cache) {
internal_weights_cache = (struct xnn_internal_weights_cache*) weights_cache->context;
}
}
ASSERT_EQ(
xnn_status_success,
xnn_create_deconvolution2d_nhwc_qs8_qc8w(
padding_top(), padding_right(), padding_bottom(), padding_left(),
kernel_height(), kernel_width(), stride_height(), stride_width(),
dilation_height(), dilation_width(), groups(),
group_input_channels(), group_output_channels(),
input_pixel_stride(), output_pixel_stride(), input_zero_point,
/*input_scale=*/1.0f, requantization_scales.data(), kernel.data(),
has_bias() ? bias.data() : nullptr, output_zero_point,
/*output_scale=*/1.f, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80),
/*flags=*/0, /*code_cache=*/nullptr, auto_weights_cache.get(), &deconvolution_op));
if (use_weights_cache()) {
ASSERT_EQ(xnn_status_success,
xnn_finalize_weights_cache(auto_weights_cache.get(), xnn_weights_cache_finalization_kind_soft));
}
// Smart pointer to automatically delete deconvolution_op.
std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_deconvolution_op(deconvolution_op, xnn_delete_operator);
ASSERT_EQ(xnn_status_success,
xnn_reshape_deconvolution2d_nhwc_qs8_qc8w(
deconvolution_op,
batch_size(), input_height(), input_width(),
adjustment_height(), adjustment_width(),
/*output_height_out=*/nullptr, /*output_width_out=*/nullptr,
/*threadpool=*/nullptr));
ASSERT_EQ(xnn_status_success,
xnn_setup_deconvolution2d_nhwc_qs8_qc8w(
deconvolution_op,
input.data(), output.data()));
ASSERT_EQ(xnn_status_success,
xnn_run_operator(deconvolution_op, /*threadpool=*/nullptr));
VerifyQC8(output, output_ref);
if (use_weights_cache()) {
xnn_operator_t deconvolution_op2 = nullptr;
size_t old_weights_cache_size = internal_weights_cache->cache.weights.size;
ASSERT_EQ(
xnn_status_success,
xnn_create_deconvolution2d_nhwc_qs8_qc8w(
padding_top(), padding_right(), padding_bottom(), padding_left(),
kernel_height(), kernel_width(), stride_height(), stride_width(),
dilation_height(), dilation_width(), groups(),
group_input_channels(), group_output_channels(),
input_pixel_stride(), output_pixel_stride(), input_zero_point,
1.0f /* input scale */, requantization_scales.data(), kernel.data(),
has_bias() ? bias.data() : nullptr, output_zero_point,
/*output_scale=*/1.0f, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80),
/*flags=*/0, /*code_cache=*/nullptr, auto_weights_cache.get(), &deconvolution_op2));
// Smart pointer to automatically delete deconvolution_op2.
std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_deconvolution_op(deconvolution_op2, xnn_delete_operator);
std::vector<int8_t> output2(output.size(), INT8_C(0xA5));
ASSERT_EQ(xnn_status_success,
xnn_reshape_deconvolution2d_nhwc_qs8_qc8w(
deconvolution_op2,
batch_size(), input_height(), input_width(),
adjustment_height(), adjustment_width(),
/*output_height_out=*/nullptr, /*output_width_out=*/nullptr,
/*threadpool=*/nullptr));
ASSERT_EQ(xnn_status_success,
xnn_setup_deconvolution2d_nhwc_qs8_qc8w(
deconvolution_op2,
input.data(), output2.data()));
ASSERT_EQ(xnn_status_success,
xnn_run_operator(deconvolution_op2, /*threadpool=*/nullptr));
VerifyWeightsCache(internal_weights_cache, old_weights_cache_size);
VerifyQC8(output2, output_ref);
}
}
}
void VerifyQC8(const std::vector<int8_t> &output,
const std::vector<double> &output_ref) const {
for (size_t i = 0; i < batch_size(); i++) {
for (size_t y = 0; y < output_height(); y++) {
for (size_t x = 0; x < output_width(); x++) {
for (size_t g = 0; g < groups(); g++) {
for (size_t c = 0; c < group_output_channels(); c++) {
EXPECT_LE(int32_t(output[((i * output_height() + y) * output_width() + x) * output_pixel_stride() + g * group_output_channels() + c]), int32_t(qmax() - 0x80))
<< "(x, y) = (" << x << ", " << y << "), group = " << g << ", channel = " << c;
EXPECT_GE(int32_t(output[((i * output_height() + y) * output_width() + x) * output_pixel_stride() + g * group_output_channels() + c]), int32_t(qmin() - 0x80))
<< "(x, y) = (" << x << ", " << y << "), group = " << g << ", channel = " << c;
EXPECT_NEAR(
output_ref[(((i * output_height() + y) * output_width() + x) * groups() + g) * group_output_channels() + c],
double(output[((i * output_height() + y) * output_width() + x) * output_pixel_stride() + g * group_output_channels() + c]),
0.9)
<< "(x, y) = (" << x << ", " << y << "), group = " << g << ", channel = " << c;
}
}
}
}
}
}
void VerifyWeightsCache(struct xnn_internal_weights_cache* weights_cache, size_t old_size) const {
ASSERT_EQ(weights_cache->cache.hits, 1);
// Ensure that we did not write more weights to the cache because it was a cache hit.
ASSERT_EQ(old_size, weights_cache->cache.weights.size);
};
void TestQU8() const {
ASSERT_EQ(weights_type(), WeightsType::Default);
xnnpack::ReplicableRandomDevice rng;
std::uniform_int_distribution<int32_t> i32dist(-10000, 10000);
std::uniform_int_distribution<int32_t> u8dist(
std::numeric_limits<uint8_t>::min(), std::numeric_limits<uint8_t>::max());
std::vector<uint8_t> input(XNN_EXTRA_BYTES / sizeof(uint8_t) +
(batch_size() * input_height() * input_width() - 1) * input_pixel_stride() + groups() * group_input_channels());
std::vector<uint8_t> kernel(groups() * group_output_channels() * kernel_height() * kernel_width() * group_input_channels());
std::vector<int32_t> bias(groups() * group_output_channels());
std::vector<uint8_t> output((batch_size() * output_height() * output_width() - 1) * output_pixel_stride() + groups() * group_output_channels());
std::vector<int32_t> accumulators(batch_size() * output_height() * output_width() * groups() * group_output_channels());
std::vector<double> output_ref(batch_size() * output_height() * output_width() * groups() * group_output_channels());
const uint8_t input_zero_point = 127;
const uint8_t kernel_zero_point = 127;
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return u8dist(rng); });
std::generate(kernel.begin(), kernel.end(), [&]() { return u8dist(rng); });
std::generate(bias.begin(), bias.end(), [&]() { return i32dist(rng); });
std::fill(output.begin(), output.end(), UINT8_C(0xA5));
// Compute reference results, without renormalization.
if (has_bias()) {
for (size_t i = 0; i < batch_size(); i++) {
for (size_t oy = 0; oy < output_height(); oy++) {
for (size_t ox = 0; ox < output_width(); ox++) {
for (size_t g = 0; g < groups(); g++) {
for (size_t oc = 0; oc < group_output_channels(); oc++) {
accumulators[(((i * output_height() + oy) * output_width() + ox) * groups() + g) * group_output_channels() + oc] =
bias[g * group_output_channels() + oc];
}
}
}
}
}
} else {
std::fill(accumulators.begin(), accumulators.end(), 0);
}
for (size_t i = 0; i < batch_size(); i++) {
for (size_t oy = 0; oy < output_height(); oy++) {
for (size_t ox = 0; ox < output_width(); ox++) {
for (size_t ky = 0; ky < kernel_height(); ky++) {
const size_t y = oy + padding_top() - ky * dilation_height();
const size_t iy = y / stride_height();
if (iy * stride_height() == y && iy < input_height()) {
for (size_t kx = 0; kx < kernel_width(); kx++) {
const size_t x = ox + padding_left() - kx * dilation_width();
const size_t ix = x / stride_width();
if (ix * stride_width() == x && ix < input_width()) {
for (size_t g = 0; g < groups(); g++) {
for (size_t oc = 0; oc < group_output_channels(); oc++) {
for (size_t ic = 0; ic < group_input_channels(); ic++) {
accumulators[(((i * output_height() + oy) * output_width() + ox) * groups() + g) * group_output_channels() + oc] +=
(int32_t(input[((i * input_height() + iy) * input_width() + ix) * input_pixel_stride() + g * group_input_channels() + ic]) - int32_t(input_zero_point)) *
(int32_t(kernel[(((g * group_output_channels() + oc) * kernel_height() + ky) * kernel_width() + kx) * group_input_channels() + ic]) - int32_t(kernel_zero_point));
}
}
}
}
}
}
}
}
}
}
// Compute renormalization parameters.
const int32_t accumulated_min = *std::min_element(accumulators.cbegin(), accumulators.cend());
const int32_t accumulated_max = *std::max_element(accumulators.cbegin(), accumulators.cend());
const double output_scale = double(uint32_t(accumulated_max - accumulated_min)) / 255.0;
const uint8_t output_zero_point = uint8_t(std::max(std::min(
lrint(127.5 - 0.5 * double(accumulated_min + accumulated_max) / output_scale),
long(std::numeric_limits<uint8_t>::max())), long(std::numeric_limits<uint8_t>::min())));
// Renormalize reference results.
std::transform(accumulators.cbegin(), accumulators.cend(), output_ref.begin(),
[this, output_scale, output_zero_point](int32_t x) -> double {
return std::max<double>(std::min<double>(double(x) / output_scale, double(qmax()) - output_zero_point), double(qmin()) - output_zero_point);
});
// Create, setup, run, and destroy Deconvolution operator.
ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */));
xnn_operator_t deconvolution_op = nullptr;
struct xnn_internal_weights_cache* internal_weights_cache = nullptr;
std::unique_ptr<xnn_weights_cache_provider, decltype(&xnn_delete_weights_cache)> auto_weights_cache(
nullptr, xnn_delete_weights_cache);
if (use_weights_cache()) {
xnn_weights_cache_t weights_cache = nullptr;
xnn_create_weights_cache(&weights_cache);
auto_weights_cache.reset(weights_cache);
if (weights_cache) {
internal_weights_cache = (struct xnn_internal_weights_cache*) weights_cache->context;
}
}
ASSERT_EQ(
xnn_status_success,
xnn_create_deconvolution2d_nhwc_qu8(
padding_top(), padding_right(), padding_bottom(), padding_left(),
kernel_height(), kernel_width(), stride_height(), stride_width(),
dilation_height(), dilation_width(), groups(),
group_input_channels(), group_output_channels(),
input_pixel_stride(), output_pixel_stride(), input_zero_point,
1.0f /* input scale */, kernel_zero_point,
1.0f /* kernel scale */, kernel.data(),
has_bias() ? bias.data() : nullptr, output_zero_point,
output_scale, qmin(), qmax(),
/*flags=*/0, /*code_cache=*/nullptr, auto_weights_cache.get(), &deconvolution_op));
if (use_weights_cache()) {
ASSERT_EQ(xnn_status_success,
xnn_finalize_weights_cache(auto_weights_cache.get(), xnn_weights_cache_finalization_kind_soft));
}
// Smart pointer to automatically delete deconvolution_op.
std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_deconvolution_op(deconvolution_op, xnn_delete_operator);
ASSERT_EQ(xnn_status_success,
xnn_reshape_deconvolution2d_nhwc_qu8(
deconvolution_op,
batch_size(), input_height(), input_width(),
adjustment_height(), adjustment_width(),
/*output_height_out=*/nullptr, /*output_width_out=*/nullptr,
/*threadpool=*/nullptr));
ASSERT_EQ(xnn_status_success,
xnn_setup_deconvolution2d_nhwc_qu8(
deconvolution_op,
input.data(), output.data()));
ASSERT_EQ(xnn_status_success,
xnn_run_operator(deconvolution_op, /*threadpool=*/nullptr));
// Verify results.
VerifyQU8(output, output_ref, output_zero_point);
if (use_weights_cache()) {
xnn_operator_t deconvolution_op2 = nullptr;
size_t old_weights_cache_size = internal_weights_cache->cache.weights.size;
ASSERT_EQ(
xnn_status_success,
xnn_create_deconvolution2d_nhwc_qu8(
padding_top(), padding_right(), padding_bottom(), padding_left(),
kernel_height(), kernel_width(), stride_height(), stride_width(),
dilation_height(), dilation_width(), groups(),
group_input_channels(), group_output_channels(),
input_pixel_stride(), output_pixel_stride(), input_zero_point,
1.0f /* input scale */, kernel_zero_point,
1.0f /* kernel scale */, kernel.data(),
has_bias() ? bias.data() : nullptr, output_zero_point,
output_scale, qmin(), qmax(),
/*flags=*/0, /*code_cache=*/nullptr, auto_weights_cache.get(), &deconvolution_op2));
// Smart pointer to automatically delete deconvolution_op2.
std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_deconvolution_op(deconvolution_op2, xnn_delete_operator);
ASSERT_EQ(xnn_status_success,
xnn_reshape_deconvolution2d_nhwc_qu8(
deconvolution_op2,
batch_size(), input_height(), input_width(),
adjustment_height(), adjustment_width(),
/*output_height_out=*/nullptr, /*output_width_out=*/nullptr,
/*threadpool=*/nullptr));
ASSERT_EQ(xnn_status_success,
xnn_setup_deconvolution2d_nhwc_qu8(
deconvolution_op2,
input.data(), output.data()));
ASSERT_EQ(xnn_status_success,
xnn_run_operator(deconvolution_op2, /*threadpool=*/nullptr));
VerifyWeightsCache(internal_weights_cache, old_weights_cache_size);
VerifyQU8(output, output_ref, output_zero_point);
}
}
}
void VerifyQU8(const std::vector<uint8_t> &output,
const std::vector<double> &output_ref,
uint8_t output_zero_point) const {
for (size_t i = 0; i < batch_size(); i++) {
for (size_t y = 0; y < output_height(); y++) {
for (size_t x = 0; x < output_width(); x++) {
for (size_t g = 0; g < groups(); g++) {
for (size_t c = 0; c < group_output_channels(); c++) {
EXPECT_LE(int32_t(output[((i * output_height() + y) * output_width() + x) * output_pixel_stride() + g * group_output_channels() + c]), int32_t(qmax()))
<< "(x, y) = (" << x << ", " << y << "), group = " << g << ", channel = " << c;
EXPECT_GE(int32_t(output[((i * output_height() + y) * output_width() + x) * output_pixel_stride() + g * group_output_channels() + c]), int32_t(qmin()))
<< "(x, y) = (" << x << ", " << y << "), group = " << g << ", channel = " << c;
EXPECT_NEAR(
output_ref[(((i * output_height() + y) * output_width() + x) * groups() + g) * group_output_channels() + c],
double(output[((i * output_height() + y) * output_width() + x) * output_pixel_stride() + g * group_output_channels() + c]) - double(output_zero_point),
0.9)
<< "(x, y) = (" << x << ", " << y << "), group = " << g << ", channel = " << c;
}
}
}
}
}
}
void TestF16() const {
switch (weights_type()) {
case WeightsType::Default:
break;
case WeightsType::FP32:
break;
default:
GTEST_FAIL() << "unexpected weights type";
}
xnnpack::ReplicableRandomDevice rng;
std::uniform_real_distribution<float> f32dist(0.1f, 1.0f);
std::vector<xnn_float16> input(XNN_EXTRA_BYTES / sizeof(xnn_float16) +
(batch_size() * input_height() * input_width() - 1) * input_pixel_stride() + groups() * group_input_channels());
std::vector<xnn_float16> kernel(groups() * group_output_channels() * kernel_height() * kernel_width() * group_input_channels());
std::vector<float> kernel_as_float(kernel.size());
std::vector<xnn_float16> bias(groups() * group_output_channels());
std::vector<float> bias_as_float(bias.size());
std::vector<xnn_float16> output((batch_size() * output_height() * output_width() - 1) * output_pixel_stride() + groups() * group_output_channels());
std::vector<float> output_ref(batch_size() * output_height() * output_width() * groups() * group_output_channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); });
std::generate(kernel.begin(), kernel.end(), [&]() { return f32dist(rng); });
std::copy(kernel.cbegin(), kernel.cend(), kernel_as_float.begin());
std::generate(bias.begin(), bias.end(), [&]() { return f32dist(rng); });
std::copy(bias.cbegin(), bias.cend(), bias_as_float.begin());
std::fill(output.begin(), output.end(), std::nanf(""));
// Compute reference results, without clamping.
if (has_bias()) {
for (size_t i = 0; i < batch_size(); i++) {
for (size_t oy = 0; oy < output_height(); oy++) {
for (size_t ox = 0; ox < output_width(); ox++) {
for (size_t g = 0; g < groups(); g++) {
for (size_t oc = 0; oc < group_output_channels(); oc++) {
output_ref[(((i * output_height() + oy) * output_width() + ox) * groups() + g) * group_output_channels() + oc] =
bias_as_float[g * group_output_channels() + oc];
}
}
}
}
}
} else {
std::fill(output_ref.begin(), output_ref.end(), 0.0f);
}
for (size_t i = 0; i < batch_size(); i++) {
for (size_t oy = 0; oy < output_height(); oy++) {
for (size_t ox = 0; ox < output_width(); ox++) {
for (size_t ky = 0; ky < kernel_height(); ky++) {
const size_t y = oy + padding_top() - ky * dilation_height();
const size_t iy = y / stride_height();
if (iy * stride_height() == y && iy < input_height()) {
for (size_t kx = 0; kx < kernel_width(); kx++) {
const size_t x = ox + padding_left() - kx * dilation_width();
const size_t ix = x / stride_width();
if (ix * stride_width() == x && ix < input_width()) {
for (size_t g = 0; g < groups(); g++) {
for (size_t oc = 0; oc < group_output_channels(); oc++) {
for (size_t ic = 0; ic < group_input_channels(); ic++) {
output_ref[(((i * output_height() + oy) * output_width() + ox) * groups() + g) * group_output_channels() + oc] +=
input[((i * input_height() + iy) * input_width() + ix) * input_pixel_stride() + g * group_input_channels() + ic] *
kernel_as_float[(((g * group_output_channels() + oc) * kernel_height() + ky) * kernel_width() + kx) * group_input_channels() + ic];
}
}
}
}
}
}
}
}
}
}
// Compute clamping parameters.
const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
const float accumulated_range = accumulated_max - accumulated_min;
float output_min = accumulated_min + accumulated_range / 255.0f * float(qmin());
float output_max = accumulated_max - accumulated_range / 255.0f * float(255 - qmax());
output_min = xnn_float16(output_min);
output_max = xnn_float16(output_max);
if (accumulated_range == 0.0f) {
output_min = -std::numeric_limits<float>::infinity();
output_max = +std::numeric_limits<float>::infinity();
}
if (qmin() == std::numeric_limits<uint8_t>::min()) {
output_min = -std::numeric_limits<float>::infinity();
}
if (qmax() == std::numeric_limits<uint8_t>::max()) {
output_max = +std::numeric_limits<float>::infinity();
}
// Clamp reference results.
for (float& value : output_ref) {
value = std::max(std::min(value, output_max), output_min);