Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backends/nvidia/tensorrt/converters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
Expand All @@ -11,6 +11,7 @@
from executorch.backends.nvidia.tensorrt.converters import add # noqa: F401
from executorch.backends.nvidia.tensorrt.converters import addmm # noqa: F401
from executorch.backends.nvidia.tensorrt.converters import batch_norm # noqa: F401
from executorch.backends.nvidia.tensorrt.converters import bmm # noqa: F401
from executorch.backends.nvidia.tensorrt.converters import clamp # noqa: F401
from executorch.backends.nvidia.tensorrt.converters import concat # noqa: F401
from executorch.backends.nvidia.tensorrt.converters import conv2d # noqa: F401
Expand Down
45 changes: 45 additions & 0 deletions backends/nvidia/tensorrt/converters/bmm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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.

"""Converter for batch matrix multiplication operations."""

from typing import Any, Dict, Optional

import tensorrt as trt
import torch
from executorch.backends.nvidia.tensorrt.converter_registry import converter
from executorch.backends.nvidia.tensorrt.converter_utils import set_layer_name


@converter("aten.bmm.default")
def convert_bmm(
node: torch.fx.Node,
network: trt.INetworkDefinition,
input_map: Dict[torch.fx.Node, Any],
edge_program: Optional[Any] = None,
) -> trt.ITensor:
"""Convert aten.bmm.default to TensorRT MatrixMultiply.

Performs batch matrix multiplication of two 3D tensors (B, M, K) @ (B, K, N) -> (B, M, N).
TensorRT's IMatrixMultiplyLayer supports batch matrix multiplication natively.
"""
lhs_arg = node.args[0]
rhs_arg = node.args[1]

if lhs_arg not in input_map:
raise ValueError(f"Input node '{lhs_arg.name}' not found in input_map for bmm")
if rhs_arg not in input_map:
raise ValueError(f"Input node '{rhs_arg.name}' not found in input_map for bmm")

lhs = input_map[lhs_arg]
rhs = input_map[rhs_arg]

layer = network.add_matrix_multiply(
lhs, trt.MatrixOperation.NONE, rhs, trt.MatrixOperation.NONE
)
set_layer_name(layer, node, "bmm")

return layer.get_output(0)
1 change: 1 addition & 0 deletions backends/nvidia/tensorrt/converters/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def define_common_targets():
"add.py",
"addmm.py",
"batch_norm.py",
"bmm.py",
"clamp.py",
"concat.py",
"conv2d.py",
Expand Down
5 changes: 5 additions & 0 deletions examples/nvidia/tensorrt/export.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
Expand Down Expand Up @@ -38,6 +38,10 @@
"conv1d",
"dl3",
"edsr",
# "efficient_sam", # TODO: diff ~41 — likely bicubic interpolation decomposition or ConvTranspose2d issue
"emformer_join",
# "emformer_predict", # TODO: passes 1/3 seeds — precision sensitive with randomized inputs
"emformer_transcribe",
"ic3",
"linear",
"mul",
Expand Down Expand Up @@ -126,6 +130,7 @@

et_module = _load_for_executorch_from_buffer(pte_bytes)


for seed in _TEST_SEEDS:
inputs = _randomise_inputs(example_inputs, seed)

Expand Down
18 changes: 13 additions & 5 deletions examples/nvidia/tensorrt/tests/test_export.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
Expand All @@ -16,7 +16,7 @@
import shutil
import unittest

import torch

Check warning on line 19 in examples/nvidia/tensorrt/tests/test_export.py

View workflow job for this annotation

GitHub Actions / lintrunner

FLAKE8 F401

'torch' imported but unused See https://www.flake8rules.com/rules/F401.html.

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
Expand All @@ -40,11 +40,7 @@
for env_var, filename in _WEIGHT_ENV_VARS.items():
src = os.environ.get(env_var)
if src and os.path.isfile(src):
# dog.jpg goes to CWD (mv2 model downloads it there)
if filename == "dog.jpg":
dst = os.path.join(os.getcwd(), filename)
else:
dst = os.path.join(cache_dir, filename)
dst = os.path.join(cache_dir, filename)
if not os.path.exists(dst):
shutil.copy2(src, dst)
logger.info(f"Cached {filename} from {src}")
Expand Down Expand Up @@ -87,7 +83,7 @@
from executorch.examples.models import MODEL_NAME_TO_MODEL
from executorch.examples.models.model_factory import EagerModelFactory
from executorch.exir import to_edge_transform_and_lower
import torch

Check warning on line 86 in examples/nvidia/tensorrt/tests/test_export.py

View workflow job for this annotation

GitHub Actions / lintrunner

FLAKE8 F811

redefinition of unused 'torch' from line 19 See https://www.flake8rules.com/rules/F811.html.

Check warning on line 86 in examples/nvidia/tensorrt/tests/test_export.py

View workflow job for this annotation

GitHub Actions / lintrunner

FLAKE8 F401

'torch' imported but unused See https://www.flake8rules.com/rules/F401.html.
from torch.export import export
model, example_inputs, _, _ = EagerModelFactory.create_model(*MODEL_NAME_TO_MODEL["add"])
model = model.eval()
Expand Down Expand Up @@ -121,3 +117,15 @@

def test_sdpa(self) -> None:
_export_and_verify("sdpa")

def test_emformer_join(self) -> None:
_export_and_verify("emformer_join")

def test_softmax(self) -> None:

Check warning on line 124 in examples/nvidia/tensorrt/tests/test_export.py

View workflow job for this annotation

GitHub Actions / lintrunner

FLAKE8 F811

redefinition of unused 'test_softmax' from line 97 See https://www.flake8rules.com/rules/F811.html.
_export_and_verify("softmax")

def test_mv3(self) -> None:

Check warning on line 127 in examples/nvidia/tensorrt/tests/test_export.py

View workflow job for this annotation

GitHub Actions / lintrunner

FLAKE8 F811

redefinition of unused 'test_mv3' from line 100 See https://www.flake8rules.com/rules/F811.html.
_export_and_verify("mv3")

def test_ic3(self) -> None:

Check warning on line 130 in examples/nvidia/tensorrt/tests/test_export.py

View workflow job for this annotation

GitHub Actions / lintrunner

FLAKE8 F811

redefinition of unused 'test_ic3' from line 115 See https://www.flake8rules.com/rules/F811.html.
_export_and_verify("ic3")
Loading