Skip to content

Commit 9aa6e50

Browse files
Removed identity element argument from 'reduce' ops (#1164)
* Removed identity element argument from 'reduce' ops * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Addressed feedback --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5cfd911 commit 9aa6e50

55 files changed

Lines changed: 165 additions & 242 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ChangeLog.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
### Language
66

7+
* BREAKING: with the introduction of `infinity` to the language in `v0.25` all reduction operations have
8+
sensible zero-dimensional values. Therefore the following operations no longer take the identity element
9+
as an argument, i.e.:
10+
```
11+
reduceAdd e xs -> reduceAdd xs (if 0D returns 0)
12+
reduceMul e xs -> reduceMul xs (if 0D returns 1)
13+
reduceMin e xs -> reduceMin xs (if 0D returns infinity)
14+
reduceMax e xs -> reduceMax xs (if 0D returns -infinity)
15+
reduceAnd e xs -> reduceAnd xs (if 0D returns True)
16+
reduceOr e xs -> reduceOr xs (if 0D returns False)
17+
```
18+
719
* Added the operators:
820
```
921
^ : Tensor Real ds -> Real -> Tensor Real ds

docs/language/tensors.rst

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,14 @@ The following operations over tensors are currently supported:
130130
- |backendall_full|
131131
* - | Reduce
132132
| and
133-
- ``reduceAnd e t``
134-
- | ``Bool →``
135-
| ``Tensor Bool ds →``
133+
- ``reduceAnd t``
134+
- | ``Tensor Bool ds →``
136135
| ``Bool``
137136
- |backendall_full|
138137
* - | Reduce
139138
| or
140-
- ``reduceOr e t``
141-
- | ``Bool →``
142-
| ``Tensor Bool ds →``
139+
- ``reduceOr t``
140+
- | ``Tensor Bool ds →``
143141
| ``Bool``
144142
- |backendall_full|
145143
* - | Pointwise
@@ -209,26 +207,26 @@ The following operations over tensors are currently supported:
209207
| |backendisabelle_easy|
210208
* - | Reduce
211209
| add
212-
- ``reduceAdd e t``
213-
- | ``A → Tensor A ds → A``
210+
- ``reduceAdd t``
211+
- | ``Tensor A ds → A``
214212
| (if ``A`` supports ``+``)
215213
- |backendall_full|
216214
* - | Reduce
217215
| multiply
218-
- ``reduceMul e t``
219-
- | ``A → Tensor A ds → A``
216+
- ``reduceMul t``
217+
- | ``Tensor A ds → A``
220218
| (if ``A`` supports ``*``)
221219
- |backendall_full|
222220
* - | Reduce
223221
| min
224-
- ``reduceMin e t``
225-
- | ``A → Tensor A ds → A``
222+
- ``reduceMin t``
223+
- | ``Tensor A ds → A``
226224
| (if ``A`` supports ``min``)
227225
- |backendall_full|
228226
* - | Reduce
229227
| max
230-
- ``reduceMax e t``
231-
- | ``A → Tensor A ds → A``
228+
- ``reduceMax t``
229+
- | ``Tensor A ds → A``
232230
| (if ``A`` supports ``max``)
233231
- |backendall_full|
234232

vehicle-python/src/vehicle_lang/_ast/_nodes.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ class Var(Expression):
208208

209209
@dataclass(frozen=True)
210210
class RatTensor(Expression):
211-
"""RatTensor (Tensor ExtendedFraction) from JSON"""
212211

213212
contents: Tensor
214213

@@ -222,109 +221,91 @@ class NegRatTensor(Expression):
222221

223222
@dataclass(frozen=True)
224223
class AddRatTensor(Expression):
225-
"""Binary addition: AddRatTensor left right - provides App interface for translation"""
226224

227225
x: Expression
228226
y: Expression
229227

230228

231229
@dataclass(frozen=True)
232230
class SubRatTensor(Expression):
233-
"""Binary subtraction: SubRatTensor left right - behaves like App for translation"""
234231

235232
x: Expression
236233
y: Expression
237234

238235

239236
@dataclass(frozen=True)
240237
class MulRatTensor(Expression):
241-
"""Binary multiplication: MulRatTensor left right"""
242238

243239
x: Expression
244240
y: Expression
245241

246242

247243
@dataclass(frozen=True)
248244
class DivRatTensor(Expression):
249-
"""Binary division: DivRatTensor left right"""
250245

251246
x: Expression
252247
y: Expression
253248

254249

255250
@dataclass(frozen=True)
256251
class MinRatTensor(Expression):
257-
"""Binary minimum: MinRatTensor left right"""
258252

259253
x: Expression
260254
y: Expression
261255

262256

263257
@dataclass(frozen=True)
264258
class MaxRatTensor(Expression):
265-
"""Binary maximum: MaxRatTensor left right"""
266259

267260
x: Expression
268261
y: Expression
269262

270263

271264
@dataclass(frozen=True)
272265
class PowRatTensor(Expression):
273-
"""Binary power: PowRatTensor left right"""
274266

275267
x: Expression
276268
y: Expression
277269

278270

279271
@dataclass(frozen=True)
280272
class LogRatTensor(Expression):
281-
"""Pointwise natural log: LogRatTensor x"""
282273

283274
x: Expression
284275

285276

286277
@dataclass(frozen=True)
287278
class ExpRatTensor(Expression):
288-
"""Pointwise exponential: ExpRatTensor x"""
289279

290280
x: Expression
291281

292282

293283
@dataclass(frozen=True)
294284
class ReduceAddRatTensor(Expression):
295-
"""Reduce addition: ReduceAddRatTensor expr dims"""
296285

297-
f: Expression
298286
x: Expression
299287

300288

301289
@dataclass(frozen=True)
302290
class ReduceMulRatTensor(Expression):
303-
"""Reduce multiplication: ReduceMulRatTensor expr dims"""
304291

305-
f: Expression
306292
x: Expression
307293

308294

309295
@dataclass(frozen=True)
310296
class ReduceMinRatTensor(Expression):
311-
"""Reduce minimum: ReduceMinRatTensor expr dims"""
312297

313-
f: Expression
314298
x: Expression
315299

316300

317301
@dataclass(frozen=True)
318302
class ReduceMaxRatTensor(Expression):
319-
"""Reduce maximum: ReduceMaxRatTensor expr dims"""
320303

321-
f: Expression
322304
x: Expression
323305

324306

325307
@dataclass(frozen=True)
326308
class SearchRatTensor(Expression):
327-
"""Search tensor: SearchRatTensor reductionOp lowerBound upperBound searchLambda"""
328309

329310
name: str
330311
reduction_op: Expression
@@ -337,19 +318,17 @@ class SearchRatTensor(Expression):
337318

338319
@dataclass(frozen=True)
339320
class Dimension(Expression):
340-
"""Dimension Int - for JSON parsing"""
341321

342322
value: int
343323

344324

345325
@dataclass(frozen=True)
346326
class DimensionNil(Expression):
347-
"""DimensionNil - for JSON parsing"""
327+
pass
348328

349329

350330
@dataclass(frozen=True)
351331
class DimensionLookup(Expression):
352-
"""Dimension lookup: DimensionLookup tensor index"""
353332

354333
xs: Expression
355334
i: Expression
@@ -368,15 +347,13 @@ class DimensionIndex(Expression):
368347

369348
@dataclass(frozen=True)
370349
class ConstTensor(Expression):
371-
"""ConstTensor shape value - for JSON parsing"""
372350

373351
c: ExtendedFraction
374352
ds: Sequence[int]
375353

376354

377355
@dataclass(frozen=True)
378356
class StackTensor(Expression):
379-
"""StackTensor : StackTensor tensor_list"""
380357

381358
xs: Sequence[Expression]
382359

vehicle-python/src/vehicle_lang/loss/_abc/_builtins.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ def LogRatTensor(self, x: vcl.Tensor) -> vcl.Tensor: ...
5454
def ExpRatTensor(self, x: vcl.Tensor) -> vcl.Tensor: ...
5555

5656
@abstractmethod
57-
def ReduceAddRatTensor(self, e: vcl.Rat, xs: vcl.Tensor) -> vcl.Tensor: ...
57+
def ReduceAddRatTensor(self, xs: vcl.Tensor) -> vcl.Tensor: ...
5858

5959
@abstractmethod
60-
def ReduceMulRatTensor(self, e: vcl.Rat, x: vcl.Tensor) -> vcl.Tensor: ...
60+
def ReduceMulRatTensor(self, x: vcl.Tensor) -> vcl.Tensor: ...
6161

6262
@abstractmethod
63-
def ReduceMinRatTensor(self, e: vcl.Rat, x: vcl.Tensor) -> vcl.Tensor: ...
63+
def ReduceMinRatTensor(self, x: vcl.Tensor) -> vcl.Tensor: ...
6464

6565
@abstractmethod
66-
def ReduceMaxRatTensor(self, e: vcl.Rat, x: vcl.Tensor) -> vcl.Tensor: ...
66+
def ReduceMaxRatTensor(self, x: vcl.Tensor) -> vcl.Tensor: ...
6767

6868
@abstractmethod
6969
def DimensionLookup(self, xs: vcl.Tensor, i: vcl.Index) -> vcl.Tensor: ...

vehicle-python/src/vehicle_lang/loss/_python/_translation.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ def translate_ReduceAddRatTensor(
287287
"""Translate ReduceAddRatTensor to builtin call."""
288288
return py_app(
289289
py_builtin("ReduceAddRatTensor", provenance=vcl.MISSING),
290-
self.translate_expression(expression.f), # Note: using current field names
291290
self.translate_expression(expression.x),
292291
provenance=vcl.MISSING,
293292
)
@@ -298,7 +297,6 @@ def translate_ReduceMulRatTensor(
298297
"""Translate ReduceMulRatTensor to builtin call."""
299298
return py_app(
300299
py_builtin("ReduceMulRatTensor", provenance=vcl.MISSING),
301-
self.translate_expression(expression.f),
302300
self.translate_expression(expression.x),
303301
provenance=vcl.MISSING,
304302
)
@@ -309,7 +307,6 @@ def translate_ReduceMinRatTensor(
309307
"""Translate ReduceMinRatTensor to builtin call."""
310308
return py_app(
311309
py_builtin("ReduceMinRatTensor", provenance=vcl.MISSING),
312-
self.translate_expression(expression.f),
313310
self.translate_expression(expression.x),
314311
provenance=vcl.MISSING,
315312
)
@@ -320,20 +317,15 @@ def translate_ReduceMaxRatTensor(
320317
"""Translate ReduceMaxRatTensor to builtin call."""
321318
return py_app(
322319
py_builtin("ReduceMaxRatTensor", provenance=vcl.MISSING),
323-
self.translate_expression(expression.f),
324320
self.translate_expression(expression.x),
325321
provenance=vcl.MISSING,
326322
)
327323

328324
def translate_SearchRatTensor(self, expression: vcl.SearchRatTensor) -> py.expr:
329325
"""Translate SearchRatTensor to builtin call.
330326
331-
The reduction_op is a curried function (λe. λxs. reduce e xs) where:
332-
- e is the identity element (a 0-dimensional tensor)
327+
The reduction_op is a curried function (λxs. reduce xs) where:
333328
- xs is the sequence of samples to reduce
334-
335-
Since the Python Reduce* functions don't actually use the identity parameter,
336-
we pass a dummy 0-dimensional tensor with value 0.
337329
"""
338330
# Call sampler once to get samples
339331
sampler_call = py_app(
@@ -350,27 +342,9 @@ def translate_SearchRatTensor(self, expression: vcl.SearchRatTensor) -> py.expr:
350342
provenance=vcl.MISSING,
351343
)
352344

353-
# Create a dummy identity element (0-dimensional tensor with value 0)
354-
# The Python Reduce* implementations don't actually use this parameter
355-
identity = py_app(
356-
py_builtin("ConstTensor", provenance=vcl.MISSING),
357-
py.Constant(value=0, **asdict(vcl.MISSING)),
358-
py_app(
359-
py_builtin("DimensionNil", provenance=vcl.MISSING),
360-
provenance=vcl.MISSING,
361-
),
362-
provenance=vcl.MISSING,
363-
)
364-
365-
# Apply as: reduction_op(identity)(samples)
366-
partial_reduction = py_app(
367-
self.translate_expression(expression.reduction_op),
368-
identity,
369-
provenance=vcl.MISSING,
370-
)
371-
345+
# Apply as: reduction_op(samples)
372346
return py_app(
373-
partial_reduction,
347+
self.translate_expression(expression.reduction_op),
374348
sampler_call,
375349
provenance=vcl.MISSING,
376350
)

vehicle-python/src/vehicle_lang/loss/_pytorch/_builtins.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,31 +118,19 @@ def ExpRatTensor(self, x: torch.Tensor) -> torch.Tensor:
118118
return torch.exp(torch.as_tensor(x))
119119

120120
@override
121-
def ReduceAddRatTensor(
122-
self, e: float, xs: torch.Tensor | Sequence[torch.Tensor]
123-
) -> torch.Tensor:
124-
xs = torch.stack(list(xs))
125-
return torch.add(torch.sum(xs), e)
121+
def ReduceAddRatTensor(self, xs: torch.Tensor) -> torch.Tensor:
122+
return torch.sum(xs)
126123

127124
@override
128-
def ReduceMulRatTensor(
129-
self, e: float, x: torch.Tensor | Sequence[torch.Tensor]
130-
) -> torch.Tensor:
131-
x = torch.stack(list(x))
132-
return torch.mul(torch.prod(x), e)
125+
def ReduceMulRatTensor(self, x: torch.Tensor) -> torch.Tensor:
126+
return torch.prod(x)
133127

134128
@override
135-
def ReduceMinRatTensor(
136-
self, e: float, x: torch.Tensor | Sequence[torch.Tensor]
137-
) -> torch.Tensor:
138-
x = torch.stack([torch.Tensor(e)] + list(x))
129+
def ReduceMinRatTensor(self, x: torch.Tensor) -> torch.Tensor:
139130
return torch.min(x)
140131

141132
@override
142-
def ReduceMaxRatTensor(
143-
self, e: float, x: torch.Tensor | Sequence[torch.Tensor]
144-
) -> torch.Tensor:
145-
x = torch.stack([torch.Tensor(e)] + list(x))
133+
def ReduceMaxRatTensor(self, x: torch.Tensor) -> torch.Tensor:
146134
return torch.max(x)
147135

148136
@override

0 commit comments

Comments
 (0)