Creates and registers NodeVisitors for operators that have two inputs and map directly to a TOSA op.
(
bw_target: str, tosa_op, attr_builder: Callable[[Any], None]
)
| 24 | |
| 25 | |
| 26 | def binary_operator_factory( |
| 27 | bw_target: str, tosa_op, attr_builder: Callable[[Any], None] |
| 28 | ): |
| 29 | """Creates and registers NodeVisitors for operators that have two inputs and |
| 30 | map directly to a TOSA op. |
| 31 | """ |
| 32 | |
| 33 | class BinaryOperator(NodeVisitor): |
| 34 | target = bw_target |
| 35 | |
| 36 | def define_node( |
| 37 | self, |
| 38 | node: torch.fx.Node, |
| 39 | tosa_graph: Any, |
| 40 | inputs: List[TosaArg], |
| 41 | output: TosaArg, |
| 42 | ) -> None: |
| 43 | validate_num_inputs(self.target, inputs, 2) |
| 44 | validate_same_dtype(self.target, [*inputs, output], ts) |
| 45 | |
| 46 | if self.target in [ |
| 47 | "aten.bitwise_and.Tensor", |
| 48 | "aten.bitwise_xor.Tensor", |
| 49 | "aten.bitwise_or.Tensor", |
| 50 | "aten.bitwise_left_shift.Tensor", |
| 51 | ]: |
| 52 | validate_valid_dtype( |
| 53 | self.target, |
| 54 | [*inputs, output], |
| 55 | [ts.DType.INT8, ts.DType.INT16, ts.DType.INT32], |
| 56 | self.tosa_spec, |
| 57 | ) |
| 58 | if self.target in [ |
| 59 | "aten.logical_and.default", |
| 60 | "aten.logical_xor.defaul", |
| 61 | "aten.logical_or.default", |
| 62 | ]: |
| 63 | validate_valid_dtype( |
| 64 | self.target, |
| 65 | [*inputs, output], |
| 66 | [ts.DType.BOOL], |
| 67 | self.tosa_spec, |
| 68 | ) |
| 69 | attr = ts.TosaSerializerAttribute() |
| 70 | attr_builder(attr) |
| 71 | self._serialize_operator( |
| 72 | node, |
| 73 | tosa_graph, |
| 74 | tosa_op, |
| 75 | [inputs[0].name, inputs[1].name], |
| 76 | [output.name], |
| 77 | attr, |
| 78 | ) |
| 79 | |
| 80 | register_node_visitor(BinaryOperator) |
| 81 | |
| 82 | |
| 83 | binary_operator_factory( |
no test coverage detected