File size: 4,442 Bytes
d8290eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Reserved asynchronous summary logging; disk work stays off the control thread."""
from __future__ import annotations
from collections import Counter
import csv
import json
from pathlib import Path
import queue
import threading
import numpy as np
from .events import ADJUDICATION_VERSION
from .metrics import BallMetrics, REWARD_FIELDS, format_ball


def json_default(value):
    if isinstance(value, np.ndarray):
        return value.tolist()
    if isinstance(value, np.generic):
        return value.item()
    raise TypeError(type(value).__name__)


class AsyncJournal:
    def __init__(self, root, capacity):
        self.root = Path(root)
        self.queue = queue.Queue()
        self.slots = threading.BoundedSemaphore(capacity)
        self.error = None
        self.closed = False
        self.dropped_balls = Counter()
        self.dropped_telemetry = 0
        self.submitted_balls = self.written_balls = 0
        self.metrics = BallMetrics()
        self.thread = threading.Thread(target=self._run, name="h2-rl-journal", daemon=True)
        self.thread.start()

    def reserve(self):
        return not self.closed and self.error is None and self.slots.acquire(blocking=False)

    def missed_ball(self, phase):
        self.dropped_balls[phase] += 1
        if sum(self.dropped_balls.values()) == 1:
            print("[RL ERROR] ball summary unavailable; unlogged attempts are counted in journal_status.json")

    def submit_reserved(self, record):
        if record.get("kind", "ball") == "ball":
            self.submitted_balls += 1
        self.queue.put_nowait(record)

    def telemetry(self, record):
        if self.reserve():
            self.submit_reserved(dict(kind="telemetry", summary=record))
        else:
            self.dropped_telemetry += 1

    def snapshot(self):
        return dict(unlogged_attempts=dict(self.dropped_balls),
                    submitted_balls=self.submitted_balls, written_balls=self.written_balls,
                    unwritten_balls=self.submitted_balls-self.written_balls,
                    dropped_telemetry=self.dropped_telemetry, error=self.error)

    def _run(self):
        try:
            with (self.root / "balls.jsonl").open("a", encoding="utf-8") as balls, \
                 (self.root / "control.jsonl").open("a", encoding="utf-8") as control, \
                 (self.root / "metrics.csv").open("w", newline="", encoding="utf-8") as metrics:
                template = dict(kind="", phase="", attempt="", **dict.fromkeys(REWARD_FIELDS),
                                **self.metrics.phases["warmup"].snapshot())
                writer = csv.DictWriter(metrics, fieldnames=list(template))
                writer.writeheader()
                while True:
                    item = self.queue.get()
                    if item is None:
                        for phase, stats in self.metrics.phases.items():
                            writer.writerow(dict(kind="final", phase=phase, attempt="",
                                                 **stats.snapshot(self.dropped_balls[phase])))
                        return
                    try:
                        summary = dict(item["summary"], adjudication_version=ADJUDICATION_VERSION)
                        is_ball = item.get("kind", "ball") == "ball"
                        stream = balls if is_ball else control
                        stream.write(json.dumps(summary, default=json_default, allow_nan=False) + "\n")
                        stream.flush()
                        if is_ball:
                            self.written_balls += 1
                            row = self.metrics.add(summary, self.dropped_balls[summary["phase"]])
                            writer.writerow(row)
                            metrics.flush()
                            print(format_ball(summary, row), flush=True)
                    finally:
                        self.slots.release()
        except Exception as error:
            self.error = str(error)
            print(f"[RL ERROR] journal failed: {error}", flush=True)

    def close(self):
        if self.closed:
            return
        self.closed = True
        self.queue.put_nowait(None)
        self.thread.join()
        (self.root / "journal_status.json").write_text(
            json.dumps(self.snapshot(), indent=2), encoding="utf-8")
        if self.error:
            raise RuntimeError(f"RL journal failed: {self.error}")