[GH-ISSUE #358] Parallel requests #62196

Closed
opened 2026-05-03 07:49:13 -05:00 by GiteaMirror · 84 comments
Owner

Originally created by @youssef02 on GitHub (Aug 16, 2023).
Original GitHub issue: https://github.com/ollama/ollama/issues/358

Originally assigned to: @dhiltgen on GitHub.

the app is amazing but the problem is If I want to create a multiagent from one api, I need to create a Queue system, as it can reply only to one request at a time, is there a way to improve this or do I have to implement a Queue system?

I just started here so sorry for any mistake, ;)

Originally created by @youssef02 on GitHub (Aug 16, 2023). Original GitHub issue: https://github.com/ollama/ollama/issues/358 Originally assigned to: @dhiltgen on GitHub. the app is amazing but the problem is If I want to create a multiagent from one api, I need to create a Queue system, as it can reply only to one request at a time, is there a way to improve this or do I have to implement a Queue system? I just started here so sorry for any mistake, ;)
GiteaMirror added the feature request label 2026-05-03 07:49:13 -05:00
Author
Owner

@jmorganca commented on GitHub (Aug 23, 2023):

Not a mistake – Ollama will serve one generation at a time currently, but supporting 2+ concurrent requests is definitely on the roadmap

<!-- gh-comment-id:1690437277 --> @jmorganca commented on GitHub (Aug 23, 2023): Not a mistake – Ollama will serve one generation at a time currently, but supporting 2+ concurrent requests is definitely on the roadmap
Author
Owner

@LeHaroun commented on GitHub (Oct 17, 2023):

Is there a way to run multiple instances on the same machine

the app is amazing but the problem is If I want to create a multiagent from one api, I need to create a Queue system, as it can reply only to one request at a time, is there a way to improve this or do I have to implement a Queue system?

I just started here so sorry for any mistake, ;)

I am working on a similar implementation using MAS, If you have beefy hardware, I recommend you run several instances in closed envs. Save completions or generation and process them later on.

Also check LiteLLM APIs on top of Ollama. Quite helpful.

<!-- gh-comment-id:1767367656 --> @LeHaroun commented on GitHub (Oct 17, 2023): Is there a way to run multiple instances on the same machine > the app is amazing but the problem is If I want to create a multiagent from one api, I need to create a Queue system, as it can reply only to one request at a time, is there a way to improve this or do I have to implement a Queue system? > > I just started here so sorry for any mistake, ;) I am working on a similar implementation using MAS, If you have beefy hardware, I recommend you run several instances in closed envs. Save completions or generation and process them later on. Also check [LiteLLM](https://docs.litellm.ai/docs/providers/ollama) APIs on top of Ollama. Quite helpful.
Author
Owner

@skye0402 commented on GitHub (Jan 9, 2024):

Would be definitely a great addition to Ollama:

  • Concurrency of requests
  • Using GPU mem for several models

I'm running it on cloud using a T4 with 16GB GPU memory and having a phi-2 and codellama both in the V-RAM would be no issue at all. Ideally other models are kept in regular RAM instead of loading it from disk.
Adding to it: Users might switch models, so the queuing approach would apply to model switches, too 😃

<!-- gh-comment-id:1882029890 --> @skye0402 commented on GitHub (Jan 9, 2024): Would be definitely a great addition to Ollama: - Concurrency of requests - Using GPU mem for several models I'm running it on cloud using a T4 with 16GB GPU memory and having a phi-2 and codellama both in the V-RAM would be no issue at all. Ideally other models are kept in regular RAM instead of loading it from disk. Adding to it: Users might switch models, so the queuing approach would apply to model switches, too 😃
Author
Owner

@ivanfioravanti commented on GitHub (Jan 18, 2024):

Any news on this one? Parallel requests can be a real game changer for Ollama

<!-- gh-comment-id:1899316861 --> @ivanfioravanti commented on GitHub (Jan 18, 2024): Any news on this one? Parallel requests can be a real game changer for Ollama
Author
Owner

@ParisNeo commented on GitHub (Jan 30, 2024):

If you have enough VRAM to run multiple models, you can create multiple instances of ollama with different port numbers then use my proxy to manage access and route to the requests to each:
ParisNeo's ollama_proxy_server

<!-- gh-comment-id:1916241524 --> @ParisNeo commented on GitHub (Jan 30, 2024): If you have enough VRAM to run multiple models, you can create multiple instances of ollama with different port numbers then use my proxy to manage access and route to the requests to each: [ParisNeo's ollama_proxy_server](https://github.com/ParisNeo/ollama_proxy_server)
Author
Owner

@ehartford commented on GitHub (Jan 30, 2024):

I would really like to have an m2 ultra 192gb on my company's intranet that can service the whole R&D department (a dozen people)

as long as I have enough ram, I wish to be able to run multiple inference request at the same time. Thank you for considering my wish!

<!-- gh-comment-id:1918033760 --> @ehartford commented on GitHub (Jan 30, 2024): I would really like to have an m2 ultra 192gb on my company's intranet that can service the whole R&D department (a dozen people) as long as I have enough ram, I wish to be able to run multiple inference request at the same time. Thank you for considering my wish!
Author
Owner

@ivanfioravanti commented on GitHub (Jan 30, 2024):

same here, multiple 7B models served by an M2 Ultra. My dream! 🙏

<!-- gh-comment-id:1918100403 --> @ivanfioravanti commented on GitHub (Jan 30, 2024): same here, multiple 7B models served by an M2 Ultra. My dream! 🙏
Author
Owner

@Adphi commented on GitHub (Jan 31, 2024):

At first glance, when I started examining the source code, I thought that the problem of concurrent requests was due to the fact that the current implementation referred to the use of global variables in its llama.cpp binding. After paying a little more attention to the original llapma.cpp source code, I realized that the original implementation wasn't exactly geared towards multithreaded or server-side use, but rather towards to a local development/experimentation use case.
So apart from setting up an API using queued workers (managed using gRPC for example) based on a fork/exec model to get around the lack of batch processing on the llama.cpp side, which doesn't seem to procure too much attention from the developpers (reference missing, but found in the github project's conversations), which Local AI does in a way, I don't see exactly how it could be implemented here.

<!-- gh-comment-id:1918125618 --> @Adphi commented on GitHub (Jan 31, 2024): At first glance, when I started examining the source code, I thought that the problem of concurrent requests was due to the fact that the current implementation referred to the use of global variables in its llama.cpp binding. After paying a little more attention to the original llapma.cpp source code, I realized that the original implementation wasn't exactly geared towards multithreaded or server-side use, but rather towards to a local development/experimentation use case. So apart from setting up an API using queued workers (managed using gRPC for example) based on a fork/exec model to get around the lack of batch processing on the llama.cpp side, which doesn't seem to procure too much attention from the developpers (reference missing, but found in the github project's conversations), which Local AI does in a way, I don't see exactly how it could be implemented here.
Author
Owner

@ParisNeo commented on GitHub (Jan 31, 2024):

That's why I had to build a proxy. You can install multiple servers on a single or multiple machines then use my proxy to service multiple users with a multi queue. For example if you have 2 servers then you can servicec at most 2 clients simultaniously and when the two are buzy, the current message is queued on the server that has the least full queue. In practice, since the generation is very fast, you don't get too much collisions. We rarely get more than 1 person in the queue.

I also added security features as well as logging features. It works fine and serves all of us perfectly.

Also, some dude built a docker docker version and I accepted his PR:
https://github.com/ParisNeo/ollama_proxy_server

If you are interested, you can try it, it is open source, you can also read the code, get inspired, help enhance it etc. It is Apache 2.0 so you can do whatever you want with it 100% free.

We get around 300 tokens/s for each user so it is not causing any significant delay.

<!-- gh-comment-id:1918664301 --> @ParisNeo commented on GitHub (Jan 31, 2024): That's why I had to build a proxy. You can install multiple servers on a single or multiple machines then use my proxy to service multiple users with a multi queue. For example if you have 2 servers then you can servicec at most 2 clients simultaniously and when the two are buzy, the current message is queued on the server that has the least full queue. In practice, since the generation is very fast, you don't get too much collisions. We rarely get more than 1 person in the queue. I also added security features as well as logging features. It works fine and serves all of us perfectly. Also, some dude built a docker docker version and I accepted his PR: https://github.com/ParisNeo/ollama_proxy_server If you are interested, you can try it, it is open source, you can also read the code, get inspired, help enhance it etc. It is Apache 2.0 so you can do whatever you want with it 100% free. We get around 300 tokens/s for each user so it is not causing any significant delay.
Author
Owner

@ehartford commented on GitHub (Jan 31, 2024):

llama.cpp can be run as multiple process, multiple thread, or as server.
but it's totally fair for the feature request to be deprioritized, of course.

<!-- gh-comment-id:1918794572 --> @ehartford commented on GitHub (Jan 31, 2024): llama.cpp can be run as multiple process, multiple thread, or as server. but it's totally fair for the feature request to be deprioritized, of course.
Author
Owner

@farhanhubble commented on GitHub (Feb 1, 2024):

The easiest way to multiplex Ollama, at least on linux system should be with a reverse proxy load balancer like HAProxy. Launch multiple instances of ollama serve on different ports and map them to a single port using HAproxy.

Note that the approach can sometimes deteriorate performance due to CPU contention. I have a decent system with 64 cores and 24GB of GPU RAM. When I run 3 instances of Ollama with HAproxy to generate embeddings it does speed up the process however if I try to generate text, the processing time is much worse than with a single instance.

<!-- gh-comment-id:1920604398 --> @farhanhubble commented on GitHub (Feb 1, 2024): The easiest way to multiplex Ollama, at least on linux system should be with a reverse proxy load balancer like HAProxy. Launch multiple instances of `ollama serve` on different ports and map them to a single port using HAproxy. Note that the approach can sometimes deteriorate performance due to CPU contention. I have a decent system with 64 cores and 24GB of GPU RAM. When I run 3 instances of Ollama with HAproxy to generate embeddings it does speed up the process however if I try to generate text, the processing time is much worse than with a single instance.
Author
Owner

@trymeouteh commented on GitHub (Mar 1, 2024):

Would like to see the ability for using the same LLM in two or more apps at the same time and would like to see the ability to use multiple LLMs in two or more apps at the same time

<!-- gh-comment-id:1972485301 --> @trymeouteh commented on GitHub (Mar 1, 2024): Would like to see the ability for using the same LLM in two or more apps at the same time and would like to see the ability to use multiple LLMs in two or more apps at the same time
Author
Owner

@ParisNeo commented on GitHub (Mar 1, 2024):

well technically, you can run multiple instances of the same model by runnin multiple instances of ollama with different port numbers, configure them in the proxy config file then they can be accessed from multiple clients at once.

<!-- gh-comment-id:1973993924 --> @ParisNeo commented on GitHub (Mar 1, 2024): well technically, you can run multiple instances of the same model by runnin multiple instances of ollama with different port numbers, configure them in the proxy config file then they can be accessed from multiple clients at once.
Author
Owner

@ehartford commented on GitHub (Mar 1, 2024):

That's just wrong 😂

<!-- gh-comment-id:1974008063 --> @ehartford commented on GitHub (Mar 1, 2024): That's just wrong 😂
Author
Owner

@ParisNeo commented on GitHub (Mar 1, 2024):

ollama does not support batching and unless they build something like vllm, I don't see how you can do it.

<!-- gh-comment-id:1974010515 --> @ParisNeo commented on GitHub (Mar 1, 2024): ollama does not support batching and unless they build something like vllm, I don't see how you can do it.
Author
Owner

@ehartford commented on GitHub (Mar 1, 2024):

manage a process/thread pool. Load balance. It's not rocket science.

<!-- gh-comment-id:1974013539 --> @ehartford commented on GitHub (Mar 1, 2024): manage a process/thread pool. Load balance. It's not rocket science.
Author
Owner

@youssef02 commented on GitHub (Mar 1, 2024):

This is somehow a problem with scaling, I think. While ollama imodel can only handle one request at a time. It can't be concurrent to multiple request as it will need to run new models, which take more resources. I really want for someone to share their finding when trying to handle multiple request like in a uni environment with many student.

How to approach this problem

<!-- gh-comment-id:1974019759 --> @youssef02 commented on GitHub (Mar 1, 2024): This is somehow a problem with scaling, I think. While ollama imodel can only handle one request at a time. It can't be concurrent to multiple request as it will need to run new models, which take more resources. I really want for someone to share their finding when trying to handle multiple request like in a uni environment with many student. How to approach this problem
Author
Owner

@ehartford commented on GitHub (Mar 2, 2024):

The idea is the system (a MacBook or Mac studio with 128gb+ of unified RAM) can handle several dozen 4-bit 7b models at once.

So taking more resources is ok, no problem.

<!-- gh-comment-id:1974132771 --> @ehartford commented on GitHub (Mar 2, 2024): The idea is the system (a MacBook or Mac studio with 128gb+ of unified RAM) can handle several dozen 4-bit 7b models at once. So taking more resources is ok, no problem.
Author
Owner

@ParisNeo commented on GitHub (Mar 2, 2024):

The idea is the system (a MacBook or Mac studio with 128gb+ of unified RAM) can handle several dozen 4-bit 7b models at once.

So taking more resources is ok, no problem.

If you want that way, then you can use my ollama_proxy project, it does exactly what you asked for.
If multiplying the number of resources doesn't bother you then this is a simple solution. Each ollama process runs on its own and the proxy manages the routing and queues and everything:
https://github.com/ParisNeo/ollama_proxy_server

I was really wanting a better more optimized way. Neural nets can do inferance on a batch of inputs. You don't need to duplicate the model weights. That's what we talk about when we say real multigeneration system. You can perform fast inference with that, just as in vllm.

<!-- gh-comment-id:1974827413 --> @ParisNeo commented on GitHub (Mar 2, 2024): > The idea is the system (a MacBook or Mac studio with 128gb+ of unified RAM) can handle several dozen 4-bit 7b models at once. > > So taking more resources is ok, no problem. If you want that way, then you can use my ollama_proxy project, it does exactly what you asked for. If multiplying the number of resources doesn't bother you then this is a simple solution. Each ollama process runs on its own and the proxy manages the routing and queues and everything: [https://github.com/ParisNeo/ollama_proxy_server](https://github.com/ParisNeo/ollama_proxy_server) I was really wanting a better more optimized way. Neural nets can do inferance on a batch of inputs. You don't need to duplicate the model weights. That's what we talk about when we say real multigeneration system. You can perform fast inference with that, just as in vllm.
Author
Owner

@ehartford commented on GitHub (Mar 2, 2024):

I appreciate that you have provided a workaround.

Basically that's what ollama needs to implement internally.

I want ollama to be smart, and internally spawn and destroy llama.cpp threads/processes as needed to handle parallel requests without an external proxy.

That's what I'm asking for. it should "just work" ollama-style.

<!-- gh-comment-id:1974853026 --> @ehartford commented on GitHub (Mar 2, 2024): I appreciate that you have provided a workaround. Basically that's what ollama needs to implement internally. I want ollama to be smart, and internally spawn and destroy llama.cpp threads/processes as needed to handle parallel requests without an external proxy. That's what I'm asking for. it should "just work" ollama-style.
Author
Owner

@ffaubert commented on GitHub (Mar 18, 2024):

+1 for this. It would be a game changer. @bmizerany is this something in progress?

<!-- gh-comment-id:2002702529 --> @ffaubert commented on GitHub (Mar 18, 2024): +1 for this. It would be a game changer. @bmizerany is this something in progress?
Author
Owner

@triet0612 commented on GitHub (Mar 19, 2024):

I notice that new requests will wait for old requests to complete despite that we interrupt and stop old request when they're running or waiting to be executed. Have anyone have this issue? Thank you and sorry for taking your time.

<!-- gh-comment-id:2007168385 --> @triet0612 commented on GitHub (Mar 19, 2024): I notice that new requests will wait for old requests to complete despite that we interrupt and stop old request when they're running or waiting to be executed. Have anyone have this issue? Thank you and sorry for taking your time.
Author
Owner

@ehartford commented on GitHub (Mar 19, 2024):

I notice similar sometimes I have to kill and restart ollama because of it but I think that deserves it's own issue.

<!-- gh-comment-id:2007456580 --> @ehartford commented on GitHub (Mar 19, 2024): I notice similar sometimes I have to kill and restart ollama because of it but I think that deserves it's own issue.
Author
Owner

@jammsen commented on GitHub (Mar 20, 2024):

Not a mistake – Ollama will serve one generation at a time currently, but supporting 2+ concurrent requests is definitely on the roadmap

@jmorganca you wrote its on the roadmap, could you explain for when? Do you have a date?

Something beeing on a roadmap doesnt in reality mean it gets solved/done/programmed.

Could you help us/the community out please? The request is from 6months++ ago.

<!-- gh-comment-id:2010280146 --> @jammsen commented on GitHub (Mar 20, 2024): > Not a mistake – Ollama will serve one generation at a time currently, but supporting 2+ concurrent requests is definitely on the roadmap @jmorganca you wrote its on the roadmap, could you explain for when? Do you have a date? Something beeing on a roadmap doesnt in reality mean it gets solved/done/programmed. Could you help us/the community out please? The request is from 6months++ ago.
Author
Owner

@ehartford commented on GitHub (Mar 20, 2024):

this feature would allow ollama to be a production backend. Low traffic for sure but still, this would get a lot of startup running / hosting MVP with minimal effort.

<!-- gh-comment-id:2010706535 --> @ehartford commented on GitHub (Mar 20, 2024): this feature would allow ollama to be a production backend. Low traffic for sure but still, this would get a lot of startup running / hosting MVP with minimal effort.
Author
Owner

@ParisNeo commented on GitHub (Mar 24, 2024):

In the meantime you can try my ollama proxy :)
It can be configured to fire up multiple ollama services, then you can also define users with keys to access the service and it balances the loads between the instances of ollama with multiple queues.

<!-- gh-comment-id:2016773686 --> @ParisNeo commented on GitHub (Mar 24, 2024): In the meantime you can try my ollama proxy :) It can be configured to fire up multiple ollama services, then you can also define users with keys to access the service and it balances the loads between the instances of ollama with multiple queues.
Author
Owner

