vllm-npu 0.4.2__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- vllm/__init__.py +23 -0
- vllm/_custom_ops.py +251 -0
- vllm/attention/__init__.py +13 -0
- vllm/attention/backends/__init__.py +0 -0
- vllm/attention/backends/abstract.py +127 -0
- vllm/attention/backends/flash_attn.py +271 -0
- vllm/attention/backends/flashinfer.py +220 -0
- vllm/attention/backends/rocm_flash_attn.py +374 -0
- vllm/attention/backends/torch_sdpa.py +250 -0
- vllm/attention/backends/xformers.py +393 -0
- vllm/attention/layer.py +56 -0
- vllm/attention/ops/__init__.py +0 -0
- vllm/attention/ops/paged_attn.py +216 -0
- vllm/attention/ops/prefix_prefill.py +792 -0
- vllm/attention/ops/triton_flash_attention.py +810 -0
- vllm/attention/selector.py +91 -0
- vllm/block.py +84 -0
- vllm/config.py +1225 -0
- vllm/core/__init__.py +0 -0
- vllm/core/block/__init__.py +0 -0
- vllm/core/block/block_table.py +295 -0
- vllm/core/block/common.py +199 -0
- vllm/core/block/cpu_gpu_block_allocator.py +228 -0
- vllm/core/block/interfaces.py +205 -0
- vllm/core/block/naive_block.py +318 -0
- vllm/core/block/prefix_caching_block.py +606 -0
- vllm/core/block_manager_v1.py +625 -0
- vllm/core/block_manager_v2.py +258 -0
- vllm/core/evictor_v1.py +105 -0
- vllm/core/evictor_v2.py +127 -0
- vllm/core/interfaces.py +113 -0
- vllm/core/policy.py +45 -0
- vllm/core/scheduler.py +1163 -0
- vllm/distributed/__init__.py +3 -0
- vllm/distributed/communication_op.py +237 -0
- vllm/distributed/device_communicators/__init__.py +0 -0
- vllm/distributed/device_communicators/custom_all_reduce.py +274 -0
- vllm/distributed/device_communicators/pynccl.py +287 -0
- vllm/distributed/device_communicators/pynccl_utils.py +66 -0
- vllm/distributed/parallel_state.py +339 -0
- vllm/distributed/utils.py +136 -0
- vllm/engine/__init__.py +0 -0
- vllm/engine/arg_utils.py +649 -0
- vllm/engine/async_llm_engine.py +737 -0
- vllm/engine/llm_engine.py +784 -0
- vllm/engine/metrics.py +368 -0
- vllm/engine/output_processor/__init__.py +0 -0
- vllm/engine/output_processor/interfaces.py +76 -0
- vllm/engine/output_processor/multi_step.py +142 -0
- vllm/engine/output_processor/single_step.py +284 -0
- vllm/engine/output_processor/stop_checker.py +101 -0
- vllm/engine/output_processor/util.py +19 -0
- vllm/entrypoints/__init__.py +0 -0
- vllm/entrypoints/api_server.py +119 -0
- vllm/entrypoints/llm.py +259 -0
- vllm/entrypoints/openai/__init__.py +0 -0
- vllm/entrypoints/openai/api_server.py +186 -0
- vllm/entrypoints/openai/cli_args.py +115 -0
- vllm/entrypoints/openai/protocol.py +460 -0
- vllm/entrypoints/openai/serving_chat.py +392 -0
- vllm/entrypoints/openai/serving_completion.py +347 -0
- vllm/entrypoints/openai/serving_engine.py +234 -0
- vllm/envs.py +217 -0
- vllm/executor/__init__.py +0 -0
- vllm/executor/cpu_executor.py +152 -0
- vllm/executor/distributed_gpu_executor.py +115 -0
- vllm/executor/executor_base.py +115 -0
- vllm/executor/gpu_executor.py +150 -0
- vllm/executor/multiproc_worker_utils.py +263 -0
- vllm/executor/neuron_executor.py +91 -0
- vllm/executor/ray_gpu_executor.py +327 -0
- vllm/executor/ray_utils.py +119 -0
- vllm/logger.py +153 -0
- vllm/logging/__init__.py +5 -0
- vllm/logging/formatter.py +15 -0
- vllm/lora/__init__.py +0 -0
- vllm/lora/fully_sharded_layers.py +262 -0
- vllm/lora/layers.py +1181 -0
- vllm/lora/lora.py +167 -0
- vllm/lora/models.py +645 -0
- vllm/lora/punica.py +213 -0
- vllm/lora/request.py +32 -0
- vllm/lora/utils.py +98 -0
- vllm/lora/worker_manager.py +251 -0
- vllm/model_executor/__init__.py +7 -0
- vllm/model_executor/guided_decoding/__init__.py +25 -0
- vllm/model_executor/guided_decoding/lm_format_enforcer_decoding.py +70 -0
- vllm/model_executor/guided_decoding/outlines_decoding.py +130 -0
- vllm/model_executor/guided_decoding/outlines_logits_processors.py +184 -0
- vllm/model_executor/layers/__init__.py +0 -0
- vllm/model_executor/layers/activation.py +173 -0
- vllm/model_executor/layers/fused_moe/__init__.py +7 -0
- vllm/model_executor/layers/fused_moe/configs/E=16,N=1344,device_name=NVIDIA_A100-SXM4-40GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=16,N=1344,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=16,N=1344,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=16,N=2688,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=16,N=2688,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=1792,device_name=NVIDIA_A100-SXM4-40GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=1792,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=1792,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=2048,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=2048,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_A100-SXM4-40GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_H100_80GB_HBM3,dtype=float8.json +140 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=4096,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=4096,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=7168,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=7168,device_name=NVIDIA_H100_80GB_HBM3,dtype=float8.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=7168,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/fused_moe.py +479 -0
- vllm/model_executor/layers/layernorm.py +71 -0
- vllm/model_executor/layers/linear.py +709 -0
- vllm/model_executor/layers/logits_processor.py +115 -0
- vllm/model_executor/layers/ops/__init__.py +0 -0
- vllm/model_executor/layers/ops/rand.py +157 -0
- vllm/model_executor/layers/ops/sample.py +406 -0
- vllm/model_executor/layers/quantization/__init__.py +35 -0
- vllm/model_executor/layers/quantization/aqlm.py +376 -0
- vllm/model_executor/layers/quantization/awq.py +175 -0
- vllm/model_executor/layers/quantization/base_config.py +97 -0
- vllm/model_executor/layers/quantization/fp8.py +265 -0
- vllm/model_executor/layers/quantization/gptq.py +224 -0
- vllm/model_executor/layers/quantization/gptq_marlin.py +438 -0
- vllm/model_executor/layers/quantization/marlin.py +227 -0
- vllm/model_executor/layers/quantization/schema.py +84 -0
- vllm/model_executor/layers/quantization/squeezellm.py +137 -0
- vllm/model_executor/layers/rejection_sampler.py +405 -0
- vllm/model_executor/layers/rotary_embedding.py +525 -0
- vllm/model_executor/layers/sampler.py +1051 -0
- vllm/model_executor/layers/vocab_parallel_embedding.py +155 -0
- vllm/model_executor/model_loader/__init__.py +30 -0
- vllm/model_executor/model_loader/loader.py +362 -0
- vllm/model_executor/model_loader/neuron.py +136 -0
- vllm/model_executor/model_loader/tensorizer.py +368 -0
- vllm/model_executor/model_loader/utils.py +41 -0
- vllm/model_executor/model_loader/weight_utils.py +372 -0
- vllm/model_executor/models/__init__.py +119 -0
- vllm/model_executor/models/baichuan.py +410 -0
- vllm/model_executor/models/bloom.py +327 -0
- vllm/model_executor/models/chatglm.py +386 -0
- vllm/model_executor/models/commandr.py +373 -0
- vllm/model_executor/models/dbrx.py +413 -0
- vllm/model_executor/models/decilm.py +122 -0
- vllm/model_executor/models/deepseek.py +438 -0
- vllm/model_executor/models/falcon.py +444 -0
- vllm/model_executor/models/gemma.py +393 -0
- vllm/model_executor/models/gpt2.py +266 -0
- vllm/model_executor/models/gpt_bigcode.py +274 -0
- vllm/model_executor/models/gpt_j.py +281 -0
- vllm/model_executor/models/gpt_neox.py +295 -0
- vllm/model_executor/models/internlm2.py +323 -0
- vllm/model_executor/models/jais.py +333 -0
- vllm/model_executor/models/llama.py +442 -0
- vllm/model_executor/models/llava.py +239 -0
- vllm/model_executor/models/minicpm.py +531 -0
- vllm/model_executor/models/mixtral.py +583 -0
- vllm/model_executor/models/mixtral_quant.py +404 -0
- vllm/model_executor/models/mpt.py +295 -0
- vllm/model_executor/models/olmo.py +356 -0
- vllm/model_executor/models/opt.py +349 -0
- vllm/model_executor/models/orion.py +319 -0
- vllm/model_executor/models/phi.py +300 -0
- vllm/model_executor/models/qwen.py +284 -0
- vllm/model_executor/models/qwen2.py +367 -0
- vllm/model_executor/models/qwen2_moe.py +447 -0
- vllm/model_executor/models/stablelm.py +301 -0
- vllm/model_executor/models/starcoder2.py +302 -0
- vllm/model_executor/models/xverse.py +366 -0
- vllm/model_executor/sampling_metadata.py +588 -0
- vllm/model_executor/utils.py +35 -0
- vllm/outputs.py +150 -0
- vllm/py.typed +2 -0
- vllm/sampling_params.py +340 -0
- vllm/sequence.py +766 -0
- vllm/spec_decode/__init__.py +0 -0
- vllm/spec_decode/batch_expansion.py +397 -0
- vllm/spec_decode/interfaces.py +73 -0
- vllm/spec_decode/metrics.py +191 -0
- vllm/spec_decode/multi_step_worker.py +203 -0
- vllm/spec_decode/ngram_worker.py +176 -0
- vllm/spec_decode/spec_decode_worker.py +472 -0
- vllm/spec_decode/top1_proposer.py +200 -0
- vllm/spec_decode/util.py +228 -0
- vllm/test_utils.py +41 -0
- vllm/transformers_utils/__init__.py +0 -0
- vllm/transformers_utils/config.py +58 -0
- vllm/transformers_utils/configs/__init__.py +16 -0
- vllm/transformers_utils/configs/chatglm.py +68 -0
- vllm/transformers_utils/configs/dbrx.py +278 -0
- vllm/transformers_utils/configs/falcon.py +87 -0
- vllm/transformers_utils/configs/jais.py +236 -0
- vllm/transformers_utils/configs/mpt.py +178 -0
- vllm/transformers_utils/detokenizer.py +313 -0
- vllm/transformers_utils/tokenizer.py +149 -0
- vllm/transformers_utils/tokenizer_group/__init__.py +33 -0
- vllm/transformers_utils/tokenizer_group/base_tokenizer_group.py +55 -0
- vllm/transformers_utils/tokenizer_group/ray_tokenizer_group.py +169 -0
- vllm/transformers_utils/tokenizer_group/tokenizer_group.py +78 -0
- vllm/transformers_utils/tokenizers/__init__.py +5 -0
- vllm/transformers_utils/tokenizers/baichuan.py +255 -0
- vllm/usage/__init__.py +0 -0
- vllm/usage/usage_lib.py +209 -0
- vllm/utils.py +677 -0
- vllm/worker/__init__.py +0 -0
- vllm/worker/cache_engine.py +105 -0
- vllm/worker/cpu_model_runner.py +346 -0
- vllm/worker/cpu_worker.py +321 -0
- vllm/worker/model_runner.py +1168 -0
- vllm/worker/neuron_model_runner.py +196 -0
- vllm/worker/neuron_worker.py +98 -0
- vllm/worker/worker.py +345 -0
- vllm/worker/worker_base.py +146 -0
- vllm_npu-0.4.2.dist-info/LICENSE +201 -0
- vllm_npu-0.4.2.dist-info/METADATA +173 -0
- vllm_npu-0.4.2.dist-info/RECORD +219 -0
- vllm_npu-0.4.2.dist-info/WHEEL +5 -0
- vllm_npu-0.4.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,301 @@
|
|
1
|
+
# coding=utf-8
|
2
|
+
# Copyright 2023 Stability AI, EleutherAI, and The HuggingFace Inc. team.
|
3
|
+
# All rights reserved.
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
# This code is based off the following work:
|
18
|
+
# https://huggingface.co/stabilityai/stablelm-3b-4e1t/blob/main/modeling_stablelm_epoch.py
|
19
|
+
# https://huggingface.co/stabilityai/stablelm-3b-4e1t/blob/main/config.json
|
20
|
+
"""Inference-only StabeLM (https://github.com/Stability-AI/StableLM)
|
21
|
+
model compatible with HuggingFace weights."""
|
22
|
+
from typing import Iterable, List, Optional, Tuple
|
23
|
+
|
24
|
+
import torch
|
25
|
+
from torch import nn
|
26
|
+
from transformers import PretrainedConfig
|
27
|
+
|
28
|
+
from vllm.attention import Attention, AttentionMetadata
|
29
|
+
from vllm.distributed import get_tensor_model_parallel_world_size
|
30
|
+
from vllm.model_executor.layers.activation import SiluAndMul
|
31
|
+
from vllm.model_executor.layers.linear import (MergedColumnParallelLinear,
|
32
|
+
QKVParallelLinear,
|
33
|
+
RowParallelLinear)
|
34
|
+
from vllm.model_executor.layers.logits_processor import LogitsProcessor
|
35
|
+
from vllm.model_executor.layers.quantization.base_config import (
|
36
|
+
QuantizationConfig)
|
37
|
+
from vllm.model_executor.layers.rotary_embedding import get_rope
|
38
|
+
from vllm.model_executor.layers.sampler import Sampler
|
39
|
+
from vllm.model_executor.layers.vocab_parallel_embedding import (
|
40
|
+
ParallelLMHead, VocabParallelEmbedding)
|
41
|
+
from vllm.model_executor.model_loader.weight_utils import default_weight_loader
|
42
|
+
from vllm.model_executor.sampling_metadata import SamplingMetadata
|
43
|
+
from vllm.sequence import SamplerOutput
|
44
|
+
|
45
|
+
|
46
|
+
class StablelmMLP(nn.Module):
|
47
|
+
|
48
|
+
def __init__(self,
|
49
|
+
config: PretrainedConfig,
|
50
|
+
quant_config: Optional[QuantizationConfig] = None) -> None:
|
51
|
+
super().__init__()
|
52
|
+
self.config = config
|
53
|
+
self.hidden_size = config.hidden_size
|
54
|
+
self.intermediate_size = config.intermediate_size
|
55
|
+
self.gate_up_proj = MergedColumnParallelLinear(
|
56
|
+
config.hidden_size, [config.intermediate_size] * 2,
|
57
|
+
bias=False,
|
58
|
+
quant_config=quant_config)
|
59
|
+
self.down_proj = RowParallelLinear(config.intermediate_size,
|
60
|
+
config.hidden_size,
|
61
|
+
bias=False)
|
62
|
+
self.act_fn = SiluAndMul()
|
63
|
+
|
64
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
65
|
+
gate_up, _ = self.gate_up_proj(x)
|
66
|
+
x = self.act_fn(gate_up)
|
67
|
+
x, _ = self.down_proj(x)
|
68
|
+
return x
|
69
|
+
|
70
|
+
|
71
|
+
class StablelmAttention(nn.Module):
|
72
|
+
|
73
|
+
def __init__(self,
|
74
|
+
config: PretrainedConfig,
|
75
|
+
quant_config: Optional[QuantizationConfig] = None) -> None:
|
76
|
+
super().__init__()
|
77
|
+
self.config = config
|
78
|
+
self.hidden_size = config.hidden_size
|
79
|
+
tp_size = get_tensor_model_parallel_world_size()
|
80
|
+
self.total_num_heads = config.num_attention_heads
|
81
|
+
self.num_heads = self.total_num_heads // tp_size
|
82
|
+
|
83
|
+
self.total_num_key_value_heads = config.num_key_value_heads
|
84
|
+
if self.total_num_key_value_heads >= tp_size:
|
85
|
+
# Number of KV heads is greater than TP size, so we partition
|
86
|
+
# the KV heads across multiple tensor parallel GPUs.
|
87
|
+
assert self.total_num_key_value_heads % tp_size == 0
|
88
|
+
else:
|
89
|
+
# Number of KV heads is less than TP size, so we replicate
|
90
|
+
# the KV heads across multiple tensor parallel GPUs.
|
91
|
+
assert tp_size % self.total_num_key_value_heads == 0
|
92
|
+
self.num_key_value_heads = max(
|
93
|
+
1, self.total_num_key_value_heads // tp_size)
|
94
|
+
self.head_dim = self.hidden_size // self.total_num_heads
|
95
|
+
self.max_position_embeddings = config.max_position_embeddings
|
96
|
+
rope_pct = getattr(config, "rope_pct",
|
97
|
+
getattr(config, "partial_rotary_factor", 1))
|
98
|
+
self.rotary_ndims = int(self.head_dim * rope_pct)
|
99
|
+
self.scaling = self.head_dim**-0.5
|
100
|
+
self.q_size = self.num_heads * self.head_dim
|
101
|
+
self.kv_size = self.num_key_value_heads * self.head_dim
|
102
|
+
self.qkv_bias = getattr(config, "use_qkv_bias", False)
|
103
|
+
if (self.head_dim * self.num_heads * tp_size) != self.hidden_size:
|
104
|
+
raise ValueError(f"hidden_size must be divisible by num_heads "
|
105
|
+
f"(got `hidden_size`: {self.hidden_size}"
|
106
|
+
f" and `num_heads`: {self.num_heads}).")
|
107
|
+
|
108
|
+
self.qkv_proj = QKVParallelLinear(self.hidden_size,
|
109
|
+
self.head_dim,
|
110
|
+
self.total_num_heads,
|
111
|
+
self.total_num_key_value_heads,
|
112
|
+
self.qkv_bias,
|
113
|
+
quant_config=quant_config)
|
114
|
+
self.o_proj = RowParallelLinear(self.total_num_heads * self.head_dim,
|
115
|
+
self.hidden_size,
|
116
|
+
bias=False,
|
117
|
+
quant_config=quant_config)
|
118
|
+
self.rotary_emb = get_rope(
|
119
|
+
self.head_dim,
|
120
|
+
rotary_dim=self.rotary_ndims,
|
121
|
+
max_position=self.config.max_position_embeddings,
|
122
|
+
base=self.config.rope_theta,
|
123
|
+
)
|
124
|
+
self.attn = Attention(self.num_heads,
|
125
|
+
self.head_dim,
|
126
|
+
self.scaling,
|
127
|
+
num_kv_heads=self.num_key_value_heads)
|
128
|
+
|
129
|
+
def forward(
|
130
|
+
self,
|
131
|
+
positions: torch.Tensor,
|
132
|
+
hidden_states: torch.Tensor,
|
133
|
+
kv_cache: torch.Tensor,
|
134
|
+
attn_metadata: AttentionMetadata,
|
135
|
+
) -> torch.Tensor:
|
136
|
+
qkv, _ = self.qkv_proj(hidden_states)
|
137
|
+
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
138
|
+
q, k = self.rotary_emb(positions, q, k)
|
139
|
+
attn_output = self.attn(q, k, v, kv_cache, attn_metadata)
|
140
|
+
output, _ = self.o_proj(attn_output)
|
141
|
+
return output
|
142
|
+
|
143
|
+
|
144
|
+
class StablelmDecoderLayer(nn.Module):
|
145
|
+
|
146
|
+
def __init__(
|
147
|
+
self,
|
148
|
+
config: PretrainedConfig,
|
149
|
+
quant_config: Optional[QuantizationConfig] = None,
|
150
|
+
) -> None:
|
151
|
+
super().__init__()
|
152
|
+
self.self_attn = StablelmAttention(config)
|
153
|
+
self.mlp = StablelmMLP(config, quant_config)
|
154
|
+
norm_eps = getattr(config, "norm_eps",
|
155
|
+
getattr(config, "layer_norm_eps", 1e-05))
|
156
|
+
self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=norm_eps)
|
157
|
+
self.post_attention_layernorm = nn.LayerNorm(config.hidden_size,
|
158
|
+
eps=norm_eps)
|
159
|
+
|
160
|
+
def forward(
|
161
|
+
self,
|
162
|
+
positions: torch.Tensor,
|
163
|
+
hidden_states: torch.Tensor,
|
164
|
+
kv_cache: torch.Tensor,
|
165
|
+
attn_metadata: AttentionMetadata,
|
166
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
167
|
+
# Self Attention
|
168
|
+
residual = hidden_states
|
169
|
+
hidden_states = self.input_layernorm(hidden_states)
|
170
|
+
hidden_states = self.self_attn(
|
171
|
+
positions=positions,
|
172
|
+
hidden_states=hidden_states,
|
173
|
+
kv_cache=kv_cache,
|
174
|
+
attn_metadata=attn_metadata,
|
175
|
+
)
|
176
|
+
hidden_states = residual + hidden_states
|
177
|
+
|
178
|
+
# Fully Connected
|
179
|
+
residual = hidden_states
|
180
|
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
181
|
+
hidden_states = self.mlp(hidden_states)
|
182
|
+
hidden_states = residual + hidden_states
|
183
|
+
|
184
|
+
return hidden_states, residual
|
185
|
+
|
186
|
+
|
187
|
+
class StableLMEpochModel(nn.Module):
|
188
|
+
|
189
|
+
def __init__(self,
|
190
|
+
config: PretrainedConfig,
|
191
|
+
quant_config: Optional[QuantizationConfig] = None) -> None:
|
192
|
+
super().__init__()
|
193
|
+
self.embed_tokens = VocabParallelEmbedding(
|
194
|
+
config.vocab_size,
|
195
|
+
config.hidden_size,
|
196
|
+
)
|
197
|
+
self.layers = nn.ModuleList([
|
198
|
+
StablelmDecoderLayer(config, quant_config)
|
199
|
+
for _ in range(config.num_hidden_layers)
|
200
|
+
])
|
201
|
+
norm_eps = getattr(config, "norm_eps",
|
202
|
+
getattr(config, "layer_norm_eps", 1e-05))
|
203
|
+
self.norm = nn.LayerNorm(config.hidden_size, eps=norm_eps)
|
204
|
+
|
205
|
+
def forward(
|
206
|
+
self,
|
207
|
+
input_ids: torch.Tensor,
|
208
|
+
positions: torch.Tensor,
|
209
|
+
kv_caches: List[torch.Tensor],
|
210
|
+
attn_metadata: AttentionMetadata,
|
211
|
+
) -> torch.Tensor:
|
212
|
+
hidden_states = self.embed_tokens(input_ids)
|
213
|
+
for i in range(len(self.layers)):
|
214
|
+
layer = self.layers[i]
|
215
|
+
hidden_states, residual = layer(
|
216
|
+
positions,
|
217
|
+
hidden_states,
|
218
|
+
kv_caches[i],
|
219
|
+
attn_metadata,
|
220
|
+
)
|
221
|
+
hidden_states = self.norm(hidden_states)
|
222
|
+
return hidden_states
|
223
|
+
|
224
|
+
|
225
|
+
class StablelmForCausalLM(nn.Module):
|
226
|
+
|
227
|
+
def __init__(
|
228
|
+
self,
|
229
|
+
config: PretrainedConfig,
|
230
|
+
quant_config: Optional[QuantizationConfig] = None,
|
231
|
+
) -> None:
|
232
|
+
super().__init__()
|
233
|
+
self.config = config
|
234
|
+
self.quant_config = quant_config
|
235
|
+
self.model = StableLMEpochModel(config, quant_config)
|
236
|
+
self.lm_head = ParallelLMHead(config.vocab_size, config.hidden_size)
|
237
|
+
self.logits_processor = LogitsProcessor(config.vocab_size)
|
238
|
+
self.sampler = Sampler()
|
239
|
+
|
240
|
+
def forward(
|
241
|
+
self,
|
242
|
+
input_ids: torch.Tensor,
|
243
|
+
positions: torch.Tensor,
|
244
|
+
kv_caches: List[torch.Tensor],
|
245
|
+
attn_metadata: AttentionMetadata,
|
246
|
+
) -> torch.Tensor:
|
247
|
+
hidden_states = self.model(input_ids, positions, kv_caches,
|
248
|
+
attn_metadata)
|
249
|
+
return hidden_states
|
250
|
+
|
251
|
+
def compute_logits(self, hidden_states: torch.Tensor,
|
252
|
+
sampling_metadata: SamplingMetadata) -> torch.Tensor:
|
253
|
+
logits = self.logits_processor(self.lm_head.weight, hidden_states,
|
254
|
+
sampling_metadata)
|
255
|
+
return logits
|
256
|
+
|
257
|
+
def sample(
|
258
|
+
self,
|
259
|
+
logits: torch.Tensor,
|
260
|
+
sampling_metadata: SamplingMetadata,
|
261
|
+
) -> Optional[SamplerOutput]:
|
262
|
+
next_tokens = self.sampler(logits, sampling_metadata)
|
263
|
+
return next_tokens
|
264
|
+
|
265
|
+
def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
|
266
|
+
stacked_params_mapping = [
|
267
|
+
# (param_name, shard_name, shard_id)
|
268
|
+
("qkv_proj", "q_proj", "q"),
|
269
|
+
("qkv_proj", "k_proj", "k"),
|
270
|
+
("qkv_proj", "v_proj", "v"),
|
271
|
+
("gate_up_proj", "gate_proj", 0),
|
272
|
+
("gate_up_proj", "up_proj", 1),
|
273
|
+
]
|
274
|
+
params_dict = dict(self.named_parameters())
|
275
|
+
for name, loaded_weight in weights:
|
276
|
+
if "rotary_emb.inv_freq" in name:
|
277
|
+
continue
|
278
|
+
if ("rotary_emb.cos_cached" in name
|
279
|
+
or "rotary_emb.sin_cached" in name):
|
280
|
+
# Models trained using ColossalAI may include these tensors in
|
281
|
+
# the checkpoint. Skip them.
|
282
|
+
continue
|
283
|
+
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
284
|
+
if weight_name not in name:
|
285
|
+
continue
|
286
|
+
name = name.replace(weight_name, param_name)
|
287
|
+
# Skip loading extra bias for GPTQ models.
|
288
|
+
if name.endswith(".bias") and name not in params_dict:
|
289
|
+
continue
|
290
|
+
param = params_dict[name]
|
291
|
+
weight_loader = param.weight_loader
|
292
|
+
weight_loader(param, loaded_weight, shard_id)
|
293
|
+
break
|
294
|
+
else:
|
295
|
+
# Skip loading extra bias for GPTQ models.
|
296
|
+
if name.endswith(".bias") and name not in params_dict:
|
297
|
+
continue
|
298
|
+
param = params_dict[name]
|
299
|
+
weight_loader = getattr(param, "weight_loader",
|
300
|
+
default_weight_loader)
|
301
|
+
weight_loader(param, loaded_weight)
|
@@ -0,0 +1,302 @@
|
|
1
|
+
# coding=utf-8
|
2
|
+
# Copyright 2024 BigCode and the HuggingFace Inc. team. All rights reserved.
|
3
|
+
#
|
4
|
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5
|
+
# and OPT implementations in this library. It has been modified from its
|
6
|
+
# original forms to accommodate minor architectural differences compared
|
7
|
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8
|
+
#
|
9
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10
|
+
# you may not use this file except in compliance with the License.
|
11
|
+
# You may obtain a copy of the License at
|
12
|
+
#
|
13
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
|
+
#
|
15
|
+
# Unless required by applicable law or agreed to in writing, software
|
16
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18
|
+
# See the License for the specific language governing permissions and
|
19
|
+
# limitations under the License.
|
20
|
+
""" PyTorch Starcoder2 model."""
|
21
|
+
from typing import Iterable, List, Optional, Tuple
|
22
|
+
|
23
|
+
import torch
|
24
|
+
from torch import nn
|
25
|
+
from transformers import Starcoder2Config
|
26
|
+
|
27
|
+
from vllm.attention import Attention, AttentionMetadata
|
28
|
+
from vllm.distributed import get_tensor_model_parallel_world_size
|
29
|
+
from vllm.model_executor.layers.activation import get_act_fn
|
30
|
+
from vllm.model_executor.layers.linear import (ColumnParallelLinear,
|
31
|
+
QKVParallelLinear,
|
32
|
+
RowParallelLinear)
|
33
|
+
from vllm.model_executor.layers.logits_processor import LogitsProcessor
|
34
|
+
from vllm.model_executor.layers.quantization.base_config import (
|
35
|
+
QuantizationConfig)
|
36
|
+
from vllm.model_executor.layers.rotary_embedding import get_rope
|
37
|
+
from vllm.model_executor.layers.sampler import Sampler
|
38
|
+
from vllm.model_executor.layers.vocab_parallel_embedding import (
|
39
|
+
DEFAULT_VOCAB_PADDING_SIZE, ParallelLMHead, VocabParallelEmbedding)
|
40
|
+
from vllm.model_executor.model_loader.weight_utils import default_weight_loader
|
41
|
+
from vllm.model_executor.sampling_metadata import SamplingMetadata
|
42
|
+
from vllm.sequence import SamplerOutput
|
43
|
+
|
44
|
+
|
45
|
+
class Starcoder2Attention(nn.Module):
|
46
|
+
|
47
|
+
def __init__(self,
|
48
|
+
config: Starcoder2Config,
|
49
|
+
quant_config: Optional[QuantizationConfig] = None):
|
50
|
+
super().__init__()
|
51
|
+
self.config = config
|
52
|
+
|
53
|
+
self.hidden_size = config.hidden_size
|
54
|
+
tp_size = get_tensor_model_parallel_world_size()
|
55
|
+
self.total_num_heads = config.num_attention_heads
|
56
|
+
assert self.total_num_heads % tp_size == 0
|
57
|
+
self.num_heads = self.total_num_heads // tp_size
|
58
|
+
self.total_num_kv_heads = config.num_key_value_heads
|
59
|
+
if self.total_num_kv_heads >= tp_size:
|
60
|
+
# Number of KV heads is greater than TP size, so we partition
|
61
|
+
# the KV heads across multiple tensor parallel GPUs.
|
62
|
+
assert self.total_num_kv_heads % tp_size == 0
|
63
|
+
else:
|
64
|
+
# Number of KV heads is less than TP size, so we replicate
|
65
|
+
# the KV heads across multiple tensor parallel GPUs.
|
66
|
+
assert tp_size % self.total_num_kv_heads == 0
|
67
|
+
self.num_kv_heads = max(1, self.total_num_kv_heads // tp_size)
|
68
|
+
self.head_dim = self.hidden_size // self.total_num_heads
|
69
|
+
self.q_size = self.num_heads * self.head_dim
|
70
|
+
self.kv_size = self.num_kv_heads * self.head_dim
|
71
|
+
self.scaling = self.head_dim**-0.5
|
72
|
+
self.rope_theta = config.rope_theta
|
73
|
+
self.max_position_embeddings = config.max_position_embeddings
|
74
|
+
self.use_bias = config.use_bias
|
75
|
+
self.sliding_window = config.sliding_window
|
76
|
+
|
77
|
+
self.qkv_proj = QKVParallelLinear(
|
78
|
+
self.hidden_size,
|
79
|
+
self.head_dim,
|
80
|
+
self.total_num_heads,
|
81
|
+
self.total_num_kv_heads,
|
82
|
+
bias=self.use_bias,
|
83
|
+
quant_config=quant_config,
|
84
|
+
)
|
85
|
+
self.o_proj = RowParallelLinear(
|
86
|
+
self.total_num_heads * self.head_dim,
|
87
|
+
self.hidden_size,
|
88
|
+
bias=self.use_bias,
|
89
|
+
quant_config=quant_config,
|
90
|
+
)
|
91
|
+
self.rotary_emb = get_rope(
|
92
|
+
self.head_dim,
|
93
|
+
rotary_dim=self.head_dim,
|
94
|
+
max_position=self.max_position_embeddings,
|
95
|
+
base=int(self.rope_theta),
|
96
|
+
is_neox_style=True,
|
97
|
+
)
|
98
|
+
self.attn = Attention(
|
99
|
+
self.num_heads,
|
100
|
+
self.head_dim,
|
101
|
+
self.scaling,
|
102
|
+
num_kv_heads=self.num_kv_heads,
|
103
|
+
sliding_window=self.sliding_window,
|
104
|
+
)
|
105
|
+
|
106
|
+
def forward(
|
107
|
+
self,
|
108
|
+
positions: torch.Tensor,
|
109
|
+
hidden_states: torch.Tensor,
|
110
|
+
kv_cache: torch.Tensor,
|
111
|
+
attn_metadata: AttentionMetadata,
|
112
|
+
) -> torch.Tensor:
|
113
|
+
qkv, _ = self.qkv_proj(hidden_states)
|
114
|
+
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
115
|
+
q, k = self.rotary_emb(positions, q, k)
|
116
|
+
attn_output = self.attn(q, k, v, kv_cache, attn_metadata)
|
117
|
+
output, _ = self.o_proj(attn_output)
|
118
|
+
return output
|
119
|
+
|
120
|
+
|
121
|
+
class Starcoder2MLP(nn.Module):
|
122
|
+
|
123
|
+
def __init__(self,
|
124
|
+
config: Starcoder2Config,
|
125
|
+
quant_config: Optional[QuantizationConfig] = None):
|
126
|
+
super().__init__()
|
127
|
+
self.c_fc = ColumnParallelLinear(
|
128
|
+
config.hidden_size,
|
129
|
+
config.intermediate_size,
|
130
|
+
bias=config.use_bias,
|
131
|
+
quant_config=quant_config,
|
132
|
+
)
|
133
|
+
self.c_proj = RowParallelLinear(
|
134
|
+
config.intermediate_size,
|
135
|
+
config.hidden_size,
|
136
|
+
bias=config.use_bias,
|
137
|
+
quant_config=quant_config,
|
138
|
+
)
|
139
|
+
self.act = get_act_fn(config.hidden_act, quant_config,
|
140
|
+
config.intermediate_size)
|
141
|
+
|
142
|
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
143
|
+
hidden_states, _ = self.c_fc(hidden_states)
|
144
|
+
hidden_states = self.act(hidden_states)
|
145
|
+
hidden_states, _ = self.c_proj(hidden_states)
|
146
|
+
return hidden_states
|
147
|
+
|
148
|
+
|
149
|
+
class Starcoder2DecoderLayer(nn.Module):
|
150
|
+
|
151
|
+
def __init__(self,
|
152
|
+
config: Starcoder2Config,
|
153
|
+
quant_config: Optional[QuantizationConfig] = None):
|
154
|
+
super().__init__()
|
155
|
+
self.hidden_size = config.hidden_size
|
156
|
+
self.self_attn = Starcoder2Attention(config, quant_config=quant_config)
|
157
|
+
self.mlp = Starcoder2MLP(config, quant_config=quant_config)
|
158
|
+
self.input_layernorm = nn.LayerNorm(config.hidden_size,
|
159
|
+
eps=config.norm_epsilon)
|
160
|
+
self.post_attention_layernorm = nn.LayerNorm(config.hidden_size,
|
161
|
+
eps=config.norm_epsilon)
|
162
|
+
|
163
|
+
def forward(
|
164
|
+
self,
|
165
|
+
positions: torch.Tensor,
|
166
|
+
hidden_states: torch.Tensor,
|
167
|
+
kv_cache: torch.Tensor,
|
168
|
+
attn_metadata: AttentionMetadata,
|
169
|
+
) -> torch.Tensor:
|
170
|
+
# Self Attention
|
171
|
+
residual = hidden_states
|
172
|
+
hidden_states = self.input_layernorm(hidden_states)
|
173
|
+
hidden_states = self.self_attn(
|
174
|
+
positions=positions,
|
175
|
+
hidden_states=hidden_states,
|
176
|
+
kv_cache=kv_cache,
|
177
|
+
attn_metadata=attn_metadata,
|
178
|
+
)
|
179
|
+
hidden_states = residual + hidden_states
|
180
|
+
|
181
|
+
# Fully Connected
|
182
|
+
residual = hidden_states
|
183
|
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
184
|
+
hidden_states = self.mlp(hidden_states)
|
185
|
+
hidden_states = residual + hidden_states
|
186
|
+
|
187
|
+
return hidden_states
|
188
|
+
|
189
|
+
|
190
|
+
class Starcoder2Model(nn.Module):
|
191
|
+
|
192
|
+
def __init__(self,
|
193
|
+
config: Starcoder2Config,
|
194
|
+
quant_config: Optional[QuantizationConfig] = None):
|
195
|
+
super().__init__()
|
196
|
+
self.config = config
|
197
|
+
self.padding_idx = config.pad_token_id
|
198
|
+
self.vocab_size = config.vocab_size
|
199
|
+
|
200
|
+
# TODO: consider padding_idx (currently removed)
|
201
|
+
self.embed_tokens = VocabParallelEmbedding(config.vocab_size,
|
202
|
+
config.hidden_size)
|
203
|
+
self.layers = nn.ModuleList([
|
204
|
+
Starcoder2DecoderLayer(config, quant_config=quant_config)
|
205
|
+
for _ in range(config.num_hidden_layers)
|
206
|
+
])
|
207
|
+
self.norm = nn.LayerNorm(config.hidden_size, eps=config.norm_epsilon)
|
208
|
+
|
209
|
+
def forward(
|
210
|
+
self,
|
211
|
+
input_ids: torch.Tensor,
|
212
|
+
positions: torch.Tensor,
|
213
|
+
kv_caches: List[torch.Tensor],
|
214
|
+
attn_metadata: AttentionMetadata,
|
215
|
+
) -> torch.Tensor:
|
216
|
+
hidden_states = self.embed_tokens(input_ids)
|
217
|
+
for i in range(len(self.layers)):
|
218
|
+
layer = self.layers[i]
|
219
|
+
hidden_states = layer(positions, hidden_states, kv_caches[i],
|
220
|
+
attn_metadata)
|
221
|
+
hidden_states = self.norm(hidden_states)
|
222
|
+
return hidden_states
|
223
|
+
|
224
|
+
|
225
|
+
class Starcoder2ForCausalLM(nn.Module):
|
226
|
+
|
227
|
+
def __init__(self,
|
228
|
+
config: Starcoder2Config,
|
229
|
+
quant_config: Optional[QuantizationConfig] = None):
|
230
|
+
super().__init__()
|
231
|
+
self.config = config
|
232
|
+
self.model = Starcoder2Model(config, quant_config=quant_config)
|
233
|
+
self.vocab_size = config.vocab_size
|
234
|
+
self.unpadded_vocab_size = config.vocab_size
|
235
|
+
if config.tie_word_embeddings:
|
236
|
+
self.lm_head_weight = self.model.embed_tokens.weight
|
237
|
+
else:
|
238
|
+
self.unpadded_vocab_size = config.vocab_size
|
239
|
+
self.lm_head = ParallelLMHead(
|
240
|
+
self.unpadded_vocab_size,
|
241
|
+
config.hidden_size,
|
242
|
+
org_num_embeddings=config.vocab_size,
|
243
|
+
padding_size=DEFAULT_VOCAB_PADDING_SIZE,
|
244
|
+
)
|
245
|
+
self.lm_head_weight = self.lm_head.weight
|
246
|
+
self.logits_processor = LogitsProcessor(self.unpadded_vocab_size,
|
247
|
+
config.vocab_size)
|
248
|
+
self.sampler = Sampler()
|
249
|
+
|
250
|
+
def forward(
|
251
|
+
self,
|
252
|
+
input_ids: torch.Tensor,
|
253
|
+
positions: torch.Tensor,
|
254
|
+
kv_caches: List[torch.Tensor],
|
255
|
+
attn_metadata: AttentionMetadata,
|
256
|
+
) -> torch.Tensor:
|
257
|
+
hidden_states = self.model(input_ids, positions, kv_caches,
|
258
|
+
attn_metadata)
|
259
|
+
return hidden_states
|
260
|
+
|
261
|
+
def compute_logits(self, hidden_states: torch.Tensor,
|
262
|
+
sampling_metadata: SamplingMetadata) -> torch.Tensor:
|
263
|
+
logits = self.logits_processor(self.lm_head_weight, hidden_states,
|
264
|
+
sampling_metadata)
|
265
|
+
return logits
|
266
|
+
|
267
|
+
def sample(
|
268
|
+
self,
|
269
|
+
logits: Optional[torch.Tensor],
|
270
|
+
sampling_metadata: SamplingMetadata,
|
271
|
+
) -> Optional[SamplerOutput]:
|
272
|
+
next_tokens = self.sampler(logits, sampling_metadata)
|
273
|
+
return next_tokens
|
274
|
+
|
275
|
+
def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
|
276
|
+
stacked_params_mapping = [
|
277
|
+
# (param_name, shard_name, shard_id)
|
278
|
+
("qkv_proj", "q_proj", "q"),
|
279
|
+
("qkv_proj", "k_proj", "k"),
|
280
|
+
("qkv_proj", "v_proj", "v"),
|
281
|
+
]
|
282
|
+
|
283
|
+
params_dict = dict(self.named_parameters(remove_duplicate=False))
|
284
|
+
for name, loaded_weight in weights:
|
285
|
+
if "rotary_emb.inv_freq" in name:
|
286
|
+
continue
|
287
|
+
|
288
|
+
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
289
|
+
if weight_name not in name:
|
290
|
+
continue
|
291
|
+
name = name.replace(weight_name, param_name)
|
292
|
+
param = params_dict[name]
|
293
|
+
weight_loader = param.weight_loader
|
294
|
+
weight_loader(param, loaded_weight, shard_id)
|
295
|
+
break
|
296
|
+
else:
|
297
|
+
if self.config.tie_word_embeddings and "lm_head.weight" in name:
|
298
|
+
continue
|
299
|
+
param = params_dict[name]
|
300
|
+
weight_loader = getattr(param, "weight_loader",
|
301
|
+
default_weight_loader)
|
302
|
+
weight_loader(param, loaded_weight)
|