JAX: Add JAX implementation for wealth dynamics#304
Conversation
|
Hi @HumphreyYang , This seems to be ready for the first review. Please have a look. Thanks |
There was a problem hiding this comment.
Many Thanks for the update @Smit-create. The code looks great to me. One improvement we could potentially make is to hide the code using numba using
---
tags: [hide-input]
---
and only compare the speed when making the final comparison.
I think it is clear to readers when comparing the performance of two implementations in my case since there is only one function involved, but in this case, having two implementations throughout the lecture may confuse readers and obscure the focus of the lecture. Please let me know what you think :) (also CC @jstac)
| # Check if JAX is using GPU | ||
| print("jax backend: {}".format(jax.lib.xla_bridge.get_backend().platform)) | ||
| # Check the devices available for JAX | ||
| print(jax.devices()) |
There was a problem hiding this comment.
I think we can use
print(f"jax backend: {jax.devices()[0].platform}")
in our PRs for simplicity and consistency.
There was a problem hiding this comment.
Thanks @HumphreyYang , simple is good.
@Smit-create , please change jax to JAX in the text.
There was a problem hiding this comment.
Thanks, I will change that accordingly.
This sounds like a nice idea, let's try that out and see how it looks. |
|
@Smit-create and @HumphreyYang, please let me know when this is ready for another review. |
@Smit-create I think it looks cleaner and simpler with Numba code hidden. How do you feel about this change? |
I think that looks cleaner. What do you think @jstac?
@jstac, I think that the content is ready for review. Might just need to edit/add a couple of cells following the discussions in #301 (comment). |
| ## Implementation | ||
| ## Implementation using JAX | ||
|
|
||
| Let's define a Model to represent the wealth dynamics. |
There was a problem hiding this comment.
Please change "Model" to "model" and the same below.
| update_states_jax = jax.jit(update_states_jax, static_argnums=(2,)) | ||
| ``` | ||
|
|
||
| Here’s function to simulate the time series of wealth for individual households using `for` loop and JAX. |
|
|
||
| # 2nd time is expected to be very fast because of JIT | ||
| w_jax_result = wealth_time_series_for_loop_jax(wdy.y_mean, ts_length, wdy, size) | ||
| ``` |
There was a problem hiding this comment.
does this need block_until_ready?
I guess not but just checking.
There was a problem hiding this comment.
Let's add that. The documentation for benchmarking also recommends using it: https://jax.readthedocs.io/en/latest/faq.html#benchmarking-jax-code
|
Looking at the current deployment https://63b52771da28e92bd5e7733b--nostalgic-wright-5fa355.netlify.app/wealth_dynamics.html shows that one numba code cell is not hidden, while the others are. Is this intensional? |
|
Should we compare the timing of I suspect that, on a large cross-section, the JAX code will be much faster... |
|
Does this sentence need to be adjusted/removed? "Notice how effectively Numba has implemented multithreading for this routine: all 8 CPUs on our workstation are running at maximum capacity (even though four of them are virtual)." I think we are using JAX at this point. |
|
Many thanks @Smit-create . Please see my comments above. @mmcky , if you have time please have a quick glance at this once @Smit-create has pushed changes. |
I thought to keep it unhidden so that the below cell that measures time can relate to it. But let's hide all the numba code and just keep the JAX available readily.
It's already been compared using
Let's remove this completely along with the image? |
|
thanks @Smit-create, @HumphreyYang and @jstac Is the purpose of hiding the This is what it looks like in the preview |
|
For the text in the lecture Could we use the |
|
Yes, I think it looks nice to have the numba code hidden, and I can understand the motivation, but at the same time it might be confusing for the reader --- why is some code hidden and some not? Also, if we hide numba code here, then we should do it elsewhere when we use JAX --- but then it's a burden and possibly unclear exactly what we should hide. Having thought all this through, I suggest that we show the numba code rather than hiding it and do the same for the remaining lectures. |
Sure, will do that.
Yes, added it, thanks! |
|
Thanks @Smit-create @HumphreyYang just checking that you saw the discussion above regarding hiding / not hiding. Could you please give this PR one more careful look over and then let me know when you are done. After that I think we'll be ready to merge. |
Hi @jstac, Before that, could you please have a look at the GPU Warning and JAX imports I pushed in my PR so that @Smit-create and I can have a consistent GPU warning? |
|
Thanks @HumphreyYang , good idea. Here's what I suggest for the GPU warning, which slightly modifies the version of @HumphreyYang : This lecture is built using hardware that has access to a GPU and uses JAX for GPU programming. As a result, the lecture will be slower when running on a machine without a GPU. Free GPUs are available on Google Colab. To use this option, please click on the play icon top right, select Colab, and set the runtime environment to include a GPU. Alternatively, if if you have your own GPU, you can follow the instructions for for installing JAX with GPU support. |
|
The PR is ready now. The failure is because of this diff. Once we merge that PR, I'll rebase this on top of that and that should fix the CI. |
|
Thanks @Smit-create
|
Thanks for the review @jstac. I have updated the diff.
The last commit (10th commit) was failing on CI and so I suppose it won't be deployed. I have pushed a new commit and let's see if that works. |
|
The tests pass and the deployed version is available at https://63c622d321fc755cc8652567--nostalgic-wright-5fa355.netlify.app/ |
|
Great work @Smit-create , many thanks. Merging. |

#300