@jammsen commented on GitHub (Mar 24, 2024):

In the meantime you can try my ollama proxy :) It can be configured to fire up multiple ollama services, then you can also define users with keys to access the service and it balances the loads between the instances of ollama with multiple queues.

@ParisNeo
Do you have a link for me please? Also how does that works from an architectur point of view? I guessing sticky-session, so people usng this wont get mixed up and have a english cooking recipe with c++ snippets and german dictionaries in there right?

<!-- gh-comment-id:2016775351 --> @jammsen commented on GitHub (Mar 24, 2024): > In the meantime you can try my ollama proxy :) It can be configured to fire up multiple ollama services, then you can also define users with keys to access the service and it balances the loads between the instances of ollama with multiple queues. @ParisNeo Do you have a link for me please? Also how does that works from an architectur point of view? I guessing sticky-session, so people usng this wont get mixed up and have a english cooking recipe with c++ snippets and german dictionaries in there right?
Author
Owner

@m4r1k commented on GitHub (Mar 26, 2024):

While the ollama_proxy_server is a valuable starting point, Ollama's lack of native parallel request handling hinders its scalability. This is particularly evident in cloud environments where Ollama pods/instances could dynamically scale based on load.

The current approach in ollama_proxy_server requires hardcoded backend configuration, complicating consistent and dynamic scaling.

<!-- gh-comment-id:2020259515 --> @m4r1k commented on GitHub (Mar 26, 2024): While the [`ollama_proxy_server`](https://github.com/ParisNeo/ollama_proxy_server) is a valuable starting point, Ollama's lack of native parallel request handling hinders its scalability. This is particularly evident in cloud environments where Ollama pods/instances could dynamically scale based on load. The current approach in `ollama_proxy_server` requires hardcoded backend configuration, complicating consistent and dynamic scaling.
Author
Owner

@0x77dev commented on GitHub (Mar 27, 2024):

This issue seems to be stale for a while. Ollama is a promising project with the potential to expand beyond just "running on laptops." I believe there are many projects that wish to use it in production despite facing challenges (pre-pulling a model on start, custom model registry, etc), with parallel requests being a significant obstacle in my opinion.

Merged PR #3348 has newer version of llama.cpp, where parallel decoding is supported for sure.

I will try to delve into the Ollama codebase to address this issue. It would be helpful if a maintainer or someone familiar with the /llm section of the codebase could identify any blockers, maybe potential blocking of parallel processing of requests on the Golang side.

Related:

<!-- gh-comment-id:2022394098 --> @0x77dev commented on GitHub (Mar 27, 2024): [This issue seems to be stale for a while.](https://github.com/ollama/ollama/issues/358#issuecomment-2010280146) Ollama is a promising project with the potential to expand beyond just "running on laptops." I believe there are many projects that wish [to use it in production despite facing challenges](https://github.com/ollama/ollama/issues/358#issuecomment-2010706535) ([pre-pulling a model on start](https://github.com/ollama/ollama/issues/3369#issue-2210418734), custom model registry, etc), with parallel requests being a significant obstacle in my opinion. Merged PR #3348 has newer version of llama.cpp, where parallel decoding is supported for sure. I will try to delve into the Ollama codebase to address this issue. It would be helpful if a maintainer or someone familiar with the `/llm` section of the codebase could identify any blockers, maybe potential blocking of parallel processing of requests on the Golang side. Related: - [Discord thread](https://discordapp.com/channels/1128867683291627614/1128867684130508875/1222495299113713758) - https://github.com/ollama/ollama/issues/380#issuecomment-1762925095 - The [llama.cpp server example](https://github.com/ggerganov/llama.cpp/tree/master/examples/server#llamacpp-http-server) demonstrates support for parallel decoding.
Author
Owner

@0x77dev commented on GitHub (Mar 28, 2024):

There has been some progress, but there are still issues to address. For instance, support for calling different models simultaneously (maybe calling different models simultaneously can be skipped, but it may cause DX confusion), and loaded.mu.{Lock,Unlock}() is not implemented correctly in my changes below.

This will require more testing and work on a better design for model loading

https://github.com/ollama/ollama/assets/46429701/00d84851-ca18-4e0a-a939-be367d49226d

git diff
diff --git a/llm/dyn_ext_server.go b/llm/dyn_ext_server.go
index 6e43333..5b0e435 100644
--- a/llm/dyn_ext_server.go
+++ b/llm/dyn_ext_server.go
@@ -95,7 +95,7 @@ func newDynExtServer(library, model string, adapters, projectors []string, opts
 	sparams.n_batch = C.uint(opts.NumBatch)
 	sparams.n_gpu_layers = C.int(opts.NumGPU)
 	sparams.main_gpu = C.int(opts.MainGPU)
-	sparams.n_parallel = 1 // TODO - wire up concurrency
+	sparams.n_parallel = 4 // TODO - wire up concurrency
 
 	// Always use the value encoded in the model
 	sparams.rope_freq_base = 0.0
diff --git a/server/routes.go b/server/routes.go
index 5582f3a..1ce55e6 100644
--- a/server/routes.go
+++ b/server/routes.go
@@ -67,8 +67,10 @@ var loaded struct {
 
 var defaultSessionDuration = 5 * time.Minute
 
-// load a model into memory if it is not already loaded, it is up to the caller to lock loaded.mu before calling this function
 func load(c *gin.Context, model *Model, opts api.Options, sessionDuration time.Duration) error {
+	loaded.mu.Lock()
+	defer loaded.mu.Unlock()
+
 	needLoad := loaded.runner == nil || // is there a model loaded?
 		loaded.ModelPath != model.ModelPath || // has the base model changed?
 		!reflect.DeepEqual(loaded.AdapterPaths, model.AdapterPaths) || // have the adapters changed?
@@ -145,9 +147,6 @@ func isSupportedImageType(image []byte) bool {
 }
 
 func GenerateHandler(c *gin.Context) {
-	loaded.mu.Lock()
-	defer loaded.mu.Unlock()
-
 	checkpointStart := time.Now()
 	var req api.GenerateRequest
 	err := c.ShouldBindJSON(&req)
<!-- gh-comment-id:2024554016 --> @0x77dev commented on GitHub (Mar 28, 2024): There has been some progress, but there are still issues to address. For instance, support for calling different models simultaneously (maybe calling different models simultaneously can be skipped, but it may cause _DX confusion_), and `loaded.mu.{Lock,Unlock}()` is not implemented correctly in my changes below. This will require more testing and work on a better design for model loading https://github.com/ollama/ollama/assets/46429701/00d84851-ca18-4e0a-a939-be367d49226d <details> <summary> git diff </summary> ```diff diff --git a/llm/dyn_ext_server.go b/llm/dyn_ext_server.go index 6e43333..5b0e435 100644 --- a/llm/dyn_ext_server.go +++ b/llm/dyn_ext_server.go @@ -95,7 +95,7 @@ func newDynExtServer(library, model string, adapters, projectors []string, opts sparams.n_batch = C.uint(opts.NumBatch) sparams.n_gpu_layers = C.int(opts.NumGPU) sparams.main_gpu = C.int(opts.MainGPU) - sparams.n_parallel = 1 // TODO - wire up concurrency + sparams.n_parallel = 4 // TODO - wire up concurrency // Always use the value encoded in the model sparams.rope_freq_base = 0.0 diff --git a/server/routes.go b/server/routes.go index 5582f3a..1ce55e6 100644 --- a/server/routes.go +++ b/server/routes.go @@ -67,8 +67,10 @@ var loaded struct { var defaultSessionDuration = 5 * time.Minute -// load a model into memory if it is not already loaded, it is up to the caller to lock loaded.mu before calling this function func load(c *gin.Context, model *Model, opts api.Options, sessionDuration time.Duration) error { + loaded.mu.Lock() + defer loaded.mu.Unlock() + needLoad := loaded.runner == nil || // is there a model loaded? loaded.ModelPath != model.ModelPath || // has the base model changed? !reflect.DeepEqual(loaded.AdapterPaths, model.AdapterPaths) || // have the adapters changed? @@ -145,9 +147,6 @@ func isSupportedImageType(image []byte) bool { } func GenerateHandler(c *gin.Context) { - loaded.mu.Lock() - defer loaded.mu.Unlock() - checkpointStart := time.Now() var req api.GenerateRequest err := c.ShouldBindJSON(&req) ``` </details>
Author
Owner

@easp commented on GitHub (Mar 30, 2024):

Looks relevant: https://github.com/ollama/ollama/pull/3418

<!-- gh-comment-id:2028468980 --> @easp commented on GitHub (Mar 30, 2024): Looks relevant: https://github.com/ollama/ollama/pull/3418
Author
Owner

@darwinvelez58 commented on GitHub (Apr 9, 2024):

any update?

<!-- gh-comment-id:2043966473 --> @darwinvelez58 commented on GitHub (Apr 9, 2024): any update?
Author
Owner

@0x77dev commented on GitHub (Apr 9, 2024):

@darwinvelez58 there is some work being done in https://github.com/ollama/ollama/pull/3418 and a9195b3efa

was testing both options, at the moment seems very unstable

<!-- gh-comment-id:2043977502 --> @0x77dev commented on GitHub (Apr 9, 2024): @darwinvelez58 there is some work being done in https://github.com/ollama/ollama/pull/3418 ~~and https://github.com/Adphi/ollama/commit/a9195b3efa3a63ea8dee2907ecc7c06dccc59e6f~~ was testing both options, at the moment seems very unstable
Author
Owner

@ParisNeo commented on GitHub (Apr 9, 2024):

In the meantime you can try my ollama proxy :) It can be configured to fire up multiple ollama services, then you can also define users with keys to access the service and it balances the loads between the instances of ollama with multiple queues.

@ParisNeo Do you have a link for me please? Also how does that works from an architectur point of view? I guessing sticky-session, so people usng this wont get mixed up and have a english cooking recipe with c++ snippets and german dictionaries in there right?

Hi, sorry I was very very buzy lately and had no time to look at the github messages.
The proxy server can be found here:
https://github.com/ParisNeo/ollama_proxy_server

It has also an optional security to allow you to have a users base with a personal key for each user.
The architecture is simple:
You specify multiple ollama instances viewable only on the server side each one has a different port number, then the proxy manages both the authentication of the users, the logging of their access, and distribution of the users over multiple queues.
Each queue is behind one instance of ollama and the proxy will always put you in the least filled queue.
So if you run 4 ollama instances, you can simultaniously serve 4 users and the others will be queued.

I agree that you can do more if ollama manages this all inside to mutualize the weights and stuff. I have built my proxy because I needed the security and the management and back then ollama had no plans to do this so got to move, but I'll be happy if they integrate multiusers directly inside.

<!-- gh-comment-id:2044774847 --> @ParisNeo commented on GitHub (Apr 9, 2024): > > In the meantime you can try my ollama proxy :) It can be configured to fire up multiple ollama services, then you can also define users with keys to access the service and it balances the loads between the instances of ollama with multiple queues. > > @ParisNeo Do you have a link for me please? Also how does that works from an architectur point of view? I guessing sticky-session, so people usng this wont get mixed up and have a english cooking recipe with c++ snippets and german dictionaries in there right? Hi, sorry I was very very buzy lately and had no time to look at the github messages. The proxy server can be found here: [https://github.com/ParisNeo/ollama_proxy_server](https://github.com/ParisNeo/ollama_proxy_server) It has also an optional security to allow you to have a users base with a personal key for each user. The architecture is simple: You specify multiple ollama instances viewable only on the server side each one has a different port number, then the proxy manages both the authentication of the users, the logging of their access, and distribution of the users over multiple queues. Each queue is behind one instance of ollama and the proxy will always put you in the least filled queue. So if you run 4 ollama instances, you can simultaniously serve 4 users and the others will be queued. I agree that you can do more if ollama manages this all inside to mutualize the weights and stuff. I have built my proxy because I needed the security and the management and back then ollama had no plans to do this so got to move, but I'll be happy if they integrate multiusers directly inside.
Author
Owner

@TheMasterFX commented on GitHub (Apr 10, 2024):

vLLM uses a PagedAttention. Is this a part which must be integrated in ollama or in the llama.cpp part?

<!-- gh-comment-id:2048230440 --> @TheMasterFX commented on GitHub (Apr 10, 2024): vLLM uses a [PagedAttention](https://blog.vllm.ai/2023/06/20/vllm.html). Is this a part which must be integrated in ollama or in the llama.cpp part?
Author
Owner

@0x77dev commented on GitHub (Apr 10, 2024):

@TheMasterFX this is llama.cpp side of things

<!-- gh-comment-id:2048276520 --> @0x77dev commented on GitHub (Apr 10, 2024): @TheMasterFX this is llama.cpp side of things
Author
Owner

@TheMasterFX commented on GitHub (Apr 12, 2024):

Seems like there are improvements coming:
https://twitter.com/AlexReibman/status/1778695203957977148

<!-- gh-comment-id:2052218953 --> @TheMasterFX commented on GitHub (Apr 12, 2024): Seems like there are improvements coming: https://twitter.com/AlexReibman/status/1778695203957977148
Author
Owner

@guitmonk-1290 commented on GitHub (Apr 21, 2024):

I was reading ray serve's docs and it seems they support Dynamic Request Batching, which creates a queue system.
https://docs.ray.io/en/latest/serve/advanced-guides/dyn-req-batch.html

I am new to this, so can we use this with Ollama for batch inference?

<!-- gh-comment-id:2068145664 --> @guitmonk-1290 commented on GitHub (Apr 21, 2024): I was reading ray serve's docs and it seems they support Dynamic Request Batching, which creates a queue system. https://docs.ray.io/en/latest/serve/advanced-guides/dyn-req-batch.html I am new to this, so can we use this with Ollama for batch inference?
Author
Owner

@jpmcb commented on GitHub (Apr 24, 2024):

I've had some success load balancing multiple Ollama instances on Kubernetes with a pool of GPU nodes:

❯ kubectl get nodes -A
NAME                     STATUS   ROLES    AGE   VERSION
defaultpool-88943984-0   Ready    <none>   5d    v1.29.2
defaultpool-88943984-1   Ready    <none>   5d    v1.29.2
gpupool-42074538-0       Ready    <none>   41h   v1.29.2
gpupool-42074538-1       Ready    <none>   41h   v1.29.2
gpupool-42074538-2       Ready    <none>   41h   v1.29.2
gpupool-42074538-3       Ready    <none>   41h   v1.29.2
gpupool-42074538-4       Ready    <none>   41h   v1.29.2

Notice the 5 "gpupool" nodes, each with a T4 nvidia GPU. Ollama is then deployed as a daemonset on each of the GPU nodes:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: ollama-daemonset
  namespace: ollama-ns
spec:
  selector:
    matchLabels:
      app: ollama
  template:
    metadata:
      labels:
        app: ollama
    spec:
      containers:
      - image: ollama/ollama:latest
        imagePullPolicy: Always
        lifecycle:
          postStart:
            exec:
              command:
              - ollama
              - pull
              - llama3
        name: ollama
        ports:
        - containerPort: 11434
          protocol: TCP

      # A few special selectors / tolerations to schedule on the GPU nodes
      nodeSelector:
        accelerator: nvidia
      tolerations:
      - effect: NoSchedule
        key: sku
        operator: Equal
        value: gpu

These then can be load balanced behind a Kubernetes service with the ClusterIP being exposed internally "as a service" to the rest of the cluster:

apiVersion: v1
kind: Service
metadata:
  name: ollama-service
  namespace: ollama-ns
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 11434
  selector:
    app: ollama
  sessionAffinity: None
  type: ClusterIP

Inside the cluster, I can hit the load balancing service resolved by CoreDNS:

$ curl ollama-service.ollama-ns.svc.cluster.local
Ollama is running

This works pretty well for slower requests but has 2 problems:

  • The scaling of this is directly tied to the number of nodes (and in this case, expensive GPU nodes) that have daemonsets on them
  • Kubernetes load balancing isn't perfect and could very easily send a request to a "busy" daemonset pod that already is servicing a request.
    • I believe when this happens, the whole pod seems to stale and doesn't print any additional logs, just seems to hang. Might be worth some more experimentation to see if there's a smoking gun when multiple requests come through

It'd be absolutely incredible if multiple requests could be serviced by a single ollama serve: It would take this example daemonset deployment with a pool of GPUs and make it much more scalable.

Thanks for all the amazing work on ollama - I'm happy to test anything out and report back if there's a ollama/ollama:concurrency test image or if there's code reviews needed ❤️

<!-- gh-comment-id:2075077413 --> @jpmcb commented on GitHub (Apr 24, 2024): I've had some success load balancing multiple Ollama instances on Kubernetes with a pool of GPU nodes: ``` ❯ kubectl get nodes -A NAME STATUS ROLES AGE VERSION defaultpool-88943984-0 Ready <none> 5d v1.29.2 defaultpool-88943984-1 Ready <none> 5d v1.29.2 gpupool-42074538-0 Ready <none> 41h v1.29.2 gpupool-42074538-1 Ready <none> 41h v1.29.2 gpupool-42074538-2 Ready <none> 41h v1.29.2 gpupool-42074538-3 Ready <none> 41h v1.29.2 gpupool-42074538-4 Ready <none> 41h v1.29.2 ``` Notice the 5 "gpupool" nodes, each with a T4 nvidia GPU. Ollama is then deployed as a daemonset on each of the GPU nodes: ```yaml apiVersion: apps/v1 kind: DaemonSet metadata: name: ollama-daemonset namespace: ollama-ns spec: selector: matchLabels: app: ollama template: metadata: labels: app: ollama spec: containers: - image: ollama/ollama:latest imagePullPolicy: Always lifecycle: postStart: exec: command: - ollama - pull - llama3 name: ollama ports: - containerPort: 11434 protocol: TCP # A few special selectors / tolerations to schedule on the GPU nodes nodeSelector: accelerator: nvidia tolerations: - effect: NoSchedule key: sku operator: Equal value: gpu ``` These then can be load balanced behind a Kubernetes service with the ClusterIP being exposed internally "as a service" to the rest of the cluster: ```yaml apiVersion: v1 kind: Service metadata: name: ollama-service namespace: ollama-ns spec: ports: - port: 80 protocol: TCP targetPort: 11434 selector: app: ollama sessionAffinity: None type: ClusterIP ``` Inside the cluster, I can hit the load balancing service resolved by CoreDNS: ``` $ curl ollama-service.ollama-ns.svc.cluster.local Ollama is running ``` --- This works _pretty well_ for slower requests but has 2 problems: - The scaling of this is directly tied to the number of nodes (and in this case, expensive GPU nodes) that have daemonsets on them - Kubernetes load balancing isn't perfect and could very easily send a request to a "busy" daemonset pod that already is servicing a request. - I believe when this happens, the whole pod seems to stale and doesn't print any additional logs, just seems to hang. Might be worth some more experimentation to see if there's a smoking gun when multiple requests come through It'd be absolutely incredible if multiple requests could be serviced by a single `ollama serve`: It would take this example daemonset deployment with a pool of GPUs and make it much more scalable. Thanks for all the amazing work on ollama - I'm happy to test anything out and report back if there's a `ollama/ollama:concurrency` test image or if there's code reviews needed ❤️
Author
Owner

@skye0402 commented on GitHub (Apr 26, 2024):

I'm with vLLM now for that use case, it's very fast and manages concurrency plus has a big model pool and openAI compatible API.
I'm still with Ollama for trying out new models that's where it really shines and saves time.

<!-- gh-comment-id:2078683079 --> @skye0402 commented on GitHub (Apr 26, 2024): I'm with vLLM now for that use case, it's very fast and manages concurrency plus has a big model pool and openAI compatible API. I'm still with Ollama for trying out new models that's where it really shines and saves time.
Author
Owner

@shing100 commented on GitHub (Apr 29, 2024):

I really wish this feature was added.

<!-- gh-comment-id:2081896147 --> @shing100 commented on GitHub (Apr 29, 2024): I really wish this feature was added.
Author
Owner

@iakashpaul commented on GitHub (Apr 29, 2024):

For anyone else stumbling onto this thread-

To use webUI with parallel chat generation on multiple clients use TGI or server.cpp/duplicate my HF Space onto a GPU instance directly with parallel flag set as 2 or more basis your VRAM, then run the ollama container with server.cpp acting as an OpenAI replacement as shown below.

Server.cpp

 ./server -m ./models/mistral-7b-instruct-v0.2.Q8_0.gguf -ngl 30 --host 127.0.0.1 --port 8080 --parallel 2

Ollama container

 docker run  -p 3000:8080   -v open-webui:/app/backend/data   -e OPENAI_API_BASE_URLS="http://127.0.0.1:8080/v1"   -e OPENAI_API_KEYS="randomplaceholder"   --restart always   ghcr.io/open-webui/open-webui:main

cc: @ehartford

Latest release has experimental flags,

OLLAMA_NUM_PARALLEL=4 OLLAMA_MAX_LOADED_MODELS=4 ollama serve
<!-- gh-comment-id:2082599253 --> @iakashpaul commented on GitHub (Apr 29, 2024): ## For anyone else stumbling onto this thread- To use webUI with parallel chat generation on multiple clients use [TGI or server.cpp/duplicate my HF Space onto a GPU instance](https://huggingface.co/spaces/iAkashPaul/Ghudsavar) directly with parallel flag set as 2 or more basis your VRAM, then run the ollama container with server.cpp acting as an OpenAI replacement as shown below. > Server.cpp ```bash ./server -m ./models/mistral-7b-instruct-v0.2.Q8_0.gguf -ngl 30 --host 127.0.0.1 --port 8080 --parallel 2 ``` > Ollama container ```bash docker run -p 3000:8080 -v open-webui:/app/backend/data -e OPENAI_API_BASE_URLS="http://127.0.0.1:8080/v1" -e OPENAI_API_KEYS="randomplaceholder" --restart always ghcr.io/open-webui/open-webui:main ``` cc: @ehartford ----- Latest [release](https://github.com/ollama/ollama/releases/tag/v0.1.33-rc5) has experimental flags, ``` OLLAMA_NUM_PARALLEL=4 OLLAMA_MAX_LOADED_MODELS=4 ollama serve ```
Author
Owner

@mili-tan commented on GitHub (Apr 29, 2024):

https://github.com/ollama/ollama/releases/tag/v0.1.33-rc5

<!-- gh-comment-id:2082615180 --> @mili-tan commented on GitHub (Apr 29, 2024): [https://github.com/ollama/ollama/releases/tag/v0.1.33-rc5](https://github.com/ollama/ollama/releases/tag/v0.1.33-rc5)
Author
Owner

@hanzlaramey commented on GitHub (Apr 29, 2024):

game changer!!! Thank you @mili-tan

<!-- gh-comment-id:2083008704 --> @hanzlaramey commented on GitHub (Apr 29, 2024): game changer!!! Thank you @mili-tan
Author
Owner

@ehartford commented on GitHub (Apr 30, 2024):

Thank you!

<!-- gh-comment-id:2084007279 --> @ehartford commented on GitHub (Apr 30, 2024): Thank you!
Author
Owner

@ehartford commented on GitHub (Apr 30, 2024):

If I don't want to limit the number of loaded models, I just don't set that variable?

<!-- gh-comment-id:2084008979 --> @ehartford commented on GitHub (Apr 30, 2024): If I don't want to limit the number of loaded models, I just don't set that variable?
Author
Owner

@BBjie commented on GitHub (Apr 30, 2024):

My question is for the concurrency feature, can I edit and add them into my compose-up file.

ollama:
    image: ollama/ollama:latest
    container_name: ollama
    ports:
      - "11434:11434"

    
    volumes:
      - ollama:/root/.ollama
    deploy:
      resources:
        reservations:
          devices:
            - capabilities: [gpu]
    command: serve
volumes:
  ollama:

or is there other way to pass the value in for
OLLAMA_NUM_PARALLEL=4 OLLAMA_MAX_LOADED_MODELS=4 ollama serve

<!-- gh-comment-id:2085348731 --> @BBjie commented on GitHub (Apr 30, 2024): My question is for the concurrency feature, can I edit and add them into my compose-up file. ``` ollama: image: ollama/ollama:latest container_name: ollama ports: - "11434:11434" volumes: - ollama:/root/.ollama deploy: resources: reservations: devices: - capabilities: [gpu] command: serve volumes: ollama: ``` or is there other way to pass the value in for `OLLAMA_NUM_PARALLEL=4 OLLAMA_MAX_LOADED_MODELS=4 ollama serve`
Author
Owner

@jammsen commented on GitHub (Apr 30, 2024):

My question is for the concurrency feature, can I edit and add them into my compose-up file.

ollama:
    image: ollama/ollama:latest
    container_name: ollama
    ports:
      - "11434:11434"

    
    volumes:
      - ollama:/root/.ollama
    deploy:
      resources:
        reservations:
          devices:
            - capabilities: [gpu]
    command: serve
volumes:
  ollama:

or is there other way to pass the value in for OLLAMA_NUM_PARALLEL=4 OLLAMA_MAX_LOADED_MODELS=4 ollama serve

I think thoose are 2 different things, the ENVs should be passed to ollama serve when you run the native app on your system. Not sure though how you can use this in a Docker Image of ollama @jmorganca could you clear this up for us please?

<!-- gh-comment-id:2085372347 --> @jammsen commented on GitHub (Apr 30, 2024): > My question is for the concurrency feature, can I edit and add them into my compose-up file. > > ``` > ollama: > image: ollama/ollama:latest > container_name: ollama > ports: > - "11434:11434" > > > volumes: > - ollama:/root/.ollama > deploy: > resources: > reservations: > devices: > - capabilities: [gpu] > command: serve > volumes: > ollama: > ``` > > or is there other way to pass the value in for `OLLAMA_NUM_PARALLEL=4 OLLAMA_MAX_LOADED_MODELS=4 ollama serve` I think thoose are 2 different things, the ENVs should be passed to ollama serve when you run the native app on your system. Not sure though how you can use this in a Docker Image of ollama @jmorganca could you clear this up for us please?
Author
Owner

@BBjie commented on GitHub (Apr 30, 2024):

Have not tested but should work:


ollama:

    image: ollama/ollama:latest

    container_name: ollama
    environment:
      - OLLAMA_NUM_PARALLEL=4
      - OLLAMA_MAX_LOADED_MODELS=4
...

I tested it one hour ago, it was not working...

<!-- gh-comment-id:2085628261 --> @BBjie commented on GitHub (Apr 30, 2024): > Have not tested but should work: > > > ``` > > > > ollama: > > > > image: ollama/ollama:latest > > > > container_name: ollama > > environment: > > - OLLAMA_NUM_PARALLEL=4 > > - OLLAMA_MAX_LOADED_MODELS=4 > > ... > > ``` I tested it one hour ago, it was not working...
Author
Owner

@laktosterror commented on GitHub (Apr 30, 2024):

Sorry you might need to get the pre-release image. Corrected previous answear.

<!-- gh-comment-id:2085653806 --> @laktosterror commented on GitHub (Apr 30, 2024): Sorry you might need to get the pre-release image. Corrected previous answear.
Author
Owner

@laktosterror commented on GitHub (Apr 30, 2024):

On mobile. I seem to have messed up my original post.

Try this:

ollama:
    image: ollama/ollama:0.1.33-rc5
    container_name: ollama
    environment:
      - OLLAMA_NUM_PARALLEL=4
      - OLLAMA_MAX_LOADED_MODELS=4
...
<!-- gh-comment-id:2085726491 --> @laktosterror commented on GitHub (Apr 30, 2024): On mobile. I seem to have messed up my original post. Try this: ``` ollama: image: ollama/ollama:0.1.33-rc5 container_name: ollama environment: - OLLAMA_NUM_PARALLEL=4 - OLLAMA_MAX_LOADED_MODELS=4 ... ```
Author
Owner

@BBjie commented on GitHub (Apr 30, 2024):

On mobile. I seem to have messed up my original post.

Try this:

ollama:
    image: ollama/ollama:0.1.33-rc5
    container_name: ollama
    environment:
      - OLLAMA_NUM_PARALLEL=4
      - OLLAMA_MAX_LOADED_MODELS=4
...
ollama  | {"function":"log_server_request","level":"INFO","line":2734,"method":"POST","msg":"request","params":{},"path":"/tokenize","remote_addr":"127.0.0.1","remote_port":46394,"status":200,"tid":"140623411642368","timestamp":1714493452}
ollama  | {"function":"process_single_task","level":"INFO","line":1506,"msg":"slot data","n_idle_slots":4,"n_processing_slots":0,"task_id":399,"tid":"140624811528192","timestamp":1714493452}
ollama  | {"function":"log_server_request","level":"INFO","line":2734,"method":"GET","msg":"request","params":{},"path":"/health","remote_addr":"127.0.0.1","remote_port":46410,"status":200,"tid":"140623403249664","timestamp":1714493452}
ollama  | {"function":"log_server_request","level":"INFO","line":2734,"method":"POST","msg":"request","params":{},"path":"/tokenize","remote_addr":"127.0.0.1","remote_port":46410,"status":200,"tid":"140623403249664","timestamp":1714493452}
ollama  | {"function":"process_single_task","level":"INFO","line":1506,"msg":"slot data","n_idle_slots":4,"n_processing_slots":0,"task_id":400,"tid":"140624811528192","timestamp":1714493452}
ollama  | {"function":"log_server_request","level":"INFO","line":2734,"method":"GET","msg":"request","params":{},"path":"/health","remote_addr":"127.0.0.1","remote_port":46410,"status":200,"tid":"140623403249664","timestamp":1ollama  | {"function":"print_timings","level":"INFO","line":269,"msg":"prompt eval time     =     991.40 ms /   713 tokens (    1.39 ms per token,   719.19 tokens per second)","n_prompt_tokens_processed":713,"n_tokens_second":719.1864417721239,"slot_id":1,"t_prompt_processing":991.398,"t_token":1.3904600280504908,"task_id":402,"tid":"140624811528192","timestamp":1714493454}
ollama  | {"function":"print_timings","level":"INFO","line":283,"msg":"generation eval time =    1251.42 ms /    56 runs   (   22.35 ms per token,    44.75 tokens per second)","n_decoded":56,"n_tokens_second":44.7492364661528,"slot_id":1,"t_token":22.346749999999997,"t_token_generation":1251.418,"task_id":402,"tid":"140624811528192","timestamp":1714493454}
ollama  | {"function":"print_timings","level":"INFO","line":293,"msg":"          total time =    2242.82 ms","slot_id":1,"t_prompt_processing":991.398,"t_token_generation":1251.418,"t_total":2242.816,"task_id":402,"tid":"140624811528192","timestamp":1714493454}
ollama  | {"function":"update_slots","level":"INFO","line":1640,"msg":"slot released","n_cache_tokens":832,"n_ctx":3904,"n_past":831,"n_system_tokens":0,"slot_id":1,"task_id":402,"tid":"140624811528192","timestamp":1714493454,"truncated":false}
ollama  | {"function":"log_server_request","level":"INFO","line":2734,"method":"POST","msg":"request","params":{},"path":"/completion","remote_addr":"127.0.0.1","remote_port":46416,"status":200,"tid":"140623966629888","timestamp":1714493454}
ollama  | [GIN] 2024/04/30 - 16:10:54 | 200 |  2.546651478s |      172.19.0.4 | POST     "/api/chat"

Thhanks, but I believe it still not working as expected..

<!-- gh-comment-id:2085822869 --> @BBjie commented on GitHub (Apr 30, 2024): > On mobile. I seem to have messed up my original post. > > Try this: > > ``` > ollama: > image: ollama/ollama:0.1.33-rc5 > container_name: ollama > environment: > - OLLAMA_NUM_PARALLEL=4 > - OLLAMA_MAX_LOADED_MODELS=4 > ... > ``` ``` ollama | {"function":"log_server_request","level":"INFO","line":2734,"method":"POST","msg":"request","params":{},"path":"/tokenize","remote_addr":"127.0.0.1","remote_port":46394,"status":200,"tid":"140623411642368","timestamp":1714493452} ollama | {"function":"process_single_task","level":"INFO","line":1506,"msg":"slot data","n_idle_slots":4,"n_processing_slots":0,"task_id":399,"tid":"140624811528192","timestamp":1714493452} ollama | {"function":"log_server_request","level":"INFO","line":2734,"method":"GET","msg":"request","params":{},"path":"/health","remote_addr":"127.0.0.1","remote_port":46410,"status":200,"tid":"140623403249664","timestamp":1714493452} ollama | {"function":"log_server_request","level":"INFO","line":2734,"method":"POST","msg":"request","params":{},"path":"/tokenize","remote_addr":"127.0.0.1","remote_port":46410,"status":200,"tid":"140623403249664","timestamp":1714493452} ollama | {"function":"process_single_task","level":"INFO","line":1506,"msg":"slot data","n_idle_slots":4,"n_processing_slots":0,"task_id":400,"tid":"140624811528192","timestamp":1714493452} ollama | {"function":"log_server_request","level":"INFO","line":2734,"method":"GET","msg":"request","params":{},"path":"/health","remote_addr":"127.0.0.1","remote_port":46410,"status":200,"tid":"140623403249664","timestamp":1ollama | {"function":"print_timings","level":"INFO","line":269,"msg":"prompt eval time = 991.40 ms / 713 tokens ( 1.39 ms per token, 719.19 tokens per second)","n_prompt_tokens_processed":713,"n_tokens_second":719.1864417721239,"slot_id":1,"t_prompt_processing":991.398,"t_token":1.3904600280504908,"task_id":402,"tid":"140624811528192","timestamp":1714493454} ollama | {"function":"print_timings","level":"INFO","line":283,"msg":"generation eval time = 1251.42 ms / 56 runs ( 22.35 ms per token, 44.75 tokens per second)","n_decoded":56,"n_tokens_second":44.7492364661528,"slot_id":1,"t_token":22.346749999999997,"t_token_generation":1251.418,"task_id":402,"tid":"140624811528192","timestamp":1714493454} ollama | {"function":"print_timings","level":"INFO","line":293,"msg":" total time = 2242.82 ms","slot_id":1,"t_prompt_processing":991.398,"t_token_generation":1251.418,"t_total":2242.816,"task_id":402,"tid":"140624811528192","timestamp":1714493454} ollama | {"function":"update_slots","level":"INFO","line":1640,"msg":"slot released","n_cache_tokens":832,"n_ctx":3904,"n_past":831,"n_system_tokens":0,"slot_id":1,"task_id":402,"tid":"140624811528192","timestamp":1714493454,"truncated":false} ollama | {"function":"log_server_request","level":"INFO","line":2734,"method":"POST","msg":"request","params":{},"path":"/completion","remote_addr":"127.0.0.1","remote_port":46416,"status":200,"tid":"140623966629888","timestamp":1714493454} ollama | [GIN] 2024/04/30 - 16:10:54 | 200 | 2.546651478s | 172.19.0.4 | POST "/api/chat" ``` Thhanks, but I believe it still not working as expected..
Author
Owner

@dhiltgen commented on GitHub (May 2, 2024):

I'm going to close this as fixed now in 0.1.33. As commenters above have pointed out, it's opt-in for now, but we do intend to eventually do concurrency automatically without requiring the env vars.

To clarify some questions above: There are 2 layers of concurrency. There's OLLAMA_NUM_PARALLEL which controls how many requests can be answered against a single loaded model, and there's OLLAMA_MAX_LOADED_MODELS which controls how many models can be loaded at the same time, up to the limits of VRAM. (note: we do not support loading two or more copies of the same model)

https://github.com/ollama/ollama/releases

<!-- gh-comment-id:2091482644 --> @dhiltgen commented on GitHub (May 2, 2024): I'm going to close this as fixed now in 0.1.33. As commenters above have pointed out, it's opt-in for now, but we do intend to eventually do concurrency automatically without requiring the env vars. To clarify some questions above: There are 2 layers of concurrency. There's `OLLAMA_NUM_PARALLEL` which controls how many requests can be answered against a single loaded model, and there's `OLLAMA_MAX_LOADED_MODELS` which controls how many models can be loaded at the same time, up to the limits of VRAM. (note: we do not support loading two or more copies of the same model) https://github.com/ollama/ollama/releases
Author
Owner

@taozhiyuai commented on GitHub (May 3, 2024):

I'm going to close this as fixed now in 0.1.33. As commenters above have pointed out, it's opt-in for now, but we do intend to eventually do concurrency automatically without requiring the env vars.

To clarify some questions above: There are 2 layers of concurrency. There's OLLAMA_NUM_PARALLEL which controls how many requests can be answered against a single loaded model, and there's OLLAMA_MAX_LOADED_MODELS which controls how many models can be loaded at the same time, up to the limits of VRAM. (note: we do not support loading two or more copies of the same model)

https://github.com/ollama/ollama/releases

@dhiltgen
I install ollama on Mac.
are these the default value for this two env?OLLAMA_NUM_PARALLEL=4 OLLAMA_MAX_LOADED_MODELS=4.

the following should work, right? :)
t@603e5f4a42f1 ~ % launchctl setenv OLLAMA_NUM_PARALLEL 5
t@603e5f4a42f1 ~ % launchctl setenv OLLAMA_MAX_LOADED_MODELS 5

to check the value of ENVs :)
launchctl getenv OLLAMA_NUM_PARALLEL
launchctl getenv OLLAMA_MAX_LOADED_MODELS

restart ollama to activate new value of ENVs. :) very nice!

<!-- gh-comment-id:2092047353 --> @taozhiyuai commented on GitHub (May 3, 2024): > I'm going to close this as fixed now in 0.1.33. As commenters above have pointed out, it's opt-in for now, but we do intend to eventually do concurrency automatically without requiring the env vars. > > To clarify some questions above: There are 2 layers of concurrency. There's `OLLAMA_NUM_PARALLEL` which controls how many requests can be answered against a single loaded model, and there's `OLLAMA_MAX_LOADED_MODELS` which controls how many models can be loaded at the same time, up to the limits of VRAM. (note: we do not support loading two or more copies of the same model) > > https://github.com/ollama/ollama/releases @dhiltgen I install ollama on Mac. are these the default value for this two env?OLLAMA_NUM_PARALLEL=4 OLLAMA_MAX_LOADED_MODELS=4. the following should work, right? :) t@603e5f4a42f1 ~ % launchctl setenv OLLAMA_NUM_PARALLEL 5 t@603e5f4a42f1 ~ % launchctl setenv OLLAMA_MAX_LOADED_MODELS 5 to check the value of ENVs :) launchctl getenv OLLAMA_NUM_PARALLEL launchctl getenv OLLAMA_MAX_LOADED_MODELS restart ollama to activate new value of ENVs. :) very nice!
Author
Owner

@dhiltgen commented on GitHub (May 4, 2024):

The default values for these settings in 0.1.33 retain existing behavior of only 1 request at a time, and only 1 model at a time. In a future version we plan to adjust the default to enable this automatically, but until then, yes, if you want to use concurrency, you'll have to set these environment variables on the server.

<!-- gh-comment-id:2094402641 --> @dhiltgen commented on GitHub (May 4, 2024): The default values for these settings in 0.1.33 retain existing behavior of only 1 request at a time, and only 1 model at a time. In a future version we plan to adjust the default to enable this automatically, but until then, yes, if you want to use concurrency, you'll have to set these environment variables on the server.
Author
Owner

@murillojndem commented on GitHub (May 21, 2024):

How do I set 2 models to run on different ports? I want to use crewAI but I need the ports of each one to use them as agents.

<!-- gh-comment-id:2123215376 --> @murillojndem commented on GitHub (May 21, 2024): How do I set 2 models to run on different ports? I want to use crewAI but I need the ports of each one to use them as agents.
Author
Owner

@ITHealer commented on GitHub (May 22, 2024):

How to set env to run parallel in linux?
I had set up env by:

sudo nano ~/.bashrc
export OLLAMA_NUM_PARALLEL=4
source ~/.bashrc

But it doesn't work
Please help me!!!

<!-- gh-comment-id:2124014966 --> @ITHealer commented on GitHub (May 22, 2024): How to set env to run parallel in linux? I had set up env by: sudo nano ~/.bashrc export OLLAMA_NUM_PARALLEL=4 source ~/.bashrc But it doesn't work Please help me!!!
Author
Owner

@Miroun commented on GitHub (May 22, 2024):

How does it work ? I mean. what prevents me to do OLLAMA_NUM_PARALLEL=999999 ?
How can I smartly access this number if I want to use my GPU(s) at full capacity ?

<!-- gh-comment-id:2124654155 --> @Miroun commented on GitHub (May 22, 2024): How does it work ? I mean. what prevents me to do OLLAMA_NUM_PARALLEL=999999 ? How can I smartly access this number if I want to use my GPU(s) at full capacity ?
Author
Owner

@dhiltgen commented on GitHub (May 23, 2024):

@murillojndem the ollama server only binds to a single port, so in your use-case, you'll want to run multiple servers on different ports using the OLLAMA_HOST environment variable to control which port they bind to. Each Ollama instance wont know about the others, so if you only have a single GPU, you may run into OOM problems if you don't sequence model loading to ensure they're not racing, or control the amount of layers they load by explicitly setting num_gpu. If you do have multiple GPUs, you can use the GPU vendor specific GPU Selection variable to dedicate specific GPUs to specific instances. https://github.com/ollama/ollama/blob/main/docs/gpu.md

@ITHealer this variable must be set on the server, not the client. See https://github.com/ollama/ollama/blob/main/docs/faq.md#setting-environment-variables-on-linux

@Miroun our goal is to make this setting automatic in a future release. Until then, it's your responsibility to test your setup to ensure it meets your needs balancing the model size, context size, and your GPU(s) VRAM capacity. The memory allocation on the GPU is impacted by the parallel setting multiplied by context size, so a large parallel with a large context will result in significant VRAM usage. You can experiment and use the new ollama ps to see where the optimal configuration is for your usecase.

<!-- gh-comment-id:2127619132 --> @dhiltgen commented on GitHub (May 23, 2024): @murillojndem the ollama server only binds to a single port, so in your use-case, you'll want to run multiple servers on different ports using the OLLAMA_HOST environment variable to control which port they bind to. Each Ollama instance wont know about the others, so if you only have a single GPU, you may run into OOM problems if you don't sequence model loading to ensure they're not racing, or control the amount of layers they load by explicitly setting `num_gpu`. If you do have multiple GPUs, you can use the GPU vendor specific `GPU Selection` variable to dedicate specific GPUs to specific instances. https://github.com/ollama/ollama/blob/main/docs/gpu.md @ITHealer this variable must be set on the server, not the client. See https://github.com/ollama/ollama/blob/main/docs/faq.md#setting-environment-variables-on-linux @Miroun our goal is to make this setting automatic in a future release. Until then, it's your responsibility to test your setup to ensure it meets your needs balancing the model size, context size, and your GPU(s) VRAM capacity. The memory allocation on the GPU is impacted by the parallel setting multiplied by context size, so a large parallel with a large context will result in significant VRAM usage. You can experiment and use the new `ollama ps` to see where the optimal configuration is for your usecase.
Author
Owner

@murillojndem commented on GitHub (May 24, 2024):

@murillojndem the ollama server only binds to a single port, so in your use-case, you'll want to run multiple servers on different ports using the OLLAMA_HOST environment variable to control which port they bind to. Each Ollama instance wont know about the others, so if you only have a single GPU, you may run into OOM problems if you don't sequence model loading to ensure they're not racing, or control the amount of layers they load by explicitly setting num_gpu. If you do have multiple GPUs, you can use the GPU vendor specific GPU Selection variable to dedicate specific GPUs to specific instances. https://github.com/ollama/ollama/blob/main/docs/gpu.md

@ITHealer this variable must be set on the server, not the client. See https://github.com/ollama/ollama/blob/main/docs/faq.md#setting-environment-variables-on-linux

@Miroun our goal is to make this setting automatic in a future release. Until then, it's your responsibility to test your setup to ensure it meets your needs balancing the model size, context size, and your GPU(s) VRAM capacity. The memory allocation on the GPU is impacted by the parallel setting multiplied by context size, so a large parallel with a large context will result in significant VRAM usage. You can experiment and use the new ollama ps to see where the optimal configuration is for your usecase.

But how do I set the environment variable? Do I set 2 variables each one for one port? It doesn't say in the docs. And they'll be running at the same time but I'll only use one at a time.

<!-- gh-comment-id:2128305194 --> @murillojndem commented on GitHub (May 24, 2024): > @murillojndem the ollama server only binds to a single port, so in your use-case, you'll want to run multiple servers on different ports using the OLLAMA_HOST environment variable to control which port they bind to. Each Ollama instance wont know about the others, so if you only have a single GPU, you may run into OOM problems if you don't sequence model loading to ensure they're not racing, or control the amount of layers they load by explicitly setting `num_gpu`. If you do have multiple GPUs, you can use the GPU vendor specific `GPU Selection` variable to dedicate specific GPUs to specific instances. https://github.com/ollama/ollama/blob/main/docs/gpu.md > > @ITHealer this variable must be set on the server, not the client. See https://github.com/ollama/ollama/blob/main/docs/faq.md#setting-environment-variables-on-linux > > @Miroun our goal is to make this setting automatic in a future release. Until then, it's your responsibility to test your setup to ensure it meets your needs balancing the model size, context size, and your GPU(s) VRAM capacity. The memory allocation on the GPU is impacted by the parallel setting multiplied by context size, so a large parallel with a large context will result in significant VRAM usage. You can experiment and use the new `ollama ps` to see where the optimal configuration is for your usecase. But how do I set the environment variable? Do I set 2 variables each one for one port? It doesn't say in the docs. And they'll be running at the same time but I'll only use one at a time.
Author
Owner

@dhiltgen commented on GitHub (May 25, 2024):

@Miroun I'd suggest the easiest path would be to use 2 different containers, but if you want to run on the host, you'll need to set up 2 different systemd services that have the settings you want.

<!-- gh-comment-id:2131318565 --> @dhiltgen commented on GitHub (May 25, 2024): @Miroun I'd suggest the easiest path would be to use 2 different [containers](https://github.com/ollama/ollama/blob/main/docs/docker.md), but if you want to run on the host, you'll need to set up 2 different [systemd services](https://github.com/ollama/ollama/blob/main/scripts/install.sh#L100) that have the settings you want.
Author
Owner

@goreng2 commented on GitHub (Jun 10, 2024):

@dhiltgen

How did handle multiple requests?

I want to know the principle.

I've looked at the change log, but I'm not sure.

<!-- gh-comment-id:2157716645 --> @goreng2 commented on GitHub (Jun 10, 2024): @dhiltgen How did handle multiple requests? I want to know the principle. I've looked at the change log, but I'm not sure.
Author
Owner

@EthraZa commented on GitHub (Jun 19, 2024):

It took me some minutes to figure it out, but to make things clear to next one searching for it:

1- You can run multiple docker containers in different ports and probably have clear control of the resources for each one.

or

2- You can run multiple LLM in one host:
$ sudo vi /etc/systemd/system/ollama.service

Add env vars to the Service:

Environment="OLLAMA_NUM_PARALLEL=4"
Environment="OLLAMA_MAX_LOADED_MODELS=4"
Environment="PATH=...

Restart services:

sudo systemctl daemon-reload
sudo systemctl restart ollama

Thank you Ollama guys.

<!-- gh-comment-id:2179184518 --> @EthraZa commented on GitHub (Jun 19, 2024): It took me some minutes to figure it out, but to make things clear to next one searching for it: 1- You can run multiple docker containers in different ports and probably have clear control of the resources for each one. or 2- You can run multiple LLM in one host: `$ sudo vi /etc/systemd/system/ollama.service` Add env vars to the Service: ``` Environment="OLLAMA_NUM_PARALLEL=4" Environment="OLLAMA_MAX_LOADED_MODELS=4" Environment="PATH=... ``` Restart services: ``` sudo systemctl daemon-reload sudo systemctl restart ollama ``` Thank you Ollama guys.
Author
Owner

@dhiltgen commented on GitHub (Jun 19, 2024):

@goreng2 while concurrency is in experimental preview, you can enable it with 2 settings
OLLAMA_NUM_PARALLEL sets the number of concurrent requests for each individual loaded model
OLLAMA_MAX_LOADED_MODELS sets the number of models that can be loaded at the same time, provided there is sufficient VRAM to fully load the models. (Partial loads always max at 1 loaded model)

So for example, if you set OLLAMA_NUM_PARALLEL=4 and OLLAMA_MAX_LOADED_MODELS=2 then you could load 2 models, and each model could handle 4 requests in parallel, so a total of 8 concurrent requests can be processed. (assuming you have enough VRAM to load the 2 models with 4x the context size). If more requests are being sent they'll queue in a FIFO order, up to OLLAMA_MAX_QUEUE requests (default 512)

When concurrency is out of experimental status in a future release, if you don't specify these settings, we'll pick reasonable defaults based on the capabilities of your system.

<!-- gh-comment-id:2179494439 --> @dhiltgen commented on GitHub (Jun 19, 2024): @goreng2 while concurrency is in experimental preview, you can enable it with 2 settings OLLAMA_NUM_PARALLEL sets the number of concurrent requests for each individual loaded model OLLAMA_MAX_LOADED_MODELS sets the number of models that can be loaded at the same time, provided there is sufficient VRAM to fully load the models. (Partial loads always max at 1 loaded model) So for example, if you set OLLAMA_NUM_PARALLEL=4 and OLLAMA_MAX_LOADED_MODELS=2 then you could load 2 models, and each model could handle 4 requests in parallel, so a total of 8 concurrent requests can be processed. (assuming you have enough VRAM to load the 2 models with 4x the context size). If more requests are being sent they'll queue in a FIFO order, up to OLLAMA_MAX_QUEUE requests (default 512) When concurrency is out of experimental status in a future release, if you don't specify these settings, we'll pick reasonable defaults based on the capabilities of your system.
Author
Owner

@edenbuaa commented on GitHub (Jun 26, 2024):

good idea. @edenbuaa

<!-- gh-comment-id:2191332858 --> @edenbuaa commented on GitHub (Jun 26, 2024): good idea. @edenbuaa
Author
Owner

@vincent-herlemont commented on GitHub (Jul 15, 2024):

@dhiltgen Is there a way for an app which use the Ollama API to retrieve the values of OLLAMA_NUM_PARALLEL and OLLAMA_MAX_LOADED_MODELS in order to know how many parallel requests Ollama can handle?

<!-- gh-comment-id:2229486709 --> @vincent-herlemont commented on GitHub (Jul 15, 2024): @dhiltgen Is there a way for an app which use the Ollama API to retrieve the values of OLLAMA_NUM_PARALLEL and OLLAMA_MAX_LOADED_MODELS in order to know how many parallel requests Ollama can handle?
Author
Owner

@dhiltgen commented on GitHub (Jul 18, 2024):

@vincent-herlemont this isn't exposed via API today.

<!-- gh-comment-id:2236934093 --> @dhiltgen commented on GitHub (Jul 18, 2024): @vincent-herlemont this isn't exposed via API today.
Author
Owner

@Omega-Centauri-21 commented on GitHub (Jul 28, 2024):

I install ollama on Mac. are these the default value for this two env?OLLAMA_NUM_PARALLEL=4 OLLAMA_MAX_LOADED_MODELS=4.

the following should work, right? :) t@603e5f4a42f1 ~ % launchctl setenv OLLAMA_NUM_PARALLEL 5 t@603e5f4a42f1 ~ % launchctl setenv OLLAMA_MAX_LOADED_MODELS 5

to check the value of ENVs :) launchctl getenv OLLAMA_NUM_PARALLEL launchctl getenv OLLAMA_MAX_LOADED_MODELS

restart ollama to activate new value of ENVs. :) very nice!

For everyone trying this on Windows, first make sure your Ollama is updated to v3.0+

Then in your command prompt and use

setx OLLAMA_NUM_PARALLEL 5
setx OLLAMA_MAX_LOADED_MODELS 5

And now restart the server

<!-- gh-comment-id:2254692293 --> @Omega-Centauri-21 commented on GitHub (Jul 28, 2024): > I install ollama on Mac. are these the default value for this two env?OLLAMA_NUM_PARALLEL=4 OLLAMA_MAX_LOADED_MODELS=4. > > the following should work, right? :) t@603e5f4a42f1 ~ % launchctl setenv OLLAMA_NUM_PARALLEL 5 t@603e5f4a42f1 ~ % launchctl setenv OLLAMA_MAX_LOADED_MODELS 5 > > to check the value of ENVs :) launchctl getenv OLLAMA_NUM_PARALLEL launchctl getenv OLLAMA_MAX_LOADED_MODELS > > restart ollama to activate new value of ENVs. :) very nice! For everyone trying this on Windows, first make sure your Ollama is updated to v3.0+ Then in your command prompt and use ``` setx OLLAMA_NUM_PARALLEL 5 setx OLLAMA_MAX_LOADED_MODELS 5 ``` And now restart the server
Author
Owner

