A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 16, 19:30 UTC
[Models] Support Qwen model with PP (#6974)
Signed-off-by: Muralidhar Andoorveedu <muralidhar.andoorveedu@centml.ai>
Murali Andoorveedu committed
Aug 1, 2024 at 12:40 UTC
fc912e0886f5eaa584c1a65fad81c6c269f609a0
3 files changed
+46
-11
docs/source/serving/distributed_serving.rst
+1
-1
@@ -50,7 +50,7 @@ You can also additionally specify :code:`--pipeline-parallel-size` to enable pip
50
$ --pipeline-parallel-size 2
51
52
.. note::
53
- Pipeline parallel is a beta feature. It is only supported for online serving as well as LLaMa, GPT2, and Mixtral style models.
53
+ Pipeline parallel is a beta feature. It is only supported for online serving as well as LLaMa, GPT2, Mixtral, Qwen, Qwen2, and Nemotron style models.
54
55
Multi-Node Inference and Serving
56
--------------------------------
vllm/config.py
+1
@@ -42,6 +42,7 @@ _PP_SUPPORTED_MODELS = [
42
"NemotronForCausalLM",
43
"Qwen2ForCausalLM",
44
"Qwen2MoeForCausalLM",
45
+ "QWenLMHeadModel",
46
]
47
48
vllm/model_executor/models/qwen.py
+44
-10
@@ -12,7 +12,7 @@ from transformers import PretrainedConfig
12
13
from vllm.attention import Attention, AttentionMetadata
14
from vllm.config import CacheConfig
15
-from vllm.distributed import get_tensor_model_parallel_world_size
15
+from vllm.distributed import get_pp_group, get_tensor_model_parallel_world_size
16
from vllm.model_executor.layers.activation import SiluAndMul
17
from vllm.model_executor.layers.layernorm import RMSNorm
18
from vllm.model_executor.layers.linear import (MergedColumnParallelLinear,
@@ -30,6 +30,8 @@ from vllm.model_executor.sampling_metadata import SamplingMetadata
30
from vllm.sequence import IntermediateTensors, SamplerOutput
31
from vllm.utils import print_warning_once
32
33
+from .utils import is_pp_missing_parameter, make_layers
34
+
35
36
class QWenMLP(nn.Module):
37
@@ -186,6 +188,7 @@ class QWenModel(nn.Module):
188
config: PretrainedConfig,
189
cache_config: Optional[CacheConfig] = None,
190
quant_config: Optional[QuantizationConfig] = None,
191
+ prefix: str = "",
192
):
193
super().__init__()
194
self.config = config
@@ -195,10 +198,10 @@ class QWenModel(nn.Module):
198
config.vocab_size,
199
config.hidden_size,
200
)
198
- self.h = nn.ModuleList([
199
- QWenBlock(config, cache_config, quant_config)
200
- for _ in range(config.num_hidden_layers)
201
- ])
201
+ self.start_layer, self.end_layer, self.h = make_layers(
202
+ config.num_hidden_layers,
203
+ lambda prefix: QWenBlock(config, cache_config, quant_config),
204
+ prefix=f"{prefix}.h")
205
self.ln_f = RMSNorm(config.hidden_size, eps=config.layer_norm_epsilon)
206
207
def forward(
@@ -207,18 +210,29 @@ class QWenModel(nn.Module):
210
positions: torch.Tensor,
211
kv_caches: List[torch.Tensor],
212
attn_metadata: AttentionMetadata,
213
+ intermediate_tensors: Optional[IntermediateTensors],
214
) -> torch.Tensor:
211
- hidden_states = self.wte(input_ids)
212
- residual = None
213
- for i in range(len(self.h)):
215
+ if get_pp_group().is_first_rank:
216
+ hidden_states = self.wte(input_ids)
217
+ residual = None
218
+ else:
219
+ assert intermediate_tensors is not None
220
+ hidden_states = intermediate_tensors["hidden_states"]
221
+ residual = intermediate_tensors["residual"]
222
+ for i in range(self.start_layer, self.end_layer):
223
layer = self.h[i]
224
hidden_states, residual = layer(
225
positions,
226
hidden_states,
218
- kv_caches[i],
227
+ kv_caches[i - self.start_layer],
228
attn_metadata,
229
residual,
230
)
231
+ if not get_pp_group().is_last_rank:
232
+ return IntermediateTensors({
233
+ "hidden_states": hidden_states,
234
+ "residual": residual
235
+ })
236
hidden_states, _ = self.ln_f(hidden_states, residual)
237
return hidden_states
238
@@ -250,9 +264,23 @@ class QWenLMHeadModel(nn.Module):
264
intermediate_tensors: Optional[IntermediateTensors] = None,
265
) -> torch.Tensor:
266
hidden_states = self.transformer(input_ids, positions, kv_caches,
253
- attn_metadata)
267
+ attn_metadata, intermediate_tensors)
268
return hidden_states
269
270
+ def make_empty_intermediate_tensors(
271
+ self, batch_size: int, dtype: torch.dtype,
272
+ device: torch.device) -> IntermediateTensors:
273
+ return IntermediateTensors({
274
+ "hidden_states":
275
+ torch.zeros((batch_size, self.config.hidden_size),
276
+ dtype=dtype,
277
+ device=device),
278
+ "residual":
279
+ torch.zeros((batch_size, self.config.hidden_size),
280
+ dtype=dtype,
281
+ device=device),
282
+ })
283
+
284
def compute_logits(self, hidden_states: torch.Tensor,
285
sampling_metadata: SamplingMetadata) -> torch.Tensor:
286
logits = self.logits_processor(self.lm_head, hidden_states,
@@ -284,6 +312,9 @@ class QWenLMHeadModel(nn.Module):
312
# Skip loading extra bias for GPTQ models.
313
if name.endswith(".bias") and name not in params_dict:
314
continue
315
+ # Skip layers on other devices.
316
+ if is_pp_missing_parameter(name, self):
317
+ continue
318
param = params_dict[name]
319
weight_loader = param.weight_loader
320
weight_loader(param, loaded_weight, shard_id)
@@ -301,6 +332,9 @@ class QWenLMHeadModel(nn.Module):
332
"Only text inputs are allowed. Images won't be handled "
333
"until Qwen-VL models are fully supported.")
334
continue
335
+ # Skip layers on other devices.
336
+ if is_pp_missing_parameter(name, self):
337
+ continue
338
param = params_dict[name]
339
weight_loader = getattr(param, "weight_loader",
340
default_weight_loader)