Skip to content
fn

gradient_in_dtype_of

→TensorImplPtr
int gradient_in_dtype_of(const int & grad, const int & like)

A graph-mode gradient in the dtype of the tensor it is for.

A complex gradient reaching a real tensor keeps its real part — fft of a real input answers complex, and d/dx of a real x is real — and any other difference is a cast. AccumulateGrad::apply does this to a storage; the graph-mode paths (backward(create_graph=True) and grad(create_graph=True)) returned the gradient as it came, complex where the storage path was real.

Parameters

gradconst TensorImplPtr&
The gradient, possibly null.
likeconst TensorImplPtr&
The tensor it is the gradient of.

Returns

TensorImplPtr

grad itself when the dtypes already agree, else a recorded projection or cast of it.