How to prevent pytorch from building from source

I’m trying to install this python project that is dependent on stable-ts which is dependent on pytorch. Trying to build my attempt at packaging the software results in my poor laptop i9 being unable to keep up with building pytorch. While I could limit the resources available to the rebuild, I suspect there’s a better way using cachix to avoid this problem altogether.

I’ve had a similar issue here.

Currently I can install pytorch to my system but if I recall it took about 40m to build and I didn’t have a lot of faith in it when it was happening.

Trying to add the cachix cache to home manager with:

  nix = {
    package = pkgs.nix;
    settings =
      substituters = ["https://cache.nixos.org"];
      trusted-public-keys = [ "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" ];
    };
  };

results in the warning:

warning: ignoring the client-specified setting 'trusted-public-keys', because it is a restricted setting and you are not a trusted user

dotfiles are on codeberg

Is there any way to install pytorch within home manager that’s light on system resources?

Any help would be greatly appreciated!

python3Packages.pytorch succesfully substitutes for me.

So you probably have enabled cuda support, and in that case you might have some luck by adding the nix-community cachix.

I do have cuda enabled but for pytorch and ctranslate2 only, I’ve found only those two packages compiled with cuda support are neccesary for openai-whisper to be gpu accelerated.

Looks like nix-community did significantly reduce the number of packages I have to build, the only two that still want to build from source are triton and onnxruntime. Is there no available cache for these two? Triton was one of the main reasons that the build was freezing in the first place.

For now, I’ve limited the resources available to the build and I expect to build fine but is there any way to avoid needing to build triton on my computer?

If its not available in an existing cache, create your own. You will have to build it somewhere though to be able to push the result to the cache.

Makes sense, thank you

It finished building (took just over an hour) but now I’m having issues with setuptools_scm[toml]>=8 which is a dependency of SubPlz.

My current config is:

{
  pkgs,
  system,
  lib,
  ...
}: let
  buildPythonPackage = pkgs.python311Packages.buildPythonPackage;

  stable-ts = buildPythonPackage rec {
    pname = "stable-ts";
    version = "2.19.0";
    format = "setuptools";

    src = pkgs.fetchFromGitHub {
      owner = "jianfch";
      repo = "stable-ts";
      rev = "97b4bcd20df50ac79aee18b2d3ab4ceda753734f";
      hash = "sha256-EGkGct1l/PR3TO2/aa9OT1BPjhq2MJe9Gdq1gPSt+RA=";
    };

    doCheck = false;

    propagatedBuildInputs = with pkgs.python311Packages; [
      numpy
      # Might need not the bin
      torch
      torchaudio
      tqdm
      # Might need openai-whisper
      faster-whisper
    ];
  };

  subplz = buildPythonPackage rec {
    pname = "SubPlz";
    version = "1.4.0";
    #    format = "pyproject";

    doCheck = false;

    src = pkgs.fetchFromGitHub {
      owner = "kanjieater";
      repo = "SubPlz";
      rev = "7ce5671a3006a0e54d58b7f44651c97d593ae571";
      hash = "sha256-x5pBgRP20Y3RefE7lnawidY9trmizwo/9qGl81oHm8w=";
    };

    pypaBuildFlags = ["--skip-dependency-check"];

    pyproject = true;

    propagatedBuildInputs = with pkgs.python311Packages; [
      # Build dependencies
      setuptools
      setuptools-scm

      # SubPlz dependencies
      beautifulsoup4
      biopython
      ebooklib
      faster-whisper
      ffmpeg-python
      rapidfuzz
      numpy
      regex
      wcwidth
      natsort
      tqdm
      pysbd
      stable-ts
      ctranslate2
      stanza
    ];
  };
in {
  nixpkgs = {
    config = {
      cudaSupport = false;
      allowUnfree = true;
    };

    # Set ctranslate2 cuda support
    overlays = [
      (final: prev: {
        ctranslate2 = prev.ctranslate2.override {
          withCUDA = true;
          withCuDNN = true;
        };
        torchvision = prev.torchvision.override {
          withCUDA = true;
          withCuDNN = true;
        };
      })
    ];
  };

  nix = {
    package = pkgs.nix;
    settings = {
      cores = 6;
      max-jobs = 2;
      #      substituters = [
      #        "https://cachix.cachix.org"
      #        "https://nix-community.cachix.org"
      #      ];
      #      trusted-public-keys = [
      #        "cachix.cachix.org-1:eWNHQldwUO7G2VkjpnjDbWwy4KQ/HNxht7H4SSoMckM="
      #        "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
      #      ];
    };
  };

  home.packages = [
    (pkgs.python311.withPackages (
      p:
        with p; [
          subplz
        ]
    ))
  ];
}

