-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathops.py
More file actions
303 lines (246 loc) · 8.73 KB
/
Copy pathops.py
File metadata and controls
303 lines (246 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
Registry of arithmetic *operation* micro-benchmarks.
Where :mod:`benchmarks.registry` benchmarks whole model builds, this benchmarks
single operations — ``var * array``, ``expr + expr``, ``expr <= c`` — with the
operands built *outside* the measured region, so a run isolates one op rather
than a whole build. That granularity attributes regressions to a specific path
(a whole-build benchmark says "kvl got heavier"; an op benchmark says "expr+expr
broadcast got heavier").
One 3-D size profile (``3×4×1000``, ~12 K elements): multi-dim so it exercises
broadcast/alignment across dims; ~MB-scale ops sit above the memory-measurement
noise floor; the asymmetric shape catches dim-order/transpose bugs. CodSpeed
records time *and* memory on every benchmark, so a second size isn't needed to
separate the two signals.
The one axis beyond the op itself is **alignment** — for binary labelled ops,
``match`` (identical coords, the fast path) vs ``broadcast`` (an extra dim → §9
cross-product). That's where the alignment-path regressions live, so it's
first-class, not incidental.
"""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
import numpy as np
import pandas as pd
import xarray as xr
import linopy
# --- size profiles ----------------------------------------------------------
@dataclass(frozen=True)
class Profile:
"""A benchmark size: named dimensions and their lengths."""
key: str
dims: tuple[str, ...]
shape: tuple[int, ...]
@property
def size(self) -> int:
return int(np.prod(self.shape))
GRID = Profile("grid", ("d0", "d1", "d2"), (3, 4, 1000))
# a broadcast operand always adds this one extra dim (kept small so the
# cross-product stays cheap while still exercising the broadcast path)
EXTRA_DIM = "b"
EXTRA_LEN = 5
# --- operand builders (run in setup, never measured) ------------------------
def _coords(dims: tuple[str, ...], shape: tuple[int, ...]) -> dict[str, pd.Index]:
return {d: pd.RangeIndex(n, name=d) for d, n in zip(dims, shape)}
def var(profile: Profile, name: str = "x") -> linopy.Variable:
"""A variable spanning the profile's dimensions."""
m = linopy.Model()
return m.add_variables(
coords=list(_coords(profile.dims, profile.shape).values()),
dims=list(profile.dims),
name=name,
)
def array(profile: Profile) -> xr.DataArray:
"""A coefficient array matching the profile's dims (the ``match`` case)."""
return xr.DataArray(
np.linspace(-1.0, 1.0, profile.size).reshape(profile.shape),
dims=list(profile.dims),
coords=_coords(profile.dims, profile.shape),
)
def extra_array(_: Profile) -> xr.DataArray:
"""An array on a *new* dim — broadcasting it introduces that dim (§9)."""
return xr.DataArray(
np.linspace(1.0, 2.0, EXTRA_LEN),
dims=[EXTRA_DIM],
coords={EXTRA_DIM: pd.RangeIndex(EXTRA_LEN, name=EXTRA_DIM)},
)
def extra_var(profile: Profile, name: str = "z") -> linopy.Variable:
"""A variable on a *new* dim — for var+var broadcast."""
m = linopy.Model()
return m.add_variables(
coords=[pd.RangeIndex(EXTRA_LEN, name=EXTRA_DIM)], dims=[EXTRA_DIM], name=name
)
def expr(profile: Profile) -> linopy.LinearExpression:
"""A linear expression spanning the profile's dims (coeffs vary)."""
return array(profile) * var(profile)
def cond(profile: Profile) -> xr.DataArray:
"""A boolean mask over the profile's dims (~half the slots)."""
return array(profile) > 0.0
def masked_expr(profile: Profile) -> linopy.LinearExpression:
"""An expression carrying absence (§4) — masked in place."""
return expr(profile).where(cond(profile))
def grouped_expr(profile: Profile) -> linopy.LinearExpression:
"""An expression with a coarse ``g`` group coord on the last dim (8 groups)."""
last, n = profile.dims[-1], profile.shape[-1]
g = xr.DataArray(
np.arange(n) * 8 // n,
dims=[last],
coords={last: pd.RangeIndex(n, name=last)},
)
return expr(profile).assign_coords(g=g)
# --- op registry ------------------------------------------------------------
@dataclass(frozen=True)
class OpSpec:
"""One operation benchmark: build operands, then measure ``op(*operands)``."""
name: str
group: str
setup: Callable[[Profile], tuple]
op: Callable[..., object]
OP_REGISTRY: dict[str, OpSpec] = {}
def register_op(
name: str,
group: str,
setup: Callable[[Profile], tuple],
op: Callable[..., object],
) -> None:
if name in OP_REGISTRY:
raise ValueError(f"op {name!r} already registered")
OP_REGISTRY[name] = OpSpec(name, group, setup, op)
def iter_ops() -> list[OpSpec]:
"""Every registered op — the pytest parametrize source."""
return list(OP_REGISTRY.values())
# --- the operations ---------------------------------------------------------
# Binary labelled ops register a `match` and a `broadcast` variant; the
# alignment case is baked into the operands the setup builds.
# scaling / construction
register_op("var_mul_scalar", "scale", lambda p: (var(p),), lambda x: 2.0 * x)
register_op("var_div_scalar", "scale", lambda p: (var(p),), lambda x: x / 2.0)
register_op("var_neg", "scale", lambda p: (var(p),), lambda x: -x)
register_op("var_to_linexpr", "scale", lambda p: (var(p),), lambda x: 1 * x)
register_op(
"var_mul_array_match", "scale", lambda p: (var(p), array(p)), lambda x, a: a * x
)
register_op(
"var_mul_array_bcast",
"scale",
lambda p: (var(p), extra_array(p)),
lambda x, a: a * x,
)
# variable arithmetic
register_op("var_add_scalar", "var_arith", lambda p: (var(p),), lambda x: x + 2.0)
register_op(
"var_add_array_match", "var_arith", lambda p: (var(p), array(p)), lambda x, a: x + a
)
register_op(
"var_add_array_bcast",
"var_arith",
lambda p: (var(p), extra_array(p)),
lambda x, a: x + a,
)
register_op(
"var_add_var_match",
"var_arith",
lambda p: (var(p, "x"), var(p, "y")),
lambda x, y: x + y,
)
register_op(
"var_add_var_bcast",
"var_arith",
lambda p: (var(p, "x"), extra_var(p)),
lambda x, z: x + z,
)
register_op(
"var_sub_var_match",
"var_arith",
lambda p: (var(p, "x"), var(p, "y")),
lambda x, y: x - y,
)
# quadratic
register_op(
"var_mul_var", "quad", lambda p: (var(p, "x"), var(p, "y")), lambda x, y: x * y
)
register_op(
"expr_mul_var", "quad", lambda p: (expr(p), var(p, "y")), lambda e, y: e * y
)
# expression arithmetic
register_op("expr_add_scalar", "expr_arith", lambda p: (expr(p),), lambda e: e + 2.0)
register_op(
"expr_add_array_match",
"expr_arith",
lambda p: (expr(p), array(p)),
lambda e, a: e + a,
)
register_op(
"expr_add_array_bcast",
"expr_arith",
lambda p: (expr(p), extra_array(p)),
lambda e, a: e + a,
)
register_op(
"expr_add_var", "expr_arith", lambda p: (expr(p), var(p, "y")), lambda e, y: e + y
)
register_op(
"expr_add_expr_match",
"expr_arith",
lambda p: (expr(p), expr(p)),
lambda a, b: a + b,
)
register_op(
"expr_add_expr_bcast",
"expr_arith",
lambda p: (expr(p), extra_array(p) * var(p)),
lambda a, b: a + b,
)
register_op(
"expr_sub_expr_match",
"expr_arith",
lambda p: (expr(p), expr(p)),
lambda a, b: a - b,
)
register_op("expr_mul_scalar", "expr_arith", lambda p: (expr(p),), lambda e: 2.0 * e)
register_op(
"expr_mul_array_match",
"expr_arith",
lambda p: (expr(p), array(p)),
lambda e, a: a * e,
)
register_op(
"expr_mul_array_bcast",
"expr_arith",
lambda p: (expr(p), extra_array(p)),
lambda e, a: a * e,
)
# reductions
register_op("var_sum_dim", "reduce", lambda p: (var(p),), lambda x: x.sum("d0"))
register_op("expr_sum_dim", "reduce", lambda p: (expr(p),), lambda e: e.sum("d0"))
register_op("expr_sum_all", "reduce", lambda p: (expr(p),), lambda e: e.sum())
# constraint construction
register_op("con_le_scalar", "constraint", lambda p: (expr(p),), lambda e: e <= 2.0)
register_op(
"con_le_array", "constraint", lambda p: (expr(p), array(p)), lambda e, a: e <= a
)
register_op(
"con_eq_expr", "constraint", lambda p: (expr(p), expr(p)), lambda a, b: a == b
)
# absence / masking (§4–§7)
register_op("expr_where", "mask", lambda p: (expr(p), cond(p)), lambda e, c: e.where(c))
register_op("expr_fillna", "mask", lambda p: (masked_expr(p),), lambda e: e.fillna(0.0))
register_op(
"expr_add_masked",
"mask",
lambda p: (expr(p), masked_expr(p)),
lambda a, b: a + b,
)
# groupby
register_op(
"expr_groupby_sum",
"groupby",
lambda p: (grouped_expr(p),),
lambda e: e.groupby("g").sum(),
)
# N-way assembly (constraint building sums many terms)
register_op(
"merge_sum",
"merge",
lambda p: tuple(expr(p) for _ in range(8)),
lambda *es: sum(es[1:], es[0]),
)