|
| 1 | +import argparse |
| 2 | +import functools |
| 3 | +import numpy |
| 4 | +import unittest |
| 5 | +import os |
| 6 | + |
| 7 | +from result_analyzer import ResultAnalyzer, parse_args |
| 8 | + |
| 9 | +fns_whole = (numpy.min, numpy.median, numpy.max) |
| 10 | +fns_skip_head = (numpy.mean, numpy.std) |
| 11 | +fns_all = fns_whole + fns_skip_head |
| 12 | + |
| 13 | + |
| 14 | +def apply(fn, data): |
| 15 | + return fn(data) |
| 16 | + |
| 17 | + |
| 18 | +def apply_skip_head(fn, data): |
| 19 | + return fn(data[1:]) |
| 20 | + |
| 21 | + |
| 22 | +@functools.cache |
| 23 | +def get_dirname(): |
| 24 | + return os.path.dirname(__file__) |
| 25 | + |
| 26 | + |
| 27 | +@functools.cache |
| 28 | +def get_dataline(): |
| 29 | + import json |
| 30 | + example_json = os.path.join(get_dirname(), "example.json") |
| 31 | + with open(example_json, "r") as f: |
| 32 | + return json.load(f) |
| 33 | + |
| 34 | + |
| 35 | +class TestResultAnalyzer(unittest.TestCase): |
| 36 | + |
| 37 | + def _key(self, fn, metric): |
| 38 | + return f"{fn.__name__}_{metric}" |
| 39 | + |
| 40 | + def _check(self, dataline, output, fns, metric, output_value_fn): |
| 41 | + for fn in fns: |
| 42 | + key = self._key(fn, metric) |
| 43 | + self.assertIn(key, output) |
| 44 | + self.assertEqual(output[key], |
| 45 | + output_value_fn(fn, dataline["metrics"][metric])) |
| 46 | + |
| 47 | + def _test_calculate_metrics(self, xla, dynamo): |
| 48 | + dataline = get_dataline() |
| 49 | + dataline["experiment"]["xla"] = xla |
| 50 | + dataline["experiment"]["dynamo"] = dynamo |
| 51 | + |
| 52 | + r = ResultAnalyzer(parse_args(["--output-dirname", get_dirname()])) |
| 53 | + output = r.get_calculated_metrics({}, dataline) |
| 54 | + |
| 55 | + # Check that output has data for each metric, summarized by |
| 56 | + # each of its corresponding summary functions. |
| 57 | + |
| 58 | + # - metrics with more than one measurement |
| 59 | + for metric in ("total_cpu_time_s", "total_cuda_time_s", |
| 60 | + "per_iter_cpu_time_s", "per_iter_cuda_time_s", "total_time", |
| 61 | + "per_iter_time"): |
| 62 | + self._check(dataline, output, fns_whole, metric, apply) |
| 63 | + self._check(dataline, output, fns_skip_head, metric, apply_skip_head) |
| 64 | + |
| 65 | + # - single_value: since it has only one value, we only check it for |
| 66 | + # fns_whole set of statistical functions |
| 67 | + self._check(dataline, output, fns_whole, "single_value", apply) |
| 68 | + |
| 69 | + # Check that there are is no mean and std for single-valued timings. |
| 70 | + for fn in fns_skip_head: |
| 71 | + self.assertNotIn(self._key(fn, "single_value"), output) |
| 72 | + |
| 73 | + return output, dataline |
| 74 | + |
| 75 | + def test_calculate_metrics_inductor(self): |
| 76 | + output, _ = self._test_calculate_metrics(xla=None, dynamo="inductor") |
| 77 | + |
| 78 | + # There should be a dynamo_compile_time key, if it's not an XLA run. |
| 79 | + self.assertIn("dynamo_compile_time", output) |
| 80 | + |
| 81 | + # For all trace_per_iter_time summary data inside output, all of them |
| 82 | + # should be -1. |
| 83 | + for fn in fns_all: |
| 84 | + k = self._key(fn, "trace_per_iter_time") |
| 85 | + |
| 86 | + # It's ok not to have it in output, since it's not an XLA data anyway. |
| 87 | + if k in output: |
| 88 | + self.assertEqual(output[k], -1) |
| 89 | + |
| 90 | + def test_calculate_metrics_xla(self): |
| 91 | + output, dataline = self._test_calculate_metrics( |
| 92 | + xla="PJRT", dynamo="openxla") |
| 93 | + |
| 94 | + # There should be an xla_compile_time key. |
| 95 | + self.assertIn("xla_compile_time", output) |
| 96 | + |
| 97 | + # The trace_per_iter_time summary data should be populated. |
| 98 | + self._check(dataline, output, fns_whole, "trace_per_iter_time", apply) |
| 99 | + self._check(dataline, output, fns_skip_head, "trace_per_iter_time", |
| 100 | + apply_skip_head) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + unittest.main() |
0 commit comments