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).
Experiments:
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
Thomas