Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do you set environment variables to disable preallocation in JAX


How do you set environment variables to disable preallocation in JAX


To disable preallocation of GPU memory in JAX, you can set the environment variable `XLA_PYTHON_CLIENT_PREALLOCATE` to `'false'`. This can be done either within your Python script or outside of it before running the script. Here's how you can do it:

Setting Environment Variable Inside Python Script

You need to set this variable before importing JAX, as setting it afterward will have no effect:

python
import os
os.environ['XLA_PYTHON_CLIENT_PREALLOCATE'] = 'false'
import jax

Setting Environment Variable Outside Python Script

You can also set this variable in your terminal or command prompt before running your Python script:

bash
export XLA_PYTHON_CLIENT_PREALLOCATE=false

On Windows, use:

cmd
set XLA_PYTHON_CLIENT_PREALLOCATE=false

This will ensure that JAX allocates GPU memory as needed instead of preallocating it, which can help prevent out-of-memory errors but may lead to more memory fragmentation[1][3][5].

Citations:
[1] https://github.com/jax-ml/jax/discussions/21962
[2] https://www.jan-ebert.com/blog/jax-intro.html
[3] https://kolonist26-jax-kr.readthedocs.io/en/latest/gpu_memory_allocation.html
[4] https://github.com/jax-ml/jax/discussions/19014
[5] https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
[6] https://www.kaggle.com/code/darshan1504/exploring-diffusion-models-with-jax
[7] https://github.com/google/jax/issues/19267
[8] https://dokumen.pub/google-jax-cookbook.html
[9] https://crikit.science/documentation/ad_systems/
[10] https://docs.jax.dev/en/latest/faq.html