Unverified Commit da593617 authored by Gaétan Lepage's avatar Gaétan Lepage Committed by GitHub
Browse files

python3Packages.torch: fetch upstream patch to allow gcc 14 with cuda 12.9 (#504339)

parents 40e43b49 17be5665
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
  lib,
  fetchFromGitHub,
  fetchFromGitLab,
  fetchpatch,
  git-unroll,
  buildPythonPackage,
  python,
@@ -304,6 +305,18 @@ buildPythonPackage.override { inherit stdenv; } (finalAttrs: {

  patches = [
    ./clang19-template-warning.patch

    # The GCC version upperbounds were wrong for cuda 12.8 and 12.9, which led downstream builds to
    # illegitimately fail with:
    #   RuntimeError: The current installed version of g++ (14.3.0) is greater than the maximum
    #   required version by CUDA 12.9. Please make sure to use an adequate version of g++
    #   (>=6.0.0, <14.0).
    # TODO: remove at the next release
    (fetchpatch {
      name = "allow-gcc-14-with-cuda-12.8-9";
      url = "https://github.com/pytorch/pytorch/commit/39565a7dcf8f93ea22cedeaa20088b24ff6d2634.patch";
      hash = "sha256-Au5fVbs7i33d9c4Xj8koiBP7lGnsTGTaX4VlE2gAfy8=";
    })
  ]
  ++ lib.optionals cudaSupport [
    ./fix-cmake-cuda-toolkit.patch
@@ -741,7 +754,9 @@ buildPythonPackage.override { inherit stdenv; } (finalAttrs: {
    blasProvider = blas.provider;
    # To help debug when a package is broken due to CUDA support
    inherit brokenConditions;
    tests = callPackage ../tests { };
    tests = callPackage ../tests {
      inherit rocmSupport cudaSupport;
    };
  };

  meta = {
+48 −23
Original line number Diff line number Diff line
{ callPackage }:
{
  lib,
  stdenv,
  callPackage,
  torchvision,
  torchcodec,
  torchaudio,

rec {
  rocmSupport,
  cudaSupport,
}:

let
  cudaOnly = {
    # To perform the runtime check use either
    # `nix run .#python3Packages.torch.tests.tester-cudaAvailable` (outside the sandbox), or
    # `nix build .#python3Packages.torch.tests.tester-cudaAvailable.gpuCheck` (in a relaxed sandbox)
@@ -9,25 +20,39 @@ rec {
      versionAttr = "cuda";
      libraries = ps: [ ps.torchWithCuda ];
    };
  tester-rocmAvailable = callPackage ./mk-runtime-check.nix {
    feature = "rocm";
    versionAttr = "hip";
    libraries = ps: [ ps.torchWithRocm ];
  };

  compileCpu = tester-compileCpu.gpuCheck;
  tester-compileCpu = callPackage ./mk-torch-compile-check.nix {
    feature = null;
    libraries = ps: [ ps.torch ];
  };
    tester-compileCuda = callPackage ./mk-torch-compile-check.nix {
      feature = "cuda";
      libraries = ps: [ ps.torchWithCuda ];
    };
  };

  rocmOnly = {
    tester-rocmAvailable = callPackage ./mk-runtime-check.nix {
      feature = "rocm";
      versionAttr = "hip";
      libraries = ps: [ ps.torchWithRocm ];
    };
    tester-compileRocm = callPackage ./mk-torch-compile-check.nix {
      feature = "rocm";
      libraries = ps: [ ps.torchWithRocm ];
    };
  };
in
let
  tester-compileCpu = callPackage ./mk-torch-compile-check.nix {
    feature = null;
    libraries = ps: [ ps.torch ];
  };
in
{
  inherit tester-compileCpu;
  compileCpu = tester-compileCpu.gpuCheck;

  mnist-example = callPackage ./mnist-example { };

  # Core packages from the torch ecosystem
  inherit torchvision torchaudio torchcodec;
}
// lib.optionalAttrs cudaSupport cudaOnly
// lib.optionalAttrs rocmSupport rocmOnly
+14 −12
Original line number Diff line number Diff line
{
  lib,
  stdenv,
  buildPythonPackage,
  fetchFromGitHub,

@@ -53,18 +54,6 @@ buildPythonPackage.override { inherit (torch) stdenv; } (finalAttrs: {
    TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CUDA = 1;
    TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MULTIGPU_CUDA = 1;

    # At of 2026-03-27, the default `cudaPackages` version is 12.9.1
    # According to Nvidia, it should support GCC versions up to 14.x:
    # -> https://docs.nvidia.com/cuda/archive/12.9.1/cuda-installation-guide-linux/index.html#host-compiler-support-policy
    # However, PyTorch's *strict* upper bound is 14.0:
    # -> https://github.com/pytorch/pytorch/blob/v2.11.0/torch/utils/cpp_extension.py#L75
    # Hence, the build fails with:
    #   RuntimeError: The current installed version of g++ (14.3.0) is greater than the maximum
    #   required version by CUDA 12.9. Please make sure to use an adequate version of g++
    #   (>=6.0.0, <14.0).
    # Hence, we disable the version check to silence the error:
    TORCH_DONT_CHECK_COMPILER_ABI = cudaSupport;

    # ROCM
    USE_ROCM = rocmSupport;
    PYTORCH_ROCM_ARCH = lib.optionalString rocmSupport torch.gpuTargetString;
@@ -80,6 +69,11 @@ buildPythonPackage.override { inherit (torch) stdenv; } (finalAttrs: {
    #   RuntimeError: Test is known to fail for Python 3.10, disabling for now
    #   See: https://github.com/pytorch/audio/pull/2224#issuecomment-1048329450
    TORCHAUDIO_TEST_ALLOW_SKIP_IF_ON_PYTHON_310 = true;

    # Fails on aarch64-linux with:
    #   RuntimeError: `fbgemm` is not available
    TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_QUANTIZATION =
      stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isAarch64;
  };

  build-system = [
@@ -128,9 +122,17 @@ buildPythonPackage.override { inherit (torch) stdenv; } (finalAttrs: {

    # Very long to run
    "AutogradCPUTest"
  ]
  ++ lib.optionals (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isAarch64) [
    # AssertionError: Tensor-likes are not close!
    "test_batch_inverse_spectrogram"
    "test_batch_pitch_shift"
    "test_batch_spectrogram"
    "test_griffinlim_0_99"
  ];

  passthru.gpuCheck = torchaudio.overridePythonAttrs (old: {
    pname = "${finalAttrs.pname}-gpuCheck";
    requiredSystemFeatures = [ "cuda" ];

    env = (old.env or { }) // {
+0 −12
Original line number Diff line number Diff line
@@ -63,18 +63,6 @@ buildPythonPackage.override { inherit (torch) stdenv; } (finalAttrs: {
    TORCHVISION_LIBRARY = "${libjpeg_turbo}/lib/";
  }
  // lib.optionalAttrs cudaSupport {
    # At of 2026-03-27, the default `cudaPackages` version is 12.9.1
    # According to Nvidia, it should support GCC versions up to 14.x:
    # -> https://docs.nvidia.com/cuda/archive/12.9.1/cuda-installation-guide-linux/index.html#host-compiler-support-policy
    # However, PyTorch's *strict* upper bound is 14.0:
    # -> https://github.com/pytorch/pytorch/blob/v2.11.0/torch/utils/cpp_extension.py#L75
    # Hence, the build fails with:
    #   RuntimeError: The current installed version of g++ (14.3.0) is greater than the maximum
    #   required version by CUDA 12.9. Please make sure to use an adequate version of g++
    #   (>=6.0.0, <14.0).
    # Hence, we disable the version check to silence the error:
    TORCH_DONT_CHECK_COMPILER_ABI = 1;

    TORCH_CUDA_ARCH_LIST = "${lib.concatStringsSep ";" cudaCapabilities}";
    FORCE_CUDA = 1;
  };