forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops_registrations.py
963 lines (867 loc) · 33.3 KB
/
ops_registrations.py
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
from math import prod
from typing import Optional, Tuple
import torch
from executorch.backends.cadence.aot.utils import (
get_conv1d_output_size,
get_conv2d_output_size,
get_im2row_output_size,
)
from executorch.exir.scalar_type import ScalarType
from torch.library import Library, register_fake
lib = Library("cadence", "DEF")
lib.define(
"quantize_per_tensor(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype) -> (Tensor Z)"
)
lib.define(
"quantize_per_tensor.out(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"dequantize_per_tensor(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype) -> (Tensor Z)"
)
lib.define(
"dequantize_per_tensor.out(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_layer_norm(Tensor X, Tensor X_scale, Tensor X_zero_point, int[] normalized_shape, Tensor weight, Tensor bias, float eps, float output_scale, int output_zero_point) -> (Tensor Y)"
)
lib.define(
"quantized_layer_norm.out(Tensor X, Tensor X_scale, Tensor X_zero_point, int[] normalized_shape, Tensor weight, Tensor bias, float eps, float output_scale, int output_zero_point, *, Tensor(a!) out) -> Tensor (a!)"
)
lib.define(
"quantized_layer_norm.per_tensor(Tensor X, float X_scale, int X_zero_point, int[] normalized_shape, Tensor weight, Tensor bias, float eps, float output_scale, int output_zero_point) -> (Tensor Y)"
)
lib.define(
"quantized_layer_norm.per_tensor_out(Tensor X, float X_scale, int X_zero_point, int[] normalized_shape, Tensor weight, Tensor bias, float eps, float output_scale, int output_zero_point, *, Tensor(a!) out) -> Tensor (a!)"
)
lib.define(
"quantized_linear(Tensor src, Tensor weight, Tensor bias, int src_zero_point, Tensor weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point, Tensor? offset) -> (Tensor Z)"
)
lib.define(
"quantized_linear.out(Tensor src, Tensor weight, Tensor bias, int src_zero_point, Tensor weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point, Tensor? offset, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_linear.per_tensor_out(Tensor src, Tensor weight, Tensor bias, SymInt src_zero_point, SymInt weight_zero_point, SymInt out_multiplier, SymInt out_shift, SymInt out_zero_point, Tensor? offset, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_linear.per_tensor(Tensor src, Tensor weight, Tensor bias, SymInt src_zero_point, "
"SymInt weight_zero_point, SymInt out_multiplier, SymInt out_shift, SymInt out_zero_point, Tensor? offset) -> Tensor"
)
lib.define(
"quantized_relu(Tensor X, Tensor X_zero_point, int out_zero_point, Tensor out_multiplier, Tensor out_shift) -> (Tensor Y)"
)
lib.define(
"quantized_relu.out(Tensor X, Tensor X_zero_point, int out_zero_point, Tensor out_multiplier, Tensor out_shift, *, Tensor(a!) out) -> Tensor (a!)"
)
lib.define(
"quantized_conv(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, int[] dilation, int groups, int input_zero_point, Tensor weight_zero_point, Tensor bias_scale, float out_scale, int out_zero_point, Tensor out_multiplier, Tensor out_shift, bool channel_last=False) -> (Tensor Z)"
)
lib.define(
"quantized_conv.out(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, int[] dilation, int groups, int input_zero_point, Tensor weight_zero_point, Tensor bias_scale, float out_scale, int out_zero_point, Tensor out_multiplier, Tensor out_shift, bool channel_last=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_conv.per_tensor(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, int[] dilation, int groups, int input_zero_point, int weight_zero_point, float bias_scale, float out_scale, int out_zero_point, int out_multiplier, int out_shift, bool channel_last=False) -> (Tensor Z)"
)
lib.define(
"quantized_conv.per_tensor_out(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, int[] dilation, int groups, int input_zero_point, int weight_zero_point, float bias_scale, float out_scale, int out_zero_point, int out_multiplier, int out_shift, bool channel_last=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_matmul(Tensor X, int X_zero_point, Tensor Y, int Y_zero_point, Tensor? bias, int out_multiplier, int out_shift, int out_zero_point, bool transposed=False) -> (Tensor Z)"
)
lib.define(
"quantized_matmul.out(Tensor X, int X_zero_point, Tensor Y, int Y_zero_point, Tensor? bias, int out_multiplier, int out_shift, int out_zero_point, bool transposed=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"convolution(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, "
"int[] dilation, int groups, bool channel_last=False) -> (Tensor Y)"
)
lib.define(
"transposed_convolution(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, "
"int[] dilation, SymInt[] output_padding, int groups, bool channel_last=False) -> (Tensor Y)"
)
lib.define("dequantize(Tensor X, Tensor X_scale, Tensor X_zero_point) -> (Tensor Y)")
lib.define(
"quantized_add(Tensor X, Tensor X_scale, Tensor X_zero_point, Tensor Y, Tensor Y_scale, "
"Tensor Y_zero_point, float out_scale, int out_zero_point) -> (Tensor Z)"
)
lib.define(
"quantized_add.per_tensor(Tensor X, float X_scale, int X_zero_point, Tensor Y, float Y_scale, "
"int Y_zero_point, float out_scale, int out_zero_point) -> (Tensor Z)"
)
lib.define(
"quantized_mul(Tensor X, Tensor X_scale, Tensor X_zero_point, Tensor Y, Tensor Y_scale, "
"Tensor Y_zero_point, float out_scale, int out_zero_point) -> (Tensor Z)"
)
lib.define(
"quantized_add_Scalar(Tensor X, Tensor X_scale, Tensor X_zero_point, Scalar Y, "
"float out_scale, int out_zero_point) -> (Tensor Z)"
)
lib.define(
"quantized_mul_Scalar(Tensor X, Tensor X_scale, Tensor X_zero_point, Scalar Y, "
"float out_scale, int out_zero_point) -> (Tensor Z)"
)
lib.define(
"quantized_embedding_byte(Tensor weight, Tensor weight_scales, Tensor weight_zero_points, "
"Tensor indices, bool pruned_weights=False) -> (Tensor X)"
)
lib.define(
"quantized_transposed_conv(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, "
"int[] dilation, SymInt[] output_padding, int groups, int input_zero_point, Tensor weight_zero_point, "
"Tensor bias_scale, float out_scale, int out_zero_point, Tensor out_multiplier, Tensor out_shift, bool channel_last=False) -> (Tensor out)"
)
lib.define(
"avg_pool2d(Tensor input, int[2] kernel_size, int[2] stride=[], int[2] padding=0, bool ceil_mode=False, "
"bool count_include_pad=True, int? divisor_override=None, Tensor? in_zero_point=None, bool channel_last=False) -> (Tensor out)"
)
lib.define(
"im2row(Tensor input, int[2] kernel_size, int[2] dilation, int[2] padding, int[2] stride, "
"Tensor in_zero_point, bool channel_last=False) -> (Tensor out)"
)
lib.define(
"im2row.per_tensor(Tensor input, int[2] kernel_size, int[2] dilation, int[2] padding, int[2] stride, "
"int in_zero_point, bool channel_last=False) -> (Tensor out)"
)
lib.define("linalg_vector_norm(Tensor X) -> (Tensor Y)")
lib.define("rms_norm(Tensor X, float eps, Tensor W) -> (Tensor Y)")
lib.define(
"transposed_im2row(Tensor input, int[2] kernel_size, int[2] dilation, int[2] padding, int[2] stride, "
"int[2] output_padding, Tensor in_zero_point, bool channel_last=False) -> (Tensor out)"
)
lib.define(
"requantize(Tensor input, Tensor in_scale, Tensor in_zero_point, Tensor out_scale, "
"Tensor out_zero_point, ScalarType out_dtype) -> (Tensor Y)"
)
lib.define(
"requantize.per_tensor(Tensor input, float in_scale, int in_zero_point, float out_scale, "
"int out_zero_point, ScalarType out_dtype) -> (Tensor Y)"
)
lib.define(
"fully_connected(Tensor input, Tensor weight, Tensor? bias=None) -> (Tensor out)"
)
lib.define(
"quantized_fully_connected(Tensor src, Tensor weight, Tensor bias, int src_zero_point, "
"Tensor weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point, Tensor? offset) -> (Tensor Z)"
)
lib.define(
"quantized_fully_connected.per_tensor(Tensor src, Tensor weight, Tensor bias, int src_zero_point, "
"int weight_zero_point, int out_multiplier, int out_shift, int out_zero_point, Tensor? offset) -> (Tensor Z)"
)
lib.define("where_Scalar(Tensor condition, float self, float other) -> (Tensor Z)")
lib.define(
"where_Scalar.out(Tensor condition, float self, float other, *, Tensor(a!) out) -> Tensor(a!)"
)
# ------------------------------------ #
# Migrated from custom_ops.yaml #
# ------------------------------------ #
# Migrated from the custom_ops.yaml files containing different operator variants (e.g., .out, .tensor_out)
lib.define(
"convolution.out(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, int[] dilation, "
"int groups, bool channel_last=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"transposed_convolution.out(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, "
"int[] dilation, SymInt[] output_padding, int groups, bool channel_last=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_relu.per_tensor(Tensor X, int X_zero_point, int out_zero_point, int out_multiplier, int out_shift) -> Tensor"
)
lib.define(
"quantized_relu.per_tensor_out(Tensor X, int X_zero_point, int out_zero_point, int out_multiplier, "
"int out_shift, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_add.out(Tensor X, Tensor X_scale, Tensor X_zero_point, Tensor Y, Tensor Y_scale, "
"Tensor Y_zero_point, float out_scale, int out_zero_point, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_add.per_tensor_out(Tensor X, float X_scale, int X_zero_point, Tensor Y, float Y_scale, "
"int Y_zero_point, float out_scale, int out_zero_point, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_mul.out(Tensor X, Tensor X_scale, Tensor X_zero_point, Tensor Y, Tensor Y_scale, "
"Tensor Y_zero_point, float out_scale, int out_zero_point, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_add_Scalar.out(Tensor X, Tensor X_scale, Tensor X_zero_point, Scalar Y, "
"float out_scale, int out_zero_point, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_mul_Scalar.out(Tensor X, Tensor X_scale, Tensor X_zero_point, Scalar Y, "
"float out_scale, int out_zero_point, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"fully_connected.out(Tensor input, Tensor weight, Tensor? bias=None, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define("linalg_vector_norm.out(Tensor X, *, Tensor(a!) out) -> Tensor(a!)")
lib.define(
"rms_norm.out(Tensor X, float eps, Tensor W, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_fully_connected.out(Tensor src, Tensor weight, Tensor bias, int src_zero_point, "
"Tensor weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point, Tensor? offset, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_fully_connected.per_tensor_out(Tensor src, Tensor weight, Tensor bias, int src_zero_point, "
"int weight_zero_point, int out_multiplier, int out_shift, int out_zero_point, Tensor? offset, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_embedding_byte.out(Tensor weight, Tensor weight_scales, Tensor weight_zero_points, "
"Tensor indices, bool pruned_weights=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"quantized_transposed_conv.out(Tensor input, Tensor weight, Tensor bias, int[] stride, "
"SymInt[] padding, int[] dilation, SymInt[] output_padding, int groups, int input_zero_point, "
"Tensor weight_zero_point, Tensor bias_scale, float out_scale, int out_zero_point, "
"Tensor out_multiplier, Tensor out_shift, bool channel_last=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"avg_pool2d.out(Tensor input, int[2] kernel_size, int[2] stride=[], int[2] padding=0, "
"bool ceil_mode=False, bool count_include_pad=True, int? divisor_override=None, "
"Tensor? in_zero_point=None, bool channel_last=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"im2row.out(Tensor input, int[2] kernel_size, int[2] dilation, int[2] padding, int[2] stride, "
"Tensor in_zero_point, bool channel_last=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"im2row.per_tensor_out(Tensor input, int[2] kernel_size, int[2] dilation, int[2] padding, int[2] stride, "
"int in_zero_point, bool channel_last=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"transposed_im2row.out(Tensor input, int[2] kernel_size, int[2] dilation, int[2] padding, "
"int[2] stride, int[2] output_padding, Tensor in_zero_point, bool channel_last=False, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"requantize.out(Tensor input, Tensor in_scale, Tensor in_zero_point, Tensor out_scale, "
"Tensor out_zero_point, ScalarType out_dtype, *, Tensor(a!) out) -> Tensor(a!)"
)
lib.define(
"requantize.per_tensor_out(Tensor input, float in_scale, int in_zero_point, float out_scale, "
"int out_zero_point, ScalarType out_dtype, *, Tensor(a!) out) -> Tensor(a!)"
)
# Custom ops with aten namespace. Need to specify the lib var as FRAGMENT type as aten library is already defined
aten_lib = Library("aten", "FRAGMENT")
aten_lib.define(
"chunk.out(Tensor self, int chunks, int dim=0, *, Tensor(a!)[] out) -> ()"
)
aten_lib.define(
"contiguous.out(Tensor self, *, MemoryFormat memory_format=contiguous_format, "
"Tensor(a!) out) -> Tensor(a!)"
)
aten_lib.define(
"tensor_split.sections_out(Tensor self, int sections, int dim=0, *, Tensor(a!)[] out) -> ()"
)
aten_lib.define(
"_slice_copy_nop(Tensor self, int dim=0, SymInt? start=None, SymInt? end=None, "
"SymInt step=1) -> Tensor(a!)"
)
aten_lib.define(
"_select_copy_nop.int_out(Tensor self, int dim, SymInt index, *, Tensor(a!) out) -> Tensor(a!)"
)
aten_lib.define(
"_slice_copy_nop.Tensor_out(Tensor self, int dim=0, SymInt? start=None, SymInt? end=None, "
"SymInt step=1, *, Tensor(a!) out) -> Tensor(a!)"
)
aten_lib.define("_cat_nop(Tensor[] tensors, int dim=0) -> Tensor(a!)")
aten_lib.define(
"_cat_nop.out(Tensor[] tensors, int dim=0, *, Tensor(a!) out) -> Tensor(a!)"
)
# Custom ops with cadence_nn_ops namespace
jarvis_nn_lib = Library("jarvis_nn_ops", "DEF")
jarvis_nn_lib.define(
"attention_mask.out(Tensor input, Tensor start, Tensor stop, *, Tensor(a!) out) -> Tensor(a!)"
)
# Custom ops in aten namespace. RMSNorm is usually decomposed, so having
# an out-variant is non-standard
lib_aten = Library("aten", "FRAGMENT")
lib_aten.define(
"rms_norm.out(Tensor input, SymInt[] normalized_shape, Tensor? weight=None, float? eps=None, *, Tensor(a!) out) -> Tensor(a!)"
)
@register_fake("cadence::quantize_per_tensor")
def quantize_per_tensor_meta(
input: torch.Tensor,
scale: float,
zero_point: int,
quant_min: int,
quant_max: int,
dtype: torch.dtype,
) -> torch.Tensor:
return input.new_empty(input.size(), dtype=dtype)
@register_fake("cadence::dequantize_per_tensor")
def dequantize_per_tensor_meta(
input: torch.Tensor,
scale: float,
zero_point: int,
quant_min: int,
quant_max: int,
dtype: torch.dtype,
) -> torch.Tensor:
return input.new_empty(input.size(), dtype=torch.float)
@register_fake("cadence::quantized_add")
def quantized_add_meta(
X: torch.Tensor,
X_scale: torch.Tensor,
X_zero_point: torch.Tensor,
Y: torch.Tensor,
Y_scale: torch.Tensor,
Y_zero_point: torch.Tensor,
out_scale: float,
out_zero_point: int,
) -> torch.Tensor:
out_size = X.size()
if list(X.size()) == [1]:
out_size = Y.size()
return X.new_empty(out_size, dtype=X.dtype)
@register_fake("cadence::quantized_add.per_tensor")
def quantized_add_per_tensor_meta(
X: torch.Tensor,
X_scale: float,
X_zero_point: int,
Y: torch.Tensor,
Y_scale: float,
Y_zero_point: int,
out_scale: float,
out_zero_point: int,
) -> torch.Tensor:
out_size = X.size()
if list(X.size()) == [1]:
out_size = Y.size()
return X.new_empty(out_size, dtype=X.dtype)
@register_fake("cadence::quantized_linear")
def quantized_linear_meta(
src: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
in_zero_point: int,
weight_zero_point: torch.Tensor,
out_multiplier: torch.Tensor,
out_shift: torch.Tensor,
out_zero_point: int,
offset: Optional[torch.Tensor],
) -> torch.Tensor:
# src comes in shape [leading_dims, in_dim]
# weight comes in shape [out_dim, in_dim]
# output comes in empty with shape [leading_dims, out_dim]
out_size = list(src.size())
weight_size = list(weight.size())
assert len(weight_size) == 2
out_size[-1] = weight_size[0]
return src.new_empty(out_size, dtype=src.dtype)
@register_fake("cadence::quantized_linear.per_tensor")
def quantized_linear_per_tensor_meta(
src: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
in_zero_point: torch.SymInt,
weight_zero_point: torch.SymInt,
out_multiplier: torch.SymInt,
out_shift: torch.SymInt,
out_zero_point: torch.SymInt,
offset: Optional[torch.Tensor],
) -> torch.Tensor:
# src comes in shape [leading_dims, in_dim]
# weight comes in shape [out_dim, in_dim]
# output comes in empty with shape [leading_dims, out_dim]
out_size = list(src.size())
weight_size = list(weight.size())
assert len(weight_size) == 2
out_size[-1] = weight_size[0]
return src.new_empty(out_size, dtype=src.dtype)
@register_fake("cadence::quantized_conv")
def quantized_conv_meta(
input: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
stride: Tuple[int],
padding: Tuple[int],
dilation: Tuple[int],
groups: int,
in_zero_point: int,
weight_zero_point: torch.Tensor,
bias_scale: torch.Tensor,
output_scale: float,
output_zero_point: int,
out_multiplier: torch.Tensor,
out_shift: torch.Tensor,
channel_last: bool = False,
) -> torch.Tensor:
if channel_last:
out_channels, *kernel_size, _ = weight.shape
else:
out_channels, _, *kernel_size = weight.shape
in_size = input.shape
# Assert that the input tensor has at least 3 dimensions, and at most 6
assert len(in_size) > 2
assert len(in_size) < 6
# Compute the output tensor size
output_size = (
get_conv1d_output_size(
in_size,
out_channels,
stride[1],
padding[1],
dilation[1],
kernel_size[0],
channel_last,
)
if len(in_size) == 3
else get_conv2d_output_size(
in_size, out_channels, stride, padding, dilation, kernel_size, channel_last
)
)
return input.new_empty(output_size, dtype=input.dtype)
@register_fake("cadence::quantized_conv.per_tensor")
def quantized_conv_per_tensor_meta(
input: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
stride: Tuple[int],
padding: Tuple[int],
dilation: Tuple[int],
groups: int,
in_zero_point: int,
weight_zero_point: int,
bias_scale: float,
output_scale: float,
output_zero_point: int,
out_multiplier: int,
out_shift: int,
channel_last: bool = False,
) -> torch.Tensor:
if channel_last:
out_channels, *kernel_size, _ = weight.shape
else:
out_channels, _, *kernel_size = weight.shape
in_size = input.shape
# Assert that the input tensor has at least 3 dimensions, and at most 6
assert len(in_size) > 2
assert len(in_size) < 6
# Compute the output tensor size
output_size = (
get_conv1d_output_size(
in_size,
out_channels,
stride[1],
padding[1],
dilation[1],
kernel_size[0],
channel_last,
)
if len(in_size) == 3
else get_conv2d_output_size(
in_size, out_channels, stride, padding, dilation, kernel_size, channel_last
)
)
return input.new_empty(output_size, dtype=input.dtype)
@register_fake("cadence::quantized_layer_norm")
def quantized_layer_norm_meta(
input: torch.Tensor,
X_scale: torch.Tensor,
X_zero_point: torch.Tensor,
normalized_shape: int,
weight: torch.Tensor,
bias: torch.Tensor,
eps: float,
output_scale: float,
output_zero_point: int,
) -> torch.Tensor:
return input.new_empty(input.size(), dtype=input.dtype)
@register_fake("cadence::quantized_layer_norm.per_tensor")
def quantized_layer_norm_per_tensor_meta(
input: torch.Tensor,
X_scale: float,
X_zero_point: int,
normalized_shape: int,
weight: torch.Tensor,
bias: torch.Tensor,
eps: float,
output_scale: float,
output_zero_point: int,
) -> torch.Tensor:
return input.new_empty(input.size(), dtype=input.dtype)
@register_fake("cadence::quantized_relu")
def quantized_relu_meta(
X: torch.Tensor,
X_zero_point: torch.Tensor,
out_zero_point: int,
out_multiplier: torch.Tensor,
out_shift: torch.Tensor,
) -> torch.Tensor:
return X.new_empty(X.size(), dtype=X.dtype)
@register_fake("cadence::quantized_matmul")
def quantized_matmul_meta(
X: torch.Tensor,
X_zero_point: int,
Y: torch.Tensor,
Y_zero_point: int,
bias: Optional[torch.Tensor],
out_multiplier: int,
out_shift: int,
out_zero_point: int,
transposed: bool = False,
) -> torch.Tensor:
X_size = list(X.size())
Y_size = list(Y.size())
# Get the batch dimensions for both tensors
X_batch_dims = X_size[:-2]
Y_batch_dims = Y_size[:-2]
# If they don't match, check that they're compatible
if X_batch_dims != Y_batch_dims:
assert prod(X_batch_dims) == prod(
Y_batch_dims
), f"Batch dimensions of X and Y do not match: {X_batch_dims} vs {Y_batch_dims}"
# Get the matmul output size
if transposed:
assert X_size[-1] == Y_size[-1], "matrices cannot be multiplied"
mat_size = [X_size[-2], Y_size[-2]]
else:
assert X_size[-1] == Y_size[-2], "matrices cannot be multiplied"
mat_size = [X_size[-2], Y_size[-1]]
# Combine the larger batch dimensions with the matmul output size
out_size = (
X_batch_dims + mat_size
if len(X_batch_dims) > len(Y_batch_dims)
else Y_batch_dims + mat_size
)
return X.new_empty(out_size, dtype=X.dtype)
@register_fake("cadence::im2row")
def im2row_meta(
input: torch.Tensor,
kernel_size: Tuple[int],
dilation: Tuple[int],
padding: Tuple[int],
stride: Tuple[int],
in_zero_point: torch.Tensor,
channel_last: bool = False,
) -> torch.Tensor:
output_size = get_im2row_output_size(
input, kernel_size, dilation, padding, stride, channel_last
)
return input.new_empty(output_size, dtype=input.dtype)
@register_fake("cadence::im2row.per_tensor")
def im2row_per_tensor_meta(
input: torch.Tensor,
kernel_size: Tuple[int],
dilation: Tuple[int],
padding: Tuple[int],
stride: Tuple[int],
in_zero_point: int,
channel_last: bool = False,
) -> torch.Tensor:
output_size = get_im2row_output_size(
input, kernel_size, dilation, padding, stride, channel_last
)
return input.new_empty(output_size, dtype=input.dtype)
# Define the abstract implementations of the operators as required
@register_fake("cadence::linalg_vector_norm")
def linalg_vector_norm_meta(
X: torch.Tensor,
) -> torch.Tensor:
# Output of norm is a scalar, so we return a [] tensor
return X.new_empty([], dtype=X.dtype)
@register_fake("cadence::requantize")
def requantize_meta(
input: torch.Tensor,
in_scale: torch.Tensor,
in_zero_point: torch.Tensor,
out_scale: torch.Tensor,
out_zero_point: torch.Tensor,
dtype: ScalarType,
) -> torch.Tensor:
return input.new_empty(
input.size(),
# pyre-ignore[6]: Incompatible type
dtype=dtype,
)
@register_fake("cadence::requantize.per_tensor")
def requantize_per_tensor_meta(
input: torch.Tensor,
in_scale: float,
in_zero_point: int,
out_scale: float,
out_zero_point: int,
dtype: ScalarType,
) -> torch.Tensor:
return input.new_empty(
input.size(),
# pyre-ignore[6]: Incompatible type
dtype=dtype,
)
@register_fake("cadence::quantized_relu.per_tensor")
def quantized_relu_per_tensor_meta(
input: torch.Tensor,
in_zero_point: int,
out_zero_point: int,
out_multiplier: int,
out_shift: int,
) -> torch.Tensor:
return input.new_empty(input.size(), dtype=input.dtype)
@register_fake("cadence::fully_connected")
def fully_connected_meta(
src: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
) -> torch.Tensor:
# src comes in shape [leading_dims, in_dim]
# weight comes in shape [out_dim, in_dim]
# output comes in empty with shape [leading_dims, out_dim]
out_size = list(src.size())
weight_size = list(weight.size())
assert len(weight_size) == 2
out_size[-1] = weight_size[0]
return src.new_empty(out_size, dtype=src.dtype)
@register_fake("cadence::quantized_fully_connected")
def quantized_fully_connected_meta(
src: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
in_zero_point: int,
weight_zero_point: torch.Tensor,
out_multiplier: torch.Tensor,
out_shift: torch.Tensor,
out_zero_point: int,
offset: Optional[torch.Tensor],
) -> torch.Tensor:
# src comes in shape [leading_dims, in_dim]
# weight comes in shape [out_dim, in_dim]
# output comes in empty with shape [leading_dims, out_dim]
out_size = list(src.size())
weight_size = list(weight.size())
assert len(weight_size) == 2
out_size[-1] = weight_size[0]
return src.new_empty(out_size, dtype=src.dtype)
@register_fake("cadence::quantized_fully_connected.per_tensor")
def quantized_fully_connected_per_tensor_meta(
src: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
in_zero_point: int,
weight_zero_point: int,
out_multiplier: int,
out_shift: int,
out_zero_point: int,
offset: Optional[torch.Tensor],
) -> torch.Tensor:
# src comes in shape [leading_dims, in_dim]
# weight comes in shape [out_dim, in_dim]
# output comes in empty with shape [leading_dims, out_dim]
out_size = list(src.size())
weight_size = list(weight.size())
assert len(weight_size) == 2
out_size[-1] = weight_size[0]
return src.new_empty(out_size, dtype=src.dtype)
@register_fake("cadence::convolution")
def convolution_meta(
input: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
stride: Tuple[int],
padding: Tuple[int],
dilation: Tuple[int],
groups: int,
channel_last: bool = False,
) -> torch.Tensor:
if channel_last:
out_channels, *kernel_size, _ = weight.shape
else:
out_channels, _, *kernel_size = weight.shape
in_size = input.shape
# Assert that the input tensor has at least 3 dimensions, and at most 6
assert len(in_size) > 2
assert len(in_size) < 6
# Compute the output tensor size
output_size = (
get_conv1d_output_size(
in_size,
out_channels,
stride[0],
padding[0],
dilation[0],
kernel_size[0],
channel_last,
)
if len(in_size) == 3
else get_conv2d_output_size(
in_size, out_channels, stride, padding, dilation, kernel_size, channel_last
)
)
return input.new_empty(output_size, dtype=input.dtype)
@register_fake("cadence::transposed_convolution")
def transposed_convolution_meta(
input: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
stride: Tuple[int],
padding: Tuple[int],
dilation: Tuple[int],
output_padding: Tuple[int],
groups: int,
channel_last: bool = False,
) -> torch.Tensor:
# The native definition of torch transposed conv will have weight shape as
# (in_channels, out_channels/groups, *kernel_size).
# However, the two channel position is flipped in the Jarvis pass of replacing it
# with cadence::transposed_convolution here: https://fburl.com/code/d2s7pkyy
out_channels, _input_channels, *kernel_size = weight.shape
out_channels *= groups
in_size = input.shape
# Get the output size of a transposed 1D convolution given the input size and parameters
def get_conv_transpose1d_output_size(
in_size: torch.Size,
kernel_size: list[int],
out_channels: int,
stride: Tuple[int],
padding: Tuple[int],
dilation: Tuple[int],
output_padding: Tuple[int],
channel_last: bool = False,
) -> torch.Size:
assert len(in_size) == 3
if channel_last:
N, L, C = in_size
else:
N, C, L = in_size
# Reference: https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose1d.html
lout = (
(L - 1) * stride[0]
- 2 * padding[0]
+ dilation[0] * (kernel_size[0] - 1)
+ output_padding[0]
+ 1
)
if channel_last:
return torch.Size((in_size[0], lout, out_channels))
else:
return torch.Size((in_size[0], out_channels, lout))
def get_conv_transpose2d_output_size(
in_size: torch.Size,
kernel_size: list[int],
out_channels: int,
stride: Tuple[int],
padding: Tuple[int],
dilation: Tuple[int],
output_padding: Tuple[int],
channel_last: bool = False,
) -> torch.Size:
assert len(in_size) == 4
if channel_last:
N, H, W, C = in_size
else:
N, C, H, W = in_size
# Reference: https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose2d.html
hout = (
(H - 1) * stride[0]
- 2 * padding[0]
+ dilation[0] * (kernel_size[0] - 1)
+ output_padding[0]
+ 1
)
wout = (
(W - 1) * stride[1]
- 2 * padding[1]
+ dilation[1] * (kernel_size[1] - 1)
+ output_padding[1]
+ 1
)
if channel_last:
return torch.Size((in_size[0], hout, wout, out_channels))
else:
return torch.Size((in_size[0], out_channels, hout, wout))
# Compute the output tensor size
if len(in_size) == 3:
output_size = get_conv_transpose1d_output_size(
in_size,
kernel_size,
out_channels,
stride,
padding,
dilation,
output_padding,
channel_last,
)
elif len(in_size) == 4:
output_size = get_conv_transpose2d_output_size(
in_size,
kernel_size,
out_channels,
stride,
padding,
dilation,
output_padding,
channel_last,
)
else:
raise NotImplementedError(
f"transposed_convolution meta is not implemented for input tensor with {len(in_size)} dimensions"
)
return input.new_empty(output_size, dtype=input.dtype)
@register_fake("cadence::avg_pool2d")
def avg_pool2d_meta(
input: torch.Tensor,
kernel_size: Tuple[int],
stride: Tuple[int],
padding: Tuple[int],
ceil_mode: bool,
count_include_pad: Optional[bool] = True,
divisor_override: Optional[int] = None,
in_zero_point: Optional[int] = None,
channel_last: bool = False,
) -> torch.Tensor:
# Use torch native meta kernels when operator semantics are similar
return torch._meta_registrations.meta_avg_pool2d(
input,
kernel_size,
stride,
padding,
ceil_mode,
count_include_pad,
divisor_override,
)
@register_fake("cadence::transposed_im2row")
def transposed_im2row_meta(
input: torch.Tensor,
kernel_size: Tuple[int],
dilation: Tuple[int],
padding: Tuple[int],
stride: Tuple[int],
output_padding: Tuple[int],
in_zero_point: torch.Tensor,
channel_last: bool = False,
) -> torch.Tensor:
if len(input.shape) == 3:
height_dim = 1 if channel_last else 2
input = input.unsqueeze(height_dim)
batch_size = input.shape[0]
n_input_plane = input.shape[3] if channel_last else input.shape[1]
input_height = input.shape[1] if channel_last else input.shape[2]
input_width = input.shape[2] if channel_last else input.shape[3]
output_height = (
(input_height - 1) * stride[0]
- 2 * padding[0]
+ dilation[0] * (kernel_size[0] - 1)
+ output_padding[0]
+ 1
)
output_width = (
(input_width - 1) * stride[1]
- 2 * padding[1]
+ dilation[1] * (kernel_size[1] - 1)
+ output_padding[1]
+ 1
)
n_output_plane = n_input_plane * kernel_size[0] * kernel_size[1]
output_length = output_height * output_width
output_size = torch.Size((batch_size, output_length, n_output_plane))
return input.new_empty(output_size, dtype=input.dtype)
@register_fake("cadence::where_Scalar")
def where_Scalar_meta(
condition: torch.Tensor,
self: float,
other: float,
) -> torch.Tensor:
return condition.new_empty(condition.size(), dtype=torch.float32)