Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix: windows build #6167

Merged
merged 7 commits into from
Mar 28, 2024
Merged

nix: windows build #6167

merged 7 commits into from
Mar 28, 2024

Conversation

hutli
Copy link
Contributor

@hutli hutli commented Mar 19, 2024

Fixes correct file extensien when using pkgsCross stdenv for crossCompiling for windows using mingwW64 inside Nix.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

Comment on lines 231 to 232
mv $out/bin/main${executableSuffix} $out/bin/llama
mv $out/bin/server${executableSuffix} $out/bin/llama-server
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been brought up before, it's really awkward we maintain a custom intsallPhase: the cmakelists need to be updated

(out of scope for this PR)

build.zig Outdated Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❯ nix run nixpkgs#wine64 -- "$(nix build github:ggerganov/llama.cpp/553aafca25ed6aa3721b3e0bfa121eed3f2b7b58#windows --print-out-paths)"/bin/llama-bench.exe
0084:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005
0084:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005
0084:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005
0084:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005
| model                          |       size |     params | backend    |    threads | test       |
         t/s |
| ------------------------------ | ---------: | ---------: | ---------- | ---------: | ---------- | ----
-----------: |
main: error: failed to load model 'models/7B/ggml-model-q4_0.gguf'

I guess this works

@SomeoneSerge SomeoneSerge added the nix Issues specific to consuming flake.nix, or generally concerned with ❄ Nix-based llama.cpp deployment label Mar 21, 2024
Copy link
Collaborator

@SomeoneSerge SomeoneSerge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's wait for the CI to pass and squash-merge this

flake.nix Outdated
Comment on lines 158 to 160
windows = config.legacyPackages.llamaPackages.llama-cpp.override {
stdenv = pkgs.pkgsCross.mingwW64.stdenv;
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait! Actually, I'm surprised this worked. I never touched windows, but shouldn't we be taking the other dependencies from pkgsCross as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which would look something like (pkgsCross.mingwW64.callPackage .devops/nix/scope.nix { }).llama-cpp

Copy link
Contributor Author

@hutli hutli Mar 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my experimentation the only parts that are needed to cross compile for Windows is the MinGW executable and the libraries it comes with which is all in stdenv. I did consider using pkgsCross.callPackage instead of overwriting stdenv but wouldn't that mean that we cannot compile for windows and CUDA/ROCM at the same time, since we use pkgsCuda.callPackage and pkgsRocm.callPackage respectively?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but wouldn't that mean that we cannot compile for windows and CUDA/ROCM since we use pkgsCuda.callPackage and pkgsRocm.callPackage respectively?

Being able to construct a configuration with cuda+rocm and exposing a flake output for that configuration are two different things. The flake outputs are there mostly for show. The actual "useful" program is the package.nix (and the scope.nix) which are functions that you can call from arbitrary instances of Nixpkgs.

Those can be consumed e.g. as overlays

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But also linking the build platform's (linux's) CUDA libraries to a mingw executable would obviously fail

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I've tried doing this while using pkgs.pkgsCross.mingwW64.callPackage but I'm getting problems with packages that complain about the platform now. This will take some time to get working in the right way but I'm on it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'm also surprised we didn't have to use makeScopeWithSplicing)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright after quite a bit of testing it seems that the best solution is to override stdenv - I've tested the binaries on a Windows system and they all seems to run correctly 👍

I think this should be ready to merge. Let me know if you think anything is missing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, check out the patches I just pushed. The only thing that was broken in the cross-compilation set up was python

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did it! I tried to use pkgs.pkgsCross.mingwW64.callPackage but I kept getting that python error - which you've fixed now. I can even compile for Vulkan and it works on our Windows machine.

screenshot

hutli and others added 3 commits March 27, 2024 13:09
initial nix build for windows using zig

mingwW64 build

removes nix zig windows build

removes nix zig windows build

removed unnessesary glibc.static

removed unnessesary import of pkgs in nix

fixed missing trailing newline on non-windows nix builds

overriding stdenv when building for crosscompiling to windows in nix

better variables when crosscompiling windows in nix

cross compile windows on macos

removed trailing whitespace

remove unnessesary overwrite of "CMAKE_SYSTEM_NAME" in nix windows build

nix: keep file extension when copying result files during cross compile for windows

nix: better checking for file extensions when using MinGW

nix: using hostPlatform instead of targetPlatform when cross compiling for Windows

using hostPlatform.extensions.executable to extract executable format
- The generic /usr/bin/env shebangs are good enough
- Python deps are provisioned in the devShells
- We need to be able to leave python out at least on windows (currently breaks eval)
Take all dependencies from the cross stage, rather tha only stdenv
@SomeoneSerge
Copy link
Collaborator

@hutli I squashed and force-pushed your commits because there were some git conflicts with the master branch. I imagine you still have the commits with your original experiments (zig&c) locally if that matters

@hutli
Copy link
Contributor Author

hutli commented Mar 27, 2024

@hutli I squashed and force-pushed your commits because there were some git conflicts with the master branch. I imagine you still have the commits with your original experiments (zig&c) locally if that matters

Amazing, thank you! Yes, I have it locally and I'll save it just in case, thank you for the heads up 👍

@@ -200,7 +200,7 @@ effectiveStdenv.mkDerivation (
++ optionals useMpi [ mpi ]
++ optionals useOpenCL [ clblast ]
++ optionals useRocm rocmBuildInputs
++ optionals useBlas [ blas ]
++ optionals (useBlas && builtins.elem effectiveStdenv.hostPlatform.system blas.meta.platforms) [ blas ]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is lib.meta.availableOn but first say, does this really break in .#windows?

@@ -200,7 +200,7 @@ effectiveStdenv.mkDerivation (
++ optionals useMpi [ mpi ]
++ optionals useOpenCL [ clblast ]
++ optionals useRocm rocmBuildInputs
++ optionals useBlas [ blas ]
++ optionals (useBlas && blas.meta.available) [ blas ]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think we should instead && this in

useBlas ? builtins.all (x: !x) [
useCuda
useMetalKit
useOpenCL
useRocm
useVulkan
],
.

Rationale: user-supplied useBlas should have the priority, but we want to choose a different default for useBlas in the cross-compilation setting

@SomeoneSerge
Copy link
Collaborator

SomeoneSerge commented Mar 28, 2024

The failure is from #6346. Wfm locally, also the eval job succeeds both on mac and ubuntu, so I'm guessing this is ready

@SomeoneSerge SomeoneSerge merged commit d2d8f38 into ggerganov:master Mar 28, 2024
21 of 22 checks passed
@hutli hutli deleted the nix-windows branch March 28, 2024 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nix Issues specific to consuming flake.nix, or generally concerned with ❄ Nix-based llama.cpp deployment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants