1
0

flake.nix 6.1 KB

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