{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# For tips on running notebooks in Google Colab, see\n",
    "# https://docs.pytorch.org/tutorials/beginner/colab\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Jacobians, Hessians, hvp, vhp, and more: composing function transforms\n",
    "\n",
    "Computing jacobians or hessians are useful in a number of\n",
    "non-traditional deep learning models. It is difficult (or annoying) to\n",
    "compute these quantities efficiently using PyTorch\\'s regular autodiff\n",
    "APIs (`Tensor.backward()`, `torch.autograd.grad`). PyTorch\\'s\n",
    "[JAX-inspired](https://github.com/google/jax) [function transforms\n",
    "API](https://pytorch.org/docs/master/func.html) provides ways of\n",
    "computing various higher-order autodiff quantities efficiently.\n",
    "\n",
    "<div style=\"background-color: #54c7ec; color: #fff; font-weight: 700; padding-left: 10px; padding-top: 5px; padding-bottom: 5px\"><strong>NOTE:</strong></div>\n",
    "\n",
    "<div style=\"background-color: #f3f4f7; padding-left: 10px; padding-top: 10px; padding-bottom: 10px; padding-right: 10px\">\n",
    "\n",
    "<p>This tutorial requires PyTorch 2.0.0 or later.</p>\n",
    "\n",
    "</div>\n",
    "\n",
    "## Computing the Jacobian\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "from functools import partial\n",
    "_ = torch.manual_seed(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s start with a function that we\\'d like to compute the jacobian of.\n",
    "This is a simple linear function with non-linear activation.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def predict(weight, bias, x):\n",
    "    return F.linear(x, weight, bias).tanh()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s add some dummy data: a weight, a bias, and a feature vector x.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "D = 16\n",
    "weight = torch.randn(D, D)\n",
    "bias = torch.randn(D)\n",
    "x = torch.randn(D)  # feature vector"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s think of `predict` as a function that maps the input `x` from\n",
    "$R^D \\to R^D$. PyTorch Autograd computes vector-Jacobian products. In\n",
    "order to compute the full Jacobian of this $R^D \\to R^D$ function, we\n",
    "would have to compute it row-by-row by using a different unit vector\n",
    "each time.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def compute_jac(xp):\n",
    "    jacobian_rows = [torch.autograd.grad(predict(weight, bias, xp), xp, vec)[0]\n",
    "                     for vec in unit_vectors]\n",
    "    return torch.stack(jacobian_rows)\n",
    "\n",
    "xp = x.clone().requires_grad_()\n",
    "unit_vectors = torch.eye(D)\n",
    "\n",
    "jacobian = compute_jac(xp)\n",
    "\n",
    "print(jacobian.shape)\n",
    "print(jacobian[0])  # show first row"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Instead of computing the jacobian row-by-row, we can use PyTorch\\'s\n",
    "`torch.vmap` function transform to get rid of the for-loop and vectorize\n",
    "the computation. We can't directly apply `vmap` to\n",
    "`torch.autograd.grad`; instead, PyTorch provides a `torch.func.vjp`\n",
    "transform that composes with `torch.vmap`:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch.func import vmap, vjp\n",
    "\n",
    "_, vjp_fn = vjp(partial(predict, weight, bias), x)\n",
    "\n",
    "ft_jacobian, = vmap(vjp_fn)(unit_vectors)\n",
    "\n",
    "# let's confirm both methods compute the same result\n",
    "assert torch.allclose(ft_jacobian, jacobian)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In a later tutorial a composition of reverse-mode AD and `vmap` will\n",
    "give us per-sample-gradients. In this tutorial, composing reverse-mode\n",
    "AD and `vmap` gives us Jacobian computation! Various compositions of\n",
    "`vmap` and autodiff transforms can give us different interesting\n",
    "quantities.\n",
    "\n",
    "PyTorch provides `torch.func.jacrev` as a convenience function that\n",
    "performs the `vmap-vjp` composition to compute jacobians. `jacrev`\n",
    "accepts an `argnums` argument that says which argument we would like to\n",
    "compute Jacobians with respect to.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch.func import jacrev\n",
    "\n",
    "ft_jacobian = jacrev(predict, argnums=2)(weight, bias, x)\n",
    "\n",
    "# Confirm by running the following:\n",
    "assert torch.allclose(ft_jacobian, jacobian)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s compare the performance of the two ways to compute the jacobian.\n",
    "The function transform version is much faster (and becomes even faster\n",
    "the more outputs there are).\n",
    "\n",
    "In general, we expect that vectorization via `vmap` can help eliminate\n",
    "overhead and give better utilization of your hardware.\n",
    "\n",
    "`vmap` does this magic by pushing the outer loop down into the\n",
    "function\\'s primitive operations in order to obtain better performance.\n",
    "\n",
    "Let\\'s make a quick function to evaluate performance and deal with\n",
    "microseconds and milliseconds measurements:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def get_perf(first, first_descriptor, second, second_descriptor):\n",
    "    \"\"\"takes torch.benchmark objects and compares delta of second vs first.\"\"\"\n",
    "    faster = second.times[0]\n",
    "    slower = first.times[0]\n",
    "    gain = (slower-faster)/slower\n",
    "    if gain < 0: gain *=-1\n",
    "    final_gain = gain*100\n",
    "    print(f\" Performance delta: {final_gain:.4f} percent improvement with {second_descriptor} \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And then run the performance comparison:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch.utils.benchmark import Timer\n",
    "\n",
    "without_vmap = Timer(stmt=\"compute_jac(xp)\", globals=globals())\n",
    "with_vmap = Timer(stmt=\"jacrev(predict, argnums=2)(weight, bias, x)\", globals=globals())\n",
    "\n",
    "no_vmap_timer = without_vmap.timeit(500)\n",
    "with_vmap_timer = with_vmap.timeit(500)\n",
    "\n",
    "print(no_vmap_timer)\n",
    "print(with_vmap_timer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s do a relative performance comparison of the above with our\n",
    "`get_perf` function:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "get_perf(no_vmap_timer, \"without vmap\",  with_vmap_timer, \"vmap\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Furthermore, it's pretty easy to flip the problem around and say we want\n",
    "to compute Jacobians of the parameters to our model (weight, bias)\n",
    "instead of the input\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# note the change in input via ``argnums`` parameters of 0,1 to map to weight and bias\n",
    "ft_jac_weight, ft_jac_bias = jacrev(predict, argnums=(0, 1))(weight, bias, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reverse-mode Jacobian (`jacrev`) vs forward-mode Jacobian (`jacfwd`)\n",
    "\n",
    "We offer two APIs to compute jacobians: `jacrev` and `jacfwd`:\n",
    "\n",
    "-   `jacrev` uses reverse-mode AD. As you saw above it is a composition\n",
    "    of our `vjp` and `vmap` transforms.\n",
    "-   `jacfwd` uses forward-mode AD. It is implemented as a composition of\n",
    "    our `jvp` and `vmap` transforms.\n",
    "\n",
    "`jacfwd` and `jacrev` can be substituted for each other but they have\n",
    "different performance characteristics.\n",
    "\n",
    "As a general rule of thumb, if you're computing the jacobian of an\n",
    "$R^N \\to R^M$ function, and there are many more outputs than inputs (for\n",
    "example, $M > N$) then `jacfwd` is preferred, otherwise use `jacrev`.\n",
    "There are exceptions to this rule, but a non-rigorous argument for this\n",
    "follows:\n",
    "\n",
    "In reverse-mode AD, we are computing the jacobian row-by-row, while in\n",
    "forward-mode AD (which computes Jacobian-vector products), we are\n",
    "computing it column-by-column. The Jacobian matrix has M rows and N\n",
    "columns, so if it is taller or wider one way we may prefer the method\n",
    "that deals with fewer rows or columns.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch.func import jacrev, jacfwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, let\\'s benchmark with more inputs than outputs:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Din = 32\n",
    "Dout = 2048\n",
    "weight = torch.randn(Dout, Din)\n",
    "\n",
    "bias = torch.randn(Dout)\n",
    "x = torch.randn(Din)\n",
    "\n",
    "# remember the general rule about taller vs wider... here we have a taller matrix:\n",
    "print(weight.shape)\n",
    "\n",
    "using_fwd = Timer(stmt=\"jacfwd(predict, argnums=2)(weight, bias, x)\", globals=globals())\n",
    "using_bwd = Timer(stmt=\"jacrev(predict, argnums=2)(weight, bias, x)\", globals=globals())\n",
    "\n",
    "jacfwd_timing = using_fwd.timeit(500)\n",
    "jacrev_timing = using_bwd.timeit(500)\n",
    "\n",
    "print(f'jacfwd time: {jacfwd_timing}')\n",
    "print(f'jacrev time: {jacrev_timing}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and then do a relative benchmark:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "get_perf(jacfwd_timing, \"jacfwd\", jacrev_timing, \"jacrev\", );"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and now the reverse - more outputs (M) than inputs (N):\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Din = 2048\n",
    "Dout = 32\n",
    "weight = torch.randn(Dout, Din)\n",
    "bias = torch.randn(Dout)\n",
    "x = torch.randn(Din)\n",
    "\n",
    "using_fwd = Timer(stmt=\"jacfwd(predict, argnums=2)(weight, bias, x)\", globals=globals())\n",
    "using_bwd = Timer(stmt=\"jacrev(predict, argnums=2)(weight, bias, x)\", globals=globals())\n",
    "\n",
    "jacfwd_timing = using_fwd.timeit(500)\n",
    "jacrev_timing = using_bwd.timeit(500)\n",
    "\n",
    "print(f'jacfwd time: {jacfwd_timing}')\n",
    "print(f'jacrev time: {jacrev_timing}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and a relative performance comparison:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "get_perf(jacrev_timing, \"jacrev\", jacfwd_timing, \"jacfwd\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hessian computation with functorch.hessian\n",
    "\n",
    "We offer a convenience API to compute hessians: `torch.func.hessiani`.\n",
    "Hessians are the jacobian of the jacobian (or the partial derivative of\n",
    "the partial derivative, aka second order).\n",
    "\n",
    "This suggests that one can just compose functorch jacobian transforms to\n",
    "compute the Hessian. Indeed, under the hood, `hessian(f)` is simply\n",
    "`jacfwd(jacrev(f))`.\n",
    "\n",
    "Note: to boost performance: depending on your model, you may also want\n",
    "to use `jacfwd(jacfwd(f))` or `jacrev(jacrev(f))` instead to compute\n",
    "hessians leveraging the rule of thumb above regarding wider vs taller\n",
    "matrices.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch.func import hessian\n",
    "\n",
    "# lets reduce the size in order not to overwhelm Colab. Hessians require\n",
    "# significant memory:\n",
    "Din = 512\n",
    "Dout = 32\n",
    "weight = torch.randn(Dout, Din)\n",
    "bias = torch.randn(Dout)\n",
    "x = torch.randn(Din)\n",
    "\n",
    "hess_api = hessian(predict, argnums=2)(weight, bias, x)\n",
    "hess_fwdfwd = jacfwd(jacfwd(predict, argnums=2), argnums=2)(weight, bias, x)\n",
    "hess_revrev = jacrev(jacrev(predict, argnums=2), argnums=2)(weight, bias, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s verify we have the same result regardless of using hessian API or\n",
    "using `jacfwd(jacfwd())`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "torch.allclose(hess_api, hess_fwdfwd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Batch Jacobian and Batch Hessian\n",
    "\n",
    "In the above examples we've been operating with a single feature vector.\n",
    "In some cases you might want to take the Jacobian of a batch of outputs\n",
    "with respect to a batch of inputs. That is, given a batch of inputs of\n",
    "shape `(B, N)` and a function that goes from $R^N \\to R^M$, we would\n",
    "like a Jacobian of shape `(B, M, N)`.\n",
    "\n",
    "The easiest way to do this is to use `vmap`:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "batch_size = 64\n",
    "Din = 31\n",
    "Dout = 33\n",
    "\n",
    "weight = torch.randn(Dout, Din)\n",
    "print(f\"weight shape = {weight.shape}\")\n",
    "\n",
    "bias = torch.randn(Dout)\n",
    "\n",
    "x = torch.randn(batch_size, Din)\n",
    "\n",
    "compute_batch_jacobian = vmap(jacrev(predict, argnums=2), in_dims=(None, None, 0))\n",
    "batch_jacobian0 = compute_batch_jacobian(weight, bias, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you have a function that goes from (B, N) -\\> (B, M) instead and are\n",
    "certain that each input produces an independent output, then it\\'s also\n",
    "sometimes possible to do this without using `vmap` by summing the\n",
    "outputs and then computing the Jacobian of that function:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def predict_with_output_summed(weight, bias, x):\n",
    "    return predict(weight, bias, x).sum(0)\n",
    "\n",
    "batch_jacobian1 = jacrev(predict_with_output_summed, argnums=2)(weight, bias, x).movedim(1, 0)\n",
    "assert torch.allclose(batch_jacobian0, batch_jacobian1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you instead have a function that goes from $R^N \\to R^M$ but inputs\n",
    "that are batched, you compose `vmap` with `jacrev` to compute batched\n",
    "jacobians:\n",
    "\n",
    "Finally, batch hessians can be computed similarly. It\\'s easiest to\n",
    "think about them by using `vmap` to batch over hessian computation, but\n",
    "in some cases the sum trick also works.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "compute_batch_hessian = vmap(hessian(predict, argnums=2), in_dims=(None, None, 0))\n",
    "\n",
    "batch_hess = compute_batch_hessian(weight, bias, x)\n",
    "batch_hess.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Computing Hessian-vector products\n",
    "\n",
    "The naive way to compute a Hessian-vector product (hvp) is to\n",
    "materialize the full Hessian and perform a dot-product with a vector. We\n",
    "can do better: it turns out we don\\'t need to materialize the full\n",
    "Hessian to do this. We\\'ll go through two (of many) different strategies\n",
    "to compute Hessian-vector products: - composing reverse-mode AD with\n",
    "reverse-mode AD - composing reverse-mode AD with forward-mode AD\n",
    "\n",
    "Composing reverse-mode AD with forward-mode AD (as opposed to\n",
    "reverse-mode with reverse-mode) is generally the more memory efficient\n",
    "way to compute a hvp because forward-mode AD doesn\\'t need to construct\n",
    "an Autograd graph and save intermediates for backward:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch.func import jvp, grad, vjp\n",
    "\n",
    "def hvp(f, primals, tangents):\n",
    "  return jvp(grad(f), primals, tangents)[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here\\'s some sample usage.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def f(x):\n",
    "  return x.sin().sum()\n",
    "\n",
    "x = torch.randn(2048)\n",
    "tangent = torch.randn(2048)\n",
    "\n",
    "result = hvp(f, (x,), (tangent,))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If PyTorch forward-AD does not have coverage for your operations, then\n",
    "we can instead compose reverse-mode AD with reverse-mode AD:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def hvp_revrev(f, primals, tangents):\n",
    "  _, vjp_fn = vjp(grad(f), *primals)\n",
    "  return vjp_fn(*tangents)\n",
    "\n",
    "result_hvp_revrev = hvp_revrev(f, (x,), (tangent,))\n",
    "assert torch.allclose(result, result_hvp_revrev[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
