yamahei21 commited on
Commit
2288fa5
·
verified ·
1 Parent(s): 5a04745

Add external eval + speculator quantizer

Browse files
kit_manifest.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "built_from_commit": "785e81b0367f96f2bd846e5887d7fa21640ad5f6",
3
  "capabilities": [
4
  "rht_int4_convert.layer_filter",
5
  "eval_quantized.dtype",
@@ -25,9 +25,11 @@
25
  "scripts/merge_language_hessians.py": "a6385caaa0d7190024b4e47ded59d1ab662e771ce81e18de94b30c8846829b4e",
26
  "scripts/merge_calibration_groups.py": "58a347ff40c47f629c073c23aea54e215c1e75c052737e5b4a48ff81946d8060",
27
  "scripts/eval_quantized.py": "5b4e5454630724c116d14fbc64061ffdd6066f4a458e228d372354d3eb32c5b0",
 
 
28
  "scripts/qad_finetune.py": "d20b15123731c8b5939ddfc00b3cb5c456a783e569c4babeef3a677a4b537244",
29
  "scripts/compute_bpc.py": "2b55fd1df406d54b40daeeca2ecaf2efb35981655f5bf5e0cc10c057ce796256",
30
- "scripts/run_lane.py": "dc437d0bc41e2eeca3fc51218551b2e7175c6cc8bfc6cb43a61edaa55fbf72cc",
31
  "scripts/make_sweep_spec.py": "dc31ec026c89e7db316a185575a4423bca988c2ac6cf08ef6df8f955328cda69",
32
  "scripts/make_smoke_specs.py": "a76091d7a6a38427cdde5264a49d7bb4d8bfcc75284705817dd4c83b012bba2e",
33
  "scripts/combine_lane_manifests.py": "164ba691d60b3e2987b805426c606d135dcf3cf0c855b38cf7f5d6d57628a481",
@@ -48,7 +50,7 @@
48
  "scripts/publish_results.py": "bc371280cc9f2fc45b2fee7d63422fb7e51b1f5bb0e14015f58de0f3ecec48ee",
49
  "scripts/tiny_sharding_probe.py": "29eb58f2689242b919616f6aab6ff753e0de031b16371137975b4d645e836fae",
50
  "scripts/audit_public_corpus.py": "84f1ec805a74004e71f63a70a8ed19967cb3fad9966f254798e655ab95e53ab0",
51
- "scripts/build_kit.py": "1cd317b281a5279980d3db3ef7d786007e103328f5e3128a632efebda940f4ce",
52
  "scripts/bootstrap_kaggle.py": "1633e3edbb80976c337444e3e4544e2b5210b2d8d1821e3a2717c1099c00843e",
53
  "data/calibration_en_wikitext2_dedup.txt": "18a79f4f938cc25747a90a12b120c7131e7f4ac55b58dfc691795ea61d4b5e9a",
54
  "data/calibration_ja_wikipedia_dedup.txt": "b45e3f3f00f2137be280a75c1fa9c9883a7e69d289257c4388fc701f33fee0d1",
@@ -93,6 +95,9 @@
93
  "lanes/smoke-group-size-ablation-08b.json": "60a2f363b94cff31abddf80738420bd02988ac2d8de171e84768221ab0982b00",
94
  "lanes/smoke-robustness-08b-shard1of5.json": "2afb4543eae46384e43f439998d295fe8550b2ec6f69c07ab5d8c9fb1dd46833",
95
  "lanes/smoke-seed-variance-08b.json": "f6acb2f2d3d3e0213a44013fecab6fac2050a477d28a563a3d307defe49f0fa2",
 
 
 
96
  "tests/test_experiment_guards.py": "d71ddf9586c1d7ece52e217288b9d4b09079be54d32ceaf391be113c1832b6ef",
97
  "tests/test_compute_bpc.py": "bba5b6d0afc9b0699ebb6f7f0ed9fc5ebfcb98472867e9ccff4262fe9999949f",
98
  "tests/test_run_lane.py": "0043b2fff1c972612055f89990d15bec10cdf947d74652034d65c267ebaabda8",
 
1
  {
2
+ "built_from_commit": "59783ecd4ae674d2124d85b224dc33854c57fb3d",
3
  "capabilities": [
4
  "rht_int4_convert.layer_filter",
5
  "eval_quantized.dtype",
 
25
  "scripts/merge_language_hessians.py": "a6385caaa0d7190024b4e47ded59d1ab662e771ce81e18de94b30c8846829b4e",
26
  "scripts/merge_calibration_groups.py": "58a347ff40c47f629c073c23aea54e215c1e75c052737e5b4a48ff81946d8060",
27
  "scripts/eval_quantized.py": "5b4e5454630724c116d14fbc64061ffdd6066f4a458e228d372354d3eb32c5b0",
28
+ "scripts/eval_external_model.py": "55e432b6e581ad78e7ea6e292eff25fbaf7a17706f8eebc492a58fc03a51f948",
29
+ "scripts/quantize_speculator.py": "6d9299c63774476d93463ea7b5714f090c31d4cba3fdfc07b561982c78d47afa",
30
  "scripts/qad_finetune.py": "d20b15123731c8b5939ddfc00b3cb5c456a783e569c4babeef3a677a4b537244",
31
  "scripts/compute_bpc.py": "2b55fd1df406d54b40daeeca2ecaf2efb35981655f5bf5e0cc10c057ce796256",
32
+ "scripts/run_lane.py": "f60869399ab84ae84f654e9773794b813ae1ab52bd031927e46c0ba0650d9b04",
33
  "scripts/make_sweep_spec.py": "dc31ec026c89e7db316a185575a4423bca988c2ac6cf08ef6df8f955328cda69",
34
  "scripts/make_smoke_specs.py": "a76091d7a6a38427cdde5264a49d7bb4d8bfcc75284705817dd4c83b012bba2e",
35
  "scripts/combine_lane_manifests.py": "164ba691d60b3e2987b805426c606d135dcf3cf0c855b38cf7f5d6d57628a481",
 
50
  "scripts/publish_results.py": "bc371280cc9f2fc45b2fee7d63422fb7e51b1f5bb0e14015f58de0f3ecec48ee",
51
  "scripts/tiny_sharding_probe.py": "29eb58f2689242b919616f6aab6ff753e0de031b16371137975b4d645e836fae",
52
  "scripts/audit_public_corpus.py": "84f1ec805a74004e71f63a70a8ed19967cb3fad9966f254798e655ab95e53ab0",
53
+ "scripts/build_kit.py": "9a61a9d5aff46d62f9bb3fc491ce37607ba589012f12cf9244ed7f2fc2927255",
54
  "scripts/bootstrap_kaggle.py": "1633e3edbb80976c337444e3e4544e2b5210b2d8d1821e3a2717c1099c00843e",
55
  "data/calibration_en_wikitext2_dedup.txt": "18a79f4f938cc25747a90a12b120c7131e7f4ac55b58dfc691795ea61d4b5e9a",
56
  "data/calibration_ja_wikipedia_dedup.txt": "b45e3f3f00f2137be280a75c1fa9c9883a7e69d289257c4388fc701f33fee0d1",
 
95
  "lanes/smoke-group-size-ablation-08b.json": "60a2f363b94cff31abddf80738420bd02988ac2d8de171e84768221ab0982b00",
96
  "lanes/smoke-robustness-08b-shard1of5.json": "2afb4543eae46384e43f439998d295fe8550b2ec6f69c07ab5d8c9fb1dd46833",
97
  "lanes/smoke-seed-variance-08b.json": "f6acb2f2d3d3e0213a44013fecab6fac2050a477d28a563a3d307defe49f0fa2",
98
+ "lanes/sym8-emb-bits-08b.json": "165d30284ffaa4209cc26c2be88222f9053c5a74c892a16ad7fa84a5ca5cc75e",
99
+ "lanes/sym8-wide8-d1e-1-08b.json": "24d273ce3cd1a7e0ed1a12c91aaf4bb3d9209056dd4ff5db3c9a6bbb580e6e9d",
100
+ "lanes/sym8-wide8-d1e0-08b.json": "f9c8785bb47427c2be23a63568d81c8c00a5d5b882cf05cff3bc9124052e7abf",
101
  "tests/test_experiment_guards.py": "d71ddf9586c1d7ece52e217288b9d4b09079be54d32ceaf391be113c1832b6ef",
102
  "tests/test_compute_bpc.py": "bba5b6d0afc9b0699ebb6f7f0ed9fc5ebfcb98472867e9ccff4262fe9999949f",
103
  "tests/test_run_lane.py": "0043b2fff1c972612055f89990d15bec10cdf947d74652034d65c267ebaabda8",
lanes/sym8-emb-bits-08b.json ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "lane": "sym8-emb-bits-08b",
3
+ "model": {
4
+ "hf_repo": "Qwen/Qwen3.5-0.8B",
5
+ "spec_name": "Qwen3.5-0.8B",
6
+ "bf16_dir": "bf16-lalamo",
7
+ "bf16_source": "/kaggle/input/uzu-bf16-08b-lalamo",
8
+ "bf16_source_auto": true
9
+ },
10
+ "paths": {
11
+ "kit": "kit",
12
+ "work": "/kaggle/working",
13
+ "jax_platform": "gpu"
14
+ },
15
+ "eval": {
16
+ "max_sequences": 100,
17
+ "max_tokens": 128,
18
+ "min_tokens": 16,
19
+ "corpora": {
20
+ "en": "data/eval_en_wikitext2_dedup.txt",
21
+ "ja": "data/eval_ja_wikipedia_dedup.txt"
22
+ }
23
+ },
24
+ "condition_defaults": {
25
+ "samples": 32,
26
+ "max_tokens": 128,
27
+ "output_block": "small",
28
+ "output_block_limit": 2048,
29
+ "damping": 0.1,
30
+ "raw_output": false
31
+ },
32
+ "convert": {
33
+ "bits": 4,
34
+ "group_size": 32,
35
+ "block_size": 32,
36
+ "parallel": "tensor",
37
+ "symmetric_bits": "8",
38
+ "quantize_embedding": true,
39
+ "layer_bits_json": "lanes/bits-down-out-8.json"
40
+ },
41
+ "conditions": [
42
+ {
43
+ "name": "emb8",
44
+ "convert": {
45
+ "embedding_bits": 8
46
+ },
47
+ "calibration_corpus": "data/calibration_mixed_en_ja_dedup.txt"
48
+ },
49
+ {
50
+ "name": "emb4",
51
+ "convert": {
52
+ "embedding_bits": 4
53
+ },
54
+ "calibration_corpus": "data/calibration_mixed_en_ja_dedup.txt"
55
+ }
56
+ ]
57
+ }
lanes/sym8-wide8-d1e-1-08b.json ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "lane": "sym8-wide8-d1e-1-08b",
3
+ "model": {
4
+ "hf_repo": "Qwen/Qwen3.5-0.8B",
5
+ "spec_name": "Qwen3.5-0.8B",
6
+ "bf16_dir": "bf16-lalamo",
7
+ "bf16_source": "/kaggle/input/uzu-bf16-08b-lalamo",
8
+ "bf16_source_auto": true
9
+ },
10
+ "paths": {
11
+ "kit": "kit",
12
+ "work": "/kaggle/working",
13
+ "jax_platform": "gpu"
14
+ },
15
+ "eval": {
16
+ "max_sequences": 100,
17
+ "max_tokens": 128,
18
+ "min_tokens": 16,
19
+ "corpora": {
20
+ "en": "data/eval_en_wikitext2_dedup.txt",
21
+ "ja": "data/eval_ja_wikipedia_dedup.txt"
22
+ }
23
+ },
24
+ "condition_defaults": {
25
+ "samples": 32,
26
+ "max_tokens": 128,
27
+ "output_block": "small",
28
+ "output_block_limit": 2048,
29
+ "damping": 0.1,
30
+ "raw_output": false
31
+ },
32
+ "convert": {
33
+ "bits": 4,
34
+ "group_size": 32,
35
+ "block_size": 32,
36
+ "parallel": "tensor",
37
+ "symmetric_bits": "8",
38
+ "quantize_embedding": true,
39
+ "embedding_bits": 4,
40
+ "layer_bits_json": "lanes/bits-wide-8.json"
41
+ },
42
+ "conditions": [
43
+ {
44
+ "name": "wide8-d1e-1",
45
+ "calibration_corpus": "data/calibration_mixed_en_ja_dedup.txt"
46
+ }
47
+ ]
48
+ }
lanes/sym8-wide8-d1e0-08b.json ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "lane": "sym8-wide8-d1e0-08b",
3
+ "model": {
4
+ "hf_repo": "Qwen/Qwen3.5-0.8B",
5
+ "spec_name": "Qwen3.5-0.8B",
6
+ "bf16_dir": "bf16-lalamo",
7
+ "bf16_source": "/kaggle/input/uzu-bf16-08b-lalamo",
8
+ "bf16_source_auto": true
9
+ },
10
+ "paths": {
11
+ "kit": "kit",
12
+ "work": "/kaggle/working",
13
+ "jax_platform": "gpu"
14
+ },
15
+ "eval": {
16
+ "max_sequences": 100,
17
+ "max_tokens": 128,
18
+ "min_tokens": 16,
19
+ "corpora": {
20
+ "en": "data/eval_en_wikitext2_dedup.txt",
21
+ "ja": "data/eval_ja_wikipedia_dedup.txt"
22
+ }
23
+ },
24
+ "condition_defaults": {
25
+ "samples": 32,
26
+ "max_tokens": 128,
27
+ "output_block": "small",
28
+ "output_block_limit": 2048,
29
+ "damping": 1.0,
30
+ "raw_output": false
31
+ },
32
+ "convert": {
33
+ "bits": 4,
34
+ "group_size": 32,
35
+ "block_size": 32,
36
+ "parallel": "tensor",
37
+ "symmetric_bits": "8",
38
+ "quantize_embedding": true,
39
+ "embedding_bits": 4,
40
+ "layer_bits_json": "lanes/bits-wide-8.json"
41
+ },
42
+ "conditions": [
43
+ {
44
+ "name": "wide8-d1e0",
45
+ "calibration_corpus": "data/calibration_mixed_en_ja_dedup.txt"
46
+ }
47
+ ]
48
+ }
scripts/build_kit.py CHANGED
@@ -40,6 +40,8 @@ KIT_SCRIPTS = [
40
  "merge_language_hessians.py",
41
  "merge_calibration_groups.py",
42
  "eval_quantized.py",
 
 
43
  "qad_finetune.py",
44
  "compute_bpc.py",
45
  "run_lane.py",
 
40
  "merge_language_hessians.py",
41
  "merge_calibration_groups.py",
42
  "eval_quantized.py",
43
+ "eval_external_model.py",
44
+ "quantize_speculator.py",
45
  "qad_finetune.py",
46
  "compute_bpc.py",
47
  "run_lane.py",
scripts/eval_external_model.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Evaluate an already-quantized model directory against a BF16 reference.
3
+
4
+ `run_lane.py` always converts from BF16 first, so it cannot measure a model
5
+ that arrives ready-made (an official release, a QAD output, a downloaded
6
+ checkpoint). This is the missing half: point it at two directories and it
7
+ produces the same ΔBPC JSON that the lanes report.
8
+
9
+ Both directories must be lalamo-loadable after `make_lalamo_eval_copy.py`; the
10
+ script builds the eval copies itself.
11
+
12
+ Usage:
13
+ python scripts/eval_external_model.py \
14
+ --bf16-dir /kaggle/working/bf16-lalamo \
15
+ --candidate-dir /kaggle/working/official \
16
+ --corpus data/eval_en_wikitext2_dedup.txt --corpus data/eval_ja_wikipedia_dedup.txt \
17
+ --out /kaggle/working/official-eval
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import argparse
23
+ import json
24
+ import subprocess
25
+ import sys
26
+ from pathlib import Path
27
+
28
+ HERE = Path(__file__).resolve().parent
29
+ LANGS = {"wikitext2": "en", "wikipedia": "ja"}
30
+
31
+
32
+ def language_of(corpus: Path) -> str:
33
+ name = corpus.name.lower()
34
+ for marker, lang in LANGS.items():
35
+ if marker in name:
36
+ return lang
37
+ return name.split("_")[1] if name.startswith("eval_") and "_" in name else "unknown"
38
+
39
+
40
+ def main() -> None:
41
+ parser = argparse.ArgumentParser()
42
+ parser.add_argument("--bf16-dir", type=Path, required=True, help="reference BF16 model directory")
43
+ parser.add_argument("--candidate-dir", type=Path, required=True, help="model to measure")
44
+ parser.add_argument("--corpus", type=Path, action="append", required=True)
45
+ parser.add_argument("--out", type=Path, required=True)
46
+ parser.add_argument("--max-sequences", type=int, default=100)
47
+ parser.add_argument("--max-tokens", type=int, default=128)
48
+ parser.add_argument("--min-tokens", type=int, default=16)
49
+ parser.add_argument("--dtype", default="float32")
50
+ parser.add_argument("--dry-run", action="store_true")
51
+ args = parser.parse_args()
52
+
53
+ args.out.mkdir(parents=True, exist_ok=True)
54
+ copies = {}
55
+ for label, source in (("bf16", args.bf16_dir), ("candidate", args.candidate_dir)):
56
+ destination = args.out / f"{label}-eval"
57
+ copies[label] = destination
58
+ command = [
59
+ sys.executable,
60
+ str(HERE / "make_lalamo_eval_copy.py"),
61
+ "--source",
62
+ str(source),
63
+ "--dest",
64
+ str(destination),
65
+ ]
66
+ if args.dry_run:
67
+ print("dry-run:", " ".join(command))
68
+ else:
69
+ subprocess.run(command, check=True)
70
+
71
+ results = {}
72
+ for corpus in args.corpus:
73
+ lang = language_of(corpus)
74
+ payload_path = args.out / f"{lang}.json"
75
+ command = [
76
+ sys.executable,
77
+ str(HERE / "eval_quantized.py"),
78
+ "--bf16-dir",
79
+ str(copies["bf16"]),
80
+ "--candidate-dir",
81
+ str(copies["candidate"]),
82
+ "--corpus",
83
+ str(corpus),
84
+ "--max-sequences",
85
+ str(args.max_sequences),
86
+ "--max-tokens",
87
+ str(args.max_tokens),
88
+ "--min-tokens",
89
+ str(args.min_tokens),
90
+ "--dtype",
91
+ args.dtype,
92
+ "--json-out",
93
+ str(payload_path),
94
+ ]
95
+ if args.dry_run:
96
+ print("dry-run:", " ".join(command))
97
+ continue
98
+ subprocess.run(command, check=True)
99
+ payload = json.loads(payload_path.read_text())
100
+ delta = (payload["bpc_candidate"] / payload["bpc_bf16"] - 1) * 100
101
+ results[lang] = {
102
+ "ppl": payload["candidate_ppl"],
103
+ "bf16_ppl": payload["bf16_ppl"],
104
+ "bpc_delta_pct": round(delta, 2),
105
+ }
106
+ print(f"{lang}: {payload['candidate_ppl']:.3f} bpc_delta={delta:+.2f}%", flush=True)
107
+
108
+ if args.dry_run:
109
+ return
110
+ (args.out / "summary.json").write_text(
111
+ json.dumps(results, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
112
+ )
113
+ print("=== FINAL ===", flush=True)
114
+ print(json.dumps(results, indent=1), flush=True)
115
+
116
+
117
+ if __name__ == "__main__":
118
+ main()
scripts/quantize_speculator.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Quantize a converted DFlash speculator to int4 like the official Mirai draft.
3
+
4
+ `lalamo speculator convert` only writes float dtypes, so a converted draft stays
5
+ bf16 (about 2.2GB for the 9B draft) while the official one is 755MB. This
6
+ script reloads the draft as a lalamo module, compresses every Linear with a
7
+ 4-bit group-32 hybrid spec (the same scheme as the target model) and writes the
8
+ result back in the uzu-loadable layout.
9
+
10
+ The speculator directory also needs `shapes.json`, which the converter does not
11
+ write; pass --shapes to copy one (or create an Argmax chain by default).
12
+
13
+ Usage:
14
+ python scripts/quantize_speculator.py \
15
+ --source /tmp/spec_guglxni --output /tmp/spec_guglxni_int4 \
16
+ --bits 4 --group-size 32
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import argparse
22
+ import json
23
+ import shutil
24
+ import time
25
+ from pathlib import Path
26
+
27
+
28
+ def main() -> None:
29
+ parser = argparse.ArgumentParser()
30
+ parser.add_argument("--source", type=Path, required=True, help="converted speculator directory")
31
+ parser.add_argument("--output", type=Path, required=True)
32
+ parser.add_argument("--bits", type=int, default=4, choices=(4, 8))
33
+ parser.add_argument("--group-size", type=int, default=32)
34
+ parser.add_argument("--block-size", type=int, default=32)
35
+ parser.add_argument(
36
+ "--symmetric",
37
+ action="store_true",
38
+ help="omit zero points (required for int8; uzu never compiles asymmetric int8)",
39
+ )
40
+ parser.add_argument(
41
+ "--no-rewrite",
42
+ action="store_true",
43
+ help="skip the uzu metadata rewrite (the result will not load in uzu)",
44
+ )
45
+ parser.add_argument(
46
+ "--shapes",
47
+ type=Path,
48
+ default=None,
49
+ help="shapes.json to copy; default writes an Argmax chain with budget 16",
50
+ )
51
+ args = parser.parse_args()
52
+
53
+ import equinox as eqx
54
+ import jax
55
+ import jax.numpy as jnp
56
+
57
+ from lalamo.compressed.hybrid import HybridSpec
58
+ from lalamo.compressed.incoherence import HadamardSpec, IncoherenceProcessingMode
59
+ from lalamo.compressed.int import IntSpec
60
+ from lalamo.models.speculator import SpeculatorModel
61
+ from lalamo.modules.linear import Linear
62
+ from lalamo.utils.sharding import ShardingConfig
63
+ from lalamo.utils.surgery import map_nodes_of_type_with_path
64
+ from lalamo.weight_matrix import Layout
65
+
66
+ if args.bits == 8 and not args.symmetric:
67
+ raise SystemExit("int8 must be symmetric: uzu only compiles zero-point dequant for 4 bits")
68
+
69
+ sharding = ShardingConfig.replicated()
70
+ started = time.time()
71
+ model = SpeculatorModel.load(args.source, sharding, dtype=jnp.bfloat16)
72
+ print(f"loaded {args.source} in {time.time() - started:.1f}s", flush=True)
73
+
74
+ spec = HybridSpec(
75
+ quantization_spec=IntSpec(
76
+ bits=args.bits,
77
+ group_size=args.group_size,
78
+ is_symmetric=args.symmetric,
79
+ layout=Layout.OUTPUT_INPUT,
80
+ ),
81
+ adapter_spec=None,
82
+ incoherence_spec=None
83
+ if args.block_size <= 0
84
+ else HadamardSpec(
85
+ block_size=args.block_size,
86
+ processing_mode=IncoherenceProcessingMode.INPUT_OUTPUT,
87
+ ),
88
+ )
89
+
90
+ quantized = 0
91
+ skipped: list[tuple[str, tuple[int, ...]]] = []
92
+ key = jax.random.key(0)
93
+
94
+ def quantize_linear(path: tuple[str, ...], linear: Linear) -> Linear:
95
+ nonlocal quantized
96
+ shape = tuple(int(x) for x in linear.weights.shape)
97
+ if len(shape) != 2 or any(dimension % args.group_size for dimension in shape):
98
+ skipped.append(("/".join(path), shape))
99
+ return linear
100
+ started_at = time.time()
101
+ new_weights = spec.compress(
102
+ linear.weights.decompress(),
103
+ key=key,
104
+ sharding_config=linear.weights.sharding_config,
105
+ is_sharded=linear.weights.is_sharded,
106
+ )
107
+ quantized += 1
108
+ print(f" {'.'.join(path)} {shape} {time.time() - started_at:.1f}s", flush=True)
109
+ return eqx.tree_at(lambda x: x.weights, linear, new_weights)
110
+
111
+ model = map_nodes_of_type_with_path(Linear, quantize_linear, model)
112
+ print(f"quantized {quantized} Linear layers, skipped {len(skipped)}", flush=True)
113
+ for path, shape in skipped:
114
+ print(f" skipped {path} {shape}")
115
+
116
+ args.output.mkdir(parents=True, exist_ok=True)
117
+ model.save(args.output)
118
+
119
+ if args.shapes is not None:
120
+ shutil.copy2(args.shapes, args.output / "shapes.json")
121
+ else:
122
+ (args.output / "shapes.json").write_text(
123
+ json.dumps(
124
+ {
125
+ "default": {
126
+ "tree_budget": 16,
127
+ "max_tree_depth": 16,
128
+ "dflash_depth_override": None,
129
+ "construction_method": {"type": "Argmax"},
130
+ }
131
+ },
132
+ indent=2,
133
+ )
134
+ + "\n",
135
+ encoding="utf-8",
136
+ )
137
+
138
+ if not args.no_rewrite:
139
+ import sys
140
+
141
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
142
+ from rewrite_lalamo_hybrid_spec import rewrite
143
+
144
+ # uzu only understands the flat HybridSpec form; lalamo writes the nested one.
145
+ rewrite(args.output / "model.safetensors", backup=False)
146
+
147
+ total = sum(path.stat().st_size for path in args.output.iterdir())
148
+ print(f"wrote {args.output} ({total / 1e6:.0f}MB) in {time.time() - started:.0f}s")
149
+
150
+
151
+ if __name__ == "__main__":
152
+ main()
scripts/run_lane.py CHANGED
@@ -484,13 +484,17 @@ class Runner:
484
  ]
485
  convert_settings = dict(self.spec.get("convert", {}))
486
  convert_settings.update(condition.get("convert") or {})
487
- for key in ("bits", "group_size", "block_size", "parallel", "embedding_bits"):
488
  if key in convert_settings:
489
  convert_command += [f"--{key.replace('_', '-')}", str(convert_settings[key])]
490
  if convert_settings.get("quantize_embedding"):
491
  convert_command.append("--quantize-embedding")
492
  if convert_settings.get("layer_filter"):
493
  convert_command += ["--layer-filter", str(convert_settings["layer_filter"])]
 
 
 
 
494
  if condition.get("layer_bits_json"):
495
  bits_path = self.resolve(condition["layer_bits_json"])
496
  assert bits_path is not None
@@ -576,7 +580,7 @@ class Runner:
576
  "--calibration-json",
577
  str(out / "calibration.json"),
578
  ]
