Is multiplication by 1 a no-op

If I do sth like x * (y if condition else 1), will multiplication by 1 skip copying / allocating new memory?

No, you would need to do x * y if condition else x (if short circuits, so x * y will not be evaluated if the condition is false).

Multiplication is not a no-op

x = torch.randn(12)
y = x * 1
print(x.data_ptr(), y.data_ptr())

if short-circuits:

x = []
print(x[1] if len(x) > 0 else None)

Note that skipping allocation is not a good option here:

  • == 1 is fishy for floating point (see the thread with a mask the other day),
  • people will use + 0 or * 1 for getting copies…

Best regards


Yeah, maybe fixing this would require the user also casing for 1, if an inplace op is used after multiplication.

Let’s just say there is nothing to fix. :slight_smile: