-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_server.py
More file actions
527 lines (460 loc) · 19.3 KB
/
data_server.py
File metadata and controls
527 lines (460 loc) · 19.3 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
from __future__ import annotations
import argparse
import array
import bisect
import concurrent.futures
import gzip
import hashlib
import json
import os
import random
import threading
import time
import uuid
from dataclasses import dataclass
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from typing import Any
from urllib.parse import parse_qs, urlparse
from omegaconf import OmegaConf
def load_api_keys_from_file(file_path: str | None) -> list[str]:
if not file_path:
return []
expanded = os.path.expanduser(file_path)
if not os.path.exists(expanded):
return []
keys: list[str] = []
with open(expanded, "r", encoding="utf-8") as key_file:
for line in key_file:
value = line.strip()
if value:
keys.append(value)
return keys
@dataclass(frozen=True)
class IndexedFile:
path: str
size: int
mtime_ns: int
index_path: str
count: int
cumulative_end: int
def _iter_jsonl_files(data_root: str) -> list[str]:
result: list[str] = []
for root, _, names in os.walk(data_root):
for name in names:
if name.endswith(".jsonl"):
result.append(os.path.join(root, name))
result.sort()
return result
def _index_path_for(index_root: str, file_path: str) -> str:
digest = hashlib.sha1(file_path.encode("utf-8")).hexdigest()
return os.path.join(index_root, f"{digest}.idx")
def _build_offsets_file(file_path: str, index_path: str) -> tuple[int, int, int]:
offsets = array.array("Q")
with open(file_path, "rb") as source:
while True:
offset = source.tell()
line = source.readline()
if not line:
break
offsets.append(offset)
os.makedirs(os.path.dirname(index_path), exist_ok=True)
with open(index_path, "wb") as index_file:
offsets.tofile(index_file)
stat = os.stat(file_path)
return len(offsets), stat.st_size, stat.st_mtime_ns
class JsonlIndexStore:
def __init__(self, data_root: str, index_root: str, workers: int | None = None) -> None:
self.data_root = os.path.abspath(data_root)
self.index_root = os.path.abspath(index_root)
os.makedirs(self.index_root, exist_ok=True)
self.files: list[IndexedFile] = []
self._offset_cache: dict[str, array.array] = {}
self.workers = max(1, workers or (os.cpu_count() or 4))
self.manifest_path = os.path.join(self.index_root, "manifest.json")
self._build_manifest()
def _load_manifest(self) -> dict[str, dict[str, int | str]] | None:
if not os.path.exists(self.manifest_path):
return None
with open(self.manifest_path, "r", encoding="utf-8") as manifest_file:
payload = json.load(manifest_file)
files = payload.get("files")
if not isinstance(files, dict):
return None
return files
def _load_or_build_offsets(self, file_path: str) -> array.array:
cached = self._offset_cache.get(file_path)
if cached is not None:
return cached
index_path = _index_path_for(self.index_root, file_path)
if os.path.exists(index_path):
offsets = array.array("Q")
with open(index_path, "rb") as index_file:
offsets.frombytes(index_file.read())
self._offset_cache[file_path] = offsets
return offsets
_build_offsets_file(file_path, index_path)
offsets = array.array("Q")
with open(index_path, "rb") as index_file:
offsets.frombytes(index_file.read())
self._offset_cache[file_path] = offsets
return offsets
def _save_manifest(self, manifest_files: dict[str, dict[str, int | str]]) -> None:
payload = {
"data_root": self.data_root,
"index_root": self.index_root,
"files": manifest_files,
}
tmp_path = f"{self.manifest_path}.tmp"
with open(tmp_path, "w", encoding="utf-8") as manifest_file:
json.dump(payload, manifest_file, ensure_ascii=False)
os.replace(tmp_path, self.manifest_path)
def _build_manifest(self) -> None:
data_files = _iter_jsonl_files(self.data_root)
if not data_files:
raise FileNotFoundError(f"no .jsonl files found under {self.data_root}")
cached_manifest = self._load_manifest() or {}
manifest_files: dict[str, dict[str, int | str]] = {}
rebuild_jobs: list[tuple[str, str]] = []
for file_path in data_files:
stat = os.stat(file_path)
index_path = _index_path_for(self.index_root, file_path)
cached = cached_manifest.get(file_path)
if (
cached is not None
and cached.get("size") == stat.st_size
and cached.get("mtime_ns") == stat.st_mtime_ns
and cached.get("index_path") == index_path
and os.path.exists(index_path)
):
manifest_files[file_path] = cached
else:
rebuild_jobs.append((file_path, index_path))
if rebuild_jobs:
with concurrent.futures.ProcessPoolExecutor(max_workers=self.workers) as executor:
future_map = {
executor.submit(_build_offsets_file, file_path, index_path): (file_path, index_path)
for file_path, index_path in rebuild_jobs
}
for future in concurrent.futures.as_completed(future_map):
file_path, index_path = future_map[future]
count, size, mtime_ns = future.result()
manifest_files[file_path] = {
"index_path": index_path,
"count": count,
"size": size,
"mtime_ns": mtime_ns,
}
self._save_manifest(manifest_files)
cumulative = 0
for file_path in data_files:
info = manifest_files[file_path]
count = int(info["count"])
cumulative += count
stat = os.stat(file_path)
self.files.append(
IndexedFile(
path=file_path,
size=stat.st_size,
mtime_ns=stat.st_mtime_ns,
index_path=str(info["index_path"]),
count=count,
cumulative_end=cumulative,
)
)
if cumulative == 0:
raise ValueError(f"no records found under {self.data_root}")
@property
def total_records(self) -> int:
return self.files[-1].cumulative_end
def resolve_index(self, global_index: int) -> tuple[IndexedFile, int]:
if global_index < 0 or global_index >= self.total_records:
raise IndexError(f"global_index out of range: {global_index}")
cumulative_ends = [item.cumulative_end for item in self.files]
file_idx = bisect.bisect_right(cumulative_ends, global_index)
indexed_file = self.files[file_idx]
previous_end = 0 if file_idx == 0 else self.files[file_idx - 1].cumulative_end
local_index = global_index - previous_end
return indexed_file, local_index
def read_record(self, global_index: int) -> dict[str, Any]:
indexed_file, local_index = self.resolve_index(global_index)
offsets = self._load_or_build_offsets(indexed_file.path)
offset = offsets[local_index]
with open(indexed_file.path, "rb") as source:
source.seek(offset)
line = source.readline()
record = json.loads(line.decode("utf-8"))
record["_global_index"] = global_index
return record
class StreamSession:
def __init__(self, session_id: str, store: JsonlIndexStore, seed: int, shuffle_buffer_size: int) -> None:
self.session_id = session_id
self.store = store
self.seed = seed
self.shuffle_buffer_size = max(1, shuffle_buffer_size)
self.file_order = list(range(len(store.files)))
self.rng = random.Random(seed)
self.rng.shuffle(self.file_order)
self.file_order_cursor = 0
self.current_local_index = 0
self.buffer: list[int] = []
self.exhausted = False
self.lock = threading.Lock()
def _next_global_index(self) -> int | None:
while self.file_order_cursor < len(self.file_order):
file_idx = self.file_order[self.file_order_cursor]
indexed_file = self.store.files[file_idx]
if self.current_local_index < indexed_file.count:
file_start = indexed_file.cumulative_end - indexed_file.count
global_index = file_start + self.current_local_index
self.current_local_index += 1
return global_index
self.file_order_cursor += 1
self.current_local_index = 0
return None
def _fill_buffer(self) -> None:
while len(self.buffer) < self.shuffle_buffer_size:
next_index = self._next_global_index()
if next_index is None:
break
self.buffer.append(next_index)
if not self.buffer and self._next_global_index() is None:
self.exhausted = True
def next_batch(self, batch_size: int) -> dict[str, Any]:
texts: list[str] = []
indices: list[int] = []
with self.lock:
self._fill_buffer()
while len(texts) < batch_size and self.buffer:
picked_idx = self.rng.randrange(len(self.buffer))
global_index = self.buffer[picked_idx]
replacement = self._next_global_index()
if replacement is None:
self.buffer.pop(picked_idx)
else:
self.buffer[picked_idx] = replacement
record = self.store.read_record(global_index)
text = record.get("text")
if not text:
continue
texts.append(text)
indices.append(global_index)
if not self.buffer and self.file_order_cursor >= len(self.file_order):
self.exhausted = True
return {
"session_id": self.session_id,
"texts": texts,
"indices": indices,
"batch_size": len(texts),
"exhausted": self.exhausted and not texts,
}
class DataServerState:
def __init__(
self,
data_root: str,
index_root: str,
api_key_file: str | None = None,
workers: int | None = None,
) -> None:
self.store = JsonlIndexStore(data_root=data_root, index_root=index_root, workers=workers)
self.sessions: dict[str, StreamSession] = {}
self.sessions_lock = threading.Lock()
self.started_at = time.time()
self.api_keys = self._resolve_api_keys(api_key_file=api_key_file)
if not self.api_keys:
raise ValueError(
"no API keys loaded; please configure data.data_server_api_key_file "
"with at least one non-empty key"
)
@staticmethod
def _resolve_api_keys(api_key_file: str | None) -> set[str]:
keys: set[str] = set()
for key in load_api_keys_from_file(api_key_file):
keys.add(key)
return keys
def open_session(self, seed: int, shuffle_buffer_size: int) -> dict[str, Any]:
session_id = uuid.uuid4().hex
session = StreamSession(
session_id=session_id,
store=self.store,
seed=seed,
shuffle_buffer_size=shuffle_buffer_size,
)
with self.sessions_lock:
self.sessions[session_id] = session
return {
"session_id": session_id,
"seed": seed,
"shuffle_buffer_size": shuffle_buffer_size,
"total_records": self.store.total_records,
}
def get_session(self, session_id: str) -> StreamSession:
with self.sessions_lock:
session = self.sessions.get(session_id)
if session is None:
raise KeyError(f"unknown session_id: {session_id}")
return session
def build_handler(state: DataServerState):
class DataServerHandler(BaseHTTPRequestHandler):
server_version = "NestedLearningDataServer/1.0"
def log_message(self, fmt: str, *args: object) -> None:
return
def _supports_gzip(self) -> bool:
accept_encoding = self.headers.get("Accept-Encoding", "")
return "gzip" in accept_encoding.lower()
def _write_json(self, payload: dict[str, Any], status: int = HTTPStatus.OK) -> None:
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
use_gzip = self._supports_gzip() and len(body) >= 1024
if use_gzip:
body = gzip.compress(body, compresslevel=5)
self.send_response(status)
self.send_header("Content-Type", "application/json; charset=utf-8")
if use_gzip:
self.send_header("Content-Encoding", "gzip")
self.send_header("Vary", "Accept-Encoding")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def _check_api_key(self) -> bool:
if not state.api_keys:
return True
provided = self.headers.get("X-Infimory-Api-Key", "")
return provided in state.api_keys
def do_GET(self) -> None:
parsed = urlparse(self.path)
query = parse_qs(parsed.query)
try:
if not self._check_api_key():
self._write_json({"error": "unauthorized"}, status=HTTPStatus.UNAUTHORIZED)
return
if parsed.path == "/health":
self._write_json(
{
"status": "ok",
"total_records": state.store.total_records,
"uptime_sec": round(time.time() - state.started_at, 3),
}
)
return
if parsed.path == "/v1/meta":
self._write_json(
{
"total_records": state.store.total_records,
"files": len(state.store.files),
"data_root": state.store.data_root,
"index_root": state.store.index_root,
"workers": state.store.workers,
}
)
return
if parsed.path == "/v1/sample":
global_index = int(query["idx"][0])
record = state.store.read_record(global_index)
self._write_json(record)
return
if parsed.path == "/v1/session/open":
seed = int(query.get("seed", ["1234"])[0])
shuffle_buffer_size = int(query.get("shuffle_buffer_size", ["8192"])[0])
self._write_json(state.open_session(seed=seed, shuffle_buffer_size=shuffle_buffer_size))
return
if parsed.path == "/v1/session/next":
session_id = query["session_id"][0]
batch_size = int(query.get("batch_size", ["1"])[0])
session = state.get_session(session_id)
payload = session.next_batch(batch_size=batch_size)
print(
json.dumps(
{
"event": "batch_sent",
"time": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
"session_id": session_id,
"requested_batch_size": batch_size,
"sent_batch_size": payload.get("batch_size", 0),
},
ensure_ascii=False,
),
flush=True,
)
self._write_json(payload)
return
self._write_json({"error": f"unknown path: {parsed.path}"}, status=HTTPStatus.NOT_FOUND)
except KeyError as exc:
self._write_json({"error": str(exc)}, status=HTTPStatus.NOT_FOUND)
except IndexError as exc:
self._write_json({"error": str(exc)}, status=HTTPStatus.BAD_REQUEST)
except Exception as exc:
self._write_json({"error": str(exc)}, status=HTTPStatus.INTERNAL_SERVER_ERROR)
return DataServerHandler
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Serve cleaned JSONL training data over HTTP.")
parser.add_argument(
"--config-file",
default="configs/hope_tiny.yaml",
help="YAML config file that contains data.data_server_* settings.",
)
parser.add_argument(
"--data-root",
default=None,
help="Root directory containing cleaned JSONL files.",
)
parser.add_argument(
"--index-root",
default=None,
help="Directory for persistent line offset indexes.",
)
parser.add_argument("--host", default="0.0.0.0")
parser.add_argument("--port", type=int, default=18080)
parser.add_argument("--workers", type=int, default=max(1, os.cpu_count() or 4))
return parser.parse_args()
def load_server_config(config_file: str) -> dict[str, Any]:
expanded = os.path.expanduser(config_file)
if not os.path.exists(expanded):
return {}
cfg = OmegaConf.load(expanded)
data_cfg = cfg.get("data")
if data_cfg is None:
return {}
return OmegaConf.to_container(data_cfg, resolve=True) # type: ignore[return-value]
def main() -> None:
args = parse_args()
data_cfg = load_server_config(args.config_file)
data_root = args.data_root or data_cfg.get(
"data_server_data_root",
"/data/datasets/nested-learning/extract_data/cleaned_data",
)
index_root = args.index_root or data_cfg.get(
"data_server_index_root",
"/data/datasets/nested-learning/extract_data/data_index",
)
api_key_file = data_cfg.get(
"data_server_api_key_file",
"~/.config/infimory/api_key.txt",
)
state = DataServerState(
data_root=data_root,
index_root=index_root,
api_key_file=api_key_file,
workers=args.workers,
)
server = ThreadingHTTPServer((args.host, args.port), build_handler(state))
print(
json.dumps(
{
"event": "data_server_started",
"host": args.host,
"port": args.port,
"config_file": os.path.abspath(os.path.expanduser(args.config_file)),
"data_root": os.path.abspath(os.path.expanduser(data_root)),
"index_root": os.path.abspath(os.path.expanduser(index_root)),
"workers": args.workers,
"api_key_enabled": bool(state.api_keys),
"api_key_count": len(state.api_keys),
"total_records": state.store.total_records,
},
ensure_ascii=False,
),
flush=True,
)
server.serve_forever()
if __name__ == "__main__":
main()