|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +"""Converter for batch matrix multiplication operations.""" |
| 8 | + |
| 9 | +from typing import Any, Dict, Optional |
| 10 | + |
| 11 | +import tensorrt as trt |
| 12 | +import torch |
| 13 | +from executorch.backends.nvidia.tensorrt.converter_registry import converter |
| 14 | +from executorch.backends.nvidia.tensorrt.converter_utils import set_layer_name |
| 15 | + |
| 16 | + |
| 17 | +@converter("aten.bmm.default") |
| 18 | +def convert_bmm( |
| 19 | + node: torch.fx.Node, |
| 20 | + network: trt.INetworkDefinition, |
| 21 | + input_map: Dict[torch.fx.Node, Any], |
| 22 | + edge_program: Optional[Any] = None, |
| 23 | +) -> trt.ITensor: |
| 24 | + """Convert aten.bmm.default to TensorRT MatrixMultiply. |
| 25 | +
|
| 26 | + Performs batch matrix multiplication of two 3D tensors (B, M, K) @ (B, K, N) -> (B, M, N). |
| 27 | + TensorRT's IMatrixMultiplyLayer supports batch matrix multiplication natively. |
| 28 | + """ |
| 29 | + lhs_arg = node.args[0] |
| 30 | + rhs_arg = node.args[1] |
| 31 | + |
| 32 | + if lhs_arg not in input_map: |
| 33 | + raise ValueError(f"Input node '{lhs_arg.name}' not found in input_map for bmm") |
| 34 | + if rhs_arg not in input_map: |
| 35 | + raise ValueError(f"Input node '{rhs_arg.name}' not found in input_map for bmm") |
| 36 | + |
| 37 | + lhs = input_map[lhs_arg] |
| 38 | + rhs = input_map[rhs_arg] |
| 39 | + |
| 40 | + layer = network.add_matrix_multiply( |
| 41 | + lhs, trt.MatrixOperation.NONE, rhs, trt.MatrixOperation.NONE |
| 42 | + ) |
| 43 | + set_layer_name(layer, node, "bmm") |
| 44 | + |
| 45 | + return layer.get_output(0) |
0 commit comments