-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathgeneric_node_configs.py
617 lines (438 loc) · 19.8 KB
/
generic_node_configs.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
# 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-unsafe
import logging
from typing import cast, List, Optional
import torch
from executorch.backends.xnnpack.partition.config.xnnpack_config import (
ConfigPrecisionType,
XNNPartitionerConfig,
)
from executorch.backends.xnnpack.utils.quant_utils import is_dequant, is_quant
from executorch.backends.xnnpack.utils.utils import get_input_node
from executorch.exir.backend.canonical_partitioners.config_partitioner import (
format_target_name,
)
from executorch.exir.backend.utils import is_shape_dynamic, WhyNoPartition
from torch.export import ExportedProgram
from torch.fx.experimental.symbolic_shapes import has_free_symbols
logger = logging.getLogger(__name__)
why = WhyNoPartition(logger=logger)
class GenericNodePartitionerConfig(XNNPartitionerConfig):
def __init__(self, fused_act: Optional[List[str]] = None, **kwargs):
"""
fused_act is a list of node target names that can be fused with this
node under quantization
"""
self.fused_acts = fused_act or []
super().__init__(**kwargs)
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
return self.check_common_constraints(node, ep)
def get_node_and_deps(
self, node: torch.fx.Node, ep: ExportedProgram
) -> List[torch.fx.Node]:
deps = [node]
quantized_deps = []
if ConfigPrecisionType.STATIC_QUANT in self.enabled_precision_types:
# try to partition dequant inputs and quant outputs if static quant is enabled
if [(is_dequant(dq_input)) for dq_input in node.all_input_nodes].count(
False
):
# if not all inputs are dequant nodes then it isn't quantized
return deps
quantized_deps.extend(node.all_input_nodes)
# check if quantized pattern has fused activation
if len(node.users) != 1:
return deps
node_output = list(node.users)[0]
if (
node_output.op == "call_function"
and format_target_name(node_output.target.__name__) in self.fused_acts
):
quantized_deps.append(node_output)
fused_out_users = list(node_output.users.keys())
if len(fused_out_users) == 1:
node_output = fused_out_users[0]
if not is_quant(node_output):
# Expected node --> fused_act (optional) --> dequant
return deps
quantized_deps.append(node_output)
return deps + quantized_deps
class QuantizedPerTensorConfig(GenericNodePartitionerConfig):
target_name = "quantize_per_tensor.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.STATIC_QUANT]
class DeQuantizedPerTensorConfig(GenericNodePartitionerConfig):
target_name = "dequantize_per_tensor.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.STATIC_QUANT]
class HardtanhConfig(GenericNodePartitionerConfig):
target_name = "hardtanh.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
class AddConfig(GenericNodePartitionerConfig):
target_name = "add.Tensor"
def __init__(self, **kwargs):
super().__init__(fused_act=["relu.default"], **kwargs)
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
class ReLUConfig(GenericNodePartitionerConfig):
target_name = "relu.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
class AbsConfig(GenericNodePartitionerConfig):
target_name = "abs.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class AvgPoolingConfig(GenericNodePartitionerConfig):
target_name = "avg_pool2d.default"
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
XNNPACK does not support ceil_mode = True and count_include_pad = True
Additionally, we only support divisor_override if divisor_override = pooling region
"""
if not self.check_common_constraints(node, ep):
return False
args = node.args
ceil_mode = False # default is False
if len(args) >= 5:
ceil_mode = cast(bool, args[4])
count_include_pad = True # default is True
if len(args) >= 6:
count_include_pad = cast(bool, args[5])
kernel_size = cast(List[int], args[1])
pooling_region = kernel_size[0] * kernel_size[1]
divisor_override = pooling_region # Default divisor is pooling_region
if len(args) >= 7:
divisor_override = cast(int, args[6])
if ceil_mode:
why(node, reason="ceil mode is not supported")
return False
if count_include_pad:
why(
node,
reason="zero-padding in the averaging calculation is not supported",
)
return False
if divisor_override != pooling_region:
why(node, reason="divisor override is not supported")
return False
return True
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class CatConfig(GenericNodePartitionerConfig):
target_name = "cat.default"
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
Only support concatenation of 2 - 5 tensors
"""
if not self.check_common_constraints(node, ep):
return False
num_tensors = len(node.all_input_nodes)
if not (num_tensors >= 2):
why(
node,
reason=f"only support concatenation of > 2 tensors, got {num_tensors} tensors",
)
return False
return True
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
class CeilConfig(GenericNodePartitionerConfig):
target_name = "ceil.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class ClampConfig(GenericNodePartitionerConfig):
target_name = "clamp.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
class DivConfig(GenericNodePartitionerConfig):
target_name = "div.Tensor"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class EluConfig(GenericNodePartitionerConfig):
target_name = "elu.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
def get_original_aten(self) -> Optional[torch._ops.OpOverload]:
return torch.ops.aten.elu.default
class SoftmaxConfig(GenericNodePartitionerConfig):
target_name = "_softmax.default"
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
Check that dim is always the last dim
"""
if not self.check_common_constraints(node, ep):
return False
dim = cast(int, node.args[1])
node_input = node.all_input_nodes[0]
tensor_dims = node_input.meta["val"].dim()
if not (dim == -1 or dim == tensor_dims - 1):
why(
node,
reason=f"dim must be the last dim, got dim = {dim} for tensor of rank {tensor_dims}",
)
return False
return True
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class PermuteConfig(GenericNodePartitionerConfig):
target_name = "permute_copy.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
class SigmoidConfig(GenericNodePartitionerConfig):
target_name = "sigmoid.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class MulConfig(GenericNodePartitionerConfig):
target_name = "mul.Tensor"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
class MaximumConfig(GenericNodePartitionerConfig):
target_name = "maximum.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class MaxPool2dConfig(GenericNodePartitionerConfig):
target_name = "max_pool2d.default"
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
XNNPACK's maxpool2d does not support ceil mode and requires stride <= kernel_size
"""
if not self.check_common_constraints(node, ep):
return False
kernel_size = node.args[1]
stride = node.args[2]
is_ceil_mode = len(node.args) >= 6 and cast(bool, node.args[5])
# Ceil mode is supported via op padding, which must be statically known.
if is_ceil_mode and is_shape_dynamic(node):
why(node, reason="ceil mode is not supported for dynamic shapes")
return False
if stride[0] > kernel_size[0] or stride[1] > kernel_size[1]: # pyre-ignore[16]
why(
node,
reason=f"stride ({stride}) must be less than or equal to kernel size ({kernel_size})",
)
return False
return True
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
def get_original_aten(self) -> Optional[torch._ops.OpOverload]:
return torch.ops.aten.max_pool2d.default
class SqueezeCopyConfig(GenericNodePartitionerConfig):
target_name = "squeeze_copy.dims"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
def get_original_aten(self) -> Optional[torch._ops.OpOverload]:
return torch.ops.aten.squeeze_copy.default
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
XNNPACK's static_reshape only supports 1 dynamic dimension
"""
if not self.check_common_constraints(node, ep):
return False
new_shape = node.meta["val"].shape
dynamic_dim_count = sum(1 for d in new_shape if has_free_symbols(d))
if dynamic_dim_count > 1:
why(node, reason="only a single dynamic dimension is supported")
return False
return True
class UpsampleBilinear2dConfig(GenericNodePartitionerConfig):
target_name = "upsample_bilinear2d.vec"
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
XNNPACK's static_resize_bilinear does not support dynamic output sizes
"""
if not self.check_common_constraints(node, ep):
return False
if is_shape_dynamic(node):
why(node, reason="dynamic output sizes are not supported")
return False
return True
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
def get_original_aten(self) -> Optional[torch._ops.OpOverload]:
return torch.ops.aten.upsample_bilinear2d.vec
class UnsqueezeCopyConfig(GenericNodePartitionerConfig):
target_name = "unsqueeze_copy.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
def get_original_aten(self) -> Optional[torch._ops.OpOverload]:
return torch.ops.aten.unsqueeze_copy.default
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
XNNPACK's static_reshape only supports 1 dynamic dimension
"""
if not self.check_common_constraints(node, ep):
return False
new_shape = node.meta["val"].shape
dynamic_dim_count = sum(
1 for d in new_shape if not isinstance(d, int) and has_free_symbols(d)
)
if dynamic_dim_count > 1:
why(node, reason="only a single dynamic dimension is supported")
return False
return True
class ViewCopyConfig(GenericNodePartitionerConfig):
target_name = "view_copy.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
XNNPACK's static_reshape only supports 1 dynamic dimension
"""
if not self.check_common_constraints(node, ep):
return False
new_shape = node.args[1]
if not all(isinstance(n, int) for n in new_shape):
why(node, reason="symbolic reshape is not supported")
return False
dynamic_dim_count = sum(1 for d in new_shape if d == -1)
if dynamic_dim_count > 1:
why(node, reason="only a single dynamic dimension is supported")
return False
return True
class FloorConfig(GenericNodePartitionerConfig):
target_name = "floor.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class HardswishConfig(GenericNodePartitionerConfig):
target_name = "hardswish.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class LeakyReLUConfig(GenericNodePartitionerConfig):
target_name = "leaky_relu.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class MeanDimConfig(GenericNodePartitionerConfig):
target_name = "mean.dim"
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
Mean Dim currently only supports averaging 4D tensors across the innermost
dimensions
"""
if not self.check_common_constraints(node, ep):
return False
dims = node.args[1]
output_dims = node.meta["val"].dim()
if dims not in ([-2, -1], [-1, -2]):
why(
node,
reason="mean.dim only supports averaging 4D tensors across the innermost dimensions",
)
return False
if output_dims != 4:
why(
node,
reason=f"mean.dim only supports averaging 4D tensors, got tensor of rank {output_dims}",
)
return False
return True
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
class MinimumConfig(GenericNodePartitionerConfig):
target_name = "minimum.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class NegConfig(GenericNodePartitionerConfig):
target_name = "neg.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class PowConfig(GenericNodePartitionerConfig):
target_name = "pow.Tensor_Scalar"
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
Only support powers of two
"""
if not self.check_common_constraints(node, ep):
return False
power = node.args[1]
if not isinstance(power, int):
why(node, reason=f"only support int powers, got {power}")
return False
if power != 2:
why(node, reason=f"only support power == 2, got {power}")
return False
return True
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class SliceCopyConfig(GenericNodePartitionerConfig):
target_name = "slice_copy.Tensor"
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
Support slicing with stride = 1, no zero-dim tensors, Slice isn't supported
if the input or output is dynamic
"""
if not self.check_common_constraints(node, ep):
return False
stride = 1
if len(node.args) > 4:
stride = cast(int, node.args[4])
if stride != 1:
return False
input_node = get_input_node(node, 0)
output_node = node
input_shape = list(input_node.meta["val"].shape)
output_shape = list(output_node.meta["val"].shape)
for dim in input_shape:
if not isinstance(dim, int) or dim == 0:
why(
node,
reason=f"input tensor has invalid shape, dim: {dim} of type {type(dim)}. Expecting non-zero, int values.",
)
return False
for dim in output_shape:
if not isinstance(dim, int) or dim == 0:
why(
node,
reason=f"output tensor has invalid shape, dim: {dim} of type {type(dim)}. Expecting non-zero, int values.",
)
return False
return True
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
class SquareRootConfig(GenericNodePartitionerConfig):
target_name = "sqrt.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class ConstantPadConfig(GenericNodePartitionerConfig):
target_name = "constant_pad_nd.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class SubConfig(GenericNodePartitionerConfig):
target_name = "sub.Tensor"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]
class BMMConfig(GenericNodePartitionerConfig):
"""
Despite being a GEMM Kernel, BMM Can be partitioned like a single node partitioner
because it does not perform any packing on the inputs being matrix multiplied
"""
target_name = "bmm.default"
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]
class SDPAConfig(GenericNodePartitionerConfig):
target_name = "scaled_dot_product_attention.default"
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
"""
Requires Mask to have Rank 2
"""
if not self.check_common_constraints(node, ep):
return False
if len(node.all_input_nodes) < 4:
return False
mask_node = node.all_input_nodes[3]
mask_rank = mask_node.meta["val"].dim()
if mask_rank != 2:
why(
node,
reason=f"mask must have rank 2, got mask of rank {mask_rank}",
)
return False
return True
def get_original_aten(self) -> Optional[torch._ops.OpOverload]:
return torch.ops.aten.scaled_dot_product_attention.default
def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32]