Yeah, It’s working on tensor, but if they are variable, diff[mask] = diff[mask] ** 2 is a in-place operation, and when backwards, it will raise RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.