nchen909 commited on
Commit
bbfa44f
1 Parent(s): 8181cad

Upload model

Browse files
config.json ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/223040239/medbase/ckpts/Qwen-1_8B-ALL-SFT_train/checkpoint-0-9420/tfmr/",
3
+ "architectures": [
4
+ "QWenLMHeadModel"
5
+ ],
6
+ "attn_dropout_prob": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_qwen.QWenConfig",
9
+ "AutoModelForCausalLM": "modeling_qwen.QWenLMHeadModel"
10
+ },
11
+ "bf16": true,
12
+ "emb_dropout_prob": 0.0,
13
+ "fp16": false,
14
+ "fp32": false,
15
+ "hidden_size": 2048,
16
+ "initializer_range": 0.02,
17
+ "intermediate_size": 11008,
18
+ "kv_channels": 128,
19
+ "layer_norm_epsilon": 1e-06,
20
+ "max_position_embeddings": 8192,
21
+ "model_type": "qwen",
22
+ "no_bias": true,
23
+ "num_attention_heads": 16,
24
+ "num_hidden_layers": 24,
25
+ "onnx_safe": null,
26
+ "rotary_emb_base": 10000,
27
+ "rotary_pct": 1.0,
28
+ "scale_attn_weights": true,
29
+ "seq_length": 8192,
30
+ "softmax_in_fp32": false,
31
+ "tie_word_embeddings": false,
32
+ "tokenizer_class": "QWenTokenizer",
33
+ "torch_dtype": "bfloat16",
34
+ "transformers_version": "4.38.2",
35
+ "use_cache": true,
36
+ "use_cache_kernel": false,
37
+ "use_cache_quantization": false,
38
+ "use_dynamic_ntk": true,
39
+ "use_flash_attn": true,
40
+ "use_logn_attn": true,
41
+ "vocab_size": 151936
42
+ }
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,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "chat_format": "raw",
3
+ "do_sample": true,
4
+ "eos_token_id": 151643,
5
+ "max_new_tokens": 512,
6
+ "pad_token_id": 151643,
7
+ "stop_words_ids": [
8
+ [
9
+ 151643
10
+ ]
11
+ ],
12
+ "top_k": 0,
13
+ "top_p": 0.8,
14
+ "transformers_version": "4.38.2"
15
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2ffc762b3007da782705748bef8a33c8270ec60d909b868b978da7d95dc70519
3
+ size 3673678408
modeling_qwen.py ADDED
@@ -0,0 +1,1375 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
17
+ from torch.nn import CrossEntropyLoss
18
+ from transformers import PreTrainedTokenizer, GenerationConfig, StoppingCriteriaList
19
+ from transformers.generation.logits_process import LogitsProcessorList
20
+
21
+ if TYPE_CHECKING:
22
+ from transformers.generation.streamers import BaseStreamer
23
+ from transformers.generation.utils import GenerateOutput
24
+ from transformers.modeling_outputs import (
25
+ BaseModelOutputWithPast,
26
+ CausalLMOutputWithPast,
27
+ )
28
+ from transformers.modeling_utils import PreTrainedModel
29
+ from transformers.utils import logging
30
+
31
+ try:
32
+ from einops import rearrange
33
+ except ImportError:
34
+ rearrange = None
35
+ from torch import nn
36
+
37
+ SUPPORT_CUDA = torch.cuda.is_available()
38
+ SUPPORT_BF16 = SUPPORT_CUDA and torch.cuda.is_bf16_supported()
39
+ SUPPORT_FP16 = SUPPORT_CUDA and torch.cuda.get_device_capability(0)[0] >= 7
40
+ SUPPORT_TORCH2 = hasattr(torch, '__version__') and int(torch.__version__.split(".")[0]) >= 2
41
+
42
+
43
+ from .configuration_qwen import QWenConfig
44
+ from .qwen_generation_utils import (
45
+ HistoryType,
46
+ make_context,
47
+ decode_tokens,
48
+ get_stop_words_ids,
49
+ StopWordsLogitsProcessor,
50
+ )
51
+
52
+
53
+ logger = logging.get_logger(__name__)
54
+
55
+ _CHECKPOINT_FOR_DOC = "qwen"
56
+ _CONFIG_FOR_DOC = "QWenConfig"
57
+
58
+ QWen_PRETRAINED_MODEL_ARCHIVE_LIST = ["qwen-7b"]
59
+
60
+ _ERROR_BAD_CHAT_FORMAT = """\
61
+ 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".
62
+ 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().
63
+ 我们检测到您可能在使用预训练模型(而非chat模型)进行多轮chat,因为您当前在generation_config指定的chat_format,并未设置为我们在对话中所支持的"chatml"格式。
64
+ 如果您在直接使用我们从Huggingface提供的模型,请确保您在调用model.chat()时,使用的是"Qwen/Qwen-7B-Chat"模型(而非"Qwen/Qwen-7B"预训练模型)。
65
+ """
66
+
67
+ _SENTINEL = object()
68
+ _ERROR_STREAM_IN_CHAT = """\
69
+ Pass argument `stream` to model.chat() is buggy, deprecated, and marked for removal. Please use model.chat_stream(...) instead of model.chat(..., stream=True).
70
+ 向model.chat()传入参数stream的用法可能存在Bug,该用法已被废弃,将在未来被移除。请使用model.chat_stream(...)代替model.chat(..., stream=True)。
71
+ """
72
+
73
+ _ERROR_INPUT_CPU_QUERY_WITH_FLASH_ATTN_ACTIVATED = """\
74
+ 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).
75
+ 检测到您的模型已激活了flash attention支持,但正在执行CPU运算任务。如使用flash attention,请您确认模型输入已经传到GPU上。如果您确认要执行CPU运算,请您在载入模型(调用AutoModelForCausalLM.from_pretrained)时,按照readme说法,指定device_map="cpu"以禁用flash attention。
76
+ """
77
+
78
+ apply_rotary_emb_func = None
79
+ rms_norm = None
80
+ flash_attn_unpadded_func = None
81
+ flash_attn_func = None
82
+
83
+ def _import_flash_attn():
84
+ global apply_rotary_emb_func, rms_norm, flash_attn_unpadded_func, flash_attn_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
+ _flash_attn_func = None
106
+ if not hasattr(flash_attn, '__version__'):
107
+ from flash_attn.flash_attn_interface import flash_attn_unpadded_func as __flash_attn_unpadded_func
108
+ else:
109
+ if int(flash_attn.__version__.split(".")[0]) >= 2:
110
+ if int(flash_attn.__version__.split(".")[1]) >= 1:
111
+ from flash_attn.flash_attn_interface import flash_attn_func as _flash_attn_func
112
+ from flash_attn.flash_attn_interface import flash_attn_varlen_func as __flash_attn_unpadded_func
113
+ else:
114
+ from flash_attn.flash_attn_interface import flash_attn_unpadded_func as __flash_attn_unpadded_func
115
+ flash_attn_unpadded_func = __flash_attn_unpadded_func
116
+ flash_attn_func = _flash_attn_func
117
+ except ImportError:
118
+ logger.warn(
119
+ "Warning: import flash_attn fail, please install FlashAttention to get higher efficiency "
120
+ "https://github.com/Dao-AILab/flash-attention"
121
+ )
122
+
123
+ def quantize_cache_v(fdata, bits, qmax, qmin):
124
+ # b, s, head, h-dim->b, head, s, h-dim
125
+ qtype = torch.uint8
126
+ device = fdata.device
127
+ shape = fdata.shape
128
+
129
+ fdata_cal = torch.flatten(fdata, 2)
130
+ fmax = torch.amax(fdata_cal, dim=-1, keepdim=True)
131
+ fmin = torch.amin(fdata_cal, dim=-1, keepdim=True)
132
+ # Compute params
133
+ if qmax.device != fmax.device:
134
+ qmax = qmax.to(device)
135
+ qmin = qmin.to(device)
136
+ scale = (fmax - fmin) / (qmax - qmin)
137
+ zero = qmin - fmin / scale
138
+ scale = scale.unsqueeze(-1).repeat(1,1,shape[2],1).contiguous()
139
+ zero = zero.unsqueeze(-1).repeat(1,1,shape[2],1).contiguous()
140
+ # Quantize
141
+ res_data = fdata / scale + zero
142
+ qdata = torch.clamp(res_data, qmin, qmax).to(qtype)
143
+ return qdata.contiguous(), scale, zero
144
+
145
+ def dequantize_cache_torch(qdata, scale, zero):
146
+ data = scale * (qdata - zero)
147
+ return data
148
+
149
+ class FlashSelfAttention(torch.nn.Module):
150
+ def __init__(
151
+ self,
152
+ causal=False,
153
+ softmax_scale=None,
154
+ attention_dropout=0.0,
155
+ ):
156
+ super().__init__()
157
+ assert flash_attn_unpadded_func is not None, (
158
+ "Please install FlashAttention first, " "e.g., with pip install flash-attn"
159
+ )
160
+ assert (
161
+ rearrange is not None
162
+ ), "Please install einops first, e.g., with pip install einops"
163
+ self.causal = causal
164
+ self.softmax_scale = softmax_scale
165
+ self.dropout_p = attention_dropout
166
+
167
+ def unpad_input(self, hidden_states, attention_mask):
168
+ valid_mask = attention_mask.squeeze(1).squeeze(1).eq(0)
169
+ seqlens_in_batch = valid_mask.sum(dim=-1, dtype=torch.int32)
170
+ indices = torch.nonzero(valid_mask.flatten(), as_tuple=False).flatten()
171
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
172
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
173
+ hidden_states = hidden_states[indices]
174
+ return hidden_states, indices, cu_seqlens, max_seqlen_in_batch
175
+
176
+ def pad_input(self, hidden_states, indices, batch, seqlen):
177
+ output = torch.zeros(batch * seqlen, *hidden_states.shape[1:], device=hidden_states.device,
178
+ dtype=hidden_states.dtype)
179
+ output[indices] = hidden_states
180
+ return rearrange(output, '(b s) ... -> b s ...', b=batch)
181
+
182
+ def forward(self, q, k, v, attention_mask=None):
183
+ assert all((i.dtype in [torch.float16, torch.bfloat16] for i in (q, k, v)))
184
+ assert all((i.is_cuda for i in (q, k, v)))
185
+ batch_size, seqlen_q = q.shape[0], q.shape[1]
186
+ seqlen_k = k.shape[1]
187
+ seqlen_out = seqlen_q
188
+
189
+ if flash_attn_func is not None and batch_size == 1:
190
+ dropout_p = self.dropout_p if self.training else 0
191
+ output = flash_attn_func(q, k, v, dropout_p, softmax_scale=self.softmax_scale, causal=self.causal)
192
+ return output
193
+
194
+ q, k, v = [rearrange(x, "b s ... -> (b s) ...") for x in [q, k, v]]
195
+ cu_seqlens_q = torch.arange(
196
+ 0,
197
+ (batch_size + 1) * seqlen_q,
198
+ step=seqlen_q,
199
+ dtype=torch.int32,
200
+ device=q.device,
201
+ )
202
+
203
+ if batch_size > 1 and attention_mask is not None:
204
+ k, indices_k, cu_seqlens_k, seqlen_k = self.unpad_input(k, attention_mask)
205
+ if q.size(0) == v.size(0):
206
+ q = q[indices_k]
207
+ cu_seqlens_q = cu_seqlens_k
208
+ seqlen_q = seqlen_k
209
+ v = v[indices_k]
210
+ else:
211
+ cu_seqlens_k = torch.arange(
212
+ 0,
213
+ (batch_size + 1) * seqlen_k,
214
+ step=seqlen_k,
215
+ dtype=torch.int32,
216
+ device=q.device,
217
+ )
218
+
219
+ if self.training:
220
+ assert seqlen_k == seqlen_q
221
+ is_causal = self.causal
222
+ dropout_p = self.dropout_p
223
+ else:
224
+ is_causal = seqlen_q == seqlen_k
225
+ dropout_p = 0
226
+
227
+ output = flash_attn_unpadded_func(
228
+ q,
229
+ k,
230
+ v,
231
+ cu_seqlens_q,
232
+ cu_seqlens_k,
233
+ seqlen_q,
234
+ seqlen_k,
235
+ dropout_p,
236
+ softmax_scale=self.softmax_scale,
237
+ causal=is_causal,
238
+ )
239
+ if batch_size > 1 and attention_mask is not None and seqlen_q == seqlen_k:
240
+ output = self.pad_input(output, indices_k, batch_size, seqlen_out)
241
+ else:
242
+ new_shape = (batch_size, output.shape[0] // batch_size) + output.shape[1:]
243
+ output = output.view(new_shape)
244
+ return output
245
+
246
+
247
+ class QWenAttention(nn.Module):
248
+ def __init__(self, config):
249
+ super().__init__()
250
+
251
+ self.register_buffer("masked_bias", torch.tensor(-1e4), persistent=False)
252
+ self.seq_length = config.seq_length
253
+
254
+ self.hidden_size = config.hidden_size
255
+ self.split_size = config.hidden_size
256
+ self.num_heads = config.num_attention_heads
257
+ self.head_dim = self.hidden_size // self.num_heads
258
+
259
+ self.use_flash_attn = config.use_flash_attn
260
+ self.scale_attn_weights = True
261
+
262
+ self.projection_size = config.kv_channels * config.num_attention_heads
263
+
264
+ assert self.projection_size % config.num_attention_heads == 0
265
+ self.hidden_size_per_attention_head = (
266
+ self.projection_size // config.num_attention_heads
267
+ )
268
+
269
+ self.c_attn = nn.Linear(config.hidden_size, 3 * self.projection_size)
270
+
271
+ self.c_proj = nn.Linear(
272
+ config.hidden_size, self.projection_size, bias=not config.no_bias
273
+ )
274
+
275
+ self.is_fp32 = not (config.bf16 or config.fp16)
276
+ if (
277
+ self.use_flash_attn
278
+ and flash_attn_unpadded_func is not None
279
+ and not self.is_fp32
280
+ ):
281
+ self.core_attention_flash = FlashSelfAttention(
282
+ causal=True, attention_dropout=config.attn_dropout_prob
283
+ )
284
+ self.bf16 = config.bf16
285
+
286
+ self.use_dynamic_ntk = config.use_dynamic_ntk
287
+ self.use_logn_attn = config.use_logn_attn
288
+
289
+ logn_list = [
290
+ math.log(i, self.seq_length) if i > self.seq_length else 1
291
+ for i in range(1, 32768)
292
+ ]
293
+ logn_tensor = torch.tensor(logn_list)[None, :, None, None]
294
+ self.register_buffer("logn_tensor", logn_tensor, persistent=False)
295
+
296
+ self.attn_dropout = nn.Dropout(config.attn_dropout_prob)
297
+ self.softmax_in_fp32 = config.softmax_in_fp32 if hasattr(config, 'softmax_in_fp32') else False
298
+ self.use_cache_quantization = config.use_cache_quantization if hasattr(config, 'use_cache_quantization') else False
299
+ self.use_cache_kernel = config.use_cache_kernel if hasattr(config,'use_cache_kernel') else False
300
+ cache_dtype = torch.float
301
+ if self.bf16:
302
+ cache_dtype=torch.bfloat16
303
+ elif config.fp16:
304
+ cache_dtype = torch.float16
305
+ self.cache_qmax = torch.tensor(torch.iinfo(torch.uint8).max, dtype=cache_dtype)
306
+ self.cache_qmin = torch.tensor(torch.iinfo(torch.uint8).min, dtype=cache_dtype)
307
+
308
+ if config.use_cache_quantization and config.use_cache_kernel:
309
+ # pre check if the support files existing
310
+ module_root = pathlib.Path(__file__).parent
311
+ src_files = ("cache_autogptq_cuda_256.cpp", "cache_autogptq_cuda_kernel_256.cu")
312
+ if any(not (module_root/src).is_file() for src in src_files):
313
+ warnings.warn("KV cache kernel source files (.cpp and .cu) not found.")
314
+ self.cache_kernels = None
315
+ else:
316
+ try:
317
+ from .cpp_kernels import cache_autogptq_cuda_256
318
+ self.cache_kernels = cache_autogptq_cuda_256
319
+ except ImportError:
320
+ warnings.warn("Failed to import KV cache kernels.")
321
+ self.cache_kernels = None
322
+
323
+ def _attn(self, query, key, value, causal_mask=None, attention_mask=None, head_mask=None):
324
+ device = query.device
325
+ if self.use_cache_quantization:
326
+ qk, qk_scale, qk_zero = key
327
+ if self.use_cache_kernel and self.cache_kernels is not None:
328
+ shape = query.shape[:-1] + (qk.shape[-2],)
329
+ attn_weights = torch.zeros(shape, dtype=torch.float16, device=device)
330
+ self.cache_kernels.vecquant8matmul_batched_faster_old(
331
+ query.contiguous() if query.dtype == torch.float16 else query.to(torch.float16).contiguous(),
332
+ qk.transpose(-1, -2).contiguous(),
333
+ attn_weights,
334
+ qk_scale.contiguous() if qk_scale.dtype == torch.float16 else qk_scale.to(torch.float16).contiguous(),
335
+ qk_zero.contiguous()if qk_zero.dtype == torch.float16 else qk_zero.to(torch.float16).contiguous())
336
+ # attn_weights = attn_weights.to(query.dtype).contiguous()
337
+ else:
338
+ key = dequantize_cache_torch(qk, qk_scale, qk_zero)
339
+ attn_weights = torch.matmul(query, key.transpose(-1, -2))
340
+ else:
341
+ attn_weights = torch.matmul(query, key.transpose(-1, -2))
342
+
343
+ if self.scale_attn_weights:
344
+ if self.use_cache_quantization:
345
+ size_temp = value[0].size(-1)
346
+ else:
347
+ size_temp = value.size(-1)
348
+ attn_weights = attn_weights / (size_temp ** 0.5)
349
+
350
+ mask_value = torch.finfo(attn_weights.dtype).min
351
+ if causal_mask is not None:
352
+ attn_weights = torch.where(
353
+ causal_mask, attn_weights.to(attn_weights.dtype), mask_value
354
+ )
355
+
356
+ if attention_mask is not None:
357
+ attn_weights = attn_weights + attention_mask
358
+
359
+ if self.softmax_in_fp32:
360
+ attn_weights = nn.functional.softmax(attn_weights.float(), dim=-1)
361
+ else:
362
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1)
363
+
364
+ attn_weights = attn_weights.type(query.dtype)
365
+ attn_weights = self.attn_dropout(attn_weights)
366
+
367
+ if head_mask is not None:
368
+ attn_weights = attn_weights * head_mask
369
+
370
+ if self.use_cache_quantization:
371
+ qv, qv_scale, qv_zero = value
372
+ if self.use_cache_kernel and self.cache_kernels is not None:
373
+ shape = attn_weights.shape[:-1] + (query.shape[-1],)
374
+ attn_output = torch.zeros(shape, dtype=torch.float16, device=device)
375
+ self.cache_kernels.vecquant8matmul_batched_column_compression_faster_old(
376
+ attn_weights.contiguous() if attn_weights.dtype == torch.float16 else attn_weights.to(torch.float16).contiguous(),
377
+ qv.contiguous(), # dtype: int32
378
+ attn_output,
379
+ qv_scale.contiguous() if qv_scale.dtype == torch.float16 else qv_scale.to(torch.float16).contiguous(),
380
+ qv_zero.contiguous() if qv_zero.dtype == torch.float16 else qv_zero.to(torch.float16).contiguous())
381
+ if attn_output.dtype != query.dtype:
382
+ attn_output = attn_output.to(query.dtype)
383
+ attn_weights = attn_weights.to(query.dtype)
384
+ else:
385
+ value = dequantize_cache_torch(qv, qv_scale, qv_zero)
386
+ attn_output = torch.matmul(attn_weights, value)
387
+ else:
388
+ attn_output = torch.matmul(attn_weights, value)
389
+
390
+ attn_output = attn_output.transpose(1, 2)
391
+
392
+ return attn_output, attn_weights
393
+
394
+ def _split_heads(self, tensor, num_heads, attn_head_size):
395
+ new_shape = tensor.size()[:-1] + (num_heads, attn_head_size)
396
+ tensor = tensor.view(new_shape)
397
+ return tensor
398
+
399
+ def _merge_heads(self, tensor, num_heads, attn_head_size):
400
+ tensor = tensor.contiguous()
401
+ new_shape = tensor.size()[:-2] + (num_heads * attn_head_size,)
402
+ return tensor.view(new_shape)
403
+
404
+ def forward(
405
+ self,
406
+ hidden_states: Optional[Tuple[torch.FloatTensor]],
407
+ rotary_pos_emb_list: Optional[List[List[torch.Tensor]]] = None,
408
+ layer_past: Optional[Tuple[torch.Tensor]] = None,
409
+ attention_mask: Optional[torch.FloatTensor] = None,
410
+ head_mask: Optional[torch.FloatTensor] = None,
411
+ encoder_hidden_states: Optional[torch.Tensor] = None,
412
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
413
+ output_attentions: Optional[bool] = False,
414
+ use_cache: Optional[bool] = False,
415
+ ):
416
+ mixed_x_layer = self.c_attn(hidden_states)
417
+
418
+ query, key, value = mixed_x_layer.split(self.split_size, dim=2)
419
+
420
+ query = self._split_heads(query, self.num_heads, self.head_dim)
421
+ key = self._split_heads(key, self.num_heads, self.head_dim)
422
+ value = self._split_heads(value, self.num_heads, self.head_dim)
423
+
424
+ if rotary_pos_emb_list is not None:
425
+ cur_len = query.shape[1]
426
+ if len(rotary_pos_emb_list) == 1:
427
+ rotary_pos_emb = rotary_pos_emb_list[0]
428
+ rotary_pos_emb = [i[:, -cur_len:, :, :] for i in rotary_pos_emb]
429
+ rotary_pos_emb = (rotary_pos_emb,) * 2
430
+ q_pos_emb, k_pos_emb = rotary_pos_emb
431
+ # Slice the pos emb for current inference
432
+ query = apply_rotary_pos_emb(query, q_pos_emb)
433
+ key = apply_rotary_pos_emb(key, k_pos_emb)
434
+ else:
435
+ query_list = []
436
+ key_list = []
437
+ for i, rotary_pos_emb in enumerate(rotary_pos_emb_list):
438
+ rotary_pos_emb = [i[:, -cur_len:, :, :] for i in rotary_pos_emb]
439
+ rotary_pos_emb = (rotary_pos_emb,) * 2
440
+ q_pos_emb, k_pos_emb = rotary_pos_emb
441
+ # Slice the pos emb for current inference
442
+ query_list += [apply_rotary_pos_emb(query[i:i+1, :, :], q_pos_emb)]
443
+ key_list += [apply_rotary_pos_emb(key[i:i+1, :, :], k_pos_emb)]
444
+ query = torch.cat(query_list, dim=0)
445
+ key = torch.cat(key_list, dim=0)
446
+
447
+ if self.use_cache_quantization:
448
+ key = quantize_cache_v(key.permute(0, 2, 1, 3),
449
+ bits=8,
450
+ qmin=self.cache_qmin,
451
+ qmax=self.cache_qmax)
452
+ value = quantize_cache_v(value.permute(0, 2, 1, 3),
453
+ bits=8,
454
+ qmin=self.cache_qmin,
455
+ qmax=self.cache_qmax)
456
+
457
+
458
+ if layer_past is not None:
459
+ past_key, past_value = layer_past[0], layer_past[1]
460
+ if self.use_cache_quantization:
461
+ # use_cache_quantization:
462
+ # present=((q_key,key_scale,key_zero_point),
463
+ # (q_value,value_scale,value_zero_point))
464
+ key = (torch.cat((past_key[0], key[0]), dim=2),
465
+ torch.cat((past_key[1], key[1]), dim=2),
466
+ torch.cat((past_key[2], key[2]), dim=2))
467
+ value = (torch.cat((past_value[0], value[0]), dim=2),
468
+ torch.cat((past_value[1], value[1]), dim=2),
469
+ torch.cat((past_value[2], value[2]), dim=2))
470
+ else:
471
+ # not use_cache_quantization:
472
+ # present=(key,value)
473
+ key = torch.cat((past_key, key), dim=1)
474
+ value = torch.cat((past_value, value), dim=1)
475
+
476
+ if use_cache:
477
+ present = (key, value)
478
+ else:
479
+ present = None
480
+
481
+ key_size = key[0].size(2) if self.use_cache_quantization else key.size(1)
482
+ if key_size > self.seq_length and self.use_logn_attn and not self.training:
483
+ if self.use_cache_quantization:
484
+ seq_start = key[0].size(2) - query.size(1)
485
+ seq_end = key[0].size(2)
486
+ else:
487
+ seq_start = key.size(1) - query.size(1)
488
+ seq_end = key.size(1)
489
+ logn_tensor = self.logn_tensor[:, seq_start:seq_end, :, :].type_as(query)
490
+ query = query * logn_tensor.expand_as(query)
491
+
492
+ if (
493
+ self.use_flash_attn
494
+ and flash_attn_unpadded_func is not None
495
+ and not self.is_fp32
496
+ and query.is_cuda
497
+ ):
498
+ q, k, v = query, key, value
499
+ attn_output = self.core_attention_flash(q, k, v, attention_mask=attention_mask)
500
+ else:
501
+ key_size = key[0].size(2) if self.use_cache_quantization else key.size(1)
502
+ if query.size(1) == key_size:
503
+ causal_mask = torch.tril(
504
+ torch.ones((key_size, key_size), dtype=torch.bool, device=query.device)
505
+ ).view(1, 1, key_size, key_size)
506
+ else:
507
+ causal_mask = None
508
+ query = query.permute(0, 2, 1, 3)
509
+ if not self.use_cache_quantization:
510
+ key = key.permute(0, 2, 1, 3)
511
+ value = value.permute(0, 2, 1, 3)
512
+ if (
513
+ causal_mask is None
514
+ and self.use_flash_attn
515
+ and flash_attn_unpadded_func is not None
516
+ and not self.is_fp32
517
+ and not query.is_cuda
518
+ ):
519
+ raise Exception(_ERROR_INPUT_CPU_QUERY_WITH_FLASH_ATTN_ACTIVATED)
520
+
521
+ if not self.use_cache_quantization and SUPPORT_TORCH2:
522
+ if attention_mask is not None:
523
+ attention_mask = attention_mask.expand(-1, -1, query.size(2), -1)
524
+ if causal_mask is not None:
525
+ attention_mask = attention_mask.masked_fill(~causal_mask, torch.finfo(query.dtype).min)
526
+ else:
527
+ attention_mask = causal_mask
528
+ attn_output = F.scaled_dot_product_attention(
529
+ query, key, value, attn_mask=attention_mask
530
+ ).transpose(1, 2)
531
+ attn_weight = None
532
+ else:
533
+ attn_output, attn_weight = self._attn(
534
+ query, key, value, causal_mask, attention_mask, head_mask
535
+ )
536
+ context_layer = self._merge_heads(
537
+ attn_output, self.num_heads, self.head_dim
538
+ )
539
+
540
+ attn_output = self.c_proj(context_layer)
541
+
542
+ outputs = (attn_output, present)
543
+ if output_attentions:
544
+ if (
545
+ self.use_flash_attn
546
+ and flash_attn_unpadded_func is not None
547
+ and not self.is_fp32
548
+ ):
549
+ raise ValueError("Cannot output attentions while using flash-attn")
550
+ elif not self.use_cache_quantization and SUPPORT_TORCH2:
551
+ raise ValueError("Cannot output attentions while using scaled_dot_product_attention")
552
+ else:
553
+ outputs += (attn_weight,)
554
+
555
+ return outputs
556
+
557
+
558
+ class QWenMLP(nn.Module):
559
+ def __init__(self, config):
560
+ super().__init__()
561
+ self.w1 = nn.Linear(
562
+ config.hidden_size, config.intermediate_size // 2, bias=not config.no_bias
563
+ )
564
+ self.w2 = nn.Linear(
565
+ config.hidden_size, config.intermediate_size // 2, bias=not config.no_bias
566
+ )
567
+ ff_dim_in = config.intermediate_size // 2
568
+ self.c_proj = nn.Linear(ff_dim_in, config.hidden_size, bias=not config.no_bias)
569
+
570
+ def forward(self, hidden_states):
571
+ a1 = self.w1(hidden_states)
572
+ a2 = self.w2(hidden_states)
573
+ intermediate_parallel = a1 * F.silu(a2)
574
+ output = self.c_proj(intermediate_parallel)
575
+ return output
576
+
577
+
578
+ class QWenBlock(nn.Module):
579
+ def __init__(self, config):
580
+ super().__init__()
581
+ hidden_size = config.hidden_size
582
+ self.bf16 = config.bf16
583
+
584
+ self.ln_1 = RMSNorm(
585
+ hidden_size,
586
+ eps=config.layer_norm_epsilon,
587
+ )
588
+ self.attn = QWenAttention(config)
589
+ self.ln_2 = RMSNorm(
590
+ hidden_size,
591
+ eps=config.layer_norm_epsilon,
592
+ )
593
+
594
+ self.mlp = QWenMLP(config)
595
+
596
+ def forward(
597
+ self,
598
+ hidden_states: Optional[Tuple[torch.FloatTensor]],
599
+ rotary_pos_emb_list: Optional[List[List[torch.Tensor]]] = None,
600
+ layer_past: Optional[Tuple[torch.Tensor]] = None,
601
+ attention_mask: Optional[torch.FloatTensor] = None,
602
+ head_mask: Optional[torch.FloatTensor] = None,
603
+ encoder_hidden_states: Optional[torch.Tensor] = None,
604
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
605
+ use_cache: Optional[bool] = False,
606
+ output_attentions: Optional[bool] = False,
607
+ ):
608
+ layernorm_output = self.ln_1(hidden_states)
609
+
610
+ attn_outputs = self.attn(
611
+ layernorm_output,
612
+ rotary_pos_emb_list,
613
+ layer_past=layer_past,
614
+ attention_mask=attention_mask,
615
+ head_mask=head_mask,
616
+ use_cache=use_cache,
617
+ output_attentions=output_attentions,
618
+ )
619
+ attn_output = attn_outputs[0]
620
+
621
+ outputs = attn_outputs[1:]
622
+
623
+ residual = hidden_states
624
+ layernorm_input = attn_output + residual
625
+
626
+ layernorm_output = self.ln_2(layernorm_input)
627
+
628
+ residual = layernorm_input
629
+ mlp_output = self.mlp(layernorm_output)
630
+ hidden_states = residual + mlp_output
631
+
632
+ if use_cache:
633
+ outputs = (hidden_states,) + outputs
634
+ else:
635
+ outputs = (hidden_states,) + outputs[1:]
636
+
637
+ return outputs
638
+
639
+
640
+ class QWenPreTrainedModel(PreTrainedModel):
641
+ config_class = QWenConfig
642
+ base_model_prefix = "transformer"
643
+ is_parallelizable = False
644
+ supports_gradient_checkpointing = True
645
+ _no_split_modules = ["QWenBlock"]
646
+ _skip_keys_device_placement = "past_key_values"
647
+
648
+ def __init__(self, *inputs, **kwargs):
649
+ super().__init__(*inputs, **kwargs)
650
+
651
+ def _init_weights(self, module):
652
+ """Initialize the weights."""
653
+ if isinstance(module, nn.Linear):
654
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
655
+ if module.bias is not None:
656
+ module.bias.data.zero_()
657
+ elif isinstance(module, nn.Embedding):
658
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
659
+ if module.padding_idx is not None:
660
+ module.weight.data[module.padding_idx].zero_()
661
+ elif isinstance(module, RMSNorm):
662
+ module.weight.data.fill_(1.0)
663
+
664
+ for name, p in module.named_parameters():
665
+ if name == "c_proj.weight":
666
+ p.data.normal_(
667
+ mean=0.0,
668
+ std=(
669
+ self.config.initializer_range
670
+ / math.sqrt(2 * self.config.num_hidden_layers)
671
+ ),
672
+ )
673
+ def _set_gradient_checkpointing(self, enable: bool = False, gradient_checkpointing_func: Callable = None):
674
+ is_gradient_checkpointing_set = False
675
+
676
+ if isinstance(self, QWenModel):
677
+ self.gradient_checkpointing = enable
678
+ self._gradient_checkpointing_func = gradient_checkpointing_func
679
+ is_gradient_checkpointing_set = True
680
+
681
+ for module in self.modules():
682
+ if isinstance(module, QWenModel):
683
+ module.gradient_checkpointing = enable
684
+ module._gradient_checkpointing_func = gradient_checkpointing_func
685
+ is_gradient_checkpointing_set = True
686
+
687
+ if not is_gradient_checkpointing_set:
688
+ raise ValueError(f"{self.__class__.__name__} is not compatible with gradient checkpointing. Make sure all the architecture support it by setting a boolean attribute 'gradient_checkpointing' to modules of the model that uses checkpointing.")
689
+
690
+
691
+ class QWenModel(QWenPreTrainedModel):
692
+ _keys_to_ignore_on_load_missing = ["attn.masked_bias"]
693
+
694
+ def __init__(self, config):
695
+ super().__init__(config)
696
+ self.vocab_size = config.vocab_size
697
+ self.num_hidden_layers = config.num_hidden_layers
698
+ self.embed_dim = config.hidden_size
699
+ self.use_cache_quantization = self.config.use_cache_quantization if hasattr(self.config, 'use_cache_quantization') else False
700
+
701
+ self.gradient_checkpointing = False
702
+ self.use_dynamic_ntk = config.use_dynamic_ntk
703
+ self.seq_length = config.seq_length
704
+
705
+ self.wte = nn.Embedding(self.vocab_size, self.embed_dim)
706
+
707
+ self.drop = nn.Dropout(config.emb_dropout_prob)
708
+
709
+ if config.rotary_pct == 1.0:
710
+ self.rotary_ndims = None
711
+ else:
712
+ assert config.rotary_pct < 1
713
+ self.rotary_ndims = int(
714
+ config.kv_channels * config.rotary_pct
715
+ )
716
+ dim = (
717
+ self.rotary_ndims
718
+ if self.rotary_ndims is not None
719
+ else config.kv_channels
720
+ )
721
+ self.rotary_emb = RotaryEmbedding(dim, base=config.rotary_emb_base)
722
+
723
+ self.use_flash_attn = config.use_flash_attn
724
+ self.is_fp32 = not (config.bf16 or config.fp16)
725
+
726
+ self.h = nn.ModuleList(
727
+ [
728
+ QWenBlock(
729
+ config
730
+ )
731
+ for i in range(config.num_hidden_layers)
732
+ ]
733
+ )
734
+ self.ln_f = RMSNorm(
735
+ self.embed_dim,
736
+ eps=config.layer_norm_epsilon,
737
+ )
738
+
739
+ self.post_init()
740
+
741
+ def get_input_embeddings(self):
742
+ return self.wte
743
+
744
+ def set_input_embeddings(self, new_embeddings):
745
+ self.wte = new_embeddings
746
+
747
+ def get_ntk_alpha(self, true_seq_len):
748
+ context_value = math.log(true_seq_len / self.seq_length, 2) + 1
749
+ ntk_alpha = 2 ** math.ceil(context_value) - 1
750
+ ntk_alpha = max(ntk_alpha, 1)
751
+ return ntk_alpha
752
+
753
+ def forward(
754
+ self,
755
+ input_ids: Optional[torch.LongTensor] = None,
756
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
757
+ attention_mask: Optional[torch.FloatTensor] = None,
758
+ token_type_ids: Optional[torch.LongTensor] = None,
759
+ position_ids: Optional[torch.LongTensor] = None,
760
+ head_mask: Optional[torch.FloatTensor] = None,
761
+ inputs_embeds: Optional[torch.FloatTensor] = None,
762
+ encoder_hidden_states: Optional[torch.Tensor] = None,
763
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
764
+ use_cache: Optional[bool] = None,
765
+ output_attentions: Optional[bool] = None,
766
+ output_hidden_states: Optional[bool] = None,
767
+ return_dict: Optional[bool] = None,
768
+ ):
769
+ output_attentions = (
770
+ output_attentions
771
+ if output_attentions is not None
772
+ else self.config.output_attentions
773
+ )
774
+ output_hidden_states = (
775
+ output_hidden_states
776
+ if output_hidden_states is not None
777
+ else self.config.output_hidden_states
778
+ )
779
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
780
+ return_dict = (
781
+ return_dict if return_dict is not None else self.config.use_return_dict
782
+ )
783
+
784
+ if input_ids is not None and inputs_embeds is not None:
785
+ raise ValueError(
786
+ "You cannot specify both input_ids and inputs_embeds at the same time"
787
+ )
788
+ elif input_ids is not None:
789
+ input_shape = input_ids.size()
790
+ input_ids = input_ids.view(-1, input_shape[-1])
791
+ batch_size = input_ids.shape[0]
792
+ elif inputs_embeds is not None:
793
+ input_shape = inputs_embeds.size()[:-1]
794
+ batch_size = inputs_embeds.shape[0]
795
+ else:
796
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
797
+
798
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
799
+
800
+ if token_type_ids is not None:
801
+ token_type_ids = token_type_ids.view(-1, input_shape[-1])
802
+ if position_ids is not None:
803
+ position_ids = position_ids.view(-1, input_shape[-1])
804
+
805
+ if past_key_values is None:
806
+ past_length = 0
807
+ past_key_values = tuple([None] * len(self.h))
808
+ else:
809
+ if self.use_cache_quantization:
810
+ past_length = past_key_values[0][0][0].size(2)
811
+ else:
812
+ past_length = past_key_values[0][0].size(-2)
813
+ if position_ids is None:
814
+ position_ids = torch.arange(
815
+ past_length,
816
+ input_shape[-1] + past_length,
817
+ dtype=torch.long,
818
+ device=device,
819
+ )
820
+ position_ids = position_ids.unsqueeze(0).view(-1, input_shape[-1])
821
+
822
+ if attention_mask is not None:
823
+ if batch_size <= 0:
824
+ raise ValueError("batch_size has to be defined and > 0")
825
+ attention_mask = attention_mask.view(batch_size, -1)
826
+ attention_mask = attention_mask[:, None, None, :]
827
+ attention_mask = attention_mask.to(dtype=self.dtype)
828
+ attention_mask = (1.0 - attention_mask) * torch.finfo(self.dtype).min
829
+
830
+ encoder_attention_mask = None
831
+ head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers)
832
+
833
+ if inputs_embeds is None:
834
+ inputs_embeds = self.wte(input_ids)
835
+ hidden_states = inputs_embeds
836
+
837
+ kv_seq_len = hidden_states.size()[1]
838
+ if past_key_values[0] is not None:
839
+ # past key values[0][0] shape: bs * seq_len * head_num * dim
840
+ if self.use_cache_quantization:
841
+ kv_seq_len += past_key_values[0][0][0].shape[2]
842
+ else:
843
+ kv_seq_len += past_key_values[0][0].shape[1]
844
+
845
+ if self.training or not self.use_dynamic_ntk:
846
+ ntk_alpha_list = [1.0]
847
+ elif kv_seq_len != hidden_states.size()[1]:
848
+ ntk_alpha_list = self.rotary_emb._ntk_alpha_cached_list
849
+ else:
850
+ ntk_alpha_list = []
851
+ if attention_mask is not None and kv_seq_len > self.seq_length:
852
+ true_seq_lens = attention_mask.squeeze(1).squeeze(1).eq(0).sum(dim=-1, dtype=torch.int32)
853
+ for i in range(hidden_states.size()[0]):
854
+ true_seq_len = true_seq_lens[i].item()
855
+ ntk_alpha = self.get_ntk_alpha(true_seq_len)
856
+ ntk_alpha_list.append(ntk_alpha)
857
+ else:
858
+ ntk_alpha = self.get_ntk_alpha(kv_seq_len)
859
+ ntk_alpha_list.append(ntk_alpha)
860
+ self.rotary_emb._ntk_alpha_cached_list = ntk_alpha_list
861
+ rotary_pos_emb_list = [
862
+ self.rotary_emb(kv_seq_len, ntk_alpha=ntk_alpha) for ntk_alpha in ntk_alpha_list
863
+ ]
864
+
865
+ hidden_states = self.drop(hidden_states)
866
+ output_shape = input_shape + (hidden_states.size(-1),)
867
+
868
+ if self.gradient_checkpointing and self.training:
869
+ if use_cache:
870
+ logger.warning_once(
871
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
872
+ )
873
+ use_cache = False
874
+
875
+ presents = () if use_cache else None
876
+ all_self_attentions = () if output_attentions else None
877
+ all_hidden_states = () if output_hidden_states else None
878
+ for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)):
879
+
880
+ if output_hidden_states:
881
+ all_hidden_states = all_hidden_states + (hidden_states,)
882
+
883
+ if self.gradient_checkpointing and self.training:
884
+
885
+ def create_custom_forward(module):
886
+ def custom_forward(*inputs):
887
+ # None for past_key_value
888
+ return module(*inputs, use_cache, output_attentions)
889
+
890
+ return custom_forward
891
+
892
+ outputs = torch.utils.checkpoint.checkpoint(
893
+ create_custom_forward(block),
894
+ hidden_states,
895
+ rotary_pos_emb_list,
896
+ None,
897
+ attention_mask,
898
+ head_mask[i],
899
+ encoder_hidden_states,
900
+ encoder_attention_mask,
901
+ )
902
+ else:
903
+ outputs = block(
904
+ hidden_states,
905
+ layer_past=layer_past,
906
+ rotary_pos_emb_list=rotary_pos_emb_list,
907
+ attention_mask=attention_mask,
908
+ head_mask=head_mask[i],
909
+ encoder_hidden_states=encoder_hidden_states,
910
+ encoder_attention_mask=encoder_attention_mask,
911
+ use_cache=use_cache,
912
+ output_attentions=output_attentions,
913
+ )
914
+
915
+ hidden_states = outputs[0]
916
+ if use_cache is True:
917
+ presents = presents + (outputs[1],)
918
+
919
+ if output_attentions:
920
+ all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],)
921
+
922
+ hidden_states = self.ln_f(hidden_states)
923
+ hidden_states = hidden_states.view(output_shape)
924
+ # Add last hidden state
925
+ if output_hidden_states:
926
+ all_hidden_states = all_hidden_states + (hidden_states,)
927
+
928
+ if not return_dict:
929
+ return tuple(
930
+ v for v in [hidden_states, presents, all_hidden_states] if v is not None
931
+ )
932
+
933
+ return BaseModelOutputWithPast(
934
+ last_hidden_state=hidden_states,
935
+ past_key_values=presents,
936
+ hidden_states=all_hidden_states,
937
+ attentions=all_self_attentions,
938
+ )
939
+
940
+
941
+ class QWenLMHeadModel(QWenPreTrainedModel):
942
+ _keys_to_ignore_on_load_missing = [r"h\.\d+\.attn\.rotary_emb\.inv_freq"]
943
+ _keys_to_ignore_on_load_unexpected = [r"h\.\d+\.attn\.masked_bias"]
944
+
945
+ def __init__(self, config):
946
+ super().__init__(config)
947
+ assert (
948
+ config.bf16 + config.fp16 + config.fp32 <= 1
949
+ ), "Only one of \"bf16\", \"fp16\", \"fp32\" can be true"
950
+
951
+ autoset_precision = config.bf16 + config.fp16 + config.fp32 == 0
952
+
953
+ if autoset_precision:
954
+ if SUPPORT_BF16:
955
+ logger.warn(
956
+ "The model is automatically converting to bf16 for faster inference. "
957
+ "If you want to disable the automatic precision, please manually add bf16/fp16/fp32=True to \"AutoModelForCausalLM.from_pretrained\"."
958
+ )
959
+ config.bf16 = True
960
+ elif SUPPORT_FP16:
961
+ logger.warn(
962
+ "The model is automatically converting to fp16 for faster inference. "
963
+ "If you want to disable the automatic precision, please manually add bf16/fp16/fp32=True to \"AutoModelForCausalLM.from_pretrained\"."
964
+ )
965
+ config.fp16 = True
966
+ else:
967
+ config.fp32 = True
968
+
969
+ if config.bf16 and SUPPORT_CUDA and not SUPPORT_BF16:
970
+ logger.warn("Your device does NOT seem to support bf16, you can switch to fp16 or fp32 by by passing fp16/fp32=True in \"AutoModelForCausalLM.from_pretrained\".")
971
+ if config.fp16 and SUPPORT_CUDA and not SUPPORT_FP16:
972
+ logger.warn("Your device does NOT support faster inference with fp16, please switch to fp32 which is likely to be faster")
973
+ if config.fp32:
974
+ if SUPPORT_BF16:
975
+ logger.warn("Your device support faster inference by passing bf16=True in \"AutoModelForCausalLM.from_pretrained\".")
976
+ elif SUPPORT_FP16:
977
+ logger.warn("Your device support faster inference by passing fp16=True in \"AutoModelForCausalLM.from_pretrained\".")
978
+
979
+ if config.use_flash_attn == "auto":
980
+ if config.bf16 or config.fp16:
981
+ logger.warn("Try importing flash-attention for faster inference...")
982
+ config.use_flash_attn = True
983
+ else:
984
+ config.use_flash_attn = False
985
+ if config.use_flash_attn and config.fp32:
986
+ logger.warn("Flash attention will be disabled because it does NOT support fp32.")
987
+
988
+ if config.use_flash_attn:
989
+ _import_flash_attn()
990
+
991
+ self.transformer = QWenModel(config)
992
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
993
+
994
+ if config.bf16:
995
+ self.transformer.bfloat16()
996
+ self.lm_head.bfloat16()
997
+ if config.fp16:
998
+ self.transformer.half()
999
+ self.lm_head.half()
1000
+ self.post_init()
1001
+
1002
+ def get_output_embeddings(self):
1003
+ return self.lm_head
1004
+
1005
+ def set_output_embeddings(self, new_embeddings):
1006
+ self.lm_head = new_embeddings
1007
+
1008
+ def prepare_inputs_for_generation(
1009
+ self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs
1010
+ ):
1011
+ if past_key_values:
1012
+ input_ids = input_ids[:, -1].unsqueeze(-1)
1013
+
1014
+ if input_ids.size(0) == 1:
1015
+ attention_mask = None
1016
+ else:
1017
+ attention_mask = kwargs.get("attention_mask", None)
1018
+
1019
+ if inputs_embeds is not None and past_key_values is None:
1020
+ model_inputs = {"inputs_embeds": inputs_embeds}
1021
+ else:
1022
+ model_inputs = {"input_ids": input_ids}
1023
+
1024
+ model_inputs.update(
1025
+ {
1026
+ "past_key_values": past_key_values,
1027
+ "use_cache": kwargs.get("use_cache"),
1028
+ "attention_mask": attention_mask,
1029
+ }
1030
+ )
1031
+ return model_inputs
1032
+
1033
+ def forward(
1034
+ self,
1035
+ input_ids: Optional[torch.LongTensor] = None,
1036
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
1037
+ attention_mask: Optional[torch.FloatTensor] = None,
1038
+ token_type_ids: Optional[torch.LongTensor] = None,
1039
+ position_ids: Optional[torch.LongTensor] = None,
1040
+ head_mask: Optional[torch.FloatTensor] = None,
1041
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1042
+ encoder_hidden_states: Optional[torch.Tensor] = None,
1043
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
1044
+ labels: Optional[torch.LongTensor] = None,
1045
+ use_cache: Optional[bool] = None,
1046
+ output_attentions: Optional[bool] = None,
1047
+ output_hidden_states: Optional[bool] = None,
1048
+ return_dict: Optional[bool] = None,
1049
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
1050
+
1051
+ return_dict = (
1052
+ return_dict if return_dict is not None else self.config.use_return_dict
1053
+ )
1054
+
1055
+ transformer_outputs = self.transformer(
1056
+ input_ids,
1057
+ past_key_values=past_key_values,
1058
+ attention_mask=attention_mask,
1059
+ token_type_ids=token_type_ids,
1060
+ position_ids=position_ids,
1061
+ head_mask=head_mask,
1062
+ inputs_embeds=inputs_embeds,
1063
+ encoder_hidden_states=encoder_hidden_states,
1064
+ encoder_attention_mask=encoder_attention_mask,
1065
+ use_cache=use_cache,
1066
+ output_attentions=output_attentions,
1067
+ output_hidden_states=output_hidden_states,
1068
+ return_dict=return_dict,
1069
+ )
1070
+ hidden_states = transformer_outputs[0]
1071
+
1072
+ lm_logits = self.lm_head(hidden_states)
1073
+
1074
+ loss = None
1075
+ if labels is not None:
1076
+ labels = labels.to(lm_logits.device)
1077
+ shift_logits = lm_logits[..., :-1, :].contiguous()
1078
+ shift_labels = labels[..., 1:].contiguous()
1079
+ loss_fct = CrossEntropyLoss()
1080
+ loss = loss_fct(
1081
+ shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1)
1082
+ )
1083
+
1084
+ if not return_dict:
1085
+ output = (lm_logits,) + transformer_outputs[1:]
1086
+ return ((loss,) + output) if loss is not None else output
1087
+
1088
+ return CausalLMOutputWithPast(
1089
+ loss=loss,
1090
+ logits=lm_logits,
1091
+ past_key_values=transformer_outputs.past_key_values,
1092
+ hidden_states=transformer_outputs.hidden_states,
1093
+ attentions=transformer_outputs.attentions,
1094
+ )
1095
+
1096
+ @staticmethod
1097
+ def _reorder_cache(
1098
+ past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor
1099
+ ) -> Tuple[Tuple[torch.Tensor]]:
1100
+
1101
+ return tuple(
1102
+ tuple(
1103
+ past_state.index_select(0, beam_idx.to(past_state.device))
1104
+ for past_state in layer_past
1105
+ )
1106
+ for layer_past in past_key_values
1107
+ )
1108
+
1109
+ def chat(
1110
+ self,
1111
+ tokenizer: PreTrainedTokenizer,
1112
+ query: str,
1113
+ history: Optional[HistoryType],
1114
+ system: str = "You are a helpful assistant.",
1115
+ stream: Optional[bool] = _SENTINEL,
1116
+ stop_words_ids: Optional[List[List[int]]] = None,
1117
+ generation_config: Optional[GenerationConfig] = None,
1118
+ **kwargs,
1119
+ ) -> Tuple[str, HistoryType]:
1120
+ generation_config = generation_config if generation_config is not None else self.generation_config
1121
+
1122
+ assert stream is _SENTINEL, _ERROR_STREAM_IN_CHAT
1123
+ assert generation_config.chat_format == 'chatml', _ERROR_BAD_CHAT_FORMAT
1124
+ if history is None:
1125
+ history = []
1126
+ else:
1127
+ # make a copy of the user's input such that is is left untouched
1128
+ history = copy.deepcopy(history)
1129
+
1130
+ if stop_words_ids is None:
1131
+ stop_words_ids = []
1132
+
1133
+ max_window_size = kwargs.get('max_window_size', None)
1134
+ if max_window_size is None:
1135
+ max_window_size = generation_config.max_window_size
1136
+ raw_text, context_tokens = make_context(
1137
+ tokenizer,
1138
+ query,
1139
+ history=history,
1140
+ system=system,
1141
+ max_window_size=max_window_size,
1142
+ chat_format=generation_config.chat_format,
1143
+ )
1144
+
1145
+ stop_words_ids.extend(get_stop_words_ids(
1146
+ generation_config.chat_format, tokenizer
1147
+ ))
1148
+ input_ids = torch.tensor([context_tokens]).to(self.device)
1149
+ outputs = self.generate(
1150
+ input_ids,
1151
+ stop_words_ids=stop_words_ids,
1152
+ return_dict_in_generate=False,
1153
+ generation_config=generation_config,
1154
+ **kwargs,
1155
+ )
1156
+
1157
+ response = decode_tokens(
1158
+ outputs[0],
1159
+ tokenizer,
1160
+ raw_text_len=len(raw_text),
1161
+ context_length=len(context_tokens),
1162
+ chat_format=generation_config.chat_format,
1163
+ verbose=False,
1164
+ errors='replace'
1165
+ )
1166
+
1167
+ # as history is a copy of the user inputs,
1168
+ # we can always return the new turn to the user.
1169
+ # separating input history and output history also enables the user
1170
+ # to implement more complex history management
1171
+ history.append((query, response))
1172
+
1173
+ return response, history
1174
+
1175
+ def chat_stream(
1176
+ self,
1177
+ tokenizer: PreTrainedTokenizer,
1178
+ query: str,
1179
+ history: Optional[HistoryType],
1180
+ system: str = "You are a helpful assistant.",
1181
+ stop_words_ids: Optional[List[List[int]]] = None,
1182
+ logits_processor: Optional[LogitsProcessorList] = None,
1183
+ generation_config: Optional[GenerationConfig] = None,
1184
+ **kwargs,
1185
+ ) -> Generator[str, Any, None]:
1186
+ generation_config = generation_config if generation_config is not None else self.generation_config
1187
+ assert generation_config.chat_format == 'chatml', _ERROR_BAD_CHAT_FORMAT
1188
+ if history is None:
1189
+ history = []
1190
+ if stop_words_ids is None:
1191
+ stop_words_ids = []
1192
+
1193
+ max_window_size = kwargs.get('max_window_size', None)
1194
+ if max_window_size is None:
1195
+ max_window_size = generation_config.max_window_size
1196
+ raw_text, context_tokens = make_context(
1197
+ tokenizer,
1198
+ query,
1199
+ history=history,
1200
+ system=system,
1201
+ max_window_size=max_window_size,
1202
+ chat_format=generation_config.chat_format,
1203
+ )
1204
+
1205
+ stop_words_ids.extend(get_stop_words_ids(
1206
+ generation_config.chat_format, tokenizer
1207
+ ))
1208
+ if stop_words_ids is not None:
1209
+ stop_words_logits_processor = StopWordsLogitsProcessor(
1210
+ stop_words_ids=stop_words_ids,
1211
+ eos_token_id=generation_config.eos_token_id,
1212
+ )
1213
+ if logits_processor is None:
1214
+ logits_processor = LogitsProcessorList([stop_words_logits_processor])
1215
+ else:
1216
+ logits_processor.append(stop_words_logits_processor)
1217
+ input_ids = torch.tensor([context_tokens]).to(self.device)
1218
+
1219
+ from transformers_stream_generator.main import NewGenerationMixin, StreamGenerationConfig
1220
+ self.__class__.generate_stream = NewGenerationMixin.generate
1221
+ self.__class__.sample_stream = NewGenerationMixin.sample_stream
1222
+ stream_config = StreamGenerationConfig(**generation_config.to_dict(), do_stream=True)
1223
+
1224
+ def stream_generator():
1225
+ outputs = []
1226
+ for token in self.generate_stream(
1227
+ input_ids,
1228
+ return_dict_in_generate=False,
1229
+ generation_config=stream_config,
1230
+ logits_processor=logits_processor,
1231
+ seed=-1,
1232
+ **kwargs):
1233
+ outputs.append(token.item())
1234
+ yield tokenizer.decode(outputs, skip_special_tokens=True, errors='ignore')
1235
+
1236
+ return stream_generator()
1237
+
1238
+ def generate(
1239
+ self,
1240
+ inputs: Optional[torch.Tensor] = None,
1241
+ generation_config: Optional[GenerationConfig] = None,
1242
+ logits_processor: Optional[LogitsProcessorList] = None,
1243
+ stopping_criteria: Optional[StoppingCriteriaList] = None,
1244
+ prefix_allowed_tokens_fn: Optional[
1245
+ Callable[[int, torch.Tensor], List[int]]
1246
+ ] = None,
1247
+ synced_gpus: Optional[bool] = None,
1248
+ assistant_model: Optional["PreTrainedModel"] = None,
1249
+ streamer: Optional["BaseStreamer"] = None,
1250
+ **kwargs,
1251
+ ) -> Union[GenerateOutput, torch.LongTensor]:
1252
+ generation_config = generation_config if generation_config is not None else self.generation_config
1253
+
1254
+ # Process stop_words_ids.
1255
+ stop_words_ids = kwargs.pop("stop_words_ids", None)
1256
+ if stop_words_ids is None and generation_config is not None:
1257
+ stop_words_ids = getattr(generation_config, "stop_words_ids", None)
1258
+ if stop_words_ids is None:
1259
+ stop_words_ids = getattr(generation_config, "stop_words_ids", None)
1260
+
1261
+ if stop_words_ids is not None:
1262
+ stop_words_logits_processor = StopWordsLogitsProcessor(
1263
+ stop_words_ids=stop_words_ids,
1264
+ eos_token_id=generation_config.eos_token_id,
1265
+ )
1266
+ if logits_processor is None:
1267
+ logits_processor = LogitsProcessorList([stop_words_logits_processor])
1268
+ else:
1269
+ logits_processor.append(stop_words_logits_processor)
1270
+
1271
+ return super().generate(
1272
+ inputs,
1273
+ generation_config=generation_config,
1274
+ logits_processor=logits_processor,
1275
+ stopping_criteria=stopping_criteria,
1276
+ prefix_allowed_tokens_fn=prefix_allowed_tokens_fn,
1277
+ synced_gpus=synced_gpus,
1278
+ assistant_model=assistant_model,
1279
+ streamer=streamer,
1280
+ **kwargs,
1281
+ )
1282
+
1283
+
1284
+ class RotaryEmbedding(torch.nn.Module):
1285
+ def __init__(self, dim, base=10000):
1286
+ super().__init__()
1287
+ self.dim = dim
1288
+ self.base = base
1289
+ inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
1290
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
1291
+ if importlib.util.find_spec("einops") is None:
1292
+ raise RuntimeError("einops is required for Rotary Embedding")
1293
+
1294
+ self._rotary_pos_emb_cache = None
1295
+ self._seq_len_cached = 0
1296
+ self._ntk_alpha_cached = 1.0
1297
+ self._ntk_alpha_cached_list = [1.0]
1298
+
1299
+ def update_rotary_pos_emb_cache(self, seqlen, ntk_alpha=1.0):
1300
+ if seqlen > self._seq_len_cached or ntk_alpha != self._ntk_alpha_cached:
1301
+ base = self.base * ntk_alpha ** (self.dim / (self.dim - 2))
1302
+ self.inv_freq = 1.0 / (
1303
+ base
1304
+ ** (
1305
+ torch.arange(0, self.dim, 2, device=self.inv_freq.device).float()
1306
+ / self.dim
1307
+ )
1308
+ )
1309
+ self._seq_len_cached = max(2 * seqlen, 16)
1310
+ self._ntk_alpha_cached = ntk_alpha
1311
+ seq = torch.arange(self._seq_len_cached, device=self.inv_freq.device)
1312
+ freqs = torch.outer(seq.type_as(self.inv_freq), self.inv_freq)
1313
+
1314
+ emb = torch.cat((freqs, freqs), dim=-1)
1315
+ from einops import rearrange
1316
+
1317
+ emb = rearrange(emb, "n d -> 1 n 1 d")
1318
+
1319
+ cos, sin = emb.cos(), emb.sin()
1320
+ self._rotary_pos_emb_cache = [cos, sin]
1321
+
1322
+ def forward(self, max_seq_len, ntk_alpha=1.0):
1323
+ self.update_rotary_pos_emb_cache(max_seq_len, ntk_alpha)
1324
+ cos, sin = self._rotary_pos_emb_cache
1325
+ return [cos[:, :max_seq_len], sin[:, :max_seq_len]]
1326
+
1327
+
1328
+ def _rotate_half(x):
1329
+ from einops import rearrange
1330
+
1331
+ x = rearrange(x, "... (j d) -> ... j d", j=2)
1332
+ x1, x2 = x.unbind(dim=-2)
1333
+ return torch.cat((-x2, x1), dim=-1)
1334
+
1335
+
1336
+ def apply_rotary_pos_emb(t, freqs):
1337
+ """ Apply rotary embedding to the first rotary_dim of the iput
1338
+
1339
+ Arguments:
1340
+ t (tensor(batch_size, seq_len, n_head, head_dim)):
1341
+ the input embedding/hidden states
1342
+ freqs (list[tensor(1, seq_len, 1, rotary_dim), tensor(1, seq_len, 1, rotary_dim)]):
1343
+ the cached cos/sin position embeddings
1344
+ """
1345
+ rot_dim = freqs[0].shape[-1]
1346
+ cos, sin = freqs
1347
+ t_float = t.float()
1348
+ if apply_rotary_emb_func is not None and t.is_cuda:
1349
+ # apply_rotary_emb in flash_attn requires cos/sin to be of
1350
+ # shape (seqlen, rotary_dim / 2) and apply rotary embedding
1351
+ # to the first rotary_dim of the input
1352
+ cos = cos.squeeze(0).squeeze(1)[:, : rot_dim // 2]
1353
+ sin = sin.squeeze(0).squeeze(1)[:, : rot_dim // 2]
1354
+ return apply_rotary_emb_func(t_float, cos, sin).type_as(t)
1355
+ else:
1356
+ t_rot, t_pass = t_float[..., :rot_dim], t_float[..., rot_dim:]
1357
+ t_rot = (t_rot * cos) + (_rotate_half(t_rot) * sin)
1358
+ return torch.cat((t_rot, t_pass), dim=-1).type_as(t)
1359
+
1360
+
1361
+ class RMSNorm(torch.nn.Module):
1362
+ def __init__(self, dim: int, eps: float = 1e-6):
1363
+ super().__init__()
1364
+ self.eps = eps
1365
+ self.weight = nn.Parameter(torch.ones(dim))
1366
+
1367
+ def _norm(self, x):
1368
+ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
1369
+
1370
+ def forward(self, x):
1371
+ if rms_norm is not None and x.is_cuda:
1372
+ return rms_norm(x, self.weight, self.eps)
1373
+ else:
1374
+ output = self._norm(x.float()).type_as(x)
1375
+ return output * self.weight
qwen_generation_utils.py ADDED
@@ -0,0 +1,416 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """Generation support."""
7
+
8
+ from typing import Tuple, List, Union, Iterable
9
+
10
+ import numpy as np
11
+ import torch
12
+ import torch.nn.functional as F
13
+ from transformers import PreTrainedTokenizer
14
+ from transformers import logging
15
+ from transformers.generation import LogitsProcessor
16
+
17
+ logger = logging.get_logger(__name__)
18
+
19
+ # Types.
20
+ HistoryType = List[Tuple[str, str]]
21
+ TokensType = List[int]
22
+ BatchTokensType = List[List[int]]
23
+
24
+
25
+ def pad_batch(batch: BatchTokensType, pad_id: int, seq_length: int) -> BatchTokensType:
26
+ for tokens in batch:
27
+ context_length = len(tokens)
28
+ if context_length < seq_length:
29
+ tokens.extend([pad_id] * (seq_length - context_length))
30
+ return batch
31
+
32
+
33
+ def get_ltor_masks_and_position_ids(
34
+ data,
35
+ eod_token,
36
+ reset_position_ids,
37
+ reset_attention_mask,
38
+ eod_mask_loss,
39
+ ):
40
+ """Build masks and position id for left to right model."""
41
+
42
+ # Extract batch size and sequence length.
43
+ micro_batch_size, seq_length = data.size()
44
+
45
+ # Attention mask (lower triangular).
46
+ if reset_attention_mask:
47
+ att_mask_batch = micro_batch_size
48
+ else:
49
+ att_mask_batch = 1
50
+ attention_mask = torch.tril(
51
+ torch.ones((att_mask_batch, seq_length, seq_length), device=data.device)
52
+ ).view(att_mask_batch, 1, seq_length, seq_length)
53
+
54
+ # Loss mask.
55
+ loss_mask = torch.ones(data.size(), dtype=torch.float, device=data.device)
56
+ if eod_mask_loss:
57
+ loss_mask[data == eod_token] = 0.0
58
+
59
+ # Position ids.
60
+ position_ids = torch.arange(seq_length, dtype=torch.long, device=data.device)
61
+ position_ids = position_ids.unsqueeze(0).expand_as(data)
62
+ # We need to clone as the ids will be modifed based on batch index.
63
+ if reset_position_ids:
64
+ position_ids = position_ids.clone()
65
+
66
+ if reset_position_ids or reset_attention_mask:
67
+ # Loop through the batches:
68
+ for b in range(micro_batch_size):
69
+
70
+ # Find indecies where EOD token is.
71
+ eod_index = position_ids[b, data[b] == eod_token]
72
+ # Detach indecies from positions if going to modify positions.
73
+ if reset_position_ids:
74
+ eod_index = eod_index.clone()
75
+
76
+ # Loop through EOD indecies:
77
+ prev_index = 0
78
+ for j in range(eod_index.size()[0]):
79
+ i = eod_index[j]
80
+ # Mask attention loss.
81
+ if reset_attention_mask:
82
+ attention_mask[b, 0, (i + 1) :, : (i + 1)] = 0
83
+ # Reset positions.
84
+ if reset_position_ids:
85
+ position_ids[b, (i + 1) :] -= i + 1 - prev_index
86
+ prev_index = i + 1
87
+
88
+ # Convert attention mask to binary:
89
+ attention_mask = attention_mask < 0.5
90
+
91
+ return attention_mask, loss_mask, position_ids
92
+
93
+
94
+ def get_batch(context_tokens: torch.LongTensor, eod_id: int):
95
+ """Generate batch from context tokens."""
96
+ # Move to GPU.
97
+ tokens = context_tokens.contiguous().to(context_tokens.device)
98
+ # Get the attention mask and postition ids.
99
+ attention_mask, _, position_ids = get_ltor_masks_and_position_ids(
100
+ tokens,
101
+ eod_id,
102
+ reset_position_ids=False,
103
+ reset_attention_mask=False,
104
+ eod_mask_loss=False,
105
+ )
106
+ return tokens, attention_mask, position_ids
107
+
108
+
109
+ def get_stop_words_ids(chat_format, tokenizer):
110
+ if chat_format == "raw":
111
+ stop_words_ids = [tokenizer.encode("Human:"), [tokenizer.eod_id]]
112
+ elif chat_format == "chatml":
113
+ stop_words_ids = [[tokenizer.im_end_id], [tokenizer.im_start_id]]
114
+ else:
115
+ raise NotImplementedError(f"Unknown chat format {chat_format!r}")
116
+ return stop_words_ids
117
+
118
+
119
+ def make_context(
120
+ tokenizer: PreTrainedTokenizer,
121
+ query: str,
122
+ history: List[Tuple[str, str]] = None,
123
+ system: str = "",
124
+ max_window_size: int = 6144,
125
+ chat_format: str = "chatml",
126
+ ):
127
+ if history is None:
128
+ history = []
129
+
130
+ if chat_format == "chatml":
131
+ im_start, im_end = "<|im_start|>", "<|im_end|>"
132
+ im_start_tokens = [tokenizer.im_start_id]
133
+ im_end_tokens = [tokenizer.im_end_id]
134
+ nl_tokens = tokenizer.encode("\n")
135
+
136
+ def _tokenize_str(role, content):
137
+ return f"{role}\n{content}", tokenizer.encode(
138
+ role, allowed_special=set()
139
+ ) + nl_tokens + tokenizer.encode(content, allowed_special=set())
140
+
141
+ system_text, system_tokens_part = _tokenize_str("system", system)
142
+ system_tokens = im_start_tokens + system_tokens_part + im_end_tokens
143
+
144
+ raw_text = ""
145
+ context_tokens = []
146
+
147
+ for turn_query, turn_response in reversed(history):
148
+ query_text, query_tokens_part = _tokenize_str("user", turn_query)
149
+ query_tokens = im_start_tokens + query_tokens_part + im_end_tokens
150
+ response_text, response_tokens_part = _tokenize_str(
151
+ "assistant", turn_response
152
+ )
153
+ response_tokens = im_start_tokens + response_tokens_part + im_end_tokens
154
+
155
+ next_context_tokens = nl_tokens + query_tokens + nl_tokens + response_tokens
156
+ prev_chat = (
157
+ f"\n{im_start}{query_text}{im_end}\n{im_start}{response_text}{im_end}"
158
+ )
159
+
160
+ current_context_size = (
161
+ len(system_tokens) + len(next_context_tokens) + len(context_tokens)
162
+ )
163
+ if current_context_size < max_window_size:
164
+ context_tokens = next_context_tokens + context_tokens
165
+ raw_text = prev_chat + raw_text
166
+ else:
167
+ break
168
+
169
+ context_tokens = system_tokens + context_tokens
170
+ raw_text = f"{im_start}{system_text}{im_end}" + raw_text
171
+ context_tokens += (
172
+ nl_tokens
173
+ + im_start_tokens
174
+ + _tokenize_str("user", query)[1]
175
+ + im_end_tokens
176
+ + nl_tokens
177
+ + im_start_tokens
178
+ + tokenizer.encode("assistant")
179
+ + nl_tokens
180
+ )
181
+ raw_text += f"\n{im_start}user\n{query}{im_end}\n{im_start}assistant\n"
182
+
183
+ elif chat_format == "raw":
184
+ raw_text = query
185
+ context_tokens = tokenizer.encode(raw_text)
186
+ else:
187
+ raise NotImplementedError(f"Unknown chat format {chat_format!r}")
188
+
189
+ return raw_text, context_tokens
190
+
191
+
192
+ def _decode_default(
193
+ tokens: List[int],
194
+ *,
195
+ stop_words: List[str],
196
+ eod_words: List[str],
197
+ tokenizer: PreTrainedTokenizer,
198
+ raw_text_len: int,
199
+ verbose: bool = False,
200
+ return_end_reason: bool = False,
201
+ errors: str='replace',
202
+ ):
203
+ trim_decode_tokens = tokenizer.decode(tokens, errors=errors)[raw_text_len:]
204
+ if verbose:
205
+ print("\nRaw Generate: ", trim_decode_tokens)
206
+
207
+ end_reason = f"Gen length {len(tokens)}"
208
+ for stop_word in stop_words:
209
+ trim_decode_tokens = trim_decode_tokens.replace(stop_word, "").strip()
210
+ for eod_word in eod_words:
211
+ if eod_word in trim_decode_tokens:
212
+ end_reason = f"Gen {eod_word!r}"
213
+ trim_decode_tokens = trim_decode_tokens.split(eod_word)[0]
214
+ trim_decode_tokens = trim_decode_tokens.strip()
215
+ if verbose:
216
+ print("\nEnd Reason:", end_reason)
217
+ print("\nGenerate: ", trim_decode_tokens)
218
+
219
+ if return_end_reason:
220
+ return trim_decode_tokens, end_reason
221
+ else:
222
+ return trim_decode_tokens
223
+
224
+
225
+ def _decode_chatml(
226
+ tokens: List[int],
227
+ *,
228
+ stop_words: List[str],
229
+ eod_token_ids: List[int],
230
+ tokenizer: PreTrainedTokenizer,
231
+ raw_text_len: int,
232
+ context_length: int,
233
+ verbose: bool = False,
234
+ return_end_reason: bool = False,
235
+ errors: str='replace'
236
+ ):
237
+ end_reason = f"Gen length {len(tokens)}"
238
+ eod_token_idx = context_length
239
+ for eod_token_idx in range(context_length, len(tokens)):
240
+ if tokens[eod_token_idx] in eod_token_ids:
241
+ end_reason = f"Gen {tokenizer.decode([tokens[eod_token_idx]])!r}"
242
+ break
243
+
244
+ trim_decode_tokens = tokenizer.decode(tokens[:eod_token_idx], errors=errors)[raw_text_len:]
245
+ if verbose:
246
+ print("\nRaw Generate w/o EOD:", tokenizer.decode(tokens, errors=errors)[raw_text_len:])
247
+ print("\nRaw Generate:", trim_decode_tokens)
248
+ print("\nEnd Reason:", end_reason)
249
+ for stop_word in stop_words:
250
+ trim_decode_tokens = trim_decode_tokens.replace(stop_word, "").strip()
251
+ trim_decode_tokens = trim_decode_tokens.strip()
252
+ if verbose:
253
+ print("\nGenerate:", trim_decode_tokens)
254
+
255
+ if return_end_reason:
256
+ return trim_decode_tokens, end_reason
257
+ else:
258
+ return trim_decode_tokens
259
+
260
+
261
+ def decode_tokens(
262
+ tokens: Union[torch.LongTensor, TokensType],
263
+ tokenizer: PreTrainedTokenizer,
264
+ raw_text_len: int,
265
+ context_length: int,
266
+ chat_format: str,
267
+ verbose: bool = False,
268
+ return_end_reason: bool = False,
269
+ errors: str="replace",
270
+ ) -> str:
271
+ if torch.is_tensor(tokens):
272
+ tokens = tokens.cpu().numpy().tolist()
273
+
274
+ if chat_format == "chatml":
275
+ return _decode_chatml(
276
+ tokens,
277
+ stop_words=[],
278
+ eod_token_ids=[tokenizer.im_start_id, tokenizer.im_end_id],
279
+ tokenizer=tokenizer,
280
+ raw_text_len=raw_text_len,
281
+ context_length=context_length,
282
+ verbose=verbose,
283
+ return_end_reason=return_end_reason,
284
+ errors=errors,
285
+ )
286
+ elif chat_format == "raw":
287
+ return _decode_default(
288
+ tokens,
289
+ stop_words=["<|endoftext|>"],
290
+ eod_words=["<|endoftext|>"],
291
+ tokenizer=tokenizer,
292
+ raw_text_len=raw_text_len,
293
+ verbose=verbose,
294
+ return_end_reason=return_end_reason,
295
+ errors=errors,
296
+ )
297
+ else:
298
+ raise NotImplementedError(f"Unknown chat format {chat_format!r}")
299
+
300
+
301
+ class StopWordsLogitsProcessor(LogitsProcessor):
302
+ """
303
+ :class:`transformers.LogitsProcessor` that enforces that when specified sequences appear, stop geration.
304
+
305
+ Args:
306
+ stop_words_ids (:obj:`List[List[int]]`):
307
+ List of list of token ids of stop ids. In order to get the tokens of the words
308
+ that should not appear in the generated text, use :obj:`tokenizer(bad_word,
309
+ add_prefix_space=True).input_ids`.
310
+ eos_token_id (:obj:`int`):
311
+ The id of the `end-of-sequence` token.
312
+ """
313
+
314
+ def __init__(self, stop_words_ids: Iterable[Iterable[int]], eos_token_id: int):
315
+
316
+ if not isinstance(stop_words_ids, List) or len(stop_words_ids) == 0:
317
+ raise ValueError(
318
+ f"`stop_words_ids` has to be a non-emtpy list, but is {stop_words_ids}."
319
+ )
320
+ if any(not isinstance(bad_word_ids, list) for bad_word_ids in stop_words_ids):
321
+ raise ValueError(
322
+ f"`stop_words_ids` has to be a list of lists, but is {stop_words_ids}."
323
+ )
324
+ if any(
325
+ any(
326
+ (not isinstance(token_id, (int, np.integer)) or token_id < 0)
327
+ for token_id in stop_word_ids
328
+ )
329
+ for stop_word_ids in stop_words_ids
330
+ ):
331
+ raise ValueError(
332
+ f"Each list in `stop_words_ids` has to be a list of positive integers, but is {stop_words_ids}."
333
+ )
334
+
335
+ self.stop_words_ids = list(
336
+ filter(
337
+ lambda bad_token_seq: bad_token_seq != [eos_token_id], stop_words_ids
338
+ )
339
+ )
340
+ self.eos_token_id = eos_token_id
341
+ for stop_token_seq in self.stop_words_ids:
342
+ assert (
343
+ len(stop_token_seq) > 0
344
+ ), "Stop words token sequences {} cannot have an empty list".format(
345
+ stop_words_ids
346
+ )
347
+
348
+ def __call__(
349
+ self, input_ids: torch.LongTensor, scores: torch.FloatTensor
350
+ ) -> torch.FloatTensor:
351
+ stopped_samples = self._calc_stopped_samples(input_ids)
352
+ for i, should_stop in enumerate(stopped_samples):
353
+ if should_stop:
354
+ scores[i, self.eos_token_id] = float(2**15)
355
+ return scores
356
+
357
+ def _tokens_match(self, prev_tokens: torch.LongTensor, tokens: List[int]) -> bool:
358
+ if len(tokens) == 0:
359
+ # if bad word tokens is just one token always ban it
360
+ return True
361
+ elif len(tokens) > len(prev_tokens):
362
+ # if bad word tokens are longer then prev input_ids they can't be equal
363
+ return False
364
+ elif prev_tokens[-len(tokens) :].tolist() == tokens:
365
+ # if tokens match
366
+ return True
367
+ else:
368
+ return False
369
+
370
+ def _calc_stopped_samples(self, prev_input_ids: Iterable[int]) -> Iterable[int]:
371
+ stopped_samples = []
372
+ for prev_input_ids_slice in prev_input_ids:
373
+ match = False
374
+ for stop_token_seq in self.stop_words_ids:
375
+ if self._tokens_match(prev_input_ids_slice, stop_token_seq):
376
+ # if tokens do not match continue
377
+ match = True
378
+ break
379
+ stopped_samples.append(match)
380
+
381
+ return stopped_samples
382
+
383
+
384
+ def top_k_logits(logits, top_k=0, top_p=0.0, filter_value=-float("Inf")):
385
+ """This function has been mostly taken from huggingface conversational
386
+ ai code at
387
+ https://medium.com/huggingface/how-to-build-a-state-of-the-art-
388
+ conversational-ai-with-transfer-learning-2d818ac26313"""
389
+
390
+ if top_k > 0:
391
+ # Remove all tokens with a probability less than the
392
+ # last token of the top-k
393
+ indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
394
+ logits[indices_to_remove] = filter_value
395
+
396
+ if top_p > 0.0:
397
+ # Cconvert to 1D
398
+ sorted_logits, sorted_indices = torch.sort(logits, descending=True, dim=-1)
399
+ cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
400
+
401
+ # Remove tokens with cumulative probability above the threshold
402
+ sorted_indices_to_remove = cumulative_probs > top_p
403
+ # Shift the indices to the right to keep also the first token
404
+ # above the threshold
405
+ sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
406
+ sorted_indices_to_remove[..., 0] = 0
407
+ for i in range(sorted_indices.size(0)):
408
+ indices_to_remove = sorted_indices[i][sorted_indices_to_remove[i]]
409
+ logits[i][indices_to_remove] = filter_value
410
+
411
+ return logits
412
+
413
+
414
+ def switch(val1, val2, boolean):
415
+ boolean = boolean.type_as(val1)
416
+ return (1 - boolean) * val1 + boolean * val2