jax.fwd_and_bwd#
- jax.fwd_and_bwd(fun, argnums, has_aux=False, jitted=True)[source]#
Creates functions
fwdandbwdcorresponding to the forward and backward pass of a given functionfun. The forward functionfwd(*args)functionally behaves much likey, fun_vjp = jax.vjp(fun, *args), but allows reuse of the backward functionbwdacross multiple iterations, which is useful to avoid recompilation when the forward and backward do not end up in a single jitted function:>>> import jax >>> >>> x = W = cot_out = jax.numpy.ones((4,4)) >>> >>> def f(x, W): ... return x @ W ... >>> f_jitted = jax.jit(f) >>> for i in range(3): ... y, f_vjp = jax.vjp(f_jitted, x, W) ... cot_x, cot_W = f_vjp(cot_out) # not jitted ... cot_x, cot_W = jax.jit(f_vjp)(cot_out) # recompiles on every iteration ... >>> fwd, bwd = jax.fwd_and_bwd(f, argnums=(0,1)) >>> for i in range(3): ... y, residuals = fwd(x, W) ... cot_x, cot_W = bwd(residuals, cot_out) # jitted, compiles once ...
- Parameters:
fun (Callable) – Function to produce a forward and backward of.
argnums (int | Sequence[int]) – Integer or sequence of integers. Specifies which positional argument(s) to differentiate with respect to.
has_aux (bool) – Optional, bool. Indicates whether
funreturns a pair where the first element is considered the output of the mathematical function to be differentiated and the second element is auxiliary data. Default False.jitted (bool) – Optional, bool. Indicates whether to return the
jax.jitof forward and backward. Note that jit-ing only the backward but not the forward will result in the backward recompiling on every invocation, so we default to jit-ing both.
- Returns:
The two functions,
fwdandbwd.If
has_auxisFalse,fwd(*primals)returns a tuple(primals_out, residuals), whereprimals_outisfun(*primals). Ifhas_auxisTrue, returns a(primals_out, residuals, aux)tuple whereauxis the auxiliary data returned byfun.bwdis a function fromresidualsand a cotangent vector with the same shape asprimals_outto a tuple of cotangent vectors with the same number and shapes as theprimalsdesignated byargnums, representing the vector-Jacobian product offunevaluated atprimals.- Return type: