@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / e73f76eec6
Fork 0

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, 18:15 UTC

[Model] Pipeline parallel support for JAIS (#7603)

Besher Alkurdi committed Aug 17, 2024 at 21:11 UTC e73f76eec6b5aa69b065923c175610d10ccabd50
2 files changed +51 -18
vllm/config.py
+1
@@ -36,6 +36,7 @@ _PP_SUPPORTED_MODELS = [
36 "AquilaForCausalLM",
37 "DeepseekV2ForCausalLM",
38 "InternLMForCausalLM",
39 + "JAISLMHeadModel",
40 "LlamaForCausalLM",
41 "LLaMAForCausalLM",
42 "MistralForCausalLM",
vllm/model_executor/models/jais.py
+50 -18
@@ -20,14 +20,14 @@
20 """Inference-only Jais model compatible with HuggingFace weights."""
21
22 import math
23 -from typing import Iterable, List, Optional, Tuple
23 +from typing import Iterable, List, Optional, Tuple, Union
24
25 import torch
26 from torch import nn
27
28 from vllm.attention import Attention, AttentionMetadata
29 from vllm.config import CacheConfig
30 -from vllm.distributed import (get_tensor_model_parallel_rank,
30 +from vllm.distributed import (get_pp_group, get_tensor_model_parallel_rank,
31 get_tensor_model_parallel_world_size)
32 from vllm.model_executor.layers.linear import (ColumnParallelLinear,
33 QKVParallelLinear,
@@ -43,6 +43,8 @@ from vllm.model_executor.sampling_metadata import SamplingMetadata
43 from vllm.sequence import IntermediateTensors, SamplerOutput
44 from vllm.transformers_utils.configs import JAISConfig
45
46 +from .utils import is_pp_missing_parameter, make_layers
47 +
48
49 class SwiGLUActivation(nn.Module):
50
@@ -216,6 +218,7 @@ class JAISModel(nn.Module):
218 config: JAISConfig,
219 cache_config: Optional[CacheConfig] = None,
220 quant_config: Optional[QuantizationConfig] = None,
221 + prefix: str = "",
222 ):
223 super().__init__()
224 self.config = config
@@ -231,10 +234,15 @@ class JAISModel(nn.Module):
234 self.embeddings_scale = config.embeddings_scale
235 else:
236 self.embeddings_scale = config.mup_embeddings_scale
234 - self.h = nn.ModuleList([
235 - JAISBlock(config, cache_config, quant_config)
236 - for _ in range(config.num_hidden_layers)
237 - ])
237 +
238 + self.start_layer, self.end_layer, self.h = make_layers(
239 + config.num_hidden_layers,
240 + lambda prefix: JAISBlock(config=config,
241 + cache_config=cache_config,
242 + quant_config=quant_config),
243 + prefix=f"{prefix}.h",
244 + )
245 +
246 self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon)
247
248 def forward(
@@ -243,19 +251,29 @@ class JAISModel(nn.Module):
251 position_ids: torch.Tensor,
252 kv_caches: List[torch.Tensor],
253 attn_metadata: AttentionMetadata,
246 - ) -> torch.Tensor:
247 - inputs_embeds = self.wte(input_ids)
248 - if self.wpe is not None:
249 - position_embeds = self.wpe(position_ids)
250 - hidden_states = inputs_embeds + position_embeds
254 + intermediate_tensors: Optional[IntermediateTensors] = None,
255 + ) -> Union[IntermediateTensors, torch.Tensor]:
256 + if get_pp_group().is_first_rank:
257 + inputs_embeds = self.wte(input_ids)
258 + if self.wpe is not None:
259 + position_embeds = self.wpe(position_ids)
260 + hidden_states = inputs_embeds + position_embeds
261 + else:
262 + hidden_states = inputs_embeds
263 + hidden_states *= torch.tensor(float(self.embeddings_scale),
264 + dtype=hidden_states.dtype)
265 else:
252 - hidden_states = inputs_embeds
253 - hidden_states *= torch.tensor(float(self.embeddings_scale),
254 - dtype=hidden_states.dtype)
266 + assert intermediate_tensors is not None
267 + hidden_states = intermediate_tensors["hidden_states"]
268
256 - for i in range(len(self.h)):
269 + for i in range(self.start_layer, self.end_layer):
270 layer = self.h[i]
258 - hidden_states = layer(hidden_states, kv_caches[i], attn_metadata)
271 + hidden_states = layer(hidden_states,
272 + kv_caches[i - self.start_layer],
273 + attn_metadata)
274 +
275 + if not get_pp_group().is_last_rank:
276 + return IntermediateTensors({"hidden_states": hidden_states})
277
278 hidden_states = self.ln_f(hidden_states)
279 return hidden_states
@@ -290,9 +308,9 @@ class JAISLMHeadModel(nn.Module):
308 kv_caches: List[torch.Tensor],
309 attn_metadata: AttentionMetadata,
310 intermediate_tensors: Optional[IntermediateTensors] = None,
293 - ) -> torch.Tensor:
311 + ) -> Union[IntermediateTensors, torch.Tensor]:
312 hidden_states = self.transformer(input_ids, positions, kv_caches,
295 - attn_metadata)
313 + attn_metadata, intermediate_tensors)
314 return hidden_states
315
316 def compute_logits(
@@ -304,6 +322,16 @@ class JAISLMHeadModel(nn.Module):
322 sampling_metadata)
323 return logits
324
325 + def make_empty_intermediate_tensors(
326 + self, batch_size: int, dtype: torch.dtype,
327 + device: torch.device) -> IntermediateTensors:
328 + return IntermediateTensors({
329 + "hidden_states":
330 + torch.zeros((batch_size, self.config.hidden_size),
331 + dtype=dtype,
332 + device=device),
333 + })
334 +
335 def sample(
336 self,
337 logits: torch.Tensor,
@@ -327,6 +355,10 @@ class JAISLMHeadModel(nn.Module):
355 continue
356 if not name.startswith("transformer."):
357 name = "transformer." + name
358 +
359 + if is_pp_missing_parameter(name, self):
360 + continue
361 +
362 param = params_dict[name]
363 # The HF's GPT-2 implementation uses Conv1D instead of Linear.
364 # Because of this, we need to transpose the weights.