Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial remote hermetic cuda toolchain #72

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/build-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ jobs:
- run: bazelisk build @rules_cuda_examples//rdc:all
- run: bazelisk build @rules_cuda_examples//if_cuda:main
- run: bazelisk build @rules_cuda_examples//if_cuda:main --enable_cuda=False

- run: cd examples && bazelisk build //basic:all --config=bzlmod
- run: cd examples && bazelisk build //rdc:all --config=bzlmod
- run: cd examples && bazelisk build //if_cuda:main --config=bzlmod
Expand Down
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module(
version = "0.0.0",
)


bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "platforms", version = "0.0.6")

Expand Down
28 changes: 27 additions & 1 deletion cuda/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ local_cuda = repository_rule(
configure = True,
local = True,
environ = ["CUDA_PATH", "PATH", "CUDA_CLANG_PATH", "BAZEL_LLVM"],
# remotable = True,
)

def rules_cuda_dependencies(toolkit_path = None):
Expand Down Expand Up @@ -232,4 +231,31 @@ def rules_cuda_dependencies(toolkit_path = None):
],
)

maybe(
name = "libcudacxx",
repo_rule = http_archive,
build_file = "@rules_cuda//third_party:libcu++.BUILD",
sha256 = "05b1435ad65f5bdef1bb8d1eb29dc8f0f7df34c01d77bf8686687a4603588bce",
strip_prefix = "libcudacxx-1.9.0",
urls = ["https://github.com/NVIDIA/libcudacxx/archive/refs/tags/1.9.0.tar.gz"],
)

maybe(
name = "thrust",
repo_rule = http_archive,
build_file = "@rules_cuda//third_party:thrust.BUILD",
sha256 = "d021e37f5aac30fd1b9737865399feb57db8e601ae2fc0af3cd41784435e9523",
strip_prefix = "thrust-1.17.2",
urls = ["https://github.com/NVIDIA/thrust/archive/refs/tags/1.17.2.tar.gz"],
)

maybe(
name = "cub",
repo_rule = http_archive,
build_file = "@rules_cuda//third_party:cub.BUILD",
sha256 = "1013a595794548c359f22c07e1f8c620b97e3a577f7e8496d9407f74566a3e2a",
strip_prefix = "cub-1.17.2",
urls = ["https://github.com/NVIDIA/cub/archive/refs/tags/1.17.2.tar.gz"],
)

local_cuda(name = "local_cuda", toolkit_path = toolkit_path)
53 changes: 52 additions & 1 deletion cuda/private/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,43 @@ cuda_toolchain = rule(
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
},
)

CPP_TOOLCHAIN_TYPE = "@bazel_tools//tools/cpp:toolchain_type"
CUDA_TOOLCHAIN_TYPE = "//cuda:toolchain_type"

def _remote_cuda_impl(repository_ctx):
redist = repository_ctx.read(Label(repository_ctx.attr.json_path))
repos = json.decode(redist)
repos_to_define = dict()
base_url = repository_ctx.attr.base_url

for key in repos:
if key == "release_date":
continue
for arch in repos[key]:
if arch == "name" or arch == "license" or arch == "version":
continue
repos_to_define[key + "-%s" % arch] = {
"repo_rule": "http_archive",
"name": key + "-%s" % arch,
"sha256": repos[key][arch]["sha256"],
"url": base_url + repos[key][arch]["relative_path"],
}

repo_defs = "\n".join([" maybe(name = \"{}\", build_file = \"@rules_cuda//cuda:templates/BUILD.remote_nvcc\", sha256 = \"{}\", repo_rule = {}, urls = [\"{}\"], strip_prefix=\"{}\")\n".format(repos_to_define[repo_name]["name"], repos_to_define[repo_name]["sha256"], repos_to_define[repo_name]["repo_rule"], repos_to_define[repo_name]["url"], repos_to_define[repo_name]["url"].split("/")[-1][:-7]) for repo_name in repos_to_define])

repository_ctx.template("repositories.bzl", Label("//cuda:templates/BUILD.repo_template"), substitutions = {"%{repos}": repo_defs}, executable = False)

repository_ctx.symlink(Label("//cuda:runtime/BUILD.remote_cuda"), "BUILD.bazel")
repository_ctx.symlink(Label("//cuda:templates/BUILD.remote_toolchain_nvcc"), "toolchain/BUILD.bazel")

_remote_cuda = repository_rule(
implementation = _remote_cuda_impl,
attrs = {
"base_url": attr.string(default = "https://developer.download.nvidia.com/compute/cuda/redist/"),
"json_path": attr.string(mandatory = True),
},
)

# buildifier: disable=unused-variable
def use_cpp_toolchain(mandatory = True):
"""Helper to depend on the C++ toolchain.
Expand Down Expand Up @@ -77,6 +110,24 @@ def find_cuda_toolkit(ctx):
"""
return ctx.toolchains[CUDA_TOOLCHAIN_TYPE].cuda_toolkit[CudaToolkitInfo]

CUDA_VERSIONS_JSON = {
"11.4.4": "//cuda:redistrib/redistrib_11.4.4.json",
"11.5.2": "//cuda:redistrib/redistrib_11.5.2.json",
"11.6.2": "//cuda:redistrib/redistrib_11.6.2.json",
"11.7.1": "//cuda:redistrib/redistrib_11.7.1.json",
"11.8.0": "//cuda:redistrib/redistrib_11.8.0.json",
"12.0.0": "//cuda:redistrib/redistrib_12.0.0.json",
"12.0.1": "//cuda:redistrib/redistrib_12.0.1.json",
"12.1.0": "//cuda:redistrib/redistrib_12.1.0.json",
}

def register_cuda_toolchains(name = "remote_cuda_toolchain", version = "12.0.0", cuda_versions = CUDA_VERSIONS_JSON):
_remote_cuda(name = name, json_path = cuda_versions[version])

native.register_toolchains(
"@%s//toolchain:nvcc-local-toolchain" % name,
)

# buildifier: disable=unnamed-macro
def register_detected_cuda_toolchains():
"""Helper to register the automatically detected CUDA toolchain(s).
Expand Down
Empty file added cuda/redistrib/BUILD.bazel
Empty file.
Loading
Loading