-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_generation_clone.py
More file actions
211 lines (190 loc) · 7.63 KB
/
Copy pathbench_generation_clone.py
File metadata and controls
211 lines (190 loc) · 7.63 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
#!/usr/bin/env python3
"""Reproduce SD-12 generation clone/publish bookkeeping evidence."""
from __future__ import annotations
import argparse
import json
import logging
import shutil
import statistics
import tempfile
import time
from pathlib import Path
from codexa.store.generations import (
GenerationCloneStats,
begin_generation_build,
publish_generation_if_staged,
)
from codexa.indexing.progressive_ready_proof import ReadyProof
from codexa.store.progressive_generation_publish import (
commit_ready_generation,
mark_generation_ready,
)
from codexa.store.progressive_generations import _progressive_clone_base
_MIB = 1024 * 1024
def _cfg(root: Path) -> dict:
return {
"store": {
"backend": "sharded_chroma",
"persist_dir": str(root),
"publish": "generations",
"retain_generations": 2,
},
"metadata": {
"manifest_file": str(root / "manifest.json"),
"index_info_file": str(root / "index_info.json"),
},
}
def _sized_file(path: Path, size: int) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("wb") as stream:
stream.truncate(size)
def _seed(root: Path, buckets: int) -> None:
current = root / "gen_000001"
current.mkdir(parents=True)
for index in range(buckets):
stem = "txt_a_0000000000" if index == 0 else f"pdf_b_{index:010d}"
_sized_file(current / f"{stem}.sqlite3", _MIB)
shard = current / stem
_sized_file(shard / "segment.bin", _MIB // 2)
(shard / "chroma.sqlite3").symlink_to(Path("..") / f"{stem}.sqlite3")
_sized_file(current / "manifest.json", 64 * 1024)
(current / "index_info.json").write_text("{}", encoding="utf-8")
_sized_file(current / "bm25.db", _MIB // 2)
_sized_file(current / "colbert_doc_embeddings.sqlite3", _MIB // 2)
(root / "CURRENT").write_text("gen_000001\n", encoding="utf-8")
def _one_run(buckets: int) -> tuple[GenerationCloneStats, float]:
with tempfile.TemporaryDirectory(prefix="codexa-sd12-") as temp:
root = Path(temp) / "store"
_seed(root, buckets)
cfg = _cfg(root)
log = logging.getLogger("codexa.sd12.bench")
build = begin_generation_build(
cfg,
changed_sources=["/data/apple.txt"],
force_reindex=False,
manifest_path=str(root / "manifest.json"),
index_info_path=str(root / "index_info.json"),
log=log,
)
if build is None or build.clone_stats is None:
raise RuntimeError("generation clone did not run")
started = time.perf_counter()
publish_generation_if_staged(build.cfg, log)
publish_ms = (time.perf_counter() - started) * 1000.0
return build.clone_stats, publish_ms
def _progressive_proof(generation: str) -> ReadyProof:
return ReadyProof(
build_id="benchmark", generation=generation, checkpoint_seq=1,
base_generation=None, checkpoint_mode="delta", contract="benchmark",
revision="benchmark", target_digest="a" * 64,
manifest_digest="b" * 64, manifest_records_digest="c" * 64,
dense_digest="d" * 64, manifest_sources=1, manifest_chunks=1,
dense_sources=1, dense_chunks=1, bm25_enabled=False,
bm25_digest=None, bm25_sources=None, bm25_rows=None,
colbert_enabled=False, colbert_digest=None, colbert_sources=None,
colbert_rows=None, bm25_inventory_digest=None,
colbert_inventory_digest=None, index_info_digest="e" * 64,
indexed=1, total=2, partial_corpus=True, processed_ordinals=(0,),
indexed_ordinals=(0,), dense_inventory_digest="f" * 64,
batch_mapping_digest="1" * 64, cumulative_source_digest="2" * 64,
committed_phase_digest="3" * 64, phase_state_digest="4" * 64,
phase_transition_digest="5" * 64,
)
def _one_progressive_run(
buckets: int,
) -> tuple[GenerationCloneStats, float]:
with tempfile.TemporaryDirectory(prefix="codexa-rob116-") as temp:
root = Path(temp) / "clone"
_seed(root, buckets)
staging = root / "gen_000002"
stats = _progressive_clone_base(
root,
staging,
build_id="benchmark",
checkpoint_seq=2,
base_generation="gen_000001",
lineage_fields={
"lineage_digest": "0" * 64,
"base_checkpoint_seq": 1,
},
manifest_name="manifest.json",
index_info_name="index_info.json",
)
if stats is None:
raise RuntimeError("progressive clone did not run")
shutil.rmtree(staging)
publish_root = Path(temp) / "publish"
cfg = _cfg(publish_root)
build = begin_generation_build(
cfg,
changed_sources=[],
force_reindex=True,
manifest_path=str(publish_root / "manifest.json"),
index_info_path=str(publish_root / "index_info.json"),
log=logging.getLogger("codexa.rob116.bench"),
)
if build is None:
raise RuntimeError("progressive publication stage did not open")
proof = _progressive_proof(build.generation_id)
mark_generation_ready(
build.cfg,
proof_json=proof.as_json(),
base_generation=None,
log=logging.getLogger("codexa.rob116.bench"),
)
started = time.perf_counter()
published = commit_ready_generation(
build.cfg,
proof_json=proof.as_json(),
base_generation=None,
validate=lambda: proof,
log=logging.getLogger("codexa.rob116.bench"),
)
pointer_ms = (time.perf_counter() - started) * 1000.0
if not published:
raise RuntimeError("progressive pointer publication did not complete")
return stats, pointer_ms
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--runs", type=int, default=5)
parser.add_argument("--buckets", type=int, default=32)
args = parser.parse_args()
if args.runs < 1 or args.buckets < 2:
parser.error("--runs must be >= 1 and --buckets must be >= 2")
samples = [_one_run(args.buckets) for _ in range(args.runs)]
progressive = [
_one_progressive_run(args.buckets) for _ in range(args.runs)
]
stats = samples[-1][0]
progressive_stats = progressive[-1][0]
payload = {
"runs": args.runs,
"buckets": args.buckets,
"logical_bytes": stats.logical_bytes,
"copied_files": stats.copied_files,
"copied_bytes": stats.copied_bytes,
"copied_ratio": stats.copied_bytes / stats.logical_bytes,
"hardlinked_files": stats.hardlinked_files,
"hardlinked_bytes": stats.hardlinked_bytes,
"hardlink_fallback_files": stats.hardlink_fallback_files,
"median_clone_ms": statistics.median(
sample.clone_duration_s * 1000.0 for sample, _ in samples
),
"median_publish_gc_ms": statistics.median(
publish_ms for _, publish_ms in samples
),
"progressive_hardlinked_files": progressive_stats.hardlinked_files,
"progressive_copied_files": progressive_stats.copied_files,
"progressive_hardlink_fallback_files": (
progressive_stats.hardlink_fallback_files
),
"median_progressive_clone_ms": statistics.median(
sample.clone_duration_s * 1000.0 for sample, _ in progressive
),
"median_progressive_pointer_ms": statistics.median(
pointer_ms for _, pointer_ms in progressive
),
}
print(json.dumps(payload, indent=2, sort_keys=True))
if __name__ == "__main__":
main()