xnn.common.models.fast.compile_differentiable#

xnn.common.models.fast.compile_differentiable(fn, n_inputs, n_constants=0, dynamic=True, options=None)[source]#

fn compiled by torch.compile(), with a fast first derivative and a second one on request.

compile_twice_differentiable() recomputes fn inside every backward pass, the price of a backward that can itself be differentiated. Here the forward pass keeps the compiled graph of fn (as torch.compile() does), and a backward pass runs its compiled backward. A backward pass that builds a graph (create_graph=True), or a second backward pass through the same graph (retain_graph=True), takes the vector-Jacobian product of compile_twice_differentiable(), which agrees with the compiled backward to rounding; second derivatives stay available.

Parameters:
  • fn (callable) – A function of n_inputs tensors returning a tensor or a tuple of tensors, written in differentiable torch operations.

  • n_inputs (int) – The number of its tensor arguments that are differentiated.

  • n_constants (int, optional) – The number of further arguments, after the inputs, that are not differentiated. Default 0.

  • dynamic (bool, optional) – Compile with symbolic sizes (default) or for the sizes of the first call.

  • options (dict, optional) – Inductor options of the compiled functions.

Returns:

fn with the same arguments and outputs.

Return type:

callable