Skip to content

Conversation

@lordaarush
Copy link

@lordaarush lordaarush commented Jan 17, 2026

Description

This PR fixes a failure in the Torch-TensorRT dynamo converter when lowering repeat() patterns that internally get rewritten into expand() under dynamic or symbolic shapes. In these situations, len(input_t.shape) may fail because symbolic dimensions are not standard Python sequences, leading to errors such as:

ValueError: len() should return >= 0

This was triggered by positional embedding patterns like:

x[..., None, None, :].repeat(batch, 1, H, W, 1)

Root cause:
After padding singleton dimensions, the converter asserted:
assert len(input_t.shape) == shape_rank
However, calling len() on symbolic shapes can fail or return invalid results, causing the converter to raise Python-level errors during lowering.

Fix:
Replaces the unsafe assert with a robust rank check that:

  • attempts to read the rank of input_t.shape safely,
  • falls back to the known initial_tensor_rank if len() is not supported, and
  • emits a clear RuntimeError if ranks still mismatch.

This preserves all existing expand() semantics. There are no behavior changes for static shapes.

A minimal regression test has been added at:
tests/dynamo/test_repeat_expand_repro.py
It reproduces the issue in #3972 and is skipped automatically when CUDA + Torch-TensorRT are unavailable.

Fixes: #3972

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code where needed
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally where applicable (test is skipped when Torch-TensorRT runtime is unavailable)
  • I have added the relevant labels to my PR so that reviewers are notified

@meta-cla meta-cla bot added the cla signed label Jan 17, 2026
@github-actions github-actions bot added component: tests Issues re: Tests component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: converters Issues re: Specific op converters component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Jan 17, 2026
@github-actions github-actions bot requested a review from zewenli98 January 17, 2026 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug] Shape mismatch when using repeat

1 participant