579
- for key in ("bits", "group_size", "block_size", "parallel", "embedding_bits"):
580
  if key in self.spec.get("convert", {}):
581
  convert_command += [f"--{key.replace('_', '-')}", str(self.spec["convert"][key])]
582
  if self.spec.get("convert", {}).get("quantize_embedding"):
 
484
  ]
485
  convert_settings = dict(self.spec.get("convert", {}))
486
  convert_settings.update(condition.get("convert") or {})
487
+ for key in ("bits", "group_size", "block_size", "parallel", "embedding_bits", "symmetric_bits"):
488
  if key in convert_settings:
489
  convert_command += [f"--{key.replace('_', '-')}", str(convert_settings[key])]
490
  if convert_settings.get("quantize_embedding"):
491
  convert_command.append("--quantize-embedding")
492
  if convert_settings.get("layer_filter"):
493
  convert_command += ["--layer-filter", str(convert_settings["layer_filter"])]
494
+ if convert_settings.get("layer_bits_json"):
495
+ settings_path = self.resolve(convert_settings["layer_bits_json"])
496
+ assert settings_path is not None
497
+ convert_command += ["--layer-bits-json", str(settings_path)]
498
  if condition.get("layer_bits_json"):
499
  bits_path = self.resolve(condition["layer_bits_json"])
500
  assert bits_path is not None
 
580
  "--calibration-json",
581
  str(out / "calibration.json"),
582
  ]
583
+ for key in ("bits", "group_size", "block_size", "parallel", "embedding_bits", "symmetric_bits"):
584
  if key in self.spec.get("convert", {}):
585
  convert_command += [f"--{key.replace('_', '-')}", str(self.spec["convert"][key])]
586
  if self.spec.get("convert", {}).get("quantize_embedding"):