|
28 | 28 | "cell_type": "markdown", |
29 | 29 | "metadata": {}, |
30 | 30 | "source": [ |
31 | | - "This minimal tutorial demonstrates how to use the torch frontend for `S2FFT` to compute spherical harmonic transforms. 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." |
| 31 | + "This minimal tutorial demonstrates how to use the torch frontend for `S2FFT` to compute spherical harmonic transforms. 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. As the torch functions wrap the JAX implementations we need to configure JAX to use 64-bit precision floating point types by default to ensure sufficient precision for the transforms - `S2FFT` will emit a warning if this has not been done." |
32 | 32 | ] |
33 | 33 | }, |
34 | 34 | { |
35 | 35 | "cell_type": "code", |
36 | 36 | "execution_count": 2, |
37 | 37 | "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "import jax\n", |
| 41 | + "jax.config.update(\"jax_enable_x64\", True)\n", |
| 42 | + "import torch \n", |
| 43 | + "import numpy as np\n", |
| 44 | + "from s2fft.transforms.spherical import inverse, forward\n", |
| 45 | + "from s2fft.precompute_transforms.spherical import (\n", |
| 46 | + " inverse as precompute_inverse, forward as precompute_forward\n", |
| 47 | + ")\n", |
| 48 | + "from s2fft.precompute_transforms.construct import spin_spherical_kernel_torch\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": 3, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "L = 64 \n", |
| 66 | + "rng = np.random.default_rng(1234951510)\n", |
| 67 | + "flm = torch.from_numpy(signal_generator.generate_flm(rng, L))" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "markdown", |
| 72 | + "metadata": {}, |
| 73 | + "source": [ |
| 74 | + "Now lets calculate the signal on the sphere by applying the inverse spherical harmonic transform" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 4, |
| 80 | + "metadata": {}, |
38 | 81 | "outputs": [ |
39 | 82 | { |
40 | 83 | "name": "stderr", |
41 | 84 | "output_type": "stream", |
42 | 85 | "text": [ |
43 | | - "JAX is not using 64-bit precision. This will dramatically affect numerical precision at even moderate L.\n" |
| 86 | + "An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.\n" |
44 | 87 | ] |
45 | 88 | } |
46 | 89 | ], |
47 | 90 | "source": [ |
48 | | - "import torch \n", |
49 | | - "import numpy as np \n", |
50 | | - "from s2fft.precompute_transforms.spherical import inverse, forward\n", |
51 | | - "from s2fft.precompute_transforms.construct import spin_spherical_kernel\n", |
52 | | - "from s2fft.utils import signal_generator" |
| 91 | + "f = inverse(flm, L, method=\"torch\")" |
53 | 92 | ] |
54 | 93 | }, |
55 | 94 | { |
56 | 95 | "cell_type": "markdown", |
57 | 96 | "metadata": {}, |
58 | 97 | "source": [ |
59 | | - "Lets set up a mock problem by specifiying a bandlimit $L$ and generating some arbitrary harmonic coefficients." |
| 98 | + "To calculate the corresponding spherical harmonic representation execute" |
60 | 99 | ] |
61 | 100 | }, |
62 | 101 | { |
63 | 102 | "cell_type": "code", |
64 | | - "execution_count": 3, |
| 103 | + "execution_count": 5, |
65 | 104 | "metadata": {}, |
66 | 105 | "outputs": [], |
67 | 106 | "source": [ |
68 | | - "L = 64 # Spherical harmonic bandlimit\n", |
69 | | - "rng = np.random.default_rng(1234951510) # Random seed for signal generator\n", |
70 | | - "flm = signal_generator.generate_flm(rng, L, using_torch=True) # Random set of spherical harmonic coefficients" |
| 107 | + "flm_check = forward(f, L, method=\"torch\")" |
71 | 108 | ] |
72 | 109 | }, |
73 | 110 | { |
74 | 111 | "cell_type": "markdown", |
75 | 112 | "metadata": {}, |
76 | 113 | "source": [ |
77 | | - "For the fully precompute transform we must also generate the precompute kernels which we store as a torch tensors." |
| 114 | + "Finally, lets check the error on the round trip is as expected for 64 bit machine precision floating point arithmetic" |
78 | 115 | ] |
79 | 116 | }, |
80 | 117 | { |
81 | 118 | "cell_type": "code", |
82 | | - "execution_count": 4, |
| 119 | + "execution_count": 6, |
83 | 120 | "metadata": {}, |
84 | | - "outputs": [], |
| 121 | + "outputs": [ |
| 122 | + { |
| 123 | + "name": "stdout", |
| 124 | + "output_type": "stream", |
| 125 | + "text": [ |
| 126 | + "Mean absolute error = 2.8915048238993476e-14\n" |
| 127 | + ] |
| 128 | + } |
| 129 | + ], |
85 | 130 | "source": [ |
86 | | - "inverse_kernel = spin_spherical_kernel(L, using_torch=True, forward=False) \n", |
87 | | - "forward_kernel = spin_spherical_kernel(L, using_torch=True, forward=True) " |
| 131 | + "print(f\"Mean absolute error = {np.nanmean(np.abs(flm_check - flm))}\")" |
88 | 132 | ] |
89 | 133 | }, |
90 | 134 | { |
91 | 135 | "cell_type": "markdown", |
92 | 136 | "metadata": {}, |
93 | 137 | "source": [ |
94 | | - "Now lets calculate the signal on the sphere by applying the inverse spherical harmonic transform" |
| 138 | + "For the fully precompute transform we must also generate the precompute kernels which we store as a torch tensors." |
95 | 139 | ] |
96 | 140 | }, |
97 | 141 | { |
98 | 142 | "cell_type": "code", |
99 | | - "execution_count": 5, |
| 143 | + "execution_count": 7, |
100 | 144 | "metadata": {}, |
101 | 145 | "outputs": [], |
102 | 146 | "source": [ |
103 | | - "f = inverse(flm, L, 0, inverse_kernel, method=\"torch\")" |
| 147 | + "inverse_kernel = spin_spherical_kernel_torch(L, forward=False) \n", |
| 148 | + "forward_kernel = spin_spherical_kernel_torch(L, forward=True) " |
104 | 149 | ] |
105 | 150 | }, |
106 | 151 | { |
107 | 152 | "cell_type": "markdown", |
108 | 153 | "metadata": {}, |
109 | 154 | "source": [ |
110 | | - "To calculate the corresponding spherical harmonic representation execute" |
| 155 | + "We then pass the kernels as additional arguments to the transform functions" |
111 | 156 | ] |
112 | 157 | }, |
113 | 158 | { |
114 | 159 | "cell_type": "code", |
115 | | - "execution_count": 6, |
| 160 | + "execution_count": 8, |
116 | 161 | "metadata": {}, |
117 | 162 | "outputs": [], |
118 | 163 | "source": [ |
119 | | - "flm_check = forward(f, L, 0, forward_kernel, method=\"torch\")" |
| 164 | + "precompute_f = precompute_inverse(flm, L, kernel=inverse_kernel, method=\"torch\")\n", |
| 165 | + "precompute_flm_check = precompute_forward(f, L, kernel=forward_kernel, method=\"torch\")" |
120 | 166 | ] |
121 | 167 | }, |
122 | 168 | { |
123 | 169 | "cell_type": "markdown", |
124 | 170 | "metadata": {}, |
125 | 171 | "source": [ |
126 | | - "Finally, lets check the error on the roundtrip is at 64bit machine precision" |
| 172 | + "Again, we check the error on the round trip is as expected" |
127 | 173 | ] |
128 | 174 | }, |
129 | 175 | { |
130 | 176 | "cell_type": "code", |
131 | | - "execution_count": 7, |
| 177 | + "execution_count": 9, |
132 | 178 | "metadata": {}, |
133 | 179 | "outputs": [ |
134 | 180 | { |
135 | 181 | "name": "stdout", |
136 | 182 | "output_type": "stream", |
137 | 183 | "text": [ |
138 | | - "Mean absolute error = 1.1866908936078849e-14\n" |
| 184 | + "Mean absolute error = 2.904741595325594e-14\n" |
139 | 185 | ] |
140 | 186 | } |
141 | 187 | ], |
142 | 188 | "source": [ |
143 | | - "print(f\"Mean absolute error = {np.nanmean(np.abs(flm_check - flm))}\")" |
| 189 | + "print(f\"Mean absolute error = {np.nanmean(np.abs(precompute_flm_check - flm))}\")" |
144 | 190 | ] |
145 | 191 | } |
146 | 192 | ], |
147 | 193 | "metadata": { |
148 | 194 | "kernelspec": { |
149 | | - "display_name": "Python 3.10.4 ('s2fft')", |
| 195 | + "display_name": "s2fft", |
150 | 196 | "language": "python", |
151 | 197 | "name": "python3" |
152 | 198 | }, |
|
160 | 206 | "name": "python", |
161 | 207 | "nbconvert_exporter": "python", |
162 | 208 | "pygments_lexer": "ipython3", |
163 | | - "version": "3.10.0" |
| 209 | + "version": "3.11.10" |
164 | 210 | }, |
165 | | - "orig_nbformat": 4, |
166 | | - "vscode": { |
167 | | - "interpreter": { |
168 | | - "hash": "3425e24474cbe920550266ea26b478634978cc419579f9dbcf479231067df6a3" |
169 | | - } |
170 | | - } |
| 211 | + "orig_nbformat": 4 |
171 | 212 | }, |
172 | 213 | "nbformat": 4, |
173 | 214 | "nbformat_minor": 2 |
|
0 commit comments