guldo-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- guldo/__init__.py +0 -0
- guldo/_src/COMPILER.md +329 -0
- guldo/_src/DEPLOY.md +200 -0
- guldo/_src/LAUNCH.md +85 -0
- guldo/_src/PROTOCOL.md +294 -0
- guldo/_src/RULES.md +25 -0
- guldo/_src/SUBMIT.md +170 -0
- guldo/_src/__init__.py +0 -0
- guldo/_src/attest.py +257 -0
- guldo/_src/build_site.py +958 -0
- guldo/_src/bundle_templates/check.py +1382 -0
- guldo/_src/bundle_templates/qcheck.py +253 -0
- guldo/_src/bundle_templates/qscore.py +402 -0
- guldo/_src/bundle_templates/score.py +649 -0
- guldo/_src/certificate.py +828 -0
- guldo/_src/certs_site.py +108 -0
- guldo/_src/claims.py +343 -0
- guldo/_src/compile_domain.py +1312 -0
- guldo/_src/compile_quality.py +286 -0
- guldo/_src/count_runner.py +248 -0
- guldo/_src/counted.py +274 -0
- guldo/_src/declared.py +215 -0
- guldo/_src/deploy/Caddyfile +48 -0
- guldo/_src/deploy/ledger-backup.service +9 -0
- guldo/_src/deploy/ledger-backup.sh +73 -0
- guldo/_src/deploy/ledger-backup.timer +10 -0
- guldo/_src/deploy/ledger-export.service +12 -0
- guldo/_src/deploy/ledger-export.timer +9 -0
- guldo/_src/deploy/ledger-web.service +38 -0
- guldo/_src/deploy/pin_cpu.sh +34 -0
- guldo/_src/deploy/provision_eval.sh +128 -0
- guldo/_src/deploy_smoke.py +148 -0
- guldo/_src/dlog_experiment.py +165 -0
- guldo/_src/domains/aidecode/compiled.json +586 -0
- guldo/_src/domains/aidecode/decl.py +134 -0
- guldo/_src/domains/aidecode/reference.py +73 -0
- guldo/_src/domains/aiinfer/compiled.json +722 -0
- guldo/_src/domains/aiinfer/decl.py +91 -0
- guldo/_src/domains/aiinfer/reference.py +42 -0
- guldo/_src/domains/dlog/compiled.json +248 -0
- guldo/_src/domains/dlog/decl.py +41 -0
- guldo/_src/domains/dlog/reference.py +10 -0
- guldo/_src/domains/maxcut/compiled.json +291 -0
- guldo/_src/domains/maxcut/decl.py +46 -0
- guldo/_src/domains/maxcut/reference.py +16 -0
- guldo/_src/drift_check.py +89 -0
- guldo/_src/export_bundle.py +750 -0
- guldo/_src/extrapolate.py +326 -0
- guldo/_src/families.py +53 -0
- guldo/_src/gen_cheat_table.py +36 -0
- guldo/_src/gpu_meter.py +634 -0
- guldo/_src/gpu_runner.py +63 -0
- guldo/_src/harness.py +273 -0
- guldo/_src/instances.py +86 -0
- guldo/_src/instances_factor.py +40 -0
- guldo/_src/instances_mlwe.py +69 -0
- guldo/_src/judge.py +278 -0
- guldo/_src/limits_plan.py +158 -0
- guldo/_src/official.py +189 -0
- guldo/_src/ops.py +281 -0
- guldo/_src/portal.py +313 -0
- guldo/_src/portal_core.py +57 -0
- guldo/_src/power.py +174 -0
- guldo/_src/prereg.py +173 -0
- guldo/_src/public_portal.py +500 -0
- guldo/_src/quality_core.py +126 -0
- guldo/_src/recheck.py +236 -0
- guldo/_src/remeasure.py +88 -0
- guldo/_src/report_html.py +493 -0
- guldo/_src/results/.keep +0 -0
- guldo/_src/rounds.py +846 -0
- guldo/_src/runner.py +52 -0
- guldo/_src/sandbox/Dockerfile +11 -0
- guldo/_src/sandbox/Dockerfile.v2 +19 -0
- guldo/_src/sandbox/README.md +40 -0
- guldo/_src/sandbox/linux_selftest.py +117 -0
- guldo/_src/sandbox/meter.py +89 -0
- guldo/_src/sandbox/meter_core.py +28 -0
- guldo/_src/site_assets.py +176 -0
- guldo/_src/site_live_check.py +79 -0
- guldo/_src/solvers/aidecode_flops_kv.py +73 -0
- guldo/_src/solvers/aidecode_flops_lossy.py +73 -0
- guldo/_src/solvers/aidecode_flops_ref.py +64 -0
- guldo/_src/solvers/aidecode_kv.py +76 -0
- guldo/_src/solvers/aidecode_kv_f32.py +77 -0
- guldo/_src/solvers/aidecode_lossy.py +76 -0
- guldo/_src/solvers/aidecode_repeat.py +5 -0
- guldo/_src/solvers/aiinfer_batched.py +40 -0
- guldo/_src/solvers/aiinfer_f32.py +41 -0
- guldo/_src/solvers/aiinfer_gpu_batched.py +42 -0
- guldo/_src/solvers/aiinfer_gpu_ref.py +45 -0
- guldo/_src/solvers/aiinfer_lossy.py +42 -0
- guldo/_src/solvers/baseline_bkz.py +14 -0
- guldo/_src/solvers/cheat_lookup.py +24 -0
- guldo/_src/solvers/cheat_table.json +1 -0
- guldo/_src/solvers/cheat_table_v1_stale.json +1 -0
- guldo/_src/solvers/daemon_cpu_hider.py +17 -0
- guldo/_src/solvers/dlog_bsgs.py +19 -0
- guldo/_src/solvers/dlog_stride4.py +18 -0
- guldo/_src/solvers/egg_base.py +11 -0
- guldo/_src/solvers/egg_eightaxis.py +68 -0
- guldo/_src/solvers/egg_template.py +11 -0
- guldo/_src/solvers/factor_brent.py +34 -0
- guldo/_src/solvers/factor_floyd.py +19 -0
- guldo/_src/solvers/factor_trial_fast_py.py +21 -0
- guldo/_src/solvers/factor_trial_np.py +21 -0
- guldo/_src/solvers/factor_trial_py.py +12 -0
- guldo/_src/solvers/fixed_overhead_ref.py +21 -0
- guldo/_src/solvers/fork_bomb.py +18 -0
- guldo/_src/solvers/gpu_noop.py +6 -0
- guldo/_src/solvers/hostile_probe.py +36 -0
- guldo/_src/solvers/kill_meter.py +27 -0
- guldo/_src/solvers/memory_bomb.py +10 -0
- guldo/_src/solvers/noop.py +5 -0
- guldo/_src/solvers/orphan_cpu_hider.py +16 -0
- guldo/_src/solvers/planted_peek.py +23 -0
- guldo/_src/solvers/sleeping_daemon_hang.py +12 -0
- guldo/_src/solvers/small_n_trick.py +22 -0
- guldo/_src/solvers/starter_template.py +21 -0
- guldo/_src/solvers/stdout_flood.py +9 -0
- guldo/_src/solvers/tmp_fill.py +10 -0
- guldo/_src/solvers/tuned_bkz.py +25 -0
- guldo/_src/static_check.py +214 -0
- guldo/_src/submit_check.py +166 -0
- guldo/_src/svp.py +889 -0
- guldo/_src/tensorops.py +304 -0
- guldo/_src/tflops.py +453 -0
- guldo/_src/tflops_runner.py +134 -0
- guldo/cli.py +60 -0
- guldo_cli-0.1.0.dist-info/METADATA +30 -0
- guldo_cli-0.1.0.dist-info/RECORD +134 -0
- guldo_cli-0.1.0.dist-info/WHEEL +5 -0
- guldo_cli-0.1.0.dist-info/entry_points.txt +2 -0
- guldo_cli-0.1.0.dist-info/top_level.txt +1 -0
guldo/_src/tensorops.py
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"""The tensor API a FLOP-counted solver is written in (see tflops.py). Every operation charges an analytic cost computed from SHAPES ONLY, so the total is exactly
|
|
2
|
+
reproducible on any machine: it does not depend on the clock, the CPU, BLAS, threads, numpy's version or the dtype.
|
|
3
|
+
|
|
4
|
+
Cost table (one unit = one scalar operation; this is a convention, stated in every verdict, not a measurement):
|
|
5
|
+
elementwise + - * / neg exp sqrt relu size of the result
|
|
6
|
+
matmul / @ (..., m, k) @ (k, n) 2 * size(result) * k
|
|
7
|
+
einsum two operands 2 * product of every index's extent
|
|
8
|
+
sum max mean argmax size of the input
|
|
9
|
+
stream SplitMix64 floats 10 per element (the integer hash of the model-regeneration step)
|
|
10
|
+
tolist size of the tensor (at most 2**22 elements)
|
|
11
|
+
FREE reshape transpose take concat slicing item-assignment zeros tensor causal_mask (data movement and constants)
|
|
12
|
+
Memory: no tensor above 2**27 elements and no more than 2**28 elements alive at once (views counted as copies); a request past either fails like an exhausted budget,
|
|
13
|
+
BEFORE anything is allocated. Python-level memory (a list made with `[0.0] * 10**9`) is not covered here: tflops.py's parent-side resident-memory watchdog stops that.
|
|
14
|
+
|
|
15
|
+
What this is blind to, on purpose: memory traffic, cache behaviour, kernel quality, dtype and vectorisation. It certifies how much ARITHMETIC a solver asks for, so it
|
|
16
|
+
can certify "N times fewer operations" (a KV cache, pruning, early exit, a smaller model at equal output) and cannot certify "faster at the same arithmetic".
|
|
17
|
+
The solver's own Python is charged separately, one unit per bytecode instruction, so work cannot be hidden in plain-float loops (tflops_runner.py).
|
|
18
|
+
|
|
19
|
+
A solver sees only the functions in EXPORTS and the Tensor objects they return. Tensor keeps its array in a private attribute the restricted subset forbids naming.
|
|
20
|
+
"""
|
|
21
|
+
import re
|
|
22
|
+
|
|
23
|
+
import numpy as np
|
|
24
|
+
|
|
25
|
+
_FLOPS = [0] # total tensor operations charged so far (the runner reads this; a solver cannot name this module, only the EXPORTS)
|
|
26
|
+
_LIMIT = [None] # the runner sets the budget; charging past it raises BudgetExceeded
|
|
27
|
+
MAX_ELEMENTS = 1 << 27 # no tensor may have more elements than this (1 GiB of float64): a bigger one is the same failure as an exhausted budget
|
|
28
|
+
MAX_LIVE = 1 << 28 # nor may the tensors alive at once (2 GiB): views count as copies, which only over-counts. Without it, memory would be bounded only by the op budget
|
|
29
|
+
MAX_TOLIST = 1 << 22 # tolist() makes a Python list (about 32 bytes per float): the one way out of the tensor world, so it is small
|
|
30
|
+
_LIVE = [0]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class BudgetExceeded(BaseException):
|
|
34
|
+
"""A BaseException, so a solver's `except Exception` cannot swallow it."""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _charge(n):
|
|
38
|
+
_FLOPS[0] += int(n)
|
|
39
|
+
if _LIMIT[0] is not None and _FLOPS[0] > _LIMIT[0]:
|
|
40
|
+
raise BudgetExceeded()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _size(n):
|
|
44
|
+
"""The size check, made BEFORE anything is allocated: a request over MAX_ELEMENTS fails without ever existing in memory. (Callers charge separately, also before allocating.)"""
|
|
45
|
+
if n > MAX_ELEMENTS or _LIVE[0] + n > MAX_LIVE:
|
|
46
|
+
raise BudgetExceeded()
|
|
47
|
+
return n
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _dims(s):
|
|
51
|
+
"""A shape or axes argument: a list or tuple of at most 32 entries. Any other iterable (a range, itertools.repeat) could be made to build a huge sequence here."""
|
|
52
|
+
if not isinstance(s, (list, tuple)) or len(s) > 32:
|
|
53
|
+
raise TypeError("a shape or axes argument is a list or tuple of at most 32 ints")
|
|
54
|
+
return tuple(s)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _shape(s):
|
|
58
|
+
s = _dims(s)
|
|
59
|
+
if not all(isinstance(d, int) and not isinstance(d, bool) and d >= 0 for d in s):
|
|
60
|
+
raise ValueError("a shape is a tuple of non-negative ints")
|
|
61
|
+
return s
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _numbers(x, budget):
|
|
65
|
+
"""Count the numbers in a nested list/tuple of ints and floats, refusing anything else (and anything bigger than `budget`) before numpy builds an array from it."""
|
|
66
|
+
if isinstance(x, bool) or not isinstance(x, (int, float, list, tuple)):
|
|
67
|
+
raise TypeError("tensor(): nested lists or tuples of ints and floats only")
|
|
68
|
+
if not isinstance(x, (list, tuple)):
|
|
69
|
+
return 1
|
|
70
|
+
n = 0
|
|
71
|
+
for e in x:
|
|
72
|
+
n += _numbers(e, budget - n)
|
|
73
|
+
if n > budget:
|
|
74
|
+
raise BudgetExceeded()
|
|
75
|
+
return n
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _prod(shape):
|
|
79
|
+
n = 1
|
|
80
|
+
for d in shape:
|
|
81
|
+
n *= int(d)
|
|
82
|
+
return n
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _matmul_shape(sa, sb):
|
|
86
|
+
if not sa or not sb:
|
|
87
|
+
raise ValueError("@ needs tensors of at least one dimension")
|
|
88
|
+
k = sa[-1]
|
|
89
|
+
if k != (sb[0] if len(sb) == 1 else sb[-2]):
|
|
90
|
+
raise ValueError("matmul: the inner dimensions differ")
|
|
91
|
+
batch = np.broadcast_shapes(sa[:-2] if len(sa) >= 2 else (), sb[:-2] if len(sb) >= 2 else ())
|
|
92
|
+
return tuple(batch) + (() if len(sa) == 1 else (sa[-2],)) + (() if len(sb) == 1 else (sb[-1],)), k
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _raw(v):
|
|
96
|
+
if isinstance(v, Tensor):
|
|
97
|
+
return v._a
|
|
98
|
+
if isinstance(v, bool) or not isinstance(v, (int, float)):
|
|
99
|
+
raise TypeError("a tensor operand must be a Tensor or a number")
|
|
100
|
+
return v
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _key(k):
|
|
104
|
+
ks = k if isinstance(k, tuple) else (k,)
|
|
105
|
+
for e in ks:
|
|
106
|
+
if not (e is None or e is Ellipsis or isinstance(e, slice) or (isinstance(e, int) and not isinstance(e, bool))):
|
|
107
|
+
raise TypeError("an index must be an int, a slice, None or ...")
|
|
108
|
+
return k
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class Tensor:
|
|
112
|
+
__slots__ = ("_a",)
|
|
113
|
+
|
|
114
|
+
def __init__(self, a):
|
|
115
|
+
self._a = np.asarray(a, dtype=np.float64)
|
|
116
|
+
_LIVE[0] += self._a.size
|
|
117
|
+
|
|
118
|
+
def __del__(self):
|
|
119
|
+
try:
|
|
120
|
+
_LIVE[0] -= self._a.size
|
|
121
|
+
except (AttributeError, TypeError): # __init__ failed before the array was set, or the interpreter is shutting down and the module globals are gone
|
|
122
|
+
pass
|
|
123
|
+
|
|
124
|
+
@property
|
|
125
|
+
def shape(self):
|
|
126
|
+
return tuple(int(s) for s in self._a.shape)
|
|
127
|
+
|
|
128
|
+
def item(self):
|
|
129
|
+
return float(self._a.item()) # a one-element tensor of any rank; numpy raises ValueError for anything else (float(array) is deprecated for ndim > 0)
|
|
130
|
+
|
|
131
|
+
def tolist(self):
|
|
132
|
+
n = self._a.size
|
|
133
|
+
if n > MAX_TOLIST:
|
|
134
|
+
raise BudgetExceeded()
|
|
135
|
+
_charge(n)
|
|
136
|
+
return self._a.tolist()
|
|
137
|
+
|
|
138
|
+
def __getitem__(self, k):
|
|
139
|
+
return Tensor(self._a[_key(k)])
|
|
140
|
+
|
|
141
|
+
def __setitem__(self, k, v):
|
|
142
|
+
self._a[_key(k)] = _raw(v)
|
|
143
|
+
|
|
144
|
+
def _bin(self, other, fn, flip=False):
|
|
145
|
+
a, b = (_raw(other), self._a) if flip else (self._a, _raw(other))
|
|
146
|
+
n = _size(_prod(np.broadcast_shapes(np.shape(a), np.shape(b))))
|
|
147
|
+
_charge(n)
|
|
148
|
+
return Tensor(fn(a, b))
|
|
149
|
+
|
|
150
|
+
def __add__(self, o): return self._bin(o, np.add)
|
|
151
|
+
def __radd__(self, o): return self._bin(o, np.add, True)
|
|
152
|
+
def __sub__(self, o): return self._bin(o, np.subtract)
|
|
153
|
+
def __rsub__(self, o): return self._bin(o, np.subtract, True)
|
|
154
|
+
def __mul__(self, o): return self._bin(o, np.multiply)
|
|
155
|
+
def __rmul__(self, o): return self._bin(o, np.multiply, True)
|
|
156
|
+
def __truediv__(self, o): return self._bin(o, np.true_divide)
|
|
157
|
+
def __rtruediv__(self, o): return self._bin(o, np.true_divide, True)
|
|
158
|
+
|
|
159
|
+
def __neg__(self):
|
|
160
|
+
_charge(self._a.size)
|
|
161
|
+
return Tensor(-self._a)
|
|
162
|
+
|
|
163
|
+
def __matmul__(self, o):
|
|
164
|
+
if not isinstance(o, Tensor):
|
|
165
|
+
raise TypeError("@ needs two Tensors")
|
|
166
|
+
shape, k = _matmul_shape(self._a.shape, o._a.shape)
|
|
167
|
+
n = _size(_prod(shape))
|
|
168
|
+
_charge(2 * n * k)
|
|
169
|
+
return Tensor(np.asarray(self._a @ o._a))
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def stream(seed, start, n, shape=None):
|
|
173
|
+
"""n floats in [-1, 1) from the pinned SplitMix64 stream (the model-regeneration step), optionally reshaped."""
|
|
174
|
+
if not all(isinstance(v, int) and not isinstance(v, bool) for v in (seed, start, n)) or n < 0 or seed < 0 or start < 0 or seed >= 1 << 64 or start + n >= 1 << 64:
|
|
175
|
+
raise ValueError("stream(seed, start, n): non-negative ints below 2**64")
|
|
176
|
+
shape = None if shape is None else _shape(shape)
|
|
177
|
+
if shape is not None and _prod(shape) != n:
|
|
178
|
+
raise ValueError("stream: the shape does not hold n elements")
|
|
179
|
+
_size(n)
|
|
180
|
+
_charge(10 * n)
|
|
181
|
+
u = np.uint64
|
|
182
|
+
with np.errstate(over="ignore"):
|
|
183
|
+
z = (np.arange(start, start + n, dtype=u) + u(1)) * u(0x9E3779B97F4A7C15) + u(seed)
|
|
184
|
+
z = (z ^ (z >> u(30))) * u(0xBF58476D1CE4E5B9)
|
|
185
|
+
z = (z ^ (z >> u(27))) * u(0x94D049BB133111EB)
|
|
186
|
+
z = z ^ (z >> u(31))
|
|
187
|
+
a = (z >> u(11)).astype(np.float64) * 2.0 ** -52 - 1.0
|
|
188
|
+
return Tensor(a if shape is None else a.reshape(shape))
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def tensor(x):
|
|
192
|
+
_numbers(x, MAX_ELEMENTS)
|
|
193
|
+
return Tensor(np.array(x, dtype=np.float64))
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def zeros(shape):
|
|
197
|
+
shape = _shape(shape)
|
|
198
|
+
_size(_prod(shape))
|
|
199
|
+
return Tensor(np.zeros(shape))
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def take(table, idx):
|
|
203
|
+
"""Rows of `table`: idx is an int or a list of ints. Data movement: free."""
|
|
204
|
+
if isinstance(idx, int) and not isinstance(idx, bool):
|
|
205
|
+
return Tensor(table._a[idx])
|
|
206
|
+
if not isinstance(idx, (list, tuple)):
|
|
207
|
+
raise TypeError("take: an int or a list of ints")
|
|
208
|
+
_size(len(idx) * _prod(table._a.shape[1:]))
|
|
209
|
+
idx = list(idx)
|
|
210
|
+
if not all(isinstance(i, int) and not isinstance(i, bool) for i in idx):
|
|
211
|
+
raise TypeError("take: indices are ints")
|
|
212
|
+
_size(len(idx) * _prod(table._a.shape[1:]))
|
|
213
|
+
return Tensor(table._a[idx])
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def concat(ts, axis=0):
|
|
217
|
+
if not isinstance(ts, (list, tuple)) or not all(isinstance(t, Tensor) for t in ts):
|
|
218
|
+
raise TypeError("concat: a list or tuple of Tensors")
|
|
219
|
+
total = 0 # (no builtin sum: this module defines its own `sum`)
|
|
220
|
+
for t in ts:
|
|
221
|
+
total += t._a.size
|
|
222
|
+
_size(total)
|
|
223
|
+
return Tensor(np.concatenate([t._a for t in ts], axis=axis))
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def reshape(x, shape):
|
|
227
|
+
return Tensor(x._a.reshape(_dims(shape)))
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def transpose(x, axes):
|
|
231
|
+
return Tensor(x._a.transpose(_dims(axes)))
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def causal_mask(n):
|
|
235
|
+
"""(n, n): 0 on and below the diagonal, -1e30 above. A constant of the shape: free."""
|
|
236
|
+
if not isinstance(n, int) or isinstance(n, bool) or n < 0:
|
|
237
|
+
raise ValueError("causal_mask(n): a non-negative int")
|
|
238
|
+
_size(n * n)
|
|
239
|
+
return Tensor(np.triu(np.full((n, n), -1e30), 1))
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
_EINSUM = re.compile(r"^([a-z]+),([a-z]+)->([a-z]*)$")
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def einsum(spec, a, b):
|
|
246
|
+
"""Two-operand einsum with plain lowercase indices (no `...`, no repeated index inside one operand): the cost is read off the index extents, so a spec the parser
|
|
247
|
+
could misread (an ellipsis, a repeated letter) is refused rather than undercharged."""
|
|
248
|
+
m = _EINSUM.match(spec) if isinstance(spec, str) else None
|
|
249
|
+
if not m:
|
|
250
|
+
raise ValueError("einsum: a spec like 'ij,jk->ik' (lowercase letters only)")
|
|
251
|
+
sa, sb, out = m.groups()
|
|
252
|
+
if len(set(sa)) != len(sa) or len(set(sb)) != len(sb) or len(set(out)) != len(out) or not set(out) <= set(sa) | set(sb):
|
|
253
|
+
raise ValueError("einsum: repeated or unknown output index")
|
|
254
|
+
if len(sa) != a._a.ndim or len(sb) != b._a.ndim:
|
|
255
|
+
raise ValueError("einsum: an operand's rank does not match its subscripts")
|
|
256
|
+
ext = {}
|
|
257
|
+
for sub, t in ((sa, a), (sb, b)):
|
|
258
|
+
for ch, d in zip(sub, t._a.shape):
|
|
259
|
+
if ext.setdefault(ch, d) != d:
|
|
260
|
+
raise ValueError("einsum: an index has two different extents")
|
|
261
|
+
_size(_prod([ext[c] for c in out]))
|
|
262
|
+
_charge(2 * _prod(ext.values()))
|
|
263
|
+
return Tensor(np.einsum(spec, a._a, b._a))
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def exp(x):
|
|
267
|
+
_charge(x._a.size)
|
|
268
|
+
return Tensor(np.exp(x._a))
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def sqrt(x):
|
|
272
|
+
_charge(x._a.size)
|
|
273
|
+
return Tensor(np.sqrt(x._a))
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def relu(x):
|
|
277
|
+
_charge(x._a.size)
|
|
278
|
+
return Tensor(np.maximum(x._a, 0.0))
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def sum(x, axis=None, keepdims=False):
|
|
282
|
+
_charge(x._a.size)
|
|
283
|
+
return Tensor(x._a.sum(axis=axis, keepdims=keepdims))
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def max(x, axis=None, keepdims=False):
|
|
287
|
+
_charge(x._a.size)
|
|
288
|
+
return Tensor(x._a.max(axis=axis, keepdims=keepdims))
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def mean(x, axis=None, keepdims=False):
|
|
292
|
+
_charge(x._a.size)
|
|
293
|
+
return Tensor(x._a.mean(axis=axis, keepdims=keepdims))
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def argmax(x):
|
|
297
|
+
"""Index of the largest element of a 1-D tensor, as a Python int (lowest index on ties)."""
|
|
298
|
+
if x._a.ndim != 1:
|
|
299
|
+
raise ValueError("argmax takes a 1-D tensor")
|
|
300
|
+
_charge(x._a.size)
|
|
301
|
+
return int(np.argmax(x._a))
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
EXPORTS = ("stream", "tensor", "zeros", "take", "concat", "reshape", "transpose", "causal_mask", "einsum", "exp", "sqrt", "relu", "sum", "max", "mean", "argmax")
|