Seems like this person had a very similar issue but I can’t seem to replicate their results.

Here’s the trace when trying to build:

yuki% home-manager switch --flake ~/.nix --show-trace
warning: Git tree '/home/safri/.nix' is dirty
warning: Git tree '/home/safri/.nix' is dirty
warning: Git tree '/home/safri/.nix' is dirty
warning: Git tree '/home/safri/.nix' is dirty
error: builder for '/nix/store/jk6m0xnl5qqp3firjpdpdj02gy33s8dw-python3.11-SubPlz-1.4.0.drv' failed with exit code 1;
       last 25 log lines:
       > adding 'subplz/sync.py'
       > adding 'subplz/test_align.py'
       > adding 'subplz/text.py'
       > adding 'subplz/transcribe.py'
       > adding 'subplz/utils.py'
       > adding 'subplz/alass/__init__.py'
       > adding 'subplz/alass/alass-linux64'
       > adding 'subplz/alass/alass.py'
       > adding 'subplz-2.0.0.dist-info/licenses/LICENSE'
       > adding 'subplz-2.0.0.dist-info/METADATA'
       > adding 'subplz-2.0.0.dist-info/WHEEL'
       > adding 'subplz-2.0.0.dist-info/entry_points.txt'
       > adding 'subplz-2.0.0.dist-info/top_level.txt'
       > adding 'subplz-2.0.0.dist-info/RECORD'
       > removing build/bdist.linux-x86_64/wheel
       > Successfully built subplz-2.0.0-py3-none-any.whl
       > Finished creating a wheel...
       > Finished executing pypaBuildPhase
       > Running phase: pythonRuntimeDepsCheckHook
       > Executing pythonRuntimeDepsCheck
       > Checking runtime dependencies for subplz-2.0.0-py3-none-any.whl
       >   - beautifulsoup4~=4.13.3 not satisfied by version 4.12.3
       >   - rapidfuzz~=3.12.1 not satisfied by version 3.13.0
       >   - numpy~=1.26.4 not satisfied by version 2.2.5
       >   - ctranslate2==4.4.0 not satisfied by version 4.6.0
       For full logs, run:
         nix log /nix/store/jk6m0xnl5qqp3firjpdpdj02gy33s8dw-python3.11-SubPlz-1.4.0.drv
error: 1 dependencies of derivation '/nix/store/9h8iw9l1pvnacj3nksdqqlqwaw6dbrrd-python3-3.11.13-env.drv' failed to build
error: 1 dependencies of derivation '/nix/store/snkh2b8g321lza8qycfjs7437bnmpk5z-home-manager-path.drv' failed to build
error: 1 dependencies of derivation '/nix/store/25bn5wawk1sfd888jxwvkqw07vw93bwc-home-manager-generation.drv' failed to build

It appears my eye’s only ever look at red in error messages, pretty sure the error has nothing to do with setuptools-scm and is just complaining about dependency versions.

Could you explain to me best practice for pinning to the package versions as found here:

dependencies = [
    "beautifulsoup4 ~= 4.13.3",
    "biopython ~= 1.85",
    "EbookLib ~= 0.18",
    "faster_whisper ~= 1.1.1",
    "ffmpeg_python ~= 0.2.0",
    "rapidfuzz ~= 3.12.1",
    "numpy ~= 1.26.4",
    "regex ~= 2024.11.6",
    "wcwidth >= 0.2.13",
    "natsort == 8.4.0",
    "tqdm ~= 4.67.1",
    "pysbd ~= 0.3.4",
    "stable-ts ~= 2.19.0",
    "ctranslate2==4.4.0",
    "stanza~=1.10.1",
]

The ones with incorrect versions are the following:

┃        > Checking runtime dependencies for subplz-2.0.0-py3-none-any.whl
┃        >   - beautifulsoup4~=4.13.3 not satisfied by version 4.12.3
┃        >   - rapidfuzz~=3.12.1 not satisfied by version 3.13.0
┃        >   - numpy~=1.26.4 not satisfied by version 2.2.5
┃        >   - ctranslate2==4.4.0 not satisfied by version 4.6.0

Also, how would I fetch a specific revision of a package? For example, how would I replace the current ctranslate2, with the one found here?