torch.func transforms operate on functions. In particular, to compute the NTK,
we will need a function that accepts the parameters of the model and a single
input (as opposed to a batch of inputs!) and returns a single output.
We’ll use torch.func.functional_call, which allows us to call an nn.Module
using different parameters/buffers, to help accomplish the first step.
Keep in mind that the model was originally written to accept a batch of input
data points. In our CNN example, there are no inter-batch operations. That
is, each data point in the batch is independent of other data points. With
this assumption in mind, we can easily generate a function that evaluates the
model on a single data point:
net=CNN().to(device)# Detaching the parameters because we won't be calling Tensor.backward().params={k:v.detach()fork,vinnet.named_parameters()}deffnet_single(params,x):returnfunctional_call(net,params,(x.unsqueeze(0),)).squeeze(0)
Compute the NTK: method 1 (Jacobian contraction)
We’re ready to compute the empirical NTK. The empirical NTK for two data
points x1 and x2 is defined as the matrix product between the Jacobian
of the model evaluated at x1 and the Jacobian of the model evaluated at
x2:
Jnet(x1)JnetT(x2)
In the batched case where x1 is a batch of data points and x2 is a
batch of data points, then we want the matrix product between the Jacobians
of all combinations of data points from x1 and .
The first method consists of doing just that - computing the two Jacobians,
and contracting them. Here’s how to compute the NTK in the batched case:
/usr/local/lib/python3.12/dist-packages/triton/knobs.py:504:UserWarning: TRITON_DEFAULT_FP_FUSION is unset: defaulting to FP fusion ENABLED for backward compatibility inTriton3.9.The default will change to DISABLED inTriton3.10;set TRITON_DEFAULT_FP_FUSION=1 to keep fusion enabled or=0 to adopt the new default early and silence this warning.warn_once(FP_FUSION_DEFAULT_WARNING)
torch.
In some cases, you may only want the diagonal or the trace of this quantity,
especially if you know beforehand that the network architecture results in an
NTK where the non-diagonal elements can be approximated by zero. It’s easy to
adjust the above function to do that:
The asymptotic time complexity of this method is NO[FP] (time to
compute the Jacobians) + N2O2P (time to contract the Jacobians),
where N is the batch size of x1 and ,
is the model’s output size, is the total number of parameters, and
is the cost of a single forward pass through the model. See
section 3.2 in
for details.
Compute the NTK: method 2 (NTK-vector products)
The next method we will discuss is a way to compute the NTK using NTK-vector
products.
This method reformulates NTK as a stack of NTK-vector products applied to
columns of an identity matrix IO of size O×O
(where O is the output size of the model):
where eo∈RO are column vectors of the identity matrix
IO.
Let vjpo=JnetT(x2)eo. We can use
a vector-Jacobian product to compute this.
This suggests that we can use a combination of reverse-mode AD (to compute
the vector-Jacobian product) and forward-mode AD (to compute the
Jacobian-vector product) to compute the NTK.
/usr/local/lib/python3.12/dist-packages/torch/jit/_script.py:1491:FutureWarning: `torch.jit.script` is deprecated.Please switch to `torch.compile` or `torch.export`.
warnings.warn(
Our code for empirical_ntk_ntk_vps looks like a direct translation from
the math above! This showcases the power of function transforms: good luck
trying to write an efficient version of the above by only using
torch.autograd.grad.
The asymptotic time complexity of this method is N2O[FP], where
N is the batch size of x1 and x2, is the
model’s output size, and is the cost of a single forward pass
through the model. Hence this method performs more forward passes through the
network than method 1, Jacobian contraction ( instead of
), but avoids the contraction cost altogether (no
term, where is the total number of model’s parameters). Therefore,
this method is preferable when is large relative to ,
such as fully-connected (not convolutional) models with many outputs .
Memory-wise, both methods should be comparable. See section 3.3 in
for details.
Total running time of the script: (0 minutes 1.375 seconds)