Commit 32340fc3 authored by Jiacheng Huang's avatar Jiacheng Huang Committed by wooway777
Browse files

issue/925 - Speed up `scripts/build_ntops.py` and...

issue/925 - Speed up `scripts/build_ntops.py` and `src/infiniop/ninetoothed/build.py` with `concurrent.futures`
parent 55cd22e3
import concurrent.futures
import importlib
import pathlib
......@@ -11,16 +12,27 @@ SRC_DIR_PATH = CURRENT_FILE_PATH.parent.parent / "src"
def _find_and_build_ops():
ops_path = SRC_DIR_PATH / "infiniop" / "ops"
for op_dir in ops_path.iterdir():
ninetoothed_path = op_dir / "ninetoothed"
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = []
if ninetoothed_path.is_dir():
module_path = ninetoothed_path / "build"
relative_path = module_path.relative_to(SRC_DIR_PATH)
import_name = ".".join(relative_path.parts)
module = importlib.import_module(import_name)
for op_dir in ops_path.iterdir():
ninetoothed_path = op_dir / "ninetoothed"
module.build()
if not ninetoothed_path.is_dir():
continue
futures.append(executor.submit(_build, ninetoothed_path))
concurrent.futures.as_completed(futures)
def _build(ninetoothed_path):
module_path = ninetoothed_path / "build"
relative_path = module_path.relative_to(SRC_DIR_PATH)
import_name = ".".join(relative_path.parts)
module = importlib.import_module(import_name)
module.build()
if __name__ == "__main__":
......
import concurrent.futures
import functools
import inspect
import itertools
......@@ -16,40 +17,28 @@ BUILD_DIRECTORY_PATH = (
def build(premake, constexpr_param_grid, caller, op_name, output_dir):
headers = []
all_param_names = []
combinations = []
launches = []
for combination in _generate_param_value_combinations(constexpr_param_grid):
arrangement, application, tensors = premake(**combination)
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = []
for param_name, param_value in combination.items():
if isinstance(param_value, str):
combination[param_name] = (
f"INFINI_DTYPE_{combination[param_name].replace('fp', 'F').upper()}"
)
for combination in tuple(
_generate_param_value_combinations(constexpr_param_grid)
):
future = executor.submit(
_make, premake, combination, caller, op_name, output_dir
)
combination = {f"{name}_": value for name, value in combination.items()}
futures.append(future)
kernel_name = f"{op_name}_{_generate_suffix(combination.values())}"
for future in concurrent.futures.as_completed(futures):
header, param_names, combination, launch = future.result()
ninetoothed.make(
arrangement,
application,
tensors,
caller=caller,
kernel_name=kernel_name,
output_dir=output_dir,
)
header = output_dir / f"{kernel_name}.h"
param_names = ("stream",) + tuple(
inspect.signature(application).parameters.keys()
)
launch = f""" if ({_generate_condition(combination)})
return launch_{kernel_name}({", ".join(param_names)});"""
headers.append(header)
all_param_names.append(param_names)
launches.append(launch)
headers.append(header)
all_param_names.append(param_names)
combinations.append(combination)
launches.append(launch)
includes = "\n".join(f'#include "{header}"' for header in headers)
......@@ -64,7 +53,7 @@ def build(premake, constexpr_param_grid, caller, op_name, output_dir):
"NineToothedStream",
] + ["NineToothedTensor" for _ in range(len(param_names) - 1)]
for param_name in combination:
for param_name in functools.reduce(lambda x, y: x | y, combinations, {}):
param_names.append(param_name)
param_types.append("int")
......@@ -97,6 +86,36 @@ def build(premake, constexpr_param_grid, caller, op_name, output_dir):
(BUILD_DIRECTORY_PATH / header_file_name).write_text(header_content)
def _make(premake, combination, caller, op_name, output_dir):
arrangement, application, tensors = premake(**combination)
for param_name, param_value in combination.items():
if isinstance(param_value, str):
combination[param_name] = (
f"INFINI_DTYPE_{combination[param_name].replace('fp', 'F').upper()}"
)
combination = {f"{name}_": value for name, value in combination.items()}
kernel_name = f"{op_name}_{_generate_suffix(combination.values())}"
ninetoothed.make(
arrangement,
application,
tensors,
caller=caller,
kernel_name=kernel_name,
output_dir=output_dir,
)
header = output_dir / f"{kernel_name}.h"
param_names = ("stream",) + tuple(inspect.signature(application).parameters.keys())
launch = f""" if ({_generate_condition(combination)})
return launch_{kernel_name}({", ".join(param_names)});"""
return header, param_names, combination, launch
def _generate_condition(combination):
return " && ".join(f"{param} == {value}" for param, value in combination.items())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment