|
8 | 8 | import logging |
9 | 9 | import operator |
10 | 10 | from dataclasses import dataclass |
| 11 | +from functools import partial |
11 | 12 | from typing import Callable, final, Mapping |
12 | 13 |
|
13 | 14 | import torch |
@@ -227,6 +228,7 @@ def tag_qdq_clusters(self, nodes: list[torch.fx.Node]): |
227 | 228 | exir_ops.edge.aten.mm.default: MMConverter, # noqa F405 |
228 | 229 | exir_ops.edge.aten.mul.Tensor: MulTensorConverter, # noqa F405 |
229 | 230 | exir_ops.edge.aten.neg.default: NegConverter, # noqa F405 |
| 231 | + exir_ops.edge.aten.pad.default: PadConverter, # noqa F405 |
230 | 232 | exir_ops.edge.aten.permute_copy.default: PermuteCopyConverter, # noqa F405 |
231 | 233 | exir_ops.edge.aten.prelu.default: PReLUConverter, # noqa F405 |
232 | 234 | exir_ops.edge.aten.relu.default: ReLUConverter, # noqa F405 |
@@ -360,6 +362,11 @@ def __init__( |
360 | 362 | self.preserve_ops = preserve_ops or [] |
361 | 363 | self.check_op_support = check_op_support |
362 | 364 |
|
| 365 | + # dict: exir_op -> converter |
| 366 | + self.edge_op_to_converter = {} |
| 367 | + for exir_op, converter in supported_ops.items(): |
| 368 | + self.edge_op_to_converter[exir_op._op] = converter |
| 369 | + |
363 | 370 | @staticmethod |
364 | 371 | def _partition_contains_compute_nodes( |
365 | 372 | partition: Partition, parameters_mapping: dict[str, Parameter] |
@@ -501,57 +508,63 @@ def partition(self, exported_program: ExportedProgram) -> PartitionResult: |
501 | 508 | tagged_exported_program=exported_program, partition_tags=partition_tags |
502 | 509 | ) |
503 | 510 |
|
| 511 | + def _preserve_ops_filter( |
| 512 | + self, |
| 513 | + parameters_mapping: dict[str, Parameter], |
| 514 | + node: torch.fx.Node, |
| 515 | + ): |
| 516 | + """Filter for `preserve_ops`. This filter makes `preserve_ops` a candidate list of ops that can be or cannot be preserved, |
| 517 | + based on conditions below. |
| 518 | + """ |
| 519 | + # Make `preserve_ops` a candidate list, otherwise the conditions would be checked for all graph ops. |
| 520 | + if node.target not in self.preserve_ops: |
| 521 | + return False |
| 522 | + |
| 523 | + # If the converter for the node does not exist, the operator should be decomposed. |
| 524 | + # When it does not have a converter, it usually means it is natively decomposed into simpler ops that have a converter. |
| 525 | + node_converter = self.edge_op_to_converter.get(node.target) |
| 526 | + if node_converter is None: |
| 527 | + logger.w( |
| 528 | + f"Node of target {str(node.target)} might be decomposed to other edge operators " |
| 529 | + + "because no appropriate NodeConverter exists." |
| 530 | + ) |
| 531 | + return False |
| 532 | + |
| 533 | + # op must be decomposed if it is not delegable |
| 534 | + delegable = node_converter.is_supported( |
| 535 | + node, |
| 536 | + self.neutron_target_spec, |
| 537 | + parameters_mapping, |
| 538 | + self.custom_delegation_options, |
| 539 | + ) |
| 540 | + |
| 541 | + # if the node is delegable and the base check_op callable determines the node should not be decomposed, |
| 542 | + # then do not decompose |
| 543 | + base_check_op = ( |
| 544 | + True if self.check_op_support is None else self.check_op_support(node) |
| 545 | + ) |
| 546 | + return delegable and base_check_op |
| 547 | + |
504 | 548 | def ops_to_not_decompose( |
505 | 549 | self, |
506 | 550 | ep: ExportedProgram, |
507 | 551 | ) -> tuple[list[torch._ops.OpOverload], Callable[[torch.fx.Node], bool] | None]: |
508 | 552 | """ |
509 | | - Method to determine which operators SHOULD NOT be decomposed to edge dialect. |
| 553 | + Method to determine which operators SHOULD NOT be decomposed to simpler edge ops. |
510 | 554 |
|
511 | | - The method returns: |
512 | | - a list of operators NOT to decompose |
513 | | - optional callable - filter |
514 | | - If the operator is in the list and the evaluation of the callable is True (or the callable is not specified), |
515 | | - the operator should not be decomposed (i.e. it truly belongs to the list). Otherwise, it should be decomposed. |
| 555 | + Returns: |
| 556 | + List[torch._ops.OpOverload]: a list of ops that should not be decomposed. |
| 557 | + Optional[Callable[[torch.fx.Node], bool]]]: an optional filter, called for each node in the |
| 558 | + graph, that lets a node be decomposed even though its op is in the list above. A node is kept |
| 559 | + (not decomposed) only if the filter returns True for it; if it returns False, the node is decomposed. |
516 | 560 | """ |
517 | 561 |
|
518 | 562 | # The parameters_mapping will only contain `FakeTensor` objects since the partitioning is done with a "Fake model". |
519 | 563 | # If a node is selected to not be decomposed and it requires the real static parameters in its 'is_supported_*' methods, |
520 | 564 | # the state dict from the quantized model will need to be provided here. |
521 | 565 | parameters_mapping = EdgeProgramToIRConverter.map_inputs_to_parameters(ep) |
522 | | - aten_op_to_converter = {} |
523 | | - for exir_op, converter in supported_ops.items(): |
524 | | - aten_op_to_converter[exir_op._op] = converter |
525 | 566 |
|
526 | 567 | # determine node format so it can be checked if the nodes are delegable |
527 | 568 | NodeFormatInference(ep, only_for_op_support_check=True).identify_node_formats() |
528 | 569 |
|
529 | | - def check_op_support_extended(node: torch.fx.Node): |
530 | | - # if the operator should not be preserved, then it should be decomposed |
531 | | - if node.target not in self.preserve_ops: |
532 | | - return False |
533 | | - |
534 | | - # if the converter for the node does not exist, the operator should be decomposed |
535 | | - node_converter = aten_op_to_converter.get(node.target) |
536 | | - if node_converter is None: |
537 | | - logger.w( |
538 | | - f"Node of target {str(node.target)} might be decomposed to other edge operators " |
539 | | - + "because no appropriate NodeConverter exists." |
540 | | - ) |
541 | | - return False |
542 | | - |
543 | | - delegable = node_converter.is_supported( |
544 | | - node, |
545 | | - self.neutron_target_spec, |
546 | | - parameters_mapping, |
547 | | - self.custom_delegation_options, |
548 | | - ) |
549 | | - |
550 | | - # if the node is delegable and the base check_op callable determines the node should not be decomposed, |
551 | | - # then do not decompose |
552 | | - base_check_op = ( |
553 | | - True if self.check_op_support is None else self.check_op_support(node) |
554 | | - ) |
555 | | - return delegable and base_check_op |
556 | | - |
557 | | - return self.preserve_ops, check_op_support_extended |
| 570 | + return self.preserve_ops, partial(self._preserve_ops_filter, parameters_mapping) |
0 commit comments