|
| 1 | +# Ollama: Run quantized LLMs on CPUs and GPUs |
| 2 | +<p align="center"> |
| 3 | + <img src="https://i.imgur.com/HfqnGVA.png" width="400"> |
| 4 | +</p> |
| 5 | + |
| 6 | +[Ollama](https://github.com/ollama/ollama) is popular library for running LLMs on both CPUs and GPUs. |
| 7 | +It supports a wide range of models, including quantized versions of `llama2`, `llama2:70b`, `mistral`, `phi`, `gemma:7b` and many [more](https://ollama.com/library). |
| 8 | +You can use SkyPilot to run these models on CPU instances on any cloud provider, Kubernetes cluster, or even on your local machine. |
| 9 | +And if your instance has GPUs, Ollama will automatically use them for faster inference. |
| 10 | + |
| 11 | +In this example, you will run a quantized version of Llama2 on 4 CPUs with 8GB of memory, and then scale it up to more replicas with SkyServe. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | +To get started, install the latest version of SkyPilot: |
| 15 | + |
| 16 | +```bash |
| 17 | +pip install "skypilot-nightly[all]" |
| 18 | +``` |
| 19 | + |
| 20 | +For detailed installation instructions, please refer to the [installation guide](https://skypilot.readthedocs.io/en/latest/getting-started/installation.html). |
| 21 | + |
| 22 | +Once installed, run `sky check` to verify you have cloud access. |
| 23 | + |
| 24 | +### [Optional] Running locally on your machine |
| 25 | +If you do not have cloud access, you also can run this recipe on your local machine by creating a local Kubernetes cluster with `sky local up`. |
| 26 | + |
| 27 | +Make sure you have KinD installed and Docker running with 5 or more CPUs and 10GB or more of memory allocated to the [Docker runtime](https://kind.sigs.k8s.io/docs/user/quick-start/#settings-for-docker-desktop). |
| 28 | + |
| 29 | +To create a local Kubernetes cluster, run: |
| 30 | + |
| 31 | +```console |
| 32 | +sky local up |
| 33 | +``` |
| 34 | + |
| 35 | +<details> |
| 36 | +<summary>Example outputs:</summary> |
| 37 | + |
| 38 | +```console |
| 39 | +$ sky local up |
| 40 | +Creating local cluster... |
| 41 | +To view detailed progress: tail -n100 -f ~/sky_logs/sky-2024-04-09-19-14-03-599730/local_up.log |
| 42 | +I 04-09 19:14:33 log_utils.py:79] Kubernetes is running. |
| 43 | +I 04-09 19:15:33 log_utils.py:117] SkyPilot CPU image pulled. |
| 44 | +I 04-09 19:15:49 log_utils.py:123] Nginx Ingress Controller installed. |
| 45 | +⠸ Running sky check... |
| 46 | +Local Kubernetes cluster created successfully with 16 CPUs. |
| 47 | +`sky launch` can now run tasks locally. |
| 48 | +Hint: To change the number of CPUs, change your docker runtime settings. See https://kind.sigs.k8s.io/docs/user/quick-start/#settings-for-docker-desktop for more info. |
| 49 | +``` |
| 50 | +</details> |
| 51 | + |
| 52 | +After running this, `sky check` should show that you have access to a Kubernetes cluster. |
| 53 | + |
| 54 | +## SkyPilot YAML |
| 55 | +To run Ollama with SkyPilot, create a YAML file with the following content: |
| 56 | + |
| 57 | +<details> |
| 58 | +<summary>Click to see the full recipe YAML</summary> |
| 59 | + |
| 60 | +```yaml |
| 61 | +envs: |
| 62 | + MODEL_NAME: llama2 # mistral, phi, other ollama supported models |
| 63 | + OLLAMA_HOST: 0.0.0.0:8888 # Host and port for Ollama to listen on |
| 64 | + |
| 65 | +resources: |
| 66 | + cpus: 4+ |
| 67 | + memory: 8+ # 8 GB+ for 7B models, 16 GB+ for 13B models, 32 GB+ for 33B models |
| 68 | + # accelerators: L4:1 # No GPUs necessary for Ollama, but you can use them to run inference faster |
| 69 | + ports: 8888 |
| 70 | + |
| 71 | +service: |
| 72 | + replicas: 2 |
| 73 | + # An actual request for readiness probe. |
| 74 | + readiness_probe: |
| 75 | + path: /v1/chat/completions |
| 76 | + post_data: |
| 77 | + model: $MODEL_NAME |
| 78 | + messages: |
| 79 | + - role: user |
| 80 | + content: Hello! What is your name? |
| 81 | + max_tokens: 1 |
| 82 | + |
| 83 | +setup: | |
| 84 | + # Install Ollama |
| 85 | + if [ "$(uname -m)" == "aarch64" ]; then |
| 86 | + # For apple silicon support |
| 87 | + sudo curl -L https://ollama.com/download/ollama-linux-arm64 -o /usr/bin/ollama |
| 88 | + else |
| 89 | + sudo curl -L https://ollama.com/download/ollama-linux-amd64 -o /usr/bin/ollama |
| 90 | + fi |
| 91 | + sudo chmod +x /usr/bin/ollama |
| 92 | + |
| 93 | + # Start `ollama serve` and capture PID to kill it after pull is done |
| 94 | + ollama serve & |
| 95 | + OLLAMA_PID=$! |
| 96 | + |
| 97 | + # Wait for ollama to be ready |
| 98 | + IS_READY=false |
| 99 | + for i in {1..20}; |
| 100 | + do ollama list && IS_READY=true && break; |
| 101 | + sleep 5; |
| 102 | + done |
| 103 | + if [ "$IS_READY" = false ]; then |
| 104 | + echo "Ollama was not ready after 100 seconds. Exiting." |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | + |
| 108 | + # Pull the model |
| 109 | + ollama pull $MODEL_NAME |
| 110 | + echo "Model $MODEL_NAME pulled successfully." |
| 111 | + |
| 112 | + # Kill `ollama serve` after pull is done |
| 113 | + kill $OLLAMA_PID |
| 114 | +
|
| 115 | +run: | |
| 116 | + # Run `ollama serve` in the foreground |
| 117 | + echo "Serving model $MODEL_NAME" |
| 118 | + ollama serve |
| 119 | +``` |
| 120 | +</details> |
| 121 | +
|
| 122 | +You can also get the full YAML [here](https://github.com/skypilot-org/skypilot/tree/master/llm/ollama/ollama.yaml). |
| 123 | +
|
| 124 | +## Serving Llama2 with a CPU instance |
| 125 | +Start serving Llama2 on a 4 CPU instance with the following command: |
| 126 | +
|
| 127 | +```console |
| 128 | +sky launch ollama.yaml -c ollama --detach-run |
| 129 | +``` |
| 130 | + |
| 131 | +Wait until the model command returns successfully. |
| 132 | + |
| 133 | +<details> |
| 134 | +<summary>Example outputs:</summary> |
| 135 | + |
| 136 | +```console |
| 137 | +... |
| 138 | +== Optimizer == |
| 139 | +Target: minimizing cost |
| 140 | +Estimated cost: $0.0 / hour |
| 141 | + |
| 142 | +Considered resources (1 node): |
| 143 | +------------------------------------------------------------------------------------------------------- |
| 144 | + CLOUD INSTANCE vCPUs Mem(GB) ACCELERATORS REGION/ZONE COST ($) CHOSEN |
| 145 | +------------------------------------------------------------------------------------------------------- |
| 146 | + Kubernetes 4CPU--8GB 4 8 - kubernetes 0.00 ✔ |
| 147 | + AWS c6i.xlarge 4 8 - us-east-1 0.17 |
| 148 | + Azure Standard_F4s_v2 4 8 - eastus 0.17 |
| 149 | + GCP n2-standard-4 4 16 - us-central1-a 0.19 |
| 150 | + Fluidstack rec3pUyh6pNkIjCaL 6 24 RTXA4000:1 norway_4_eu 0.64 |
| 151 | +------------------------------------------------------------------------------------------------------- |
| 152 | +... |
| 153 | +``` |
| 154 | + |
| 155 | +</details> |
| 156 | + |
| 157 | +**💡Tip:** You can further reduce costs by using the `--use-spot` flag to run on spot instances. |
| 158 | + |
| 159 | +To launch a different model, use the `MODEL_NAME` environment variable: |
| 160 | + |
| 161 | +```console |
| 162 | +sky launch ollama.yaml -c ollama --detach-run --env MODEL_NAME=mistral |
| 163 | +``` |
| 164 | + |
| 165 | +Ollama supports `llama2`, `llama2:70b`, `mistral`, `phi`, `gemma:7b` and many more models. |
| 166 | +See the full list [here](https://ollama.com/library). |
| 167 | + |
| 168 | +Once the `sky launch` command returns successfully, you can interact with the model via |
| 169 | +- Standard OpenAPI-compatible endpoints (e.g., `/v1/chat/completions`) |
| 170 | +- [Ollama API](https://github.com/ollama/ollama/blob/main/docs/api.md) |
| 171 | + |
| 172 | +To curl `/v1/chat/completions`: |
| 173 | +```console |
| 174 | +ENDPOINT=$(sky status --endpoint 8888 ollama) |
| 175 | +curl $ENDPOINT/v1/chat/completions \ |
| 176 | + -H "Content-Type: application/json" \ |
| 177 | + -d '{ |
| 178 | + "model": "llama2", |
| 179 | + "messages": [ |
| 180 | + { |
| 181 | + "role": "system", |
| 182 | + "content": "You are a helpful assistant." |
| 183 | + }, |
| 184 | + { |
| 185 | + "role": "user", |
| 186 | + "content": "Who are you?" |
| 187 | + } |
| 188 | + ] |
| 189 | + }' |
| 190 | +``` |
| 191 | + |
| 192 | +<details> |
| 193 | +<summary>Example curl response:</summary> |
| 194 | + |
| 195 | +```json |
| 196 | +{ |
| 197 | + "id": "chatcmpl-322", |
| 198 | + "object": "chat.completion", |
| 199 | + "created": 1712015174, |
| 200 | + "model": "llama2", |
| 201 | + "system_fingerprint": "fp_ollama", |
| 202 | + "choices": [ |
| 203 | + { |
| 204 | + "index": 0, |
| 205 | + "message": { |
| 206 | + "role": "assistant", |
| 207 | + "content": "Hello there! *adjusts glasses* I am Assistant, your friendly and helpful AI companion. My purpose is to assist you in any way possible, from answering questions to providing information on a wide range of topics. Is there something specific you would like to know or discuss? Feel free to ask me anything!" |
| 208 | + }, |
| 209 | + "finish_reason": "stop" |
| 210 | + } |
| 211 | + ], |
| 212 | + "usage": { |
| 213 | + "prompt_tokens": 29, |
| 214 | + "completion_tokens": 68, |
| 215 | + "total_tokens": 97 |
| 216 | + } |
| 217 | +} |
| 218 | +``` |
| 219 | +</details> |
| 220 | + |
| 221 | +**💡Tip:** To speed up inference, you can use GPUs by specifying the `accelerators` field in the YAML. |
| 222 | + |
| 223 | +To stop the instance: |
| 224 | +```console |
| 225 | +sky stop ollama |
| 226 | +``` |
| 227 | + |
| 228 | +To shut down all resources: |
| 229 | +```console |
| 230 | +sky down ollama |
| 231 | +``` |
| 232 | + |
| 233 | +If you are using a local Kubernetes cluster created with `sky local up`, shut it down with: |
| 234 | +```console |
| 235 | +sky local down |
| 236 | +``` |
| 237 | + |
| 238 | +## Serving LLMs on CPUs at scale with SkyServe |
| 239 | + |
| 240 | +After experimenting with the model, you can deploy multiple replicas of the model with autoscaling and load-balancing using SkyServe. |
| 241 | + |
| 242 | +With no change to the YAML, launch a fully managed service on your infra: |
| 243 | +```console |
| 244 | +sky serve up ollama.yaml -n ollama |
| 245 | +``` |
| 246 | + |
| 247 | +Wait until the service is ready: |
| 248 | +```console |
| 249 | +watch -n10 sky serve status ollama |
| 250 | +``` |
| 251 | + |
| 252 | +<details> |
| 253 | +<summary>Example outputs:</summary> |
| 254 | + |
| 255 | +```console |
| 256 | +Services |
| 257 | +NAME VERSION UPTIME STATUS REPLICAS ENDPOINT |
| 258 | +ollama 1 3m 15s READY 2/2 34.171.202.102:30001 |
| 259 | + |
| 260 | +Service Replicas |
| 261 | +SERVICE_NAME ID VERSION IP LAUNCHED RESOURCES STATUS REGION |
| 262 | +ollama 1 1 34.69.185.170 4 mins ago 1x GCP(vCPU=4) READY us-central1 |
| 263 | +ollama 2 1 35.184.144.198 4 mins ago 1x GCP(vCPU=4) READY us-central1 |
| 264 | +``` |
| 265 | +</details> |
| 266 | + |
| 267 | + |
| 268 | +Get a single endpoint that load-balances across replicas: |
| 269 | +```console |
| 270 | +ENDPOINT=$(sky serve status --endpoint ollama) |
| 271 | +``` |
| 272 | + |
| 273 | +**💡Tip:** SkyServe fully manages the lifecycle of your replicas. For example, if a spot replica is preempted, the controller will automatically replace it. This significantly reduces the operational burden while saving costs. |
| 274 | + |
| 275 | +To curl the endpoint: |
| 276 | +```console |
| 277 | +curl -L $ENDPOINT/v1/chat/completions \ |
| 278 | + -H "Content-Type: application/json" \ |
| 279 | + -d '{ |
| 280 | + "model": "llama2", |
| 281 | + "messages": [ |
| 282 | + { |
| 283 | + "role": "system", |
| 284 | + "content": "You are a helpful assistant." |
| 285 | + }, |
| 286 | + { |
| 287 | + "role": "user", |
| 288 | + "content": "Who are you?" |
| 289 | + } |
| 290 | + ] |
| 291 | + }' |
| 292 | +``` |
| 293 | + |
| 294 | +To shut down all resources: |
| 295 | +```console |
| 296 | +sky serve down ollama |
| 297 | +``` |
| 298 | + |
| 299 | +See more details in [SkyServe docs](https://skypilot.readthedocs.io/en/latest/serving/sky-serve.html). |
0 commit comments