Skip to content
Snippets Groups Projects
Commit 04d867ce authored by Yifan Zhao's avatar Yifan Zhao
Browse files

Changed profiler API so we can just profile a few configs

parent f7bf857e
No related branches found
No related tags found
No related merge requests found
HPVM Profiler API HPVM Profiler API
====================== ======================
.. autofunction:: hpvm_profiler.profile_configs .. autofunction:: hpvm_profiler.profile_config_file
.. autofunction:: hpvm_profiler.plot_hpvm_configs .. autofunction:: hpvm_profiler.plot_hpvm_configs
...@@ -207,14 +207,14 @@ we obtained in the tuning step. ...@@ -207,14 +207,14 @@ we obtained in the tuning step.
.. code-block:: python .. code-block:: python
from hpvm_profiler import profile_configs, plot_hpvm_configs from hpvm_profiler import profile_config_file, plot_hpvm_configs
# Set `target_binary` to the path of the plain binary. # Set `target_binary` to the path of the plain binary.
target_binary = "./alexnet2_cifar10/build/alexnet2_cifar10" target_binary = "./alexnet2_cifar10/build/alexnet2_cifar10"
# Set `config_file` to the config file produced in tuning, such as "hpvm_confs.txt". # Set `config_file` to the config file produced in tuning, such as "hpvm_confs.txt".
config_file = "hpvm_confs.txt" config_file = "hpvm_confs.txt"
out_config_file = "hpvm_confs_profiled.txt" out_config_file = "hpvm_confs_profiled.txt"
profile_configs(target_binary, config_file, out_config_file) profile_config_file(target_binary, config_file, out_config_file)
plot_hpvm_configs(out_config_file, "configs_profiled.png") plot_hpvm_configs(out_config_file, "configs_profiled.png")
``hpvm_confs_profiled.txt`` contains the profiled configurations in HPVM format, ``hpvm_confs_profiled.txt`` contains the profiled configurations in HPVM format,
......
...@@ -4,16 +4,16 @@ from subprocess import PIPE, CalledProcessError ...@@ -4,16 +4,16 @@ from subprocess import PIPE, CalledProcessError
from typing import Iterable, List, Tuple, Union from typing import Iterable, List, Tuple, Union
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from tqdm import trange
PathLike = Union[Path, str] PathLike = Union[Path, str]
conf_opening, conf_closing = "+++++", "-----" conf_opening, conf_closing = "+++++", "-----"
def profile_configs( def profile_config_file(
binary_path: PathLike, binary_path: PathLike,
config_path: PathLike, config_path: PathLike,
output_config_path: PathLike, output_config_path: PathLike,
progress_bar: bool = True,
profile_filename: str = "profile_info.txt", profile_filename: str = "profile_info.txt",
qos_filename: str = "final_accuracy", qos_filename: str = "final_accuracy",
) -> None: ) -> None:
...@@ -33,39 +33,64 @@ def profile_configs( ...@@ -33,39 +33,64 @@ def profile_configs(
It contains a single float number as the QoS of this run. It contains a single float number as the QoS of this run.
This defaults to "final_accuracy" and should not be changed for HPVM binaries. This defaults to "final_accuracy" and should not be changed for HPVM binaries.
""" """
from subprocess import check_call
from tempfile import NamedTemporaryFile
# Read first line ("the float") and configs in config file # Read first line ("the float") and configs in config file
header, configs = read_hpvm_configs(Path(config_path)) header, configs = read_hpvm_configs(Path(config_path))
if not configs: if not configs:
raise ValueError("Config file with no configs is unsupported.") raise ValueError("Config file with no configs is unsupported.")
temp_file = NamedTemporaryFile("w") # Modifies configs in place.
baseline_time, baseline_acc = None, None profile_configs(
for idx in trange(len(configs), desc="Configs profiled"): binary_path, configs[1:], configs[0], progress_bar, profile_filename, qos_filename
config = configs[idx] )
# Write config to temp config file write_hpvm_configs(header, configs, Path(output_config_path))
write_hpvm_config(header, [config], Path(temp_file.name))
# Run binary_path binary,
# which generates `profile_filename` and `qos_filename` file in cwd. def profile_configs(
try: binary_path: PathLike,
check_call([str(binary_path), "-c", str(temp_file.name)]) configs: Iterable["Config"],
except CalledProcessError as e: baseline_config: "Config",
print("Output from the program:") progress_bar: bool = True,
print(e.output) profile_filename: str = "profile_info.txt",
raise e qos_filename: str = "final_accuracy",
# Read these two files for time and QoS info. ) -> None:
time = _read_profile_file(Path(profile_filename)) """Profile a sequence of HPVM configs.
acc = _read_qos_file(Path(qos_filename)) This function modifies argument `configs` in place."""
if idx == 0:
baseline_time, baseline_acc = time, acc from tqdm import tqdm
continue
assert baseline_time is not None and baseline_acc is not None baseline_time, baseline_acc = profile_config(binary_path, baseline_config)
iterable = tqdm(configs, desc="Configs profiled") if progress_bar else configs
for idx in iterable:
time, acc = profile_config(binary_path, config, profile_filename, qos_filename)
speedup = baseline_time / time speedup = baseline_time / time
config.update_profile_results(speedup, acc, baseline_acc) config.update_profile_results(speedup, acc, baseline_acc)
write_hpvm_config(header, configs, Path(output_config_path)) return configs
def measure_config(
binary_path: PathLike,
config: "Config",
profile_filename: str = "profile_info.txt",
qos_filename: str = "final_accuracy",
):
from subprocess import check_call
from tempfile import NamedTemporaryFile
import os
temp_file = NamedTemporaryFile("w")
write_hpvm_configs("0.0", [config], Path(temp_file.name))
# Run binary_path binary,
# which generates `profile_filename` and `qos_filename` file in cwd.
try:
with open(os.devnull, "w") as f:
check_call([str(binary_path), "-c", str(temp_file.name)], stdout=f)
except CalledProcessError as e:
print("Output from the program:")
print(e.output)
raise e
time = _read_profile_file(Path(profile_filename))
acc = _read_qos_file(Path(qos_filename))
temp_file.close() temp_file.close()
return time, acc
def plot_hpvm_configs( def plot_hpvm_configs(
...@@ -157,8 +182,7 @@ def read_hpvm_configs(config_file: PathLike) -> Tuple[str, List[Config]]: ...@@ -157,8 +182,7 @@ def read_hpvm_configs(config_file: PathLike) -> Tuple[str, List[Config]]:
return header, ret_configs return header, ret_configs
def write_hpvm_config(header: str, configs: Iterable[Config], to_file: PathLike): def write_hpvm_configs(header: str, configs: Iterable[Config], to_file: PathLike):
text_segs = [header] + [str(config) for config in configs] text_segs = [header] + [str(config) for config in configs]
with open(to_file, "w") as f: with open(to_file, "w") as f:
f.write("\n".join(text_segs)) f.write("\n".join(text_segs))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment