Skip to content

Expose num_gpu as a first-class argument on generate/chat - #720

Open
shreesha345 wants to merge 1 commit into
ollama:mainfrom
shreesha345:feat/gpu-selection-num-gpu
Open

Expose num_gpu as a first-class argument on generate/chat#720
shreesha345 wants to merge 1 commit into
ollama:mainfrom
shreesha345:feat/gpu-selection-num-gpu

Conversation

@shreesha345

@shreesha345 shreesha345 commented Aug 17, 2026

Copy link
Copy Markdown

Summary

  • Adds a documented, type-checked num_gpu keyword argument to Client/AsyncClient generate() and chat() (sync + async), merged into options via a new _merge_options() helper so it composes cleanly with any user-supplied options= dict/Options instance.
  • Previously num_gpu was only reachable via options={'num_gpu': N} — undiscoverable, and easily confused with "number of GPUs" rather than its actual meaning: "layers offloaded to GPU". This PR documents that distinction explicitly.
  • Adds a "GPU Selection" section to README.md and a new examples/gpu-selection.py, both explaining num_gpu and the multi-ollama serve / multi-Client(host=...) pattern for hard GPU isolation across processes on a shared multi-GPU server (device selection itself is a server-side concern controlled by CUDA_VISIBLE_DEVICES per ollama serve process — there's no per-request "which physical GPU" field in the REST API).

Closes #161, #603
Relates to #62

Test plan

  • pytest tests/test_client.py -k num_gpu -v — 3 new tests pass (test_client_chat_with_num_gpu, test_client_chat_with_num_gpu_merges_options, test_client_generate_with_num_gpu)
  • pytest ollama tests — full suite passes except 8 pre-existing, unrelated Windows-only failures (tempfile permission / image serialization edge cases), confirmed via git stash to exist on main prior to this change
  • ruff check / ruff format --check — clean

Adds a documented, type-checked num_gpu keyword argument to
Client/AsyncClient generate() and chat(), merged into options via a
new _merge_options() helper so it composes with any user-supplied
options dict/Options instance.

Previously num_gpu was only reachable via options={'num_gpu': N},
which was undiscoverable and easy to confuse with "number of GPUs"
rather than "layers offloaded to GPU". Documents the distinction and
the multi ollama serve / multi Client(host=...) pattern for hard
GPU isolation in the README and a new examples/gpu-selection.py.

Fixes ollama#161, ollama#603
Relates to ollama#62
@shreesha345
shreesha345 force-pushed the feat/gpu-selection-num-gpu branch from b3e352d to 989a5de Compare August 17, 2026 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GPU Selection & Device Specification

1 participant