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,333 @@
|
|
1
|
+
# coding=utf-8
|
2
|
+
# Adapted from
|
3
|
+
# https://huggingface.co/core42/jais-30b-chat-v3/blob/main/modeling_jais.py
|
4
|
+
# Copyright 2023 The vLLM team.
|
5
|
+
# Copyright 2023 the Jais authors and HuggingFace Inc. team. All rights
|
6
|
+
# reserved.
|
7
|
+
# Copyright 2023 Cerebras Systems.
|
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
|
+
"""Inference-only Jais model compatible with HuggingFace weights."""
|
21
|
+
|
22
|
+
import math
|
23
|
+
from typing import Iterable, List, Optional, Tuple
|
24
|
+
|
25
|
+
import torch
|
26
|
+
from torch import nn
|
27
|
+
|
28
|
+
from vllm.attention import Attention, AttentionMetadata
|
29
|
+
from vllm.distributed import (get_tensor_model_parallel_rank,
|
30
|
+
get_tensor_model_parallel_world_size)
|
31
|
+
from vllm.model_executor.layers.linear import (ColumnParallelLinear,
|
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.sampler import Sampler
|
38
|
+
from vllm.model_executor.layers.vocab_parallel_embedding import (
|
39
|
+
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
|
+
from vllm.transformers_utils.configs import JAISConfig
|
44
|
+
|
45
|
+
|
46
|
+
class SwiGLUActivation(nn.Module):
|
47
|
+
|
48
|
+
def forward(self, x1: torch.Tensor, x2: torch.Tensor) -> torch.Tensor:
|
49
|
+
return x1 * nn.functional.silu(x2)
|
50
|
+
|
51
|
+
|
52
|
+
def _get_alibi_slopes(n):
|
53
|
+
|
54
|
+
def get_slopes_power_of_2(n):
|
55
|
+
start = 2**(-(2**-(math.log2(n) - 3)))
|
56
|
+
ratio = start
|
57
|
+
return [start * ratio**i for i in range(n)]
|
58
|
+
|
59
|
+
if math.log2(n).is_integer():
|
60
|
+
return get_slopes_power_of_2(n)
|
61
|
+
else:
|
62
|
+
closest_power_of_2 = 2**math.floor(math.log2(n))
|
63
|
+
return (get_slopes_power_of_2(closest_power_of_2) + _get_alibi_slopes(
|
64
|
+
2 * closest_power_of_2)[0::2][:n - closest_power_of_2])
|
65
|
+
|
66
|
+
|
67
|
+
class JAISAttention(nn.Module):
|
68
|
+
|
69
|
+
def __init__(
|
70
|
+
self,
|
71
|
+
config: JAISConfig,
|
72
|
+
quant_config: Optional[QuantizationConfig] = None,
|
73
|
+
):
|
74
|
+
super().__init__()
|
75
|
+
self.hidden_size = config.hidden_size
|
76
|
+
total_num_heads = config.num_attention_heads
|
77
|
+
tensor_model_parallel_world_size = (
|
78
|
+
get_tensor_model_parallel_world_size())
|
79
|
+
assert total_num_heads % tensor_model_parallel_world_size == 0
|
80
|
+
self.num_heads = total_num_heads // tensor_model_parallel_world_size
|
81
|
+
self.head_dim = self.hidden_size // total_num_heads
|
82
|
+
if hasattr(config, "scale_qk_dot_by_d"):
|
83
|
+
config.mup_scale_qk_dot_by_d = config.scale_qk_dot_by_d
|
84
|
+
self.attn_scale_power = 1.0 if config.mup_scale_qk_dot_by_d else 0.5
|
85
|
+
self.scale = self.head_dim**-self.attn_scale_power
|
86
|
+
|
87
|
+
self.c_attn = QKVParallelLinear(
|
88
|
+
self.hidden_size,
|
89
|
+
self.head_dim,
|
90
|
+
total_num_heads,
|
91
|
+
bias=True,
|
92
|
+
quant_config=quant_config,
|
93
|
+
)
|
94
|
+
self.c_proj = RowParallelLinear(
|
95
|
+
self.hidden_size,
|
96
|
+
self.hidden_size,
|
97
|
+
bias=True,
|
98
|
+
quant_config=quant_config,
|
99
|
+
)
|
100
|
+
|
101
|
+
tp_rank = get_tensor_model_parallel_rank()
|
102
|
+
head_start = tp_rank * self.num_heads
|
103
|
+
head_end = (tp_rank + 1) * self.num_heads
|
104
|
+
alibi_slopes = _get_alibi_slopes(total_num_heads)
|
105
|
+
alibi_slopes = alibi_slopes[head_start:head_end]
|
106
|
+
self.attn = Attention(
|
107
|
+
self.num_heads,
|
108
|
+
self.head_dim,
|
109
|
+
scale=self.scale,
|
110
|
+
alibi_slopes=alibi_slopes,
|
111
|
+
)
|
112
|
+
|
113
|
+
def forward(
|
114
|
+
self,
|
115
|
+
hidden_states: torch.Tensor,
|
116
|
+
kv_cache: torch.Tensor,
|
117
|
+
attn_metadata: AttentionMetadata,
|
118
|
+
) -> torch.Tensor:
|
119
|
+
qkv, _ = self.c_attn(hidden_states)
|
120
|
+
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
121
|
+
attn_output = self.attn(q, k, v, kv_cache, attn_metadata)
|
122
|
+
attn_output, _ = self.c_proj(attn_output)
|
123
|
+
return attn_output
|
124
|
+
|
125
|
+
|
126
|
+
class JAISMLP(nn.Module):
|
127
|
+
|
128
|
+
def __init__(
|
129
|
+
self,
|
130
|
+
intermediate_size: int,
|
131
|
+
config: JAISConfig,
|
132
|
+
quant_config: Optional[QuantizationConfig] = None,
|
133
|
+
):
|
134
|
+
super().__init__()
|
135
|
+
hidden_size = config.hidden_size
|
136
|
+
self.swiglu = config.activation_function == "swiglu"
|
137
|
+
self.c_fc = ColumnParallelLinear(
|
138
|
+
hidden_size,
|
139
|
+
intermediate_size,
|
140
|
+
bias=True,
|
141
|
+
quant_config=quant_config,
|
142
|
+
)
|
143
|
+
self.c_fc2 = (ColumnParallelLinear(
|
144
|
+
hidden_size,
|
145
|
+
intermediate_size,
|
146
|
+
bias=True,
|
147
|
+
quant_config=quant_config,
|
148
|
+
) if self.swiglu else None)
|
149
|
+
self.c_proj = RowParallelLinear(
|
150
|
+
intermediate_size,
|
151
|
+
hidden_size,
|
152
|
+
bias=True,
|
153
|
+
quant_config=quant_config,
|
154
|
+
)
|
155
|
+
|
156
|
+
self.act = SwiGLUActivation()
|
157
|
+
|
158
|
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
159
|
+
if self.swiglu:
|
160
|
+
hidden_states2, _ = self.c_fc2(hidden_states)
|
161
|
+
hidden_states, _ = self.c_fc(hidden_states)
|
162
|
+
hidden_states = (self.act(hidden_states, hidden_states2)
|
163
|
+
if self.swiglu else self.act(hidden_states))
|
164
|
+
hidden_states, _ = self.c_proj(hidden_states)
|
165
|
+
return hidden_states
|
166
|
+
|
167
|
+
|
168
|
+
class JAISBlock(nn.Module):
|
169
|
+
|
170
|
+
def __init__(
|
171
|
+
self,
|
172
|
+
config: JAISConfig,
|
173
|
+
quant_config: Optional[QuantizationConfig] = None,
|
174
|
+
):
|
175
|
+
super().__init__()
|
176
|
+
hidden_size = config.hidden_size
|
177
|
+
inner_dim = (config.n_inner if config.n_inner is not None else 4 *
|
178
|
+
hidden_size)
|
179
|
+
|
180
|
+
self.ln_1 = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
|
181
|
+
self.attn = JAISAttention(config, quant_config)
|
182
|
+
self.ln_2 = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
|
183
|
+
self.mlp = JAISMLP(inner_dim, config, quant_config)
|
184
|
+
|
185
|
+
def forward(
|
186
|
+
self,
|
187
|
+
hidden_states: torch.Tensor,
|
188
|
+
kv_cache: torch.Tensor,
|
189
|
+
attn_metadata: AttentionMetadata,
|
190
|
+
) -> torch.Tensor:
|
191
|
+
residual = hidden_states
|
192
|
+
hidden_states = self.ln_1(hidden_states)
|
193
|
+
attn_output = self.attn(
|
194
|
+
hidden_states=hidden_states,
|
195
|
+
kv_cache=kv_cache,
|
196
|
+
attn_metadata=attn_metadata,
|
197
|
+
)
|
198
|
+
# residual connection
|
199
|
+
hidden_states = attn_output + residual
|
200
|
+
|
201
|
+
residual = hidden_states
|
202
|
+
hidden_states = self.ln_2(hidden_states)
|
203
|
+
feed_forward_hidden_states = self.mlp(hidden_states)
|
204
|
+
# residual connection
|
205
|
+
hidden_states = residual + feed_forward_hidden_states
|
206
|
+
return hidden_states
|
207
|
+
|
208
|
+
|
209
|
+
class JAISModel(nn.Module):
|
210
|
+
|
211
|
+
def __init__(
|
212
|
+
self,
|
213
|
+
config: JAISConfig,
|
214
|
+
quant_config: Optional[QuantizationConfig] = None,
|
215
|
+
):
|
216
|
+
super().__init__()
|
217
|
+
self.config = config
|
218
|
+
assert not config.add_cross_attention
|
219
|
+
assert not config.scale_attn_by_inverse_layer_idx
|
220
|
+
assert not config.reorder_and_upcast_attn
|
221
|
+
self.embed_dim = config.hidden_size
|
222
|
+
self.wte = VocabParallelEmbedding(config.vocab_size, self.embed_dim)
|
223
|
+
self.wpe = (nn.Embedding(config.max_position_embeddings,
|
224
|
+
self.embed_dim)
|
225
|
+
if config.position_embedding_type != "alibi" else None)
|
226
|
+
if hasattr(config, "embeddings_scale"):
|
227
|
+
self.embeddings_scale = config.embeddings_scale
|
228
|
+
else:
|
229
|
+
self.embeddings_scale = config.mup_embeddings_scale
|
230
|
+
self.h = nn.ModuleList([
|
231
|
+
JAISBlock(config, quant_config)
|
232
|
+
for _ in range(config.num_hidden_layers)
|
233
|
+
])
|
234
|
+
self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon)
|
235
|
+
|
236
|
+
def forward(
|
237
|
+
self,
|
238
|
+
input_ids: torch.Tensor,
|
239
|
+
position_ids: torch.Tensor,
|
240
|
+
kv_caches: List[torch.Tensor],
|
241
|
+
attn_metadata: AttentionMetadata,
|
242
|
+
) -> torch.Tensor:
|
243
|
+
inputs_embeds = self.wte(input_ids)
|
244
|
+
if self.wpe is not None:
|
245
|
+
position_embeds = self.wpe(position_ids)
|
246
|
+
hidden_states = inputs_embeds + position_embeds
|
247
|
+
else:
|
248
|
+
hidden_states = inputs_embeds
|
249
|
+
hidden_states *= torch.tensor(float(self.embeddings_scale),
|
250
|
+
dtype=hidden_states.dtype)
|
251
|
+
|
252
|
+
for i in range(len(self.h)):
|
253
|
+
layer = self.h[i]
|
254
|
+
hidden_states = layer(hidden_states, kv_caches[i], attn_metadata)
|
255
|
+
|
256
|
+
hidden_states = self.ln_f(hidden_states)
|
257
|
+
return hidden_states
|
258
|
+
|
259
|
+
|
260
|
+
class JAISLMHeadModel(nn.Module):
|
261
|
+
|
262
|
+
def __init__(
|
263
|
+
self,
|
264
|
+
config: JAISConfig,
|
265
|
+
quant_config: Optional[QuantizationConfig] = None,
|
266
|
+
):
|
267
|
+
super().__init__()
|
268
|
+
self.config = config
|
269
|
+
self.quant_config = quant_config
|
270
|
+
self.transformer = JAISModel(config, quant_config)
|
271
|
+
self.lm_head_weight = self.transformer.wte.weight
|
272
|
+
if hasattr(config, "width_scale"):
|
273
|
+
self.output_logits_scale = config.width_scale
|
274
|
+
else:
|
275
|
+
self.output_logits_scale = (config.mup_output_alpha *
|
276
|
+
config.mup_width_scale)
|
277
|
+
self.logits_processor = LogitsProcessor(vocab_size=config.vocab_size,
|
278
|
+
scale=self.output_logits_scale)
|
279
|
+
self.sampler = Sampler()
|
280
|
+
|
281
|
+
def forward(
|
282
|
+
self,
|
283
|
+
input_ids: torch.Tensor,
|
284
|
+
positions: torch.Tensor,
|
285
|
+
kv_caches: List[torch.Tensor],
|
286
|
+
attn_metadata: AttentionMetadata,
|
287
|
+
) -> torch.Tensor:
|
288
|
+
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
289
|
+
attn_metadata)
|
290
|
+
return hidden_states
|
291
|
+
|
292
|
+
def compute_logits(self, hidden_states: torch.Tensor,
|
293
|
+
sampling_metadata: SamplingMetadata) -> torch.Tensor:
|
294
|
+
logits = self.logits_processor(self.lm_head_weight, hidden_states,
|
295
|
+
sampling_metadata)
|
296
|
+
return logits
|
297
|
+
|
298
|
+
def sample(
|
299
|
+
self,
|
300
|
+
logits: torch.Tensor,
|
301
|
+
sampling_metadata: SamplingMetadata,
|
302
|
+
) -> Optional[SamplerOutput]:
|
303
|
+
next_tokens = self.sampler(logits, sampling_metadata)
|
304
|
+
return next_tokens
|
305
|
+
|
306
|
+
def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
|
307
|
+
params_dict = dict(self.named_parameters(remove_duplicate=False))
|
308
|
+
for name, loaded_weight in weights:
|
309
|
+
if "lm_head.weight" in name:
|
310
|
+
# GPT-2 ties the weights of the embedding layer and the final
|
311
|
+
# linear layer.
|
312
|
+
continue
|
313
|
+
if ".attn.bias" in name or ".attn.masked_bias" in name:
|
314
|
+
# Skip attention mask.
|
315
|
+
# NOTE: "c_attn.bias" should not be skipped.
|
316
|
+
continue
|
317
|
+
if "relative_pe" in name:
|
318
|
+
continue
|
319
|
+
if not name.startswith("transformer."):
|
320
|
+
name = "transformer." + name
|
321
|
+
param = params_dict[name]
|
322
|
+
# The HF's GPT-2 implementation uses Conv1D instead of Linear.
|
323
|
+
# Because of this, we need to transpose the weights.
|
324
|
+
# Note(zhuohan): the logic below might break quantized models.
|
325
|
+
for conv1d_weight_name in ["c_attn", "c_proj", "c_fc"]:
|
326
|
+
if conv1d_weight_name not in name:
|
327
|
+
continue
|
328
|
+
if not name.endswith(".weight"):
|
329
|
+
continue
|
330
|
+
loaded_weight = loaded_weight.t()
|
331
|
+
weight_loader = getattr(param, "weight_loader",
|
332
|
+
default_weight_loader)
|
333
|
+
weight_loader(param, loaded_weight)
|