init model
Browse files- config.json +37 -0
- configuration_qwen.py +71 -0
- cpp_kernels.py +55 -0
- generation_config.json +12 -0
- model-00001-of-00002.safetensors +3 -0
- model-00002-of-00002.safetensors +3 -0
- model.safetensors.index.json +202 -0
- modeling_qwen.py +1372 -0
- qwen.tiktoken +0 -0
- qwen_generation_utils.py +416 -0
- tokenization_qwen.py +276 -0
- tokenizer_config.json +10 -0
config.json
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"QWenLMHeadModel"
|
4 |
+
],
|
5 |
+
"auto_map": {
|
6 |
+
"AutoConfig": "configuration_qwen.QWenConfig",
|
7 |
+
"AutoModelForCausalLM": "modeling_qwen.QWenLMHeadModel"
|
8 |
+
},
|
9 |
+
"attn_dropout_prob": 0.0,
|
10 |
+
"bf16": false,
|
11 |
+
"emb_dropout_prob": 0.0,
|
12 |
+
"fp16": false,
|
13 |
+
"fp32": false,
|
14 |
+
"hidden_size": 2048,
|
15 |
+
"intermediate_size": 11008,
|
16 |
+
"initializer_range": 0.02,
|
17 |
+
"kv_channels": 128,
|
18 |
+
"layer_norm_epsilon": 1e-06,
|
19 |
+
"max_position_embeddings": 8192,
|
20 |
+
"model_type": "qwen",
|
21 |
+
"no_bias": true,
|
22 |
+
"num_attention_heads": 16,
|
23 |
+
"num_hidden_layers": 24,
|
24 |
+
"onnx_safe": null,
|
25 |
+
"rotary_emb_base": 10000,
|
26 |
+
"rotary_pct": 1.0,
|
27 |
+
"scale_attn_weights": true,
|
28 |
+
"seq_length": 8192,
|
29 |
+
"tie_word_embeddings": false,
|
30 |
+
"tokenizer_class": "QWenTokenizer",
|
31 |
+
"transformers_version": "4.32.0",
|
32 |
+
"use_cache": true,
|
33 |
+
"use_dynamic_ntk": true,
|
34 |
+
"use_flash_attn": "auto",
|
35 |
+
"use_logn_attn": true,
|
36 |
+
"vocab_size": 151936
|
37 |
+
}
|
configuration_qwen.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Alibaba Cloud.
|
2 |
+
#
|
3 |
+
# This source code is licensed under the license found in the
|
4 |
+
# LICENSE file in the root directory of this source tree.
|
5 |
+
|
6 |
+
from transformers import PretrainedConfig
|
7 |
+
|
8 |
+
|
9 |
+
class QWenConfig(PretrainedConfig):
|
10 |
+
model_type = "qwen"
|
11 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
12 |
+
|
13 |
+
def __init__(
|
14 |
+
self,
|
15 |
+
vocab_size=151936,
|
16 |
+
hidden_size=4096,
|
17 |
+
num_hidden_layers=32,
|
18 |
+
num_attention_heads=32,
|
19 |
+
emb_dropout_prob=0.0,
|
20 |
+
attn_dropout_prob=0.0,
|
21 |
+
layer_norm_epsilon=1e-6,
|
22 |
+
initializer_range=0.02,
|
23 |
+
max_position_embeddings=8192,
|
24 |
+
scale_attn_weights=True,
|
25 |
+
use_cache=True,
|
26 |
+
bf16=False,
|
27 |
+
fp16=False,
|
28 |
+
fp32=False,
|
29 |
+
kv_channels=128,
|
30 |
+
rotary_pct=1.0,
|
31 |
+
rotary_emb_base=10000,
|
32 |
+
use_dynamic_ntk=True,
|
33 |
+
use_logn_attn=True,
|
34 |
+
use_flash_attn="auto",
|
35 |
+
intermediate_size=22016,
|
36 |
+
no_bias=True,
|
37 |
+
tie_word_embeddings=False,
|
38 |
+
use_cache_quantization=False,
|
39 |
+
use_cache_kernel=False,
|
40 |
+
softmax_in_fp32=False,
|
41 |
+
**kwargs,
|
42 |
+
):
|
43 |
+
self.vocab_size = vocab_size
|
44 |
+
self.hidden_size = hidden_size
|
45 |
+
self.intermediate_size = intermediate_size
|
46 |
+
self.num_hidden_layers = num_hidden_layers
|
47 |
+
self.num_attention_heads = num_attention_heads
|
48 |
+
self.emb_dropout_prob = emb_dropout_prob
|
49 |
+
self.attn_dropout_prob = attn_dropout_prob
|
50 |
+
self.layer_norm_epsilon = layer_norm_epsilon
|
51 |
+
self.initializer_range = initializer_range
|
52 |
+
self.scale_attn_weights = scale_attn_weights
|
53 |
+
self.use_cache = use_cache
|
54 |
+
self.max_position_embeddings = max_position_embeddings
|
55 |
+
self.bf16 = bf16
|
56 |
+
self.fp16 = fp16
|
57 |
+
self.fp32 = fp32
|
58 |
+
self.kv_channels = kv_channels
|
59 |
+
self.rotary_pct = rotary_pct
|
60 |
+
self.rotary_emb_base = rotary_emb_base
|
61 |
+
self.use_dynamic_ntk = use_dynamic_ntk
|
62 |
+
self.use_logn_attn = use_logn_attn
|
63 |
+
self.use_flash_attn = use_flash_attn
|
64 |
+
self.no_bias = no_bias
|
65 |
+
self.use_cache_quantization = use_cache_quantization
|
66 |
+
self.use_cache_kernel = use_cache_kernel
|
67 |
+
self.softmax_in_fp32 = softmax_in_fp32
|
68 |
+
super().__init__(
|
69 |
+
tie_word_embeddings=tie_word_embeddings,
|
70 |
+
**kwargs
|
71 |
+
)
|
cpp_kernels.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from torch.utils import cpp_extension
|
2 |
+
import pathlib
|
3 |
+
import os
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def _get_cuda_bare_metal_version(cuda_dir):
|
7 |
+
raw_output = subprocess.check_output([cuda_dir + "/bin/nvcc", "-V"],
|
8 |
+
universal_newlines=True)
|
9 |
+
output = raw_output.split()
|
10 |
+
release_idx = output.index("release") + 1
|
11 |
+
release = output[release_idx].split(".")
|
12 |
+
bare_metal_major = release[0]
|
13 |
+
bare_metal_minor = release[1][0]
|
14 |
+
|
15 |
+
return raw_output, bare_metal_major, bare_metal_minor
|
16 |
+
|
17 |
+
def _create_build_dir(buildpath):
|
18 |
+
try:
|
19 |
+
os.mkdir(buildpath)
|
20 |
+
except OSError:
|
21 |
+
if not os.path.isdir(buildpath):
|
22 |
+
print(f"Creation of the build directory {buildpath} failed")
|
23 |
+
|
24 |
+
# Check if cuda 11 is installed for compute capability 8.0
|
25 |
+
cc_flag = []
|
26 |
+
_, bare_metal_major, bare_metal_minor = _get_cuda_bare_metal_version(cpp_extension.CUDA_HOME)
|
27 |
+
if int(bare_metal_major) >= 11:
|
28 |
+
cc_flag.append('-gencode')
|
29 |
+
cc_flag.append('arch=compute_80,code=sm_80')
|
30 |
+
if int(bare_metal_minor) >= 7:
|
31 |
+
cc_flag.append('-gencode')
|
32 |
+
cc_flag.append('arch=compute_90,code=sm_90')
|
33 |
+
|
34 |
+
# Build path
|
35 |
+
srcpath = pathlib.Path(__file__).parent.absolute()
|
36 |
+
buildpath = srcpath / 'build'
|
37 |
+
_create_build_dir(buildpath)
|
38 |
+
|
39 |
+
def _cpp_extention_load_helper(name, sources, extra_cuda_flags):
|
40 |
+
return cpp_extension.load(
|
41 |
+
name=name,
|
42 |
+
sources=sources,
|
43 |
+
build_directory=buildpath,
|
44 |
+
extra_cflags=['-O3', ],
|
45 |
+
extra_cuda_cflags=['-O3',
|
46 |
+
'-gencode', 'arch=compute_70,code=sm_70',
|
47 |
+
'--use_fast_math'] + extra_cuda_flags + cc_flag,
|
48 |
+
verbose=1
|
49 |
+
)
|
50 |
+
|
51 |
+
extra_flags = []
|
52 |
+
|
53 |
+
cache_autogptq_cuda_256_sources = ["./cache_autogptq_cuda_256.cpp",
|
54 |
+
"./cache_autogptq_cuda_kernel_256.cu"]
|
55 |
+
cache_autogptq_cuda_256 = _cpp_extention_load_helper("cache_autogptq_cuda_256", cache_autogptq_cuda_256_sources, extra_flags)
|
generation_config.json
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"chat_format": "chatml",
|
3 |
+
"eos_token_id": 151643,
|
4 |
+
"pad_token_id": 151643,
|
5 |
+
"max_window_size": 6144,
|
6 |
+
"max_new_tokens": 512,
|
7 |
+
"do_sample": true,
|
8 |
+
"top_k": 0,
|
9 |
+
"top_p": 0.8,
|
10 |
+
"repetition_penalty": 1.1,
|
11 |
+
"transformers_version": "4.31.0"
|
12 |
+
}
|
model-00001-of-00002.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:afe102dfe02cbf973a5647a4ccef97dad0f088ae12a749e67000f25af3c6c997
|
3 |
+
size 2039259008
|
model-00002-of-00002.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f710454106cacb0bee8138866573f1ecd453c3cd55f122b74e70a0e392d21435
|
3 |
+
size 1634419264
|
model.safetensors.index.json
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"metadata": {
|
3 |
+
"total_size": 3673657344
|
4 |
+
},
|
5 |
+
"weight_map": {
|
6 |
+
"lm_head.weight": "model-00002-of-00002.safetensors",
|
7 |
+
"transformer.h.0.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
8 |
+
"transformer.h.0.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
9 |
+
"transformer.h.0.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
10 |
+
"transformer.h.0.ln_1.weight": "model-00001-of-00002.safetensors",
|
11 |
+
"transformer.h.0.ln_2.weight": "model-00001-of-00002.safetensors",
|
12 |
+
"transformer.h.0.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
13 |
+
"transformer.h.0.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
14 |
+
"transformer.h.0.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
15 |
+
"transformer.h.1.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
16 |
+
"transformer.h.1.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
17 |
+
"transformer.h.1.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
18 |
+
"transformer.h.1.ln_1.weight": "model-00001-of-00002.safetensors",
|
19 |
+
"transformer.h.1.ln_2.weight": "model-00001-of-00002.safetensors",
|
20 |
+
"transformer.h.1.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
21 |
+
"transformer.h.1.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
22 |
+
"transformer.h.1.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
23 |
+
"transformer.h.10.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
24 |
+
"transformer.h.10.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
25 |
+
"transformer.h.10.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
26 |
+
"transformer.h.10.ln_1.weight": "model-00001-of-00002.safetensors",
|
27 |
+
"transformer.h.10.ln_2.weight": "model-00001-of-00002.safetensors",
|
28 |
+
"transformer.h.10.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
29 |
+
"transformer.h.10.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
30 |
+
"transformer.h.10.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
31 |
+
"transformer.h.11.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
32 |
+
"transformer.h.11.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
33 |
+
"transformer.h.11.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
34 |
+
"transformer.h.11.ln_1.weight": "model-00001-of-00002.safetensors",
|
35 |
+
"transformer.h.11.ln_2.weight": "model-00001-of-00002.safetensors",
|
36 |
+
"transformer.h.11.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
37 |
+
"transformer.h.11.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
38 |
+
"transformer.h.11.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
39 |
+
"transformer.h.12.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
40 |
+
"transformer.h.12.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
41 |
+
"transformer.h.12.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
42 |
+
"transformer.h.12.ln_1.weight": "model-00001-of-00002.safetensors",
|
43 |
+
"transformer.h.12.ln_2.weight": "model-00001-of-00002.safetensors",
|
44 |
+
"transformer.h.12.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
45 |
+
"transformer.h.12.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
46 |
+
"transformer.h.12.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
47 |
+
"transformer.h.13.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
48 |
+
"transformer.h.13.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
49 |
+
"transformer.h.13.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
50 |
+
"transformer.h.13.ln_1.weight": "model-00001-of-00002.safetensors",
|
51 |
+
"transformer.h.13.ln_2.weight": "model-00001-of-00002.safetensors",
|
52 |
+
"transformer.h.13.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
53 |
+
"transformer.h.13.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
54 |
+
"transformer.h.13.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
55 |
+
"transformer.h.14.attn.c_attn.bias": "model-00002-of-00002.safetensors",
|
56 |
+
"transformer.h.14.attn.c_attn.weight": "model-00002-of-00002.safetensors",
|
57 |
+
"transformer.h.14.attn.c_proj.weight": "model-00002-of-00002.safetensors",
|
58 |
+
"transformer.h.14.ln_1.weight": "model-00001-of-00002.safetensors",
|
59 |
+
"transformer.h.14.ln_2.weight": "model-00002-of-00002.safetensors",
|
60 |
+
"transformer.h.14.mlp.c_proj.weight": "model-00002-of-00002.safetensors",
|
61 |
+
"transformer.h.14.mlp.w1.weight": "model-00002-of-00002.safetensors",
|
62 |
+
"transformer.h.14.mlp.w2.weight": "model-00002-of-00002.safetensors",
|
63 |
+
"transformer.h.15.attn.c_attn.bias": "model-00002-of-00002.safetensors",
|
64 |
+
"transformer.h.15.attn.c_attn.weight": "model-00002-of-00002.safetensors",
|
65 |
+
"transformer.h.15.attn.c_proj.weight": "model-00002-of-00002.safetensors",
|
66 |
+
"transformer.h.15.ln_1.weight": "model-00002-of-00002.safetensors",
|
67 |
+
"transformer.h.15.ln_2.weight": "model-00002-of-00002.safetensors",
|
68 |
+
"transformer.h.15.mlp.c_proj.weight": "model-00002-of-00002.safetensors",
|
69 |
+
"transformer.h.15.mlp.w1.weight": "model-00002-of-00002.safetensors",
|
70 |
+
"transformer.h.15.mlp.w2.weight": "model-00002-of-00002.safetensors",
|
71 |
+
"transformer.h.16.attn.c_attn.bias": "model-00002-of-00002.safetensors",
|
72 |
+
"transformer.h.16.attn.c_attn.weight": "model-00002-of-00002.safetensors",
|
73 |
+
"transformer.h.16.attn.c_proj.weight": "model-00002-of-00002.safetensors",
|
74 |
+
"transformer.h.16.ln_1.weight": "model-00002-of-00002.safetensors",
|
75 |
+
"transformer.h.16.ln_2.weight": "model-00002-of-00002.safetensors",
|
76 |
+
"transformer.h.16.mlp.c_proj.weight": "model-00002-of-00002.safetensors",
|
77 |
+
"transformer.h.16.mlp.w1.weight": "model-00002-of-00002.safetensors",
|
78 |
+
"transformer.h.16.mlp.w2.weight": "model-00002-of-00002.safetensors",
|
79 |
+
"transformer.h.17.attn.c_attn.bias": "model-00002-of-00002.safetensors",
|
80 |
+
"transformer.h.17.attn.c_attn.weight": "model-00002-of-00002.safetensors",
|
81 |
+
"transformer.h.17.attn.c_proj.weight": "model-00002-of-00002.safetensors",
|
82 |
+
"transformer.h.17.ln_1.weight": "model-00002-of-00002.safetensors",
|
83 |
+
"transformer.h.17.ln_2.weight": "model-00002-of-00002.safetensors",
|
84 |
+
"transformer.h.17.mlp.c_proj.weight": "model-00002-of-00002.safetensors",
|
85 |
+
"transformer.h.17.mlp.w1.weight": "model-00002-of-00002.safetensors",
|
86 |
+
"transformer.h.17.mlp.w2.weight": "model-00002-of-00002.safetensors",
|
87 |
+
"transformer.h.18.attn.c_attn.bias": "model-00002-of-00002.safetensors",
|
88 |
+
"transformer.h.18.attn.c_attn.weight": "model-00002-of-00002.safetensors",
|
89 |
+
"transformer.h.18.attn.c_proj.weight": "model-00002-of-00002.safetensors",
|
90 |
+
"transformer.h.18.ln_1.weight": "model-00002-of-00002.safetensors",
|
91 |
+
"transformer.h.18.ln_2.weight": "model-00002-of-00002.safetensors",
|
92 |
+
"transformer.h.18.mlp.c_proj.weight": "model-00002-of-00002.safetensors",
|
93 |
+
"transformer.h.18.mlp.w1.weight": "model-00002-of-00002.safetensors",
|
94 |
+
"transformer.h.18.mlp.w2.weight": "model-00002-of-00002.safetensors",
|
95 |
+
"transformer.h.19.attn.c_attn.bias": "model-00002-of-00002.safetensors",
|
96 |
+
"transformer.h.19.attn.c_attn.weight": "model-00002-of-00002.safetensors",
|
97 |
+
"transformer.h.19.attn.c_proj.weight": "model-00002-of-00002.safetensors",
|
98 |
+
"transformer.h.19.ln_1.weight": "model-00002-of-00002.safetensors",
|
99 |
+
"transformer.h.19.ln_2.weight": "model-00002-of-00002.safetensors",
|
100 |
+
"transformer.h.19.mlp.c_proj.weight": "model-00002-of-00002.safetensors",
|
101 |
+
"transformer.h.19.mlp.w1.weight": "model-00002-of-00002.safetensors",
|
102 |
+
"transformer.h.19.mlp.w2.weight": "model-00002-of-00002.safetensors",
|
103 |
+
"transformer.h.2.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
104 |
+
"transformer.h.2.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
105 |
+
"transformer.h.2.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
106 |
+
"transformer.h.2.ln_1.weight": "model-00001-of-00002.safetensors",
|
107 |
+
"transformer.h.2.ln_2.weight": "model-00001-of-00002.safetensors",
|
108 |
+
"transformer.h.2.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
109 |
+
"transformer.h.2.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
110 |
+
"transformer.h.2.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
111 |
+
"transformer.h.20.attn.c_attn.bias": "model-00002-of-00002.safetensors",
|
112 |
+
"transformer.h.20.attn.c_attn.weight": "model-00002-of-00002.safetensors",
|
113 |
+
"transformer.h.20.attn.c_proj.weight": "model-00002-of-00002.safetensors",
|
114 |
+
"transformer.h.20.ln_1.weight": "model-00002-of-00002.safetensors",
|
115 |
+
"transformer.h.20.ln_2.weight": "model-00002-of-00002.safetensors",
|
116 |
+
"transformer.h.20.mlp.c_proj.weight": "model-00002-of-00002.safetensors",
|
117 |
+
"transformer.h.20.mlp.w1.weight": "model-00002-of-00002.safetensors",
|
118 |
+
"transformer.h.20.mlp.w2.weight": "model-00002-of-00002.safetensors",
|
119 |
+
"transformer.h.21.attn.c_attn.bias": "model-00002-of-00002.safetensors",
|
120 |
+
"transformer.h.21.attn.c_attn.weight": "model-00002-of-00002.safetensors",
|
121 |
+
"transformer.h.21.attn.c_proj.weight": "model-00002-of-00002.safetensors",
|
122 |
+
"transformer.h.21.ln_1.weight": "model-00002-of-00002.safetensors",
|
123 |
+
"transformer.h.21.ln_2.weight": "model-00002-of-00002.safetensors",
|
124 |
+
"transformer.h.21.mlp.c_proj.weight": "model-00002-of-00002.safetensors",
|
125 |
+
"transformer.h.21.mlp.w1.weight": "model-00002-of-00002.safetensors",
|
126 |
+
"transformer.h.21.mlp.w2.weight": "model-00002-of-00002.safetensors",
|
127 |
+
"transformer.h.22.attn.c_attn.bias": "model-00002-of-00002.safetensors",
|
128 |
+
"transformer.h.22.attn.c_attn.weight": "model-00002-of-00002.safetensors",
|
129 |
+
"transformer.h.22.attn.c_proj.weight": "model-00002-of-00002.safetensors",
|
130 |
+
"transformer.h.22.ln_1.weight": "model-00002-of-00002.safetensors",
|
131 |
+
"transformer.h.22.ln_2.weight": "model-00002-of-00002.safetensors",
|
132 |
+
"transformer.h.22.mlp.c_proj.weight": "model-00002-of-00002.safetensors",
|
133 |
+
"transformer.h.22.mlp.w1.weight": "model-00002-of-00002.safetensors",
|
134 |
+
"transformer.h.22.mlp.w2.weight": "model-00002-of-00002.safetensors",
|
135 |
+
"transformer.h.23.attn.c_attn.bias": "model-00002-of-00002.safetensors",
|
136 |
+
"transformer.h.23.attn.c_attn.weight": "model-00002-of-00002.safetensors",
|
137 |
+
"transformer.h.23.attn.c_proj.weight": "model-00002-of-00002.safetensors",
|
138 |
+
"transformer.h.23.ln_1.weight": "model-00002-of-00002.safetensors",
|
139 |
+
"transformer.h.23.ln_2.weight": "model-00002-of-00002.safetensors",
|
140 |
+
"transformer.h.23.mlp.c_proj.weight": "model-00002-of-00002.safetensors",
|
141 |
+
"transformer.h.23.mlp.w1.weight": "model-00002-of-00002.safetensors",
|
142 |
+
"transformer.h.23.mlp.w2.weight": "model-00002-of-00002.safetensors",
|
143 |
+
"transformer.h.3.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
144 |
+
"transformer.h.3.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
145 |
+
"transformer.h.3.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
146 |
+
"transformer.h.3.ln_1.weight": "model-00001-of-00002.safetensors",
|
147 |
+
"transformer.h.3.ln_2.weight": "model-00001-of-00002.safetensors",
|
148 |
+
"transformer.h.3.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
149 |
+
"transformer.h.3.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
150 |
+
"transformer.h.3.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
151 |
+
"transformer.h.4.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
152 |
+
"transformer.h.4.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
153 |
+
"transformer.h.4.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
154 |
+
"transformer.h.4.ln_1.weight": "model-00001-of-00002.safetensors",
|
155 |
+
"transformer.h.4.ln_2.weight": "model-00001-of-00002.safetensors",
|
156 |
+
"transformer.h.4.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
157 |
+
"transformer.h.4.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
158 |
+
"transformer.h.4.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
159 |
+
"transformer.h.5.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
160 |
+
"transformer.h.5.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
161 |
+
"transformer.h.5.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
162 |
+
"transformer.h.5.ln_1.weight": "model-00001-of-00002.safetensors",
|
163 |
+
"transformer.h.5.ln_2.weight": "model-00001-of-00002.safetensors",
|
164 |
+
"transformer.h.5.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
165 |
+
"transformer.h.5.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
166 |
+
"transformer.h.5.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
167 |
+
"transformer.h.6.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
168 |
+
"transformer.h.6.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
169 |
+
"transformer.h.6.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
170 |
+
"transformer.h.6.ln_1.weight": "model-00001-of-00002.safetensors",
|
171 |
+
"transformer.h.6.ln_2.weight": "model-00001-of-00002.safetensors",
|
172 |
+
"transformer.h.6.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
173 |
+
"transformer.h.6.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
174 |
+
"transformer.h.6.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
175 |
+
"transformer.h.7.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
176 |
+
"transformer.h.7.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
177 |
+
"transformer.h.7.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
178 |
+
"transformer.h.7.ln_1.weight": "model-00001-of-00002.safetensors",
|
179 |
+
"transformer.h.7.ln_2.weight": "model-00001-of-00002.safetensors",
|
180 |
+
"transformer.h.7.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
181 |
+
"transformer.h.7.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
182 |
+
"transformer.h.7.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
183 |
+
"transformer.h.8.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
184 |
+
"transformer.h.8.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
185 |
+
"transformer.h.8.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
186 |
+
"transformer.h.8.ln_1.weight": "model-00001-of-00002.safetensors",
|
187 |
+
"transformer.h.8.ln_2.weight": "model-00001-of-00002.safetensors",
|
188 |
+
"transformer.h.8.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
189 |
+
"transformer.h.8.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
190 |
+
"transformer.h.8.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
191 |
+
"transformer.h.9.attn.c_attn.bias": "model-00001-of-00002.safetensors",
|
192 |
+
"transformer.h.9.attn.c_attn.weight": "model-00001-of-00002.safetensors",
|
193 |
+
"transformer.h.9.attn.c_proj.weight": "model-00001-of-00002.safetensors",
|
194 |
+
"transformer.h.9.ln_1.weight": "model-00001-of-00002.safetensors",
|
195 |
+
"transformer.h.9.ln_2.weight": "model-00001-of-00002.safetensors",
|
196 |
+
"transformer.h.9.mlp.c_proj.weight": "model-00001-of-00002.safetensors",
|
197 |
+
"transformer.h.9.mlp.w1.weight": "model-00001-of-00002.safetensors",
|
198 |
+
"transformer.h.9.mlp.w2.weight": "model-00001-of-00002.safetensors",
|
199 |
+
"transformer.ln_f.weight": "model-00002-of-00002.safetensors",
|
200 |
+
"transformer.wte.weight": "model-00001-of-00002.safetensors"
|
201 |
+
}
|
202 |
+
}
|
modeling_qwen.py
ADDED
@@ -0,0 +1,1372 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Alibaba Cloud.
|
2 |
+
#
|
3 |
+
# This source code is licensed under the license found in the
|
4 |
+
# LICENSE file in the root directory of this source tree.
|
5 |
+
|
6 |
+
import copy
|
7 |
+
import importlib
|
8 |
+
import math
|
9 |
+
import pathlib
|
10 |
+
from typing import TYPE_CHECKING, Optional, Tuple, Union, Callable, List, Any, Generator
|
11 |
+
|
12 |
+
import torch
|
13 |
+
import torch.nn.functional as F
|
14 |
+
import torch.utils.checkpoint
|
15 |
+
import warnings
|
16 |
+
from torch.cuda.amp import autocast
|
17 |
+
|
18 |
+
from torch.nn import CrossEntropyLoss
|
19 |
+
from transformers import PreTrainedTokenizer, GenerationConfig, StoppingCriteriaList
|
20 |
+
from transformers.generation.logits_process import LogitsProcessorList
|
21 |
+
|
22 |
+
if TYPE_CHECKING:
|
23 |
+
from transformers.generation.streamers import BaseStreamer
|
24 |
+
from transformers.generation.utils import GenerateOutput
|
25 |
+
from transformers.modeling_outputs import (
|
26 |
+
BaseModelOutputWithPast,
|
27 |
+
CausalLMOutputWithPast,
|
28 |
+
)
|
29 |
+
from transformers.modeling_utils import PreTrainedModel
|
30 |
+
from transformers.utils import logging
|
31 |
+
|
32 |
+
try:
|
33 |
+
from einops import rearrange
|
34 |
+
except ImportError:
|
35 |
+
rearrange = None
|
36 |
+
from torch import nn
|
37 |
+
|
38 |
+
SUPPORT_CUDA = torch.cuda.is_available()
|
39 |
+
SUPPORT_BF16 = SUPPORT_CUDA and torch.cuda.is_bf16_supported()
|
40 |
+
SUPPORT_FP16 = SUPPORT_CUDA and torch.cuda.get_device_capability(0)[0] >= 7
|
41 |
+
SUPPORT_TORCH2 = hasattr(torch, '__version__') and int(torch.__version__.split(".")[0]) >= 2
|
42 |
+
|
43 |
+
|
44 |
+
from .configuration_qwen import QWenConfig
|
45 |
+
from .qwen_generation_utils import (
|
46 |
+
HistoryType,
|
47 |
+
make_context,
|
48 |
+
decode_tokens,
|
49 |
+
get_stop_words_ids,
|
50 |
+
StopWordsLogitsProcessor,
|
51 |
+
)
|
52 |
+
|
53 |
+
|
54 |
+
logger = logging.get_logger(__name__)
|
55 |
+
|
56 |
+
_CHECKPOINT_FOR_DOC = "qwen"
|
57 |
+
_CONFIG_FOR_DOC = "QWenConfig"
|
58 |
+
|
59 |
+
QWen_PRETRAINED_MODEL_ARCHIVE_LIST = ["qwen-7b"]
|
60 |
+
|
61 |
+
_ERROR_BAD_CHAT_FORMAT = """\
|
62 |
+
We detect you are probably using the pretrained model (rather than chat model) for chatting, since the chat_format in generation_config is not "chatml".
|
63 |
+
If you are directly using the model downloaded from Huggingface, please make sure you are using our "Qwen/Qwen-7B-Chat" Huggingface model (rather than "Qwen/Qwen-7B") when you call model.chat().
|
64 |
+
我们检测到您可能在使用预训练模型(而非chat模型)进行多轮chat,因为您当前在generation_config指定的chat_format,并未设置为我们在对话中所支持的"chatml"格式。
|
65 |
+
如果您在直接使用我们从Huggingface提供的模型,请确保您在调用model.chat()时,使用的是"Qwen/Qwen-7B-Chat"模型(而非"Qwen/Qwen-7B"预训练模型)。
|
66 |
+
"""
|
67 |
+
|
68 |
+
_SENTINEL = object()
|
69 |
+
_ERROR_STREAM_IN_CHAT = """\
|
70 |
+
Pass argument `stream` to model.chat() is buggy, deprecated, and marked for removal. Please use model.chat_stream(...) instead of model.chat(..., stream=True).
|
71 |
+
向model.chat()传入参数stream的用法可能存在Bug,该用法已被废弃,将在未来被移除。请使用model.chat_stream(...)代替model.chat(..., stream=True)。
|
72 |
+
"""
|
73 |
+
|
74 |
+
_ERROR_INPUT_CPU_QUERY_WITH_FLASH_ATTN_ACTIVATED = """\
|
75 |
+
We detect you have activated flash attention support, but running model computation on CPU. Please make sure that your input data has been placed on GPU. If you actually want to run CPU computation, please following the readme and set device_map="cpu" to disable flash attention when loading the model (calling AutoModelForCausalLM.from_pretrained).
|
76 |
+
检测到您的模型已激活了flash attention支持,但正在执行CPU运算任务。如使用flash attention,请您确认模型输入已经传到GPU上。如果您确认要执行CPU运算,请您在载入模型(调用AutoModelForCausalLM.from_pretrained)时,按照readme说法,指定device_map="cpu"以禁用flash attention。
|
77 |
+
"""
|
78 |
+
|
79 |
+
apply_rotary_emb_func = None
|
80 |
+
rms_norm = None
|
81 |
+
flash_attn_unpadded_func = None
|
82 |
+
|
83 |
+
def _import_flash_attn():
|
84 |
+
global apply_rotary_emb_func, rms_norm, flash_attn_unpadded_func
|
85 |
+
try:
|
86 |
+
from flash_attn.layers.rotary import apply_rotary_emb_func as __apply_rotary_emb_func
|
87 |
+
apply_rotary_emb_func = __apply_rotary_emb_func
|
88 |
+
except ImportError:
|
89 |
+
logger.warn(
|
90 |
+
"Warning: import flash_attn rotary fail, please install FlashAttention rotary to get higher efficiency "
|
91 |
+
"https://github.com/Dao-AILab/flash-attention/tree/main/csrc/rotary"
|
92 |
+
)
|
93 |
+
|
94 |
+
try:
|
95 |
+
from flash_attn.ops.rms_norm import rms_norm as __rms_norm
|
96 |
+
rms_norm = __rms_norm
|
97 |
+
except ImportError:
|
98 |
+
logger.warn(
|
99 |
+
"Warning: import flash_attn rms_norm fail, please install FlashAttention layer_norm to get higher efficiency "
|
100 |
+
"https://github.com/Dao-AILab/flash-attention/tree/main/csrc/layer_norm"
|
101 |
+
)
|
102 |
+
|
103 |
+
try:
|
104 |
+
import flash_attn
|
105 |
+
if not hasattr(flash_attn, '__version__'):
|
106 |
+
from flash_attn.flash_attn_interface import flash_attn_unpadded_func as __flash_attn_unpadded_func
|
107 |
+
else:
|
108 |
+
if int(flash_attn.__version__.split(".")[0]) >= 2:
|
109 |
+
from flash_attn.flash_attn_interface import flash_attn_varlen_func as __flash_attn_unpadded_func
|
110 |
+
else:
|
111 |
+
from flash_attn.flash_attn_interface import flash_attn_unpadded_func as __flash_attn_unpadded_func
|
112 |
+
flash_attn_unpadded_func = __flash_attn_unpadded_func
|
113 |
+
except ImportError:
|
114 |
+
logger.warn(
|
115 |
+
"Warning: import flash_attn fail, please install FlashAttention to get higher efficiency "
|
116 |
+
"https://github.com/Dao-AILab/flash-attention"
|
117 |
+
)
|
118 |
+
|
119 |
+
def quantize_cache_v(fdata, bits, qmax, qmin):
|
120 |
+
# b, s, head, h-dim->b, head, s, h-dim
|
121 |
+
qtype = torch.uint8
|
122 |
+
device = fdata.device
|
123 |
+
shape = fdata.shape
|
124 |
+
|
125 |
+
fdata_cal = torch.flatten(fdata, 2)
|
126 |
+
fmax = torch.amax(fdata_cal, dim=-1, keepdim=True)
|
127 |
+
fmin = torch.amin(fdata_cal, dim=-1, keepdim=True)
|
128 |
+
# Compute params
|
129 |
+
if qmax.device != fmax.device:
|
130 |
+
qmax = qmax.to(device)
|
131 |
+
qmin = qmin.to(device)
|
132 |
+
scale = (fmax - fmin) / (qmax - qmin)
|
133 |
+
zero = qmin - fmin / scale
|
134 |
+
scale = scale.unsqueeze(-1).repeat(1,1,shape[2],1).contiguous()
|
135 |
+
zero = zero.unsqueeze(-1).repeat(1,1,shape[2],1).contiguous()
|
136 |
+
# Quantize
|
137 |
+
res_data = fdata / scale + zero
|
138 |
+
qdata = torch.clamp(res_data, qmin, qmax).to(qtype)
|
139 |
+
return qdata.contiguous(), scale, zero
|
140 |
+
|
141 |
+
def dequantize_cache_torch(qdata, scale, zero):
|
142 |
+
data = scale * (qdata - zero)
|
143 |
+
return data
|
144 |
+
|
145 |
+
class FlashSelfAttention(torch.nn.Module):
|
146 |
+
def __init__(
|
147 |
+
self,
|
148 |
+
causal=False,
|
149 |
+
softmax_scale=None,
|
150 |
+
attention_dropout=0.0,
|
151 |
+
):
|
152 |
+
super().__init__()
|
153 |
+
assert flash_attn_unpadded_func is not None, (
|
154 |
+
"Please install FlashAttention first, " "e.g., with pip install flash-attn"
|
155 |
+
)
|
156 |
+
assert (
|
157 |
+
rearrange is not None
|
158 |
+
), "Please install einops first, e.g., with pip install einops"
|
159 |
+
self.causal = causal
|
160 |
+
self.softmax_scale = softmax_scale
|
161 |
+
self.dropout_p = attention_dropout
|
162 |
+
|
163 |
+
def unpad_input(self, hidden_states, attention_mask):
|
164 |
+
valid_mask = attention_mask.squeeze(1).squeeze(1).eq(0)
|
165 |
+
seqlens_in_batch = valid_mask.sum(dim=-1, dtype=torch.int32)
|
166 |
+
indices = torch.nonzero(valid_mask.flatten(), as_tuple=False).flatten()
|
167 |
+
max_seqlen_in_batch = seqlens_in_batch.max().item()
|
168 |
+
cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
|
169 |
+
hidden_states = hidden_states[indices]
|
170 |
+
return hidden_states, indices, cu_seqlens, max_seqlen_in_batch
|
171 |
+
|
172 |
+
def pad_input(self, hidden_states, indices, batch, seqlen):
|
173 |
+
output = torch.zeros(batch * seqlen, *hidden_states.shape[1:], device=hidden_states.device,
|
174 |
+
dtype=hidden_states.dtype)
|
175 |
+
output[indices] = hidden_states
|
176 |
+
return rearrange(output, '(b s) ... -> b s ...', b=batch)
|
177 |
+
|
178 |
+
def forward(self, q, k, v, attention_mask=None):
|
179 |
+
assert all((i.dtype in [torch.float16, torch.bfloat16] for i in (q, k, v)))
|
180 |
+
assert all((i.is_cuda for i in (q, k, v)))
|
181 |
+
batch_size, seqlen_q = q.shape[0], q.shape[1]
|
182 |
+
seqlen_k = k.shape[1]
|
183 |
+
seqlen_out = seqlen_q
|
184 |
+
|
185 |
+
q, k, v = [rearrange(x, "b s ... -> (b s) ...") for x in [q, k, v]]
|
186 |
+
cu_seqlens_q = torch.arange(
|
187 |
+
0,
|
188 |
+
(batch_size + 1) * seqlen_q,
|
189 |
+
step=seqlen_q,
|
190 |
+
dtype=torch.int32,
|
191 |
+
device=q.device,
|
192 |
+
)
|
193 |
+
|
194 |
+
if batch_size > 1 and attention_mask is not None:
|
195 |
+
k, indices_k, cu_seqlens_k, seqlen_k = self.unpad_input(k, attention_mask)
|
196 |
+
if q.size(0) == v.size(0):
|
197 |
+
q = q[indices_k]
|
198 |
+
cu_seqlens_q = cu_seqlens_k
|
199 |
+
seqlen_q = seqlen_k
|
200 |
+
v = v[indices_k]
|
201 |
+
else:
|
202 |
+
cu_seqlens_k = torch.arange(
|
203 |
+
0,
|
204 |
+
(batch_size + 1) * seqlen_k,
|
205 |
+
step=seqlen_k,
|
206 |
+
dtype=torch.int32,
|
207 |
+
device=q.device,
|
208 |
+
)
|
209 |
+
|
210 |
+
if self.training:
|
211 |
+
assert seqlen_k == seqlen_q
|
212 |
+
is_causal = self.causal
|
213 |
+
dropout_p = self.dropout_p
|
214 |
+
else:
|
215 |
+
is_causal = seqlen_q == seqlen_k
|
216 |
+
dropout_p = 0
|
217 |
+
|
218 |
+
output = flash_attn_unpadded_func(
|
219 |
+
q,
|
220 |
+
k,
|
221 |
+
v,
|
222 |
+
cu_seqlens_q,
|
223 |
+
cu_seqlens_k,
|
224 |
+
seqlen_q,
|
225 |
+
seqlen_k,
|
226 |
+
dropout_p,
|
227 |
+
softmax_scale=self.softmax_scale,
|
228 |
+
causal=is_causal,
|
229 |
+
)
|
230 |
+
if batch_size > 1 and attention_mask is not None and seqlen_q == seqlen_k:
|
231 |
+
output = self.pad_input(output, indices_k, batch_size, seqlen_out)
|
232 |
+
else:
|
233 |
+
new_shape = (batch_size, output.shape[0] // batch_size) + output.shape[1:]
|
234 |
+
output = output.view(new_shape)
|
235 |
+
return output
|
236 |
+
|
237 |
+
|
238 |
+
class QWenAttention(nn.Module):
|
239 |
+
def __init__(self, config):
|
240 |
+
super().__init__()
|
241 |
+
|
242 |
+
self.register_buffer("masked_bias", torch.tensor(-1e4), persistent=False)
|
243 |
+
self.seq_length = config.seq_length
|
244 |
+
|
245 |
+
self.hidden_size = config.hidden_size
|
246 |
+
self.split_size = config.hidden_size
|
247 |
+
self.num_heads = config.num_attention_heads
|
248 |
+
self.head_dim = self.hidden_size // self.num_heads
|
249 |
+
|
250 |
+
self.use_flash_attn = config.use_flash_attn
|
251 |
+
self.scale_attn_weights = True
|
252 |
+
|
253 |
+
self.projection_size = config.kv_channels * config.num_attention_heads
|
254 |
+
|
255 |
+
assert self.projection_size % config.num_attention_heads == 0
|
256 |
+
self.hidden_size_per_attention_head = (
|
257 |
+
self.projection_size // config.num_attention_heads
|
258 |
+
)
|
259 |
+
|
260 |
+
self.c_attn = nn.Linear(config.hidden_size, 3 * self.projection_size)
|
261 |
+
|
262 |
+
self.c_proj = nn.Linear(
|
263 |
+
config.hidden_size, self.projection_size, bias=not config.no_bias
|
264 |
+
)
|
265 |
+
|
266 |
+
self.is_fp32 = not (config.bf16 or config.fp16)
|
267 |
+
if (
|
268 |
+
self.use_flash_attn
|
269 |
+
and flash_attn_unpadded_func is not None
|
270 |
+
and not self.is_fp32
|
271 |
+
):
|
272 |
+
self.core_attention_flash = FlashSelfAttention(
|
273 |
+
causal=True, attention_dropout=config.attn_dropout_prob
|
274 |
+
)
|
275 |
+
self.bf16 = config.bf16
|
276 |
+
|
277 |
+
self.use_dynamic_ntk = config.use_dynamic_ntk
|
278 |
+
self.use_logn_attn = config.use_logn_attn
|
279 |
+
|
280 |
+
logn_list = [
|
281 |
+
math.log(i, self.seq_length) if i > self.seq_length else 1
|
282 |
+
for i in range(1, 32768)
|
283 |
+
]
|
284 |
+
logn_tensor = torch.tensor(logn_list)[None, :, None, None]
|
285 |
+
self.register_buffer("logn_tensor", logn_tensor, persistent=False)
|
286 |
+
|
287 |
+
self.attn_dropout = nn.Dropout(config.attn_dropout_prob)
|
288 |
+
self.softmax_in_fp32 = config.softmax_in_fp32 if hasattr(config, 'softmax_in_fp32') else False
|
289 |
+
self.use_cache_quantization = config.use_cache_quantization if hasattr(config, 'use_cache_quantization') else False
|
290 |
+
self.use_cache_kernel = config.use_cache_kernel if hasattr(config,'use_cache_kernel') else False
|
291 |
+
cache_dtype = torch.float
|
292 |
+
if self.bf16:
|
293 |
+
cache_dtype=torch.bfloat16
|
294 |
+
elif config.fp16:
|
295 |
+
cache_dtype = torch.float16
|
296 |
+
self.cache_qmax = torch.tensor(torch.iinfo(torch.uint8).max, dtype=cache_dtype)
|
297 |
+
self.cache_qmin = torch.tensor(torch.iinfo(torch.uint8).min, dtype=cache_dtype)
|
298 |
+
|
299 |
+
if config.use_cache_quantization and config.use_cache_kernel:
|
300 |
+
# pre check if the support files existing
|
301 |
+
module_root = pathlib.Path(__file__).parent
|
302 |
+
src_files = ("cache_autogptq_cuda_256.cpp", "cache_autogptq_cuda_kernel_256.cu")
|
303 |
+
if any(not (module_root/src).is_file() for src in src_files):
|
304 |
+
warnings.warn("KV cache kernel source files (.cpp and .cu) not found.")
|
305 |
+
self.cache_kernels = None
|
306 |
+
else:
|
307 |
+
try:
|
308 |
+
from .cpp_kernels import cache_autogptq_cuda_256
|
309 |
+
self.cache_kernels = cache_autogptq_cuda_256
|
310 |
+
except ImportError:
|
311 |
+
warnings.warn("Failed to import KV cache kernels.")
|
312 |
+
self.cache_kernels = None
|
313 |
+
|
314 |
+
def _attn(self, query, key, value, registered_causal_mask, attention_mask=None, head_mask=None):
|
315 |
+
device = query.device
|
316 |
+
if self.use_cache_quantization:
|
317 |
+
qk, qk_scale, qk_zero = key
|
318 |
+
if self.use_cache_kernel and self.cache_kernels is not None:
|
319 |
+
shape = query.shape[:-1] + (qk.shape[-2],)
|
320 |
+
attn_weights = torch.zeros(shape, dtype=torch.float16, device=device)
|
321 |
+
self.cache_kernels.vecquant8matmul_batched_faster_old(
|
322 |
+
query.contiguous() if query.dtype == torch.float16 else query.to(torch.float16).contiguous(),
|
323 |
+
qk.transpose(-1, -2).contiguous(),
|
324 |
+
attn_weights,
|
325 |
+
qk_scale.contiguous() if qk_scale.dtype == torch.float16 else qk_scale.to(torch.float16).contiguous(),
|
326 |
+
qk_zero.contiguous()if qk_zero.dtype == torch.float16 else qk_zero.to(torch.float16).contiguous())
|
327 |
+
# attn_weights = attn_weights.to(query.dtype).contiguous()
|
328 |
+
else:
|
329 |
+
key = dequantize_cache_torch(qk, qk_scale, qk_zero)
|
330 |
+
attn_weights = torch.matmul(query, key.transpose(-1, -2))
|
331 |
+
else:
|
332 |
+
attn_weights = torch.matmul(query, key.transpose(-1, -2))
|
333 |
+
|
334 |
+
if self.scale_attn_weights:
|
335 |
+
if self.use_cache_quantization:
|
336 |
+
size_temp = value[0].size(-1)
|
337 |
+
else:
|
338 |
+
size_temp = value.size(-1)
|
339 |
+
attn_weights = attn_weights / torch.full(
|
340 |
+
[],
|
341 |
+
size_temp ** 0.5,
|
342 |
+
dtype=attn_weights.dtype,
|
343 |
+
device=attn_weights.device,
|
344 |
+
)
|
345 |
+
if self.use_cache_quantization:
|
346 |
+
query_length, key_length = query.size(-2), key[0].size(-2)
|
347 |
+
else:
|
348 |
+
query_length, key_length = query.size(-2), key.size(-2)
|
349 |
+
causal_mask = registered_causal_mask[
|
350 |
+
:, :, key_length - query_length : key_length, :key_length
|
351 |
+
]
|
352 |
+
mask_value = torch.finfo(attn_weights.dtype).min
|
353 |
+
mask_value = torch.full([], mask_value, dtype=attn_weights.dtype).to(
|
354 |
+
attn_weights.device
|
355 |
+
)
|
356 |
+
attn_weights = torch.where(
|
357 |
+
causal_mask, attn_weights.to(attn_weights.dtype), mask_value
|
358 |
+
)
|
359 |
+
|
360 |
+
if attention_mask is not None:
|
361 |
+
attn_weights = attn_weights + attention_mask
|
362 |
+
|
363 |
+
if self.softmax_in_fp32:
|
364 |
+
attn_weights = nn.functional.softmax(attn_weights.float(), dim=-1)
|
365 |
+
else:
|
366 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1)
|
367 |
+
|
368 |
+
attn_weights = attn_weights.type(query.dtype)
|
369 |
+
attn_weights = self.attn_dropout(attn_weights)
|
370 |
+
|
371 |
+
if head_mask is not None:
|
372 |
+
attn_weights = attn_weights * head_mask
|
373 |
+
|
374 |
+
if self.use_cache_quantization:
|
375 |
+
qv, qv_scale, qv_zero = value
|
376 |
+
if self.use_cache_kernel and self.cache_kernels is not None:
|
377 |
+
shape = attn_weights.shape[:-1] + (query.shape[-1],)
|
378 |
+
attn_output = torch.zeros(shape, dtype=torch.float16, device=device)
|
379 |
+
self.cache_kernels.vecquant8matmul_batched_column_compression_faster_old(
|
380 |
+
attn_weights.contiguous() if attn_weights.dtype == torch.float16 else attn_weights.to(torch.float16).contiguous(),
|
381 |
+
qv.contiguous(), # dtype: int32
|
382 |
+
attn_output,
|
383 |
+
qv_scale.contiguous() if qv_scale.dtype == torch.float16 else qv_scale.to(torch.float16).contiguous(),
|
384 |
+
qv_zero.contiguous() if qv_zero.dtype == torch.float16 else qv_zero.to(torch.float16).contiguous())
|
385 |
+
if attn_output.dtype != query.dtype:
|
386 |
+
attn_output = attn_output.to(query.dtype)
|
387 |
+
attn_weights = attn_weights.to(query.dtype)
|
388 |
+
else:
|
389 |
+
value = dequantize_cache_torch(qv, qv_scale, qv_zero)
|
390 |
+
attn_output = torch.matmul(attn_weights, value)
|
391 |
+
else:
|
392 |
+
attn_output = torch.matmul(attn_weights, value)
|
393 |
+
|
394 |
+
attn_output = attn_output.transpose(1, 2)
|
395 |
+
|
396 |
+
return attn_output, attn_weights
|
397 |
+
|
398 |
+
def _split_heads(self, tensor, num_heads, attn_head_size):
|
399 |
+
new_shape = tensor.size()[:-1] + (num_heads, attn_head_size)
|
400 |
+
tensor = tensor.view(new_shape)
|
401 |
+
return tensor
|
402 |
+
|
403 |
+
def _merge_heads(self, tensor, num_heads, attn_head_size):
|
404 |
+
tensor = tensor.contiguous()
|
405 |
+
new_shape = tensor.size()[:-2] + (num_heads * attn_head_size,)
|
406 |
+
return tensor.view(new_shape)
|
407 |
+
|
408 |
+
def forward(
|
409 |
+
self,
|
410 |
+
hidden_states: Optional[Tuple[torch.FloatTensor]],
|
411 |
+
rotary_pos_emb_list: Optional[List[List[torch.Tensor]]] = None,
|
412 |
+
layer_past: Optional[Tuple[torch.Tensor]] = None,
|
413 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
414 |
+
head_mask: Optional[torch.FloatTensor] = None,
|
415 |
+
encoder_hidden_states: Optional[torch.Tensor] = None,
|
416 |
+
encoder_attention_mask: Optional[torch.FloatTensor] = None,
|
417 |
+
output_attentions: Optional[bool] = False,
|
418 |
+
use_cache: Optional[bool] = False,
|
419 |
+
):
|
420 |
+
mixed_x_layer = self.c_attn(hidden_states)
|
421 |
+
|
422 |
+
query, key, value = mixed_x_layer.split(self.split_size, dim=2)
|
423 |
+
|
424 |
+
query = self._split_heads(query, self.num_heads, self.head_dim)
|
425 |
+
key = self._split_heads(key, self.num_heads, self.head_dim)
|
426 |
+
value = self._split_heads(value, self.num_heads, self.head_dim)
|
427 |
+
|
428 |
+
if rotary_pos_emb_list is not None:
|
429 |
+
cur_len = query.shape[1]
|
430 |
+
if len(rotary_pos_emb_list) == 1:
|
431 |
+
rotary_pos_emb = rotary_pos_emb_list[0]
|
432 |
+
rotary_pos_emb = [i[:, -cur_len:, :, :] for i in rotary_pos_emb]
|
433 |
+
rotary_pos_emb = (rotary_pos_emb,) * 2
|
434 |
+
q_pos_emb, k_pos_emb = rotary_pos_emb
|
435 |
+
# Slice the pos emb for current inference
|
436 |
+
query = apply_rotary_pos_emb(query, q_pos_emb)
|
437 |
+
key = apply_rotary_pos_emb(key, k_pos_emb)
|
438 |
+
else:
|
439 |
+
query_list = []
|
440 |
+
key_list = []
|
441 |
+
for i, rotary_pos_emb in enumerate(rotary_pos_emb_list):
|
442 |
+
rotary_pos_emb = [i[:, -cur_len:, :, :] for i in rotary_pos_emb]
|
443 |
+
rotary_pos_emb = (rotary_pos_emb,) * 2
|
444 |
+
q_pos_emb, k_pos_emb = rotary_pos_emb
|
445 |
+
# Slice the pos emb for current inference
|
446 |
+
query_list += [apply_rotary_pos_emb(query[i:i+1, :, :], q_pos_emb)]
|
447 |
+
key_list += [apply_rotary_pos_emb(key[i:i+1, :, :], k_pos_emb)]
|
448 |
+
query = torch.cat(query_list, dim=0)
|
449 |
+
key = torch.cat(key_list, dim=0)
|
450 |
+
|
451 |
+
if self.use_cache_quantization:
|
452 |
+
key = quantize_cache_v(key.permute(0, 2, 1, 3),
|
453 |
+
bits=8,
|
454 |
+
qmin=self.cache_qmin,
|
455 |
+
qmax=self.cache_qmax)
|
456 |
+
value = quantize_cache_v(value.permute(0, 2, 1, 3),
|
457 |
+
bits=8,
|
458 |
+
qmin=self.cache_qmin,
|
459 |
+
qmax=self.cache_qmax)
|
460 |
+
|
461 |
+
|
462 |
+
if layer_past is not None:
|
463 |
+
past_key, past_value = layer_past[0], layer_past[1]
|
464 |
+
if self.use_cache_quantization:
|
465 |
+
# use_cache_quantization:
|
466 |
+
# present=((q_key,key_scale,key_zero_point),
|
467 |
+
# (q_value,value_scale,value_zero_point))
|
468 |
+
key = (torch.cat((past_key[0], key[0]), dim=2),
|
469 |
+
torch.cat((past_key[1], key[1]), dim=2),
|
470 |
+
torch.cat((past_key[2], key[2]), dim=2))
|
471 |
+
value = (torch.cat((past_value[0], value[0]), dim=2),
|
472 |
+
torch.cat((past_value[1], value[1]), dim=2),
|
473 |
+
torch.cat((past_value[2], value[2]), dim=2))
|
474 |
+
else:
|
475 |
+
# not use_cache_quantization:
|
476 |
+
# present=(key,value)
|
477 |
+
key = torch.cat((past_key, key), dim=1)
|
478 |
+
value = torch.cat((past_value, value), dim=1)
|
479 |
+
|
480 |
+
if use_cache:
|
481 |
+
present = (key, value)
|
482 |
+
else:
|