diff --git a/notebooks/chapter_2_lens_modeling/tutorial_2_practicalities.ipynb b/notebooks/chapter_2_lens_modeling/tutorial_2_practicalities.ipynb index 49a97e5..e25c53b 100644 --- a/notebooks/chapter_2_lens_modeling/tutorial_2_practicalities.ipynb +++ b/notebooks/chapter_2_lens_modeling/tutorial_2_practicalities.ipynb @@ -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", diff --git a/notebooks/chapter_3_pixelizations/tutorial_6_adaptive_reconstruction.ipynb b/notebooks/chapter_3_pixelizations/tutorial_6_adaptive_reconstruction.ipynb index dca5de6..98f5514 100644 --- a/notebooks/chapter_3_pixelizations/tutorial_6_adaptive_reconstruction.ipynb +++ b/notebooks/chapter_3_pixelizations/tutorial_6_adaptive_reconstruction.ipynb @@ -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." ] }, { diff --git a/notebooks/chapter_optional/tutorial_need_for_speed.ipynb b/notebooks/chapter_optional/tutorial_need_for_speed.ipynb index 2a585c2..90990db 100644 --- a/notebooks/chapter_optional/tutorial_need_for_speed.ipynb +++ b/notebooks/chapter_optional/tutorial_need_for_speed.ipynb @@ -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", @@ -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", diff --git a/scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py b/scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py index ae32ae0..c64f9c3 100755 --- a/scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py +++ b/scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py @@ -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 diff --git a/scripts/chapter_3_pixelizations/tutorial_6_adaptive_reconstruction.py b/scripts/chapter_3_pixelizations/tutorial_6_adaptive_reconstruction.py index 5b37f51..97ffcb5 100644 --- a/scripts/chapter_3_pixelizations/tutorial_6_adaptive_reconstruction.py +++ b/scripts/chapter_3_pixelizations/tutorial_6_adaptive_reconstruction.py @@ -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() diff --git a/scripts/chapter_optional/tutorial_need_for_speed.py b/scripts/chapter_optional/tutorial_need_for_speed.py index fc40f2c..fc251ef 100755 --- a/scripts/chapter_optional/tutorial_need_for_speed.py +++ b/scripts/chapter_optional/tutorial_need_for_speed.py @@ -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 @@ -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. diff --git a/workspace_index.json b/workspace_index.json index 2dfa62a..bf1874a 100644 --- a/workspace_index.json +++ b/workspace_index.json @@ -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", @@ -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",