I would like to do X = M @ X
, but without allocating an extra matrix on the RHS.
So I tried torch.matmul(M, X, out=X)
and it seems to work.
I can even do torch.matmul(X, X, out=X)
and the results seem to come out right:
[ins] In [53]: x = torch.randn(4,4)
[ins] In [54]: torch.matmul(x, x)
Out[54]:
tensor([[ 7.7674, 2.4361, 1.5354, -1.0358],
[ 4.4382, -1.2255, -0.2265, 0.7528],
[-4.6821, 0.8569, -4.4141, -0.9833],
[-1.8084, 6.9762, 2.8231, 1.0764]])
[ins] In [55]: torch.matmul(x, x, out=x)
Out[55]:
tensor([[ 7.7674, 2.4361, 1.5354, -1.0358],
[ 4.4382, -1.2255, -0.2265, 0.7528],
[-4.6821, 0.8569, -4.4141, -0.9833],
[-1.8084, 6.9762, 2.8231, 1.0764]])
My question is whether it’s safe to rely on this behaviour? Or if I need to allocate a temporary tensor for the results, then copy them to X?