tboardfs 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tboardfs/__init__.py +13 -0
- tboardfs/array_json.py +12 -0
- tboardfs/classify.py +232 -0
- tboardfs/cli.py +55 -0
- tboardfs/constants.py +50 -0
- tboardfs/custom_scalars.py +27 -0
- tboardfs/decode.py +43 -0
- tboardfs/errors.py +7 -0
- tboardfs/extract.py +36 -0
- tboardfs/file_cli.py +26 -0
- tboardfs/file_cli_commands.py +124 -0
- tboardfs/file_tree.py +112 -0
- tboardfs/filesystem.py +820 -0
- tboardfs/hparams.py +80 -0
- tboardfs/indexer.py +263 -0
- tboardfs/mesh.py +102 -0
- tboardfs/model/__init__.py +21 -0
- tboardfs/model/binary_entry.py +30 -0
- tboardfs/model/event_file_cache.py +36 -0
- tboardfs/model/field.py +15 -0
- tboardfs/model/graph_entry.py +20 -0
- tboardfs/model/json_entry.py +20 -0
- tboardfs/model/record.py +19 -0
- tboardfs/model/run_cache.py +20 -0
- tboardfs/model/scalar.py +16 -0
- tboardfs/model/sidecar_entry.py +16 -0
- tboardfs/npy_export.py +10 -0
- tboardfs/paths.py +101 -0
- tboardfs/plugin_files.py +166 -0
- tboardfs/proto_schema.py +218 -0
- tboardfs/py.typed +0 -0
- tboardfs/summary.py +136 -0
- tboardfs/tables.py +322 -0
- tboardfs/tensor.py +91 -0
- tboardfs/tfrecord.py +42 -0
- tboardfs-0.2.0.dist-info/METADATA +161 -0
- tboardfs-0.2.0.dist-info/RECORD +40 -0
- tboardfs-0.2.0.dist-info/WHEEL +4 -0
- tboardfs-0.2.0.dist-info/entry_points.txt +4 -0
- tboardfs-0.2.0.dist-info/licenses/LICENSE +21 -0
tboardfs/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from tboardfs.cli import main
|
|
2
|
+
from tboardfs.constants import FIXED_TABS
|
|
3
|
+
from tboardfs.filesystem import TensorBoardFS
|
|
4
|
+
from tboardfs.indexer import parse_file
|
|
5
|
+
from tboardfs.paths import find_tensorboard_files
|
|
6
|
+
|
|
7
|
+
__all__ = (
|
|
8
|
+
"FIXED_TABS",
|
|
9
|
+
"TensorBoardFS",
|
|
10
|
+
"find_tensorboard_files",
|
|
11
|
+
"main",
|
|
12
|
+
"parse_file",
|
|
13
|
+
)
|
tboardfs/array_json.py
ADDED
tboardfs/classify.py
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import struct
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
import magic
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from tboardfs.constants import LARGE_TENSOR_THRESHOLD
|
|
8
|
+
from tboardfs.decode import _Decode
|
|
9
|
+
from tboardfs.model import Scalar
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def detect_extension(blob: bytes | None, kind: str) -> str:
|
|
13
|
+
"""Infer a stable file extension from encoded summary bytes."""
|
|
14
|
+
if not blob:
|
|
15
|
+
return "bin"
|
|
16
|
+
extension = _Classifier.extension_from_magic(blob)
|
|
17
|
+
if extension is not None:
|
|
18
|
+
return extension
|
|
19
|
+
extension = _Classifier.extension_from_signature(blob)
|
|
20
|
+
if extension is not None:
|
|
21
|
+
return extension
|
|
22
|
+
del kind
|
|
23
|
+
return "bin"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class _Classifier:
|
|
27
|
+
"""Stateless helpers for classifying parsed TensorBoard summary values."""
|
|
28
|
+
|
|
29
|
+
@staticmethod
|
|
30
|
+
def scalar_value(value: dict[str, Any]) -> Scalar | None:
|
|
31
|
+
"""Return a scalar value from a scalar-shaped summary record."""
|
|
32
|
+
if "simple_value" in value:
|
|
33
|
+
return Scalar(value["simple_value"], np.dtype("float32"))
|
|
34
|
+
|
|
35
|
+
tensor = value.get("tensor")
|
|
36
|
+
if not tensor:
|
|
37
|
+
return None
|
|
38
|
+
|
|
39
|
+
dtype = tensor.get("dtype")
|
|
40
|
+
values = tensor.get("values", {})
|
|
41
|
+
content = tensor.get("tensor_content")
|
|
42
|
+
if content:
|
|
43
|
+
scalar = _Classifier.tensor_content_scalar(dtype, content)
|
|
44
|
+
if scalar is not None:
|
|
45
|
+
return scalar
|
|
46
|
+
|
|
47
|
+
if dtype == 1 and values.get(5):
|
|
48
|
+
return Scalar(values[5][0], np.dtype("float32"))
|
|
49
|
+
if dtype == 2 and values.get(6):
|
|
50
|
+
return Scalar(values[6][0], np.dtype("float64"))
|
|
51
|
+
if dtype == 3 and values.get(7):
|
|
52
|
+
return Scalar(_Decode.signed_int32(values[7][0]), np.dtype("int32"))
|
|
53
|
+
if dtype == 9 and values.get(10):
|
|
54
|
+
return Scalar(_Decode.signed_int64(values[10][0]), np.dtype("int64"))
|
|
55
|
+
if dtype == 10 and values.get(11):
|
|
56
|
+
return Scalar(values[11][0], np.dtype("bool"))
|
|
57
|
+
if dtype == 22 and values.get(16):
|
|
58
|
+
return Scalar(values[16][0], np.dtype("uint32"))
|
|
59
|
+
if dtype == 23 and values.get(17):
|
|
60
|
+
return Scalar(values[17][0], np.dtype("uint64"))
|
|
61
|
+
return None
|
|
62
|
+
|
|
63
|
+
@staticmethod
|
|
64
|
+
def tensor_content_scalar(dtype: int | None, content: bytes) -> Scalar | None:
|
|
65
|
+
"""Return a scalar from fixed-width tensor content when supported."""
|
|
66
|
+
specs = {
|
|
67
|
+
1: ("<f", np.dtype("float32")),
|
|
68
|
+
2: ("<d", np.dtype("float64")),
|
|
69
|
+
3: ("<i", np.dtype("int32")),
|
|
70
|
+
9: ("<q", np.dtype("int64")),
|
|
71
|
+
10: ("<?", np.dtype("bool")),
|
|
72
|
+
22: ("<I", np.dtype("uint32")),
|
|
73
|
+
23: ("<Q", np.dtype("uint64")),
|
|
74
|
+
}
|
|
75
|
+
if dtype is None:
|
|
76
|
+
return None
|
|
77
|
+
spec = specs.get(dtype)
|
|
78
|
+
if spec is None:
|
|
79
|
+
return None
|
|
80
|
+
|
|
81
|
+
fmt, np_dtype = spec
|
|
82
|
+
if len(content) != struct.calcsize(fmt):
|
|
83
|
+
return None
|
|
84
|
+
return Scalar(struct.unpack(fmt, content)[0], np_dtype)
|
|
85
|
+
|
|
86
|
+
@staticmethod
|
|
87
|
+
def binary_kind(value: dict[str, Any]) -> str | None:
|
|
88
|
+
"""Classify summary values whose raw bytes should be exposed as files."""
|
|
89
|
+
image_blob = value.get("image", {}).get("encoded_image_string")
|
|
90
|
+
if image_blob and bytes(image_blob).startswith((b"GIF87a", b"GIF89a")):
|
|
91
|
+
return "videos"
|
|
92
|
+
if image_blob:
|
|
93
|
+
return "images"
|
|
94
|
+
if value.get("audio", {}).get("encoded_audio_string"):
|
|
95
|
+
return "audio"
|
|
96
|
+
|
|
97
|
+
plugin_name = (value.get("plugin_name") or "").lower()
|
|
98
|
+
if plugin_name in {"video", "videos"}:
|
|
99
|
+
return "videos"
|
|
100
|
+
|
|
101
|
+
tensor = value.get("tensor")
|
|
102
|
+
if tensor and _Classifier.large_tensor_like(tensor):
|
|
103
|
+
return "tensors"
|
|
104
|
+
return None
|
|
105
|
+
|
|
106
|
+
@staticmethod
|
|
107
|
+
def large_tensor_like(tensor: dict[str, Any]) -> bool:
|
|
108
|
+
"""Return true for tensor values large enough to expose as blobs."""
|
|
109
|
+
tensor_content = tensor.get("tensor_content")
|
|
110
|
+
if tensor_content and len(tensor_content) >= LARGE_TENSOR_THRESHOLD:
|
|
111
|
+
return True
|
|
112
|
+
return any(
|
|
113
|
+
len(item) >= LARGE_TENSOR_THRESHOLD for item in tensor.get("string_val", [])
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
@staticmethod
|
|
117
|
+
def binary_blob(value: dict[str, Any]) -> bytes | None:
|
|
118
|
+
"""Return raw payload bytes from a binary summary value."""
|
|
119
|
+
if value.get("image", {}).get("encoded_image_string"):
|
|
120
|
+
return bytes(value["image"]["encoded_image_string"])
|
|
121
|
+
if value.get("audio", {}).get("encoded_audio_string"):
|
|
122
|
+
return bytes(value["audio"]["encoded_audio_string"])
|
|
123
|
+
tensor = value.get("tensor") or {}
|
|
124
|
+
if tensor.get("tensor_content"):
|
|
125
|
+
return bytes(tensor["tensor_content"])
|
|
126
|
+
if tensor.get("string_val"):
|
|
127
|
+
return bytes(tensor["string_val"][0])
|
|
128
|
+
return None
|
|
129
|
+
|
|
130
|
+
@staticmethod
|
|
131
|
+
def plugin_json_payload(value: dict[str, Any]) -> dict[str, Any] | None:
|
|
132
|
+
"""Return JSON-safe plugin metadata for known TensorBoard plugin records."""
|
|
133
|
+
plugin_name = value.get("plugin_name")
|
|
134
|
+
if not plugin_name:
|
|
135
|
+
tensor = value.get("tensor") or {}
|
|
136
|
+
if tensor.get("dtype") == 7 and tensor.get("string_val"):
|
|
137
|
+
plugin_name = "text"
|
|
138
|
+
else:
|
|
139
|
+
return None
|
|
140
|
+
if str(plugin_name).lower() in {"video", "videos"} and _Classifier.binary_blob(
|
|
141
|
+
value
|
|
142
|
+
):
|
|
143
|
+
return None
|
|
144
|
+
payload = {"plugin_name": plugin_name, "tag": value.get("tag")}
|
|
145
|
+
if value.get("plugin_content"):
|
|
146
|
+
payload["plugin_content"] = value["plugin_content"]
|
|
147
|
+
tensor = value.get("tensor")
|
|
148
|
+
if tensor:
|
|
149
|
+
payload["tensor"] = tensor
|
|
150
|
+
return payload
|
|
151
|
+
|
|
152
|
+
@staticmethod
|
|
153
|
+
def json_tensor(tensor: dict[str, Any]) -> dict[str, Any]:
|
|
154
|
+
"""Return a JSON-friendly view of TensorProto fields."""
|
|
155
|
+
out = dict(tensor)
|
|
156
|
+
if "values" in out:
|
|
157
|
+
out["values"] = {str(key): value for key, value in out["values"].items()}
|
|
158
|
+
if "tensor_content" in out:
|
|
159
|
+
out["tensor_content"] = {"bytes": len(out["tensor_content"])}
|
|
160
|
+
if "string_val" in out:
|
|
161
|
+
out["string_val"] = [_Decode.text(value) for value in out["string_val"]]
|
|
162
|
+
return out
|
|
163
|
+
|
|
164
|
+
@staticmethod
|
|
165
|
+
def extension_from_magic(blob: bytes) -> str | None:
|
|
166
|
+
"""Return a native extension when libmagic recognizes the payload."""
|
|
167
|
+
mime = magic.from_buffer(blob, mime=True).lower()
|
|
168
|
+
description = magic.from_buffer(blob).lower()
|
|
169
|
+
mime_extensions = {
|
|
170
|
+
"application/json": "json",
|
|
171
|
+
"audio/flac": "flac",
|
|
172
|
+
"audio/mpeg": "mp3",
|
|
173
|
+
"audio/ogg": "ogg",
|
|
174
|
+
"audio/wav": "wav",
|
|
175
|
+
"audio/webm": "webm",
|
|
176
|
+
"audio/x-flac": "flac",
|
|
177
|
+
"audio/x-wav": "wav",
|
|
178
|
+
"image/gif": "gif",
|
|
179
|
+
"image/jpeg": "jpg",
|
|
180
|
+
"image/png": "png",
|
|
181
|
+
"image/webp": "webp",
|
|
182
|
+
"text/plain": "txt",
|
|
183
|
+
"video/mp4": "mp4",
|
|
184
|
+
"video/ogg": "ogg",
|
|
185
|
+
"video/quicktime": "mov",
|
|
186
|
+
"video/webm": "webm",
|
|
187
|
+
"video/x-msvideo": "avi",
|
|
188
|
+
}
|
|
189
|
+
if mime in mime_extensions:
|
|
190
|
+
return mime_extensions[mime]
|
|
191
|
+
return _Classifier.extension_from_magic_description(description)
|
|
192
|
+
|
|
193
|
+
@staticmethod
|
|
194
|
+
def extension_from_magic_description(description: str) -> str | None:
|
|
195
|
+
"""Map reliable libmagic descriptions when MIME is too generic."""
|
|
196
|
+
description_extensions = (
|
|
197
|
+
("json text", "json"),
|
|
198
|
+
("png image", "png"),
|
|
199
|
+
("jpeg image", "jpg"),
|
|
200
|
+
("gif image", "gif"),
|
|
201
|
+
("riff", "wav"),
|
|
202
|
+
("wave audio", "wav"),
|
|
203
|
+
("ogg data", "ogg"),
|
|
204
|
+
("web/p image", "webp"),
|
|
205
|
+
("webm", "webm"),
|
|
206
|
+
("matroska", "webm"),
|
|
207
|
+
("iso media", "mp4"),
|
|
208
|
+
("quicktime", "mov"),
|
|
209
|
+
("mpeg adts", "mp3"),
|
|
210
|
+
("flac audio", "flac"),
|
|
211
|
+
)
|
|
212
|
+
for needle, extension in description_extensions:
|
|
213
|
+
if needle in description:
|
|
214
|
+
return extension
|
|
215
|
+
return None
|
|
216
|
+
|
|
217
|
+
@staticmethod
|
|
218
|
+
def extension_from_signature(blob: bytes) -> str | None:
|
|
219
|
+
"""Recognize common encoded payloads when fixture bytes are very small."""
|
|
220
|
+
if blob.startswith(b"\x89PNG\r\n\x1a\n"):
|
|
221
|
+
return "png"
|
|
222
|
+
if blob.startswith(b"\xff\xd8\xff"):
|
|
223
|
+
return "jpg"
|
|
224
|
+
if blob.startswith(b"GIF87a") or blob.startswith(b"GIF89a"):
|
|
225
|
+
return "gif"
|
|
226
|
+
if blob.startswith(b"RIFF") and blob[8:12] == b"WAVE":
|
|
227
|
+
return "wav"
|
|
228
|
+
if blob.startswith(b"OggS"):
|
|
229
|
+
return "ogg"
|
|
230
|
+
if len(blob) >= 8 and blob[4:8] == b"ftyp":
|
|
231
|
+
return "mp4"
|
|
232
|
+
return None
|
tboardfs/cli.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
import sys
|
|
3
|
+
from typing import cast
|
|
4
|
+
|
|
5
|
+
import click
|
|
6
|
+
from loguru import logger
|
|
7
|
+
from mfusepy import FUSE, Operations
|
|
8
|
+
|
|
9
|
+
from tboardfs.filesystem import TensorBoardFS
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@click.command()
|
|
13
|
+
@click.argument("source", type=click.Path(exists=True, file_okay=False))
|
|
14
|
+
@click.argument("mountpoint", type=click.Path(file_okay=False))
|
|
15
|
+
@click.option("--step-digits", type=int, default=6, show_default=True)
|
|
16
|
+
@click.option("--refresh-age-seconds", type=float, default=60.0, show_default=True)
|
|
17
|
+
@click.option("--scalar-format", default="json,tsv,npz", show_default=True)
|
|
18
|
+
@click.option("--foreground/--background", default=True, show_default=True)
|
|
19
|
+
@click.option("--logfile", type=click.Path(dir_okay=False))
|
|
20
|
+
@click.option("--loglevel", default="INFO", show_default=True)
|
|
21
|
+
def main(
|
|
22
|
+
**params: object,
|
|
23
|
+
) -> None:
|
|
24
|
+
"""Mount TensorBoard logs as a read-only virtual filesystem."""
|
|
25
|
+
source_path = Path(cast(str, params["source"]))
|
|
26
|
+
mountpoint_path = Path(cast(str, params["mountpoint"]))
|
|
27
|
+
logfile = cast(str | None, params["logfile"])
|
|
28
|
+
logfile_path = Path(logfile) if logfile is not None else None
|
|
29
|
+
loglevel = cast(str, params["loglevel"])
|
|
30
|
+
foreground = cast(bool, params["foreground"])
|
|
31
|
+
_configure_logging(logfile_path, loglevel, foreground)
|
|
32
|
+
logger.info("mounting {} at {}", source_path, mountpoint_path)
|
|
33
|
+
|
|
34
|
+
class MountedTensorBoardFS(TensorBoardFS, Operations): # type: ignore[misc]
|
|
35
|
+
"""Concrete mfusepy operations class."""
|
|
36
|
+
|
|
37
|
+
filesystem = MountedTensorBoardFS(
|
|
38
|
+
source_path,
|
|
39
|
+
step_digits=cast(int, params["step_digits"]),
|
|
40
|
+
refresh_age_seconds=cast(float, params["refresh_age_seconds"]),
|
|
41
|
+
scalar_formats=cast(str, params["scalar_format"]),
|
|
42
|
+
log_to_screen=foreground,
|
|
43
|
+
)
|
|
44
|
+
FUSE(filesystem, str(mountpoint_path), foreground=foreground, ro=True)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _configure_logging(logfile: Path | None, loglevel: str, foreground: bool) -> None:
|
|
48
|
+
"""Configure loguru output for the CLI."""
|
|
49
|
+
logger.remove()
|
|
50
|
+
if foreground:
|
|
51
|
+
logger.add(
|
|
52
|
+
sys.stderr, level=loglevel.upper(), format="<level>{message}</level>"
|
|
53
|
+
)
|
|
54
|
+
if logfile is not None:
|
|
55
|
+
logger.add(logfile, level=loglevel.upper())
|
tboardfs/constants.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from types import MappingProxyType
|
|
3
|
+
|
|
4
|
+
TENSORBOARD_EVENT_GLOB = "events.out.tfevents*"
|
|
5
|
+
LARGE_TENSOR_THRESHOLD = 1024
|
|
6
|
+
SCALAR_COLUMNS = ("epoch", "step", "wall_time", "relative_time", "value")
|
|
7
|
+
DEFAULT_SCALAR_FORMATS = ("json", "tsv", "npz")
|
|
8
|
+
CONTROL_FILES = (".cache", ".in_memory")
|
|
9
|
+
|
|
10
|
+
FIXED_TABS = (
|
|
11
|
+
"scalars",
|
|
12
|
+
"custom_scalars",
|
|
13
|
+
"images",
|
|
14
|
+
"audio",
|
|
15
|
+
"videos",
|
|
16
|
+
"histograms",
|
|
17
|
+
"distributions",
|
|
18
|
+
"text",
|
|
19
|
+
"meshes",
|
|
20
|
+
"pr_curves",
|
|
21
|
+
"hparams",
|
|
22
|
+
"tensors",
|
|
23
|
+
"graphs",
|
|
24
|
+
"projector",
|
|
25
|
+
"profile",
|
|
26
|
+
"plugins",
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
PLUGIN_TAB = MappingProxyType(
|
|
30
|
+
{
|
|
31
|
+
"custom_scalars": "custom_scalars",
|
|
32
|
+
"histograms": "histograms",
|
|
33
|
+
"hparams": "hparams",
|
|
34
|
+
"hparams_keras": "hparams",
|
|
35
|
+
"mesh": "meshes",
|
|
36
|
+
"meshes": "meshes",
|
|
37
|
+
"pr_curve": "pr_curves",
|
|
38
|
+
"pr_curves": "pr_curves",
|
|
39
|
+
"profile": "profile",
|
|
40
|
+
"projector": "projector",
|
|
41
|
+
"scalars": "scalars",
|
|
42
|
+
"tensor": "tensors",
|
|
43
|
+
"tensors": "tensors",
|
|
44
|
+
"text": "text",
|
|
45
|
+
"video": "videos",
|
|
46
|
+
"videos": "videos",
|
|
47
|
+
}
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
SOURCE_PATH_TYPE = str | Path
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from typing import Any, cast
|
|
2
|
+
|
|
3
|
+
from google.protobuf.json_format import MessageToDict
|
|
4
|
+
from google.protobuf.message import DecodeError
|
|
5
|
+
from tensorboard.plugins.custom_scalar import layout_pb2
|
|
6
|
+
|
|
7
|
+
from tboardfs.model import JsonEntry
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def custom_scalar_layout_json(entries: list[JsonEntry]) -> dict[str, Any]:
|
|
11
|
+
"""Return the first TensorBoard custom scalar layout in JSON-safe form."""
|
|
12
|
+
for entry in entries:
|
|
13
|
+
tensor = entry.payload.get("tensor") or {}
|
|
14
|
+
blobs = tensor.get("string_val") or []
|
|
15
|
+
for blob in blobs:
|
|
16
|
+
if not isinstance(blob, bytes):
|
|
17
|
+
continue
|
|
18
|
+
layout = layout_pb2.Layout()
|
|
19
|
+
try:
|
|
20
|
+
layout.ParseFromString(blob)
|
|
21
|
+
except DecodeError:
|
|
22
|
+
continue
|
|
23
|
+
return cast(
|
|
24
|
+
dict[str, Any],
|
|
25
|
+
MessageToDict(layout, preserving_proto_field_name=True),
|
|
26
|
+
)
|
|
27
|
+
return {}
|
tboardfs/decode.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class _Decode:
|
|
5
|
+
"""Small decoding helpers for TensorBoard scalar and text fields."""
|
|
6
|
+
|
|
7
|
+
@staticmethod
|
|
8
|
+
def text(data: bytes) -> str:
|
|
9
|
+
"""Decode TensorBoard text bytes without failing on partial UTF-8."""
|
|
10
|
+
return data.decode("utf-8", errors="replace")
|
|
11
|
+
|
|
12
|
+
@staticmethod
|
|
13
|
+
def signed_int32(value: int) -> int:
|
|
14
|
+
"""Decode a protobuf int32 stored in unsigned two's-complement form."""
|
|
15
|
+
value &= 0xFFFFFFFF
|
|
16
|
+
if value >= 0x80000000:
|
|
17
|
+
return value - 0x100000000
|
|
18
|
+
return value
|
|
19
|
+
|
|
20
|
+
@staticmethod
|
|
21
|
+
def signed_int64(value: int) -> int:
|
|
22
|
+
"""Decode a protobuf int64 stored in unsigned two's-complement form."""
|
|
23
|
+
value &= 0xFFFFFFFFFFFFFFFF
|
|
24
|
+
if value >= 0x8000000000000000:
|
|
25
|
+
return value - 0x10000000000000000
|
|
26
|
+
return value
|
|
27
|
+
|
|
28
|
+
@staticmethod
|
|
29
|
+
def all_ints(values: list[object]) -> bool:
|
|
30
|
+
"""Return true when all values are plain Python integers."""
|
|
31
|
+
return all(
|
|
32
|
+
isinstance(value, int) and not isinstance(value, bool) for value in values
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
@staticmethod
|
|
36
|
+
def scalar_array_dtype(dtypes: list[np.dtype]) -> np.dtype:
|
|
37
|
+
"""Return the narrow scalar dtype when all rows agree."""
|
|
38
|
+
if not dtypes:
|
|
39
|
+
return np.dtype("float64")
|
|
40
|
+
first = dtypes[0]
|
|
41
|
+
if all(dtype == first for dtype in dtypes):
|
|
42
|
+
return first
|
|
43
|
+
return np.dtype("float64")
|
tboardfs/errors.py
ADDED
tboardfs/extract.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from errno import ENOENT
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from tboardfs.classify import _Classifier
|
|
5
|
+
from tboardfs.errors import fuse_error
|
|
6
|
+
from tboardfs.model import BinaryEntry, GraphEntry
|
|
7
|
+
from tboardfs.summary import parse_event
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def extract_binary_blob(entry: BinaryEntry) -> bytes:
|
|
11
|
+
"""Read one indexed binary blob from its source event record."""
|
|
12
|
+
with entry.source_path.open("rb") as handle:
|
|
13
|
+
handle.seek(entry.payload_offset)
|
|
14
|
+
payload = handle.read(entry.payload_size)
|
|
15
|
+
event = parse_event(payload)
|
|
16
|
+
for value in event.get("values", []):
|
|
17
|
+
if (
|
|
18
|
+
value.get("tag") == entry.tag
|
|
19
|
+
and _Classifier.binary_kind(value) == entry.kind
|
|
20
|
+
):
|
|
21
|
+
blob = _Classifier.binary_blob(value)
|
|
22
|
+
if blob is not None:
|
|
23
|
+
return blob
|
|
24
|
+
raise fuse_error(ENOENT)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _extract_graph_blob(entry: GraphEntry) -> bytes:
|
|
28
|
+
"""Read one indexed graph payload from its source event record."""
|
|
29
|
+
with entry.source_path.open("rb") as handle:
|
|
30
|
+
handle.seek(entry.payload_offset)
|
|
31
|
+
payload = handle.read(entry.payload_size)
|
|
32
|
+
event: dict[str, Any] = parse_event(payload)
|
|
33
|
+
graph = event.get("graph_def")
|
|
34
|
+
if graph is None:
|
|
35
|
+
raise fuse_error(ENOENT)
|
|
36
|
+
return bytes(graph)
|
tboardfs/file_cli.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
import click
|
|
5
|
+
from loguru import logger
|
|
6
|
+
|
|
7
|
+
from tboardfs.file_cli_commands import register_file_cli_commands
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@click.group()
|
|
11
|
+
@click.option("--logfile", type=click.Path(dir_okay=False))
|
|
12
|
+
@click.option("--loglevel", default="INFO", show_default=True)
|
|
13
|
+
def main(logfile: str | None, loglevel: str) -> None:
|
|
14
|
+
"""Inspect one TensorBoard event file without mounting FUSE."""
|
|
15
|
+
_configure_logging(Path(logfile) if logfile is not None else None, loglevel)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _configure_logging(logfile: Path | None, loglevel: str) -> None:
|
|
19
|
+
"""Configure loguru for non-data CLI output."""
|
|
20
|
+
logger.remove()
|
|
21
|
+
logger.add(sys.stderr, level=loglevel.upper(), format="<level>{message}</level>")
|
|
22
|
+
if logfile is not None:
|
|
23
|
+
logger.add(logfile, level=loglevel.upper())
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
register_file_cli_commands(main)
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from pathlib import PurePosixPath
|
|
3
|
+
import sys
|
|
4
|
+
from typing import cast
|
|
5
|
+
|
|
6
|
+
import click
|
|
7
|
+
from loguru import logger
|
|
8
|
+
|
|
9
|
+
from tboardfs.file_tree import SingleEventTree, _CopyConflictError
|
|
10
|
+
from tboardfs.paths import _Paths
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def register_file_cli_commands(group: click.Group) -> None:
|
|
14
|
+
"""Register single-event file commands on a Click group."""
|
|
15
|
+
|
|
16
|
+
@group.command("list")
|
|
17
|
+
@click.argument("source", type=click.Path(exists=True, dir_okay=False))
|
|
18
|
+
@click.argument("prefix", required=False, default="/")
|
|
19
|
+
@click.option("--step-digits", type=int, default=6, show_default=True)
|
|
20
|
+
@click.option("--scalar-format", default="json,tsv,npz", show_default=True)
|
|
21
|
+
def list_files(
|
|
22
|
+
source: str, prefix: str, step_digits: int, scalar_format: str
|
|
23
|
+
) -> None:
|
|
24
|
+
"""Print virtual file paths below PREFIX."""
|
|
25
|
+
tree = SingleEventTree(
|
|
26
|
+
source, step_digits=step_digits, scalar_format=scalar_format
|
|
27
|
+
)
|
|
28
|
+
for path in tree.list_file_paths(prefix=prefix):
|
|
29
|
+
sys.stdout.write(path + "\n")
|
|
30
|
+
|
|
31
|
+
@group.command("get")
|
|
32
|
+
@click.argument("source", type=click.Path(exists=True, dir_okay=False))
|
|
33
|
+
@click.argument("vpath")
|
|
34
|
+
@click.option(
|
|
35
|
+
"-o",
|
|
36
|
+
"--output",
|
|
37
|
+
required=True,
|
|
38
|
+
type=click.Path(writable=True),
|
|
39
|
+
)
|
|
40
|
+
@click.option("--force", is_flag=True)
|
|
41
|
+
@click.option("--step-digits", type=int, default=6, show_default=True)
|
|
42
|
+
@click.option("--scalar-format", default="json,tsv,npz", show_default=True)
|
|
43
|
+
def get_file(
|
|
44
|
+
source: str,
|
|
45
|
+
vpath: str,
|
|
46
|
+
output: str,
|
|
47
|
+
force: bool,
|
|
48
|
+
step_digits: int,
|
|
49
|
+
scalar_format: str,
|
|
50
|
+
) -> None:
|
|
51
|
+
"""Write one virtual file to OUTPUT."""
|
|
52
|
+
tree = SingleEventTree(
|
|
53
|
+
source, step_digits=step_digits, scalar_format=scalar_format
|
|
54
|
+
)
|
|
55
|
+
try:
|
|
56
|
+
data = tree.read_file(vpath)
|
|
57
|
+
except FileNotFoundError as error:
|
|
58
|
+
raise click.ClickException(f"virtual path not found: {vpath}") from error
|
|
59
|
+
except IsADirectoryError as error:
|
|
60
|
+
raise click.ClickException(
|
|
61
|
+
f"virtual path is a directory: {vpath}"
|
|
62
|
+
) from error
|
|
63
|
+
if output == "-":
|
|
64
|
+
sys.stdout.buffer.write(data)
|
|
65
|
+
return
|
|
66
|
+
output_path = _resolve_output_path(vpath, output)
|
|
67
|
+
if output_path.exists() and not force:
|
|
68
|
+
raise click.ClickException(
|
|
69
|
+
f"output already exists: {output_path}\n"
|
|
70
|
+
"Use --force for forced overwriting."
|
|
71
|
+
)
|
|
72
|
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
73
|
+
output_path.write_bytes(data)
|
|
74
|
+
logger.info("wrote {} to {}", _Paths.norm_virtual_path(vpath), output_path)
|
|
75
|
+
|
|
76
|
+
@group.command("copy-all")
|
|
77
|
+
@click.argument("source", type=click.Path(exists=True, dir_okay=False))
|
|
78
|
+
@click.argument("outdir", type=click.Path(file_okay=False))
|
|
79
|
+
@click.option("--force", is_flag=True)
|
|
80
|
+
@click.option("--skip-existing", is_flag=True)
|
|
81
|
+
@click.option("--step-digits", type=int, default=6, show_default=True)
|
|
82
|
+
@click.option("--scalar-format", default="json,tsv,npz", show_default=True)
|
|
83
|
+
def copy_all(**params: object) -> None:
|
|
84
|
+
"""Copy every virtual file into OUTDIR."""
|
|
85
|
+
force = cast(bool, params["force"])
|
|
86
|
+
skip_existing = cast(bool, params["skip_existing"])
|
|
87
|
+
if force and skip_existing:
|
|
88
|
+
raise click.ClickException(
|
|
89
|
+
"Use either --force or --skip-existing, not both."
|
|
90
|
+
)
|
|
91
|
+
existing = "overwrite" if force else "skip" if skip_existing else "fail"
|
|
92
|
+
tree = SingleEventTree(
|
|
93
|
+
cast(str, params["source"]),
|
|
94
|
+
step_digits=cast(int, params["step_digits"]),
|
|
95
|
+
scalar_format=cast(str, params["scalar_format"]),
|
|
96
|
+
)
|
|
97
|
+
try:
|
|
98
|
+
copied, skipped = tree.copy_all(
|
|
99
|
+
cast(str, params["outdir"]), existing=existing
|
|
100
|
+
)
|
|
101
|
+
except _CopyConflictError as error:
|
|
102
|
+
_report_copy_conflict(error)
|
|
103
|
+
raise click.exceptions.Exit(1) from error
|
|
104
|
+
for path in skipped:
|
|
105
|
+
logger.warning("Skipped existing: {}", path)
|
|
106
|
+
logger.info("copied {} files", copied)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _resolve_output_path(vpath: str, output: str) -> Path:
|
|
110
|
+
output_path = Path(output)
|
|
111
|
+
if output_path.exists() and output_path.is_dir():
|
|
112
|
+
return output_path / PurePosixPath(_Paths.norm_virtual_path(vpath)).name
|
|
113
|
+
if output.endswith("/"):
|
|
114
|
+
return output_path / PurePosixPath(_Paths.norm_virtual_path(vpath)).name
|
|
115
|
+
return output_path
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _report_copy_conflict(error: _CopyConflictError) -> None:
|
|
119
|
+
copied_count = len(error.copied_paths)
|
|
120
|
+
logger.info("Copied successfully: {} files", copied_count)
|
|
121
|
+
for path in error.copied_paths:
|
|
122
|
+
logger.info(path)
|
|
123
|
+
logger.error("Error: output already exists: {}", error.target)
|
|
124
|
+
logger.error("Use --force for forced overwriting.")
|