-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathunary-elementwise-nc.c
2665 lines (2467 loc) · 80.4 KB
/
unary-elementwise-nc.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 2020 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 <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <fp16.h>
#include <xnnpack.h>
#include <xnnpack/allocator.h>
#include <xnnpack/log.h>
#include <xnnpack/operator.h>
#include <xnnpack/microparams-init.h>
#include <xnnpack/params.h>
static void init_unary_elementwise_nc(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
const void* params,
size_t params_size,
enum xnn_operator_type operator_type,
xnn_vunary_ukernel_fn ukernel,
xnn_operator_t unary_elementwise_op)
{
unary_elementwise_op->channels = channels;
unary_elementwise_op->input_pixel_stride = input_stride;
unary_elementwise_op->output_pixel_stride = output_stride;
if (params_size != 0) {
memcpy(&unary_elementwise_op->params, params, params_size);
}
unary_elementwise_op->ukernel.vunary.function = ukernel;
unary_elementwise_op->type = operator_type;
unary_elementwise_op->flags = flags;
unary_elementwise_op->state = xnn_run_state_invalid;
}
static enum xnn_status create_unary_elementwise_nc(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
const void* params,
size_t params_size,
uint32_t datatype_init_flags,
enum xnn_operator_type operator_type,
xnn_vunary_ukernel_fn ukernel,
xnn_operator_t* unary_elementwise_op_out)
{
xnn_operator_t unary_elementwise_op = NULL;
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));
return xnn_status_uninitialized;
}
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));
return xnn_status_unsupported_hardware;
}
if (channels == 0) {
xnn_log_error(
"failed to create %s operator with %zu channels: number of channels must be non-zero",
xnn_operator_type_to_string(operator_type), channels);
return xnn_status_invalid_parameter;
}
if (input_stride < channels) {
xnn_log_error(
"failed to create %s operator with input element stride of %zu: "
"stride must be at least as large as the number of channels (%zu)",
xnn_operator_type_to_string(operator_type), input_stride, channels);
return xnn_status_invalid_parameter;
}
if (output_stride < channels) {
xnn_log_error(
"failed to create %s operator with output element stride of %zu: "
"stride must be at least as large as the number of channels (%zu)",
xnn_operator_type_to_string(operator_type), output_stride, channels);
return xnn_status_invalid_parameter;
}
unary_elementwise_op = xnn_allocate_zero_simd_memory(sizeof(struct xnn_operator));
if (unary_elementwise_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));
return xnn_status_out_of_memory;
}
init_unary_elementwise_nc(
channels,
input_stride, output_stride,
flags,
params, params_size,
operator_type,
ukernel,
unary_elementwise_op);
*unary_elementwise_op_out = unary_elementwise_op;
return xnn_status_success;
}
static bool is_copy_operator(enum xnn_operator_type operator_type) {
switch (operator_type) {
case xnn_operator_type_copy_nc_x8:
case xnn_operator_type_copy_nc_x16:
case xnn_operator_type_copy_nc_x32:
return true;
default:
return false;
}
}
static enum xnn_status setup_unary_elementwise_nc(
xnn_operator_t unary_elementwise_op,
enum xnn_operator_type expected_operator_type,
size_t batch_size,
const void* input,
void* output,
uint32_t log2_input_size,
uint32_t log2_output_size,
const void* params,
size_t params_size,
size_t num_threads)
{
if (unary_elementwise_op->type != expected_operator_type) {
xnn_log_error("failed to setup operator: operator type mismatch (expected %s, got %s)",
xnn_operator_type_to_string(expected_operator_type),
xnn_operator_type_to_string(unary_elementwise_op->type));
return xnn_status_invalid_parameter;
}
unary_elementwise_op->state = xnn_run_state_invalid;
if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
xnn_log_error("failed to setup %s operator: XNNPACK is not initialized",
xnn_operator_type_to_string(unary_elementwise_op->type));
return xnn_status_uninitialized;
}
if (batch_size == 0 || (input == output && is_copy_operator(expected_operator_type))) {
unary_elementwise_op->state = xnn_run_state_skip;
return xnn_status_success;
}
const size_t channels = unary_elementwise_op->channels;
const size_t input_stride = unary_elementwise_op->input_pixel_stride;
const size_t output_stride = unary_elementwise_op->output_pixel_stride;
xnn_vunary_ukernel_fn ukernel = unary_elementwise_op->ukernel.vunary.function;
if ((((input_stride ^ channels) | (output_stride ^ channels)) == 0) || batch_size == 1) {
const size_t block_size = 4096;
unary_elementwise_op->context.univector_contiguous = (struct univector_contiguous_context) {
.x = input,
.y = output,
.log2_xsize = log2_input_size,
.log2_ysize = log2_output_size,
.ukernel = ukernel,
};
if (params_size != 0) {
memcpy(&unary_elementwise_op->context.univector_contiguous.params, params, params_size);
}
const size_t range = (batch_size * channels) << log2_input_size;
unary_elementwise_op->compute.type = xnn_parallelization_type_1d_tile_1d;
unary_elementwise_op->compute.task_1d_tile_1d = (pthreadpool_task_1d_tile_1d_t) xnn_compute_univector_contiguous;
unary_elementwise_op->compute.range[0] = range;
unary_elementwise_op->compute.tile[0] = (num_threads == 1) ? range : block_size;
} else {
unary_elementwise_op->context.univector_strided = (struct univector_strided_context) {
.n = channels << log2_input_size,
.x = input,
.x_stride = input_stride << log2_input_size,
.y = output,
.y_stride = output_stride << log2_output_size,
.ukernel = ukernel,
};
if (params_size != 0) {
memcpy(&unary_elementwise_op->context.univector_strided.params, params, params_size);
}
unary_elementwise_op->compute.type = xnn_parallelization_type_1d_tile_1d;
unary_elementwise_op->compute.task_1d_tile_1d = (pthreadpool_task_1d_tile_1d_t) xnn_compute_univector_strided;
unary_elementwise_op->compute.range[0] = batch_size;
unary_elementwise_op->compute.tile[0] = (num_threads == 1) ? batch_size : 1;
}
unary_elementwise_op->state = xnn_run_state_ready;
return xnn_status_success;
}
enum xnn_status xnn_create_clamp_nc_f16(
size_t channels,
size_t input_stride,
size_t output_stride,
float output_min,
float output_max,
uint32_t flags,
xnn_operator_t* clamp_op_out)
{
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(xnn_operator_type_clamp_nc_f16));
return xnn_status_uninitialized;
}
if ((xnn_params.init_flags & XNN_INIT_FLAG_F16) != XNN_INIT_FLAG_F16) {
xnn_log_error("failed to create %s operator: operations on data type are not supported",
xnn_operator_type_to_string(xnn_operator_type_clamp_nc_f16));
return xnn_status_unsupported_hardware;
}
if (isnan(output_min)) {
xnn_log_error(
"failed to create %s operator with NaN output lower bound: lower bound must be non-NaN",
xnn_operator_type_to_string(xnn_operator_type_clamp_nc_f16));
return xnn_status_invalid_parameter;
}
if (isnan(output_max)) {
xnn_log_error(
"failed to create %s operator with NaN output upper bound: upper bound must be non-NaN",
xnn_operator_type_to_string(xnn_operator_type_clamp_nc_f16));
return xnn_status_invalid_parameter;
}
const uint16_t output_min_as_half = fp16_ieee_from_fp32_value(output_min);
const uint16_t output_max_as_half = fp16_ieee_from_fp32_value(output_max);
output_min = fp16_ieee_to_fp32_value(output_min_as_half);
output_max = fp16_ieee_to_fp32_value(output_max_as_half);
if (output_min >= output_max) {
xnn_log_error(
"failed to create %s operator with [%.7g, %.7g] output range: lower bound must be below upper bound",
xnn_operator_type_to_string(xnn_operator_type_clamp_nc_f16), output_min, output_max);
return xnn_status_invalid_parameter;
}
union xnn_f16_minmax_params params;
if (xnn_params.f16.clamp.init.f16_minmax != NULL) {
xnn_params.f16.clamp.init.f16_minmax(¶ms, output_min_as_half, output_max_as_half);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F16,
xnn_operator_type_clamp_nc_f16,
xnn_params.f16.clamp.ukernel,
clamp_op_out);
}
enum xnn_status xnn_create_clamp_nc_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
float output_min,
float output_max,
uint32_t flags,
xnn_operator_t* clamp_op_out)
{
if (isnan(output_min)) {
xnn_log_error(
"failed to create %s operator with NaN output lower bound: lower bound must be non-NaN",
xnn_operator_type_to_string(xnn_operator_type_clamp_nc_f32));
return xnn_status_invalid_parameter;
}
if (isnan(output_max)) {
xnn_log_error(
"failed to create %s operator with NaN output upper bound: upper bound must be non-NaN",
xnn_operator_type_to_string(xnn_operator_type_clamp_nc_f32));
return xnn_status_invalid_parameter;
}
if (output_min >= output_max) {
xnn_log_error(
"failed to create %s operator with [%.7g, %.7g] output range: lower bound must be below upper bound",
xnn_operator_type_to_string(xnn_operator_type_clamp_nc_f32), output_min, output_max);
return xnn_status_invalid_parameter;
}
const bool relu_activation = (output_max == INFINITY) && (output_min == 0.0f);
xnn_vunary_ukernel_fn clamp_ukernel = xnn_params.f32.clamp.ukernel;
if (relu_activation && xnn_params.f32.relu.ukernel != NULL) {
clamp_ukernel = xnn_params.f32.relu.ukernel;
}
union xnn_f32_minmax_params params;
if (xnn_params.f32.clamp.init.f32_minmax != NULL) {
xnn_params.f32.clamp.init.f32_minmax(¶ms, output_min, output_max);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F32,
xnn_operator_type_clamp_nc_f32,
clamp_ukernel,
clamp_op_out);
}
enum xnn_status xnn_create_clamp_nc_s8(
size_t channels,
size_t input_stride,
size_t output_stride,
int8_t output_min,
int8_t output_max,
uint32_t flags,
xnn_operator_t* clamp_op_out)
{
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_clamp_nc_s8), output_min, output_max);
return xnn_status_invalid_parameter;
}
union xnn_s8_minmax_params params;
if (xnn_params.s8.clamp.init.s8_minmax != NULL) {
xnn_params.s8.clamp.init.s8_minmax(¶ms, output_min, output_max);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_S8,
xnn_operator_type_clamp_nc_s8,
xnn_params.s8.clamp.ukernel,
clamp_op_out);
}
enum xnn_status xnn_create_clamp_nc_u8(
size_t channels,
size_t input_stride,
size_t output_stride,
uint8_t output_min,
uint8_t output_max,
uint32_t flags,
xnn_operator_t* clamp_op_out)
{
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_clamp_nc_u8), output_min, output_max);
return xnn_status_invalid_parameter;
}
union xnn_u8_minmax_params params;
if (xnn_params.u8.clamp.init.u8_minmax != NULL) {
xnn_params.u8.clamp.init.u8_minmax(¶ms, output_min, output_max);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_U8,
xnn_operator_type_clamp_nc_u8,
xnn_params.u8.clamp.ukernel,
clamp_op_out);
}
enum xnn_status xnn_create_abs_nc_f16(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* abs_op_out)
{
union xnn_f16_abs_params params;
if (xnn_params.f16.abs.init.f16_abs != NULL) {
xnn_params.f16.abs.init.f16_abs(¶ms);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F16,
xnn_operator_type_abs_nc_f16,
xnn_params.f16.abs.ukernel,
abs_op_out);
}
enum xnn_status xnn_create_abs_nc_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* abs_op_out)
{
union xnn_f32_abs_params params;
if (xnn_params.f32.abs.init.f32_abs != NULL) {
xnn_params.f32.abs.init.f32_abs(¶ms);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F32,
xnn_operator_type_abs_nc_f32,
xnn_params.f32.abs.ukernel,
abs_op_out);
}
enum xnn_status xnn_create_bankers_rounding_nc_f16(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* rounding_op_out)
{
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
NULL, 0, XNN_INIT_FLAG_F16,
xnn_operator_type_bankers_rounding_nc_f16,
xnn_params.f16.rndne.ukernel,
rounding_op_out);
}
enum xnn_status xnn_create_bankers_rounding_nc_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* rounding_op_out)
{
union xnn_f32_rnd_params params;
if (xnn_params.f32.rndne.init.f32_rnd != NULL) {
xnn_params.f32.rndne.init.f32_rnd(¶ms);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F32,
xnn_operator_type_bankers_rounding_nc_f32,
xnn_params.f32.rndne.ukernel,
rounding_op_out);
}
enum xnn_status xnn_create_ceiling_nc_f16(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* ceiling_op_out)
{
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
NULL, 0, XNN_INIT_FLAG_F16,
xnn_operator_type_ceiling_nc_f16,
xnn_params.f16.rndu.ukernel,
ceiling_op_out);
}
enum xnn_status xnn_create_ceiling_nc_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* ceiling_op_out)
{
union xnn_f32_rnd_params params;
if (xnn_params.f32.rndu.init.f32_rnd != NULL) {
xnn_params.f32.rndu.init.f32_rnd(¶ms);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F32,
xnn_operator_type_ceiling_nc_f32,
xnn_params.f32.rndu.ukernel,
ceiling_op_out);
}
enum xnn_status xnn_create_convert_nc_f16_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* convert_op_out)
{
union xnn_f16_f32_cvt_params params;
if (xnn_params.vcvt.f16_to_f32.init.f16_f32_cvt != NULL) {
xnn_params.vcvt.f16_to_f32.init.f16_f32_cvt(¶ms);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_VCVT,
xnn_operator_type_convert_nc_f16_f32,
xnn_params.vcvt.f16_to_f32.ukernel,
convert_op_out);
}
enum xnn_status xnn_create_convert_nc_f32_f16(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* convert_op_out)
{
union xnn_f32_f16_cvt_params params;
if (xnn_params.vcvt.f32_to_f16.init.f32_f16_cvt != NULL) {
xnn_params.vcvt.f32_to_f16.init.f32_f16_cvt(¶ms);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_VCVT,
xnn_operator_type_convert_nc_f32_f16,
xnn_params.vcvt.f32_to_f16.ukernel,
convert_op_out);
}
enum xnn_status xnn_create_convert_nc_f32_qs8(
size_t channels,
size_t input_stride,
size_t output_stride,
float output_scale,
int8_t output_zero_point,
int8_t output_min,
int8_t output_max,
uint32_t flags,
xnn_operator_t* convert_op_out)
{
if (output_scale <= 0.0f || !isnormal(output_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g output scale parameter: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convert_nc_f32_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_convert_nc_f32_qs8), output_min, output_max);
return xnn_status_invalid_parameter;
}
union xnn_f32_qs8_cvt_params params;
if (xnn_params.vcvt.f32_to_qs8.init.f32_qs8_cvt != NULL) {
xnn_params.vcvt.f32_to_qs8.init.f32_qs8_cvt(¶ms, 1.0f / output_scale, output_zero_point, output_min, output_max);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_VCVT,
xnn_operator_type_convert_nc_f32_qs8,
xnn_params.vcvt.f32_to_qs8.ukernel,
convert_op_out);
}
enum xnn_status xnn_create_convert_nc_f32_qu8(
size_t channels,
size_t input_stride,
size_t output_stride,
float output_scale,
uint8_t output_zero_point,
uint8_t output_min,
uint8_t output_max,
uint32_t flags,
xnn_operator_t* convert_op_out)
{
if (output_scale <= 0.0f || !isnormal(output_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g output scale parameter: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convert_nc_f32_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_convert_nc_f32_qu8), output_min, output_max);
return xnn_status_invalid_parameter;
}
union xnn_f32_qu8_cvt_params params;
if (xnn_params.vcvt.f32_to_qu8.init.f32_qu8_cvt != NULL) {
xnn_params.vcvt.f32_to_qu8.init.f32_qu8_cvt(¶ms, 1.0f / output_scale, output_zero_point, output_min, output_max);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_VCVT,
xnn_operator_type_convert_nc_f32_qu8,
xnn_params.vcvt.f32_to_qu8.ukernel,
convert_op_out);
}
enum xnn_status xnn_create_convert_nc_qs8(
size_t channels,
size_t input_stride,
size_t output_stride,
float input_scale,
int8_t input_zero_point,
float output_scale,
int8_t output_zero_point,
uint32_t flags,
xnn_operator_t* convert_op_out)
{
if (input_scale <= 0.0f || !isnormal(input_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g input scale parameter: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convert_nc_qs8), input_scale);
return xnn_status_invalid_parameter;
}
if (output_scale <= 0.0f || !isnormal(output_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g input scale parameter: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convert_nc_qs8), output_scale);
return xnn_status_invalid_parameter;
}
const float input_output_scale = input_scale / output_scale;
if (input_output_scale < 0x1.0p-8f || input_output_scale > 0x1.0p+7f) {
xnn_log_error(
"failed to create %s operator with %.7g input-to-output scale ratio: scale ratio must be in [2**-8, 2**7] range",
xnn_operator_type_to_string(xnn_operator_type_convert_nc_qs8), input_output_scale);
return xnn_status_invalid_parameter;
}
union xnn_qs8_cvt_params params;
if (xnn_params.vcvt.qs8.init.qs8_cvt != NULL) {
xnn_params.vcvt.qs8.init.qs8_cvt(¶ms, input_output_scale, input_zero_point, output_zero_point);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_VCVT,
xnn_operator_type_convert_nc_qs8,
xnn_params.vcvt.qs8.ukernel,
convert_op_out);
}
enum xnn_status xnn_create_convert_nc_qs8_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
float input_scale,
int8_t input_zero_point,
uint32_t flags,
xnn_operator_t* convert_op_out)
{
if (input_scale <= 0.0f || !isnormal(input_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g input scale parameter: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convert_nc_qs8_f32), input_scale);
return xnn_status_invalid_parameter;
}
union xnn_qs8_f32_cvt_params params;
if (xnn_params.vcvt.qs8_to_f32.init.qs8_f32_cvt != NULL) {
xnn_params.vcvt.qs8_to_f32.init.qs8_f32_cvt(¶ms, input_scale, input_zero_point);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_VCVT,
xnn_operator_type_convert_nc_qs8_f32,
xnn_params.vcvt.qs8_to_f32.ukernel,
convert_op_out);
}
enum xnn_status xnn_create_convert_nc_qu8(
size_t channels,
size_t input_stride,
size_t output_stride,
float input_scale,
uint8_t input_zero_point,
float output_scale,
uint8_t output_zero_point,
uint32_t flags,
xnn_operator_t* convert_op_out)
{
if (input_scale <= 0.0f || !isnormal(input_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g input scale parameter: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convert_nc_qu8), input_scale);
return xnn_status_invalid_parameter;
}
if (output_scale <= 0.0f || !isnormal(output_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g input scale parameter: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convert_nc_qu8), output_scale);
return xnn_status_invalid_parameter;
}
const float input_output_scale = input_scale / output_scale;
if (input_output_scale < 0x1.0p-8f || input_output_scale > 0x1.0p+7f) {
xnn_log_error(
"failed to create %s operator with %.7g input-to-output scale ratio: scale ratio must be in [2**-8, 2**7] range",
xnn_operator_type_to_string(xnn_operator_type_convert_nc_qu8), input_output_scale);
return xnn_status_invalid_parameter;
}
union xnn_qu8_cvt_params params;
if (xnn_params.vcvt.qu8.init.qu8_cvt != NULL) {
xnn_params.vcvt.qu8.init.qu8_cvt(¶ms, input_output_scale, input_zero_point, output_zero_point);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_VCVT,
xnn_operator_type_convert_nc_qu8,
xnn_params.vcvt.qu8.ukernel,
convert_op_out);
}
enum xnn_status xnn_create_convert_nc_qu8_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
float input_scale,
uint8_t input_zero_point,
uint32_t flags,
xnn_operator_t* convert_op_out)
{
if (input_scale <= 0.0f || !isnormal(input_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g input scale parameter: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_convert_nc_qu8_f32), input_scale);
return xnn_status_invalid_parameter;
}
union xnn_qu8_f32_cvt_params params;
if (xnn_params.vcvt.qu8_to_f32.init.qu8_f32_cvt != NULL) {
xnn_params.vcvt.qu8_to_f32.init.qu8_f32_cvt(¶ms, input_scale, input_zero_point);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_VCVT,
xnn_operator_type_convert_nc_qu8_f32,
xnn_params.vcvt.qu8_to_f32.ukernel,
convert_op_out);
}
enum xnn_status xnn_create_copy_nc_x8(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* copy_op_out)
{
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
NULL, 0, 0,
xnn_operator_type_copy_nc_x8,
xnn_params.xx.copy,
copy_op_out);
}
enum xnn_status xnn_create_copy_nc_x16(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* copy_op_out)
{
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
NULL, 0, 0,
xnn_operator_type_copy_nc_x16,
xnn_params.xx.copy,
copy_op_out);
}
enum xnn_status xnn_create_copy_nc_x32(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* copy_op_out)
{
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
NULL, 0, 0,
xnn_operator_type_copy_nc_x32,
xnn_params.xx.copy,
copy_op_out);
}
enum xnn_status xnn_create_elu_nc_f16(
size_t channels,
size_t input_stride,
size_t output_stride,
float alpha,
uint32_t flags,
xnn_operator_t* elu_op_out)
{
const uint16_t alpha_as_half = fp16_ieee_from_fp32_value(alpha);
alpha = fp16_ieee_to_fp32_value(alpha_as_half);
if (alpha <= 0.0f || !isnormal(alpha)) {
xnn_log_error(
"failed to create %s operator with %.7g alpha parameter: alpha must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_elu_nc_f16), alpha);
return xnn_status_invalid_parameter;
}
union xnn_f16_elu_params params;
if (xnn_params.f16.elu.init.f16_elu != NULL) {
xnn_params.f16.elu.init.f16_elu(¶ms,
UINT16_C(0x3C00) /* prescale = 1.0h */, alpha_as_half, UINT16_C(0x3C00) /* beta = 1.0h */);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F16,
xnn_operator_type_elu_nc_f16,
xnn_params.f16.elu.ukernel,
elu_op_out);
}
enum xnn_status xnn_create_elu_nc_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
float alpha,
uint32_t flags,
xnn_operator_t* elu_op_out)
{
if (alpha <= 0.0f || !isnormal(alpha)) {
xnn_log_error(
"failed to create %s operator with %.7g alpha parameter: alpha must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_elu_nc_f32), alpha);
return xnn_status_invalid_parameter;
}
union xnn_f32_elu_params params;
if (xnn_params.f32.elu.init.f32_elu != NULL) {
xnn_params.f32.elu.init.f32_elu(¶ms, 1.0f /* prescale */, alpha, 1.0f /* beta */);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F32,
xnn_operator_type_elu_nc_f32,
xnn_params.f32.elu.ukernel,
elu_op_out);
}
enum xnn_status xnn_create_floor_nc_f16(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* floor_op_out)
{
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
NULL, 0, XNN_INIT_FLAG_F16,
xnn_operator_type_floor_nc_f16,
xnn_params.f16.rndd.ukernel,
floor_op_out);
}
enum xnn_status xnn_create_floor_nc_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* floor_op_out)
{
union xnn_f32_rnd_params params;
if (xnn_params.f32.rndd.init.f32_rnd != NULL) {
xnn_params.f32.rndd.init.f32_rnd(¶ms);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F32,
xnn_operator_type_floor_nc_f32,
xnn_params.f32.rndd.ukernel,
floor_op_out);
}
enum xnn_status xnn_create_hardswish_nc_f16(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* hardswish_op_out)
{
union xnn_f16_hswish_params params;
if (xnn_params.f16.hswish.init.f16_hswish != NULL) {
xnn_params.f16.hswish.init.f16_hswish(¶ms);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F16,
xnn_operator_type_hardswish_nc_f16,
xnn_params.f16.hswish.ukernel,
hardswish_op_out);
}
enum xnn_status xnn_create_hardswish_nc_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
uint32_t flags,
xnn_operator_t* hardswish_op_out)
{
union xnn_f32_hswish_params params;
if (xnn_params.f32.hswish.init.f32_hswish != NULL) {
xnn_params.f32.hswish.init.f32_hswish(¶ms);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F32,
xnn_operator_type_hardswish_nc_f32,
xnn_params.f32.hswish.ukernel,
hardswish_op_out);
}
enum xnn_status xnn_create_leaky_relu_nc_f16(
size_t channels,
size_t input_stride,
size_t output_stride,
float negative_slope,
uint32_t flags,
xnn_operator_t* leaky_relu_op_out)
{
const uint16_t negative_slope_as_half = fp16_ieee_from_fp32_value(negative_slope);
negative_slope = fp16_ieee_to_fp32_value(negative_slope_as_half);
if (!isfinite(negative_slope)) {
xnn_log_error(
"failed to create %s operator with %f negative slope: finite number expected",
xnn_operator_type_to_string(xnn_operator_type_leaky_relu_nc_f32),
negative_slope);
return xnn_status_invalid_parameter;
}
union xnn_f16_lrelu_params params;
if (xnn_params.f16.lrelu.init.f16_lrelu != NULL) {
xnn_params.f16.lrelu.init.f16_lrelu(¶ms, negative_slope_as_half);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F16,
xnn_operator_type_leaky_relu_nc_f16,
xnn_params.f16.lrelu.ukernel,
leaky_relu_op_out);
}
enum xnn_status xnn_create_leaky_relu_nc_f32(
size_t channels,
size_t input_stride,
size_t output_stride,
float negative_slope,
uint32_t flags,
xnn_operator_t* leaky_relu_op_out)
{
if (!isfinite(negative_slope)) {
xnn_log_error(
"failed to create %s operator with %f negative slope: finite number expected",
xnn_operator_type_to_string(xnn_operator_type_leaky_relu_nc_f32),
negative_slope);
return xnn_status_invalid_parameter;
}
union xnn_f32_lrelu_params params;
if (xnn_params.f32.lrelu.init.f32_lrelu != NULL) {
xnn_params.f32.lrelu.init.f32_lrelu(¶ms, negative_slope);
}
return create_unary_elementwise_nc(
channels, input_stride, output_stride, flags,
¶ms, sizeof(params), XNN_INIT_FLAG_F32,
xnn_operator_type_leaky_relu_nc_f32,
xnn_params.f32.lrelu.ukernel,
leaky_relu_op_out);
}
enum xnn_status xnn_create_leaky_relu_nc_qs8(
size_t channels,
size_t input_stride,
size_t output_stride,
float negative_slope,
int8_t input_zero_point,
float input_scale,
int8_t output_zero_point,
float output_scale,
uint32_t flags,
xnn_operator_t* leaky_relu_op_out)
{
if (!isfinite(negative_slope)) {
xnn_log_error(
"failed to create %s operator with %f negative slope: finite number expected",
xnn_operator_type_to_string(xnn_operator_type_leaky_relu_nc_qs8),
negative_slope);
return xnn_status_invalid_parameter;
}
if (input_scale <= 0.0f || !isnormal(input_scale)) {
xnn_log_error(
"failed to create %s operator with %.7g input scale parameter: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_leaky_relu_nc_qs8), input_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 parameter: scale must be finite, normalized, and positive",
xnn_operator_type_to_string(xnn_operator_type_leaky_relu_nc_qs8), input_scale);
return xnn_status_invalid_parameter;
}
const float positive_input_output_scale = input_scale / output_scale;
if (positive_input_output_scale < 0x1.0p-8f || positive_input_output_scale > 0x1.0p+7f) {
xnn_log_error(
"failed to create %s operator with %.7g positive-input-to-output scale ratio: scale ratio must be in [2**-8, 2**7] range",