rl/log_reader: use NumPy view in _add_feature - #572
Conversation
LogReaderTensorValue already exposes a zero-copy NumPy view of the underlying tensor buffer. Extend the protobuf repeated fields from that view instead of iterating through the ctypes-backed sequence. Serialized output is unchanged, and the sequence-example conversion test now asserts byte-for-byte equality. Test: compiler_opt/rl/log_reader_test.py
|
"Benchmarks on representative synthetic workloads measured approximately a 15% improvement in read_log_as_sequence_examples" what are the representative synthetic workloads? |
|
The benchmark used synthetic logs in the same "simple log format" that Measurement: |
Can you contribute the benchmark? |
Add a standalone benchmark for read_log_as_sequence_examples that generates a synthetic log in the simple log format, with a float32 and an int64 feature tensor per observation plus a float32 score tensor, mirroring the feature mix of the inlining and regalloc problem configs. It measures the current _add_feature against a reference copy of the previous implementation in interleaved order, reports median, mean, p95 and a 95% confidence interval, and checks that the serialized output is byte-for-byte identical.
|
Yes, added It generates a synthetic log in the same simple log format (JSON header + raw tensor buffers, float32 and int64 feature tensors per observation, float32 score tensor), measures Usage, e.g.: The default parameters are close to the workload the ~15% figure was measured on; the size is configurable so it can be scaled down for quick checks. It is a standalone script (not collected by pytest) and does not add a CI dependency. |
mtrofin
left a comment
There was a problem hiding this comment.
can you also clarify if you used AI for this patch, and either way, simplify the commit message. It doesn't need all the flowery headings, which just pull on the attention of a reader. It can just say it makes (which) performance improvement and adds a benchmark demonstrating it, plus the numbers. I'd be surprised if you need more than 2 sentences.
|
|
||
| from compiler_opt.rl import log_reader | ||
|
|
||
| flags.DEFINE_integer("observations", 8, "Number of observations to log.") |
There was a problem hiding this comment.
can you use the _OBSERVATIONS = flags.DEFINE_integer(... style see e.g. compiler_opt/tools/generate_default_trace.py
There was a problem hiding this comment.
Done - switched to the _OBSERVATIONS = flags.DEFINE_integer(...) style as in compiler_opt/tools/generate_default_trace.py (commit 1d5db98).
| FLAGS = flags.FLAGS | ||
|
|
||
|
|
||
| def _write_log(fname: str, observations: int, elems: int) -> None: |
There was a problem hiding this comment.
elem_count or num_elem, otherwise it reads like these are the actual elements
| lst.extend(value) | ||
|
|
||
|
|
||
| def _time_parse(fname: str, add_feature) -> tuple[float, dict[str, tf.train.SequenceExample]]: |
There was a problem hiding this comment.
would timeit be more canonical?
There was a problem hiding this comment.
Done - the interleaved measurements now use timeit (commit 1d5db98).
| return elapsed, result | ||
|
|
||
|
|
||
| def _stats(samples: list[float]) -> tuple[float, float, float, tuple[float, float]]: |
There was a problem hiding this comment.
the repo already depends on scipy, you can use that for e.g. p95, etc.
There was a problem hiding this comment.
Done - stats now use numpy/scipy (np.percentile for p95, scipy.stats.sem + t.interval for the 95% CI) in commit 1d5db98.
| machine-dependent; results are reported as median, mean, p95 and a 95% | ||
| confidence interval. | ||
| """ | ||
|
|
There was a problem hiding this comment.
can you add a usage example as a comment - if the default flag values are fine, perfect (i.e. the goal is to get a reader confident in how to use this "as intended". They can figure out later if they want to adjust flags)
There was a problem hiding this comment.
Done - added a usage example to the module docstring; the default flag values are the intended workload (commit 1d5db98).
Use the module-level _FLAG = flags.DEFINE_* style, rename elems to elem_count, time with timeit, compute stats with numpy/scipy, and document a usage example.
|
Thanks for the review. Regarding AI: this patch was developed with the help of an LLM coding assistant (similar to Cursor); the change itself is small and was reviewed and validated before submission. I've also simplified the message as suggested - it's now two sentences: the change makes All benchmark comments are addressed in commit 1d5db98 (flag style, |
|
see the license header error, otherwise lgtm. |
Use the zero-copy NumPy view exposed by LogReaderTensorValue when populating protobuf repeated fields in
_add_feature, makingread_log_as_sequence_examples~15% faster (~13-17% across runs, medians, disjoint 95% CIs) on representative synthetic workloads, with byte-for-byte identical serialized output. Addscompiler_opt/rl/log_reader_benchmark.pyto reproduce the measurement.