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
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""aidecode in the counted-operations API, SPEEDUP: a KV cache. Same tokens as the reference with O(1) projections per step."""
|
|
2
|
+
import math
|
|
3
|
+
|
|
4
|
+
import tensorops as tp
|
|
5
|
+
|
|
6
|
+
V, D, H, DH, FF, L, PROMPT = 64, 128, 4, 32, 512, 2, 8
|
|
7
|
+
W_SCALE, POS_SCALE = 4.0, 3.0
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def take_param(off, seed, shape, scale):
|
|
11
|
+
n = 1
|
|
12
|
+
for s in shape:
|
|
13
|
+
n *= s
|
|
14
|
+
a = tp.stream(seed, off[0], n, shape) * scale
|
|
15
|
+
off[0] += n
|
|
16
|
+
return a
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def build(seed, T):
|
|
20
|
+
off = [0]
|
|
21
|
+
sd = W_SCALE * math.sqrt(3.0 / D)
|
|
22
|
+
sf = W_SCALE * math.sqrt(3.0 / FF)
|
|
23
|
+
P = {"tok": take_param(off, seed, (V, D), math.sqrt(3.0)), "layers": []}
|
|
24
|
+
for i in range(L):
|
|
25
|
+
P["layers"].append({"Wq": take_param(off, seed, (D, D), sd), "Wk": take_param(off, seed, (D, D), sd), "Wv": take_param(off, seed, (D, D), sd),
|
|
26
|
+
"Wo": take_param(off, seed, (D, D), sd), "W1": take_param(off, seed, (D, FF), sd), "W2": take_param(off, seed, (FF, D), sf)})
|
|
27
|
+
P["pos"] = take_param(off, seed, (PROMPT + T, D), POS_SCALE * math.sqrt(3.0))
|
|
28
|
+
u = tp.stream(seed, off[0], PROMPT).tolist()
|
|
29
|
+
P["prompt"] = [min(int((x + 1.0) * 0.5 * V), V - 1) for x in u]
|
|
30
|
+
return P
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def rms(x):
|
|
34
|
+
return x / tp.sqrt(tp.mean(x * x, -1, True) + 1e-6)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def softmax_rows(s):
|
|
38
|
+
s = s - tp.max(s, -1, True)
|
|
39
|
+
a = tp.exp(s)
|
|
40
|
+
return a / tp.sum(a, -1, True)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def decode(P, T, layers_used):
|
|
44
|
+
n = PROMPT + T
|
|
45
|
+
K = [tp.zeros((n, D)) for i in range(L)]
|
|
46
|
+
Vv = [tp.zeros((n, D)) for i in range(L)]
|
|
47
|
+
seq = list(P["prompt"])
|
|
48
|
+
out = []
|
|
49
|
+
for p in range(n - 1):
|
|
50
|
+
x = tp.take(P["tok"], seq[p]) + P["pos"][p]
|
|
51
|
+
li = 0
|
|
52
|
+
for l in P["layers"]:
|
|
53
|
+
if li >= layers_used:
|
|
54
|
+
break
|
|
55
|
+
h = rms(x)
|
|
56
|
+
q = tp.reshape(h @ l["Wq"], (H, DH))
|
|
57
|
+
K[li][p] = h @ l["Wk"]
|
|
58
|
+
Vv[li][p] = h @ l["Wv"]
|
|
59
|
+
kc = tp.reshape(K[li][0:p + 1], (p + 1, H, DH))
|
|
60
|
+
vc = tp.reshape(Vv[li][0:p + 1], (p + 1, H, DH))
|
|
61
|
+
a = softmax_rows(tp.einsum("hd,thd->ht", q, kc) / math.sqrt(DH))
|
|
62
|
+
x = x + tp.reshape(tp.einsum("ht,thd->hd", a, vc), (D,)) @ l["Wo"]
|
|
63
|
+
x = x + tp.relu(rms(x) @ l["W1"]) @ l["W2"]
|
|
64
|
+
li += 1
|
|
65
|
+
if p >= PROMPT - 1:
|
|
66
|
+
t = tp.argmax(rms(x) @ tp.transpose(P["tok"], (1, 0)))
|
|
67
|
+
out.append(t)
|
|
68
|
+
seq.append(t)
|
|
69
|
+
return out
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def solve(seed, prompt_len, T):
|
|
73
|
+
return decode(build(seed, T), T, L)
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""aidecode counted-operations RED TEAM: skips the second layer (fewer operations, wrong tokens). The verifier must reject it."""
|
|
2
|
+
import math
|
|
3
|
+
|
|
4
|
+
import tensorops as tp
|
|
5
|
+
|
|
6
|
+
V, D, H, DH, FF, L, PROMPT = 64, 128, 4, 32, 512, 2, 8
|
|
7
|
+
W_SCALE, POS_SCALE = 4.0, 3.0
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def take_param(off, seed, shape, scale):
|
|
11
|
+
n = 1
|
|
12
|
+
for s in shape:
|
|
13
|
+
n *= s
|
|
14
|
+
a = tp.stream(seed, off[0], n, shape) * scale
|
|
15
|
+
off[0] += n
|
|
16
|
+
return a
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def build(seed, T):
|
|
20
|
+
off = [0]
|
|
21
|
+
sd = W_SCALE * math.sqrt(3.0 / D)
|
|
22
|
+
sf = W_SCALE * math.sqrt(3.0 / FF)
|
|
23
|
+
P = {"tok": take_param(off, seed, (V, D), math.sqrt(3.0)), "layers": []}
|
|
24
|
+
for i in range(L):
|
|
25
|
+
P["layers"].append({"Wq": take_param(off, seed, (D, D), sd), "Wk": take_param(off, seed, (D, D), sd), "Wv": take_param(off, seed, (D, D), sd),
|
|
26
|
+
"Wo": take_param(off, seed, (D, D), sd), "W1": take_param(off, seed, (D, FF), sd), "W2": take_param(off, seed, (FF, D), sf)})
|
|
27
|
+
P["pos"] = take_param(off, seed, (PROMPT + T, D), POS_SCALE * math.sqrt(3.0))
|
|
28
|
+
u = tp.stream(seed, off[0], PROMPT).tolist()
|
|
29
|
+
P["prompt"] = [min(int((x + 1.0) * 0.5 * V), V - 1) for x in u]
|
|
30
|
+
return P
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def rms(x):
|
|
34
|
+
return x / tp.sqrt(tp.mean(x * x, -1, True) + 1e-6)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def softmax_rows(s):
|
|
38
|
+
s = s - tp.max(s, -1, True)
|
|
39
|
+
a = tp.exp(s)
|
|
40
|
+
return a / tp.sum(a, -1, True)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def decode(P, T, layers_used):
|
|
44
|
+
n = PROMPT + T
|
|
45
|
+
K = [tp.zeros((n, D)) for i in range(L)]
|
|
46
|
+
Vv = [tp.zeros((n, D)) for i in range(L)]
|
|
47
|
+
seq = list(P["prompt"])
|
|
48
|
+
out = []
|
|
49
|
+
for p in range(n - 1):
|
|
50
|
+
x = tp.take(P["tok"], seq[p]) + P["pos"][p]
|
|
51
|
+
li = 0
|
|
52
|
+
for l in P["layers"]:
|
|
53
|
+
if li >= layers_used:
|
|
54
|
+
break
|
|
55
|
+
h = rms(x)
|
|
56
|
+
q = tp.reshape(h @ l["Wq"], (H, DH))
|
|
57
|
+
K[li][p] = h @ l["Wk"]
|
|
58
|
+
Vv[li][p] = h @ l["Wv"]
|
|
59
|
+
kc = tp.reshape(K[li][0:p + 1], (p + 1, H, DH))
|
|
60
|
+
vc = tp.reshape(Vv[li][0:p + 1], (p + 1, H, DH))
|
|
61
|
+
a = softmax_rows(tp.einsum("hd,thd->ht", q, kc) / math.sqrt(DH))
|
|
62
|
+
x = x + tp.reshape(tp.einsum("ht,thd->hd", a, vc), (D,)) @ l["Wo"]
|
|
63
|
+
x = x + tp.relu(rms(x) @ l["W1"]) @ l["W2"]
|
|
64
|
+
li += 1
|
|
65
|
+
if p >= PROMPT - 1:
|
|
66
|
+
t = tp.argmax(rms(x) @ tp.transpose(P["tok"], (1, 0)))
|
|
67
|
+
out.append(t)
|
|
68
|
+
seq.append(t)
|
|
69
|
+
return out
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def solve(seed, prompt_len, T):
|
|
73
|
+
return decode(build(seed, T), T, 1)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""aidecode in the counted-operations API, REFERENCE: recompute the whole sequence for every generated token (no KV cache)."""
|
|
2
|
+
import math
|
|
3
|
+
|
|
4
|
+
import tensorops as tp
|
|
5
|
+
|
|
6
|
+
V, D, H, DH, FF, L, PROMPT = 64, 128, 4, 32, 512, 2, 8
|
|
7
|
+
W_SCALE, POS_SCALE = 4.0, 3.0
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def take_param(off, seed, shape, scale):
|
|
11
|
+
n = 1
|
|
12
|
+
for s in shape:
|
|
13
|
+
n *= s
|
|
14
|
+
a = tp.stream(seed, off[0], n, shape) * scale
|
|
15
|
+
off[0] += n
|
|
16
|
+
return a
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def build(seed, T):
|
|
20
|
+
off = [0]
|
|
21
|
+
sd = W_SCALE * math.sqrt(3.0 / D)
|
|
22
|
+
sf = W_SCALE * math.sqrt(3.0 / FF)
|
|
23
|
+
P = {"tok": take_param(off, seed, (V, D), math.sqrt(3.0)), "layers": []}
|
|
24
|
+
for i in range(L):
|
|
25
|
+
P["layers"].append({"Wq": take_param(off, seed, (D, D), sd), "Wk": take_param(off, seed, (D, D), sd), "Wv": take_param(off, seed, (D, D), sd),
|
|
26
|
+
"Wo": take_param(off, seed, (D, D), sd), "W1": take_param(off, seed, (D, FF), sd), "W2": take_param(off, seed, (FF, D), sf)})
|
|
27
|
+
P["pos"] = take_param(off, seed, (PROMPT + T, D), POS_SCALE * math.sqrt(3.0))
|
|
28
|
+
u = tp.stream(seed, off[0], PROMPT).tolist()
|
|
29
|
+
P["prompt"] = [min(int((x + 1.0) * 0.5 * V), V - 1) for x in u]
|
|
30
|
+
return P
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def rms(x):
|
|
34
|
+
return x / tp.sqrt(tp.mean(x * x, -1, True) + 1e-6)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def softmax_rows(s):
|
|
38
|
+
s = s - tp.max(s, -1, True)
|
|
39
|
+
a = tp.exp(s)
|
|
40
|
+
return a / tp.sum(a, -1, True)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def forward_last(P, toks):
|
|
44
|
+
n = len(toks)
|
|
45
|
+
x = tp.take(P["tok"], toks) + P["pos"][0:n]
|
|
46
|
+
mask = tp.causal_mask(n)
|
|
47
|
+
for l in P["layers"]:
|
|
48
|
+
h = rms(x)
|
|
49
|
+
q = tp.transpose(tp.reshape(h @ l["Wq"], (n, H, DH)), (1, 0, 2))
|
|
50
|
+
k = tp.transpose(tp.reshape(h @ l["Wk"], (n, H, DH)), (1, 0, 2))
|
|
51
|
+
v = tp.transpose(tp.reshape(h @ l["Wv"], (n, H, DH)), (1, 0, 2))
|
|
52
|
+
a = softmax_rows((q @ tp.transpose(k, (0, 2, 1))) / math.sqrt(DH) + mask)
|
|
53
|
+
o = tp.reshape(tp.transpose(a @ v, (1, 0, 2)), (n, D))
|
|
54
|
+
x = x + o @ l["Wo"]
|
|
55
|
+
x = x + tp.relu(rms(x) @ l["W1"]) @ l["W2"]
|
|
56
|
+
return rms(x[n - 1]) @ tp.transpose(P["tok"], (1, 0))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def solve(seed, prompt_len, T):
|
|
60
|
+
P = build(seed, T)
|
|
61
|
+
seq = list(P["prompt"])
|
|
62
|
+
for step in range(T):
|
|
63
|
+
seq.append(tp.argmax(forward_last(P, seq)))
|
|
64
|
+
return seq[PROMPT:]
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""aidecode speedup 1: a KV cache in float64 (the textbook inference optimisation). Same tokens as the reference.
|
|
2
|
+
"""
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
V, D, H, DH, FF, L, PROMPT = 64, 128, 4, 32, 512, 2, 8
|
|
8
|
+
W_SCALE, POS_SCALE = 4.0, 3.0
|
|
9
|
+
_GOLD = 0x9E3779B97F4A7C15
|
|
10
|
+
_C1 = 0xBF58476D1CE4E5B9
|
|
11
|
+
_C2 = 0x94D049BB133111EB
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _uniform(seed, start, n):
|
|
15
|
+
u = np.uint64
|
|
16
|
+
with np.errstate(over="ignore"):
|
|
17
|
+
z = (np.arange(start, start + n, dtype=u) + u(1)) * u(_GOLD) + u(seed)
|
|
18
|
+
z = (z ^ (z >> u(30))) * u(_C1)
|
|
19
|
+
z = (z ^ (z >> u(27))) * u(_C2)
|
|
20
|
+
z = z ^ (z >> u(31))
|
|
21
|
+
return (z >> u(11)).astype(np.float64) * 2.0 ** -52 - 1.0
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _params(seed, T):
|
|
25
|
+
o = [0]
|
|
26
|
+
|
|
27
|
+
def take(shape, scale):
|
|
28
|
+
n = int(np.prod(shape))
|
|
29
|
+
a = (_uniform(seed, o[0], n) * scale).reshape(shape)
|
|
30
|
+
o[0] += n
|
|
31
|
+
return a
|
|
32
|
+
sd, sf = W_SCALE * math.sqrt(3.0 / D), W_SCALE * math.sqrt(3.0 / FF)
|
|
33
|
+
P = {"tok": take((V, D), math.sqrt(3.0)), "layers": []}
|
|
34
|
+
for _ in range(L):
|
|
35
|
+
P["layers"].append({"Wq": take((D, D), sd), "Wk": take((D, D), sd), "Wv": take((D, D), sd), "Wo": take((D, D), sd),
|
|
36
|
+
"W1": take((D, FF), sd), "W2": take((FF, D), sf)})
|
|
37
|
+
P["pos"] = take((PROMPT + T, D), POS_SCALE * math.sqrt(3.0))
|
|
38
|
+
P["prompt"] = np.minimum(((_uniform(seed, o[0], PROMPT) + 1.0) * 0.5 * V).astype(np.int64), V - 1)
|
|
39
|
+
return P
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _rms(x):
|
|
43
|
+
return x / np.sqrt((x * x).mean(axis=-1, keepdims=True) + 1e-6)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _decode(P, T, dt):
|
|
47
|
+
"""Greedy decoding with a KV cache in dtype `dt`: one token's projections per step instead of the whole sequence's."""
|
|
48
|
+
n = PROMPT + T
|
|
49
|
+
layers = [{k: w.astype(dt) for k, w in l.items()} for l in P["layers"]]
|
|
50
|
+
tok, pos = P["tok"].astype(dt), P["pos"].astype(dt)
|
|
51
|
+
K = [np.zeros((n, D), dt) for _ in range(L)]
|
|
52
|
+
Vv = [np.zeros((n, D), dt) for _ in range(L)]
|
|
53
|
+
seq = [int(t) for t in P["prompt"]]
|
|
54
|
+
out = []
|
|
55
|
+
for p in range(n - 1):
|
|
56
|
+
x = tok[seq[p]] + pos[p]
|
|
57
|
+
for li, l in enumerate(layers):
|
|
58
|
+
h = _rms(x)
|
|
59
|
+
q = (h @ l["Wq"]).reshape(H, DH)
|
|
60
|
+
K[li][p], Vv[li][p] = h @ l["Wk"], h @ l["Wv"]
|
|
61
|
+
kc = K[li][:p + 1].reshape(p + 1, H, DH)
|
|
62
|
+
vc = Vv[li][:p + 1].reshape(p + 1, H, DH)
|
|
63
|
+
s = np.einsum("hd,thd->ht", q, kc) / dt(math.sqrt(DH))
|
|
64
|
+
s -= s.max(-1, keepdims=True)
|
|
65
|
+
a = np.exp(s)
|
|
66
|
+
a /= a.sum(-1, keepdims=True)
|
|
67
|
+
x = x + np.einsum("ht,thd->hd", a, vc).reshape(D) @ l["Wo"]
|
|
68
|
+
x = x + np.maximum(_rms(x) @ l["W1"], 0) @ l["W2"]
|
|
69
|
+
if p >= PROMPT - 1:
|
|
70
|
+
t = int(np.argmax(_rms(x) @ tok.T))
|
|
71
|
+
out.append(t)
|
|
72
|
+
seq.append(t)
|
|
73
|
+
return out
|
|
74
|
+
|
|
75
|
+
def solve(seed, prompt_len, T):
|
|
76
|
+
return _decode(_params(seed, T), T, np.float64)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""aidecode speedup 2: a KV cache AND float32 (parameters cast after the exact float64 regeneration).
|
|
2
|
+
Passes the verifier's tolerance on tokens whose margin exceeds float32 noise; an inexact path would be rejected.
|
|
3
|
+
"""
|
|
4
|
+
import math
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
V, D, H, DH, FF, L, PROMPT = 64, 128, 4, 32, 512, 2, 8
|
|
9
|
+
W_SCALE, POS_SCALE = 4.0, 3.0
|
|
10
|
+
_GOLD = 0x9E3779B97F4A7C15
|
|
11
|
+
_C1 = 0xBF58476D1CE4E5B9
|
|
12
|
+
_C2 = 0x94D049BB133111EB
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _uniform(seed, start, n):
|
|
16
|
+
u = np.uint64
|
|
17
|
+
with np.errstate(over="ignore"):
|
|
18
|
+
z = (np.arange(start, start + n, dtype=u) + u(1)) * u(_GOLD) + u(seed)
|
|
19
|
+
z = (z ^ (z >> u(30))) * u(_C1)
|
|
20
|
+
z = (z ^ (z >> u(27))) * u(_C2)
|
|
21
|
+
z = z ^ (z >> u(31))
|
|
22
|
+
return (z >> u(11)).astype(np.float64) * 2.0 ** -52 - 1.0
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _params(seed, T):
|
|
26
|
+
o = [0]
|
|
27
|
+
|
|
28
|
+
def take(shape, scale):
|
|
29
|
+
n = int(np.prod(shape))
|
|
30
|
+
a = (_uniform(seed, o[0], n) * scale).reshape(shape)
|
|
31
|
+
o[0] += n
|
|
32
|
+
return a
|
|
33
|
+
sd, sf = W_SCALE * math.sqrt(3.0 / D), W_SCALE * math.sqrt(3.0 / FF)
|
|
34
|
+
P = {"tok": take((V, D), math.sqrt(3.0)), "layers": []}
|
|
35
|
+
for _ in range(L):
|
|
36
|
+
P["layers"].append({"Wq": take((D, D), sd), "Wk": take((D, D), sd), "Wv": take((D, D), sd), "Wo": take((D, D), sd),
|
|
37
|
+
"W1": take((D, FF), sd), "W2": take((FF, D), sf)})
|
|
38
|
+
P["pos"] = take((PROMPT + T, D), POS_SCALE * math.sqrt(3.0))
|
|
39
|
+
P["prompt"] = np.minimum(((_uniform(seed, o[0], PROMPT) + 1.0) * 0.5 * V).astype(np.int64), V - 1)
|
|
40
|
+
return P
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _rms(x):
|
|
44
|
+
return x / np.sqrt((x * x).mean(axis=-1, keepdims=True) + 1e-6)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _decode(P, T, dt):
|
|
48
|
+
"""Greedy decoding with a KV cache in dtype `dt`: one token's projections per step instead of the whole sequence's."""
|
|
49
|
+
n = PROMPT + T
|
|
50
|
+
layers = [{k: w.astype(dt) for k, w in l.items()} for l in P["layers"]]
|
|
51
|
+
tok, pos = P["tok"].astype(dt), P["pos"].astype(dt)
|
|
52
|
+
K = [np.zeros((n, D), dt) for _ in range(L)]
|
|
53
|
+
Vv = [np.zeros((n, D), dt) for _ in range(L)]
|
|
54
|
+
seq = [int(t) for t in P["prompt"]]
|
|
55
|
+
out = []
|
|
56
|
+
for p in range(n - 1):
|
|
57
|
+
x = tok[seq[p]] + pos[p]
|
|
58
|
+
for li, l in enumerate(layers):
|
|
59
|
+
h = _rms(x)
|
|
60
|
+
q = (h @ l["Wq"]).reshape(H, DH)
|
|
61
|
+
K[li][p], Vv[li][p] = h @ l["Wk"], h @ l["Wv"]
|
|
62
|
+
kc = K[li][:p + 1].reshape(p + 1, H, DH)
|
|
63
|
+
vc = Vv[li][:p + 1].reshape(p + 1, H, DH)
|
|
64
|
+
s = np.einsum("hd,thd->ht", q, kc) / dt(math.sqrt(DH))
|
|
65
|
+
s -= s.max(-1, keepdims=True)
|
|
66
|
+
a = np.exp(s)
|
|
67
|
+
a /= a.sum(-1, keepdims=True)
|
|
68
|
+
x = x + np.einsum("ht,thd->hd", a, vc).reshape(D) @ l["Wo"]
|
|
69
|
+
x = x + np.maximum(_rms(x) @ l["W1"], 0) @ l["W2"]
|
|
70
|
+
if p >= PROMPT - 1:
|
|
71
|
+
t = int(np.argmax(_rms(x) @ tok.T))
|
|
72
|
+
out.append(t)
|
|
73
|
+
seq.append(t)
|
|
74
|
+
return out
|
|
75
|
+
|
|
76
|
+
def solve(seed, prompt_len, T):
|
|
77
|
+
return _decode(_params(seed, T), T, np.float32)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""aidecode RED TEAM: fast but wrong, it skips the second transformer layer. The teacher-forced verifier must reject it.
|
|
2
|
+
"""
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
V, D, H, DH, FF, L, PROMPT = 64, 128, 4, 32, 512, 2, 8
|
|
8
|
+
W_SCALE, POS_SCALE = 4.0, 3.0
|
|
9
|
+
_GOLD = 0x9E3779B97F4A7C15
|
|
10
|
+
_C1 = 0xBF58476D1CE4E5B9
|
|
11
|
+
_C2 = 0x94D049BB133111EB
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _uniform(seed, start, n):
|
|
15
|
+
u = np.uint64
|
|
16
|
+
with np.errstate(over="ignore"):
|
|
17
|
+
z = (np.arange(start, start + n, dtype=u) + u(1)) * u(_GOLD) + u(seed)
|
|
18
|
+
z = (z ^ (z >> u(30))) * u(_C1)
|
|
19
|
+
z = (z ^ (z >> u(27))) * u(_C2)
|
|
20
|
+
z = z ^ (z >> u(31))
|
|
21
|
+
return (z >> u(11)).astype(np.float64) * 2.0 ** -52 - 1.0
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _params(seed, T):
|
|
25
|
+
o = [0]
|
|
26
|
+
|
|
27
|
+
def take(shape, scale):
|
|
28
|
+
n = int(np.prod(shape))
|
|
29
|
+
a = (_uniform(seed, o[0], n) * scale).reshape(shape)
|
|
30
|
+
o[0] += n
|
|
31
|
+
return a
|
|
32
|
+
sd, sf = W_SCALE * math.sqrt(3.0 / D), W_SCALE * math.sqrt(3.0 / FF)
|
|
33
|
+
P = {"tok": take((V, D), math.sqrt(3.0)), "layers": []}
|
|
34
|
+
for _ in range(L):
|
|
35
|
+
P["layers"].append({"Wq": take((D, D), sd), "Wk": take((D, D), sd), "Wv": take((D, D), sd), "Wo": take((D, D), sd),
|
|
36
|
+
"W1": take((D, FF), sd), "W2": take((FF, D), sf)})
|
|
37
|
+
P["pos"] = take((PROMPT + T, D), POS_SCALE * math.sqrt(3.0))
|
|
38
|
+
P["prompt"] = np.minimum(((_uniform(seed, o[0], PROMPT) + 1.0) * 0.5 * V).astype(np.int64), V - 1)
|
|
39
|
+
return P
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _rms(x):
|
|
43
|
+
return x / np.sqrt((x * x).mean(axis=-1, keepdims=True) + 1e-6)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _decode(P, T, dt):
|
|
47
|
+
"""Greedy decoding with a KV cache in dtype `dt`: one token's projections per step instead of the whole sequence's."""
|
|
48
|
+
n = PROMPT + T
|
|
49
|
+
layers = [{k: w.astype(dt) for k, w in l.items()} for l in P["layers"]]
|
|
50
|
+
tok, pos = P["tok"].astype(dt), P["pos"].astype(dt)
|
|
51
|
+
K = [np.zeros((n, D), dt) for _ in range(L)]
|
|
52
|
+
Vv = [np.zeros((n, D), dt) for _ in range(L)]
|
|
53
|
+
seq = [int(t) for t in P["prompt"]]
|
|
54
|
+
out = []
|
|
55
|
+
for p in range(n - 1):
|
|
56
|
+
x = tok[seq[p]] + pos[p]
|
|
57
|
+
for li, l in enumerate(layers[:1]):
|
|
58
|
+
h = _rms(x)
|
|
59
|
+
q = (h @ l["Wq"]).reshape(H, DH)
|
|
60
|
+
K[li][p], Vv[li][p] = h @ l["Wk"], h @ l["Wv"]
|
|
61
|
+
kc = K[li][:p + 1].reshape(p + 1, H, DH)
|
|
62
|
+
vc = Vv[li][:p + 1].reshape(p + 1, H, DH)
|
|
63
|
+
s = np.einsum("hd,thd->ht", q, kc) / dt(math.sqrt(DH))
|
|
64
|
+
s -= s.max(-1, keepdims=True)
|
|
65
|
+
a = np.exp(s)
|
|
66
|
+
a /= a.sum(-1, keepdims=True)
|
|
67
|
+
x = x + np.einsum("ht,thd->hd", a, vc).reshape(D) @ l["Wo"]
|
|
68
|
+
x = x + np.maximum(_rms(x) @ l["W1"], 0) @ l["W2"]
|
|
69
|
+
if p >= PROMPT - 1:
|
|
70
|
+
t = int(np.argmax(_rms(x) @ tok.T))
|
|
71
|
+
out.append(t)
|
|
72
|
+
seq.append(t)
|
|
73
|
+
return out
|
|
74
|
+
|
|
75
|
+
def solve(seed, prompt_len, T):
|
|
76
|
+
return _decode(_params(seed, T), T, np.float64)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""aiinfer speedup 1: the same float64 network, the whole batch in one BLAS matmul per layer.
|
|
2
|
+
"""
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
_GOLD = 0x9E3779B97F4A7C15
|
|
8
|
+
_C1 = 0xBF58476D1CE4E5B9
|
|
9
|
+
_C2 = 0x94D049BB133111EB
|
|
10
|
+
_CLASSES = 10
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _uniform(seed, start, n):
|
|
14
|
+
u = np.uint64
|
|
15
|
+
with np.errstate(over="ignore"):
|
|
16
|
+
z = (np.arange(start, start + n, dtype=u) + u(1)) * u(_GOLD) + u(seed)
|
|
17
|
+
z = (z ^ (z >> u(30))) * u(_C1)
|
|
18
|
+
z = (z ^ (z >> u(27))) * u(_C2)
|
|
19
|
+
z = z ^ (z >> u(31))
|
|
20
|
+
return (z >> u(11)).astype(np.float64) * 2.0 ** -52 - 1.0
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _model(seed, d, batch):
|
|
25
|
+
s = math.sqrt(3.0 / d)
|
|
26
|
+
o = 0
|
|
27
|
+
W1 = (_uniform(seed, o, d * d) * s).reshape(d, d)
|
|
28
|
+
o += d * d
|
|
29
|
+
W2 = (_uniform(seed, o, d * d) * s).reshape(d, d)
|
|
30
|
+
o += d * d
|
|
31
|
+
W3 = (_uniform(seed, o, _CLASSES * d) * s).reshape(_CLASSES, d)
|
|
32
|
+
o += _CLASSES * d
|
|
33
|
+
X = _uniform(seed, o, batch * d).reshape(batch, d)
|
|
34
|
+
return W1, W2, W3, X
|
|
35
|
+
|
|
36
|
+
def solve(seed, d, batch):
|
|
37
|
+
W1, W2, W3, X = _model(seed, d, batch)
|
|
38
|
+
h = np.maximum(X @ W1.T, 0.0)
|
|
39
|
+
h = np.maximum(h @ W2.T, 0.0)
|
|
40
|
+
return [int(c) for c in (h @ W3.T).argmax(axis=1)]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""aiinfer speedup 2: batched AND float32 (weights and inputs cast after the exact float64 regeneration). Passes the verifier's tolerance.
|
|
2
|
+
"""
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
_GOLD = 0x9E3779B97F4A7C15
|
|
8
|
+
_C1 = 0xBF58476D1CE4E5B9
|
|
9
|
+
_C2 = 0x94D049BB133111EB
|
|
10
|
+
_CLASSES = 10
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _uniform(seed, start, n):
|
|
14
|
+
u = np.uint64
|
|
15
|
+
with np.errstate(over="ignore"):
|
|
16
|
+
z = (np.arange(start, start + n, dtype=u) + u(1)) * u(_GOLD) + u(seed)
|
|
17
|
+
z = (z ^ (z >> u(30))) * u(_C1)
|
|
18
|
+
z = (z ^ (z >> u(27))) * u(_C2)
|
|
19
|
+
z = z ^ (z >> u(31))
|
|
20
|
+
return (z >> u(11)).astype(np.float64) * 2.0 ** -52 - 1.0
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _model(seed, d, batch):
|
|
25
|
+
s = math.sqrt(3.0 / d)
|
|
26
|
+
o = 0
|
|
27
|
+
W1 = (_uniform(seed, o, d * d) * s).reshape(d, d)
|
|
28
|
+
o += d * d
|
|
29
|
+
W2 = (_uniform(seed, o, d * d) * s).reshape(d, d)
|
|
30
|
+
o += d * d
|
|
31
|
+
W3 = (_uniform(seed, o, _CLASSES * d) * s).reshape(_CLASSES, d)
|
|
32
|
+
o += _CLASSES * d
|
|
33
|
+
X = _uniform(seed, o, batch * d).reshape(batch, d)
|
|
34
|
+
return W1, W2, W3, X
|
|
35
|
+
|
|
36
|
+
def solve(seed, d, batch):
|
|
37
|
+
W1, W2, W3, X = _model(seed, d, batch)
|
|
38
|
+
W1, W2, W3, X = (a.astype(np.float32) for a in (W1, W2, W3, X))
|
|
39
|
+
h = np.maximum(X @ W1.T, 0.0)
|
|
40
|
+
h = np.maximum(h @ W2.T, 0.0)
|
|
41
|
+
return [int(c) for c in (h @ W3.T).argmax(axis=1)]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""aiinfer on a GPU, SPEEDUP: the whole batch in one matmul per layer on the device, one read-back at the end. Same float32 numbers as aiinfer_gpu_ref.
|
|
2
|
+
UNRUN: written without a GPU or torch."""
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import torch
|
|
7
|
+
|
|
8
|
+
_GOLD = 0x9E3779B97F4A7C15
|
|
9
|
+
_C1 = 0xBF58476D1CE4E5B9
|
|
10
|
+
_C2 = 0x94D049BB133111EB
|
|
11
|
+
_CLASSES = 10
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _uniform(seed, start, n):
|
|
15
|
+
u = np.uint64
|
|
16
|
+
with np.errstate(over="ignore"):
|
|
17
|
+
z = (np.arange(start, start + n, dtype=u) + u(1)) * u(_GOLD) + u(seed)
|
|
18
|
+
z = (z ^ (z >> u(30))) * u(_C1)
|
|
19
|
+
z = (z ^ (z >> u(27))) * u(_C2)
|
|
20
|
+
z = z ^ (z >> u(31))
|
|
21
|
+
return (z >> u(11)).astype(np.float64) * 2.0 ** -52 - 1.0
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _model(seed, d, batch):
|
|
25
|
+
s = math.sqrt(3.0 / d)
|
|
26
|
+
o = 0
|
|
27
|
+
W1 = (_uniform(seed, o, d * d) * s).reshape(d, d)
|
|
28
|
+
o += d * d
|
|
29
|
+
W2 = (_uniform(seed, o, d * d) * s).reshape(d, d)
|
|
30
|
+
o += d * d
|
|
31
|
+
W3 = (_uniform(seed, o, _CLASSES * d) * s).reshape(_CLASSES, d)
|
|
32
|
+
o += _CLASSES * d
|
|
33
|
+
X = _uniform(seed, o, batch * d).reshape(batch, d)
|
|
34
|
+
return W1, W2, W3, X
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def solve(seed, d, batch):
|
|
38
|
+
dev = torch.device("cuda")
|
|
39
|
+
W1, W2, W3, X = (torch.from_numpy(a).to(dev, dtype=torch.float32) for a in _model(seed, d, batch))
|
|
40
|
+
h = torch.relu(X @ W1.T)
|
|
41
|
+
h = torch.relu(h @ W2.T)
|
|
42
|
+
return (h @ W3.T).argmax(dim=1).tolist()
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""aiinfer on a GPU, REFERENCE: float32 on the device, one sample at a time (a matrix-vector product per input, one synchronising read-back per sample).
|
|
2
|
+
UNRUN: written without a GPU or torch. The model definition is exactly domains/aiinfer/reference.py's (regenerated on the host, then moved to the device)."""
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import torch
|
|
7
|
+
|
|
8
|
+
_GOLD = 0x9E3779B97F4A7C15
|
|
9
|
+
_C1 = 0xBF58476D1CE4E5B9
|
|
10
|
+
_C2 = 0x94D049BB133111EB
|
|
11
|
+
_CLASSES = 10
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _uniform(seed, start, n):
|
|
15
|
+
u = np.uint64
|
|
16
|
+
with np.errstate(over="ignore"):
|
|
17
|
+
z = (np.arange(start, start + n, dtype=u) + u(1)) * u(_GOLD) + u(seed)
|
|
18
|
+
z = (z ^ (z >> u(30))) * u(_C1)
|
|
19
|
+
z = (z ^ (z >> u(27))) * u(_C2)
|
|
20
|
+
z = z ^ (z >> u(31))
|
|
21
|
+
return (z >> u(11)).astype(np.float64) * 2.0 ** -52 - 1.0
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _model(seed, d, batch):
|
|
25
|
+
s = math.sqrt(3.0 / d)
|
|
26
|
+
o = 0
|
|
27
|
+
W1 = (_uniform(seed, o, d * d) * s).reshape(d, d)
|
|
28
|
+
o += d * d
|
|
29
|
+
W2 = (_uniform(seed, o, d * d) * s).reshape(d, d)
|
|
30
|
+
o += d * d
|
|
31
|
+
W3 = (_uniform(seed, o, _CLASSES * d) * s).reshape(_CLASSES, d)
|
|
32
|
+
o += _CLASSES * d
|
|
33
|
+
X = _uniform(seed, o, batch * d).reshape(batch, d)
|
|
34
|
+
return W1, W2, W3, X
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def solve(seed, d, batch):
|
|
38
|
+
dev = torch.device("cuda")
|
|
39
|
+
W1, W2, W3, X = (torch.from_numpy(a).to(dev, dtype=torch.float32) for a in _model(seed, d, batch))
|
|
40
|
+
out = []
|
|
41
|
+
for i in range(batch):
|
|
42
|
+
h = torch.relu(W1 @ X[i])
|
|
43
|
+
h = torch.relu(W2 @ h)
|
|
44
|
+
out.append(int(torch.argmax(W3 @ h)))
|
|
45
|
+
return out
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""aiinfer RED TEAM: a fast but inaccurate model (only the first d/8 hidden units are kept). The verifier must reject it: a speedup that
|
|
2
|
+
breaks the accuracy condition is worth nothing.
|
|
3
|
+
"""
|
|
4
|
+
import math
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
_GOLD = 0x9E3779B97F4A7C15
|
|
9
|
+
_C1 = 0xBF58476D1CE4E5B9
|
|
10
|
+
_C2 = 0x94D049BB133111EB
|
|
11
|
+
_CLASSES = 10
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _uniform(seed, start, n):
|
|
15
|
+
u = np.uint64
|
|
16
|
+
with np.errstate(over="ignore"):
|
|
17
|
+
z = (np.arange(start, start + n, dtype=u) + u(1)) * u(_GOLD) + u(seed)
|
|
18
|
+
z = (z ^ (z >> u(30))) * u(_C1)
|
|
19
|
+
z = (z ^ (z >> u(27))) * u(_C2)
|
|
20
|
+
z = z ^ (z >> u(31))
|
|
21
|
+
return (z >> u(11)).astype(np.float64) * 2.0 ** -52 - 1.0
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _model(seed, d, batch):
|
|
26
|
+
s = math.sqrt(3.0 / d)
|
|
27
|
+
o = 0
|
|
28
|
+
W1 = (_uniform(seed, o, d * d) * s).reshape(d, d)
|
|
29
|
+
o += d * d
|
|
30
|
+
W2 = (_uniform(seed, o, d * d) * s).reshape(d, d)
|
|
31
|
+
o += d * d
|
|
32
|
+
W3 = (_uniform(seed, o, _CLASSES * d) * s).reshape(_CLASSES, d)
|
|
33
|
+
o += _CLASSES * d
|
|
34
|
+
X = _uniform(seed, o, batch * d).reshape(batch, d)
|
|
35
|
+
return W1, W2, W3, X
|
|
36
|
+
|
|
37
|
+
def solve(seed, d, batch):
|
|
38
|
+
W1, W2, W3, X = _model(seed, d, batch)
|
|
39
|
+
k = max(1, d // 8)
|
|
40
|
+
h = np.maximum(X @ W1[:k].T, 0.0)
|
|
41
|
+
h = np.maximum(h @ W2[:k, :k].T, 0.0)
|
|
42
|
+
return [int(c) for c in (h @ W3[:, :k].T).argmax(axis=1)]
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""Reference baseline: progressive BKZ with exact (unpruned) enumeration."""
|
|
2
|
+
from fpylll import BKZ, LLL, IntegerMatrix
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def solve(basis, bound2):
|
|
6
|
+
n = len(basis)
|
|
7
|
+
A = IntegerMatrix.from_matrix(basis)
|
|
8
|
+
LLL.reduction(A)
|
|
9
|
+
for beta in list(range(10, n + 1, 4)):
|
|
10
|
+
if sum(x * x for x in A[0]) <= bound2:
|
|
11
|
+
break
|
|
12
|
+
BKZ.reduction(A, BKZ.Param(block_size=beta, flags=BKZ.AUTO_ABORT | BKZ.MAX_LOOPS, max_loops=8))
|
|
13
|
+
v = [int(x) for x in A[0]]
|
|
14
|
+
return v if sum(x * x for x in v) <= bound2 else None
|