pytorch / pytorch/pytorch

[Inductor] Cat input consumed by another input of the same cat is misclassified as an external consumer

Open
#194,858 0 comments 0 reactions 0 assignees View on GitHub
bot-triaged module: inductor module: performance module: regression oncall: pt2 triaged
Dominant language
Python
Stars
103k
Forks
29.5k
PR merge metrics
PR metrics pending

Description

### 🐛 Describe the bug

## Summary

The behavior change of cat was introduced by `[d8ce9e08ff6c2b5608dfc073fd985ee7e17aeae9](https://github.com/pytorch/pytorch/commit/d8ce9e08ff6c2b5608dfc073fd985ee7e17aeae9)`, which correctly avoids `pointwise_cat` when a cat input has consumers outside the cat. However, the heuristic also counts another direct input of the same cat as an external consumer.

For a pattern such as:

https://github.com/huggingface/transformers/blob/6ec0f83563f56335d4227354614459a01d76ba0d/src/transformers/models/reformer/modeling_reformer.py#L733-L746

```
rotated = ...
negated = -rotated
concatenated = torch.cat((rotated, negated), dim=-1)
result = torch.argmax(concatenated, dim=-1)
```

the FX graph is:

```
%rotated = ...
%negated = aten.neg(%rotated)
%cat = aten.cat([%rotated, %negated], dim=-1)
%result = aten.argmax(%cat, dim=-1)
```

The relevant user relationships are:

```
rotated.users = {negated, cat}
negated.users = {cat}
cat.users = {argmax}
```

`negated` is a pointwise user of `rotated`, but it is also a direct input to the same `cat`. It is not an independent consumer outside the cat computation.

## Minimal reproducer

```
import torch

from torch._inductor.utils import run_and_get_code

def fn(x):
rotated = torch.sin(x)
concatenated = torch.cat((rotated, -rotated), dim=-1)
return torch.argmax(concatenated, dim=-1)

x = torch.randn(
128,
64,
device="cuda",
dtype=torch.float16,
)

compiled = torch.compile(fn)
result, codes = run_and_get_code(compiled, x)

torch.testing.assert_close(result, fn(x))

code = "\n".join(codes)
print("ConcatKernel materialization:", "reinterpret_tensor" in code)
```

Graph:

```jsx
def call(self, args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (128, 64), (64, 1), 'input')
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
arg0_1 = copy_if_misaligned(arg0_1)
buf2 = empty_strided_cuda((128, 128), (128, 1), torch.float16)
buf0 = reinterpret_tensor(buf2, (128, 64), (128, 1), 0) # alias
buf1 = reinterpret_tensor(buf2, (128, 64), (128, 1), 64) # alias
# Topologically Sorted Source Nodes: [rotated, neg], Original ATen: [aten.sin, aten.neg]
# [Provenance debug handles] triton_poi_fused_neg_sin_0:1
raw_stream0 = get_raw_stream(0)
triton_poi_fused_neg_sin_0.run(arg0_1, buf0, buf1, 8192, stream=raw_stream0)
del arg0_1
buf3 = empty_strided_cuda((128, ), (1, ), torch.int64)
# Topologically Sorted Source Nodes: [argmax], Original ATen: [aten.argmax]
# [Provenance debug handles] triton_per_fused_argmax_1:2
raw_stream0 = get_raw_stream(0)
triton_per_fused_argmax_1.run(buf2, buf3, 128, 128, stream=raw_stream0)
del buf0
del buf1
del buf2
return (buf3, )

```

## Expected behavior

For:

```
torch.cat((rotated, -rotated), dim=-1)
```

the lowering should remain eligible for `pointwise_cat`.

### Error logs

_No response_

### Versions