@dhiltgen commented on GitHub (Jul 29, 2024):

Concurrency is no longer experimental as of 0.2.0 and newer. I would recommend leaving these 2 variables unset so the default algorithm is used. If you want more (or less) parallelism per model than 4 (the default) then you can adjust OLLAMA_NUM_PARALLEL. The default max loaded models is 3x GPU count. In our performance testing, we found >3 often leads to degraded performance if all the models are being accessed concurrently, so make sure to run perf tests before settling on an alternative max models setting.

<!-- gh-comment-id:2256414346 --> @dhiltgen commented on GitHub (Jul 29, 2024): Concurrency is no longer experimental as of 0.2.0 and newer. I would recommend leaving these 2 variables unset so the default algorithm is used. If you want more (or less) parallelism per model than 4 (the default) then you can adjust OLLAMA_NUM_PARALLEL. The default max loaded models is 3x GPU count. In our performance testing, we found >3 often leads to degraded performance if all the models are being accessed concurrently, so make sure to run perf tests before settling on an alternative max models setting.
Author
Owner

@Omega-Centauri-21 commented on GitHub (Jul 29, 2024):

So Could you please provide the detailed implementation of parallelism on Windows that can be utilized with langserve and API calls?

<!-- gh-comment-id:2256817105 --> @Omega-Centauri-21 commented on GitHub (Jul 29, 2024): So Could you please provide the detailed implementation of parallelism on Windows that can be utilized with langserve and API calls?
Author
Owner

@reinhardt-bit commented on GitHub (Oct 11, 2024):

If you cant get it right then use litellm

<!-- gh-comment-id:2408156353 --> @reinhardt-bit commented on GitHub (Oct 11, 2024): If you cant get it right then use litellm
Author
Owner

@Omega-Centauri-21 commented on GitHub (Oct 12, 2024):

Actually I finally got it!

I was facing some issues with setting the variables through the terminal so I set those variables via the Edit Environment Variables through system properties in windows and created a new variable for my user.

  • Pretty simple and worked like a chart when I ran it.

As of today, with the latest running release, the parallelism feature has been delivered and can be closed for good. Anybody still facing any issues can ping me for help.

My heart goes out to the Community for the help and support.
Thankyou.

<!-- gh-comment-id:2408586843 --> @Omega-Centauri-21 commented on GitHub (Oct 12, 2024): Actually I finally got it! I was facing some issues with setting the variables through the terminal so I set those variables via the `Edit Environment Variables` through system properties in windows and created a new variable for my user. - Pretty simple and worked like a chart when I ran it. As of today, with the latest running release, the parallelism feature has been `delivered` and can be closed for good. Anybody still facing any issues can ping me for help. My heart goes out to the Community for the help and support. Thankyou.
Author
Owner

@Vimiso commented on GitHub (Oct 13, 2024):

