|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# __Torch frontend guide__\n", |
| 8 | + "\n", |
| 9 | + "---\n", |
| 10 | + "\n" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "This minimal tutorial demonstrates how to use the torch frontend for `S2FFT` to compute spherical harmonic transforms.\n", |
| 18 | + "\n", |
| 19 | + "Note that to install `S2FFT` with torch functionality from ``PyPi`` run \n", |
| 20 | + "``` bash\n", |
| 21 | + "pip install s2fft[torch] \n", |
| 22 | + "```\n", |
| 23 | + "or from source by cloning the repository and running \n", |
| 24 | + "``` bash\n", |
| 25 | + "pip install .\\[torch\\] \n", |
| 26 | + "```\n", |
| 27 | + "\n", |
| 28 | + "Though `S2FFT` is primarily designed for JAX, this torch functionality is fully unit tested (including gradients) and can be used straightforwardly as a learnable layer within existing models." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 1, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [ |
| 36 | + { |
| 37 | + "name": "stderr", |
| 38 | + "output_type": "stream", |
| 39 | + "text": [ |
| 40 | + "JAX is not using 64-bit precision. This will dramatically affect numerical precision at even moderate L.\n" |
| 41 | + ] |
| 42 | + } |
| 43 | + ], |
| 44 | + "source": [ |
| 45 | + "import torch \n", |
| 46 | + "import numpy as np \n", |
| 47 | + "from s2fft.precompute_transforms.spherical import inverse, forward\n", |
| 48 | + "from s2fft.precompute_transforms.construct import spin_spherical_kernel\n", |
| 49 | + "from s2fft.utils import signal_generator" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "Lets set up a mock problem by specifiying a bandlimit $L$ and generating some arbitrary harmonic coefficients." |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 2, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "L = 64 # Spherical harmonic bandlimit\n", |
| 66 | + "rng = np.random.default_rng(1234951510) # Random seed for signal generator\n", |
| 67 | + "flm = signal_generator.generate_flm(rng, L, using_torch=True) # Random set of spherical harmonic coefficients" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "markdown", |
| 72 | + "metadata": {}, |
| 73 | + "source": [ |
| 74 | + "For the fully precompute transform we must also generate the precompute kernels which we store as a torch tensors." |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 3, |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [], |
| 82 | + "source": [ |
| 83 | + "inverse_kernel = spin_spherical_kernel(L, using_torch=True, forward=False) \n", |
| 84 | + "forward_kernel = spin_spherical_kernel(L, using_torch=True, forward=True) " |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "metadata": {}, |
| 90 | + "source": [ |
| 91 | + "Now lets calculate the signal on the sphere by applying the inverse spherical harmonic transform" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": 4, |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "f = inverse(flm, L, 0, inverse_kernel, method=\"torch\")" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "To calculate the corresponding spherical harmonic representation execute" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": 5, |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [], |
| 115 | + "source": [ |
| 116 | + "flm_check = forward(f, L, 0, forward_kernel, method=\"torch\")" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "metadata": {}, |
| 122 | + "source": [ |
| 123 | + "Finally, lets check the error on the roundtrip is at 64bit machine precision" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": 6, |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [ |
| 131 | + { |
| 132 | + "name": "stdout", |
| 133 | + "output_type": "stream", |
| 134 | + "text": [ |
| 135 | + "Mean absolute error = 1.1866908936078849e-14\n" |
| 136 | + ] |
| 137 | + } |
| 138 | + ], |
| 139 | + "source": [ |
| 140 | + "print(f\"Mean absolute error = {np.nanmean(np.abs(flm_check - flm))}\")" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
| 144 | + "metadata": { |
| 145 | + "kernelspec": { |
| 146 | + "display_name": "Python 3.10.4 ('s2fft')", |
| 147 | + "language": "python", |
| 148 | + "name": "python3" |
| 149 | + }, |
| 150 | + "language_info": { |
| 151 | + "codemirror_mode": { |
| 152 | + "name": "ipython", |
| 153 | + "version": 3 |
| 154 | + }, |
| 155 | + "file_extension": ".py", |
| 156 | + "mimetype": "text/x-python", |
| 157 | + "name": "python", |
| 158 | + "nbconvert_exporter": "python", |
| 159 | + "pygments_lexer": "ipython3", |
| 160 | + "version": "3.10.0" |
| 161 | + }, |
| 162 | + "orig_nbformat": 4, |
| 163 | + "vscode": { |
| 164 | + "interpreter": { |
| 165 | + "hash": "3425e24474cbe920550266ea26b478634978cc419579f9dbcf479231067df6a3" |
| 166 | + } |
| 167 | + } |
| 168 | + }, |
| 169 | + "nbformat": 4, |
| 170 | + "nbformat_minor": 2 |
| 171 | +} |
0 commit comments