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

fix ollama gpu acceleration #373924

Closed
wants to merge 1 commit into from
Closed

Conversation

scd31
Copy link

@scd31 scd31 commented Jan 15, 2025

Fixed a bug where Ollama is no longer using GPU acceleration. Tested working with an Nvidia card. I am unsure if this also affected AMD cards

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@@ -199,6 +199,13 @@ goBuild {
make ${dist_cmd} -j $NIX_BUILD_CORES
'';

postInstall = lib.optionalString stdenv.hostPlatform.isLinux ''
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be a good idea to replace the condition stdenv.hostPlatform.isLinux with (enableRocm || enableCuda).
The replacement:

  1. Means what it says: if GPU acceleration is enabled, copy GPU related libraries. Currently the copying is always attempted when building on Linux, even on a default, CPU only build (which shouldn't need any GPU libraries to be copied).
  2. Won't activate for CPU builds, which is (probably) good (I'm not entirely certain, though). This script was removed because build errors were encountered when the cp was executed without any arguments to copy (there were no libraries to copy). I think that this probably happened because the copy was executed on CPU builds, and there aren't any files to copy on CPU builds (though, strangely, that would imply that there used to be GPU libraries to copy on CPU builds, since this script used to work).

I'm not actually sure whether or not the CPU build does have any GPU libraries to copy, or whether my theory is at all accurate in the first place, so further testing is probably needed. See also my other comment on the topic.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think switching this would be good as it is more aligned to the intent of the build and isLinux feels like a hack. It may also cut down the build time.

We should test to confirm that there aren't any strange surprises.

Copy link
Author

Choose a reason for hiding this comment

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

Feedback has been addressed (: I will test this on Nvidia and CPU (cannot test on AMD)

Copy link
Author

Choose a reason for hiding this comment

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

Works on both machines! Nvidia and CPU are both good

Copy link
Member

Choose a reason for hiding this comment

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

I'm not actually sure whether or not the CPU build does have any GPU libraries to copy, or whether my theory is at all accurate in the first place, so further testing is probably needed. See also my #365718 (comment) on the topic.

There are multiple types of CPU runners that use the same structure. A rocm + CPU build ends up with:

lib/ollama/runners/:
cpu_avx  cpu_avx2  rocm_avx

Official ollama releases end up with cpu_avx cpu_avx2 cuda_v11_avx cuda_v12_avx rocm_avx folders under runners.

Copy link
Author

Choose a reason for hiding this comment

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

Okay, so maybe we want to copy it indiscriminately then?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, accel flag checking approach means CPU ollama build on this PR is missing cpu_avx/cpu_avx2 😅

$ nix build github:nixos/nixpkgs/5e728b154d0e0f25cbc8f05b7006d001f20df52e#ollama
$ ls result/lib/ollama/runners
ls: cannot access 'result/lib/ollama/runners': No such file or directory

Copy link
Author

Choose a reason for hiding this comment

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

lol I will revert

Copy link
Author

Choose a reason for hiding this comment

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

Done!

Copy link
Member

Choose a reason for hiding this comment

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

CPU build looks correct now:

$ nix build github:nixos/nixpkgs/6ad8f0640cdf4077335177a93b319b94a7226f15#ollama
$ ls result/lib/ollama/runners
cpu_avx  cpu_avx2

@pbsds
Copy link
Member

pbsds commented Jan 15, 2025

Is this compatible with the changes in #373234?

@GaetanLepage
Copy link
Contributor

nixpkgs-review result

Generated using nixpkgs-review.

Command: nixpkgs-review pr 373924


x86_64-linux

✅ 5 packages built:
  • alpaca
  • chatd
  • ollama
  • ollama-cuda
  • ollama-rocm

aarch64-linux

❌ 3 packages failed to build:
  • alpaca
  • chatd
  • ollama
✅ 1 package built:
  • ollama-cuda

x86_64-darwin

✅ 1 package built:
  • ollama

aarch64-darwin

❌ 1 package failed to build:
  • ollama

@scd31
Copy link
Author

scd31 commented Jan 15, 2025

@pbsds Looks like these changes are in that PR as well. Maybe we close this and just fix it in that, if that's getting merged soon-ish?

@scd31 scd31 force-pushed the ollama-gpu-bugfix branch from 7c3ded6 to 5e728b1 Compare January 15, 2025 16:08
@scd31
Copy link
Author

scd31 commented Jan 15, 2025

@pbsds Actually we should be able to merge this one and then rebase that PR

@GaetanLepage
Copy link
Contributor

nixpkgs-review result

Generated using nixpkgs-review.

Command: nixpkgs-review pr 373924


x86_64-linux

✅ 5 packages built:
  • alpaca
  • chatd
  • ollama
  • ollama-cuda
  • ollama-rocm

aarch64-linux

✅ 4 packages built:
  • alpaca
  • chatd
  • ollama
  • ollama-cuda

x86_64-darwin

❌ 1 package failed to build:
  • chatd
✅ 1 package built:
  • ollama

aarch64-darwin

❌ 2 packages failed to build:
  • chatd
  • ollama

@scd31 scd31 force-pushed the ollama-gpu-bugfix branch from 5e728b1 to 6ad8f06 Compare January 15, 2025 20:55
@prusnak
Copy link
Member

prusnak commented Jan 15, 2025

Since #373234 is a superset of this PR and it is older, let's close this PR in favor of the other.

@prusnak prusnak closed this Jan 15, 2025
@scd31 scd31 deleted the ollama-gpu-bugfix branch January 15, 2025 21:40
@abysssol abysssol mentioned this pull request Jan 16, 2025
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants