You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit d08a73d
Browse filesBrowse the repository at this point in the historyBrowse files
* misc
* misc
* Fix call to add_tax_pure in jax_intro pure function example
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
---------
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
z_max_numpy = np.max(f(x_mesh, y_mesh)) # This works
150
168
```
151
169
152
170
In the vectorized version, all the looping takes place in compiled code.
@@ -159,11 +177,30 @@ The output should be close to one:
159
177
print(f"NumPy result: {z_max_numpy:.6f}")
160
178
```
161
179
180
+
### Memory Issues
181
+
182
+
So we have the right solution in reasonable time --- but memory usage is huge.
183
+
184
+
While the flat arrays are low-memory
185
+
186
+
```{code-cell} ipython3
187
+
grid.nbytes
188
+
```
189
+
190
+
the mesh grids are two-dimensional and hence very memory intensive
191
+
192
+
```{code-cell} ipython3
193
+
x_mesh.nbytes + y_mesh.nbytes
194
+
```
195
+
196
+
Moreover, NumPy's eager execution creates many intermediate arrays of the same size!
197
+
198
+
This kind of memory usage can be a big problem in actual research calculations.
162
199
163
200
164
201
### A Comparison with Numba
165
202
166
-
Now let's see if we can achieve better performance using Numba with a simple loop.
203
+
Let's see if we can achieve better performance using Numba with a simple loop.
167
204
168
205
```{code-cell} ipython3
169
206
@numba.jit
@@ -194,15 +231,13 @@ with qe.Timer():
194
231
compute_max_numba(grid)
195
232
```
196
233
197
-
Depending on your machine, the Numba version might be either slower or faster than NumPy.
234
+
Notice how we are using almost no memory --- we just need the one-dimensional `grid`
198
235
199
-
In most cases we find that Numba is slightly better.
236
+
Moreover, execution speed is good.
200
237
201
-
On the one hand, NumPy combines efficient arithmetic with some
202
-
multithreading, which provides an advantage.
238
+
On most machines, the Numba version will be somewhat faster than NumPy.
203
239
204
-
On the other hand, the Numba routine uses much less memory, since we are only
205
-
working with a single one-dimensional grid.
240
+
The reason is efficient machine code plus less memory read-write.
206
241
207
242
208
243
### Parallelized Numba
@@ -301,27 +336,11 @@ The compilation overhead is a one-time cost that pays off when the function is c
301
336
302
337
### JAX plus vmap
303
338
304
-
There is one problem with both the NumPy code and the JAX code above:
305
-
306
-
While the flat arrays are low-memory
307
-
308
-
```{code-cell} ipython3
309
-
grid.nbytes
310
-
```
311
-
312
-
the mesh grids are memory intensive
313
-
314
-
```{code-cell} ipython3
315
-
x_mesh.nbytes + y_mesh.nbytes
316
-
```
339
+
Because we used `jax.jit` above, we avoided creating many intermediate arrays.
317
340
318
-
This extra memory usage can be a big problem in actual research calculations.
341
+
But we still create the big arrays `z_max`, `x_mesh`, and `y_mesh`.
319
342
320
-
Fortunately, JAX admits a different approach
321
-
using [jax.vmap](https://docs.jax.dev/en/latest/_autosummary/jax.vmap.html).
322
-
323
-
The idea of `vmap` is to break vectorization into stages, transforming a
324
-
function that operates on single values into one that operates on arrays.
343
+
Fortunately, we can avoid this by using [jax.vmap](https://docs.jax.dev/en/latest/_autosummary/jax.vmap.html).
325
344
326
345
Here's how we can apply it to our problem.
327
346
@@ -330,13 +349,13 @@ Here's how we can apply it to our problem.
330
349
@jax.jit
331
350
def compute_max_vmap(grid):
332
351
# Construct a function that takes the max over all x for given y
0 commit comments