Skip to content

Index converter dynamic cases fix #3694

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
10 changes: 8 additions & 2 deletions py/torch_tensorrt/dynamo/conversion/aten_ops_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,19 @@ def index_dtype_validator(
for ind in index:
if ind is not None:
val = ind.meta.get("val")
if val is not None and val.dtype not in (torch.int32, torch.int64):
if val is not None and val.dtype not in (
torch.int32,
torch.int64,
torch.bool,
):
return False
return True


@dynamo_tensorrt_converter(
torch.ops.aten.index.Tensor, capability_validator=index_dtype_validator
torch.ops.aten.index.Tensor,
capability_validator=index_dtype_validator,
supports_dynamic_shapes=True,
)
@enforce_tensor_types(
{
Expand Down
62 changes: 60 additions & 2 deletions py/torch_tensorrt/dynamo/conversion/impl/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,65 @@ def select(
return layer.get_output(0)


def is_boolean_tensor(tensor: Union[TRTTensor, np.ndarray, torch.Tensor]) -> bool:
if isinstance(tensor, (TRTTensor)):
val = tensor.meta.get("val")
if val is not None and val.dtype is torch.bool:
return True
return isinstance(tensor, (torch.Tensor, np.ndarray)) and tensor.dtype == torch.bool


def expand_boolean_indices(
ctx: ConversionContext,
target: Target,
source_ir: Optional[SourceIR],
name: str,
input: TRTTensor,
indices: Sequence[Union[TRTTensor, np.ndarray, torch.Tensor]],
) -> Sequence[Union[TRTTensor, np.ndarray, torch.Tensor]]:
for i, ind in enumerate(indices):
if ind is not None and is_boolean_tensor(ind):
_LOGGER.debug(
f"Boolean index detected at position {i}, converting with nonzero()"
)

mask_tensor = get_trt_tensor(ctx, ind, name + f"_bool_mask_{i}")

nonzero_layer = ctx.net.add_non_zero(mask_tensor)
set_layer_name(
nonzero_layer, target, name + f"_bool_nonzero_{i}", source_ir
)
nonzero_indices = nonzero_layer.get_output(0)

# nonzero returns shape [N, dims], we need to extract dim i
if len(indices) == 1:
# x[mask] — 1D mask
squeeze_layer = ctx.net.add_shuffle(nonzero_indices)
squeeze_layer.reshape_dims = (-1,)
set_layer_name(
squeeze_layer,
target,
name + f"_bool_nonzero_squeeze_{i}",
source_ir,
)
squeezed_index = squeeze_layer.get_output(0)
ind = squeezed_index
else:
# Advanced multi-axis mask: extract index i from shape [N, D]
gather_axis = 1 # dim index
gather_layer = ctx.net.add_gather(
nonzero_indices,
get_trt_tensor(ctx, i, name + f"_dim_index_{i}"),
gather_axis,
)
set_layer_name(
gather_layer, target, name + f"_bool_nonzero_extract_{i}", source_ir
)
extracted_index = gather_layer.get_output(0)
ind = extracted_index
return indices


def index(
ctx: ConversionContext,
target: Target,
Expand All @@ -63,8 +122,6 @@ def index(
) -> TRTTensor:
adv_indx_indices = []
tensor_indices = []
# check if the input is dynamic
dynamic_shape = has_dynamic_shape(input.shape)
# is_numpy is a flag to specify if all the indices are numpy or torchTensor.
# If any is not this flag will be set to False
_LOGGER.debug(
Expand All @@ -78,6 +135,7 @@ def index(
# here we need to check if all the index are broadcastable
# if no, then we need to broadcast
last_index = None
indices = expand_boolean_indices(ctx, target, source_ir, name, input, indices)
for i, ind in enumerate(indices):
if ind is not None:
_LOGGER.debug(f"Shape of {i} index is {ind.shape}")
Expand Down
26 changes: 25 additions & 1 deletion tests/py/dynamo/conversion/test_index_aten.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,31 @@ def forward(self, input):
dtype=torch.float32,
),
]
self.run_test_with_dynamic_shape(TestModule(), input_specs)
self.run_test_with_dynamic_shape(
TestModule(), input_specs, use_dynamo_tracer=True
)


class TestIndexDynamicInputNonDynamicIndexConverter(DispatchTestCase):
def test_index_input_non_dynamic_index_dynamic(self):
class TestIndexWithRuntimeIndex(torch.nn.Module):
def forward(self, x):
mask = x > 0
idx = torch.nonzero(mask, as_tuple=True)
return torch.ops.aten.index.Tensor(x, idx)

input_specs = [
Input(
min_shape=(2, 2),
opt_shape=(2, 2),
max_shape=(8, 8),
dtype=torch.float32,
),
]
# In this case the index args[1] gets itself converted to a List of TRTTensors with use_dynamo_tracer=True
self.run_test_with_dynamic_shape(
TestIndexWithRuntimeIndex(), input_specs, use_dynamo_tracer=True
)


if __name__ == "__main__":
Expand Down
Loading