Skip to content

Commit 447f5b5

Browse files
vorspytorchmergebot
authored andcommitted
[bazel] enable sccache+nvcc in CI (pytorch#95528)
Fixes pytorch#79348 This change is mostly focused on enabling nvcc+sccache in the PyTorch CI. Along the way we had to do couple tweaks: 1. Split the rules_cc from the rules_cuda that embeeded them before. This is needed in order to apply a different patch to the rules_cc compare to the one that rules_cuda does by default. This is in turn needed because we need to workaround an nvcc behavior where it doesn't send `-iquote xxx` to the host compiler, but it does send `-isystem xxx`. So we workaround this problem with (ab)using `-isystem` instead. Without it we are getting errors like `xxx` is not found. 2. Workaround bug in bazel bazelbuild/bazel#10167 that prevents us from using a straightforward and honest `nvcc` sccache wrapper. Instead we generate ad-hock bazel specific nvcc wrapper that has internal knowledge of the relative bazel paths to local_cuda. This allows us to workaround the issue with CUDA symlinks. Without it we are getting `undeclared inclusion(s) in rule` all over the place for CUDA headers. ## Test plan Green CI build https://github.com/pytorch/pytorch/actions/runs/4267147180/jobs/7428431740 Note that now it says "CUDA" in the sccache output ``` + sccache --show-stats Compile requests 9784 Compile requests executed 6726 Cache hits 6200 Cache hits (C/C++) 6131 Cache hits (CUDA) 69 Cache misses 519 Cache misses (C/C++) 201 Cache misses (CUDA) 318 Cache timeouts 0 Cache read errors 0 Forced recaches 0 Cache write errors 0 Compilation failures 0 Cache errors 7 Cache errors (C/C++) 7 Non-cacheable compilations 0 Non-cacheable calls 2893 Non-compilation calls 165 Unsupported compiler calls 0 Average cache write 0.116 s Average cache read miss 23.722 s Average cache read hit 0.057 s Failed distributed compilations 0 ``` Pull Request resolved: pytorch#95528 Approved by: https://github.com/huydhn
1 parent 49ba119 commit 447f5b5

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

.ci/pytorch/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ if [[ "$BUILD_ENVIRONMENT" == *-bazel-* ]]; then
191191
set -e
192192

193193
get_bazel
194+
install_sccache_nvcc_for_bazel
194195

195196
# Leave 1 CPU free and use only up to 80% of memory to reduce the change of crashing
196197
# the runner

.ci/pytorch/common_utils.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ function get_bazel() {
9595
chmod +x tools/bazel
9696
}
9797

98+
# This function is bazel specific because of the bug
99+
# in the bazel that requires some special paths massaging
100+
# as a workaround. See
101+
# https://github.com/bazelbuild/bazel/issues/10167
102+
function install_sccache_nvcc_for_bazel() {
103+
sudo mv /usr/local/cuda/bin/nvcc /usr/local/cuda/bin/nvcc-real
104+
105+
# Write the `/usr/local/cuda/bin/nvcc`
106+
cat << EOF | sudo tee /usr/local/cuda/bin/nvcc
107+
#!/bin/sh
108+
if [ \$(env -u LD_PRELOAD ps -p \$PPID -o comm=) != sccache ]; then
109+
exec sccache /usr/local/cuda/bin/nvcc "\$@"
110+
else
111+
exec external/local_cuda/cuda/bin/nvcc-real "\$@"
112+
fi
113+
EOF
114+
115+
sudo chmod +x /usr/local/cuda/bin/nvcc
116+
}
117+
98118
function install_monkeytype {
99119
# Install MonkeyType
100120
pip_install MonkeyType

.lintrunner.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ include_patterns = ['**']
367367
exclude_patterns = [
368368
'**/contrib/**',
369369
'**/*.diff',
370+
'**/*.patch',
370371
'third_party/**',
371372
'aten/src/ATen/native/vulkan/api/vk_mem_alloc.h',
372373
'test/cpp/jit/upgrader_models/*.ptl',

WORKSPACE

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ workspace(name = "pytorch")
33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44
load("//tools/rules:workspace.bzl", "new_patched_local_repository")
55

6+
http_archive(
7+
name = "rules_cc",
8+
strip_prefix = "rules_cc-40548a2974f1aea06215272d9c2b47a14a24e556",
9+
patches = [
10+
"//:tools/rules_cc/cuda_support.patch",
11+
],
12+
urls = [
13+
"https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/40548a2974f1aea06215272d9c2b47a14a24e556.tar.gz",
14+
"https://github.com/bazelbuild/rules_cc/archive/40548a2974f1aea06215272d9c2b47a14a24e556.tar.gz",
15+
],
16+
)
17+
618
http_archive(
719
name = "rules_cuda",
820
strip_prefix = "runtime-b1c7cce21ba4661c17ac72421c6a0e2015e7bef3/third_party/rules_cuda",
@@ -11,7 +23,7 @@ http_archive(
1123

1224
load("@rules_cuda//cuda:dependencies.bzl", "rules_cuda_dependencies")
1325

14-
rules_cuda_dependencies()
26+
rules_cuda_dependencies(with_rules_cc = False)
1527

1628
load("@rules_cc//cc:repositories.bzl", "rules_cc_toolchains")
1729

tools/rules_cc/cuda_support.patch

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
diff --git cc/private/toolchain/unix_cc_configure.bzl cc/private/toolchain/unix_cc_configure.bzl
2+
index ba992fc..e4e8364 100644
3+
--- cc/private/toolchain/unix_cc_configure.bzl
4+
+++ cc/private/toolchain/unix_cc_configure.bzl
5+
@@ -27,6 +27,7 @@ load(
6+
"which",
7+
"write_builtin_include_directory_paths",
8+
)
9+
+load("@rules_cuda//cuda:toolchain.bzl", "cuda_compiler_deps")
10+
11+
def _field(name, value):
12+
"""Returns properly indented top level crosstool field."""
13+
@@ -397,7 +398,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
14+
cxx_opts = split_escaped(get_env_var(
15+
repository_ctx,
16+
"BAZEL_CXXOPTS",
17+
- "-std=c++0x",
18+
+ "-std=c++11",
19+
False,
20+
), ":")
21+
22+
@@ -463,7 +464,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
23+
)),
24+
"%{cc_compiler_deps}": get_starlark_list([":builtin_include_directory_paths"] + (
25+
[":cc_wrapper"] if darwin else []
26+
- )),
27+
+ ) + cuda_compiler_deps()),
28+
"%{cc_toolchain_identifier}": cc_toolchain_identifier,
29+
"%{compile_flags}": get_starlark_list(
30+
[
31+
diff --git cc/private/toolchain/unix_cc_toolchain_config.bzl cc/private/toolchain/unix_cc_toolchain_config.bzl
32+
index c3cf3ba..1744eb4 100644
33+
--- cc/private/toolchain/unix_cc_toolchain_config.bzl
34+
+++ cc/private/toolchain/unix_cc_toolchain_config.bzl
35+
@@ -25,6 +25,7 @@ load(
36+
"variable_with_value",
37+
"with_feature_set",
38+
)
39+
+load("@rules_cuda//cuda:toolchain.bzl", "cuda_toolchain_config")
40+
41+
all_compile_actions = [
42+
ACTION_NAMES.c_compile,
43+
@@ -580,7 +581,8 @@ def _impl(ctx):
44+
],
45+
flag_groups = [
46+
flag_group(
47+
- flags = ["-iquote", "%{quote_include_paths}"],
48+
+ # -isystem because there is an nvcc thing where it doesn't forward -iquote to host compiler.
49+
+ flags = ["-isystem", "%{quote_include_paths}"],
50+
iterate_over = "quote_include_paths",
51+
),
52+
flag_group(
53+
@@ -1152,10 +1154,15 @@ def _impl(ctx):
54+
unfiltered_compile_flags_feature,
55+
]
56+
57+
+ cuda = cuda_toolchain_config(
58+
+ cuda_toolchain_info = ctx.attr._cuda_toolchain_info,
59+
+ compiler_path = ctx.attr.tool_paths["gcc"],
60+
+ )
61+
+
62+
return cc_common.create_cc_toolchain_config_info(
63+
ctx = ctx,
64+
- features = features,
65+
- action_configs = action_configs,
66+
+ features = features + cuda.features,
67+
+ action_configs = action_configs + cuda.action_configs,
68+
cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories,
69+
toolchain_identifier = ctx.attr.toolchain_identifier,
70+
host_system_name = ctx.attr.host_system_name,
71+
@@ -1192,6 +1199,9 @@ cc_toolchain_config = rule(
72+
"tool_paths": attr.string_dict(),
73+
"toolchain_identifier": attr.string(mandatory = True),
74+
"unfiltered_compile_flags": attr.string_list(),
75+
+ "_cuda_toolchain_info": attr.label(
76+
+ default = Label("@rules_cuda//cuda:cuda_toolchain_info"),
77+
+ ),
78+
},
79+
provides = [CcToolchainConfigInfo],
80+
)

0 commit comments

Comments
 (0)