Skip to content

Commit f922b58

Browse files
vorsfacebook-github-bot
authored andcommitted
[bazel] GPU-support: add @local_config_cuda and @cuda (pytorch#63604)
Summary: ## Context We take the first step at tackling the GPU-bazel support by adding bazel external workspaces `local_config_cuda` and `cuda`, where the first one has some hardcoded values and lists of files, and the second one provides a nicer, high-level wrapper that maps into the already expected by pytorch bazel targets that are guarded with `if_cuda` macro. The prefix `local_config_` signifies the fact that we are breaking the bazel hermeticity philosophy by explicitly relaying on the CUDA installation that is present on the machine. ## Testing Notice an important scenario that is unlocked by this change: compilation of cpp code that depends on cuda libraries (i.e. cuda.h and so on). Before: ``` sergei.vorobev@cs-sv7xn77uoy-gpu-1628706590:~/src/pytorch4$ bazelisk build --define=cuda=true //:c10 ERROR: /home/sergei.vorobev/src/pytorch4/tools/config/BUILD:12:1: no such package 'tools/toolchain': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package. - /home/sergei.vorobev/src/pytorch4/tools/toolchain and referenced by '//tools/config:cuda_enabled_and_capable' ERROR: While resolving configuration keys for //:c10: Analysis failed ERROR: Analysis of target '//:c10' failed; build aborted: Analysis failed INFO: Elapsed time: 0.259s INFO: 0 processes. FAILED: Build did NOT complete successfully (2 packages loaded, 2 targets configured) ``` After: ``` sergei.vorobev@cs-sv7xn77uoy-gpu-1628706590:~/src/pytorch4$ bazelisk build --define=cuda=true //:c10 INFO: Analyzed target //:c10 (6 packages loaded, 246 targets configured). INFO: Found 1 target... Target //:c10 up-to-date: bazel-bin/libc10.lo bazel-bin/libc10.so INFO: Elapsed time: 0.617s, Critical Path: 0.04s INFO: 0 processes. INFO: Build completed successfully, 1 total action ``` The `//:c10` target is a good testing one for this, because it has such cases where the [glob is different](https://github.com/pytorch/pytorch/blob/075024b9a34904ec3ecdab3704c3bcaa329bdfea/BUILD.bazel#L76-L81), based on do we compile for CUDA or not. ## What is out of scope of this PR This PR is a first in a series of providing the comprehensive GPU bazel build support. Namely, we don't tackle the [cu_library](https://github.com/pytorch/pytorch/blob/11a40ad915d4d3d8551588e303204810887fcf8d/tools/rules/cu.bzl#L2) implementation here. This would be a separate large chunk of work. Pull Request resolved: pytorch#63604 Reviewed By: soulitzer Differential Revision: D30442083 Pulled By: malfet fbshipit-source-id: b2a8e4f7e5a25a69b960a82d9e36ba568eb64595
1 parent 22d38bd commit f922b58

File tree

12 files changed

+548
-5
lines changed

12 files changed

+548
-5
lines changed

.bazelrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ build --copt=-I.
33
build --copt=-isystem --copt bazel-out/k8-fastbuild/bin
44

55
# Configuration to disable tty features for environments like CI
6-
76
build:no-tty --curses no
87
build:no-tty --progress_report_interval 10
98
build:no-tty --show_progress_rate_limit 10
9+
10+
# Configuration to build with GPU support
11+
build:gpu --define=cuda=true
12+
# define a separate build folder for faster switching between configs
13+
build:gpu --platform_suffix=-gpu

.github/scripts/generate_ci_workflows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def generate_workflow_file(self, workflow_template: jinja2.Template) -> None:
497497
CIWorkflow(
498498
arch="linux",
499499
build_environment="linux-xenial-py3.6-gcc7-bazel-test",
500-
docker_image_base=f"{DOCKER_REGISTRY}/pytorch/pytorch-linux-xenial-py3.6-gcc7",
500+
docker_image_base=f"{DOCKER_REGISTRY}/pytorch/pytorch-linux-bionic-cuda10.2-cudnn7-py3.9-gcc7",
501501
test_runner_type=LINUX_CPU_TEST_RUNNER,
502502
on_pull_request=True,
503503
ciflow_config=CIFlowConfig(

.github/workflows/generated-linux-xenial-py3.6-gcc7-bazel-test.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.jenkins/pytorch/build.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,11 @@ if [[ "$BUILD_ENVIRONMENT" == *-bazel-* ]]; then
224224

225225
get_bazel
226226

227+
# first build the whole torch for CPU-only
227228
tools/bazel build --config=no-tty :torch
229+
# then build selected set of targets with GPU-support.
230+
# TODO: eventually this should converge to building the whole :torch with GPU-support
231+
tools/bazel build --config=no-tty --config=gpu :c10
228232
else
229233
# check that setup.py would fail with bad arguments
230234
echo "The next three invocations are expected to fail with invalid command error messages."

WORKSPACE

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
workspace(name = "pytorch")
22

33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4-
load("//tools/rules:workspace.bzl", "new_patched_local_repository")
4+
load("//tools/rules:workspace.bzl", "new_patched_local_repository", "new_empty_repository")
55

66
http_archive(
77
name = "bazel_skylib",
@@ -170,3 +170,14 @@ protobuf_deps()
170170
load("@rules_python//python:repositories.bzl", "py_repositories")
171171

172172
py_repositories()
173+
174+
local_repository(
175+
name = "local_config_cuda",
176+
path = "third_party/tensorflow_cuda_bazel_build",
177+
)
178+
179+
# Wrapper to expose local_config_cuda in an agnostic way
180+
new_empty_repository(
181+
name = "cuda",
182+
build_file = "//third_party:cuda.BUILD",
183+
)

third_party/cuda.BUILD

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Collect all the CUDA stuff from @local_config_cuda in a single target
3+
for convenience.
4+
"""
5+
6+
cc_library(
7+
name = "cuda",
8+
visibility = ["//visibility:public"],
9+
deps = [
10+
"@local_config_cuda//cuda:cublas",
11+
"@local_config_cuda//cuda:cuda_driver",
12+
"@local_config_cuda//cuda:cuda_headers",
13+
"@local_config_cuda//cuda:cudart",
14+
"@local_config_cuda//cuda:cufft",
15+
"@local_config_cuda//cuda:curand",
16+
],
17+
)
18+
19+
cc_library(
20+
name = "cupti",
21+
deps = [
22+
"@local_config_cuda//cuda:cupti_headers",
23+
"@local_config_cuda//cuda:cupti_link",
24+
],
25+
)
26+
27+
[
28+
alias(
29+
name = lib,
30+
actual = "@local_config_cuda//cuda:{}".format(lib),
31+
visibility = ["//visibility:public"],
32+
)
33+
for lib in [
34+
"cublas",
35+
"cufft",
36+
"cusolver",
37+
"cusparse",
38+
"curand",
39+
"nvrtc",
40+
"cuda_driver",
41+
"nvToolsExt",
42+
]
43+
]

third_party/tensorflow_cuda_bazel_build/BUILD

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Config for CUDA
2+
3+
This is a checked-in copy of the auto-generated config for building CUDA code with bazel. The content of this folder was generated from https://github.com/tensorflow/tensorflow `./configure` execution and then edited manually to fit the pytorch needs.
4+
5+
The LICENSE for the TensorFlow project is APACHE 2. The full LICENSE file could be found here https://github.com/tensorflow/tensorflow/blob/master/LICENSE.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "local_config_cuda")

0 commit comments

Comments
 (0)