Instructions to use zember/Qwen-14B-Chat-agent-function-calling with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use zember/Qwen-14B-Chat-agent-function-calling with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="zember/Qwen-14B-Chat-agent-function-calling", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("zember/Qwen-14B-Chat-agent-function-calling", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
zs commited on
Commit ·
64a58c9
1
Parent(s): 65dcc82
Add more
Browse filesFiles changed (40)
- config.jsonAdded+43
- configuration_qwen.pyAdded+71
- cpp_kernels.pyAdded+55
- generation_config.jsonAdded+12
- model-00001-of-00029.safetensorsAdded+3
- model-00002-of-00029.safetensorsAdded+3
- model-00003-of-00029.safetensorsAdded+3
- model-00004-of-00029.safetensorsAdded+3
- model-00005-of-00029.safetensorsAdded+3
- model-00006-of-00029.safetensorsAdded+3
- model-00007-of-00029.safetensorsAdded+3
- model-00008-of-00029.safetensorsAdded+3
- model-00009-of-00029.safetensorsAdded+3
- model-00010-of-00029.safetensorsAdded+3
- model-00011-of-00029.safetensorsAdded+3
- model-00012-of-00029.safetensorsAdded+3
- model-00013-of-00029.safetensorsAdded+3
- model-00014-of-00029.safetensorsAdded+3
- model-00015-of-00029.safetensorsAdded+3
- model-00016-of-00029.safetensorsAdded+3
- model-00017-of-00029.safetensorsAdded+3
- model-00018-of-00029.safetensorsAdded+3
- model-00019-of-00029.safetensorsAdded+3
- model-00020-of-00029.safetensorsAdded+3
- model-00021-of-00029.safetensorsAdded+3
- model-00022-of-00029.safetensorsAdded+3
- model-00023-of-00029.safetensorsAdded+3
- model-00024-of-00029.safetensorsAdded+3
- model-00025-of-00029.safetensorsAdded+3
- model-00026-of-00029.safetensorsAdded+3
- model-00027-of-00029.safetensorsAdded+3
- model-00028-of-00029.safetensorsAdded+3
- model-00029-of-00029.safetensorsAdded+3
- model.safetensors.index.jsonAdded+330
- modeling_qwen.pyAdded+1363
- qwen.tiktokenAdded
- qwen_generation_utils.pyAdded+416
- special_tokens_map.jsonAdded+1
- tokenization_qwen.pyAdded+276
- tokenizer_config.jsonAdded+14
12345678910111213141516171819202122232425262728293031323334353637383940414243{ "_name_or_path": "/NFS/models/Qwen/Qwen-14B-Chat", "architectures": [ "QWenLMHeadModel" ], "attn_dropout_prob": 0.0, "auto_map": { "AutoConfig": "configuration_qwen.QWenConfig", "AutoModel": "modeling_qwen.QWenLMHeadModel", "AutoModelForCausalLM": "modeling_qwen.QWenLMHeadModel" }, "bf16": false, "emb_dropout_prob": 0.0, "fp16": true, "fp32": false, "hidden_size": 5120, "initializer_range": 0.02, "intermediate_size": 27392, "kv_channels": 128, "layer_norm_epsilon": 1e-06, "max_position_embeddings": 8192, "model_type": "qwen", "no_bias": true, "num_attention_heads": 40, "num_hidden_layers": 40, "onnx_safe": null, "rotary_emb_base": 10000, "rotary_pct": 1.0, "scale_attn_weights": true, "seq_length": 2048, "softmax_in_fp32": false, "tie_word_embeddings": false, "tokenizer_class": "QWenTokenizer", "torch_dtype": "float16", "transformers_version": "4.36.2", "use_cache": true, "use_cache_kernel": false, "use_cache_quantization": false, "use_dynamic_ntk": true, "use_flash_attn": true, "use_logn_attn": true, "vocab_size": 152064}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071# Copyright (c) Alibaba Cloud.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
from transformers import PretrainedConfig
class QWenConfig(PretrainedConfig): model_type = "qwen" keys_to_ignore_at_inference = ["past_key_values"]
def __init__( self, vocab_size=151936, hidden_size=4096, num_hidden_layers=32, num_attention_heads=32, emb_dropout_prob=0.0, attn_dropout_prob=0.0, layer_norm_epsilon=1e-6, initializer_range=0.02, max_position_embeddings=8192, scale_attn_weights=True, use_cache=True, bf16=False, fp16=False, fp32=False, kv_channels=128, rotary_pct=1.0, rotary_emb_base=10000, use_dynamic_ntk=True, use_logn_attn=True, use_flash_attn="auto", intermediate_size=22016, no_bias=True, tie_word_embeddings=False, use_cache_quantization=False, use_cache_kernel=False, softmax_in_fp32=False, **kwargs, ): self.vocab_size = vocab_size self.hidden_size = hidden_size self.intermediate_size = intermediate_size self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads self.emb_dropout_prob = emb_dropout_prob self.attn_dropout_prob = attn_dropout_prob self.layer_norm_epsilon = layer_norm_epsilon self.initializer_range = initializer_range self.scale_attn_weights = scale_attn_weights self.use_cache = use_cache self.max_position_embeddings = max_position_embeddings self.bf16 = bf16 self.fp16 = fp16 self.fp32 = fp32 self.kv_channels = kv_channels self.rotary_pct = rotary_pct self.rotary_emb_base = rotary_emb_base self.use_dynamic_ntk = use_dynamic_ntk self.use_logn_attn = use_logn_attn self.use_flash_attn = use_flash_attn self.no_bias = no_bias self.use_cache_quantization = use_cache_quantization self.use_cache_kernel = use_cache_kernel self.softmax_in_fp32 = softmax_in_fp32 super().__init__( tie_word_embeddings=tie_word_embeddings, **kwargs )12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455from torch.utils import cpp_extensionimport pathlibimport osimport subprocess
def _get_cuda_bare_metal_version(cuda_dir): raw_output = subprocess.check_output([cuda_dir + "/bin/nvcc", "-V"], universal_newlines=True) output = raw_output.split() release_idx = output.index("release") + 1 release = output[release_idx].split(".") bare_metal_major = release[0] bare_metal_minor = release[1][0]
return raw_output, bare_metal_major, bare_metal_minor
def _create_build_dir(buildpath): try: os.mkdir(buildpath) except OSError: if not os.path.isdir(buildpath): print(f"Creation of the build directory {buildpath} failed")
# Check if cuda 11 is installed for compute capability 8.0cc_flag = []_, bare_metal_major, bare_metal_minor = _get_cuda_bare_metal_version(cpp_extension.CUDA_HOME)if int(bare_metal_major) >= 11: cc_flag.append('-gencode') cc_flag.append('arch=compute_80,code=sm_80') if int(bare_metal_minor) >= 7: cc_flag.append('-gencode') cc_flag.append('arch=compute_90,code=sm_90')
# Build pathsrcpath = pathlib.Path(__file__).parent.absolute()buildpath = srcpath / 'build'_create_build_dir(buildpath)
def _cpp_extention_load_helper(name, sources, extra_cuda_flags): return cpp_extension.load( name=name, sources=sources, build_directory=buildpath, extra_cflags=['-O3', ], extra_cuda_cflags=['-O3', '-gencode', 'arch=compute_70,code=sm_70', '--use_fast_math'] + extra_cuda_flags + cc_flag, verbose=1 )
extra_flags = []
cache_autogptq_cuda_256_sources = ["./cache_autogptq_cuda_256.cpp", "./cache_autogptq_cuda_kernel_256.cu"]cache_autogptq_cuda_256 = _cpp_extention_load_helper("cache_autogptq_cuda_256", cache_autogptq_cuda_256_sources, extra_flags)123456789101112{ "chat_format": "chatml", "do_sample": true, "eos_token_id": 151643, "max_new_tokens": 512, "max_window_size": 6144, "pad_token_id": 151643, "repetition_penalty": 1.1, "top_k": 0, "top_p": 0.8, "transformers_version": "4.36.2"}123version https://git-lfs.github.com/spec/v1oid sha256:5300a7195264e098075d6f543bab4d48c299d921c187a36eb79c6d86f31d8c06size 1557135496123version https://git-lfs.github.com/spec/v1oid sha256:6da8088bd1e2000338e4e7d419d76ea0c0a88a7fea6fc92328620fb1c5110d3csize 980522456123version https://git-lfs.github.com/spec/v1oid sha256:96c5e16507f9ea17e031982797e746add972ead13fbc11e982ec397d23e2b17esize 911013032123version https://git-lfs.github.com/spec/v1oid sha256:0ce3d5c2b3cd31e35a372835c9c29a4b08e8a0777feea17e40d7ca6ad5ee8c84size 980512120123version https://git-lfs.github.com/spec/v1oid sha256:0cd9da119283298332b1a16cb0559792e829fe09cfa108fc4b2c9ae7661853d0size 911013032123version https://git-lfs.github.com/spec/v1oid sha256:2706c5fdf740a583b54b36909002944e602cd00480dffc33612c953e0ad3c566size 980512120123version https://git-lfs.github.com/spec/v1oid sha256:50ab9498c936152dd585b3c31767d1fce364a18cc1de270efbd5d1da694b3694size 911013032123version https://git-lfs.github.com/spec/v1oid sha256:2ee6fda314efb33d481067c302aa902b2be6de781566d281e0faa940ff5480basize 980512128123version https://git-lfs.github.com/spec/v1oid sha256:1883393c1023db3011bb695a84b3799cf59caddfb583ff3f475ed3c6480748dcsize 911013040123version https://git-lfs.github.com/spec/v1oid sha256:88edd8828687729cf8a81c83f17ba6a407e366cd91e356d3993f6c6c6ce437eesize 980512136123version https://git-lfs.github.com/spec/v1oid sha256:b124ede88269b1fced37f042810f181489f22443119c8a664f5f474352c32578size 911013040123version https://git-lfs.github.com/spec/v1oid sha256:bd66e791b49fd61cb0afef12a5e0fbb5218157df6d66fd36186a66115b1631edsize 980512136123version https://git-lfs.github.com/spec/v1oid sha256:72fc42e78ce0431fe5f0e295249378a43d97e5762f64a63b2381847f11828ffasize 911013040123version https://git-lfs.github.com/spec/v1oid sha256:da624a87536bd2404a25e2dc3b33aff957bdb9e9a1e4736071feea3e3de3ca6bsize 980512136123version https://git-lfs.github.com/spec/v1oid sha256:847283ca82713a3f53f9fffe2a14bd5b2ba6698778e19481634a6731f044beecsize 911013040123version https://git-lfs.github.com/spec/v1oid sha256:93832231a996d7a27c503464e631dd7b27f3530b6c27788773811084179778bbsize 980512136123version https://git-lfs.github.com/spec/v1oid sha256:f670d3345e9c255b60705b7809be8aa0e6d0d78b1ed26c95bb297681748de3fcsize 911013040123version https://git-lfs.github.com/spec/v1oid sha256:05c846a4b585b23497a2aec8c09c9a0a4563b464f79f5b3416832ad0f49cefd2size 980512136123version https://git-lfs.github.com/spec/v1oid sha256:ee1cf62a7aaa25abb4be1685829aa4ead6dd5a956d8750b77523db2ceb3a17ccsize 911013040123version https://git-lfs.github.com/spec/v1oid sha256:ab1347cb6654eaaee9b9e1d4a4ea630b9fb1f5e3ce0344d39cb6d1a10ad71756size 980512136123version https://git-lfs.github.com/spec/v1oid sha256:38810b9cd3cf1ea538cc37c23bfaff7e12ea7969b7156d46b94fe93a67f26e00size 911013040123version https://git-lfs.github.com/spec/v1oid sha256:cc280f1ebf8de53a5ac5a17609ed377242733f7373231b0fc3a2e725ba8113e2size 980512136123version https://git-lfs.github.com/spec/v1oid sha256:f28e4723a233bffcb53ce04ed73de2d645b43eae3f58e6ee16cfab502864af90size 911013040123version https://git-lfs.github.com/spec/v1oid sha256:1726576b727f094ef91bb135b3321785bee86ace9a99c877c20815d5f9973a2dsize 980512136123version https://git-lfs.github.com/spec/v1oid sha256:274f27d2fbc937001e44a1333646a2c27a076234e0b2eaf8edc156a8f36bbad6size 911013040123version https://git-lfs.github.com/spec/v1oid sha256:b46ee875f254d9cf9d685efb0933b0445ff3ad4ef07a5d5d0bf22011b8a102b8size 980512136123version https://git-lfs.github.com/spec/v1oid sha256:686fb4bd1c4cd89ff2b871f38f4148d2c7777cf3cc1bd9aa87c7853c78a19484size 911013040123version https://git-lfs.github.com/spec/v1oid sha256:73db275c08df24d5f56faca32de9b211877fd95fdadb91718818dc8b0b2c78a6size 630508392123version https://git-lfs.github.com/spec/v1oid sha256:86f46b278cb1bd33b7559bd7d3a33a29201234368dfb22462461657fbe20f421size 1557135488123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330{ "metadata": { "total_size": 28334581760 }, "weight_map": { "lm_head.weight": "model-00029-of-00029.safetensors", "transformer.h.0.attn.c_attn.bias": "model-00002-of-00029.safetensors", "transformer.h.0.attn.c_attn.weight": "model-00002-of-00029.safetensors", "transformer.h.0.attn.c_proj.weight": "model-00002-of-00029.safetensors", "transformer.h.0.ln_1.weight": "model-00002-of-00029.safetensors", "transformer.h.0.ln_2.weight": "model-00002-of-00029.safetensors", "transformer.h.0.mlp.c_proj.weight": "model-00002-of-00029.safetensors", "transformer.h.0.mlp.w1.weight": "model-00002-of-00029.safetensors", "transformer.h.0.mlp.w2.weight": "model-00002-of-00029.safetensors", "transformer.h.1.attn.c_attn.bias": "model-00002-of-00029.safetensors", "transformer.h.1.attn.c_attn.weight": "model-00002-of-00029.safetensors", "transformer.h.1.attn.c_proj.weight": "model-00002-of-00029.safetensors", "transformer.h.1.ln_1.weight": "model-00002-of-00029.safetensors", "transformer.h.1.ln_2.weight": "model-00002-of-00029.safetensors", "transformer.h.1.mlp.c_proj.weight": "model-00003-of-00029.safetensors", "transformer.h.1.mlp.w1.weight": "model-00002-of-00029.safetensors", "transformer.h.1.mlp.w2.weight": "model-00003-of-00029.safetensors", "transformer.h.10.attn.c_attn.bias": "model-00008-of-00029.safetensors", "transformer.h.10.attn.c_attn.weight": "model-00008-of-00029.safetensors", "transformer.h.10.attn.c_proj.weight": "model-00008-of-00029.safetensors", "transformer.h.10.ln_1.weight": "model-00008-of-00029.safetensors", "transformer.h.10.ln_2.weight": "model-00008-of-00029.safetensors", "transformer.h.10.mlp.c_proj.weight": "model-00009-of-00029.safetensors", "transformer.h.10.mlp.w1.weight": "model-00008-of-00029.safetensors", "transformer.h.10.mlp.w2.weight": "model-00009-of-00029.safetensors", "transformer.h.11.attn.c_attn.bias": "model-00009-of-00029.safetensors", "transformer.h.11.attn.c_attn.weight": "model-00009-of-00029.safetensors", "transformer.h.11.attn.c_proj.weight": "model-00009-of-00029.safetensors", "transformer.h.11.ln_1.weight": "model-00009-of-00029.safetensors", "transformer.h.11.ln_2.weight": "model-00009-of-00029.safetensors", "transformer.h.11.mlp.c_proj.weight": "model-00009-of-00029.safetensors", "transformer.h.11.mlp.w1.weight": "model-00009-of-00029.safetensors", "transformer.h.11.mlp.w2.weight": "model-00009-of-00029.safetensors", "transformer.h.12.attn.c_attn.bias": "model-00010-of-00029.safetensors", "transformer.h.12.attn.c_attn.weight": "model-00010-of-00029.safetensors", "transformer.h.12.attn.c_proj.weight": "model-00010-of-00029.safetensors", "transformer.h.12.ln_1.weight": "model-00009-of-00029.safetensors", "transformer.h.12.ln_2.weight": "model-00010-of-00029.safetensors", "transformer.h.12.mlp.c_proj.weight": "model-00010-of-00029.safetensors", "transformer.h.12.mlp.w1.weight": "model-00010-of-00029.safetensors", "transformer.h.12.mlp.w2.weight": "model-00010-of-00029.safetensors", "transformer.h.13.attn.c_attn.bias": "model-00010-of-00029.safetensors", "transformer.h.13.attn.c_attn.weight": "model-00010-of-00029.safetensors", "transformer.h.13.attn.c_proj.weight": "model-00010-of-00029.safetensors", "transformer.h.13.ln_1.weight": "model-00010-of-00029.safetensors", "transformer.h.13.ln_2.weight": "model-00010-of-00029.safetensors", "transformer.h.13.mlp.c_proj.weight": "model-00011-of-00029.safetensors", "transformer.h.13.mlp.w1.weight": "model-00010-of-00029.safetensors", "transformer.h.13.mlp.w2.weight": "model-00011-of-00029.safetensors", "transformer.h.14.attn.c_attn.bias": "model-00011-of-00029.safetensors", "transformer.h.14.attn.c_attn.weight": "model-00011-of-00029.safetensors", "transformer.h.14.attn.c_proj.weight": "model-00011-of-00029.safetensors", "transformer.h.14.ln_1.weight": "model-00011-of-00029.safetensors", "transformer.h.14.ln_2.weight": "model-00011-of-00029.safetensors", "transformer.h.14.mlp.c_proj.weight": "model-00011-of-00029.safetensors", "transformer.h.14.mlp.w1.weight": "model-00011-of-00029.safetensors", "transformer.h.14.mlp.w2.weight": "model-00011-of-00029.safetensors", "transformer.h.15.attn.c_attn.bias": "model-00012-of-00029.safetensors", "transformer.h.15.attn.c_attn.weight": "model-00012-of-00029.safetensors", "transformer.h.15.attn.c_proj.weight": "model-00012-of-00029.safetensors", "transformer.h.15.ln_1.weight": "model-00011-of-00029.safetensors", "transformer.h.15.ln_2.weight": "model-00012-of-00029.safetensors", "transformer.h.15.mlp.c_proj.weight": "model-00012-of-00029.safetensors", "transformer.h.15.mlp.w1.weight": "model-00012-of-00029.safetensors", "transformer.h.15.mlp.w2.weight": "model-00012-of-00029.safetensors", "transformer.h.16.attn.c_attn.bias": "model-00012-of-00029.safetensors", "transformer.h.16.attn.c_attn.weight": "model-00012-of-00029.safetensors", "transformer.h.16.attn.c_proj.weight": "model-00012-of-00029.safetensors", "transformer.h.16.ln_1.weight": "model-00012-of-00029.safetensors", "transformer.h.16.ln_2.weight": "model-00012-of-00029.safetensors", "transformer.h.16.mlp.c_proj.weight": "model-00013-of-00029.safetensors", "transformer.h.16.mlp.w1.weight": "model-00012-of-00029.safetensors", "transformer.h.16.mlp.w2.weight": "model-00013-of-00029.safetensors", "transformer.h.17.attn.c_attn.bias": "model-00013-of-00029.safetensors", "transformer.h.17.attn.c_attn.weight": "model-00013-of-00029.safetensors", "transformer.h.17.attn.c_proj.weight": "model-00013-of-00029.safetensors", "transformer.h.17.ln_1.weight": "model-00013-of-00029.safetensors", "transformer.h.17.ln_2.weight": "model-00013-of-00029.safetensors", "transformer.h.17.mlp.c_proj.weight": "model-00013-of-00029.safetensors", "transformer.h.17.mlp.w1.weight": "model-00013-of-00029.safetensors", "transformer.h.17.mlp.w2.weight": "model-00013-of-00029.safetensors", "transformer.h.18.attn.c_attn.bias": "model-00014-of-00029.safetensors", "transformer.h.18.attn.c_attn.weight": "model-00014-of-00029.safetensors", "transformer.h.18.attn.c_proj.weight": "model-00014-of-00029.safetensors", "transformer.h.18.ln_1.weight": "model-00013-of-00029.safetensors", "transformer.h.18.ln_2.weight": "model-00014-of-00029.safetensors", "transformer.h.18.mlp.c_proj.weight": "model-00014-of-00029.safetensors", "transformer.h.18.mlp.w1.weight": "model-00014-of-00029.safetensors", "transformer.h.18.mlp.w2.weight": "model-00014-of-00029.safetensors", "transformer.h.19.attn.c_attn.bias": "model-00014-of-00029.safetensors", "transformer.h.19.attn.c_attn.weight": "model-00014-of-00029.safetensors", "transformer.h.19.attn.c_proj.weight": "model-00014-of-00029.safetensors", "transformer.h.19.ln_1.weight": "model-00014-of-00029.safetensors", "transformer.h.19.ln_2.weight": "model-00014-of-00029.safetensors", "transformer.h.19.mlp.c_proj.weight": "model-00015-of-00029.safetensors", "transformer.h.19.mlp.w1.weight": "model-00014-of-00029.safetensors", "transformer.h.19.mlp.w2.weight": "model-00015-of-00029.safetensors", "transformer.h.2.attn.c_attn.bias": "model-00003-of-00029.safetensors", "transformer.h.2.attn.c_attn.weight": "model-00003-of-00029.safetensors", "transformer.h.2.attn.c_proj.weight": "model-00003-of-00029.safetensors", "transformer.h.2.ln_1.weight": "model-00003-of-00029.safetensors", "transformer.h.2.ln_2.weight": "model-00003-of-00029.safetensors", "transformer.h.2.mlp.c_proj.weight": "model-00003-of-00029.safetensors", "transformer.h.2.mlp.w1.weight": "model-00003-of-00029.safetensors", "transformer.h.2.mlp.w2.weight": "model-00003-of-00029.safetensors", "transformer.h.20.attn.c_attn.bias": "model-00015-of-00029.safetensors", "transformer.h.20.attn.c_attn.weight": "model-00015-of-00029.safetensors", "transformer.h.20.attn.c_proj.weight": "model-00015-of-00029.safetensors", "transformer.h.20.ln_1.weight": "model-00015-of-00029.safetensors", "transformer.h.20.ln_2.weight": "model-00015-of-00029.safetensors", "transformer.h.20.mlp.c_proj.weight": "model-00015-of-00029.safetensors", "transformer.h.20.mlp.w1.weight": "model-00015-of-00029.safetensors", "transformer.h.20.mlp.w2.weight": "model-00015-of-00029.safetensors", "transformer.h.21.attn.c_attn.bias": "model-00016-of-00029.safetensors", "transformer.h.21.attn.c_attn.weight": "model-00016-of-00029.safetensors", "transformer.h.21.attn.c_proj.weight": "model-00016-of-00029.safetensors", "transformer.h.21.ln_1.weight": "model-00015-of-00029.safetensors", "transformer.h.21.ln_2.weight": "model-00016-of-00029.safetensors", "transformer.h.21.mlp.c_proj.weight": "model-00016-of-00029.safetensors", "transformer.h.21.mlp.w1.weight": "model-00016-of-00029.safetensors", "transformer.h.21.mlp.w2.weight": "model-00016-of-00029.safetensors", "transformer.h.22.attn.c_attn.bias": "model-00016-of-00029.safetensors", "transformer.h.22.attn.c_attn.weight": "model-00016-of-00029.safetensors", "transformer.h.22.attn.c_proj.weight": "model-00016-of-00029.safetensors", "transformer.h.22.ln_1.weight": "model-00016-of-00029.safetensors", "transformer.h.22.ln_2.weight": "model-00016-of-00029.safetensors", "transformer.h.22.mlp.c_proj.weight": "model-00017-of-00029.safetensors", "transformer.h.22.mlp.w1.weight": "model-00016-of-00029.safetensors", "transformer.h.22.mlp.w2.weight": "model-00017-of-00029.safetensors", "transformer.h.23.attn.c_attn.bias": "model-00017-of-00029.safetensors", "transformer.h.23.attn.c_attn.weight": "model-00017-of-00029.safetensors", "transformer.h.23.attn.c_proj.weight": "model-00017-of-00029.safetensors", "transformer.h.23.ln_1.weight": "model-00017-of-00029.safetensors", "transformer.h.23.ln_2.weight": "model-00017-of-00029.safetensors", "transformer.h.23.mlp.c_proj.weight": "model-00017-of-00029.safetensors", "transformer.h.23.mlp.w1.weight": "model-00017-of-00029.safetensors", "transformer.h.23.mlp.w2.weight": "model-00017-of-00029.safetensors", "transformer.h.24.attn.c_attn.bias": "model-00018-of-00029.safetensors", "transformer.h.24.attn.c_attn.weight": "model-00018-of-00029.safetensors", "transformer.h.24.attn.c_proj.weight": "model-00018-of-00029.safetensors", "transformer.h.24.ln_1.weight": "model-00017-of-00029.safetensors", "transformer.h.24.ln_2.weight": "model-00018-of-00029.safetensors", "transformer.h.24.mlp.c_proj.weight": "model-00018-of-00029.safetensors", "transformer.h.24.mlp.w1.weight": "model-00018-of-00029.safetensors", "transformer.h.24.mlp.w2.weight": "model-00018-of-00029.safetensors", "transformer.h.25.attn.c_attn.bias": "model-00018-of-00029.safetensors", "transformer.h.25.attn.c_attn.weight": "model-00018-of-00029.safetensors", "transformer.h.25.attn.c_proj.weight": "model-00018-of-00029.safetensors", "transformer.h.25.ln_1.weight": "model-00018-of-00029.safetensors", "transformer.h.25.ln_2.weight": "model-00018-of-00029.safetensors", "transformer.h.25.mlp.c_proj.weight": "model-00019-of-00029.safetensors", "transformer.h.25.mlp.w1.weight": "model-00018-of-00029.safetensors", "transformer.h.25.mlp.w2.weight": "model-00019-of-00029.safetensors", "transformer.h.26.attn.c_attn.bias": "model-00019-of-00029.safetensors", "transformer.h.26.attn.c_attn.weight": "model-00019-of-00029.safetensors", "transformer.h.26.attn.c_proj.weight": "model-00019-of-00029.safetensors", "transformer.h.26.ln_1.weight": "model-00019-of-00029.safetensors", "transformer.h.26.ln_2.weight": "model-00019-of-00029.safetensors", "transformer.h.26.mlp.c_proj.weight": "model-00019-of-00029.safetensors", "transformer.h.26.mlp.w1.weight": "model-00019-of-00029.safetensors", "transformer.h.26.mlp.w2.weight": "model-00019-of-00029.safetensors", "transformer.h.27.attn.c_attn.bias": "model-00020-of-00029.safetensors", "transformer.h.27.attn.c_attn.weight": "model-00020-of-00029.safetensors", "transformer.h.27.attn.c_proj.weight": "model-00020-of-00029.safetensors", "transformer.h.27.ln_1.weight": "model-00019-of-00029.safetensors", "transformer.h.27.ln_2.weight": "model-00020-of-00029.safetensors", "transformer.h.27.mlp.c_proj.weight": "model-00020-of-00029.safetensors", "transformer.h.27.mlp.w1.weight": "model-00020-of-00029.safetensors", "transformer.h.27.mlp.w2.weight": "model-00020-of-00029.safetensors", "transformer.h.28.attn.c_attn.bias": "model-00020-of-00029.safetensors", "transformer.h.28.attn.c_attn.weight": "model-00020-of-00029.safetensors", "transformer.h.28.attn.c_proj.weight": "model-00020-of-00029.safetensors", "transformer.h.28.ln_1.weight": "model-00020-of-00029.safetensors", "transformer.h.28.ln_2.weight": "model-00020-of-00029.safetensors", "transformer.h.28.mlp.c_proj.weight": "model-00021-of-00029.safetensors", "transformer.h.28.mlp.w1.weight": "model-00020-of-00029.safetensors", "transformer.h.28.mlp.w2.weight": "model-00021-of-00029.safetensors", "transformer.h.29.attn.c_attn.bias": "model-00021-of-00029.safetensors", "transformer.h.29.attn.c_attn.weight": "model-00021-of-00029.safetensors", "transformer.h.29.attn.c_proj.weight": "model-00021-of-00029.safetensors", "transformer.h.29.ln_1.weight": "model-00021-of-00029.safetensors", "transformer.h.29.ln_2.weight": "model-00021-of-00029.safetensors", "transformer.h.29.mlp.c_proj.weight": "model-00021-of-00029.safetensors", "transformer.h.29.mlp.w1.weight": "model-00021-of-00029.safetensors", "transformer.h.29.mlp.w2.weight": "model-00021-of-00029.safetensors", "transformer.h.3.attn.c_attn.bias": "model-00004-of-00029.safetensors", "transformer.h.3.attn.c_attn.weight": "model-00004-of-00029.safetensors", "transformer.h.3.attn.c_proj.weight": "model-00004-of-00029.safetensors", "transformer.h.3.ln_1.weight": "model-00003-of-00029.safetensors", "transformer.h.3.ln_2.weight": "model-00004-of-00029.safetensors", "transformer.h.3.mlp.c_proj.weight": "model-00004-of-00029.safetensors", "transformer.h.3.mlp.w1.weight": "model-00004-of-00029.safetensors", "transformer.h.3.mlp.w2.weight": "model-00004-of-00029.safetensors", "transformer.h.30.attn.c_attn.bias": "model-00022-of-00029.safetensors", "transformer.h.30.attn.c_attn.weight": "model-00022-of-00029.safetensors", "transformer.h.30.attn.c_proj.weight": "model-00022-of-00029.safetensors", "transformer.h.30.ln_1.weight": "model-00021-of-00029.safetensors", "transformer.h.30.ln_2.weight": "model-00022-of-00029.safetensors", "transformer.h.30.mlp.c_proj.weight": "model-00022-of-00029.safetensors", "transformer.h.30.mlp.w1.weight": "model-00022-of-00029.safetensors", "transformer.h.30.mlp.w2.weight": "model-00022-of-00029.safetensors", "transformer.h.31.attn.c_attn.bias": "model-00022-of-00029.safetensors", "transformer.h.31.attn.c_attn.weight": "model-00022-of-00029.safetensors", "transformer.h.31.attn.c_proj.weight": "model-00022-of-00029.safetensors", "transformer.h.31.ln_1.weight": "model-00022-of-00029.safetensors", "transformer.h.31.ln_2.weight": "model-00022-of-00029.safetensors", "transformer.h.31.mlp.c_proj.weight": "model-00023-of-00029.safetensors", "transformer.h.31.mlp.w1.weight": "model-00022-of-00029.safetensors", "transformer.h.31.mlp.w2.weight": "model-00023-of-00029.safetensors", "transformer.h.32.attn.c_attn.bias": "model-00023-of-00029.safetensors", "transformer.h.32.attn.c_attn.weight": "model-00023-of-00029.safetensors", "transformer.h.32.attn.c_proj.weight": "model-00023-of-00029.safetensors", "transformer.h.32.ln_1.weight": "model-00023-of-00029.safetensors", "transformer.h.32.ln_2.weight": "model-00023-of-00029.safetensors", "transformer.h.32.mlp.c_proj.weight": "model-00023-of-00029.safetensors", "transformer.h.32.mlp.w1.weight": "model-00023-of-00029.safetensors", "transformer.h.32.mlp.w2.weight": "model-00023-of-00029.safetensors", "transformer.h.33.attn.c_attn.bias": "model-00024-of-00029.safetensors", "transformer.h.33.attn.c_attn.weight": "model-00024-of-00029.safetensors", "transformer.h.33.attn.c_proj.weight": "model-00024-of-00029.safetensors", "transformer.h.33.ln_1.weight": "model-00023-of-00029.safetensors", "transformer.h.33.ln_2.weight": "model-00024-of-00029.safetensors", "transformer.h.33.mlp.c_proj.weight": "model-00024-of-00029.safetensors", "transformer.h.33.mlp.w1.weight": "model-00024-of-00029.safetensors", "transformer.h.33.mlp.w2.weight": "model-00024-of-00029.safetensors", "transformer.h.34.attn.c_attn.bias": "model-00024-of-00029.safetensors", "transformer.h.34.attn.c_attn.weight": "model-00024-of-00029.safetensors", "transformer.h.34.attn.c_proj.weight": "model-00024-of-00029.safetensors", "transformer.h.34.ln_1.weight": "model-00024-of-00029.safetensors", "transformer.h.34.ln_2.weight": "model-00024-of-00029.safetensors", "transformer.h.34.mlp.c_proj.weight": "model-00025-of-00029.safetensors", "transformer.h.34.mlp.w1.weight": "model-00024-of-00029.safetensors", "transformer.h.34.mlp.w2.weight": "model-00025-of-00029.safetensors", "transformer.h.35.attn.c_attn.bias": "model-00025-of-00029.safetensors", "transformer.h.35.attn.c_attn.weight": "model-00025-of-00029.safetensors", "transformer.h.35.attn.c_proj.weight": "model-00025-of-00029.safetensors", "transformer.h.35.ln_1.weight": "model-00025-of-00029.safetensors", "transformer.h.35.ln_2.weight": "model-00025-of-00029.safetensors", "transformer.h.35.mlp.c_proj.weight": "model-00025-of-00029.safetensors", "transformer.h.35.mlp.w1.weight": "model-00025-of-00029.safetensors", "transformer.h.35.mlp.w2.weight": "model-00025-of-00029.safetensors", "transformer.h.36.attn.c_attn.bias": "model-00026-of-00029.safetensors", "transformer.h.36.attn.c_attn.weight": "model-00026-of-00029.safetensors", "transformer.h.36.attn.c_proj.weight": "model-00026-of-00029.safetensors", "transformer.h.36.ln_1.weight": "model-00025-of-00029.safetensors", "transformer.h.36.ln_2.weight": "model-00026-of-00029.safetensors", "transformer.h.36.mlp.c_proj.weight": "model-00026-of-00029.safetensors", "transformer.h.36.mlp.w1.weight": "model-00026-of-00029.safetensors", "transformer.h.36.mlp.w2.weight": "model-00026-of-00029.safetensors", "transformer.h.37.attn.c_attn.bias": "model-00026-of-00029.safetensors", "transformer.h.37.attn.c_attn.weight": "model-00026-of-00029.safetensors", "transformer.h.37.attn.c_proj.weight": "model-00026-of-00029.safetensors", "transformer.h.37.ln_1.weight": "model-00026-of-00029.safetensors", "transformer.h.37.ln_2.weight": "model-00026-of-00029.safetensors", "transformer.h.37.mlp.c_proj.weight": "model-00027-of-00029.safetensors", "transformer.h.37.mlp.w1.weight": "model-00026-of-00029.safetensors", "transformer.h.37.mlp.w2.weight": "model-00027-of-00029.safetensors", "transformer.h.38.attn.c_attn.bias": "model-00027-of-00029.safetensors", "transformer.h.38.attn.c_attn.weight": "model-00027-of-00029.safetensors", "transformer.h.38.attn.c_proj.weight": "model-00027-of-00029.safetensors", "transformer.h.38.ln_1.weight": "model-00027-of-00029.safetensors", "transformer.h.38.ln_2.weight": "model-00027-of-00029.safetensors", "transformer.h.38.mlp.c_proj.weight": "model-00027-of-00029.safetensors", "transformer.h.38.mlp.w1.weight": "model-00027-of-00029.safetensors", "transformer.h.38.mlp.w2.weight": "model-00027-of-00029.safetensors", "transformer.h.39.attn.c_attn.bias": "model-00028-of-00029.safetensors", "transformer.h.39.attn.c_attn.weight": "model-00028-of-00029.safetensors", "transformer.h.39.attn.c_proj.weight": "model-00028-of-00029.safetensors", "transformer.h.39.ln_1.weight": "model-00027-of-00029.safetensors", "transformer.h.39.ln_2.weight": "model-00028-of-00029.safetensors", "transformer.h.39.mlp.c_proj.weight": "model-00028-of-00029.safetensors", "transformer.h.39.mlp.w1.weight": "model-00028-of-00029.safetensors", "transformer.h.39.mlp.w2.weight": "model-00028-of-00029.safetensors", "transformer.h.4.attn.c_attn.bias": "model-00004-of-00029.safetensors", "transformer.h.4.attn.c_attn.weight": "model-00004-of-00029.safetensors", "transformer.h.4.attn.c_proj.weight": "model-00004-of-00029.safetensors", "transformer.h.4.ln_1.weight": "model-00004-of-00029.safetensors", "transformer.h.4.ln_2.weight": "model-00004-of-00029.safetensors", "transformer.h.4.mlp.c_proj.weight": "model-00005-of-00029.safetensors", "transformer.h.4.mlp.w1.weight": "model-00004-of-00029.safetensors", "transformer.h.4.mlp.w2.weight": "model-00005-of-00029.safetensors", "transformer.h.5.attn.c_attn.bias": "model-00005-of-00029.safetensors", "transformer.h.5.attn.c_attn.weight": "model-00005-of-00029.safetensors", "transformer.h.5.attn.c_proj.weight": "model-00005-of-00029.safetensors", "transformer.h.5.ln_1.weight": "model-00005-of-00029.safetensors", "transformer.h.5.ln_2.weight": "model-00005-of-00029.safetensors", "transformer.h.5.mlp.c_proj.weight": "model-00005-of-00029.safetensors", "transformer.h.5.mlp.w1.weight": "model-00005-of-00029.safetensors", "transformer.h.5.mlp.w2.weight": "model-00005-of-00029.safetensors", "transformer.h.6.attn.c_attn.bias": "model-00006-of-00029.safetensors", "transformer.h.6.attn.c_attn.weight": "model-00006-of-00029.safetensors", "transformer.h.6.attn.c_proj.weight": "model-00006-of-00029.safetensors", "transformer.h.6.ln_1.weight": "model-00005-of-00029.safetensors", "transformer.h.6.ln_2.weight": "model-00006-of-00029.safetensors", "transformer.h.6.mlp.c_proj.weight": "model-00006-of-00029.safetensors", "transformer.h.6.mlp.w1.weight": "model-00006-of-00029.safetensors", "transformer.h.6.mlp.w2.weight": "model-00006-of-00029.safetensors", "transformer.h.7.attn.c_attn.bias": "model-00006-of-00029.safetensors", "transformer.h.7.attn.c_attn.weight": "model-00006-of-00029.safetensors", "transformer.h.7.attn.c_proj.weight": "model-00006-of-00029.safetensors", "transformer.h.7.ln_1.weight": "model-00006-of-00029.safetensors", "transformer.h.7.ln_2.weight": "model-00006-of-00029.safetensors", "transformer.h.7.mlp.c_proj.weight": "model-00007-of-00029.safetensors", "transformer.h.7.mlp.w1.weight": "model-00006-of-00029.safetensors", "transformer.h.7.mlp.w2.weight": "model-00007-of-00029.safetensors", "transformer.h.8.attn.c_attn.bias": "model-00007-of-00029.safetensors", "transformer.h.8.attn.c_attn.weight": "model-00007-of-00029.safetensors", "transformer.h.8.attn.c_proj.weight": "model-00007-of-00029.safetensors", "transformer.h.8.ln_1.weight": "model-00007-of-00029.safetensors", "transformer.h.8.ln_2.weight": "model-00007-of-00029.safetensors", "transformer.h.8.mlp.c_proj.weight": "model-00007-of-00029.safetensors", "transformer.h.8.mlp.w1.weight": "model-00007-of-00029.safetensors", "transformer.h.8.mlp.w2.weight": "model-00007-of-00029.safetensors", "transformer.h.9.attn.c_attn.bias": "model-00008-of-00029.safetensors", "transformer.h.9.attn.c_attn.weight": "model-00008-of-00029.safetensors", "transformer.h.9.attn.c_proj.weight": "model-00008-of-00029.safetensors", "transformer.h.9.ln_1.weight": "model-00007-of-00029.safetensors", "transformer.h.9.ln_2.weight": "model-00008-of-00029.safetensors", "transformer.h.9.mlp.c_proj.weight": "model-00008-of-00029.safetensors", "transformer.h.9.mlp.w1.weight": "model-00008-of-00029.safetensors", "transformer.h.9.mlp.w2.weight": "model-00008-of-00029.safetensors", "transformer.ln_f.weight": "model-00028-of-00029.safetensors", "transformer.wte.weight": "model-00001-of-00029.safetensors" }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363# Copyright (c) Alibaba Cloud.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
import copyimport importlibimport mathimport pathlibfrom typing import TYPE_CHECKING, Optional, Tuple, Union, Callable, List, Any, Generator
import torchimport torch.nn.functional as Fimport torch.utils.checkpointimport warnings
from torch.nn import CrossEntropyLossfrom transformers import PreTrainedTokenizer, GenerationConfig, StoppingCriteriaListfrom transformers.generation.logits_process import LogitsProcessorList
if TYPE_CHECKING: from transformers.generation.streamers import BaseStreamerfrom transformers.generation.utils import GenerateOutputfrom transformers.modeling_outputs import ( BaseModelOutputWithPast, CausalLMOutputWithPast,)from transformers.modeling_utils import PreTrainedModelfrom transformers.utils import logging
try: from einops import rearrangeexcept ImportError: rearrange = Nonefrom torch import nn
SUPPORT_CUDA = torch.cuda.is_available()SUPPORT_BF16 = SUPPORT_CUDA and torch.cuda.is_bf16_supported()SUPPORT_FP16 = SUPPORT_CUDA and torch.cuda.get_device_capability(0)[0] >= 7SUPPORT_TORCH2 = hasattr(torch, '__version__') and int(torch.__version__.split(".")[0]) >= 2
from .configuration_qwen import QWenConfigfrom .qwen_generation_utils import ( HistoryType, make_context, decode_tokens, get_stop_words_ids, StopWordsLogitsProcessor,)
logger = logging.get_logger(__name__)
_CHECKPOINT_FOR_DOC = "qwen"_CONFIG_FOR_DOC = "QWenConfig"
QWen_PRETRAINED_MODEL_ARCHIVE_LIST = ["qwen-7b"]
_ERROR_BAD_CHAT_FORMAT = """\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".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().我们检测到您可能在使用预训练模型(而非chat模型)进行多轮chat,因为您当前在generation_config指定的chat_format,并未设置为我们在对话中所支持的"chatml"格式。如果您在直接使用我们从Huggingface提供的模型,请确保您在调用model.chat()时,使用的是"Qwen/Qwen-7B-Chat"模型(而非"Qwen/Qwen-7B"预训练模型)。"""
_SENTINEL = object()_ERROR_STREAM_IN_CHAT = """\Pass argument `stream` to model.chat() is buggy, deprecated, and marked for removal. Please use model.chat_stream(...) instead of model.chat(..., stream=True).向model.chat()传入参数stream的用法可能存在Bug,该用法已被废弃,将在未来被移除。请使用model.chat_stream(...)代替model.chat(..., stream=True)。"""
_ERROR_INPUT_CPU_QUERY_WITH_FLASH_ATTN_ACTIVATED = """\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).检测到您的模型已激活了flash attention支持,但正在执行CPU运算任务。如使用flash attention,请您确认模型输入已经传到GPU上。如果您确认要执行CPU运算,请您在载入模型(调用AutoModelForCausalLM.from_pretrained)时,按照readme说法,指定device_map="cpu"以禁用flash attention。"""
apply_rotary_emb_func = Nonerms_norm = Noneflash_attn_unpadded_func = Noneflash_attn_func = None
def _import_flash_attn(): global apply_rotary_emb_func, rms_norm, flash_attn_unpadded_func, flash_attn_func try: from flash_attn.layers.rotary import apply_rotary_emb_func as __apply_rotary_emb_func apply_rotary_emb_func = __apply_rotary_emb_func except ImportError: logger.warn( "Warning: import flash_attn rotary fail, please install FlashAttention rotary to get higher efficiency " "https://github.com/Dao-AILab/flash-attention/tree/main/csrc/rotary" )
try: from flash_attn.ops.rms_norm import rms_norm as __rms_norm rms_norm = __rms_norm except ImportError: logger.warn( "Warning: import flash_attn rms_norm fail, please install FlashAttention layer_norm to get higher efficiency " "https://github.com/Dao-AILab/flash-attention/tree/main/csrc/layer_norm" )
try: import flash_attn _flash_attn_func = None if not hasattr(flash_attn, '__version__'): from flash_attn.flash_attn_interface import flash_attn_unpadded_func as __flash_attn_unpadded_func else: if int(flash_attn.__version__.split(".")[0]) >= 2: if int(flash_attn.__version__.split(".")[1]) >= 1: from flash_attn.flash_attn_interface import flash_attn_func as _flash_attn_func from flash_attn.flash_attn_interface import flash_attn_varlen_func as __flash_attn_unpadded_func else: from flash_attn.flash_attn_interface import flash_attn_unpadded_func as __flash_attn_unpadded_func flash_attn_unpadded_func = __flash_attn_unpadded_func flash_attn_func = _flash_attn_func except ImportError: logger.warn( "Warning: import flash_attn fail, please install FlashAttention to get higher efficiency " "https://github.com/Dao-AILab/flash-attention" )
def quantize_cache_v(fdata, bits, qmax, qmin): # b, s, head, h-dim->b, head, s, h-dim qtype = torch.uint8 device = fdata.device shape = fdata.shape
fdata_cal = torch.flatten(fdata, 2) fmax = torch.amax(fdata_cal, dim=-1, keepdim=True) fmin = torch.amin(fdata_cal, dim=-1, keepdim=True) # Compute params if qmax.device != fmax.device: qmax = qmax.to(device) qmin = qmin.to(device) scale = (fmax - fmin) / (qmax - qmin) zero = qmin - fmin / scale scale = scale.unsqueeze(-1).repeat(1,1,shape[2],1).contiguous() zero = zero.unsqueeze(-1).repeat(1,1,shape[2],1).contiguous() # Quantize res_data = fdata / scale + zero qdata = torch.clamp(res_data, qmin, qmax).to(qtype) return qdata.contiguous(), scale, zero
def dequantize_cache_torch(qdata, scale, zero): data = scale * (qdata - zero) return data
class FlashSelfAttention(torch.nn.Module): def __init__( self, causal=False, softmax_scale=None, attention_dropout=0.0, ): super().__init__() assert flash_attn_unpadded_func is not None, ( "Please install FlashAttention first, " "e.g., with pip install flash-attn" ) assert ( rearrange is not None ), "Please install einops first, e.g., with pip install einops" self.causal = causal self.softmax_scale = softmax_scale self.dropout_p = attention_dropout
def unpad_input(self, hidden_states, attention_mask): valid_mask = attention_mask.squeeze(1).squeeze(1).eq(0) seqlens_in_batch = valid_mask.sum(dim=-1, dtype=torch.int32) indices = torch.nonzero(valid_mask.flatten(), as_tuple=False).flatten() max_seqlen_in_batch = seqlens_in_batch.max().item() cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0)) hidden_states = hidden_states[indices] return hidden_states, indices, cu_seqlens, max_seqlen_in_batch
def pad_input(self, hidden_states, indices, batch, seqlen): output = torch.zeros(batch * seqlen, *hidden_states.shape[1:], device=hidden_states.device, dtype=hidden_states.dtype) output[indices] = hidden_states return rearrange(output, '(b s) ... -> b s ...', b=batch)
def forward(self, q, k, v, attention_mask=None): assert all((i.dtype in [torch.float16, torch.bfloat16] for i in (q, k, v))) assert all((i.is_cuda for i in (q, k, v))) batch_size, seqlen_q = q.shape[0], q.shape[1] seqlen_k = k.shape[1] seqlen_out = seqlen_q
if flash_attn_func is not None and batch_size == 1: dropout_p = self.dropout_p if self.training else 0 output = flash_attn_func(q, k, v, dropout_p, softmax_scale=self.softmax_scale, causal=self.causal) return output
q, k, v = [rearrange(x, "b s ... -> (b s) ...") for x in [q, k, v]] cu_seqlens_q = torch.arange( 0, (batch_size + 1) * seqlen_q, step=seqlen_q, dtype=torch.int32, device=q.device, )
if batch_size > 1 and attention_mask is not None: k, indices_k, cu_seqlens_k, seqlen_k = self.unpad_input(k, attention_mask) if q.size(0) == v.size(0): q = q[indices_k] cu_seqlens_q = cu_seqlens_k seqlen_q = seqlen_k v = v[indices_k] else: cu_seqlens_k = torch.arange( 0, (batch_size + 1) * seqlen_k, step=seqlen_k, dtype=torch.int32, device=q.device, )
if self.training: assert seqlen_k == seqlen_q is_causal = self.causal dropout_p = self.dropout_p else: is_causal = seqlen_q == seqlen_k dropout_p = 0
output = flash_attn_unpadded_func( q, k, v, cu_seqlens_q, cu_seqlens_k, seqlen_q, seqlen_k, dropout_p, softmax_scale=self.softmax_scale, causal=is_causal, ) if batch_size > 1 and attention_mask is not None and seqlen_q == seqlen_k: output = self.pad_input(output, indices_k, batch_size, seqlen_out) else: new_shape = (batch_size, output.shape[0] // batch_size) + output.shape[1:] output = output.view(new_shape) return output
class QWenAttention(nn.Module): def __init__(self, config): super().__init__()
self.register_buffer("masked_bias", torch.tensor(-1e4), persistent=False) self.seq_length = config.seq_length
self.hidden_size = config.hidden_size self.split_size = config.hidden_size self.num_heads = config.num_attention_heads self.head_dim = self.hidden_size // self.num_heads
self.use_flash_attn = config.use_flash_attn self.scale_attn_weights = True
self.projection_size = config.kv_channels * config.num_attention_heads
assert self.projection_size % config.num_attention_heads == 0 self.hidden_size_per_attention_head = ( self.projection_size // config.num_attention_heads )
self.c_attn = nn.Linear(config.hidden_size, 3 * self.projection_size)
self.c_proj = nn.Linear( config.hidden_size, self.projection_size, bias=not config.no_bias )
self.is_fp32 = not (config.bf16 or config.fp16) if ( self.use_flash_attn and flash_attn_unpadded_func is not None and not self.is_fp32 ): self.core_attention_flash = FlashSelfAttention( causal=True, attention_dropout=config.attn_dropout_prob ) self.bf16 = config.bf16
self.use_dynamic_ntk = config.use_dynamic_ntk self.use_logn_attn = config.use_logn_attn
logn_list = [ math.log(i, self.seq_length) if i > self.seq_length else 1 for i in range(1, 32768) ] logn_tensor = torch.tensor(logn_list)[None, :, None, None] self.register_buffer("logn_tensor", logn_tensor, persistent=False)
self.attn_dropout = nn.Dropout(config.attn_dropout_prob) self.softmax_in_fp32 = config.softmax_in_fp32 if hasattr(config, 'softmax_in_fp32') else False self.use_cache_quantization = config.use_cache_quantization if hasattr(config, 'use_cache_quantization') else False self.use_cache_kernel = config.use_cache_kernel if hasattr(config,'use_cache_kernel') else False cache_dtype = torch.float if self.bf16: cache_dtype=torch.bfloat16 elif config.fp16: cache_dtype = torch.float16 self.cache_qmax = torch.tensor(torch.iinfo(torch.uint8).max, dtype=cache_dtype) self.cache_qmin = torch.tensor(torch.iinfo(torch.uint8).min, dtype=cache_dtype)
if config.use_cache_quantization and config.use_cache_kernel: # pre check if the support files existing module_root = pathlib.Path(__file__).parent src_files = ("cache_autogptq_cuda_256.cpp", "cache_autogptq_cuda_kernel_256.cu") if any(not (module_root/src).is_file() for src in src_files): warnings.warn("KV cache kernel source files (.cpp and .cu) not found.") self.cache_kernels = None else: try: from .cpp_kernels import cache_autogptq_cuda_256 self.cache_kernels = cache_autogptq_cuda_256 except ImportError: warnings.warn("Failed to import KV cache kernels.") self.cache_kernels = None
def _attn(self, query, key, value, causal_mask=None, attention_mask=None, head_mask=None): device = query.device if self.use_cache_quantization: qk, qk_scale, qk_zero = key if self.use_cache_kernel and self.cache_kernels is not None: shape = query.shape[:-1] + (qk.shape[-2],) attn_weights = torch.zeros(shape, dtype=torch.float16, device=device) self.cache_kernels.vecquant8matmul_batched_faster_old( query.contiguous() if query.dtype == torch.float16 else query.to(torch.float16).contiguous(), qk.transpose(-1, -2).contiguous(), attn_weights, qk_scale.contiguous() if qk_scale.dtype == torch.float16 else qk_scale.to(torch.float16).contiguous(), qk_zero.contiguous()if qk_zero.dtype == torch.float16 else qk_zero.to(torch.float16).contiguous()) # attn_weights = attn_weights.to(query.dtype).contiguous() else: key = dequantize_cache_torch(qk, qk_scale, qk_zero) attn_weights = torch.matmul(query, key.transpose(-1, -2)) else: attn_weights = torch.matmul(query, key.transpose(-1, -2))
if self.scale_attn_weights: if self.use_cache_quantization: size_temp = value[0].size(-1) else: size_temp = value.size(-1) attn_weights = attn_weights / (size_temp ** 0.5)
mask_value = torch.finfo(attn_weights.dtype).min if causal_mask is not None: attn_weights = torch.where( causal_mask, attn_weights.to(attn_weights.dtype), mask_value )
if attention_mask is not None: attn_weights = attn_weights + attention_mask
if self.softmax_in_fp32: attn_weights = nn.functional.softmax(attn_weights.float(), dim=-1) else: attn_weights = nn.functional.softmax(attn_weights, dim=-1)
attn_weights = attn_weights.type(query.dtype) attn_weights = self.attn_dropout(attn_weights)
if head_mask is not None: attn_weights = attn_weights * head_mask
if self.use_cache_quantization: qv, qv_scale, qv_zero = value if self.use_cache_kernel and self.cache_kernels is not None: shape = attn_weights.shape[:-1] + (query.shape[-1],) attn_output = torch.zeros(shape, dtype=torch.float16, device=device) self.cache_kernels.vecquant8matmul_batched_column_compression_faster_old( attn_weights.contiguous() if attn_weights.dtype == torch.float16 else attn_weights.to(torch.float16).contiguous(), qv.contiguous(), # dtype: int32 attn_output, qv_scale.contiguous() if qv_scale.dtype == torch.float16 else qv_scale.to(torch.float16).contiguous(), qv_zero.contiguous() if qv_zero.dtype == torch.float16 else qv_zero.to(torch.float16).contiguous()) if attn_output.dtype != query.dtype: attn_output = attn_output.to(query.dtype) attn_weights = attn_weights.to(query.dtype) else: value = dequantize_cache_torch(qv, qv_scale, qv_zero) attn_output = torch.matmul(attn_weights, value) else: attn_output = torch.matmul(attn_weights, value)
attn_output = attn_output.transpose(1, 2)
return attn_output, attn_weights
def _split_heads(self, tensor, num_heads, attn_head_size): new_shape = tensor.size()[:-1] + (num_heads, attn_head_size) tensor = tensor.view(new_shape) return tensor
def _merge_heads(self, tensor, num_heads, attn_head_size): tensor = tensor.contiguous() new_shape = tensor.size()[:-2] + (num_heads * attn_head_size,) return tensor.view(new_shape)
def forward( self, hidden_states: Optional[Tuple[torch.FloatTensor]], rotary_pos_emb_list: Optional[List[List[torch.Tensor]]] = None, layer_past: Optional[Tuple[torch.Tensor]] = None, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, ): mixed_x_layer = self.c_attn(hidden_states)
query, key, value = mixed_x_layer.split(self.split_size, dim=2)
query = self._split_heads(query, self.num_heads, self.head_dim) key = self._split_heads(key, self.num_heads, self.head_dim) value = self._split_heads(value, self.num_heads, self.head_dim)
if rotary_pos_emb_list is not None: cur_len = query.shape[1] if len(rotary_pos_emb_list) == 1: rotary_pos_emb = rotary_pos_emb_list[0] rotary_pos_emb = [i[:, -cur_len:, :, :] for i in rotary_pos_emb] rotary_pos_emb = (rotary_pos_emb,) * 2 q_pos_emb, k_pos_emb = rotary_pos_emb # Slice the pos emb for current inference query = apply_rotary_pos_emb(query, q_pos_emb) key = apply_rotary_pos_emb(key, k_pos_emb) else: query_list = [] key_list = [] for i, rotary_pos_emb in enumerate(rotary_pos_emb_list): rotary_pos_emb = [i[:, -cur_len:, :, :] for i in rotary_pos_emb] rotary_pos_emb = (rotary_pos_emb,) * 2 q_pos_emb, k_pos_emb = rotary_pos_emb # Slice the pos emb for current inference query_list += [apply_rotary_pos_emb(query[i:i+1, :, :], q_pos_emb)] key_list += [apply_rotary_pos_emb(key[i:i+1, :, :], k_pos_emb)] query = torch.cat(query_list, dim=0) key = torch.cat(key_list, dim=0)
if self.use_cache_quantization: key = quantize_cache_v(key.permute(0, 2, 1, 3), bits=8, qmin=self.cache_qmin, qmax=self.cache_qmax) value = quantize_cache_v(value.permute(0, 2, 1, 3), bits=8, qmin=self.cache_qmin, qmax=self.cache_qmax)
if layer_past is not None: past_key, past_value = layer_past[0], layer_past[1] if self.use_cache_quantization: # use_cache_quantization: # present=((q_key,key_scale,key_zero_point), # (q_value,value_scale,value_zero_point)) key = (torch.cat((past_key[0], key[0]), dim=2), torch.cat((past_key[1], key[1]), dim=2), torch.cat((past_key[2], key[2]), dim=2)) value = (torch.cat((past_value[0], value[0]), dim=2), torch.cat((past_value[1], value[1]), dim=2), torch.cat((past_value[2], value[2]), dim=2)) else: # not use_cache_quantization: # present=(key,value) key = torch.cat((past_key, key), dim=1) value = torch.cat((past_value, value), dim=1)
if use_cache: present = (key, value) else: present = None
key_size = key[0].size(2) if self.use_cache_quantization else key.size(1) if key_size > self.seq_length and self.use_logn_attn and not self.training: if self.use_cache_quantization: seq_start = key[0].size(2) - query.size(1) seq_end = key[0].size(2) else: seq_start = key.size(1) - query.size(1) seq_end = key.size(1) logn_tensor = self.logn_tensor[:, seq_start:seq_end, :, :].type_as(query) query = query * logn_tensor.expand_as(query)
if ( self.use_flash_attn and flash_attn_unpadded_func is not None and not self.is_fp32 and query.is_cuda ): q, k, v = query, key, value attn_output = self.core_attention_flash(q, k, v, attention_mask=attention_mask) else: key_size = key[0].size(2) if self.use_cache_quantization else key.size(1) if query.size(1) == key_size: causal_mask = torch.tril( torch.ones((key_size, key_size), dtype=torch.bool, device=query.device) ).view(1, 1, key_size, key_size) else: causal_mask = None query = query.permute(0, 2, 1, 3) if not self.use_cache_quantization: key = key.permute(0, 2, 1, 3) value = value.permute(0, 2, 1, 3) if ( causal_mask is None and self.use_flash_attn and flash_attn_unpadded_func is not None and not self.is_fp32 and not query.is_cuda ): raise Exception(_ERROR_INPUT_CPU_QUERY_WITH_FLASH_ATTN_ACTIVATED)
if not self.use_cache_quantization and SUPPORT_TORCH2: if attention_mask is not None: attention_mask = attention_mask.expand(-1, -1, query.size(2), -1) if causal_mask is not None: attention_mask = attention_mask.masked_fill(~causal_mask, torch.finfo(query.dtype).min) else: attention_mask = causal_mask attn_output = F.scaled_dot_product_attention( query, key, value, attn_mask=attention_mask ).transpose(1, 2) attn_weight = None else: attn_output, attn_weight = self._attn( query, key, value, causal_mask, attention_mask, head_mask ) context_layer = self._merge_heads( attn_output, self.num_heads, self.head_dim )
attn_output = self.c_proj(context_layer)
outputs = (attn_output, present) if output_attentions: if ( self.use_flash_attn and flash_attn_unpadded_func is not None and not self.is_fp32 ): raise ValueError("Cannot output attentions while using flash-attn") elif not self.use_cache_quantization and SUPPORT_TORCH2: raise ValueError("Cannot output attentions while using scaled_dot_product_attention") else: outputs += (attn_weight,)
return outputs
class QWenMLP(nn.Module): def __init__(self, config): super().__init__() self.w1 = nn.Linear( config.hidden_size, config.intermediate_size // 2, bias=not config.no_bias ) self.w2 = nn.Linear( config.hidden_size, config.intermediate_size // 2, bias=not config.no_bias ) ff_dim_in = config.intermediate_size // 2 self.c_proj = nn.Linear(ff_dim_in, config.hidden_size, bias=not config.no_bias)
def forward(self, hidden_states): a1 = self.w1(hidden_states) a2 = self.w2(hidden_states) intermediate_parallel = a1 * F.silu(a2) output = self.c_proj(intermediate_parallel) return output
class QWenBlock(nn.Module): def __init__(self, config): super().__init__() hidden_size = config.hidden_size self.bf16 = config.bf16
self.ln_1 = RMSNorm( hidden_size, eps=config.layer_norm_epsilon, ) self.attn = QWenAttention(config) self.ln_2 = RMSNorm( hidden_size, eps=config.layer_norm_epsilon, )
self.mlp = QWenMLP(config)
def forward( self, hidden_states: Optional[Tuple[torch.FloatTensor]], rotary_pos_emb_list: Optional[List[List[torch.Tensor]]] = None, layer_past: Optional[Tuple[torch.Tensor]] = None, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, ): layernorm_output = self.ln_1(hidden_states)
attn_outputs = self.attn( layernorm_output, rotary_pos_emb_list, layer_past=layer_past, attention_mask=attention_mask, head_mask=head_mask, use_cache=use_cache, output_attentions=output_attentions, ) attn_output = attn_outputs[0]
outputs = attn_outputs[1:]
residual = hidden_states layernorm_input = attn_output + residual
layernorm_output = self.ln_2(layernorm_input)
residual = layernorm_input mlp_output = self.mlp(layernorm_output) hidden_states = residual + mlp_output
if use_cache: outputs = (hidden_states,) + outputs else: outputs = (hidden_states,) + outputs[1:]
return outputs
class QWenPreTrainedModel(PreTrainedModel): config_class = QWenConfig base_model_prefix = "transformer" is_parallelizable = False supports_gradient_checkpointing = True _no_split_modules = ["QWenBlock"] _skip_keys_device_placement = "past_key_values"
def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs)
def _init_weights(self, module): """Initialize the weights.""" if isinstance(module, nn.Linear): module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) if module.bias is not None: module.bias.data.zero_() elif isinstance(module, nn.Embedding): module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) if module.padding_idx is not None: module.weight.data[module.padding_idx].zero_() elif isinstance(module, RMSNorm): module.weight.data.fill_(1.0)
for name, p in module.named_parameters(): if name == "c_proj.weight": p.data.normal_( mean=0.0, std=( self.config.initializer_range / math.sqrt(2 * self.config.num_hidden_layers) ), )
def _set_gradient_checkpointing(self, module, value=False): if isinstance(module, QWenModel): module.gradient_checkpointing = value
class QWenModel(QWenPreTrainedModel): _keys_to_ignore_on_load_missing = ["attn.masked_bias"]
def __init__(self, config): super().__init__(config) self.vocab_size = config.vocab_size self.num_hidden_layers = config.num_hidden_layers self.embed_dim = config.hidden_size self.use_cache_quantization = self.config.use_cache_quantization if hasattr(self.config, 'use_cache_quantization') else False
self.gradient_checkpointing = False self.use_dynamic_ntk = config.use_dynamic_ntk self.seq_length = config.seq_length
self.wte = nn.Embedding(self.vocab_size, self.embed_dim)
self.drop = nn.Dropout(config.emb_dropout_prob)
if config.rotary_pct == 1.0: self.rotary_ndims = None else: assert config.rotary_pct < 1 self.rotary_ndims = int( config.kv_channels * config.rotary_pct ) dim = ( self.rotary_ndims if self.rotary_ndims is not None else config.kv_channels ) self.rotary_emb = RotaryEmbedding(dim, base=config.rotary_emb_base)
self.use_flash_attn = config.use_flash_attn self.is_fp32 = not (config.bf16 or config.fp16)
self.h = nn.ModuleList( [ QWenBlock( config ) for i in range(config.num_hidden_layers) ] ) self.ln_f = RMSNorm( self.embed_dim, eps=config.layer_norm_epsilon, )
self.post_init()
def get_input_embeddings(self): return self.wte
def set_input_embeddings(self, new_embeddings): self.wte = new_embeddings
def get_ntk_alpha(self, true_seq_len): context_value = math.log(true_seq_len / self.seq_length, 2) + 1 ntk_alpha = 2 ** math.ceil(context_value) - 1 ntk_alpha = max(ntk_alpha, 1) return ntk_alpha
def forward( self, input_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ): output_attentions = ( output_attentions if output_attentions is not None else self.config.output_attentions ) output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) use_cache = use_cache if use_cache is not None else self.config.use_cache return_dict = ( return_dict if return_dict is not None else self.config.use_return_dict )
if input_ids is not None and inputs_embeds is not None: raise ValueError( "You cannot specify both input_ids and inputs_embeds at the same time" ) elif input_ids is not None: input_shape = input_ids.size() input_ids = input_ids.view(-1, input_shape[-1]) batch_size = input_ids.shape[0] elif inputs_embeds is not None: input_shape = inputs_embeds.size()[:-1] batch_size = inputs_embeds.shape[0] else: raise ValueError("You have to specify either input_ids or inputs_embeds")
device = input_ids.device if input_ids is not None else inputs_embeds.device
if token_type_ids is not None: token_type_ids = token_type_ids.view(-1, input_shape[-1]) if position_ids is not None: position_ids = position_ids.view(-1, input_shape[-1])
if past_key_values is None: past_length = 0 past_key_values = tuple([None] * len(self.h)) else: if self.use_cache_quantization: past_length = past_key_values[0][0][0].size(2) else: past_length = past_key_values[0][0].size(-2) if position_ids is None: position_ids = torch.arange( past_length, input_shape[-1] + past_length, dtype=torch.long, device=device, ) position_ids = position_ids.unsqueeze(0).view(-1, input_shape[-1])
if attention_mask is not None: if batch_size <= 0: raise ValueError("batch_size has to be defined and > 0") attention_mask = attention_mask.view(batch_size, -1) attention_mask = attention_mask[:, None, None, :] attention_mask = attention_mask.to(dtype=self.dtype) attention_mask = (1.0 - attention_mask) * torch.finfo(self.dtype).min
encoder_attention_mask = None head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers)
if inputs_embeds is None: inputs_embeds = self.wte(input_ids) hidden_states = inputs_embeds
kv_seq_len = hidden_states.size()[1] if past_key_values[0] is not None: # past key values[0][0] shape: bs * seq_len * head_num * dim if self.use_cache_quantization: kv_seq_len += past_key_values[0][0][0].shape[2] else: kv_seq_len += past_key_values[0][0].shape[1]
if self.training or not self.use_dynamic_ntk: ntk_alpha_list = [1.0] elif kv_seq_len != hidden_states.size()[1]: ntk_alpha_list = self.rotary_emb._ntk_alpha_cached_list else: ntk_alpha_list = [] if attention_mask is not None and kv_seq_len > self.seq_length: true_seq_lens = attention_mask.squeeze(1).squeeze(1).eq(0).sum(dim=-1, dtype=torch.int32) for i in range(hidden_states.size()[0]): true_seq_len = true_seq_lens[i].item() ntk_alpha = self.get_ntk_alpha(true_seq_len) ntk_alpha_list.append(ntk_alpha) else: ntk_alpha = self.get_ntk_alpha(kv_seq_len) ntk_alpha_list.append(ntk_alpha) self.rotary_emb._ntk_alpha_cached_list = ntk_alpha_list rotary_pos_emb_list = [ self.rotary_emb(kv_seq_len, ntk_alpha=ntk_alpha) for ntk_alpha in ntk_alpha_list ]
hidden_states = self.drop(hidden_states) output_shape = input_shape + (hidden_states.size(-1),)
if self.gradient_checkpointing and self.training: if use_cache: logger.warning_once( "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." ) use_cache = False
presents = () if use_cache else None all_self_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)):
if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,)
if self.gradient_checkpointing and self.training:
def create_custom_forward(module): def custom_forward(*inputs): # None for past_key_value return module(*inputs, use_cache, output_attentions)
return custom_forward
outputs = torch.utils.checkpoint.checkpoint( create_custom_forward(block), hidden_states, rotary_pos_emb_list, None, attention_mask, head_mask[i], encoder_hidden_states, encoder_attention_mask, ) else: outputs = block( hidden_states, layer_past=layer_past, rotary_pos_emb_list=rotary_pos_emb_list, attention_mask=attention_mask, head_mask=head_mask[i], encoder_hidden_states=encoder_hidden_states, encoder_attention_mask=encoder_attention_mask, use_cache=use_cache, output_attentions=output_attentions, )
hidden_states = outputs[0] if use_cache is True: presents = presents + (outputs[1],)
if output_attentions: all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],)
hidden_states = self.ln_f(hidden_states) hidden_states = hidden_states.view(output_shape) # Add last hidden state if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,)
if not return_dict: return tuple( v for v in [hidden_states, presents, all_hidden_states] if v is not None )
return BaseModelOutputWithPast( last_hidden_state=hidden_states, past_key_values=presents, hidden_states=all_hidden_states, attentions=all_self_attentions, )
class QWenLMHeadModel(QWenPreTrainedModel): _keys_to_ignore_on_load_missing = [r"h\.\d+\.attn\.rotary_emb\.inv_freq"] _keys_to_ignore_on_load_unexpected = [r"h\.\d+\.attn\.masked_bias"]
def __init__(self, config): super().__init__(config) assert ( config.bf16 + config.fp16 + config.fp32 <= 1 ), "Only one of \"bf16\", \"fp16\", \"fp32\" can be true"
autoset_precision = config.bf16 + config.fp16 + config.fp32 == 0
if autoset_precision: if SUPPORT_BF16: logger.warn( "The model is automatically converting to bf16 for faster inference. " "If you want to disable the automatic precision, please manually add bf16/fp16/fp32=True to \"AutoModelForCausalLM.from_pretrained\"." ) config.bf16 = True elif SUPPORT_FP16: logger.warn( "The model is automatically converting to fp16 for faster inference. " "If you want to disable the automatic precision, please manually add bf16/fp16/fp32=True to \"AutoModelForCausalLM.from_pretrained\"." ) config.fp16 = True else: config.fp32 = True
if config.bf16 and SUPPORT_CUDA and not SUPPORT_BF16: 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\".") if config.fp16 and SUPPORT_CUDA and not SUPPORT_FP16: logger.warn("Your device does NOT support faster inference with fp16, please switch to fp32 which is likely to be faster") if config.fp32: if SUPPORT_BF16: logger.warn("Your device support faster inference by passing bf16=True in \"AutoModelForCausalLM.from_pretrained\".") elif SUPPORT_FP16: logger.warn("Your device support faster inference by passing fp16=True in \"AutoModelForCausalLM.from_pretrained\".")
if config.use_flash_attn == "auto": if config.bf16 or config.fp16: logger.warn("Try importing flash-attention for faster inference...") config.use_flash_attn = True else: config.use_flash_attn = False if config.use_flash_attn and config.fp32: logger.warn("Flash attention will be disabled because it does NOT support fp32.")
if config.use_flash_attn: _import_flash_attn()
self.transformer = QWenModel(config) self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
if config.bf16: self.transformer.bfloat16() self.lm_head.bfloat16() if config.fp16: self.transformer.half() self.lm_head.half() self.post_init()
def get_output_embeddings(self): return self.lm_head
def set_output_embeddings(self, new_embeddings): self.lm_head = new_embeddings
def prepare_inputs_for_generation( self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs ): if past_key_values: input_ids = input_ids[:, -1].unsqueeze(-1)
if input_ids.size(0) == 1: attention_mask = None else: attention_mask = kwargs.get("attention_mask", None)
if inputs_embeds is not None and past_key_values is None: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids}
model_inputs.update( { "past_key_values": past_key_values, "use_cache": kwargs.get("use_cache"), "attention_mask": attention_mask, } ) return model_inputs
def forward( self, input_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple, CausalLMOutputWithPast]:
return_dict = ( return_dict if return_dict is not None else self.config.use_return_dict )
transformer_outputs = self.transformer( input_ids, past_key_values=past_key_values, attention_mask=attention_mask, token_type_ids=token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds, encoder_hidden_states=encoder_hidden_states, encoder_attention_mask=encoder_attention_mask, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, ) hidden_states = transformer_outputs[0]
lm_logits = self.lm_head(hidden_states)
loss = None if labels is not None: labels = labels.to(lm_logits.device) shift_logits = lm_logits[..., :-1, :].contiguous() shift_labels = labels[..., 1:].contiguous() loss_fct = CrossEntropyLoss() loss = loss_fct( shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1) )
if not return_dict: output = (lm_logits,) + transformer_outputs[1:] return ((loss,) + output) if loss is not None else output
return CausalLMOutputWithPast( loss=loss, logits=lm_logits, past_key_values=transformer_outputs.past_key_values, hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, )
@staticmethod def _reorder_cache( past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor ) -> Tuple[Tuple[torch.Tensor]]:
return tuple( tuple( past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past ) for layer_past in past_key_values )
def chat( self, tokenizer: PreTrainedTokenizer, query: str, history: Optional[HistoryType], system: str = "You are a helpful assistant.", stream: Optional[bool] = _SENTINEL, stop_words_ids: Optional[List[List[int]]] = None, generation_config: Optional[GenerationConfig] = None, **kwargs, ) -> Tuple[str, HistoryType]: generation_config = generation_config if generation_config is not None else self.generation_config
assert stream is _SENTINEL, _ERROR_STREAM_IN_CHAT assert generation_config.chat_format == 'chatml', _ERROR_BAD_CHAT_FORMAT if history is None: history = [] else: # make a copy of the user's input such that is is left untouched history = copy.deepcopy(history)
if stop_words_ids is None: stop_words_ids = []
max_window_size = kwargs.get('max_window_size', None) if max_window_size is None: max_window_size = generation_config.max_window_size raw_text, context_tokens = make_context( tokenizer, query, history=history, system=system, max_window_size=max_window_size, chat_format=generation_config.chat_format, )
stop_words_ids.extend(get_stop_words_ids( generation_config.chat_format, tokenizer )) input_ids = torch.tensor([context_tokens]).to(self.device) outputs = self.generate( input_ids, stop_words_ids=stop_words_ids, return_dict_in_generate=False, generation_config=generation_config, **kwargs, )
response = decode_tokens( outputs[0], tokenizer, raw_text_len=len(raw_text), context_length=len(context_tokens), chat_format=generation_config.chat_format, verbose=False, errors='replace' )
# as history is a copy of the user inputs, # we can always return the new turn to the user. # separating input history and output history also enables the user # to implement more complex history management history.append((query, response))
return response, history
def chat_stream( self, tokenizer: PreTrainedTokenizer, query: str, history: Optional[HistoryType], system: str = "You are a helpful assistant.", stop_words_ids: Optional[List[List[int]]] = None, logits_processor: Optional[LogitsProcessorList] = None, generation_config: Optional[GenerationConfig] = None, **kwargs, ) -> Generator[str, Any, None]: generation_config = generation_config if generation_config is not None else self.generation_config assert generation_config.chat_format == 'chatml', _ERROR_BAD_CHAT_FORMAT if history is None: history = [] if stop_words_ids is None: stop_words_ids = []
max_window_size = kwargs.get('max_window_size', None) if max_window_size is None: max_window_size = generation_config.max_window_size raw_text, context_tokens = make_context( tokenizer, query, history=history, system=system, max_window_size=max_window_size, chat_format=generation_config.chat_format, )
stop_words_ids.extend(get_stop_words_ids( generation_config.chat_format, tokenizer )) if stop_words_ids is not None: stop_words_logits_processor = StopWordsLogitsProcessor( stop_words_ids=stop_words_ids, eos_token_id=generation_config.eos_token_id, ) if logits_processor is None: logits_processor = LogitsProcessorList([stop_words_logits_processor]) else: logits_processor.append(stop_words_logits_processor) input_ids = torch.tensor([context_tokens]).to(self.device)
from transformers_stream_generator.main import NewGenerationMixin, StreamGenerationConfig self.__class__.generate_stream = NewGenerationMixin.generate self.__class__.sample_stream = NewGenerationMixin.sample_stream stream_config = StreamGenerationConfig(**generation_config.to_dict(), do_stream=True)
def stream_generator(): outputs = [] for token in self.generate_stream( input_ids, return_dict_in_generate=False, generation_config=stream_config, logits_processor=logits_processor, seed=-1, **kwargs): outputs.append(token.item()) yield tokenizer.decode(outputs, skip_special_tokens=True, errors='ignore')
return stream_generator()
def generate( self, inputs: Optional[torch.Tensor] = None, generation_config: Optional[GenerationConfig] = None, logits_processor: Optional[LogitsProcessorList] = None, stopping_criteria: Optional[StoppingCriteriaList] = None, prefix_allowed_tokens_fn: Optional[ Callable[[int, torch.Tensor], List[int]] ] = None, synced_gpus: Optional[bool] = None, assistant_model: Optional["PreTrainedModel"] = None, streamer: Optional["BaseStreamer"] = None, **kwargs, ) -> Union[GenerateOutput, torch.LongTensor]: generation_config = generation_config if generation_config is not None else self.generation_config
# Process stop_words_ids. stop_words_ids = kwargs.pop("stop_words_ids", None) if stop_words_ids is None and generation_config is not None: stop_words_ids = getattr(generation_config, "stop_words_ids", None) if stop_words_ids is None: stop_words_ids = getattr(generation_config, "stop_words_ids", None)
if stop_words_ids is not None: stop_words_logits_processor = StopWordsLogitsProcessor( stop_words_ids=stop_words_ids, eos_token_id=generation_config.eos_token_id, ) if logits_processor is None: logits_processor = LogitsProcessorList([stop_words_logits_processor]) else: logits_processor.append(stop_words_logits_processor)
return super().generate( inputs, generation_config=generation_config, logits_processor=logits_processor, stopping_criteria=stopping_criteria, prefix_allowed_tokens_fn=prefix_allowed_tokens_fn, synced_gpus=synced_gpus, assistant_model=assistant_model, streamer=streamer, **kwargs, )
class RotaryEmbedding(torch.nn.Module): def __init__(self, dim, base=10000): super().__init__() self.dim = dim self.base = base inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer("inv_freq", inv_freq, persistent=False) if importlib.util.find_spec("einops") is None: raise RuntimeError("einops is required for Rotary Embedding")
self._rotary_pos_emb_cache = None self._seq_len_cached = 0 self._ntk_alpha_cached = 1.0 self._ntk_alpha_cached_list = [1.0]
def update_rotary_pos_emb_cache(self, seqlen, ntk_alpha=1.0): if seqlen > self._seq_len_cached or ntk_alpha != self._ntk_alpha_cached: base = self.base * ntk_alpha ** (self.dim / (self.dim - 2)) self.inv_freq = 1.0 / ( base ** ( torch.arange(0, self.dim, 2, device=self.inv_freq.device).float() / self.dim ) ) self._seq_len_cached = max(2 * seqlen, 16) self._ntk_alpha_cached = ntk_alpha seq = torch.arange(self._seq_len_cached, device=self.inv_freq.device) freqs = torch.outer(seq.type_as(self.inv_freq), self.inv_freq)
emb = torch.cat((freqs, freqs), dim=-1) from einops import rearrange
emb = rearrange(emb, "n d -> 1 n 1 d")
cos, sin = emb.cos(), emb.sin() self._rotary_pos_emb_cache = [cos, sin]
def forward(self, max_seq_len, ntk_alpha=1.0): self.update_rotary_pos_emb_cache(max_seq_len, ntk_alpha) cos, sin = self._rotary_pos_emb_cache return [cos[:, :max_seq_len], sin[:, :max_seq_len]]
def _rotate_half(x): from einops import rearrange
x = rearrange(x, "... (j d) -> ... j d", j=2) x1, x2 = x.unbind(dim=-2) return torch.cat((-x2, x1), dim=-1)
def apply_rotary_pos_emb(t, freqs): """ Apply rotary embedding to the first rotary_dim of the iput
Arguments: t (tensor(batch_size, seq_len, n_head, head_dim)): the input embedding/hidden states freqs (list[tensor(1, seq_len, 1, rotary_dim), tensor(1, seq_len, 1, rotary_dim)]): the cached cos/sin position embeddings """ rot_dim = freqs[0].shape[-1] cos, sin = freqs t_float = t.float() if apply_rotary_emb_func is not None and t.is_cuda: # apply_rotary_emb in flash_attn requires cos/sin to be of # shape (seqlen, rotary_dim / 2) and apply rotary embedding # to the first rotary_dim of the input cos = cos.squeeze(0).squeeze(1)[:, : rot_dim // 2] sin = sin.squeeze(0).squeeze(1)[:, : rot_dim // 2] return apply_rotary_emb_func(t_float, cos, sin).type_as(t) else: t_rot, t_pass = t_float[..., :rot_dim], t_float[..., rot_dim:] t_rot = (t_rot * cos) + (_rotate_half(t_rot) * sin) return torch.cat((t_rot, t_pass), dim=-1).type_as(t)
class RMSNorm(torch.nn.Module): def __init__(self, dim: int, eps: float = 1e-6): super().__init__() self.eps = eps self.weight = nn.Parameter(torch.ones(dim))
def _norm(self, x): return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x): if rms_norm is not None and x.is_cuda: return rms_norm(x, self.weight, self.eps) else: output = self._norm(x.float()).type_as(x) return output * self.weight123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416# Copyright (c) Alibaba Cloud.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
"""Generation support."""
from typing import Tuple, List, Union, Iterable
import numpy as npimport torchimport torch.nn.functional as Ffrom transformers import PreTrainedTokenizerfrom transformers import loggingfrom transformers.generation import LogitsProcessor
logger = logging.get_logger(__name__)
# Types.HistoryType = List[Tuple[str, str]]TokensType = List[int]BatchTokensType = List[List[int]]
def pad_batch(batch: BatchTokensType, pad_id: int, seq_length: int) -> BatchTokensType: for tokens in batch: context_length = len(tokens) if context_length < seq_length: tokens.extend([pad_id] * (seq_length - context_length)) return batch
def get_ltor_masks_and_position_ids( data, eod_token, reset_position_ids, reset_attention_mask, eod_mask_loss,): """Build masks and position id for left to right model."""
# Extract batch size and sequence length. micro_batch_size, seq_length = data.size()
# Attention mask (lower triangular). if reset_attention_mask: att_mask_batch = micro_batch_size else: att_mask_batch = 1 attention_mask = torch.tril( torch.ones((att_mask_batch, seq_length, seq_length), device=data.device) ).view(att_mask_batch, 1, seq_length, seq_length)
# Loss mask. loss_mask = torch.ones(data.size(), dtype=torch.float, device=data.device) if eod_mask_loss: loss_mask[data == eod_token] = 0.0
# Position ids. position_ids = torch.arange(seq_length, dtype=torch.long, device=data.device) position_ids = position_ids.unsqueeze(0).expand_as(data) # We need to clone as the ids will be modifed based on batch index. if reset_position_ids: position_ids = position_ids.clone()
if reset_position_ids or reset_attention_mask: # Loop through the batches: for b in range(micro_batch_size):
# Find indecies where EOD token is. eod_index = position_ids[b, data[b] == eod_token] # Detach indecies from positions if going to modify positions. if reset_position_ids: eod_index = eod_index.clone()
# Loop through EOD indecies: prev_index = 0 for j in range(eod_index.size()[0]): i = eod_index[j] # Mask attention loss. if reset_attention_mask: attention_mask[b, 0, (i + 1) :, : (i + 1)] = 0 # Reset positions. if reset_position_ids: position_ids[b, (i + 1) :] -= i + 1 - prev_index prev_index = i + 1
# Convert attention mask to binary: attention_mask = attention_mask < 0.5
return attention_mask, loss_mask, position_ids
def get_batch(context_tokens: torch.LongTensor, eod_id: int): """Generate batch from context tokens.""" # Move to GPU. tokens = context_tokens.contiguous().to(context_tokens.device) # Get the attention mask and postition ids. attention_mask, _, position_ids = get_ltor_masks_and_position_ids( tokens, eod_id, reset_position_ids=False, reset_attention_mask=False, eod_mask_loss=False, ) return tokens, attention_mask, position_ids
def get_stop_words_ids(chat_format, tokenizer): if chat_format == "raw": stop_words_ids = [tokenizer.encode("Human:"), [tokenizer.eod_id]] elif chat_format == "chatml": stop_words_ids = [[tokenizer.im_end_id], [tokenizer.im_start_id]] else: raise NotImplementedError(f"Unknown chat format {chat_format!r}") return stop_words_ids
def make_context( tokenizer: PreTrainedTokenizer, query: str, history: List[Tuple[str, str]] = None, system: str = "", max_window_size: int = 6144, chat_format: str = "chatml",): if history is None: history = []
if chat_format == "chatml": im_start, im_end = "<|im_start|>", "<|im_end|>" im_start_tokens = [tokenizer.im_start_id] im_end_tokens = [tokenizer.im_end_id] nl_tokens = tokenizer.encode("\n")
def _tokenize_str(role, content): return f"{role}\n{content}", tokenizer.encode( role, allowed_special=set() ) + nl_tokens + tokenizer.encode(content, allowed_special=set())
system_text, system_tokens_part = _tokenize_str("system", system) system_tokens = im_start_tokens + system_tokens_part + im_end_tokens
raw_text = "" context_tokens = []
for turn_query, turn_response in reversed(history): query_text, query_tokens_part = _tokenize_str("user", turn_query) query_tokens = im_start_tokens + query_tokens_part + im_end_tokens response_text, response_tokens_part = _tokenize_str( "assistant", turn_response ) response_tokens = im_start_tokens + response_tokens_part + im_end_tokens
next_context_tokens = nl_tokens + query_tokens + nl_tokens + response_tokens prev_chat = ( f"\n{im_start}{query_text}{im_end}\n{im_start}{response_text}{im_end}" )
current_context_size = ( len(system_tokens) + len(next_context_tokens) + len(context_tokens) ) if current_context_size < max_window_size: context_tokens = next_context_tokens + context_tokens raw_text = prev_chat + raw_text else: break
context_tokens = system_tokens + context_tokens raw_text = f"{im_start}{system_text}{im_end}" + raw_text context_tokens += ( nl_tokens + im_start_tokens + _tokenize_str("user", query)[1] + im_end_tokens + nl_tokens + im_start_tokens + tokenizer.encode("assistant") + nl_tokens ) raw_text += f"\n{im_start}user\n{query}{im_end}\n{im_start}assistant\n"
elif chat_format == "raw": raw_text = query context_tokens = tokenizer.encode(raw_text) else: raise NotImplementedError(f"Unknown chat format {chat_format!r}")
return raw_text, context_tokens
def _decode_default( tokens: List[int], *, stop_words: List[str], eod_words: List[str], tokenizer: PreTrainedTokenizer, raw_text_len: int, verbose: bool = False, return_end_reason: bool = False, errors: str='replace',): trim_decode_tokens = tokenizer.decode(tokens, errors=errors)[raw_text_len:] if verbose: print("\nRaw Generate: ", trim_decode_tokens)
end_reason = f"Gen length {len(tokens)}" for stop_word in stop_words: trim_decode_tokens = trim_decode_tokens.replace(stop_word, "").strip() for eod_word in eod_words: if eod_word in trim_decode_tokens: end_reason = f"Gen {eod_word!r}" trim_decode_tokens = trim_decode_tokens.split(eod_word)[0] trim_decode_tokens = trim_decode_tokens.strip() if verbose: print("\nEnd Reason:", end_reason) print("\nGenerate: ", trim_decode_tokens)
if return_end_reason: return trim_decode_tokens, end_reason else: return trim_decode_tokens
def _decode_chatml( tokens: List[int], *, stop_words: List[str], eod_token_ids: List[int], tokenizer: PreTrainedTokenizer, raw_text_len: int, context_length: int, verbose: bool = False, return_end_reason: bool = False, errors: str='replace'): end_reason = f"Gen length {len(tokens)}" eod_token_idx = context_length for eod_token_idx in range(context_length, len(tokens)): if tokens[eod_token_idx] in eod_token_ids: end_reason = f"Gen {tokenizer.decode([tokens[eod_token_idx]])!r}" break
trim_decode_tokens = tokenizer.decode(tokens[:eod_token_idx], errors=errors)[raw_text_len:] if verbose: print("\nRaw Generate w/o EOD:", tokenizer.decode(tokens, errors=errors)[raw_text_len:]) print("\nRaw Generate:", trim_decode_tokens) print("\nEnd Reason:", end_reason) for stop_word in stop_words: trim_decode_tokens = trim_decode_tokens.replace(stop_word, "").strip() trim_decode_tokens = trim_decode_tokens.strip() if verbose: print("\nGenerate:", trim_decode_tokens)
if return_end_reason: return trim_decode_tokens, end_reason else: return trim_decode_tokens
def decode_tokens( tokens: Union[torch.LongTensor, TokensType], tokenizer: PreTrainedTokenizer, raw_text_len: int, context_length: int, chat_format: str, verbose: bool = False, return_end_reason: bool = False, errors: str="replace",) -> str: if torch.is_tensor(tokens): tokens = tokens.cpu().numpy().tolist()
if chat_format == "chatml": return _decode_chatml( tokens, stop_words=[], eod_token_ids=[tokenizer.im_start_id, tokenizer.im_end_id], tokenizer=tokenizer, raw_text_len=raw_text_len, context_length=context_length, verbose=verbose, return_end_reason=return_end_reason, errors=errors, ) elif chat_format == "raw": return _decode_default( tokens, stop_words=["<|endoftext|>"], eod_words=["<|endoftext|>"], tokenizer=tokenizer, raw_text_len=raw_text_len, verbose=verbose, return_end_reason=return_end_reason, errors=errors, ) else: raise NotImplementedError(f"Unknown chat format {chat_format!r}")
class StopWordsLogitsProcessor(LogitsProcessor): """ :class:`transformers.LogitsProcessor` that enforces that when specified sequences appear, stop geration.
Args: stop_words_ids (:obj:`List[List[int]]`): List of list of token ids of stop ids. In order to get the tokens of the words that should not appear in the generated text, use :obj:`tokenizer(bad_word, add_prefix_space=True).input_ids`. eos_token_id (:obj:`int`): The id of the `end-of-sequence` token. """
def __init__(self, stop_words_ids: Iterable[Iterable[int]], eos_token_id: int):
if not isinstance(stop_words_ids, List) or len(stop_words_ids) == 0: raise ValueError( f"`stop_words_ids` has to be a non-emtpy list, but is {stop_words_ids}." ) if any(not isinstance(bad_word_ids, list) for bad_word_ids in stop_words_ids): raise ValueError( f"`stop_words_ids` has to be a list of lists, but is {stop_words_ids}." ) if any( any( (not isinstance(token_id, (int, np.integer)) or token_id < 0) for token_id in stop_word_ids ) for stop_word_ids in stop_words_ids ): raise ValueError( f"Each list in `stop_words_ids` has to be a list of positive integers, but is {stop_words_ids}." )
self.stop_words_ids = list( filter( lambda bad_token_seq: bad_token_seq != [eos_token_id], stop_words_ids ) ) self.eos_token_id = eos_token_id for stop_token_seq in self.stop_words_ids: assert ( len(stop_token_seq) > 0 ), "Stop words token sequences {} cannot have an empty list".format( stop_words_ids )
def __call__( self, input_ids: torch.LongTensor, scores: torch.FloatTensor ) -> torch.FloatTensor: stopped_samples = self._calc_stopped_samples(input_ids) for i, should_stop in enumerate(stopped_samples): if should_stop: scores[i, self.eos_token_id] = float(2**15) return scores
def _tokens_match(self, prev_tokens: torch.LongTensor, tokens: List[int]) -> bool: if len(tokens) == 0: # if bad word tokens is just one token always ban it return True elif len(tokens) > len(prev_tokens): # if bad word tokens are longer then prev input_ids they can't be equal return False elif prev_tokens[-len(tokens) :].tolist() == tokens: # if tokens match return True else: return False
def _calc_stopped_samples(self, prev_input_ids: Iterable[int]) -> Iterable[int]: stopped_samples = [] for prev_input_ids_slice in prev_input_ids: match = False for stop_token_seq in self.stop_words_ids: if self._tokens_match(prev_input_ids_slice, stop_token_seq): # if tokens do not match continue match = True break stopped_samples.append(match)
return stopped_samples
def top_k_logits(logits, top_k=0, top_p=0.0, filter_value=-float("Inf")): """This function has been mostly taken from huggingface conversational ai code at https://medium.com/huggingface/how-to-build-a-state-of-the-art- conversational-ai-with-transfer-learning-2d818ac26313"""
if top_k > 0: # Remove all tokens with a probability less than the # last token of the top-k indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None] logits[indices_to_remove] = filter_value
if top_p > 0.0: # Cconvert to 1D sorted_logits, sorted_indices = torch.sort(logits, descending=True, dim=-1) cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
# Remove tokens with cumulative probability above the threshold sorted_indices_to_remove = cumulative_probs > top_p # Shift the indices to the right to keep also the first token # above the threshold sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone() sorted_indices_to_remove[..., 0] = 0 for i in range(sorted_indices.size(0)): indices_to_remove = sorted_indices[i][sorted_indices_to_remove[i]] logits[i][indices_to_remove] = filter_value
return logits
def switch(val1, val2, boolean): boolean = boolean.type_as(val1) return (1 - boolean) * val1 + boolean * val21{}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276# Copyright (c) Alibaba Cloud.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
"""Tokenization classes for QWen."""
import base64import loggingimport osimport unicodedatafrom typing import Collection, Dict, List, Set, Tuple, Union
import tiktokenfrom transformers import PreTrainedTokenizer, AddedToken
logger = logging.getLogger(__name__)
VOCAB_FILES_NAMES = {"vocab_file": "qwen.tiktoken"}
PAT_STR = r"""(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"""ENDOFTEXT = "<|endoftext|>"IMSTART = "<|im_start|>"IMEND = "<|im_end|>"# as the default behavior is changed to allow special tokens in# regular texts, the surface forms of special tokens need to be# as different as possible to minimize the impactEXTRAS = tuple((f"<|extra_{i}|>" for i in range(205)))# changed to use actual index to avoid misconfiguration with vocabulary expansionSPECIAL_START_ID = 151643SPECIAL_TOKENS = tuple( enumerate( ( ( ENDOFTEXT, IMSTART, IMEND, ) + EXTRAS ), start=SPECIAL_START_ID, ))SPECIAL_TOKENS_SET = set(t for i, t in SPECIAL_TOKENS)
def _load_tiktoken_bpe(tiktoken_bpe_file: str) -> Dict[bytes, int]: with open(tiktoken_bpe_file, "rb") as f: contents = f.read() return { base64.b64decode(token): int(rank) for token, rank in (line.split() for line in contents.splitlines() if line) }
class QWenTokenizer(PreTrainedTokenizer): """QWen tokenizer."""
vocab_files_names = VOCAB_FILES_NAMES
def __init__( self, vocab_file, errors="replace", extra_vocab_file=None, **kwargs, ): super().__init__(**kwargs)
# how to handle errors in decoding UTF-8 byte sequences # use ignore if you are in streaming inference self.errors = errors
self.mergeable_ranks = _load_tiktoken_bpe(vocab_file) # type: Dict[bytes, int] self.special_tokens = { token: index for index, token in SPECIAL_TOKENS }
# try load extra vocab from file if extra_vocab_file is not None: used_ids = set(self.mergeable_ranks.values()) | set(self.special_tokens.values()) extra_mergeable_ranks = _load_tiktoken_bpe(extra_vocab_file) for token, index in extra_mergeable_ranks.items(): if token in self.mergeable_ranks: logger.info(f"extra token {token} exists, skipping") continue if index in used_ids: logger.info(f'the index {index} for extra token {token} exists, skipping') continue self.mergeable_ranks[token] = index # the index may be sparse after this, but don't worry tiktoken.Encoding will handle this
enc = tiktoken.Encoding( "Qwen", pat_str=PAT_STR, mergeable_ranks=self.mergeable_ranks, special_tokens=self.special_tokens, ) assert ( len(self.mergeable_ranks) + len(self.special_tokens) == enc.n_vocab ), f"{len(self.mergeable_ranks) + len(self.special_tokens)} != {enc.n_vocab} in encoding"
self.decoder = { v: k for k, v in self.mergeable_ranks.items() } # type: dict[int, bytes|str] self.decoder.update({v: k for k, v in self.special_tokens.items()})
self.tokenizer = enc # type: tiktoken.Encoding
self.eod_id = self.tokenizer.eot_token self.im_start_id = self.special_tokens[IMSTART] self.im_end_id = self.special_tokens[IMEND]
def __getstate__(self): # for pickle lovers state = self.__dict__.copy() del state["tokenizer"] return state
def __setstate__(self, state): # tokenizer is not python native; don't pass it; rebuild it self.__dict__.update(state) enc = tiktoken.Encoding( "Qwen", pat_str=PAT_STR, mergeable_ranks=self.mergeable_ranks, special_tokens=self.special_tokens, ) self.tokenizer = enc
def __len__(self) -> int: return self.tokenizer.n_vocab
def get_vocab(self) -> Dict[bytes, int]: return self.mergeable_ranks
def convert_tokens_to_ids( self, tokens: Union[bytes, str, List[Union[bytes, str]]] ) -> List[int]: ids = [] if isinstance(tokens, (str, bytes)): if tokens in self.special_tokens: return self.special_tokens[tokens] else: return self.mergeable_ranks.get(tokens) for token in tokens: if token in self.special_tokens: ids.append(self.special_tokens[token]) else: ids.append(self.mergeable_ranks.get(token)) return ids
def _add_tokens( self, new_tokens: Union[List[str], List[AddedToken]], special_tokens: bool = False, ) -> int: if not special_tokens and new_tokens: raise ValueError("Adding regular tokens is not supported") for token in new_tokens: surface_form = token.content if isinstance(token, AddedToken) else token if surface_form not in SPECIAL_TOKENS_SET: raise ValueError("Adding unknown special tokens is not supported") return 0
def save_vocabulary(self, save_directory: str, **kwargs) -> Tuple[str]: """ Save only the vocabulary of the tokenizer (vocabulary).
Returns: `Tuple(str)`: Paths to the files saved. """ file_path = os.path.join(save_directory, "qwen.tiktoken") with open(file_path, "w", encoding="utf8") as w: for k, v in self.mergeable_ranks.items(): line = base64.b64encode(k).decode("utf8") + " " + str(v) + "\n" w.write(line) return (file_path,)
def tokenize( self, text: str, allowed_special: Union[Set, str] = "all", disallowed_special: Union[Collection, str] = (), **kwargs, ) -> List[Union[bytes, str]]: """ Converts a string in a sequence of tokens.
Args: text (`str`): The sequence to be encoded. allowed_special (`Literal["all"]` or `set`): The surface forms of the tokens to be encoded as special tokens in regular texts. Default to "all". disallowed_special (`Literal["all"]` or `Collection`): The surface forms of the tokens that should not be in regular texts and trigger errors. Default to an empty tuple.
kwargs (additional keyword arguments, *optional*): Will be passed to the underlying model specific encode method.
Returns: `List[bytes|str]`: The list of tokens. """ tokens = [] text = unicodedata.normalize("NFC", text)
# this implementation takes a detour: text -> token id -> token surface forms for t in self.tokenizer.encode( text, allowed_special=allowed_special, disallowed_special=disallowed_special ): tokens.append(self.decoder[t]) return tokens
def convert_tokens_to_string(self, tokens: List[Union[bytes, str]]) -> str: """ Converts a sequence of tokens in a single string. """ text = "" temp = b"" for t in tokens: if isinstance(t, str): if temp: text += temp.decode("utf-8", errors=self.errors) temp = b"" text += t elif isinstance(t, bytes): temp += t else: raise TypeError("token should only be of type types or str") if temp: text += temp.decode("utf-8", errors=self.errors) return text
@property def vocab_size(self): return self.tokenizer.n_vocab
def _convert_id_to_token(self, index: int) -> Union[bytes, str]: """Converts an id to a token, special tokens included""" if index in self.decoder: return self.decoder[index] raise ValueError("unknown ids")
def _convert_token_to_id(self, token: Union[bytes, str]) -> int: """Converts a token to an id using the vocab, special tokens included""" if token in self.special_tokens: return self.special_tokens[token] if token in self.mergeable_ranks: return self.mergeable_ranks[token] raise ValueError("unknown token")
def _tokenize(self, text: str, **kwargs): """ Converts a string in a sequence of tokens (string), using the tokenizer. Split in words for word-based vocabulary or sub-words for sub-word-based vocabularies (BPE/SentencePieces/WordPieces).
Do NOT take care of added tokens. """ raise NotImplementedError
def _decode( self, token_ids: Union[int, List[int]], skip_special_tokens: bool = False, errors: str = None, **kwargs, ) -> str: if isinstance(token_ids, int): token_ids = [token_ids] if skip_special_tokens: token_ids = [i for i in token_ids if i < self.eod_id] return self.tokenizer.decode(token_ids, errors=errors or self.errors)1234567891011121314{ "added_tokens_decoder": {}, "auto_map": { "AutoTokenizer": [ "tokenization_qwen.QWenTokenizer", null ] }, "clean_up_tokenization_spaces": true, "model_max_length": 8192, "padding_side": "left", "split_special_tokens": false, "tokenizer_class": "QWenTokenizer"}config.jsonAdded+43
configuration_qwen.pyAdded+71
cpp_kernels.pyAdded+55
generation_config.jsonAdded+12
model-00001-of-00029.safetensorsAdded+3
model-00002-of-00029.safetensorsAdded+3
model-00003-of-00029.safetensorsAdded+3
model-00004-of-00029.safetensorsAdded+3
model-00005-of-00029.safetensorsAdded+3
model-00006-of-00029.safetensorsAdded+3
model-00007-of-00029.safetensorsAdded+3
model-00008-of-00029.safetensorsAdded+3
model-00009-of-00029.safetensorsAdded+3
model-00010-of-00029.safetensorsAdded+3
model-00011-of-00029.safetensorsAdded+3
model-00012-of-00029.safetensorsAdded+3
model-00013-of-00029.safetensorsAdded+3
model-00014-of-00029.safetensorsAdded+3
model-00015-of-00029.safetensorsAdded+3
model-00016-of-00029.safetensorsAdded+3
model-00017-of-00029.safetensorsAdded+3
model-00018-of-00029.safetensorsAdded+3
model-00019-of-00029.safetensorsAdded+3
model-00020-of-00029.safetensorsAdded+3
model-00021-of-00029.safetensorsAdded+3
model-00022-of-00029.safetensorsAdded+3
model-00023-of-00029.safetensorsAdded+3
model-00024-of-00029.safetensorsAdded+3
model-00025-of-00029.safetensorsAdded+3
model-00026-of-00029.safetensorsAdded+3
model-00027-of-00029.safetensorsAdded+3
model-00028-of-00029.safetensorsAdded+3
model-00029-of-00029.safetensorsAdded+3
model.safetensors.index.jsonAdded+330
modeling_qwen.pyAdded+1363
qwen.tiktokenAdded
qwen_generation_utils.pyAdded+416
special_tokens_map.jsonAdded+1
tokenization_qwen.pyAdded+276
tokenizer_config.jsonAdded+14