{
 "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": [
    "# Creating Extensions Using NumPy and SciPy\n",
    "\n",
    "**Author**: [Adam Paszke](https://github.com/apaszke)\n",
    "\n",
    "**Updated by**: [Adam Dziedzic](https://github.com/adam-dziedzic)\n",
    "\n",
    "In this tutorial, we shall go through two tasks:\n",
    "\n",
    "1.  Create a neural network layer with no parameters.\n",
    "\n",
    "    > -   This calls into **numpy** as part of its implementation\n",
    "\n",
    "2.  Create a neural network layer that has learnable weights\n",
    "\n",
    "    > -   This calls into **SciPy** as part of its implementation\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.autograd import Function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Parameter-less example\n",
    "\n",
    "This layer doesn't particularly do anything useful or mathematically\n",
    "correct.\n",
    "\n",
    "It is aptly named `BadFFTFunction`\n",
    "\n",
    "**Layer Implementation**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from numpy.fft import rfft2, irfft2\n",
    "\n",
    "\n",
    "class BadFFTFunction(Function):\n",
    "    @staticmethod\n",
    "    def forward(ctx, input):\n",
    "        numpy_input = input.detach().numpy()\n",
    "        result = abs(rfft2(numpy_input))\n",
    "        return input.new(result)\n",
    "\n",
    "    @staticmethod\n",
    "    def backward(ctx, grad_output):\n",
    "        numpy_go = grad_output.numpy()\n",
    "        result = irfft2(numpy_go)\n",
    "        return grad_output.new(result)\n",
    "\n",
    "# since this layer does not have any parameters, we can\n",
    "# simply declare this as a function, rather than as an ``nn.Module`` class\n",
    "\n",
    "\n",
    "def incorrect_fft(input):\n",
    "    return BadFFTFunction.apply(input)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Example usage of the created layer:**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "input = torch.randn(8, 8, requires_grad=True)\n",
    "result = incorrect_fft(input)\n",
    "print(result)\n",
    "result.backward(torch.randn(result.size()))\n",
    "print(input)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Parametrized example\n",
    "\n",
    "In deep learning literature, this layer is confusingly referred to as\n",
    "convolution while the actual operation is cross-correlation (the only\n",
    "difference is that filter is flipped for convolution, which is not the\n",
    "case for cross-correlation).\n",
    "\n",
    "Implementation of a layer with learnable weights, where\n",
    "cross-correlation has a filter (kernel) that represents weights.\n",
    "\n",
    "The backward pass computes the gradient `wrt` the input and the gradient\n",
    "`wrt` the filter.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from numpy import flip\n",
    "import numpy as np\n",
    "from scipy.signal import convolve2d, correlate2d\n",
    "from torch.nn.modules.module import Module\n",
    "from torch.nn.parameter import Parameter\n",
    "\n",
    "\n",
    "class ScipyConv2dFunction(Function):\n",
    "    @staticmethod\n",
    "    def forward(ctx, input, filter, bias):\n",
    "        # detach so we can cast to NumPy\n",
    "        input, filter, bias = input.detach(), filter.detach(), bias.detach()\n",
    "        result = correlate2d(input.numpy(), filter.numpy(), mode='valid')\n",
    "        result += bias.numpy()\n",
    "        ctx.save_for_backward(input, filter, bias)\n",
    "        return torch.as_tensor(result, dtype=input.dtype)\n",
    "\n",
    "    @staticmethod\n",
    "    def backward(ctx, grad_output):\n",
    "        grad_output = grad_output.detach()\n",
    "        input, filter, bias = ctx.saved_tensors\n",
    "        grad_output = grad_output.numpy()\n",
    "        grad_bias = np.sum(grad_output, keepdims=True)\n",
    "        grad_input = convolve2d(grad_output, filter.numpy(), mode='full')\n",
    "        # the previous line can be expressed equivalently as:\n",
    "        # grad_input = correlate2d(grad_output, flip(flip(filter.numpy(), axis=0), axis=1), mode='full')\n",
    "        grad_filter = correlate2d(input.numpy(), grad_output, mode='valid')\n",
    "        return torch.from_numpy(grad_input), torch.from_numpy(grad_filter).to(torch.float), torch.from_numpy(grad_bias).to(torch.float)\n",
    "\n",
    "\n",
    "class ScipyConv2d(Module):\n",
    "    def __init__(self, filter_width, filter_height):\n",
    "        super(ScipyConv2d, self).__init__()\n",
    "        self.filter = Parameter(torch.randn(filter_width, filter_height))\n",
    "        self.bias = Parameter(torch.randn(1, 1))\n",
    "\n",
    "    def forward(self, input):\n",
    "        return ScipyConv2dFunction.apply(input, self.filter, self.bias)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Example usage:**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "module = ScipyConv2d(3, 3)\n",
    "print(\"Filter and bias: \", list(module.parameters()))\n",
    "input = torch.randn(10, 10, requires_grad=True)\n",
    "output = module(input)\n",
    "print(\"Output from the convolution: \", output)\n",
    "output.backward(torch.randn(8, 8))\n",
    "print(\"Gradient for the input map: \", input.grad)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Check the gradients:**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch.autograd.gradcheck import gradcheck\n",
    "\n",
    "moduleConv = ScipyConv2d(3, 3)\n",
    "\n",
    "input = [torch.randn(20, 20, dtype=torch.double, requires_grad=True)]\n",
    "test = gradcheck(moduleConv, input, eps=1e-6, atol=1e-4)\n",
    "print(\"Are the gradients correct: \", test)"
   ]
  }
 ],
 "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
}
