|
| 1 | +Implement LLM Cross-engine Orchestration Patterns |
| 2 | +====================================================================== |
| 3 | + |
| 4 | +In this tutorial, we will introduce how to implement LLM cross-engine |
| 5 | +orchestration patterns, like prefill-decode disaggregation, in MLC-LLM |
| 6 | +via microserving API. Aiming to make disaggregated serving programmable, |
| 7 | +MicroServing provides a new RISC-style approach to design LLM serving |
| 8 | +API at sub-request level. It enables programmable cross-engine serving |
| 9 | +patterns in a few lines of python code. For more information of |
| 10 | +microserving API, check out |
| 11 | +https://blog.mlc.ai/2025/01/07/microserving-llm-engines. |
| 12 | + |
| 13 | +Below is an example of prefill-decode disaggregation implementation. An |
| 14 | +LLM cross-engine orchestration pattern is implemented in a router, which |
| 15 | +dispatches original OpenAI-style completion requests to a chain of |
| 16 | +microserving API calls. In this code example, we create a subclass of |
| 17 | +Router (which includes wrappers for calling microserving APIs), and |
| 18 | +override ``translate_request`` function. The ``translate_request`` |
| 19 | +function takes in a request and a unique identifier of the request |
| 20 | +(``request_id``), and returns an AsyncGenerator of response. We launch |
| 21 | +the CustomRouter and 2 engines, each of which has tensor parallel degree |
| 22 | +2. Engine 0 is prefill engine and engine 1 is decode engine. |
| 23 | + |
| 24 | +.. code:: python |
| 25 | +
|
| 26 | + from mlc_llm.router import Router |
| 27 | + from mlc_llm.protocol import openai_api_protocol |
| 28 | + from typing import Any, AsyncGenerator |
| 29 | + from mlc_llm.serve.entrypoints import microserving_entrypoints |
| 30 | + from mlc_llm.interface.router import serve |
| 31 | +
|
| 32 | + import aiohttp |
| 33 | +
|
| 34 | + class CustomRouter(Router): |
| 35 | + async def translate_request(self, request: openai_api_protocol.CompletionRequest, request_id: str) -> AsyncGenerator[openai_api_protocol.CompletionResponse, Any]: |
| 36 | + pass |
| 37 | + |
| 38 | +
|
| 39 | + serve( |
| 40 | + model="/path/to/model", # replace this with actual path |
| 41 | + model_lib="/path/to/model_lib", # replace this with actual path |
| 42 | + router_host="127.0.0.1", |
| 43 | + router_port=9123, |
| 44 | + endpoint_hosts=["127.0.0.1", "127.0.0.1"], |
| 45 | + endpoint_ports=[9124,9125], |
| 46 | + endpoint_num_gpus=[2,2], |
| 47 | + enable_prefix_cache=False, |
| 48 | + router_type=CustomRouter, |
| 49 | + ) |
| 50 | +
|
| 51 | +In the ``translate_request`` function, we first assign ``request_id`` to |
| 52 | +request.user, and later the request id will be passed as an argument to |
| 53 | +the microserving API. |
| 54 | + |
| 55 | +.. code:: python |
| 56 | +
|
| 57 | + # we will pass request_id as an argument in microserving API calls |
| 58 | + request.user = request_id |
| 59 | + |
| 60 | +
|
| 61 | +Next, call ``prep_recv`` on the decode engine to prepare KV entries for |
| 62 | +receiving from remote. ``end=-1`` means that we will let the prefill |
| 63 | +engine prefill all except the last token, which makes sure that the |
| 64 | +prefill engine does not need sampling logic. ``prep_recv`` returns |
| 65 | +address to receive KV from remote and matched prefix length. For |
| 66 | +simplicity, we do not enable prefix cache in the tutorial, so we only |
| 67 | +need the kv address here. |
| 68 | + |
| 69 | +.. code:: python |
| 70 | +
|
| 71 | + async with aiohttp.ClientSession( |
| 72 | + timeout=aiohttp.ClientTimeout(total=3 * 3600), trust_env=True |
| 73 | + ) as session: |
| 74 | + decode_start = len(request.prompt) -1 |
| 75 | + # 1. Ask decode engine to prepare KV entries to receive from prefill engine |
| 76 | + prep_recv_request = microserving_entrypoints.PrepRecvRequest( |
| 77 | + **request.model_dump(), end=decode_start |
| 78 | + ) |
| 79 | + ( |
| 80 | + kv_addr_info, |
| 81 | + _, |
| 82 | + ) = await self.send_prepare_receive( |
| 83 | + session=session, |
| 84 | + request=prep_recv_request, |
| 85 | + server_url=self.server_urls[1], # engine 0 is prefill, engine 1 is decode. Here is decode engine |
| 86 | + ) |
| 87 | +
|
| 88 | +Then, call ``remote_send`` on the prefill engine to compute and send KV |
| 89 | +to decode engine. ``recv_rank=self.device_id_starts[1]`` means that we |
| 90 | +are sending KV to engine 1 (decode engine). |
| 91 | + |
| 92 | +.. code:: python |
| 93 | +
|
| 94 | +
|
| 95 | + # 2. Ask prefill engine to send KV to decode engine |
| 96 | + remote_send_request = microserving_entrypoints.RemoteSendRequest( |
| 97 | + **request.model_dump(), |
| 98 | + begin=0, |
| 99 | + end=decode_start, |
| 100 | + kv_addr_info=kv_addr_info, |
| 101 | + recv_rank=self.device_id_starts[1], # the rank of decode engine |
| 102 | + ) |
| 103 | + await self.send_remote_send( |
| 104 | + session=session, |
| 105 | + request=remote_send_request, |
| 106 | + server_url=self.server_urls[0], # prefill engine |
| 107 | + ) |
| 108 | +
|
| 109 | +Finally, call ``start_generate`` on the decode engine to start |
| 110 | +generating tokens. ``begin=decode_start`` means we will prefill the last |
| 111 | +token in the prompt and start decoding. Notably, the decode process of |
| 112 | +the request may be preempted. In such case, we yield None, so that the |
| 113 | +router will rerun the ``translate_request`` function. |
| 114 | + |
| 115 | +.. code:: python |
| 116 | +
|
| 117 | + # 3. Start decoding |
| 118 | + start_generate_request = microserving_entrypoints.StartGenerateRequest( |
| 119 | + **request.model_dump(), |
| 120 | + begin=decode_start, |
| 121 | + ) |
| 122 | + async for response in self.send_start_generate( |
| 123 | + session=session, |
| 124 | + request=start_generate_request, |
| 125 | + server_url=self.server_urls[1], |
| 126 | + ): |
| 127 | + if len(response.choices) > 0: |
| 128 | + finish_reason = response.choices[0].finish_reason |
| 129 | + if finish_reason == "preempt": |
| 130 | + yield None |
| 131 | + yield response |
| 132 | +
|
| 133 | +Bringing everything together, the complete code is as below: |
| 134 | + |
| 135 | +.. code:: python |
| 136 | +
|
| 137 | + from mlc_llm.router import Router |
| 138 | + from mlc_llm.protocol import openai_api_protocol |
| 139 | + from typing import Any, AsyncGenerator |
| 140 | + from mlc_llm.serve.entrypoints import microserving_entrypoints |
| 141 | + from mlc_llm.interface.router import serve |
| 142 | +
|
| 143 | + import aiohttp |
| 144 | + class CustomRouter(Router): |
| 145 | + async def translate_request(self, request: openai_api_protocol.CompletionRequest, request_id: str) -> AsyncGenerator[openai_api_protocol.CompletionResponse, Any]: |
| 146 | + # we will pass request_id as an argument in microserving API calls |
| 147 | + request.user = request_id |
| 148 | + |
| 149 | + async with aiohttp.ClientSession( |
| 150 | + timeout=aiohttp.ClientTimeout(total=3 * 3600), trust_env=True |
| 151 | + ) as session: |
| 152 | + decode_start = len(request.prompt) -1 |
| 153 | + # 1. Ask decode engine to prepare KV entries to receive from prefill engine |
| 154 | + prep_recv_request = microserving_entrypoints.PrepRecvRequest( |
| 155 | + **request.model_dump(), end=decode_start |
| 156 | + ) |
| 157 | + ( |
| 158 | + kv_addr_info, |
| 159 | + _, |
| 160 | + ) = await self.send_prepare_receive( |
| 161 | + session=session, |
| 162 | + request=prep_recv_request, |
| 163 | + server_url=self.server_urls[1], # engine 0 is prefill, engine 1 is decode. Here is decode engine |
| 164 | + ) |
| 165 | + # 2. Ask prefill engine to send KV to decode engine |
| 166 | + remote_send_request = microserving_entrypoints.RemoteSendRequest( |
| 167 | + **request.model_dump(), |
| 168 | + begin=0, |
| 169 | + end=decode_start, |
| 170 | + kv_addr_info=kv_addr_info, |
| 171 | + recv_rank=self.device_id_starts[1], # the rank of decode engine |
| 172 | + ) |
| 173 | + await self.send_remote_send( |
| 174 | + session=session, |
| 175 | + request=remote_send_request, |
| 176 | + server_url=self.server_urls[0], # prefill engine |
| 177 | + ) |
| 178 | + # 3. Start decoding |
| 179 | + start_generate_request = microserving_entrypoints.StartGenerateRequest( |
| 180 | + **request.model_dump(), |
| 181 | + begin=decode_start, |
| 182 | + ) |
| 183 | + async for response in self.send_start_generate( |
| 184 | + session=session, |
| 185 | + request=start_generate_request, |
| 186 | + server_url=self.server_urls[1], |
| 187 | + ): |
| 188 | + if len(response.choices) > 0: |
| 189 | + finish_reason = response.choices[0].finish_reason |
| 190 | + if finish_reason == "preempt": |
| 191 | + yield None |
| 192 | + yield response |
| 193 | + |
| 194 | +
|
| 195 | + serve( |
| 196 | + model="/path/to/model", # replace this with actual path |
| 197 | + model_lib="/path/to/model_lib", # replace this with actual path |
| 198 | + router_host="127.0.0.1", |
| 199 | + router_port=9123, |
| 200 | + endpoint_hosts=["127.0.0.1", "127.0.0.1"], |
| 201 | + endpoint_ports=[9124,9125], |
| 202 | + endpoint_num_gpus=[2,2], |
| 203 | + enable_prefix_cache=False, |
| 204 | + router_type=CustomRouter, |
| 205 | + ) |
0 commit comments