N and M are two integers that we will use for the dimension of the input and output space respectively.
f:RN→RM is our basic real-to-real function such that y=f(x).
g:CN→RM is our basic complex-to-real function such that y=g(z).
For the simple real-to-real case, we write as Jf the Jacobian matrix associated with f of size M×N.
This matrix contains all the partial derivatives such that the entry at position (i,j) contains ∂xj∂yi.
Backward mode AD is then computing, for a given vector v of size M, the quantity vTJf.
Forward mode AD on the other hand is computing, for a given vector u of size N, the quantity Jfu.
For functions that contain complex values, the story is a lot more complex. We only provide the gist here and the full description can be found at Autograd for Complex Numbers.
The constraints to satisfy complex differentiability (Cauchy-Riemann equations) are too restrictive for all real-valued loss functions, so we instead opted to use Wirtinger calculus.
In a basic setting of Wirtinger calculus, the chain rule requires access to both the Wirtinger derivative (called W below) and the Conjugate Wirtinger derivative (called CW below).
Both W and CW need to be propagated because in general, despite their name, one is not the complex conjugate of the other.
To avoid having to propagate both values, for backward mode AD, we always work under the assumption that the function whose derivative is being calculated is either a real-valued function or is part of a bigger real-valued function. This assumption means that all the intermediary gradients we compute during the backward pass are also associated with real-valued functions.
In practice, this assumption is not restrictive when doing optimization as such problem require real-valued objectives (as there is no natural ordering of the complex numbers).
Under this assumption, using W and CW definitions, we can show that W=CW∗ (we use ∗ to denote complex conjugation here) and so only one of the two values actually need to be “backwarded through the graph” as the other one can easily be recovered.
To simplify internal computations, PyTorch uses 2∗CW as the value it backwards and returns when the user asks for gradients.
Similarly to the real case, when the output is actually in RM, backward mode AD does not compute 2∗CW but only vT(2∗CW) for a given vector v∈RM.
For forward mode AD, we use a similar logic, in this case, assuming that the function is part of a larger function whose input is in R. Under this assumption, we can make a similar claim that every intermediary result corresponds to a function whose input is in R and in this case, using W and CW definitions, we can show that W=CW for the intermediary functions.
To make sure the forward and backward mode compute the same quantities in the elementary case of a one dimensional function, the forward mode also computes 2∗CW.
Similarly to the real case, when the input is actually in RN, forward mode AD does not compute 2∗CW but only (2∗CW)u for a given vector u∈RN.
This formula easily generalizes for multiple outputs (M>1) by having ∂x∂y be a column vector of size M×1 like f(x+eps).
In that case, the above formula can be reused as-is and approximates the full Jacobian matrix with only two evaluations of the user function (namely f(x+eps) and f(x−eps)).
It is more computationally expensive to handle the case with multiple inputs (N>1). In this scenario, we loop over all the inputs one after the other and apply the eps perturbation for each element of x one after the other. This allows us to reconstruct the Jf matrix column by column.
hr(q)hi(q):=real(f(q)):=imag(f(q)) where q∈P.
We then do a basic gradcheck for both hr and hi using either the real-to-real or complex-to-real case described above, depending on P.
Note that, the code, as of time of writing, does not create these functions explicitly but perform the chain rule with the real or imag functions manually by passing the grad_out arguments to the different functions.
When grad_out=1, then we are considering hr.
When grad_out=1j, then we are considering hi.
where v∈RM, ur∈RN and ui∈RN.
R→R )
w_d
=
0.5
*
(
ds_dx
-
ds_dy
*
1
j
)
d
[
d_idx
]
=
grad_out
.
conjugate
()
*
conj_w_d
+
grad_out
*
w_d
.
conj
()