Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions notebooks/chapter_2_lens_modeling/tutorial_2_practicalities.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,44 @@
"Watch the progress of your first run to develop an expectation for your computer. The optional *Need for Speed*\n",
"tutorial explains how to investigate performance in more detail.\n",
"\n",
"__Automatic Compute Setup__\n",
"\n",
"The fastest way to run a fit (JAX or NumPy, CPU or GPU, how many CPU cores) depends on the likelihood and on your\n",
"hardware. **PyAutoLens** can make this choice for you, using timings measured when the software was profiled. When we\n",
"fit without asking for it, as above, the terminal prints one line saying which setup it would choose:\n",
"\n",
"```\n",
"Compute setup: not set (auto would choose JAX \u00b7 CPU \u00b7 dense \u00b7 fp64; pass setup=\"auto\")\n",
"```\n",
"\n",
"To opt in, pass `setup=\"auto\"` to the analysis and `number_of_cores=\"auto\"` to the search:\n",
"\n",
"```\n",
"analysis = al.AnalysisImaging(dataset=dataset, setup=\"auto\")\n",
"\n",
"search = af.Nautilus(..., number_of_cores=\"auto\")\n",
"```\n",
"\n",
"When the fit starts, the terminal then prints a short header. On an 8-core laptop, this tutorial's fit gives:\n",
"\n",
"```\n",
"Automatic compute setup (CPU/GPU choice from setup=\"auto\"; override with PYAUTO_SETUP or setup=...):\n",
"Setup: JAX \u00b7 CPU \u00b7 dense \u00b7 fp64 (rule imaging/mge \u00b7 extrapolated: 8 cores, rules measured on \u226516-core nodes)\n",
"Parallel: 1 worker (JAX on CPU)\n",
"Estimate: ~47 min on this hardware (7.1 ms/eval, euclid cell \u00b7 Nautilus n_live=200 \u2248 1.3e5 evals, heuristic \u00b7 1 worker (JAX))\n",
"```\n",
"\n",
"- `Setup` is the chosen backend, device, operator and numerical precision, and the rule that picked them. \"extrapolated\"\n",
" means your computer does not match the machines the rules were measured on, with the reason given.\n",
"- `Parallel` is how many CPU processes run the search. A JAX setup uses one process and lets JAX use the device.\n",
"- `Estimate` is the expected wall time: the time per likelihood evaluation from profiling, multiplied by a rough\n",
" count of how many evaluations Nautilus needs. Treat it as a guide to whether a fit takes minutes or hours, not a\n",
" promise.\n",
"\n",
"To override the choice, pass a named setup instead (for example `setup=\"jax_gpu_dense_fp64\"` on a GPU) or set the\n",
"`PYAUTO_SETUP` environment variable. The guide `scripts/guides/advanced/auto_setup.py` in the `autolens_workspace`\n",
"explains the setups in full.\n",
"\n",
"__Notebooks and Scripts__\n",
"\n",
"In a notebook, the cell containing `search.fit` remains busy until the search finishes. You can inspect files already\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@
"\n",
"The source brightness values are assigned to the **vertices**, rather than one constant brightness being assigned to each triangle. We will examine how brightness is calculated between the vertices in the next block.\n",
"\n",
"The helper below constructs a Delaunay reconstruction using our image-plane overlay. It also lets us vary the regularization coefficient."
"The helper below constructs a Delaunay reconstruction using our image-plane overlay. It also lets us vary the regularization coefficient.\n",
"\n",
"Before defining it, we call `apply_sparse_operator()` on the dataset. This precomputes a sparse operator that the many fits below reuse, which makes each reconstruction faster. Alternatively, passing `al.Settings(lazy_sparse_operator=\"auto\")` to the fit builds this operator automatically the first time a pixelized fit needs it."
]
},
{
Expand Down
31 changes: 29 additions & 2 deletions notebooks/chapter_optional/tutorial_need_for_speed.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@
"NumPy path, which can be useful when diagnosing a calculation or comparing implementations. This does not make a\n",
"gradient-based search independent of derivatives: such a search still needs a supported differentiable calculation.\n",
"\n",
"__Automatic Compute Setup__\n",
"\n",
"Rather than choosing between JAX and NumPy, CPU and GPU yourself, you can pass `setup=\"auto\"` to the analysis (and\n",
"`number_of_cores=\"auto\"` to a Nautilus search). **PyAutoLens** then picks the setup for this likelihood on this\n",
"hardware from timings measured when the software was profiled, and prints the choice, the number of CPU workers and\n",
"a run-time estimate when the fit starts. Tutorial 2 of Chapter 2 shows and explains these lines.\n",
"\n",
"The measurements show that JAX is not always the fastest choice. On a CPU, a pixelized source is usually fitted\n",
"fastest with the NumPy path and a numba sparse operator, run across several CPU cores, while a GPU favours JAX.\n",
"You can override the choice by passing a named setup or setting the `PYAUTO_SETUP` environment variable; the guide\n",
"`scripts/guides/advanced/auto_setup.py` in the `autolens_workspace` describes the options.\n",
"\n",
"__Batches and GPU Memory__\n",
"\n",
"Instead of evaluating one model at a time, a search can evaluate a batch of models together. This can increase the\n",
Expand All @@ -99,12 +111,27 @@
"\n",
"A GPU is also not guaranteed to outperform a CPU for every fit. The outcome depends on the model, dataset, batch\n",
"size, numerical precision and hardware. Compilation and moving data to the device contribute to elapsed time too.\n",
"Compare the same analysis on the hardware available to you, including the costs relevant to your use: a short\n",
"The automatic setup accounts for this using measured timings, but they come from reference machines; if yours differs,\n",
"compare the same analysis on the hardware available to you, including the costs relevant to your use: a short\n",
"interactive calculation and a long modelling campaign can favour different choices.\n",
"\n",
"__Run Time Estimation__\n",
"\n",
"To estimate how long a fit will take, start with a representative analysis: the model, dataset, search settings and\n",
"For a first estimate without running anything, `al.estimate_runtime` combines the profiled time per evaluation for\n",
"a named setup with a rough count of the evaluations a Nautilus search needs:\n",
"\n",
"```\n",
"estimate = al.estimate_runtime(\n",
" dataset=dataset, model=model, search=search, setup=\"jax_cpu_dense_fp64\", number_of_cores=1\n",
")\n",
"print(estimate.line())\n",
"```\n",
"\n",
"This prints a line in the same form as the `Estimate:` line shown when a fit with `setup=\"auto\"` starts, so you can\n",
"compare setups before choosing one. It returns `None` when no profiling measurement matches the setup. The\n",
"evaluation count is a heuristic, so the estimate tells you whether to expect minutes or hours, not the exact duration.\n",
"\n",
"To estimate more carefully, start with a representative analysis: the model, dataset, search settings and\n",
"hardware should resemble those you intend to use. A timing measured with a simpler model or a smaller image may\n",
"give a misleading impression of the work ahead.\n",
"\n",
Expand Down
38 changes: 38 additions & 0 deletions scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,44 @@
Watch the progress of your first run to develop an expectation for your computer. The optional *Need for Speed*
tutorial explains how to investigate performance in more detail.

__Automatic Compute Setup__

The fastest way to run a fit (JAX or NumPy, CPU or GPU, how many CPU cores) depends on the likelihood and on your
hardware. **PyAutoLens** can make this choice for you, using timings measured when the software was profiled. When we
fit without asking for it, as above, the terminal prints one line saying which setup it would choose:

```
Compute setup: not set (auto would choose JAX · CPU · dense · fp64; pass setup="auto")
```

To opt in, pass `setup="auto"` to the analysis and `number_of_cores="auto"` to the search:

```
analysis = al.AnalysisImaging(dataset=dataset, setup="auto")

search = af.Nautilus(..., number_of_cores="auto")
```

When the fit starts, the terminal then prints a short header. On an 8-core laptop, this tutorial's fit gives:

```
Automatic compute setup (CPU/GPU choice from setup="auto"; override with PYAUTO_SETUP or setup=...):
Setup: JAX · CPU · dense · fp64 (rule imaging/mge · extrapolated: 8 cores, rules measured on ≥16-core nodes)
Parallel: 1 worker (JAX on CPU)
Estimate: ~47 min on this hardware (7.1 ms/eval, euclid cell · Nautilus n_live=200 ≈ 1.3e5 evals, heuristic · 1 worker (JAX))
```

- `Setup` is the chosen backend, device, operator and numerical precision, and the rule that picked them. "extrapolated"
means your computer does not match the machines the rules were measured on, with the reason given.
- `Parallel` is how many CPU processes run the search. A JAX setup uses one process and lets JAX use the device.
- `Estimate` is the expected wall time: the time per likelihood evaluation from profiling, multiplied by a rough
count of how many evaluations Nautilus needs. Treat it as a guide to whether a fit takes minutes or hours, not a
promise.

To override the choice, pass a named setup instead (for example `setup="jax_gpu_dense_fp64"` on a GPU) or set the
`PYAUTO_SETUP` environment variable. The guide `scripts/guides/advanced/auto_setup.py` in the `autolens_workspace`
explains the setups in full.

__Notebooks and Scripts__

In a notebook, the cell containing `search.fit` remains busy until the search finishes. You can inspect files already
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@
The source brightness values are assigned to the **vertices**, rather than one constant brightness being assigned to each triangle. We will examine how brightness is calculated between the vertices in the next block.

The helper below constructs a Delaunay reconstruction using our image-plane overlay. It also lets us vary the regularization coefficient.

Before defining it, we call `apply_sparse_operator()` on the dataset. This precomputes a sparse operator that the many fits below reuse, which makes each reconstruction faster. Alternatively, passing `al.Settings(lazy_sparse_operator="auto")` to the fit builds this operator automatically the first time a pixelized fit needs it.
"""

dataset = dataset.apply_sparse_operator()
Expand Down
31 changes: 29 additions & 2 deletions scripts/chapter_optional/tutorial_need_for_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@
NumPy path, which can be useful when diagnosing a calculation or comparing implementations. This does not make a
gradient-based search independent of derivatives: such a search still needs a supported differentiable calculation.

__Automatic Compute Setup__

Rather than choosing between JAX and NumPy, CPU and GPU yourself, you can pass `setup="auto"` to the analysis (and
`number_of_cores="auto"` to a Nautilus search). **PyAutoLens** then picks the setup for this likelihood on this
hardware from timings measured when the software was profiled, and prints the choice, the number of CPU workers and
a run-time estimate when the fit starts. Tutorial 2 of Chapter 2 shows and explains these lines.

The measurements show that JAX is not always the fastest choice. On a CPU, a pixelized source is usually fitted
fastest with the NumPy path and a numba sparse operator, run across several CPU cores, while a GPU favours JAX.
You can override the choice by passing a named setup or setting the `PYAUTO_SETUP` environment variable; the guide
`scripts/guides/advanced/auto_setup.py` in the `autolens_workspace` describes the options.

__Batches and GPU Memory__

Instead of evaluating one model at a time, a search can evaluate a batch of models together. This can increase the
Expand All @@ -94,12 +106,27 @@

A GPU is also not guaranteed to outperform a CPU for every fit. The outcome depends on the model, dataset, batch
size, numerical precision and hardware. Compilation and moving data to the device contribute to elapsed time too.
Compare the same analysis on the hardware available to you, including the costs relevant to your use: a short
The automatic setup accounts for this using measured timings, but they come from reference machines; if yours differs,
compare the same analysis on the hardware available to you, including the costs relevant to your use: a short
interactive calculation and a long modelling campaign can favour different choices.

__Run Time Estimation__

To estimate how long a fit will take, start with a representative analysis: the model, dataset, search settings and
For a first estimate without running anything, `al.estimate_runtime` combines the profiled time per evaluation for
a named setup with a rough count of the evaluations a Nautilus search needs:

```
estimate = al.estimate_runtime(
dataset=dataset, model=model, search=search, setup="jax_cpu_dense_fp64", number_of_cores=1
)
print(estimate.line())
```

This prints a line in the same form as the `Estimate:` line shown when a fit with `setup="auto"` starts, so you can
compare setups before choosing one. It returns `None` when no profiling measurement matches the setup. The
evaluation count is a heuristic, so the estimate tells you whether to expect minutes or hours, not the exact duration.

To estimate more carefully, start with a representative analysis: the model, dataset, search settings and
hardware should resemble those you intend to use. A timing measured with a simpler model or a smaller image may
give a misleading impression of the work ahead.

Expand Down
4 changes: 3 additions & 1 deletion workspace_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
],
"cross_refs": [
"scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py",
"scripts/guides/advanced/auto_setup.py",
"scripts/guides/results/start_here.py"
],
"notebook": "notebooks/chapter_2_lens_modeling/tutorial_2_practicalities.ipynb",
Expand Down Expand Up @@ -675,7 +676,8 @@
"Wrap Up"
],
"cross_refs": [
"chapter_optional/tutorial_searches.py"
"chapter_optional/tutorial_searches.py",
"scripts/guides/advanced/auto_setup.py"
],
"notebook": "notebooks/chapter_optional/tutorial_need_for_speed.ipynb",
"path": "scripts/chapter_optional/tutorial_need_for_speed.py",
Expand Down
Loading