Developer Tools

PyTorch fixes Inductor bug causing silent wrong results on expanded tensors

A critical patch prevents data corruption in torch.compile when using index operations on expanded tensors.

Deep Dive

The PyTorch team has fixed a subtle but dangerous bug in the Inductor backend of torch.compile. The issue affected operations like index_add, index_copy, index_fill, and index_put when targeting expanded tensors—tensors created with .expand() that share physical storage via stride-0 dimensions. Because distinct logical indices can point to the same memory location, in-place writes could silently corrupt data. For example, writing into a [1,8] tensor expanded to [4,8] would accumulate values into the single backing row, then propagate that incorrect value to all rows.

The fix has two key parts. First, Inductor's reinplace pass now checks for definite internal overlap in candidate tensors and avoids converting functional writes into in-place mutations when overlap exists. Second, the index_fill reference decomposition was updated to preserve strides only for non-overlapping inputs, matching eager mode behavior. The commit (PR #184488) includes comprehensive regression tests covering all four index operations with both accumulate=True and accumulate=False variants. This ensures that torch.compile produces identical results to eager execution, eliminating a class of hard-to-detect bugs in production ML pipelines.

Key Points
  • Fixes silent wrong results for index_add, index_copy, index_fill, and index_put on expanded tensors with stride-0 dimensions
  • Prevents Inductor's reinplace pass from unsafely mutating tensors with definite internal overlap
  • Adds regression tests covering five index operation variants including expanded and non-expanded dimensions

Why It Matters

Ensures torch.compile produces correct numerical results, critical for reliability in production PyTorch deployments.

📬 Get the top 10 AI stories daily