facebookresearch/detectron2 · error · RuntimeError

Unsupported: ONNX export of repeat_interleave for unknown re

Error message

Unsupported: ONNX export of repeat_interleave for unknown repeats size.

What it means

Raised during ONNX export when torch.onnx.export encounters a repeat_interleave op whose `repeats` tensor has unknown (dynamic/None) shape in the traced graph. The detectron2 testing module monkey-patches PyTorch 1.11's opset9 symbolic for repeat_interleave, and it refuses to build ONNX nodes without concrete repeats sizes.

Source

Thrown at detectron2/utils/testing.py:392

    input = self
    # if dim is None flatten
    # By default, use the flattened input array, and return a flat output array
    if sym_help._is_none(dim):
        input = sym_help._reshape_helper(g, self, g.op("Constant", value_t=torch.tensor([-1])))
        dim = 0
    else:
        dim = sym_help._maybe_get_scalar(dim)

    repeats_dim = sym_help._get_tensor_rank(repeats)
    repeats_sizes = sym_help._get_tensor_sizes(repeats)
    input_sizes = sym_help._get_tensor_sizes(input)
    if repeats_dim is None:
        raise RuntimeError(
            "Unsupported: ONNX export of repeat_interleave for unknown " "repeats rank."
        )
    if repeats_sizes is None:
        raise RuntimeError(
            "Unsupported: ONNX export of repeat_interleave for unknown " "repeats size."
        )
    if input_sizes is None:
        raise RuntimeError(
            "Unsupported: ONNX export of repeat_interleave for unknown " "input size."
        )

    input_sizes_temp = input_sizes.copy()
    for idx, input_size in enumerate(input_sizes):
        if input_size is None:
            input_sizes[idx], input_sizes_temp[idx] = 0, -1

    # Cases where repeats is an int or single value tensor
    if repeats_dim == 0 or (repeats_dim == 1 and repeats_sizes[0] == 1):
        if not sym_help._is_tensor(repeats):
            repeats = g.op("Constant", value_t=torch.LongTensor(repeats))
        if input_sizes[dim] == 0:
            return sym_help._onnx_opset_unsupported_detailed(

View on GitHub (pinned to a2f4a8771a)

Solutions

  1. Make the repeats tensor a constant with known shape (e.g. torch.as_tensor(repeats) with concrete values) before export
  2. Avoid repeat_interleave in the exported graph: replace with expand + reshape or torch.repeat with static factors
  3. Export with a fixed input shape (no dynamic_axes) so all sizes are known
  4. Pin PyTorch to a version whose ONNX symbolic supports the pattern (this shim targets 1.11 opset9)

Example fix

// before
y = x.repeat_interleave(repeats, dim=0)  # repeats shape unknown
// after
repeats = torch.tensor([2, 2, 2])  # constant, statically known
y = x.repeat_interleave(repeats, dim=0)
Defensive patterns

Strategy: validation

Validate before calling

sizes = torch._C._jit_pass_onnx? None
# practical check before export:
def repeats_shape_known(repeats):
    return repeats is not None and isinstance(repeats, torch.Tensor) and repeats.dim() <= 1 and repeats.shape[0] is not None

Type guard

def is_exportable_repeats(r) -> bool:
    return isinstance(r, torch.Tensor) and r.dim() <= 1 and all(s is not None for s in r.shape)

Try / catch

try:
    torch.onnx.export(model, dummy, out)
except RuntimeError as e:
    if 'repeat_interleave' in str(e):
        # replace with static repeat or fixed-shape export
        ...

Prevention

When it happens

Trigger: Exporting a model to ONNX (torch.onnx.export / TrainerExport hooks) where repeat_interleave is called with a repeats tensor whose shape cannot be statically inferred (e.g. repeats computed from data-dependent ops, or dynamic batch axis dims).

Common situations: Exporting RetinaNet/mask heads or models using repeat_interleave with dynamic shapes; using dynamic_axes in torch.onnx.export so shape info is dropped; PyTorch version mismatch where sym_help._get_tensor_sizes returns None.


AI-assisted analysis of facebookresearch/detectron2@a2f4a8771a (2026-08-27). Data as JSON: /api/errors/d2596f6804122ce9. Report an issue: GitHub.