iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
In current PyTorch main, torch.unravel_index accepts a shape containing a zero-sized dimension only when the input indices tensor is empty. If the indices contain any values, PyTorch rejects the call before coordinate arithmetic because a zero-sized shape has no elements to index.
What torch.unravel_index does
The function converts flat indices into coordinates for a requested shape. It returns one coordinate tensor for each shape dimension, and each output has the same shape as the input indices. The PyTorch API documentation and implementation describe this behavior and require shape dimensions to be nonnegative.
For example, unraveling indices for a two-dimensional shape produces a row-coordinate tensor and a column-coordinate tensor. A zero-sized dimension is valid as a shape value, but it means the shaped space contains no elements.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitchesHow empty and nonempty indices behave
| Shape | Indices | Current-main behavior |
|---|---|---|
| All dimensions positive | Empty or nonempty | Not the zero-dimension edge case; normal index validation and coordinate calculation apply. |
| At least one dimension is zero | Empty | Allowed; the function can return empty coordinate tensors. |
| At least one dimension is zero | Nonempty | Rejected before coordinate calculation with an error indicating that the shape cannot have zero dimensions when indices is non-empty. |
The key distinction is whether the indices tensor has zero elements, not whether its tensor rank is zero. Current implementation checks that dimensions are nonnegative and then requires either all dimensions to be positive or the indices tensor to be empty.
#1 Best Overall
Why the nonempty case must be rejected
A flat index must refer to an element in the requested shape. If any dimension is zero, there are no elements and therefore no valid flat index to map. Without an early guard, the coordinate calculation could reach division or modulo by zero. The contribution report describes adding validation before that arithmetic so the invalid combination produces a useful validation error instead.
What the contribution reports—and what it does not establish
The contribution author links the change to PyTorch PR #191092 and describes it as merged. That is the status reported by the contribution article; the available linked material does not independently establish the PR’s exact merge date or the first PyTorch release that includes it. The present behavior described here is the behavior of current main, not a claim about every released version.
Rank #2
Do not confuse that report with PR #189389, a separate change concerning validation and error wording for zero-sized shapes. Its diff includes a test that expects an error for a nonempty index with shape (2, 0). Current main phrases the condition as zero dimensions when indices is non-empty; error text associated with separate changes may differ.
Keep this separate from out-of-bounds index behavior
PyTorch issue #136736 reports an adjacent but distinct concern: out-of-bounds indices wrapping for a nonzero shape, reported against PyTorch 2.3.0. It does not describe the zero-dimension validation case, so conclusions about that issue should not be used to characterize the behavior covered here.
Quick Recap
Rank #4
Rank #3
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

