MLCompilerBridge
Tools for streamlining communication with ML models for compiler optimizations.
Loading...
Searching...
No Matches
log_reader.py
Go to the documentation of this file.
1# ------------------------------------------------------------------------------
2#
3# Part of the MLCompilerBridge Project, under the Apache License v2.0 with LLVM
4# Exceptions. See the LICENSE file for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#
7# ------------------------------------------------------------------------------
8
13
14import ctypes
15import dataclasses
16import io
17import json
18import sys
19from typing import List, Optional
20from functools import reduce
21import operator
22
23_element_types = {
24 "float": ctypes.c_float,
25 "double": ctypes.c_double,
26 "int8_t": ctypes.c_int8,
27 "uint8_t": ctypes.c_uint8,
28 "int16_t": ctypes.c_int16,
29 "uint16_t": ctypes.c_uint16,
30 "int32_t": ctypes.c_int32,
31 "uint32_t": ctypes.c_uint32,
32 "int64_t": ctypes.c_int64,
33 "uint64_t": ctypes.c_uint64,
34}
35
36
37@dataclasses.dataclass(frozen=True)
39 name: str
40 port: int
41 shape: List[int]
42 element_type: type
43
44 @staticmethod
45 def from_dict(d: dict):
46 name = d["name"]
47 port = d["port"]
48 shape = [int(e) for e in d["shape"]]
49 element_type_str = d["type"]
50 if element_type_str not in _element_types:
51 raise ValueError(f"uknown type: {element_type_str}")
52 return TensorSpec(
53 name=name,
54 port=port,
55 shape=shape,
56 element_type=_element_types[element_type_str],
57 )
58
59
61 def __init__(self, spec: TensorSpec, buffer: bytes):
62 self._spec = spec
63 self._buffer = buffer
64 self._view = ctypes.cast(self._buffer, ctypes.POINTER(self._spec.element_type))
65 # self._len = math.prod(self._spec.shape)
66 self._len = reduce(operator.mul, self._spec.shape, 1)
67 # self._view = numpy.frombuffer(self._buffer, float)
68 # print("Value of", self._spec.name, "is:", self._view)
69
70 def spec(self) -> TensorSpec:
71 return self._spec
72
73 def __len__(self) -> int:
74 return self._len
75
76 def __getitem__(self, index):
77 if index < 0 or index >= self._len:
78 raise IndexError(f"Index {index} out of range [0..{self._len})")
79 return self._view[index]
80
81
82def read_tensor(fs: io.BufferedReader, ts: TensorSpec) -> TensorValue:
83 size = reduce(operator.mul, ts.shape, 1) * ctypes.sizeof(ts.element_type)
84 # size = math.prod(ts.shape) * ctypes.sizeof(ts.element_type)
85 data = fs.read(size)
86 return TensorValue(ts, data)
87
88
89def pretty_print_tensor_value(tv: TensorValue):
90 print(f'{tv.spec().name}: {",".join([str(v) for v in tv])}')
91
92
93def read_header(f: io.BufferedReader):
94 line = f.readline()
95 header = json.loads(line)
96 tensor_specs = [TensorSpec.from_dict(ts) for ts in header["features"]]
97 score_spec = TensorSpec.from_dict(header["score"]) if "score" in header else None
98 advice_spec = TensorSpec.from_dict(header["advice"]) if "advice" in header else None
99 return tensor_specs, score_spec, advice_spec
100
101
103 context: Optional[str],
104 event_str: str,
105 f: io.BufferedReader,
106 tensor_specs: List[TensorSpec],
107 score_spec: Optional[TensorSpec],
108):
109 features = []
110 for ts in tensor_specs:
111 features.append(read_tensor(f, ts))
112 f.readline()
113 return context, None, features, None
114
115
116def read_stream(fname: str):
117 with io.BufferedReader(io.FileIO(fname, "rb")) as f:
118 tensor_specs, score_spec, _ = read_header(f)
119 context = None
120 while True:
121 event_str = f.readline()
122 if not event_str:
123 break
124 context, observation_id, features, score = read_one_observation(
125 context, event_str, f, tensor_specs, score_spec
126 )
127 yield context, observation_id, features, score
128
129
130def read_stream2(f: io.BufferedReader):
131 context = None
132 while True:
133 tensor_specs, score_spec, _ = read_header(f)
134 # event_str = f.readline()
135 # print("Event: ", event_str)
136 # if not event_str:
137 # break
138 context, observation_id, features, score = read_one_observation(
139 context, "", f, tensor_specs, score_spec
140 )
141 yield context, observation_id, features, score
142
143
144def main(args):
145 last_context = None
146 for ctx, obs_id, features, score in read_stream(args[1]):
147 if last_context != ctx:
148 print(f"context: {ctx}")
149 last_context = ctx
150 print(f"observation: {obs_id}")
151 for fv in features:
153 if score:
155
156
157if __name__ == "__main__":
158 main(sys.argv)
__init__(self, TensorSpec spec, bytes buffer)
Definition log_reader.py:61
TensorValue read_tensor(io.BufferedReader fs, TensorSpec ts)
Definition log_reader.py:82
read_stream2(io.BufferedReader f)
read_one_observation(Optional[str] context, str event_str, io.BufferedReader f, List[TensorSpec] tensor_specs, Optional[TensorSpec] score_spec)
pretty_print_tensor_value(TensorValue tv)
Definition log_reader.py:89
read_header(io.BufferedReader f)
Definition log_reader.py:93