Can I request more information on sensible OLLAMA_NUM_PARALLEL settings? For example, let's say I have one loaded model in memory. How many parallel requests can I throw at it, or rather what system requirements must I know to arrive at an optimal number? I assume increasing OLLAMA_MAX_LOADED_MODELS (given enough VRAM) would increase the throughput. Also, is there a queue built in or will serve http requests timeout while ollama is busy?

<!-- gh-comment-id:2409117238 --> @Vimiso commented on GitHub (Oct 13, 2024): Can I request more information on sensible `OLLAMA_NUM_PARALLEL` settings? For example, let's say I have one loaded model in memory. How many parallel requests can I throw at it, or rather what system requirements must I know to arrive at an optimal number? I assume increasing `OLLAMA_MAX_LOADED_MODELS` (given enough VRAM) would increase the throughput. Also, is there a queue built in or will serve http requests timeout while ollama is busy?
Author
Owner

@dhiltgen commented on GitHub (Oct 14, 2024):

@Vimiso the current algorithm is described here It depends on how large the model is and how much VRAM (or system memory) you have - we try to load the model to support 4 concurrent requests, but if there isn't sufficient [V]RAM we fall back to supporting a single request at a time. We don't currently have an API that exposes the final settings, but you can check the server log to see how it was loaded.