```
Collecting environment information...
PyTorch version: 2.14.0.dev20260719+cu130
Is debug build: False
CUDA used to build PyTorch: 13.0
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
Clang version: Could not collect
CMake version: version 4.4.0
Libc version: glibc-2.35

Python version: 3.12.13 | packaged by conda-forge | (main, Mar 5 2026, 16:50:00) [GCC 14.3.0] (64-bit runtime)
Python platform: Linux-5.19.0-32-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 13.0.88
CUDA_MODULE_LOADING set to:
GPU models and configuration:
GPU 0: NVIDIA GeForce RTX 4080 SUPER
GPU 1: NVIDIA GeForce RTX 4070 Ti SUPER

Nvidia driver version: 580.173.02
cuDNN version: Could not collect
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: False
Caching allocator config: N/A

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 46 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 24
On-line CPU(s) list: 0-23
Vendor ID: GenuineIntel
Model name: Intel(R) Core(TM) Ultra 9 285K
CPU family: 6
Model: 198
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 24
Stepping: 2
CPU max MHz: 5100.0000
CPU min MHz: 800.0000
BogoMIPS: 7372.80
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single intel_ppin ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni wbnoinvd dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid bus_lock_detect movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 768 KiB (20 instances)
L1i cache: 1.3 MiB (20 instances)
L2 cache: 40 MiB (12 instances)
L3 cache: 36 MiB (1 instance)
NUMA node(s): 1
NUMA node0 CPU(s): 0-23
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Retbleed: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected

Versions of relevant libraries:
[pip3] numpy==2.5.1
[pip3] nvidia-cublas==13.1.1.3
[pip3] nvidia-cuda-cupti==13.0.85
[pip3] nvidia-cuda-nvrtc==13.0.88
[pip3] nvidia-cuda-runtime==13.0.96
[pip3] nvidia-cudnn-cu13==9.24.0.43
[pip3] nvidia-cufft==12.0.0.61
[pip3] nvidia-curand==10.4.0.35
[pip3] nvidia-cusolver==12.0.4.66
[pip3] nvidia-cusparse==12.6.3.3
[pip3] nvidia-cusparselt-cu13==0.8.1
[pip3] nvidia-nccl-cu13==2.30.7
[pip3] nvidia-nvjitlink==13.3.33
[pip3] nvidia-nvtx==13.0.85
[pip3] optree==0.19.1
[pip3] torch==2.14.0.dev20260719+cu130
[pip3] torchbench==0.1
[pip3] torchvision==0.29.0.dev20260720+cu130
[pip3] triton==3.8.0+git4774139f
[conda] numpy 2.5.1 pypi_0 pypi
[conda] nvidia-cublas 13.1.1.3 pypi_0 pypi
[conda] nvidia-cuda-cupti 13.0.85 pypi_0 pypi
[conda] nvidia-cuda-nvrtc 13.0.88 pypi_0 pypi
[conda] nvidia-cuda-runtime 13.0.96 pypi_0 pypi
[conda] nvidia-cudnn-cu13 9.24.0.43 pypi_0 pypi
[conda] nvidia-cufft 12.0.0.61 pypi_0 pypi
[conda] nvidia-curand 10.4.0.35 pypi_0 pypi
[conda] nvidia-cusolver 12.0.4.66 pypi_0 pypi
[conda] nvidia-cusparse 12.6.3.3 pypi_0 pypi
[conda] nvidia-cusparselt-cu13 0.8.1 pypi_0 pypi
[conda] nvidia-nccl-cu13 2.30.7 pypi_0 pypi
[conda] nvidia-nvjitlink 13.3.33 pypi_0 pypi
[conda] nvidia-nvtx 13.0.85 pypi_0 pypi
[conda] optree 0.19.1 pypi_0 pypi
[conda] torch 2.14.0.dev20260719+cu130 pypi_0 pypi
[conda] torchbench 0.1 pypi_0 pypi
[conda] torchvision 0.29.0.dev20260720+cu130 pypi_0 pypi
[conda] triton 3.8.0+git4774139f pypi_0 pypi

```

cc @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @aakhundov @coconutruben @jataylo

Contributor guide

Open the contributing guide

Research direction

Start with the Inductor cat-lowering heuristic described in the issue and run the provided torch.compile minimal reproducer. Trace how users of cat inputs are classified, then verify that a pointwise user which is also another input to the same cat is not treated as external; done means this pattern remains eligible for pointwise_cat.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.