|
| 1 | +# flake8: noqa |
| 2 | +""" |
| 3 | +Cross-node parallelism examples for Ray Serve LLM. |
| 4 | +
|
| 5 | +TP / PP / custom placement group strategies |
| 6 | +for multi-node LLM deployments. |
| 7 | +""" |
| 8 | + |
| 9 | +# __cross_node_tp_example_start__ |
| 10 | +import vllm |
| 11 | +from ray import serve |
| 12 | +from ray.serve.llm import LLMConfig, build_openai_app |
| 13 | + |
| 14 | +# Configure a model with tensor parallelism across 2 GPUs |
| 15 | +# Tensor parallelism splits model weights across GPUs |
| 16 | +llm_config = LLMConfig( |
| 17 | + model_loading_config=dict( |
| 18 | + model_id="llama-3.1-8b", |
| 19 | + model_source="meta-llama/Llama-3.1-8B-Instruct", |
| 20 | + ), |
| 21 | + deployment_config=dict( |
| 22 | + autoscaling_config=dict( |
| 23 | + min_replicas=1, |
| 24 | + max_replicas=2, |
| 25 | + ) |
| 26 | + ), |
| 27 | + accelerator_type="L4", |
| 28 | + engine_kwargs=dict( |
| 29 | + tensor_parallel_size=2, |
| 30 | + max_model_len=8192, |
| 31 | + ), |
| 32 | +) |
| 33 | + |
| 34 | +# Deploy the application |
| 35 | +app = build_openai_app({"llm_configs": [llm_config]}) |
| 36 | +serve.run(app, blocking=True) |
| 37 | +# __cross_node_tp_example_end__ |
| 38 | + |
| 39 | +# __cross_node_pp_example_start__ |
| 40 | +from ray import serve |
| 41 | +from ray.serve.llm import LLMConfig, build_openai_app |
| 42 | + |
| 43 | +# Configure a model with pipeline parallelism across 2 GPUs |
| 44 | +# Pipeline parallelism splits model layers across GPUs |
| 45 | +llm_config = LLMConfig( |
| 46 | + model_loading_config=dict( |
| 47 | + model_id="llama-3.1-8b", |
| 48 | + model_source="meta-llama/Llama-3.1-8B-Instruct", |
| 49 | + ), |
| 50 | + deployment_config=dict( |
| 51 | + autoscaling_config=dict( |
| 52 | + min_replicas=1, |
| 53 | + max_replicas=1, |
| 54 | + ) |
| 55 | + ), |
| 56 | + accelerator_type="L4", |
| 57 | + engine_kwargs=dict( |
| 58 | + pipeline_parallel_size=2, |
| 59 | + max_model_len=8192, |
| 60 | + ), |
| 61 | +) |
| 62 | + |
| 63 | +# Deploy the application |
| 64 | +app = build_openai_app({"llm_configs": [llm_config]}) |
| 65 | +serve.run(app, blocking=True) |
| 66 | +# __cross_node_pp_example_end__ |
| 67 | + |
| 68 | +# __cross_node_tp_pp_example_start__ |
| 69 | +from ray import serve |
| 70 | +from ray.serve.llm import LLMConfig, build_openai_app |
| 71 | + |
| 72 | +# Configure a model with both tensor and pipeline parallelism |
| 73 | +# This example uses 4 GPUs total (2 TP * 2 PP) |
| 74 | +llm_config = LLMConfig( |
| 75 | + model_loading_config=dict( |
| 76 | + model_id="llama-3.1-8b", |
| 77 | + model_source="meta-llama/Llama-3.1-8B-Instruct", |
| 78 | + ), |
| 79 | + deployment_config=dict( |
| 80 | + autoscaling_config=dict( |
| 81 | + min_replicas=1, |
| 82 | + max_replicas=1, |
| 83 | + ) |
| 84 | + ), |
| 85 | + accelerator_type="L4", |
| 86 | + engine_kwargs=dict( |
| 87 | + tensor_parallel_size=2, |
| 88 | + pipeline_parallel_size=2, |
| 89 | + max_model_len=8192, |
| 90 | + enable_chunked_prefill=True, |
| 91 | + max_num_batched_tokens=4096, |
| 92 | + ), |
| 93 | +) |
| 94 | + |
| 95 | +# Deploy the application |
| 96 | +app = build_openai_app({"llm_configs": [llm_config]}) |
| 97 | +serve.run(app, blocking=True) |
| 98 | +# __cross_node_tp_pp_example_end__ |
| 99 | + |
| 100 | +# __custom_placement_group_pack_example_start__ |
| 101 | +from ray import serve |
| 102 | +from ray.serve.llm import LLMConfig, build_openai_app |
| 103 | + |
| 104 | +# Configure a model with custom placement group using PACK strategy |
| 105 | +# PACK tries to place workers on as few nodes as possible for locality |
| 106 | +llm_config = LLMConfig( |
| 107 | + model_loading_config=dict( |
| 108 | + model_id="llama-3.1-8b", |
| 109 | + model_source="meta-llama/Llama-3.1-8B-Instruct", |
| 110 | + ), |
| 111 | + deployment_config=dict( |
| 112 | + autoscaling_config=dict( |
| 113 | + min_replicas=1, |
| 114 | + max_replicas=1, |
| 115 | + ) |
| 116 | + ), |
| 117 | + accelerator_type="L4", |
| 118 | + engine_kwargs=dict( |
| 119 | + tensor_parallel_size=2, |
| 120 | + max_model_len=8192, |
| 121 | + ), |
| 122 | + placement_group_config=dict( |
| 123 | + bundles=[{"GPU": 1}] * 2, |
| 124 | + strategy="PACK", |
| 125 | + ), |
| 126 | +) |
| 127 | + |
| 128 | +# Deploy the application |
| 129 | +app = build_openai_app({"llm_configs": [llm_config]}) |
| 130 | +serve.run(app, blocking=True) |
| 131 | +# __custom_placement_group_pack_example_end__ |
| 132 | + |
| 133 | +# __custom_placement_group_spread_example_start__ |
| 134 | +from ray import serve |
| 135 | +from ray.serve.llm import LLMConfig, build_openai_app |
| 136 | + |
| 137 | +# Configure a model with custom placement group using SPREAD strategy |
| 138 | +# SPREAD distributes workers across nodes for fault tolerance |
| 139 | +llm_config = LLMConfig( |
| 140 | + model_loading_config=dict( |
| 141 | + model_id="llama-3.1-8b", |
| 142 | + model_source="meta-llama/Llama-3.1-8B-Instruct", |
| 143 | + ), |
| 144 | + deployment_config=dict( |
| 145 | + autoscaling_config=dict( |
| 146 | + min_replicas=1, |
| 147 | + max_replicas=1, |
| 148 | + ) |
| 149 | + ), |
| 150 | + accelerator_type="L4", |
| 151 | + engine_kwargs=dict( |
| 152 | + tensor_parallel_size=4, |
| 153 | + max_model_len=8192, |
| 154 | + ), |
| 155 | + placement_group_config=dict( |
| 156 | + bundles=[{"GPU": 1}] * 4, |
| 157 | + strategy="SPREAD", |
| 158 | + ), |
| 159 | +) |
| 160 | + |
| 161 | +# Deploy the application |
| 162 | +app = build_openai_app({"llm_configs": [llm_config]}) |
| 163 | +serve.run(app, blocking=True) |
| 164 | +# __custom_placement_group_spread_example_end__ |
| 165 | + |
| 166 | +# __custom_placement_group_strict_pack_example_start__ |
| 167 | +from ray import serve |
| 168 | +from ray.serve.llm import LLMConfig, build_openai_app |
| 169 | + |
| 170 | +# Configure a model with custom placement group using STRICT_PACK strategy |
| 171 | +# STRICT_PACK ensures all workers are placed on the same node |
| 172 | +llm_config = LLMConfig( |
| 173 | + model_loading_config=dict( |
| 174 | + model_id="llama-3.1-8b", |
| 175 | + model_source="meta-llama/Llama-3.1-8B-Instruct", |
| 176 | + ), |
| 177 | + deployment_config=dict( |
| 178 | + autoscaling_config=dict( |
| 179 | + min_replicas=1, |
| 180 | + max_replicas=2, |
| 181 | + ) |
| 182 | + ), |
| 183 | + accelerator_type="A100", |
| 184 | + engine_kwargs=dict( |
| 185 | + tensor_parallel_size=2, |
| 186 | + max_model_len=8192, |
| 187 | + ), |
| 188 | + placement_group_config=dict( |
| 189 | + bundles=[{"GPU": 1}] * 2, |
| 190 | + strategy="STRICT_PACK", |
| 191 | + ), |
| 192 | +) |
| 193 | + |
| 194 | +# Deploy the application |
| 195 | +app = build_openai_app({"llm_configs": [llm_config]}) |
| 196 | +serve.run(app, blocking=True) |
| 197 | +# __custom_placement_group_strict_pack_example_end__ |
0 commit comments