Repository navigation
Expand file tree
/
Copy pathprofiling.py
More file actions
341 lines (315 loc) · 12.8 KB
/
Copy pathprofiling.py
File metadata and controls
341 lines (315 loc) · 12.8 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
"""Caller-owned,source-bound performance observations;never acceptance evidence."""
from infinicoder._resources import package_root
from dataclasses import dataclass
import hashlib
import json
import math
from pathlib import Path, PurePosixPath
import re
import time
import uuid
def identity():
folder = package_root()
return hashlib.sha256(
b"".join(
(folder / name).read_bytes()
for name in (
"optimization/profiling.py",
"optimization/nsight.py",
"runtime/engine.py",
"runtime/protocol.py",
"runtime/shell_loop.py",
)
)
).hexdigest()
def _text(value, label, limit=200):
if not isinstance(value, str) or not value.strip() or len(value) > limit:
raise ValueError("Invalid " + label)
return value
def _relative(value):
_text(value, "relative path", 500)
p = PurePosixPath(value)
if p.is_absolute() or ".." in p.parts or "\\" in value or ":" in value or str(p) != value:
raise ValueError("Explicit normalized relative paths required")
return value
@dataclass(frozen=True)
class ProfileWorkload:
name: str
collect: object
identity: str
def __post_init__(self):
if not isinstance(self.name, str) or not re.fullmatch(
"[A-Za-z][A-Za-z0-9_-]{0,47}", self.name
):
raise ValueError("Invalid profiling workload name")
if not callable(self.collect):
raise TypeError("Caller-owned profiling callback required")
_text(self.identity, "workload identity", 500)
@dataclass(frozen=True)
class ProfilingObjective:
paths: tuple
workloads: tuple
identity: str
max_seconds: float = 60
def __post_init__(self):
if (
not isinstance(self.paths, tuple)
or not 1 <= len(self.paths) <= 64
or len(set(self.paths)) != len(self.paths)
):
raise ValueError("One to64 unique tracked source paths required as a tuple")
for path in self.paths:
_relative(path)
if (
not isinstance(self.workloads, tuple)
or not 1 <= len(self.workloads) <= 16
or not all(isinstance(w, ProfileWorkload) for w in self.workloads)
):
raise ValueError("Explicit bounded profiling workloads required as a tuple")
if len({w.name for w in self.workloads}) != len(self.workloads):
raise ValueError("Duplicate profiling workload")
_text(self.identity, "profiling identity", 500)
if (
isinstance(self.max_seconds, bool)
or not isinstance(self.max_seconds, (int, float))
or not 0 < self.max_seconds <= 3600
):
raise ValueError("Profiling timeout must be finite and in(0,3600]")
def contract(self):
return dict(
identity=self.identity,
paths=list(self.paths),
max_seconds=self.max_seconds,
workloads=[dict(name=w.name, identity=w.identity) for w in self.workloads],
)
@dataclass(frozen=True)
class ProfileReport:
tool: str
tool_version: str
device: str
metrics: dict
hotspots: tuple = ()
artifacts: tuple = ()
status: str = "observed"
error: str = ""
def data(self, folder):
for label in ("tool", "tool_version", "device"):
_text(getattr(self, label), label)
if (
self.status not in ("observed", "unavailable", "error")
or not isinstance(self.error, str)
or len(self.error) > 1000
):
raise ValueError("Invalid profiling status")
if not isinstance(self.metrics, dict) or len(self.metrics) > 24:
raise ValueError("Bounded named profile metrics required")
for name, metric in self.metrics.items():
_text(name, "metric name", 80)
if not isinstance(metric, dict) or set(metric) != {"value", "unit"}:
raise ValueError("A profile metric needs value and unit")
value = metric["value"]
if (
isinstance(value, bool)
or not isinstance(value, (int, float))
or not math.isfinite(value)
):
raise ValueError("Finite numeric profile metric required")
_text(metric["unit"], "metric unit", 48)
if self.status != "observed" and self.metrics:
raise ValueError("Unsuccessful profiling cannot supply current metrics")
if self.status == "observed" and not self.metrics:
raise ValueError("Observed profiling requires metrics")
if not isinstance(self.hotspots, tuple) or len(self.hotspots) > 5:
raise ValueError("At most five hotspot records required")
if any(
not isinstance(v, dict) or len(json.dumps(v, ensure_ascii=False)) > 1500
for v in self.hotspots
):
raise ValueError("Bounded diagnostic hotspot records required")
if (
not isinstance(self.artifacts, tuple)
or len(self.artifacts) > 8
or len(set(self.artifacts)) != len(self.artifacts)
):
raise ValueError("At most eight unique profiler-owned artifacts required")
files = {}
for name in self.artifacts:
_relative(name)
path = folder / name
if (
path.is_symlink()
or not path.resolve().is_relative_to(folder.resolve())
or not path.is_file()
or path.stat().st_size > 256 * 1024 * 1024
):
raise ValueError("Profiler artifact must be an owned regular file")
files[name] = dict(
sha256=hashlib.sha256(path.read_bytes()).hexdigest(), bytes=path.stat().st_size
)
# Copy callback data;neither later callback mutation nor non-JSON values
# may silently alter persisted evidence.
return json.loads(
json.dumps(
dict(
tool=self.tool,
tool_version=self.tool_version,
device=self.device,
metrics=self.metrics,
hotspots=self.hotspots,
artifacts=files,
status=self.status,
error=self.error,
),
allow_nan=False,
)
)
class ProfilingSession:
def __init__(self, objective, workspace, state, store, artifact_root):
self.objective, self.ws, self.state, self.store = objective, workspace, state, store
self.root = Path(artifact_root).resolve()
if self.root.is_relative_to(workspace.root):
raise ValueError("Profiling evidence must be outside editable workspace")
self.contract = objective.contract()
saved = state.get("profiling")
if saved is not None and saved["contract"] != self.contract:
raise ValueError("Profiling contract changed across resume")
if saved is None:
state["profiling"] = dict(contract=self.contract, reports=[])
def sources(self):
result = {}
for name in self.objective.paths:
path = self.ws.path(name)
if not path.is_file() or path.stat().st_size > 8 * 1024 * 1024:
raise ValueError("Tracked profiling source missing or larger than8MiB: " + name)
result[name] = hashlib.sha256(path.read_bytes()).hexdigest()
return result
def collect(self, workload, remaining):
if not isinstance(workload, str) or workload not in {
w.name for w in self.objective.workloads
}:
raise ValueError("Choose an explicitly configured profiling workload")
if remaining() <= 0:
raise ValueError("No task budget remains for profiling")
before = self.sources()
source_id = hashlib.sha256(json.dumps(before, sort_keys=True).encode()).hexdigest()
for row in reversed(self.state["profiling"]["reports"]):
if (
row["workload"] == workload
and row["tracked_sources"] == before
and row["status"] == "observed"
):
return dict(self._summary(row), reused=True)
timeout = min(self.objective.max_seconds, remaining())
if timeout <= 0:
raise ValueError("No task budget remains for profiling")
folder = self.root / uuid.uuid4().hex
folder.mkdir(parents=True, exist_ok=False)
row = dict(
id=folder.name,
workload=workload,
source_fingerprint=source_id,
tracked_sources=before,
status="error",
metrics={},
hotspots=[],
artifacts={},
error="",
diagnostic_only=True,
acceptance_proof=False,
selection_measurement=False,
artifact_directory=str(folder),
)
self.store.event(
"profiling_started",
dict(id=row["id"], workload=workload, source_fingerprint=source_id, timeout_s=timeout),
)
tick = time.monotonic()
try:
collector = next(w.collect for w in self.objective.workloads if w.name == workload)
report = collector(self.ws.root, folder, timeout)
if not isinstance(report, ProfileReport):
raise TypeError("Trusted profiler must return ProfileReport")
row.update(report.data(folder))
except Exception as exc:
row.update(
status="error",
error=type(exc).__name__ + ": " + str(exc)[:700],
metrics={},
hotspots=[],
)
finally:
row["wall_s"] = time.monotonic() - tick
try:
unchanged = self.sources() == before
except (ValueError, OSError):
unchanged = False
row["wall_s"] = time.monotonic() - tick
if not unchanged:
row.update(
status="source_changed",
metrics={},
hotspots=[],
error="Tracked source changed during profiling;observations invalidated",
)
elif row["wall_s"] > timeout or remaining() <= 0:
row.update(
status="deadline_exceeded",
metrics={},
hotspots=[],
error="Profiling exceeded its shared task budget",
)
with (folder / "receipt.json").open("x", encoding="utf-8") as stream:
json.dump(row, stream, indent=2, allow_nan=False)
self.state["profiling"]["reports"].append(row)
del self.state["profiling"]["reports"][:-8]
self.store.event("profiling_observation", row)
return self._summary(row)
@staticmethod
def _summary(row):
return json.loads(
json.dumps(
{k: v for k, v in row.items() if k not in ("tracked_sources", "artifact_directory")}
)
)
def view(self, estimator, max_tokens=400):
rows = self.state["profiling"]["reports"]
result = dict(
workloads=[w.name for w in self.objective.workloads],
diagnostic_only=True,
acceptance_proof=False,
)
if rows:
latest = rows[-1]
try:
current = self.sources() == latest["tracked_sources"]
except (ValueError, OSError):
current = False
result["latest"] = self._summary(latest)
if not current:
result["latest"] = dict(
id=latest["id"],
workload=latest["workload"],
status="stale_source_requires_profile",
historical_source_fingerprint=latest["source_fingerprint"],
metrics={},
)
omitted = 0
while estimator.estimate(json.dumps(result, ensure_ascii=False)) > max_tokens:
latest = result.get("latest", {})
if latest.get("hotspots"):
latest["hotspots"] = latest["hotspots"][:-1]
elif latest.get("artifacts"):
latest.pop("artifacts")
elif latest.get("metrics"):
latest["metrics"].pop(next(reversed(latest["metrics"])))
omitted += 1
latest["omitted_metrics"] = omitted
else:
return None
return result
def guidance(objective):
return (
'\nprofile {"workload":name} -> run only a caller-configured profiler workload: '
+ ", ".join(w.name for w in objective.workloads)
+ ". Source-bound observations are diagnostic only;verify correctness and performance independently. Earlier observations refer to their recorded source,not current edits. Kernel duration sums and shares are not application wall time or GPU utilization.\n"
)