xnn.common.models.fast.compile_twice_differentiable#

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

fn compiled by torch.compile() and differentiable twice.

Compiled code has a first derivative but no second, which a force loss needs. Here fn runs inside an autograd function whose backward is the compiled vector-Jacobian product of fn (torch.func.vjp()), itself an autograd function whose backward is the compiled vector-Jacobian product of that. Each order is a few fused kernels, as in evaluation mode. Third derivatives are not supported.

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 (index tensors, sizes). Default 0.

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

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

Returns:

fn with the same arguments and outputs.

Return type:

callable