Repository navigation
Expand file tree
/
Copy pathbaseline.py
More file actions
64 lines (53 loc) · 2.03 KB
/
Copy pathbaseline.py
File metadata and controls
64 lines (53 loc) · 2.03 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
"""Baseline execution time measurement for MLIR benchmarks.
This script measures the execution time of MLIR code without any transformations.
It executes the evaluation codes and updates the execution times (in nanoseconds)
in the current eval json file.
Execution Command:
```bash
CONFIG_FILE_PATH=path/to/config.json poetry run baseline
```
"""
from dotenv import load_dotenv
load_dotenv(override=True)
load_dotenv('.env.debug')
from mlir_rl_artifact.execution import Execution
from mlir_rl_artifact.utils.config import Config
from mlir._mlir_libs._mlir.ir import Module, Context # type: ignore
from tqdm import tqdm
import json
import os
def main():
cfg = Config()
exec = Execution("")
if not cfg.eval_json_file:
raise ValueError("Evaluation json file is not provided in the config file")
with open(cfg.eval_json_file) as file:
benchmarks_json: dict[str, int] = json.load(file)
data_tqdm = tqdm(benchmarks_json, unit='file')
for bench in data_tqdm:
data_tqdm.set_postfix_str(bench)
full_path = os.path.join(cfg.benchmarks_folder_path, bench + ".mlir")
with open(full_path, 'r') as f:
code = f.read()
with Context():
module = Module.parse(code)
try:
et, _, _ = exec.execute_code(module, bench, [])
except Exception as e:
print(f"Failed to execute {bench}: {e}")
et = -1
benchmarks_json[bench] = et
try:
with open(cfg.eval_json_file + '.tmp', 'w') as f:
json.dump(benchmarks_json, f, indent=2)
f.flush()
os.fsync(f.fileno())
os.replace(cfg.eval_json_file + '.tmp', cfg.eval_json_file)
except Exception as e:
print(f"Failed to update {cfg.eval_json_file}: {e}")
finally:
if os.path.exists(cfg.eval_json_file + '.tmp'):
os.remove(cfg.eval_json_file + '.tmp')
print(f"Execution times updated in {cfg.eval_json_file}")
if __name__ == "__main__":
main()