Skip to content

Multi-versioning fails for CUDA toolkit #477

Description

@gkouretas-maven

For the autogenerated NVCC toolkit BUILD stubs, there is inconsistency between the declared and actual version of the toolkit components. This is due to the highest version being declared for the toolkit info, while all the other variables are aliases that get selected as a function of version.

I do see that this was done intentionally, as mentioned in this snippet here:
cuda/extensions.bzl:

    for _, toolkit in registrations.items():
        if components_mapping != None:
            # Always use the maximum version so the toolkit includes all components.
            # Components that don't exist in older versions will fall back to dummy.
            toolkit_version = sorted(redist_versions, key = _version_sort_key)[-1]
            cuda_toolkit(name = toolkit.name, components_mapping = components_mapping, version = toolkit_version)
        else:
            cuda_toolkit(**_module_tag_to_dict(toolkit))

However, issues arise when using a 12.X and 13.X version due to false positives being set for certain compiler flags. In particular, I was seeing a failure when using CUDA 12.X with version due to this random seed feature flag in particular (which, as stated in the comment, fails on CUDA <= 12.9):

cuda/private/toolchain_configs/nvcc.bzl:

    # NOTE: this only works on compiler newer than 12.9
    nvcc_fixed_random_seed_feature = feature(
        name = "nvcc_fixed_random_seed",
        enabled = True,
        flag_sets = [
            flag_set(
                actions = [
                    ACTION_NAMES.cuda_compile,
                    ACTION_NAMES.device_link,
                ],
                flag_groups = [flag_group(flags = ["--frandom-seed=%{output_file}"])],
            ),
        ] if nvcc_version_ge(ctx, 12, 9) else [],
    )

A minimal setup to see the issue is to declare a 12.X and 13.X distribution, and attempt to build with the 12.X version:

MODULE.bazel:

bazel_dep(name = "rules_cuda")

git_override(
    module_name = "rules_cuda",
    commit = "ccd93bdfb002172e570993846db16fb6b8670136",  # < This is the commit I've been working off as I said - the issue should still exist on main from what I've seen
    remote = "https://github.com/bazel-contrib/rules_cuda.git",
)

cuda = use_extension("@rules_cuda//cuda:extensions.bzl", "toolchain")
cuda.redist_json(
    name = "cuda_12_6_0",
    platforms = [
        "linux-aarch64",
        "linux-sbsa",
        "linux-x86_64",
    ],
    version = "12.6.0",
)
cuda.redist_json(
    name = "cuda_13_0_1",
    platforms = [
        "linux-sbsa",
        "linux-x86_64",
    ],
    version = "13.0.1",
)
cuda.toolkit(
    name = "cuda",
)
use_repo(cuda, "cuda")

BUILD.bazel:

load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_cuda//cuda:defs.bzl", "cuda_library")

write_file(
    name = "dummy",
    out = "dummy.cu",
    content = ["#include <cuda_runtime.h>"],
)

cuda_library(
    name = "dummy_lib",
    srcs = [":dummy"],
    deps = ["@cuda//:cuda_runtime"],
)

Build command:

bazel build --@rules_cuda//cuda:enable=true --@rules_cuda//cuda:version=12.6.0 //:dummy_lib

Error:

INFO: Analyzed target //:dummy_lib (84 packages loaded, 5855 targets configured).
ERROR: /<>/BUILD.bazel:55:13: Compiling bazel-out/aarch64-fastbuild/bin/dummy.cu failed: (Exit 1): nvcc failed: error executing CudaCompile command (from cuda_library rule target //:dummy_lib) external/rules_cuda++toolchain+cuda_nvcc_linux_sbsa_12_6_0/nvcc/bin/nvcc -x cu -Xcompiler -fPIC -ccbin /usr/bin/clang -I . -I bazel-out/aarch64-fastbuild/bin -I ... (remaining 37 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
nvcc fatal   : Unknown option '--frandom-seed=bazel-out/aarch64-fastbuild/bin/_objs/dummy_lib/dummy.pic.o'
Target //:dummy_lib failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 5.339s, Critical Path: 0.08s
INFO: 2 processes: 3 action cache hit, 2 internal.
ERROR: Build did NOT complete successfully

The natural fix would be to add the version as a select statement as done for the other versions. As constructed, this does technically need to be declared in the toolchain BUILD file itself since the version attribute(s) accepts a string or integer depending on the rule. The attached patch addresses the issue (based off commit ccd93bdfb002172e570993846db16fb6b8670136): 0000_toolchain_build_version_select.patch

Note

This patch was AI generated so there's a bit of slop / unnecessary stuff, but I think it mostly illustrates the high level changes that are required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions