1
0

flake.nix 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. {
  2. description = "Port of Facebook's LLaMA model in C/C++";
  3. inputs = {
  4. nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  5. flake-parts.url = "github:hercules-ci/flake-parts";
  6. };
  7. # Optional binary cache
  8. nixConfig = {
  9. extra-substituters = [
  10. # Populated by the CI in ggerganov/llama.cpp
  11. "https://llama-cpp.cachix.org"
  12. # A development cache for nixpkgs imported with `config.cudaSupport = true`.
  13. # Populated by https://hercules-ci.com/github/SomeoneSerge/nixpkgs-cuda-ci.
  14. # This lets one skip building e.g. the CUDA-enabled openmpi.
  15. # TODO: Replace once nix-community obtains an official one.
  16. "https://cuda-maintainers.cachix.org"
  17. ];
  18. # Verify these are the same keys as published on
  19. # - https://app.cachix.org/cache/llama-cpp
  20. # - https://app.cachix.org/cache/cuda-maintainers
  21. extra-trusted-public-keys = [
  22. "llama-cpp.cachix.org-1:H75X+w83wUKTIPSO1KWy9ADUrzThyGs8P5tmAbkWhQc="
  23. "cuda-maintainers.cachix.org-1:0dq3bujKpuEPMCX6U4WylrUDZ9JyUG0VpVZa7CNfq5E="
  24. ];
  25. };
  26. # For inspection, use `nix flake show github:ggerganov/llama.cpp` or the nix repl:
  27. #
  28. # ```bash
  29. # ❯ nix repl
  30. # nix-repl> :lf github:ggerganov/llama.cpp
  31. # Added 13 variables.
  32. # nix-repl> outputs.apps.x86_64-linux.quantize
  33. # { program = "/nix/store/00000000000000000000000000000000-llama.cpp/bin/quantize"; type = "app"; }
  34. # ```
  35. outputs =
  36. { self, flake-parts, ... }@inputs:
  37. let
  38. # We could include the git revisions in the package names but those would
  39. # needlessly trigger rebuilds:
  40. # llamaVersion = self.dirtyShortRev or self.shortRev;
  41. # Nix already uses cryptographic hashes for versioning, so we'll just fix
  42. # the fake semver for now:
  43. llamaVersion = "0.0.0";
  44. in
  45. flake-parts.lib.mkFlake { inherit inputs; }
  46. {
  47. imports = [
  48. .devops/nix/nixpkgs-instances.nix
  49. .devops/nix/apps.nix
  50. .devops/nix/devshells.nix
  51. .devops/nix/jetson-support.nix
  52. ];
  53. # An overlay can be used to have a more granular control over llama-cpp's
  54. # dependencies and configuration, than that offered by the `.override`
  55. # mechanism. Cf. https://nixos.org/manual/nixpkgs/stable/#chap-overlays.
  56. #
  57. # E.g. in a flake:
  58. # ```
  59. # { nixpkgs, llama-cpp, ... }:
  60. # let pkgs = import nixpkgs {
  61. # overlays = [ (llama-cpp.overlays.default) ];
  62. # system = "aarch64-linux";
  63. # config.allowUnfree = true;
  64. # config.cudaSupport = true;
  65. # config.cudaCapabilities = [ "7.2" ];
  66. # config.cudaEnableForwardCompat = false;
  67. # }; in {
  68. # packages.aarch64-linux.llamaJetsonXavier = pkgs.llamaPackages.llama-cpp;
  69. # }
  70. # ```
  71. #
  72. # Cf. https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html?highlight=flake#flake-format
  73. flake.overlays.default =
  74. (final: prev: {
  75. llamaPackages = final.callPackage .devops/nix/scope.nix { inherit llamaVersion; };
  76. inherit (final.llamaPackages) llama-cpp;
  77. });
  78. systems = [
  79. "aarch64-darwin"
  80. "aarch64-linux"
  81. "x86_64-darwin" # x86_64-darwin isn't tested (and likely isn't relevant)
  82. "x86_64-linux"
  83. ];
  84. perSystem =
  85. {
  86. config,
  87. lib,
  88. system,
  89. pkgs,
  90. pkgsCuda,
  91. pkgsRocm,
  92. ...
  93. }:
  94. {
  95. # Unlike `.#packages`, legacyPackages may contain values of
  96. # arbitrary types (including nested attrsets) and may even throw
  97. # exceptions. This attribute isn't recursed into by `nix flake
  98. # show` either.
  99. #
  100. # You can add arbitrary scripts to `.devops/nix/scope.nix` and
  101. # access them as `nix build .#llamaPackages.${scriptName}` using
  102. # the same path you would with an overlay.
  103. legacyPackages = {
  104. llamaPackages = pkgs.callPackage .devops/nix/scope.nix { inherit llamaVersion; };
  105. llamaPackagesCuda = pkgsCuda.callPackage .devops/nix/scope.nix { inherit llamaVersion; };
  106. llamaPackagesRocm = pkgsRocm.callPackage .devops/nix/scope.nix { inherit llamaVersion; };
  107. };
  108. # We don't use the overlay here so as to avoid making too many instances of nixpkgs,
  109. # cf. https://zimbatm.com/notes/1000-instances-of-nixpkgs
  110. packages =
  111. {
  112. default = config.legacyPackages.llamaPackages.llama-cpp;
  113. }
  114. // lib.optionalAttrs pkgs.stdenv.isLinux {
  115. opencl = config.packages.default.override { useOpenCL = true; };
  116. cuda = config.legacyPackages.llamaPackagesCuda.llama-cpp;
  117. mpi-cpu = config.packages.default.override { useMpi = true; };
  118. mpi-cuda = config.packages.default.override { useMpi = true; };
  119. }
  120. // lib.optionalAttrs (system == "x86_64-linux") {
  121. rocm = config.legacyPackages.llamaPackagesRocm.llama-cpp;
  122. };
  123. # Packages exposed in `.#checks` will be built by the CI and by
  124. # `nix flake check`. Currently we expose all packages, but we could
  125. # make more granular choices
  126. checks = config.packages;
  127. };
  128. };
  129. }