|
| 1 | +--- |
| 2 | +title: "How to configure concurrency" |
| 3 | +description: "A guide to setting concurrency for your model" |
| 4 | +--- |
| 5 | + |
| 6 | +Configuring concurrency is one of the major knobs available for getting the most performance |
| 7 | +out of your model. In this doc, we'll cover the options that are available to you. |
| 8 | + |
| 9 | +# What is concurrency, and why configure it? |
| 10 | + |
| 11 | +At a very high level, "concurrency" in this context refers to how many requests a single replica can |
| 12 | +process at the same time. There are no right answers to what this number ought to be -- the specifics |
| 13 | +of your model and the metrics you are optimizing for (throughput? latency?) matter a lot for determining this. |
| 14 | + |
| 15 | +In Baseten & Truss, there are two notions of concurrency: |
| 16 | +* **Concurrency Target** -- the number of requests that will be sent to a model at the same time |
| 17 | +* **Predict Concurrency** -- once requests have made it onto the model container, the "predict concurrency" governs how many |
| 18 | +requests can go through the `predict` function on your Truss at once. |
| 19 | + |
| 20 | +# Concurrency Target |
| 21 | + |
| 22 | +The concurrency target is set in the Baseten UI, and to re-iterate, governs the maximum number of requests that will be sent |
| 23 | +to a single model replica. |
| 24 | + |
| 25 | +<Frame> |
| 26 | + <img src="/images/concurrency-target-picture.png" /> |
| 27 | +</Frame> |
| 28 | + |
| 29 | +An important note about this setting is that it is also used as a part of the auto-scaling parameters. If all replicas have |
| 30 | +hit their Concurrency Target, this triggers Baseten's autoscaling. |
| 31 | + |
| 32 | +Let's dive into a concrete example: |
| 33 | + |
| 34 | +<Frame> |
| 35 | + <img src="/images/concurrency-flow-chart-high-level.png" /> |
| 36 | +</Frame> |
| 37 | + |
| 38 | +Let's say that there is a single replica of a model, and the concurrency target is 2. If 5 requests come in, the first 2 will |
| 39 | +be sent to the replica, and the other 3 get queued up. Once the requests on the container complete the queued up |
| 40 | +requests will make it to the model container. |
| 41 | + |
| 42 | +<Note> |
| 43 | +Remember that if all replicas have hit their concurrency target, this will trigger autoscaling. So in this specific example, |
| 44 | +the queuing of requests 3-5 will trigger another replica to come up, if the model has not hit its max replicas yet. |
| 45 | +</Note> |
| 46 | + |
| 47 | + |
| 48 | +# Predict Concurrency |
| 49 | + |
| 50 | +Alright, so we've talked about the **Concurreny Target** feature that governs how many requests will be sent to a model at once. |
| 51 | +predict concurrency is a bit different -- it operates on the level of the model container and governs how many requests will go |
| 52 | +through the `predict` function concurrently. |
| 53 | + |
| 54 | +To get a sense for why this matters, let's recap the structure of a Truss: |
| 55 | + |
| 56 | +```python model.py |
| 57 | +class Model: |
| 58 | + |
| 59 | + def __init__(self): |
| 60 | + ... |
| 61 | + |
| 62 | + def preprocess(self, request): |
| 63 | + ... |
| 64 | + |
| 65 | + def predict(self, request): |
| 66 | + ... |
| 67 | + |
| 68 | + def postprocess(self, response): |
| 69 | + ... |
| 70 | +``` |
| 71 | + |
| 72 | +In this Truss model, there are three functions that are called in order to serve a request: |
| 73 | +* **preprocess** -- this function is used to perform any prework / modifications on the request before the `predict` function |
| 74 | +runs. For instance, if you are running an image classification model, and need to download images from S3, this is a good placeholder |
| 75 | +to do it. |
| 76 | +* **predict** -- this function is where the actual inference happens. It is likely where the logic that runs GPU code lives |
| 77 | +* **postprocess** -- this function is used to perform any postwork / modifications on the response before it is returned to the |
| 78 | +user. For instance, if you are running a text-to-image model, this is a good place to implement the logic for uploading an image |
| 79 | +to S3. |
| 80 | + |
| 81 | +You can see with these three functions and the behaviors that they are used for that you might want to have different |
| 82 | +levels of concurrency for the `predict` function. The most common need here is to limit access to the GPU, since multiple |
| 83 | +requests running on the GPU at the same time could cause serious degradation in performance. |
| 84 | + |
| 85 | +Unlike **Concurrency Target**, which is configured in the Baseten UI, the **Predict Concurrency** is configured as a part |
| 86 | +of the Truss Config (in the `config.yaml` file). |
| 87 | + |
| 88 | +```yaml config.yaml |
| 89 | +model_name: "My model with concurrency limits" |
| 90 | +... |
| 91 | +runtime: |
| 92 | + predict_concurrency: 2 # the default is 1 |
| 93 | +... |
| 94 | +``` |
| 95 | + |
| 96 | +To better understand this, let's use a specific example: |
| 97 | + |
| 98 | +<Frame> |
| 99 | + <img src="/images/concurrency-flow-model-pod.png" /> |
| 100 | +</Frame> |
| 101 | + |
| 102 | +Let's say predict concurrency is 1. |
| 103 | +1. Two requests come in to the pod. |
| 104 | +2. Both requests will begin preprocessing immediately (let's say, |
| 105 | +downloading images from S3). |
| 106 | +3. Once the first request finishes preprocessing, it will begin running on the GPU. The second request |
| 107 | +will then remain queued until the first request finishes running on the GPU in predict. |
| 108 | +4. After the first request finishes, the second request will begin being processed on the GPU |
| 109 | +5. Once the second request finishes, it will begin postprocessing, even if the first request is not done postprocessing |
| 110 | + |
| 111 | +To reiterate, predict concurrency is really great to use if you want to protect your GPU resource on your model pod, |
| 112 | +while still allowing for high concurrency for the pre and post-process steps. |
| 113 | + |
| 114 | +<Note> |
| 115 | +Remember that to actually achieve the predict concurrency you desire, the Concurrency Target must be at least that amount, |
| 116 | +so that the requests make it to the model container. |
| 117 | +</Note> |
0 commit comments