-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathconvolution-nhwc.c
2217 lines (2033 loc) · 87.4 KB
/
convolution-nhwc.c
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.
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fp16.h>
#include <xnnpack.h>
#include <xnnpack/allocator.h>
#include <xnnpack/cache.h>
#include <xnnpack/common.h>
#include <xnnpack/compute.h>
#include <xnnpack/indirection.h>
#include <xnnpack/log.h>
#include <xnnpack/math.h>
#include <xnnpack/operator.h>
#include <xnnpack/operator-utils.h>
#include <xnnpack/pack.h>
#include <xnnpack/params.h>
#include <xnnpack/post-operation.h>
#include <xnnpack/microparams-init.h>
#ifndef XNN_ENABLE_GEMM_M_SPECIALIZATION
#error "XNN_ENABLE_GEMM_M_SPECIALIZATION is not defined"
#endif
static inline size_t compute_output_dimension_with_tf_same_padding(
size_t input_dimension,
size_t subsampling_dimension)
{
return divide_round_up(input_dimension, subsampling_dimension);
}
static inline const struct dwconv_parameters* find_dwconv_ukernel(
size_t kernel_size,
const struct dwconv_parameters* ukernel,
size_t num_ukernels)
{
const struct dwconv_parameters* best_ukernel = NULL;
while (num_ukernels-- != 0) {
// Find the smallest primary_tile that is at least as big as kernel_size.
if (ukernel->primary_tile >= kernel_size) {
if (best_ukernel == NULL || ukernel->primary_tile < best_ukernel->primary_tile) {
best_ukernel = ukernel;
}
}
ukernel++;
}
return best_ukernel;
}
#if XNN_PLATFORM_JIT
static inline uintptr_t cached_code_at_offset(xnn_operator_t op, size_t offset)
{
return (uintptr_t)op->code_cache->cache.code.start + offset;
}
static size_t get_generated_gemm(
struct xnn_hmp_gemm_codegen generators,
const struct jit_gemm_params *jit_gemm_params,
size_t mr,
size_t group_output_channels,
size_t nr,
size_t group_input_channels,
size_t log2_input_element_size,
struct xnn_code_cache* code_cache)
{
size_t offset = XNN_CACHE_NOT_FOUND;
xnn_jit_gemm_code_generator_fn generator = generators.function[XNN_UARCH_DEFAULT];
if (generator == NULL) {
goto error;
}
enum xnn_status status = xnn_status_success;
status = xnn_reserve_code_memory(&code_cache->cache.code, XNN_DEFAULT_MICROKERNEL_SIZE);
if (xnn_status_success != status) {
xnn_log_error("failed to ensure sufficient space in the code buffer for a microkernel");
goto error;
}
const size_t old_size = code_cache->cache.code.size;
void* old_code = (uint8_t*) code_cache->cache.code.start + old_size;
status = generator(&code_cache->cache.code, mr, group_output_channels % nr,
group_input_channels << log2_input_element_size,
jit_gemm_params);
if (xnn_status_success != status) {
xnn_log_error("failed to generate GEMM microkernel");
goto error;
}
const size_t new_size = code_cache->cache.code.size;
return xnn_get_or_insert_code_cache(code_cache, old_code, new_size - old_size);
error:
return offset;
}
static void generate_gemms_up_to_max_mr(
size_t max_mr,
struct gemm_codegens generators,
const struct jit_gemm_params *jit_gemm_params,
size_t group_output_channels,
size_t nr,
size_t group_input_channels,
size_t log2_input_element_size,
xnn_operator_t convolution_op)
{
assert(XNN_MAX_MR >= max_mr);
if (convolution_op->code_cache == NULL) {
return;
}
for (size_t mr = 1; mr <= max_mr; mr++) {
// Get smallest generator that is >= mr.
size_t smallest_mr = mr;
while (generators.gemm[smallest_mr - 1].function[XNN_UARCH_DEFAULT] == NULL && smallest_mr <= max_mr) {
smallest_mr++;
}
xnn_log_debug("using generator for mr %zu to generate gemm of mr %zu", smallest_mr, mr);
convolution_op->ukernel.gemm.gemm_cases[mr - 1].generated_code_offset[XNN_UARCH_DEFAULT] =
get_generated_gemm(generators.gemm[smallest_mr - 1], jit_gemm_params, mr, group_output_channels, nr,
group_input_channels, log2_input_element_size, convolution_op->code_cache);
}
}
static size_t get_generated_igemm(
struct xnn_hmp_igemm_codegen generators,
const struct jit_gemm_params *jit_gemm_params,
size_t group_output_channels,
size_t nr,
size_t group_input_channels,
size_t log2_input_element_size,
size_t kernel_size,
size_t mr,
struct xnn_code_cache* code_cache)
{
size_t offset = XNN_CACHE_NOT_FOUND;
xnn_jit_igemm_code_generator_fn generator = generators.function[XNN_UARCH_DEFAULT];
if (generator == NULL) {
goto error;
}
enum xnn_status status = xnn_status_success;
status = xnn_reserve_code_memory(&code_cache->cache.code, XNN_DEFAULT_MICROKERNEL_SIZE);
if (xnn_status_success != status) {
xnn_log_error("failed to ensure sufficient space in code buffer for microkernel");
goto error;
}
const size_t old_size = code_cache->cache.code.size;
void* old_code = (uint8_t*) code_cache->cache.code.start + old_size;
status = generator(&code_cache->cache.code, mr, group_output_channels % nr,
group_input_channels << log2_input_element_size,
kernel_size * mr * sizeof(void*), jit_gemm_params);
if (status != xnn_status_success) {
xnn_log_error("failed to generate IGEMM microkernel");
goto error;
}
const size_t new_size = code_cache->cache.code.size;
return xnn_get_or_insert_code_cache(code_cache, old_code, new_size - old_size);
error:
return offset;
}
static void generate_igemms_up_to_max_mr(
size_t max_mr,
struct gemm_codegens generators,
const struct jit_gemm_params *jit_gemm_params,
size_t group_output_channels,
size_t nr,
size_t group_input_channels,
size_t log2_input_element_size,
size_t kernel_size,
xnn_operator_t convolution_op)
{
assert(XNN_MAX_MR >= max_mr);
if (convolution_op->code_cache == NULL) {
return;
}
for (size_t mr = 1; mr <= max_mr; mr++) {
// Get smallest generator that is >= mr.
size_t smallest_mr = mr;
while (generators.igemm[smallest_mr - 1].function[XNN_UARCH_DEFAULT] == NULL && smallest_mr <= max_mr) {
smallest_mr++;
}
xnn_log_debug("using generator for mr %zu to generate igemm of mr %zu", smallest_mr, mr);
convolution_op->ukernel.igemm.igemm_cases[mr - 1].generated_code_offset[XNN_UARCH_DEFAULT] =
get_generated_igemm(generators.igemm[smallest_mr - 1], jit_gemm_params, group_output_channels, nr,
group_input_channels, log2_input_element_size, kernel_size, mr,
convolution_op->code_cache);
}
}
#endif // XNN_PLATFORM_JIT
static enum xnn_status create_vmulcaddc_path(
uint32_t groups,
const void* kernel,
const void* bias,
uint32_t log2_filter_element_size,
uint32_t bias_element_size,
xnn_pack_vmulcaddc_w_fn pack_vmulcaddc_w,
const void* packing_params,
int packed_weights_padding_byte,
const void* vmulcaddc_params,
size_t vmulcaddc_params_size,
const struct vmulcaddc_parameters* vmulcaddc_parameters,
enum xnn_operator_type operator_type,
xnn_operator_t convolution_op)
{
assert(vmulcaddc_parameters != NULL);
assert(vmulcaddc_params != NULL);
enum xnn_status status = xnn_status_out_of_memory;
const size_t c_stride = round_up_po2(groups, vmulcaddc_parameters->channel_tile);
const size_t packed_weights_size = ((UINT32_C(1) << log2_filter_element_size) + bias_element_size) * c_stride;
size_t aligned_total_weights_size = round_up_po2(packed_weights_size, XNN_ALLOCATION_ALIGNMENT);
void* weights_ptr = xnn_get_pointer_to_write_weights(
convolution_op, aligned_total_weights_size, packed_weights_padding_byte);
if (weights_ptr == NULL) {
xnn_log_error("failed to reserve or allocated %zu bytes for %s operator vmulcaddc packed weights",
aligned_total_weights_size, xnn_operator_type_to_string(operator_type));
goto error;
}
xnn_log_debug("allocated %zu bytes for packed weights in %s operator",
aligned_total_weights_size, xnn_operator_type_to_string(operator_type));
pack_vmulcaddc_w(groups, vmulcaddc_parameters->channel_tile, kernel, bias, weights_ptr, packing_params);
if (use_weights_cache(convolution_op)) {
convolution_op->packed_weights.offset = xnn_get_or_insert_weights_cache(
convolution_op->weights_cache, weights_ptr, aligned_total_weights_size);
}
memcpy(&convolution_op->params, vmulcaddc_params, vmulcaddc_params_size);
convolution_op->ukernel.vmulcaddc = (struct xnn_ukernel_vmulcaddc) {
.function = vmulcaddc_parameters->ukernel,
.mr = vmulcaddc_parameters->row_tile,
};
return xnn_status_success;
error:
return status;
}
static enum xnn_status create_dwconv_path(
uint32_t kernel_height,
uint32_t kernel_width,
uint32_t groups,
const void* kernel,
const void* bias,
uint32_t flags,
uint32_t log2_input_element_size,
uint32_t log2_filter_element_size,
uint32_t bias_element_size,
xnn_pack_dwconv_hwg_w_fn pack_dwconv_hwg_w,
xnn_pack_dwconv_ghw_w_fn pack_dwconv_ghw_w,
const void* packing_params,
int packed_weights_padding_byte,
size_t extra_weights_bytes,
xnn_init_qc8_scale_params_fn init_scale_params,
const float* scale_params,
const void* dwconv_params,
size_t dwconv_params_size,
const struct dwconv_parameters* dwconv_ukernel,
bool linear_activation,
enum xnn_operator_type operator_type,
size_t* zero_size,
xnn_operator_t convolution_op)
{
assert(dwconv_ukernel != NULL);
enum xnn_status status = xnn_status_out_of_memory;
const uint8_t primary_tile = dwconv_ukernel->primary_tile;
assert(primary_tile >= kernel_height * kernel_width);
const size_t c_stride = round_up_po2(groups, dwconv_ukernel->channel_tile);
const size_t packed_weights_size =
((primary_tile << log2_filter_element_size) + bias_element_size + extra_weights_bytes) * c_stride;
size_t aligned_total_weights_size = round_up_po2(packed_weights_size, XNN_ALLOCATION_ALIGNMENT);
void* weights_ptr = xnn_get_pointer_to_write_weights(
convolution_op, aligned_total_weights_size, packed_weights_padding_byte);
if (weights_ptr == NULL) {
xnn_log_error("failed to reserve or allocated %zu bytes for %s operator dwconv packed weights",
aligned_total_weights_size, xnn_operator_type_to_string(operator_type));
goto error;
}
xnn_log_debug("allocated %zu bytes for packed weights in %s operator",
aligned_total_weights_size, xnn_operator_type_to_string(operator_type));
memcpy(&convolution_op->params, dwconv_params, dwconv_params_size);
if (flags & XNN_FLAG_DEPTHWISE_CONVOLUTION) {
pack_dwconv_hwg_w(
dwconv_ukernel->primary_tile,
kernel_height, kernel_width,
groups, dwconv_ukernel->channel_tile,
kernel, bias, weights_ptr,
dwconv_ukernel->channel_tile * extra_weights_bytes,
packing_params);
} else {
pack_dwconv_ghw_w(
dwconv_ukernel->primary_tile,
kernel_height, kernel_width,
groups, dwconv_ukernel->channel_tile,
kernel, bias, weights_ptr,
dwconv_ukernel->channel_tile * extra_weights_bytes,
packing_params);
}
if (scale_params != NULL) {
assert(init_scale_params != NULL);
init_scale_params(
/*channels=*/groups,
/*channels_tile=*/dwconv_ukernel->channel_tile,
/*stride=*/dwconv_ukernel->channel_tile *
((primary_tile << log2_filter_element_size) + bias_element_size + extra_weights_bytes),
/*scale=*/scale_params,
/*packed_w=*/
(void*)((uintptr_t)weights_ptr +
dwconv_ukernel->channel_tile * ((primary_tile << log2_filter_element_size) + bias_element_size)));
}
if (use_weights_cache(convolution_op)) {
convolution_op->packed_weights.offset = xnn_get_or_insert_weights_cache(
convolution_op->weights_cache, weights_ptr, aligned_total_weights_size);
}
const union dwconv_fused_ukernels* ukernels = &dwconv_ukernel->minmax;
if (linear_activation && dwconv_ukernel->linear.unipass != NULL) {
ukernels = &dwconv_ukernel->linear;
}
convolution_op->ukernel.dwconv = (struct xnn_ukernel_dwconv) {
.unipass_fn = ukernels->unipass,
.primary_tile = dwconv_ukernel->primary_tile,
.middle_tile = dwconv_ukernel->middle_tile,
.last_tile = dwconv_ukernel->last_tile,
};
*zero_size = XNN_EXTRA_BYTES + (c_stride << log2_input_element_size);
return xnn_status_success;
error:
return status;
}
static enum xnn_status create_gemm_or_igemm(
enum xnn_microkernel_type ukernel_type,
uint32_t kernel_size,
uint32_t groups,
size_t group_input_channels,
size_t group_output_channels,
const void* kernel,
const void* bias,
uint32_t flags,
uint32_t log2_input_element_size,
uint32_t log2_filter_element_size,
uint32_t bias_element_size,
xnn_pack_gemm_goi_w_fn pack_gemm_goi_w,
xnn_pack_conv_kgo_w_fn pack_conv_kgo_w,
xnn_pack_conv_goki_w_fn pack_conv_goki_w,
const void* packing_params,
int packed_weights_padding_byte,
size_t extra_weights_bytes,
xnn_init_qc8_scale_params_fn init_scale_params,
const float* scale_params,
const void* gemm_params,
size_t gemm_params_size,
const struct gemm_parameters* gemm_parameters,
const struct jit_gemm_params* jit_gemm_params,
bool linear_activation,
bool relu_activation,
enum xnn_operator_type operator_type,
size_t num_post_operations,
void* post_operation_params,
xnn_operator_t convolution_op,
size_t* zero_size)
{
enum xnn_status status = xnn_status_out_of_memory;
const uint32_t nr = gemm_parameters->nr;
const uint32_t kr = UINT32_C(1) << gemm_parameters->log2_kr;
const uint32_t sr = UINT32_C(1) << gemm_parameters->log2_sr;
const size_t n_stride = round_up(group_output_channels, nr);
const size_t k_stride = round_up_po2(group_input_channels, kr * sr);
const size_t packed_group_weights_size =
((kernel_size * k_stride << log2_filter_element_size) + bias_element_size + extra_weights_bytes) * n_stride;
const size_t aligned_total_weights_size = round_up_po2(packed_group_weights_size * groups, XNN_ALLOCATION_ALIGNMENT);
void* weights_ptr = xnn_get_pointer_to_write_weights(
convolution_op, aligned_total_weights_size, packed_weights_padding_byte);
if (weights_ptr == NULL) {
xnn_log_error("failed to reserve or allocated %zu bytes for %s operator gemm packed weights",
aligned_total_weights_size, xnn_operator_type_to_string(operator_type));
goto error;
}
xnn_log_debug("allocated %zu bytes for packed weights in %s operator",
aligned_total_weights_size, xnn_operator_type_to_string(operator_type));
memcpy(&convolution_op->params, gemm_params, gemm_params_size);
convolution_op->num_post_operation_params = num_post_operations;
convolution_op->post_operation_params = post_operation_params;
const struct gemm_fused_ukernels* gemm_ukernels = &gemm_parameters->minmax;
const uint32_t mr = gemm_parameters->mr;
if (linear_activation && gemm_parameters->linear.gemm[mr - 1].function[XNN_UARCH_DEFAULT] != NULL) {
gemm_ukernels = &gemm_parameters->linear;
} else if (relu_activation && gemm_parameters->relu.gemm[mr - 1].function[XNN_UARCH_DEFAULT] != NULL) {
gemm_ukernels = &gemm_parameters->relu;
}
switch (ukernel_type) {
case xnn_microkernel_type_gemm:
pack_gemm_goi_w(
groups, group_output_channels, group_input_channels,
nr, kr, sr,
kernel, bias, weights_ptr, gemm_parameters->nr * extra_weights_bytes, packing_params);
convolution_op->ukernel.gemm = (struct xnn_ukernel_gemm) {
.mr = mr,
.nr = nr,
.kr = kr,
.sr = sr,
};
assert(XNN_MAX_MR >= mr);
for (size_t i = 0; i < mr; i++) {
convolution_op->ukernel.gemm.gemm_cases[i] = gemm_ukernels->gemm[i];
}
#if XNN_PLATFORM_JIT
generate_gemms_up_to_max_mr(
mr, gemm_parameters->generator, jit_gemm_params, group_output_channels, nr,
group_input_channels, log2_input_element_size, convolution_op);
#endif // XNN_PLATFORM_JIT
break;
case xnn_microkernel_type_igemm:
if (flags & XNN_FLAG_DEPTHWISE_CONVOLUTION) {
pack_conv_kgo_w(
groups, group_output_channels, kernel_size,
nr, kr, sr,
kernel, bias, weights_ptr, gemm_parameters->nr * extra_weights_bytes, packing_params);
} else {
pack_conv_goki_w(
groups, group_output_channels, kernel_size, group_input_channels,
nr, kr, sr,
kernel, bias, weights_ptr, gemm_parameters->nr * extra_weights_bytes, packing_params);
}
convolution_op->ukernel.igemm = (struct xnn_ukernel_igemm) {
.mr = mr,
.nr = nr,
.kr = kr,
.sr = sr,
};
assert(XNN_MAX_MR >= mr);
for (size_t i = 0; i < mr; i++) {
convolution_op->ukernel.igemm.igemm_cases[i] = gemm_ukernels->igemm[i];
}
#if XNN_PLATFORM_JIT
generate_igemms_up_to_max_mr(
mr, gemm_parameters->generator, jit_gemm_params, group_output_channels, nr,
group_input_channels, log2_input_element_size, kernel_size, convolution_op);
#endif // XNN_PLATFORM_JIT
break;
default:
XNN_UNREACHABLE;
}
if (scale_params != NULL) {
assert(init_scale_params != NULL);
void* group_weights =
(void*)((uintptr_t)weights_ptr +
gemm_parameters->nr * ((kernel_size * k_stride << log2_filter_element_size) + bias_element_size));
const size_t weights_stride =
(kernel_size * k_stride << log2_filter_element_size) + bias_element_size + extra_weights_bytes;
for (uint32_t group = 0; group < groups; group++) {
init_scale_params(
group_output_channels, gemm_parameters->nr,
gemm_parameters->nr * weights_stride,
scale_params, group_weights);
scale_params += group_output_channels;
group_weights = (void*) ((uintptr_t) group_weights + n_stride * weights_stride);
}
}
if (use_weights_cache(convolution_op)) {
convolution_op->packed_weights.offset = xnn_get_or_insert_weights_cache(
convolution_op->weights_cache, weights_ptr, aligned_total_weights_size);
}
*zero_size = XNN_EXTRA_BYTES + (k_stride << log2_input_element_size);
return xnn_status_success;
error:
return status;
}
static enum xnn_status create_convolution2d_nhwc(
uint32_t input_padding_top,
uint32_t input_padding_right,
uint32_t input_padding_bottom,
uint32_t input_padding_left,
uint32_t kernel_height,
uint32_t kernel_width,
uint32_t subsampling_height,
uint32_t subsampling_width,
uint32_t dilation_height,
uint32_t dilation_width,
uint32_t groups,
size_t group_input_channels,
size_t group_output_channels,
size_t input_channel_stride,
size_t output_channel_stride,
const void* kernel,
const void* bias,
uint32_t flags,
uint32_t log2_input_element_size,
uint32_t log2_filter_element_size,
uint32_t bias_element_size,
xnn_pack_vmulcaddc_w_fn pack_vmulcaddc_w,
xnn_pack_dwconv_hwg_w_fn pack_dwconv_hwg_w,
xnn_pack_dwconv_ghw_w_fn pack_dwconv_ghw_w,
xnn_pack_gemm_goi_w_fn pack_gemm_goi_w,
xnn_pack_conv_kgo_w_fn pack_conv_kgo_w,
xnn_pack_conv_goki_w_fn pack_conv_goki_w,
const void* packing_params,
int input_padding_byte,
int packed_weights_padding_byte,
size_t extra_weights_bytes,
xnn_init_qc8_scale_params_fn init_scale_params,
const float* scale_params,
const void* gemm_params,
size_t gemm_params_size,
const void* dwconv_params,
size_t dwconv_params_size,
const void* vmulcaddc_params,
size_t vmulcaddc_params_size,
const struct gemm_parameters* gemm_parameters,
const struct dwconv_parameters* dwconv_ukernel,
const struct vmulcaddc_parameters* vmulcaddc_parameters,
struct jit_gemm_params* jit_gemm_params,
bool linear_activation,
bool relu_activation,
uint32_t datatype_init_flags,
enum xnn_operator_type operator_type,
size_t num_post_operations,
void* post_operation_params,
xnn_caches_t caches,
xnn_operator_t* convolution_op_out)
{
xnn_operator_t convolution_op = NULL;
enum xnn_status status = xnn_status_uninitialized;
if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
xnn_log_error(
"failed to create %s operator: XNNPACK is not initialized",
xnn_operator_type_to_string(operator_type));
goto error;
}
status = xnn_status_unsupported_hardware;
if ((xnn_params.init_flags & datatype_init_flags) != datatype_init_flags) {
xnn_log_error(
"failed to create %s operator: operations on data type are not supported",
xnn_operator_type_to_string(operator_type));
goto error;
}
status = xnn_status_invalid_parameter;
if (kernel_width == 0 || kernel_height == 0) {
xnn_log_error(
"failed to create %s operator with %" PRIu32 "x%" PRIu32 " kernel: kernel dimensions must be non-zero",
xnn_operator_type_to_string(operator_type), kernel_width, kernel_height);
goto error;
}
if (subsampling_width == 0 || subsampling_height == 0) {
xnn_log_error(
"failed to create %s operator with %" PRIu32 "x%" PRIu32 " subsampling: subsampling dimensions must be non-zero",
xnn_operator_type_to_string(operator_type), subsampling_width, subsampling_height);
goto error;
}
if (dilation_width == 0 || dilation_height == 0) {
xnn_log_error(
"failed to create %s operator with %" PRIu32 "x%" PRIu32 " dilation: dilation dimensions must be non-zero",
xnn_operator_type_to_string(operator_type), dilation_width, dilation_height);
goto error;
}
if (groups == 0) {
xnn_log_error(
"failed to create %s operator with %" PRIu32 " groups: number of groups must be non-zero",
xnn_operator_type_to_string(operator_type), groups);
goto error;
}
if (group_input_channels == 0) {
xnn_log_error(
"failed to create %s operator with %zu input channels per group: number of channels must be non-zero",
xnn_operator_type_to_string(operator_type), group_input_channels);
goto error;
}
if (group_output_channels == 0) {
xnn_log_error(
"failed to create %s operator with %zu output channels per group: number of channels must be non-zero",
xnn_operator_type_to_string(operator_type), group_output_channels);
goto error;
}
const size_t input_channels = groups * group_input_channels;
if (input_channel_stride < input_channels) {
xnn_log_error(
"failed to create %s operator with input channel stride of %zu: "
"stride must be at least as large as the number of input channels (%" PRIu32 "x%zu)",
xnn_operator_type_to_string(operator_type),
input_channel_stride, groups, group_input_channels);
goto error;
}
const size_t output_channels = groups * group_output_channels;
if (output_channel_stride < output_channels) {
xnn_log_error(
"failed to create %s operator with output channel stride of %zu: "
"stride must be at least as large as the number of output channels (%" PRIu32 "x%zu)",
xnn_operator_type_to_string(operator_type),
output_channel_stride, groups, group_output_channels);
goto error;
}
if ((flags & XNN_FLAG_DEPTHWISE_CONVOLUTION) != 0 && group_input_channels != 1) {
xnn_log_error(
"failed to create depthwise %s operator with %zu input channels per group: "
"depthwise convolution must have exactly 1 input channel per group",
xnn_operator_type_to_string(operator_type), group_input_channels);
goto error;
}
const bool any_padding = (input_padding_left | input_padding_top | input_padding_right | input_padding_bottom) != 0;
if ((flags & XNN_FLAG_TENSORFLOW_SAME_PADDING) != 0) {
if (any_padding) {
xnn_log_error(
"failed to create %s operator with %" PRIu32 "+%" PRIu32 "x%" PRIu32 "+%" PRIu32" padding: "
"TensorFlow SAME padding can't be combined with explicit padding specification",
xnn_operator_type_to_string(operator_type),
input_padding_top, input_padding_left, input_padding_bottom, input_padding_right);
goto error;
}
}
status = xnn_status_out_of_memory;
convolution_op = xnn_allocate_zero_simd_memory(sizeof(struct xnn_operator));
if (convolution_op == NULL) {
xnn_log_error(
"failed to allocate %zu bytes for %s operator descriptor",
sizeof(struct xnn_operator), xnn_operator_type_to_string(operator_type));
goto error;
}
if (caches != NULL) {
convolution_op->weights_cache = caches->weights_cache;
convolution_op->code_cache = caches->code_cache;
}
const size_t kernel_size = kernel_height * kernel_width;
enum xnn_microkernel_type ukernel_type = xnn_microkernel_type_default;
const bool unit_subsampling = (subsampling_width | subsampling_height) == 1;
if (group_input_channels == 1 && group_output_channels == 1 && kernel_size == 1 && unit_subsampling && !any_padding && vmulcaddc_parameters != NULL) {
ukernel_type = xnn_microkernel_type_vmulcaddc;
} else if (group_input_channels == 1 && group_output_channels == 1 && dwconv_ukernel != NULL)
{
ukernel_type = xnn_microkernel_type_dwconv;
} else if (kernel_size == 1 && unit_subsampling && !any_padding) {
ukernel_type = xnn_microkernel_type_gemm;
} else {
ukernel_type = xnn_microkernel_type_igemm;
}
assert(ukernel_type != xnn_microkernel_type_default);
if (num_post_operations != 0 && (ukernel_type != xnn_microkernel_type_gemm && ukernel_type != xnn_microkernel_type_igemm)) {
xnn_log_error(
"convolution with post operations not support for these parameters: "
"kernel_size: %zu unit_subsampling: %d padding: %d, ukernel_type: %d",
kernel_size, unit_subsampling, any_padding, ukernel_type);
goto error;
}
size_t zero_size = 0;
switch (ukernel_type) {
case xnn_microkernel_type_vmulcaddc:
{
status = create_vmulcaddc_path(
groups, kernel, bias, log2_filter_element_size, bias_element_size,
pack_vmulcaddc_w, packing_params, packed_weights_padding_byte,
vmulcaddc_params, vmulcaddc_params_size, vmulcaddc_parameters,
operator_type, convolution_op);
if (status != xnn_status_success) {
goto error;
}
break;
}
case xnn_microkernel_type_dwconv:
{
status = create_dwconv_path(
kernel_height, kernel_width,
groups, kernel, bias, flags,
log2_input_element_size, log2_filter_element_size, bias_element_size,
pack_dwconv_hwg_w, pack_dwconv_ghw_w, packing_params, packed_weights_padding_byte, extra_weights_bytes,
init_scale_params, scale_params,
dwconv_params, dwconv_params_size, dwconv_ukernel,
linear_activation, operator_type, &zero_size, convolution_op);
if (status != xnn_status_success) {
goto error;
}
break;
}
case xnn_microkernel_type_gemm:
case xnn_microkernel_type_igemm:
{
status = create_gemm_or_igemm(
ukernel_type, kernel_size,
groups, group_input_channels, group_output_channels,
kernel, bias, flags,
log2_input_element_size, log2_filter_element_size, bias_element_size,
pack_gemm_goi_w, pack_conv_kgo_w, pack_conv_goki_w, packing_params,
packed_weights_padding_byte, extra_weights_bytes,
init_scale_params, scale_params,
gemm_params, gemm_params_size, gemm_parameters, jit_gemm_params,
linear_activation, relu_activation,
operator_type,
num_post_operations, post_operation_params,
convolution_op,
&zero_size);
if (status != xnn_status_success) {
goto error;
}
break;
}
default:
XNN_UNREACHABLE;
}
const bool tf_same_padding = (flags & XNN_FLAG_TENSORFLOW_SAME_PADDING) != 0 && kernel_size != 1;
if (any_padding || tf_same_padding) {
convolution_op->zero_buffer = xnn_allocate_simd_memory(zero_size);
if (convolution_op->zero_buffer == NULL) {
xnn_log_error(
"failed to allocate %zu bytes for %s operator zero padding",
zero_size, xnn_operator_type_to_string(operator_type));
goto error;
}
memset(convolution_op->zero_buffer, input_padding_byte, zero_size);
}
convolution_op->padding_top = input_padding_top;
convolution_op->padding_right = input_padding_right;
convolution_op->padding_bottom = input_padding_bottom;
convolution_op->padding_left = input_padding_left;
convolution_op->kernel_height = kernel_height;
convolution_op->kernel_width = kernel_width;
convolution_op->stride_height = subsampling_height;
convolution_op->stride_width = subsampling_width;
convolution_op->dilation_height = dilation_height;
convolution_op->dilation_width = dilation_width;
convolution_op->groups = groups;
convolution_op->group_input_channels = group_input_channels;
convolution_op->group_output_channels = group_output_channels;
convolution_op->input_pixel_stride = input_channel_stride;
convolution_op->output_pixel_stride = output_channel_stride;
convolution_op->type = operator_type;
convolution_op->ukernel.type = ukernel_type;
convolution_op->flags = flags & ~XNN_FLAG_TENSORFLOW_SAME_PADDING;
if (tf_same_padding) {
convolution_op->flags |= XNN_FLAG_TENSORFLOW_SAME_PADDING;
}
convolution_op->state = xnn_run_state_invalid;
*convolution_op_out = convolution_op;
return xnn_status_success;
error:
xnn_delete_operator(convolution_op);
return status;
}
enum xnn_status xnn_create_convolution2d_nhwc_qu8(
uint32_t input_padding_top,
uint32_t input_padding_right,
uint32_t input_padding_bottom,
uint32_t input_padding_left,
uint32_t kernel_height,
uint32_t kernel_width,
uint32_t subsampling_height,
uint32_t subsampling_width,
uint32_t dilation_height,
uint32_t dilation_width,
uint32_t groups,
size_t group_input_channels,
size_t group_output_channels,
size_t input_channel_stride,
size_t output_channel_stride,
uint8_t input_zero_point,
float input_scale,
uint8_t kernel_zero_point,
float kernel_scale,
const uint8_t* kernel,
const int32_t* bias,
uint8_t output_zero_point,
float output_scale,
uint8_t output_min,
uint8_t output_max,
uint32_t flags,
xnn_caches_t caches,
xnn_operator_t* convolution_op_out)
{
if (input_scale <= 0.0f || !isnormal(input_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g input scale: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convolution_nhwc_qu8), input_scale);
return xnn_status_invalid_parameter;
}
if (kernel_scale <= 0.0f || !isnormal(kernel_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g kernel scale: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convolution_nhwc_qu8), kernel_scale);
return xnn_status_invalid_parameter;
}
if (output_scale <= 0.0f || !isnormal(output_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g output scale: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convolution_nhwc_qu8), output_scale);
return xnn_status_invalid_parameter;
}
if (output_min >= output_max) {
xnn_log_error(
"failed to create %s operator with [%" PRIu8 ", %" PRIu8 "] output range: range min must be below range max",
xnn_operator_type_to_string(xnn_operator_type_convolution_nhwc_qu8), output_min, output_max);
return xnn_status_invalid_parameter;
}
const float requantization_scale = input_scale * kernel_scale / output_scale;
if (requantization_scale >= 256.0f) {
xnn_log_error(
"failed to create %s operator with %.7g input scale, %.7g kernel scale, and %.7g output scale: "
"requantization scale %.7g is greater or equal to 256.0",
xnn_operator_type_to_string(xnn_operator_type_convolution_nhwc_qu8),
input_scale, kernel_scale, output_scale, requantization_scale);
return xnn_status_unsupported_parameter;
}
const struct xnn_qu8_packing_params packing_params = {
.input_zero_point = input_zero_point,
.kernel_zero_point = kernel_zero_point,
};
union xnn_qu8_conv_minmax_params gemm_params;
if XNN_LIKELY(xnn_params.qu8.gemm.init.qu8 != NULL) {
xnn_params.qu8.gemm.init.qu8(&gemm_params,
kernel_zero_point, requantization_scale, output_zero_point, output_min, output_max);
}
union xnn_qu8_conv_minmax_params dwconv_params;
const struct dwconv_parameters* dwconv_ukernel =
find_dwconv_ukernel(kernel_height * kernel_width, xnn_params.qu8.dwconv, XNN_MAX_QU8_DWCONV_UKERNELS);
if XNN_LIKELY(dwconv_ukernel != NULL) {
dwconv_ukernel->init.qu8(&dwconv_params,
kernel_zero_point, requantization_scale, output_zero_point, output_min, output_max);
}
return create_convolution2d_nhwc(
input_padding_top, input_padding_right, input_padding_bottom, input_padding_left,
kernel_height, kernel_width,
subsampling_height, subsampling_width,
dilation_height, dilation_width,
groups, group_input_channels, group_output_channels,
input_channel_stride, output_channel_stride,
kernel, bias, flags,
/*log2_input_element_size=*/0, // log2(sizeof(uint8_t))
/*log2_filter_element_size=*/0, // log2(sizeof(uint8_t))
/*bias_element_size=*/sizeof(int32_t),
(xnn_pack_vmulcaddc_w_fn) NULL,
(xnn_pack_dwconv_hwg_w_fn) xnn_pack_qu8_dwconv_hwg_w,
(xnn_pack_dwconv_ghw_w_fn) xnn_pack_qu8_dwconv_ghw_w,
(xnn_pack_gemm_goi_w_fn) xnn_pack_qu8_gemm_goi_w,
(xnn_pack_conv_kgo_w_fn) xnn_pack_qu8_conv_kgo_w,
(xnn_pack_conv_goki_w_fn) xnn_pack_qu8_conv_goki_w,
/*packing_params=*/&packing_params,
/*input_padding_byte=*/input_zero_point,
/*packed_weights_padding_byte=*/kernel_zero_point,
/*extra_weights_bytes=*/0,
/*init_scale_params=*/NULL,
/*scale_params=*/NULL,
/*gemm_params=*/&gemm_params,
/*gemm_params_size=*/sizeof(gemm_params),
/*dwconv_params=*/&dwconv_params,
/*dwconv_params_size=*/sizeof(dwconv_params),
/*vmulcaddc_params=*/NULL,
/*vmulcaddc_params_size=*/0,
/*gemm_parameters=*/&xnn_params.qu8.gemm,
/*dwconv_ukernel=*/dwconv_ukernel,
/*vmulcaddc_parameters=*/NULL,
/*jit_gemm_params=*/NULL,
/*linear_activation=*/false,
/*relu_activation=*/false,
/*datatype_init_flags=*/XNN_INIT_FLAG_QU8,
/*operator_type=*/xnn_operator_type_convolution_nhwc_qu8,
/*num_post_operations=*/0,
/*post_operation_params=*/NULL,
/*caches=*/caches,
convolution_op_out);
}
enum xnn_status xnn_create_convolution2d_nhwc_qs8(
uint32_t input_padding_top,
uint32_t input_padding_right,
uint32_t input_padding_bottom,
uint32_t input_padding_left,
uint32_t kernel_height,
uint32_t kernel_width,
uint32_t subsampling_height,
uint32_t subsampling_width,
uint32_t dilation_height,
uint32_t dilation_width,
uint32_t groups,
size_t group_input_channels,
size_t group_output_channels,
size_t input_channel_stride,
size_t output_channel_stride,
int8_t input_zero_point,
float input_scale,
float kernel_scale,
const int8_t* kernel,
const int32_t* bias,
int8_t output_zero_point,
float output_scale,
int8_t output_min,
int8_t output_max,
uint32_t flags,
xnn_caches_t caches,
xnn_operator_t* convolution_op_out)
{
if (input_scale <= 0.0f || !isnormal(input_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g input scale: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convolution_nhwc_qs8), input_scale);
return xnn_status_invalid_parameter;
}
if (kernel_scale <= 0.0f || !isnormal(kernel_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g kernel scale: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convolution_nhwc_qs8), kernel_scale);
return xnn_status_invalid_parameter;
}
if (output_scale <= 0.0f || !isnormal(output_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g output scale: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convolution_nhwc_qs8), output_scale);
return xnn_status_invalid_parameter;
}
if (output_min >= output_max) {
xnn_log_error(
"failed to create %s operator with [%" PRId8 ", %" PRId8 "] output range: range min must be below range max",
xnn_operator_type_to_string(xnn_operator_type_convolution_nhwc_qs8), output_min, output_max);
return xnn_status_invalid_parameter;
}
const float requantization_scale = input_scale * kernel_scale / output_scale;
if (requantization_scale >= 256.0f) {
xnn_log_error(