Skip to content

Commit 227ff2e

Browse files
authored
[ET-VK][ez] Add a setting to disable multithreading when compiler shaders during build
Differential Revision: D89497641 Pull Request resolved: pytorch#16325
1 parent 5d07311 commit 227ff2e

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

backends/vulkan/cmake/ShaderLibrary.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ function(gen_vulkan_shader_lib_cpp shaders_path)
5353
endif()
5454
endif()
5555

56+
# Add nthreads argument for shader compilation
57+
if(DEFINED EXECUTORCH_VULKAN_SHADER_COMPILE_NTHREADS)
58+
list(APPEND GEN_SPV_ARGS "--nthreads"
59+
"${EXECUTORCH_VULKAN_SHADER_COMPILE_NTHREADS}"
60+
)
61+
endif()
62+
5663
add_custom_command(
5764
COMMENT "Generating Vulkan Compute Shaders"
5865
OUTPUT ${VULKAN_SHADERGEN_OUT_PATH}/spv.cpp

backends/vulkan/runtime/gen_vulkan_spv.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ def generateSPV( # noqa: C901
892892
output_dir: str,
893893
cache_dir: Optional[str] = None,
894894
force_rebuild: bool = False,
895+
nthreads: int = -1,
895896
) -> Dict[str, str]:
896897
# The key of this dictionary is the full path to a generated source file. The
897898
# value is a tuple that contains 3 entries:
@@ -1118,11 +1119,21 @@ def compile_spirv(shader_paths_pair) -> Tuple[str, str]:
11181119
gen_file_meta[gen_out_path] = (file_changed, include_list)
11191120

11201121
# Parallelize SPIR-V compilation to optimize build time
1121-
with ThreadPool(os.cpu_count()) as pool:
1122-
for spv_out_path, glsl_out_path in pool.map(
1123-
compile_spirv, self.output_file_map.items()
1124-
):
1122+
# Determine number of threads: -1 means use all CPU cores, 1 means sequential
1123+
num_processes = os.cpu_count() if nthreads == -1 else nthreads
1124+
1125+
if num_processes == 1:
1126+
# Sequential compilation (single-threaded)
1127+
for shader_pair in self.output_file_map.items():
1128+
spv_out_path, glsl_out_path = compile_spirv(shader_pair)
11251129
spv_to_glsl_map[spv_out_path] = glsl_out_path
1130+
else:
1131+
# Parallel compilation
1132+
with ThreadPool(num_processes) as pool:
1133+
for spv_out_path, glsl_out_path in pool.map(
1134+
compile_spirv, self.output_file_map.items()
1135+
):
1136+
spv_to_glsl_map[spv_out_path] = glsl_out_path
11261137

11271138
return spv_to_glsl_map
11281139

@@ -1443,6 +1454,12 @@ def main(argv: List[str]) -> int:
14431454
parser.add_argument(
14441455
"--env", metavar="KEY=VALUE", nargs="*", help="Set a number of key-value pairs"
14451456
)
1457+
parser.add_argument(
1458+
"--nthreads",
1459+
type=int,
1460+
default=-1,
1461+
help="Number of threads for shader compilation. -1 (default) uses all available CPU cores, 1 uses sequential compilation.",
1462+
)
14461463
options = parser.parse_args()
14471464

14481465
env = DEFAULT_ENV
@@ -1477,7 +1494,10 @@ def main(argv: List[str]) -> int:
14771494
replace_u16vecn=options.replace_u16vecn,
14781495
)
14791496
output_spv_files = shader_generator.generateSPV(
1480-
options.output_path, options.tmp_dir_path, options.force_rebuild
1497+
options.output_path,
1498+
options.tmp_dir_path,
1499+
options.force_rebuild,
1500+
options.nthreads,
14811501
)
14821502

14831503
genCppFiles(

backends/vulkan/targets.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,15 @@ def vulkan_spv_shader_lib(name, spv_filegroups, is_fbcode = False, no_volk = Fal
9393
for target, subpath in spv_filegroups.items():
9494
glsl_paths.append("$(location {})/{}".format(target, subpath))
9595

96+
nthreads = read_config("etvk", "shader_compile_nthreads", "-1")
97+
9698
genrule_cmd = (
9799
"$(exe {}) ".format(gen_vulkan_spv_target) +
98100
"--glsl-paths {} ".format(" ".join(glsl_paths)) +
99101
"--output-path $OUT " +
100102
"--glslc-path=$(exe {}) ".format(glslc_path) +
101103
"--tmp-dir-path=shader_cache " +
104+
"--nthreads {} ".format(nthreads) +
102105
("-f " if read_config("etvk", "force_shader_rebuild", "0") == "1" else " ") +
103106
select({
104107
"DEFAULT": "",

0 commit comments

Comments
 (0)