|
| 1 | +"""Measure the LearnerND speedup from `simplices_containing` and `default_loss`. |
| 2 | +
|
| 3 | +This monkeypatches adaptive (the PR #493 branch, with the Rust backend |
| 4 | +active) to use the two batched APIs, the way the adaptive-side integration |
| 5 | +would: |
| 6 | +
|
| 7 | +- ``tell_pending`` distributes a pending point over the simplices containing |
| 8 | + it with one ``tri.simplices_containing(point, simplex=hint)`` call instead |
| 9 | + of a Python loop of per-neighbour ``tri.point_in_simplex`` checks. |
| 10 | +- The default loss is the Rust ``default_loss`` instead of the Python |
| 11 | + wrapper around ``simplex_volume_in_embedding``. |
| 12 | +
|
| 13 | +Requirements: |
| 14 | + pip install adaptive-triangulation |
| 15 | + pip install "adaptive @ git+https://github.com/python-adaptive/adaptive@refs/pull/493/head" |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import time |
| 21 | +from typing import TYPE_CHECKING |
| 22 | + |
| 23 | +import adaptive_triangulation as at |
| 24 | +import numpy as np |
| 25 | +from adaptive.learner import learnerND as lnd_mod |
| 26 | +from adaptive.learner.learnerND import LearnerND |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + from collections.abc import Callable, Sequence |
| 30 | + |
| 31 | +reference_default_loss = lnd_mod.default_loss |
| 32 | + |
| 33 | + |
| 34 | +def ring_of_fire(xy: Sequence[float]) -> float: |
| 35 | + x, y = xy |
| 36 | + a, d = 0.2, 0.5 |
| 37 | + return x + np.exp(-((x**2 + y**2 - d**2) ** 2) / a**4) |
| 38 | + |
| 39 | + |
| 40 | +def sphere_of_fire(xyz: Sequence[float]) -> float: |
| 41 | + x, y, z = xyz |
| 42 | + a, d = 0.2, 0.5 |
| 43 | + return x + np.exp(-((x**2 + y**2 + z**2 - d**2) ** 2) / a**4) |
| 44 | + |
| 45 | + |
| 46 | +def _add_pending_to_simplex(self: LearnerND, point: tuple, simplex: tuple) -> tuple: |
| 47 | + """`_try_adding_pending_point_to_simplex` without the containment check |
| 48 | + (the caller already knows `simplex` contains `point`).""" |
| 49 | + if simplex not in self._subtriangulations: |
| 50 | + vertices = self.tri.get_vertices(simplex) |
| 51 | + self._subtriangulations[simplex] = self._triangulation_class(vertices) |
| 52 | + self._pending_to_simplex[point] = simplex |
| 53 | + return self._subtriangulations[simplex].add_point(point) |
| 54 | + |
| 55 | + |
| 56 | +def tell_pending_batched(self: LearnerND, point: tuple, *, simplex: tuple | None = None) -> None: |
| 57 | + """tell_pending with the point_in_simplex loop replaced by one |
| 58 | + `simplices_containing` call (the `simplex` argument becomes the hint).""" |
| 59 | + point = tuple(point) |
| 60 | + if not self.inside_bounds(point): |
| 61 | + return |
| 62 | + self.pending_points.add(point) |
| 63 | + if self.tri is None: |
| 64 | + return |
| 65 | + containing = self.tri.simplices_containing(point, simplex=simplex) |
| 66 | + for simpl in containing: |
| 67 | + _, to_add = _add_pending_to_simplex(self, point, simpl) |
| 68 | + if to_add is None: |
| 69 | + continue |
| 70 | + self._update_subsimplex_losses(simpl, to_add) |
| 71 | + |
| 72 | + |
| 73 | +def run( |
| 74 | + func: Callable[[Sequence[float]], float], |
| 75 | + bounds: list[tuple[float, float]], |
| 76 | + n_points: int, |
| 77 | + *, |
| 78 | + rust_loss: bool, |
| 79 | + batched_tell: bool, |
| 80 | +) -> tuple[float, LearnerND]: |
| 81 | + lnd_mod.default_loss = at.default_loss if rust_loss else reference_default_loss |
| 82 | + learner = LearnerND(func, bounds=bounds) |
| 83 | + if batched_tell: |
| 84 | + learner.tell_pending = tell_pending_batched.__get__(learner) |
| 85 | + t0 = time.perf_counter() |
| 86 | + for _ in range(n_points): |
| 87 | + points, _ = learner.ask(1) |
| 88 | + for p in points: |
| 89 | + learner.tell(p, func(p)) |
| 90 | + elapsed = time.perf_counter() - t0 |
| 91 | + return elapsed, learner |
| 92 | + |
| 93 | + |
| 94 | +CONFIGS = [ |
| 95 | + ("baseline (Rust backend, PR #493)", False, False), |
| 96 | + ("+ rust default_loss", True, False), |
| 97 | + ("+ simplices_containing tell_pending", False, True), |
| 98 | + ("+ both", True, True), |
| 99 | +] |
| 100 | + |
| 101 | +for name, func, bounds, n in [ |
| 102 | + ("2D ring_of_fire", ring_of_fire, [(-1, 1), (-1, 1)], 3000), |
| 103 | + ("3D sphere_of_fire", sphere_of_fire, [(-1, 1), (-1, 1), (-1, 1)], 1500), |
| 104 | +]: |
| 105 | + print(f"\n{name}, {n} points:") |
| 106 | + baseline = None |
| 107 | + base_points = None |
| 108 | + for label, rust_loss, batched_tell in CONFIGS: |
| 109 | + best = min( |
| 110 | + run(func, bounds, n, rust_loss=rust_loss, batched_tell=batched_tell)[0] |
| 111 | + for _ in range(3) |
| 112 | + ) |
| 113 | + _, learner = run(func, bounds, n, rust_loss=rust_loss, batched_tell=batched_tell) |
| 114 | + pts = sorted(learner.data) |
| 115 | + if baseline is None: |
| 116 | + baseline, base_points = best, pts |
| 117 | + same = "identical points" if pts == base_points else "DIFFERENT POINTS" |
| 118 | + print(f" {label:40s} {best:6.2f}s ({baseline / best:4.2f}x, {same})") |
0 commit comments