Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix objective.compile in Benchmarks #1483

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,20 @@ jobs:
pip install -r devtools/dev-requirements.txt
pip install matplotlib==3.9.2

- name: Benchmark with pytest-benchmark (PR)
- name: Action Details
if: env.has_changes == 'true'
run: |
source .venv-${{ env.version }}/bin/activate
which python
python --version
pwd
lscpu
pip list

- name: Benchmark with pytest-benchmark (PR)
if: env.has_changes == 'true'
run: |
source .venv-${{ env.version }}/bin/activate
cd tests/benchmarks
python -m pytest benchmark_cpu_small.py -vv \
--benchmark-save='Latest_Commit' \
Expand All @@ -114,9 +121,6 @@ jobs:
if: env.has_changes == 'true'
run: |
source .venv-${{ env.version }}/bin/activate
pwd
lscpu
pip list
cd tests/benchmarks
python -m pytest benchmark_cpu_small.py -vv \
--benchmark-save='master' \
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/notebook_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,20 @@ jobs:
pip install -r devtools/dev-requirements.txt
pip install matplotlib==3.9.2

- name: Test notebooks with pytest and nbmake
- name: Action Details
if: env.has_changes == 'true'
run: |
source .venv-${{ env.version }}/bin/activate
which python
python --version
pwd
lscpu
pip list

- name: Test notebooks with pytest and nbmake
if: env.has_changes == 'true'
run: |
source .venv-${{ env.version }}/bin/activate
export PYTHONPATH=$(pwd)
pytest -v --nbmake "./docs/notebooks" \
--nbmake-timeout=2000 \
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/regression_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,20 @@ jobs:
with:
swap-size-gb: 10

- name: Test with pytest
- name: Action Details
if: env.has_changes == 'true'
run: |
source .venv-${{ env.version }}/bin/activate
which python
python --version
pwd
lscpu
pip list

- name: Test with pytest
if: env.has_changes == 'true'
run: |
source .venv-${{ env.version }}/bin/activate
python -m pytest -v -m regression\
--durations=0 \
--cov-report xml:cov.xml \
Expand Down
4 changes: 2 additions & 2 deletions desc/objectives/objective_funs.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,12 +891,12 @@ def compile(self, mode="auto", verbose=1):
timer.disp("Hessian compilation time")
if mode in ["lsq", "all"]:
timer.start("Objective compilation time")
_ = self.compute_scaled(x, self.constants).block_until_ready()
_ = self.compute_scaled_error(x, self.constants).block_until_ready()
timer.stop("Objective compilation time")
if verbose > 1:
timer.disp("Objective compilation time")
timer.start("Jacobian compilation time")
_ = self.jac_scaled(x, self.constants).block_until_ready()
_ = self.jac_scaled_error(x, self.constants).block_until_ready()
timer.stop("Jacobian compilation time")
if verbose > 1:
timer.disp("Jacobian compilation time")
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
:caption: Tutorials

installation
performance_tips
notebooks/tutorials/basic_equilibrium.ipynb
notebooks/tutorials/advanced_equilibrium_continuation.ipynb
notebooks/tutorials/continuation_step_by_step.ipynb
Expand All @@ -35,7 +36,6 @@
notebooks/tutorials/coil_stage_two_optimization.ipynb
notebooks/tutorials/QFM_surface.ipynb
notebooks/tutorials/ideal_ballooning_stability.ipynb
memory_usage

.. toctree::
:maxdepth: 1
Expand Down
4 changes: 2 additions & 2 deletions docs/notebooks/tutorials/nae_constraint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"import os\n",
"\n",
"sys.path.insert(0, os.path.abspath(\".\"))\n",
"sys.path.append(os.path.abspath(\"../../\"))"
"sys.path.append(os.path.abspath(\"../../../\"))"
]
},
{
Expand Down Expand Up @@ -812,7 +812,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.0"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
21 changes: 21 additions & 0 deletions docs/memory_usage.rst → docs/performance_tips.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
================
Performance Tips
================

Caching the Compiled (Jitted) Code
----------------------------------
Although the compiled code is fast, it still takes time to compile. If you are running the same optimization, or some similar optimization, multiple times, you can save time by caching the compiled code. This automatically happens for a single session (for example, until you restart your kernel in Jupyter Notebook) but once you start using another session, the code will need to be recompiled. Fortunately, there is a way to bypass this. First create a cache directory (i.e. ``jax-caches``), and put the following code at the beginning of your script:

.. code-block:: python

import jax
import jax.numpy as jnp

jax.config.update("jax_compilation_cache_dir", "../jax-caches")
jax.config.update("jax_persistent_cache_min_entry_size_bytes", -1)
jax.config.update("jax_persistent_cache_min_compile_time_secs", 0)

This will use a directory called ``jax-caches`` in the parent directory of the script to store the compiled code. The ``jax_persistent_cache_min_entry_size_bytes`` and ``jax_persistent_cache_min_compile_time_secs`` parameters are set to -1 and 0, respectively, to ensure that all compiled code is cached. For more details on caching, refer to official JAX documentation [`here <https://jax.readthedocs.io/en/latest/persistent_compilation_cache.html#persistent-compilation-cache>`__].

Note: Updating JAX version might re-compile some previously cached code, and thi might increase the cache size. Every once in a while, you might need to clear your cache directory.
YigitElma marked this conversation as resolved.
Show resolved Hide resolved


Reducing Memory Size of Objective Jacobian Calculation
------------------------------------------------------
Expand Down
Loading
Loading