forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvgf_neural_statistics.py
More file actions
124 lines (95 loc) · 4.3 KB
/
Copy pathvgf_neural_statistics.py
File metadata and controls
124 lines (95 loc) · 4.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# Copyright 2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pyre-unsafe
import base64
import binascii
import json
from typing import Any, Dict, List, Sequence, Union
# Functions here are used to take the raw delegate_debug_metadata bytes stored in ETDump,
# check whether those bytes contain VGF neural statistics JSON,
# validate the schema/version, decode base64-encoded binary blobs,
# and return a normal Python dictionary that tooling can consume.
SCHEMA = "executorch.vgf.neural_statistics"
SCHEMA_VERSION = 1
DelegateMetadataBytes = Union[bytes, bytearray, str]
def _to_bytes(metadata: DelegateMetadataBytes) -> bytes:
if isinstance(metadata, bytes):
return metadata
if isinstance(metadata, bytearray):
return bytes(metadata)
if isinstance(metadata, str):
return metadata.encode("utf-8")
raise TypeError(f"Unsupported delegate metadata type: {type(metadata)}")
def _decode_blob(blob: Dict[str, Any]) -> Dict[str, Any]:
decoded = dict(blob)
if not decoded.get("available", False):
decoded.setdefault("raw_data", b"")
return decoded
if decoded.get("encoding") != "base64":
raise ValueError(
f"Unsupported VGF neural statistics blob encoding: {decoded.get('encoding')}"
)
encoded_data = decoded.get("data", "")
try:
decoded["raw_data"] = base64.b64decode(encoded_data, validate=True)
except (binascii.Error, TypeError) as exc:
raise ValueError("Malformed base64 data in VGF neural statistics blob") from exc
return decoded
def parse_vgf_neural_statistics_metadata(
metadata: DelegateMetadataBytes,
) -> Dict[str, Any]:
payload = json.loads(_to_bytes(metadata).decode("utf-8"))
if payload.get("schema") != SCHEMA:
raise ValueError(f"Not VGF neural statistics metadata: {payload.get('schema')}")
if payload.get("schema_version") != SCHEMA_VERSION:
raise ValueError(
"Unsupported VGF neural statistics metadata schema version: "
f"{payload.get('schema_version')}"
)
payload = dict(payload)
decoded_segments = []
for segment in payload.get("segments", []):
decoded_segment = dict(segment)
for key in ("debug_database", "statistics_info", "statistics_memory"):
blob = decoded_segment.get(key)
if isinstance(blob, dict):
decoded_segment[key] = _decode_blob(blob)
decoded_segments.append(decoded_segment)
payload["segments"] = decoded_segments
return payload
def parse_vgf_neural_statistics_delegate_metadata(
delegate_metadata_list: Sequence[DelegateMetadataBytes],
) -> List[Dict[str, Any]]:
parsed: List[Dict[str, Any]] = []
for metadata in delegate_metadata_list:
if metadata is None or metadata == b"" or metadata == "":
continue
try:
metadata_bytes = _to_bytes(metadata)
except TypeError:
# Not a valid delegate metadata representation.
# Treat it as unrelated metadata from another source.
continue
try:
payload = json.loads(metadata_bytes.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
# If the blob appears to be VGF neural statistics metadata but is
# malformed, surface the error instead of silently dropping it.
if SCHEMA.encode("utf-8") in metadata_bytes:
raise ValueError(
"Malformed VGF neural statistics delegate metadata"
) from exc
# Otherwise this is generic delegate metadata from another backend.
continue
if not isinstance(payload, dict) or payload.get("schema") != SCHEMA:
# Inspector events can contain delegate metadata from other backends.
# Ignore only records that are clearly not VGF neural statistics.
continue
# From this point onward the record claims to be VGF neural statistics.
# Do not swallow parse errors: malformed VGF records should be visible.
parsed.append(parse_vgf_neural_statistics_metadata(metadata_bytes))
return parsed