Skip to content

fix: Remove type casting in matmul and add scalar tensor conversion #3713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
9 changes: 8 additions & 1 deletion py/torch_tensorrt/dynamo/conversion/impl/elementwise/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import warnings
from typing import Any, Callable, Optional, Union

import numpy as np
import tensorrt as trt
import torch
from torch.fx.node import Target
Expand Down Expand Up @@ -103,6 +102,14 @@ def convert_binary_elementwise(
rhs_dtype = rhs_val.dtype
is_rhs_trt_tensor = True

# Handle scalar tensor type promotion for elementwise operations
# When one operand is a scalar tensor (0-dimensional), promote its dtype to match the other operand
# This ensures consistent type handling in Torch elementwise operations
if isinstance(lhs_val, torch.Tensor) and len(lhs_val.shape) == 0:
lhs_dtype = rhs_dtype
if isinstance(rhs_val, torch.Tensor) and len(rhs_val.shape) == 0:
rhs_dtype = lhs_dtype

if not is_lhs_trt_tensor and not is_rhs_trt_tensor:
warnings.warn(
f"Both operands of the binary elementwise op {name} "
Expand Down
11 changes: 0 additions & 11 deletions py/torch_tensorrt/dynamo/conversion/impl/matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from torch_tensorrt.dynamo.conversion._ConversionContext import ConversionContext
from torch_tensorrt.dynamo.conversion.converter_utils import (
broadcast,
cast_trt_tensor,
get_trt_tensor,
set_layer_name,
)
Expand Down Expand Up @@ -48,16 +47,6 @@ def matrix_multiply(
input, other = broadcast(
ctx, input, other, f"{name}_input", f"{name}_other", preset_diff
)
if ctx.net.get_flag(trt.NetworkDefinitionCreationFlag.STRONGLY_TYPED):
promoted_type = _enums.dtype._from(
torch.promote_types(
_enums.dtype._from(input.dtype).to(torch.dtype),
_enums.dtype._from(other.dtype).to(torch.dtype),
)
)
trt_promoted_type = promoted_type.to(trt.DataType)
input = cast_trt_tensor(ctx, input, trt_promoted_type, f"{name}_input_casted")
other = cast_trt_tensor(ctx, other, trt_promoted_type, f"{name}_other_casted")

layer = ctx.net.add_matrix_multiply(input, input_matrix_op, other, other_matrix_op)
set_layer_name(layer, target, name, source_ir)
Expand Down
Loading