GB0207

Graph-Break Type

Short name describing what triggered the graph break

sort with non-constant keys

Context

Values or code snippet captured at the break point

str(self.key)

Explanation

Explanation of why the graph break was triggered

Cannot perform sort whose key comparison is not a compile-time constant. Key type: {self.key.python_type()}. Most notably, we cannot sort with Tensor or SymInt keys, but we can sort ints.

Hints

Hints on how to resolve the graph break

Additional Information

This graph break happens when you sort a sequence (via list.sort or sorted) using a key whose values are not known when the function is compiled. The most common case is sorting by a tensor-valued key: the resulting order would depend on the values inside the tensors, which torch.compile cannot determine while building the compiled function. Sorting by ordinary Python ints, floats, or strings is fine.

Example code that causes the graph break is:

import torch


@torch.compile(fullgraph=True)
def fn(x):
    parts = [x, x * 2, x * 0.5]
    parts.sort(key=lambda t: t.sum())  # key is a tensor -> non-constant
    return parts[0]


fn(torch.randn(3))

If the order you want is really determined by static metadata, sort by that constant value instead of by a tensor. Here each tensor is paired with a Python int priority and sorted on the priority:

import torch


@torch.compile(fullgraph=True)
def fn(x):
    parts = [(2, x), (1, x * 2), (3, x * 0.5)]
    parts.sort(key=lambda pair: pair[0])  # constant int key
    return parts[0][1]


fn(torch.randn(3))

If the ordering genuinely depends on tensor values, it cannot be decided while the function is compiled; move the sort outside the compiled region instead.

Click here to add Additional Info

Back to Registry