Ho right!
Yes, you can’t modify X inplace because it’s a leaf. And when you do Y = X[:, :, :dy, :dy], Y is actually a view into X. And so when you modify Y inplace by doing Y[yn, yc, yh, yw] = foo, you also modify X leading to the error.
The right way to fix that is to use .clone() to ensure you get new memory that is not a view of X: Y = X[:, :, :dy, :dy].clone() (should be slightly faster than the *1 you use).