<!-- gh-comment-id:2411680683 --> @dhiltgen commented on GitHub (Oct 14, 2024): @Vimiso the current algorithm is described [here](https://github.com/ollama/ollama/blob/main/docs/faq.md#how-does-ollama-handle-concurrent-requests) It depends on how large the model is and how much VRAM (or system memory) you have - we try to load the model to support 4 concurrent requests, but if there isn't sufficient [V]RAM we fall back to supporting a single request at a time. We don't currently have an API that exposes the final settings, but you can check the server log to see how it was loaded.
Author
Owner

@phazei commented on GitHub (Nov 12, 2024):

So when using a model in parallel, it uses batching, right? So the two requests HAVE to start at the same time? Is there a setting for how long it waits for the request pool to fill before starting the batch? Suppose I have 2 agents, and they both request at the same time, but it takes 1 second to process the request before the next request. I'd like to use batching, but it would be ideal if it would wait for at least 1 second so be sure I can get both requests started together instead of having to wait.

<!-- gh-comment-id:2469707534 --> @phazei commented on GitHub (Nov 12, 2024): So when using a model in parallel, it uses batching, right? So the two requests HAVE to start at the same time? Is there a setting for how long it waits for the request pool to fill before starting the batch? Suppose I have 2 agents, and they both request at the same time, but it takes 1 second to process the request before the next request. I'd like to use batching, but it would be ideal if it would wait for at least 1 second so be sure I can get both requests started together instead of having to wait.
Author
Owner

@jessegross commented on GitHub (Nov 12, 2024):

There's no waiting time for batches, processing will start as soon as something is available with everything that is ready at that time. Generally, it's not helpful to wait because the savings from batching are overwhelmed by the lost cycles while things are sitting idle.

If requests are coming in continuously then new requests will be queued while the previous batch is running. Once that batch finishes, all of the queued requests will form the next batch.

<!-- gh-comment-id:2471282636 --> @jessegross commented on GitHub (Nov 12, 2024): There's no waiting time for batches, processing will start as soon as something is available with everything that is ready at that time. Generally, it's not helpful to wait because the savings from batching are overwhelmed by the lost cycles while things are sitting idle. If requests are coming in continuously then new requests will be queued while the previous batch is running. Once that batch finishes, all of the queued requests will form the next batch.
Author
Owner

@phazei commented on GitHub (Nov 13, 2024):

If lots of requests are coming in, then I'd agree, it's not helpful to wait since things will double up often enough. But on a local machine with a not so fast card and only two agents, it seems it would always end up being sequential and not taking advantage of the batching because the next batch starts the moment the previous output is complete which leaves no time for the response to be processed before being turned around for another request. If there was a configurable window of 300ms - 1000ms, then it could save a great deal of time on a GPU poor system.
There's also a thing I recently heard of called continuous batching which can start any request right away though, I don't know if Ollama supports that, but if that's available, then a waiting pool wouldn't have any necessity.

I was thinking if it would be a possibility to create my own waiting pool, but for that to work, there'd need to be a way to poll the server to see if it's busy or not, and once it's no longer busy, wait x seconds before passing through requests. I think there's 2 issues with that though, I looked at the API and didn't see anything that would work for that, I can't even find an output log showing what requests are hitting the API. And second, you said the requests already had to be waiting, so if they're sent x seconds after it's available, there inevitably will be a race condition where one is chosen since there's no pool.

<!-- gh-comment-id:2472784276 --> @phazei commented on GitHub (Nov 13, 2024): If lots of requests are coming in, then I'd agree, it's not helpful to wait since things will double up often enough. But on a local machine with a not so fast card and only two agents, it seems it would always end up being sequential and not taking advantage of the batching because the next batch starts the moment the previous output is complete which leaves no time for the response to be processed before being turned around for another request. If there was a configurable window of 300ms - 1000ms, then it could save a great deal of time on a GPU poor system. There's also a thing I recently heard of called continuous batching which can start any request right away though, I don't know if Ollama supports that, but if that's available, then a waiting pool wouldn't have any necessity. I was thinking if it would be a possibility to create my own waiting pool, but for that to work, there'd need to be a way to poll the server to see if it's busy or not, and once it's no longer busy, wait x seconds before passing through requests. I think there's 2 issues with that though, I looked at the API and didn't see anything that would work for that, I can't even find an output log showing what requests are hitting the API. And second, you said the requests already had to be waiting, so if they're sent x seconds after it's available, there inevitably will be a race condition where one is chosen since there's no pool.
Author
Owner

@easp commented on GitHub (Nov 13, 2024):

@phazei I'm pretty sure Ollama uses continuous batching. If I prompt the same model from two different terminal windows in parallel, I get a stream of results in each window simultaneously and the total # of tokens generated per second across the two is higher than it is when generating a single result set. This is what you'd expect from continuous batching.

<!-- gh-comment-id:2474899672 --> @easp commented on GitHub (Nov 13, 2024): @phazei I'm pretty sure Ollama uses continuous batching. If I prompt the same model from two different terminal windows in parallel, I get a stream of results in each window simultaneously and the total # of tokens generated per second across the two is higher than it is when generating a single result set. This is what you'd expect from continuous batching.
Author
Owner

@jessegross commented on GitHub (Nov 14, 2024):

Yes, I should have been clearer in what I wrote previously. This is more specifically what will happen:

  • Ollama will accept up to NUM_PARALLEL requests at a time. For anything beyond that, the requests will get queued. In this case, a request is something like a chat message to the API.
  • The first batch will be created and start processing as soon as something is available - this will contain a prompt.
  • Every token that is output needs to be fed back into the LLM as part of a new batch, so batches will be created continuously while a request is being processed.
  • When additional requests come in, they will be grouped together into one of these batches with tokens from other requests. As soon as the LLM finishes processing a batch, it will start on the next one with anything that is now runnable.
<!-- gh-comment-id:2477448229 --> @jessegross commented on GitHub (Nov 14, 2024): Yes, I should have been clearer in what I wrote previously. This is more specifically what will happen: - Ollama will accept up to NUM_PARALLEL requests at a time. For anything beyond that, the requests will get queued. In this case, a request is something like a chat message to the API. - The first batch will be created and start processing as soon as something is available - this will contain a prompt. - Every token that is output needs to be fed back into the LLM as part of a new batch, so batches will be created continuously while a request is being processed. - When additional requests come in, they will be grouped together into one of these batches with tokens from other requests. As soon as the LLM finishes processing a batch, it will start on the next one with anything that is now runnable.
Author
Owner

@Ivanrs297 commented on GitHub (Jan 28, 2025):

Almost year later, any news on this request?

<!-- gh-comment-id:2619297556 --> @Ivanrs297 commented on GitHub (Jan 28, 2025): Almost year later, any news on this request?
Author
Owner

@hackthedev commented on GitHub (Jan 28, 2025):

i tried to set the env variables on windows and its being ignored :|

<!-- gh-comment-id:2619850144 --> @hackthedev commented on GitHub (Jan 28, 2025): i tried to set the env variables on windows and its being ignored :|
Author
Owner

@jammsen commented on GitHub (Jan 29, 2025):

@Ivanrs297

Almost year later, any news on this request?

You can use it a long time already, have you tried to read the comment 1 person above you before posting?

https://github.com/ollama/ollama/issues/358#issuecomment-2477448229

<!-- gh-comment-id:2620458981 --> @jammsen commented on GitHub (Jan 29, 2025): @Ivanrs297 > Almost year later, any news on this request? You can use it a long time already, have you tried to read the comment 1 person above you before posting? https://github.com/ollama/ollama/issues/358#issuecomment-2477448229
Author
Owner

@alirezaghey commented on GitHub (Mar 4, 2025):

Hey folks, I went through this thread and also made some tests on my servers and I'm still not sure if the following scenario is possible:

  • I have multiple concurrent users
  • I have 4 GPUs with enough ram such that a model fits completely on one GPU with about 5 Gigs left
  • When sending prompts to 2 different models, ollama perfectly loads them both on different GPUs and processes them concurrently.
  • When multiple requests are sent to the same model, ollama handles all of them on a single GPU although there are 3 free GPUs without any resource usage.

Is there a way to load the same model on multiple GPUs and have each of them handle a few of the requests with some configurations and settings?
The next step that comes to mind is to deploy different ollama instances on different ports and handle load balancing in the application.

Any feedback is appreciate. Thank you!

<!-- gh-comment-id:2698279121 --> @alirezaghey commented on GitHub (Mar 4, 2025): Hey folks, I went through this thread and also made some tests on my servers and I'm still not sure if the following scenario is possible: - I have multiple concurrent users - I have 4 GPUs with enough ram such that a model fits completely on one GPU with about 5 Gigs left - When sending prompts to 2 different models, ollama perfectly loads them both on different GPUs and processes them concurrently. - When multiple requests are sent to the same model, ollama handles all of them on a single GPU although there are 3 free GPUs without any resource usage. Is there a way to load the same model on multiple GPUs and have each of them handle a few of the requests with some configurations and settings? The next step that comes to mind is to deploy different ollama instances on different ports and handle load balancing in the application. Any feedback is appreciate. Thank you!
Author
Owner

@dhiltgen commented on GitHub (Mar 5, 2025):

@alirezaghey tracked in feature request #3902

<!-- gh-comment-id:2701923278 --> @dhiltgen commented on GitHub (Mar 5, 2025): @alirezaghey tracked in feature request #3902
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#62196