Upload 14 files
Browse filesFiles changed (14)
- chat_template.jinjaAdded+1
- config.jsonAdded+31
- configuration_openpangu_dense.pyAdded+56
- generation_config.jsonAdded+10
- model-00001-of-00004.safetensorsAdded+3
- model-00002-of-00004.safetensorsAdded+3
- model-00003-of-00004.safetensorsAdded+3
- model-00004-of-00004.safetensorsAdded+3
- model.safetensors.index.jsonAdded+453
- modeling_openpangu_dense.pyAdded+588
- special_tokens_map.jsonAdded+30
- tokenization_openpangu.pyAdded+273
- tokenizer.modelAdded+3
- tokenizer_config.jsonAdded+334
1{% for message in messages %}{% if loop.first and messages[0]['role'] != 'system' %}{{ '[unused9]系统:[unused10]' }}{% endif %}{% if message['role'] == 'system' %}{{ '[unused9]系统:' + message['content'] + '[unused10]' }}{% endif %}{% if message['role'] == 'assistant' %}{{'[unused9]助手:' + message['content'] + '[unused10]'}}{% endif %}{% if message['role'] == 'tool' %}{{'[unused9]工具:' + message['content'] + '[unused10]'}}{% endif %}{% if message['role'] == 'function' %}{{'[unused9]方法:' + message['content'] + '[unused10]'}}{% endif %}{% if message['role'] == 'user' %}{{'[unused9]用户:' + message['content'] + '[unused10]'}}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ '[unused9]助手:' }}{% endif %}12345678910111213141516171819202122232425262728293031{ "architectures": [ "PanguEmbeddedForCausalLM" ], "attention_dropout": 0.0, "auto_map": { "AutoConfig": "configuration_openpangu_dense.PanguEmbeddedConfig", "AutoModel": "modeling_openpangu_dense.PanguEmbeddedModel", "AutoModelForCausalLM": "modeling_openpangu_dense.PanguEmbeddedForCausalLM" }, "bias": true, "bos_token_id": 1, "eos_token_id": 45892, "hidden_act": "silu", "hidden_size": 4096, "initializer_range": 0.02, "intermediate_size": 12800, "max_position_embeddings": 32768, "model_type": "PanguEmbedded", "num_attention_heads": 32, "num_hidden_layers": 34, "num_key_value_heads": 8, "pad_token_id": 0, "rms_norm_eps": 1e-05, "rope_theta": 16000000.0, "tie_word_embeddings": false, "torch_dtype": "bfloat16", "transformers_version": "4.53.2", "use_cache": true, "vocab_size": 153376}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556# coding=utf-8# Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
from transformers.utils import loggingfrom transformers.configuration_utils import PretrainedConfig
logger = logging.get_logger(__name__)
class PanguEmbeddedConfig(PretrainedConfig):
model_type = "PanguEmbedded" _auto_class = "AutoConfig"
def __init__( self, vocab_size=153376, hidden_size=4096, intermediate_size=12800, num_hidden_layers=34, num_attention_heads=32, num_key_value_heads=8, hidden_act="silu", max_position_embeddings=32768, initializer_range=0.02, rms_norm_eps=1e-5, use_cache=True, pad_token_id=0, bos_token_id=1, eos_token_id=45892, tie_word_embeddings=False, rope_theta=16000000.0, bias=True, **kwargs, ): self.vocab_size = vocab_size self.max_position_embeddings = max_position_embeddings 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.num_key_value_heads = num_key_value_heads self.hidden_act = hidden_act self.initializer_range = initializer_range self.rms_norm_eps = rms_norm_eps self.use_cache = use_cache self.rope_theta = rope_theta self.bias = bias super().__init__( pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, tie_word_embeddings=tie_word_embeddings, **kwargs, )12345678910{ "_from_model_config": true, "bos_token_id": 1, "do_sample": true, "eos_token_id": 45892, "pad_token_id": 0, "top_k": 0, "top_p": 0.8, "transformers_version": "4.53.2"}123version https://git-lfs.github.com/spec/v1oid sha256:34d46e5873c9342b47ca9a5897107643ff0a886c2aed5834cffc2fedc8ecf665size 4926838832123version https://git-lfs.github.com/spec/v1oid sha256:68de09f8440162ec209b6b39a63f0e783655d0c4edc48def37ea2b0c9a669803size 4991682264123version https://git-lfs.github.com/spec/v1oid sha256:9e859f0c8ba998abcb8dabe593b39bd061202577ff82b44473bb707cbcdbb4fbsize 4886849448123version https://git-lfs.github.com/spec/v1oid sha256:52ae62d754a63d2fcd8d1f40be1332dd161a2105ff7c43622328ef3800d4d891size 1256456320123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453{ "metadata": { "total_parameters": 8030887936, "total_size": 16061775872 }, "weight_map": { "lm_head.weight": "model-00004-of-00004.safetensors", "model.embed_tokens.weight": "model-00001-of-00004.safetensors", "model.layers.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.0.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.0.mlp.gate_proj.weight": "model-00001-of-00004.safetensors", "model.layers.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.0.post_attention_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.0.self_attn.k_proj.bias": "model-00001-of-00004.safetensors", "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00004.safetensors", "model.layers.0.self_attn.o_proj.bias": "model-00001-of-00004.safetensors", "model.layers.0.self_attn.o_proj.weight": "model-00001-of-00004.safetensors", "model.layers.0.self_attn.q_proj.bias": "model-00001-of-00004.safetensors", "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00004.safetensors", "model.layers.0.self_attn.v_proj.bias": "model-00001-of-00004.safetensors", "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00004.safetensors", "model.layers.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.1.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.1.mlp.gate_proj.weight": "model-00001-of-00004.safetensors", "model.layers.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.1.post_attention_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.1.self_attn.k_proj.bias": "model-00001-of-00004.safetensors", "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00004.safetensors", "model.layers.1.self_attn.o_proj.bias": "model-00001-of-00004.safetensors", "model.layers.1.self_attn.o_proj.weight": "model-00001-of-00004.safetensors", "model.layers.1.self_attn.q_proj.bias": "model-00001-of-00004.safetensors", "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00004.safetensors", "model.layers.1.self_attn.v_proj.bias": "model-00001-of-00004.safetensors", "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00004.safetensors", "model.layers.10.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.10.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.10.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.10.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.10.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.10.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.10.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.10.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.10.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.10.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.10.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.10.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.10.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.11.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.11.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.11.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.11.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.11.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.11.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.11.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.11.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.11.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.11.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.11.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.11.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.11.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.12.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.12.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.12.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.12.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.12.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.12.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.12.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.12.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.12.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.12.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.12.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.12.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.12.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.13.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.13.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.13.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.13.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.13.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.13.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.13.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.13.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.13.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.13.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.13.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.13.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.13.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.14.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.14.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.14.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.14.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.14.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.14.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.14.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.14.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.14.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.14.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.14.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.14.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.14.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.15.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.15.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.15.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.15.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.15.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.15.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.15.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.15.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.15.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.15.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.15.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.15.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.15.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.16.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.16.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.16.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.16.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.16.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.16.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.17.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.17.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.17.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.17.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.17.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.17.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.18.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.18.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.18.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.18.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.18.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.18.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.19.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.19.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.19.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.19.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.19.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.19.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.2.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.2.mlp.gate_proj.weight": "model-00001-of-00004.safetensors", "model.layers.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.2.post_attention_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.2.self_attn.k_proj.bias": "model-00001-of-00004.safetensors", "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00004.safetensors", "model.layers.2.self_attn.o_proj.bias": "model-00001-of-00004.safetensors", "model.layers.2.self_attn.o_proj.weight": "model-00001-of-00004.safetensors", "model.layers.2.self_attn.q_proj.bias": "model-00001-of-00004.safetensors", "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00004.safetensors", "model.layers.2.self_attn.v_proj.bias": "model-00001-of-00004.safetensors", "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00004.safetensors", "model.layers.20.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.20.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.20.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.20.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.20.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.20.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.20.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.21.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.21.self_attn.k_proj.bias": "model-00002-of-00004.safetensors", "model.layers.21.self_attn.k_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.self_attn.o_proj.bias": "model-00002-of-00004.safetensors", "model.layers.21.self_attn.o_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.self_attn.q_proj.bias": "model-00002-of-00004.safetensors", "model.layers.21.self_attn.q_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.self_attn.v_proj.bias": "model-00002-of-00004.safetensors", "model.layers.21.self_attn.v_proj.weight": "model-00002-of-00004.safetensors", "model.layers.22.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.22.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.22.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.22.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.22.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.22.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.22.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.23.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.23.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.23.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.23.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.23.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.23.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.24.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.24.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.24.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.24.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.24.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.24.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.25.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.25.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.25.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.25.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.25.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.25.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.26.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.26.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.26.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.26.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.26.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.26.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.27.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.27.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.27.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.27.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.27.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.27.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.28.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.28.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.28.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.28.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.28.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.28.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.29.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.29.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.29.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.29.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.29.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.29.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.3.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.3.mlp.gate_proj.weight": "model-00001-of-00004.safetensors", "model.layers.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.3.post_attention_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.3.self_attn.k_proj.bias": "model-00001-of-00004.safetensors", "model.layers.3.self_attn.k_proj.weight": "model-00001-of-00004.safetensors", "model.layers.3.self_attn.o_proj.bias": "model-00001-of-00004.safetensors", "model.layers.3.self_attn.o_proj.weight": "model-00001-of-00004.safetensors", "model.layers.3.self_attn.q_proj.bias": "model-00001-of-00004.safetensors", "model.layers.3.self_attn.q_proj.weight": "model-00001-of-00004.safetensors", "model.layers.3.self_attn.v_proj.bias": "model-00001-of-00004.safetensors", "model.layers.3.self_attn.v_proj.weight": "model-00001-of-00004.safetensors", "model.layers.30.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.30.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.30.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.30.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.30.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.30.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.30.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.31.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.31.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.31.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.31.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.31.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.31.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.32.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.32.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.32.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.32.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.32.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.32.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.32.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.32.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.32.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.32.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.32.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.32.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.32.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.33.input_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.33.mlp.down_proj.weight": "model-00003-of-00004.safetensors", "model.layers.33.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.33.mlp.up_proj.weight": "model-00003-of-00004.safetensors", "model.layers.33.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.33.self_attn.k_proj.bias": "model-00003-of-00004.safetensors", "model.layers.33.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.33.self_attn.o_proj.bias": "model-00003-of-00004.safetensors", "model.layers.33.self_attn.o_proj.weight": "model-00003-of-00004.safetensors", "model.layers.33.self_attn.q_proj.bias": "model-00003-of-00004.safetensors", "model.layers.33.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.33.self_attn.v_proj.bias": "model-00003-of-00004.safetensors", "model.layers.33.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.4.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.4.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.4.mlp.gate_proj.weight": "model-00001-of-00004.safetensors", "model.layers.4.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.4.post_attention_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.4.self_attn.k_proj.bias": "model-00001-of-00004.safetensors", "model.layers.4.self_attn.k_proj.weight": "model-00001-of-00004.safetensors", "model.layers.4.self_attn.o_proj.bias": "model-00001-of-00004.safetensors", "model.layers.4.self_attn.o_proj.weight": "model-00001-of-00004.safetensors", "model.layers.4.self_attn.q_proj.bias": "model-00001-of-00004.safetensors", "model.layers.4.self_attn.q_proj.weight": "model-00001-of-00004.safetensors", "model.layers.4.self_attn.v_proj.bias": "model-00001-of-00004.safetensors", "model.layers.4.self_attn.v_proj.weight": "model-00001-of-00004.safetensors", "model.layers.5.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.5.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.5.mlp.gate_proj.weight": "model-00001-of-00004.safetensors", "model.layers.5.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.5.post_attention_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.5.self_attn.k_proj.bias": "model-00001-of-00004.safetensors", "model.layers.5.self_attn.k_proj.weight": "model-00001-of-00004.safetensors", "model.layers.5.self_attn.o_proj.bias": "model-00001-of-00004.safetensors", "model.layers.5.self_attn.o_proj.weight": "model-00001-of-00004.safetensors", "model.layers.5.self_attn.q_proj.bias": "model-00001-of-00004.safetensors", "model.layers.5.self_attn.q_proj.weight": "model-00001-of-00004.safetensors", "model.layers.5.self_attn.v_proj.bias": "model-00001-of-00004.safetensors", "model.layers.5.self_attn.v_proj.weight": "model-00001-of-00004.safetensors", "model.layers.6.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.6.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.6.mlp.gate_proj.weight": "model-00001-of-00004.safetensors", "model.layers.6.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.6.post_attention_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.6.self_attn.k_proj.bias": "model-00001-of-00004.safetensors", "model.layers.6.self_attn.k_proj.weight": "model-00001-of-00004.safetensors", "model.layers.6.self_attn.o_proj.bias": "model-00001-of-00004.safetensors", "model.layers.6.self_attn.o_proj.weight": "model-00001-of-00004.safetensors", "model.layers.6.self_attn.q_proj.bias": "model-00001-of-00004.safetensors", "model.layers.6.self_attn.q_proj.weight": "model-00001-of-00004.safetensors", "model.layers.6.self_attn.v_proj.bias": "model-00001-of-00004.safetensors", "model.layers.6.self_attn.v_proj.weight": "model-00001-of-00004.safetensors", "model.layers.7.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.7.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.7.mlp.gate_proj.weight": "model-00001-of-00004.safetensors", "model.layers.7.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.7.post_attention_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.7.self_attn.k_proj.bias": "model-00001-of-00004.safetensors", "model.layers.7.self_attn.k_proj.weight": "model-00001-of-00004.safetensors", "model.layers.7.self_attn.o_proj.bias": "model-00001-of-00004.safetensors", "model.layers.7.self_attn.o_proj.weight": "model-00001-of-00004.safetensors", "model.layers.7.self_attn.q_proj.bias": "model-00001-of-00004.safetensors", "model.layers.7.self_attn.q_proj.weight": "model-00001-of-00004.safetensors", "model.layers.7.self_attn.v_proj.bias": "model-00001-of-00004.safetensors", "model.layers.7.self_attn.v_proj.weight": "model-00001-of-00004.safetensors", "model.layers.8.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.8.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.8.mlp.gate_proj.weight": "model-00001-of-00004.safetensors", "model.layers.8.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.8.post_attention_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.8.self_attn.k_proj.bias": "model-00001-of-00004.safetensors", "model.layers.8.self_attn.k_proj.weight": "model-00001-of-00004.safetensors", "model.layers.8.self_attn.o_proj.bias": "model-00001-of-00004.safetensors", "model.layers.8.self_attn.o_proj.weight": "model-00001-of-00004.safetensors", "model.layers.8.self_attn.q_proj.bias": "model-00001-of-00004.safetensors", "model.layers.8.self_attn.q_proj.weight": "model-00001-of-00004.safetensors", "model.layers.8.self_attn.v_proj.bias": "model-00001-of-00004.safetensors", "model.layers.8.self_attn.v_proj.weight": "model-00001-of-00004.safetensors", "model.layers.9.input_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.9.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.9.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.9.mlp.up_proj.weight": "model-00002-of-00004.safetensors", "model.layers.9.post_attention_layernorm.weight": "model-00002-of-00004.safetensors", "model.layers.9.self_attn.k_proj.bias": "model-00001-of-00004.safetensors", "model.layers.9.self_attn.k_proj.weight": "model-00001-of-00004.safetensors", "model.layers.9.self_attn.o_proj.bias": "model-00001-of-00004.safetensors", "model.layers.9.self_attn.o_proj.weight": "model-00001-of-00004.safetensors", "model.layers.9.self_attn.q_proj.bias": "model-00001-of-00004.safetensors", "model.layers.9.self_attn.q_proj.weight": "model-00001-of-00004.safetensors", "model.layers.9.self_attn.v_proj.bias": "model-00001-of-00004.safetensors", "model.layers.9.self_attn.v_proj.weight": "model-00001-of-00004.safetensors", "model.norm.weight": "model-00003-of-00004.safetensors" }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨# This file was automatically generated from modular_openpangu_dense.py.# Do NOT edit this file manually as any edits will be overwritten by the generation of# the file from the modular. If any change should be done, please apply the change to the# modular_openpangu_dense.py file directly. One of our CI enforces this.# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# coding=utf-8# Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.## This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX# and OPT implementations in this library. It has been modified from its# original forms to accommodate minor architectural differences compared# to GPT-NeoX and OPT used by the Meta AI team that trained the model.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
from typing import Callable, Optional, Union
import torchfrom torch import nn
try: import torch_npu from torch_npu.contrib import transfer_to_npu if "910" in torch.npu.get_device_name(): NPU_ATTN_INFR = True print("[INFO] torch_npu detected. Using NPU fused infer attention.") else: NPU_ATTN_INFR = Falseexcept ImportError: NPU_ATTN_INFR = False
from transformers.activations import ACT2FNfrom transformers.cache_utils import Cache, DynamicCachefrom transformers.generation import GenerationMixinfrom transformers.masking_utils import create_causal_maskfrom transformers.modeling_flash_attention_utils import FlashAttentionKwargsfrom transformers.modeling_layers import GradientCheckpointingLayerfrom transformers.modeling_outputs import ( BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast,)from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_updatefrom transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModelfrom transformers.processing_utils import Unpackfrom transformers.utils import LossKwargs, auto_docstring, can_return_tuple, loggingfrom .configuration_openpangu_dense import PanguEmbeddedConfig
logger = logging.get_logger(__name__)
class PanguEmbeddedRMSNorm(nn.Module): def __init__(self, hidden_size, eps=1e-6): """ PanguEmbeddedRMSNorm is equivalent to T5LayerNorm """ super().__init__() self.weight = nn.Parameter(torch.ones(hidden_size)) self.variance_epsilon = eps
def forward(self, hidden_states): input_dtype = hidden_states.dtype hidden_states = hidden_states.to(torch.float32) variance = hidden_states.pow(2).mean(-1, keepdim=True) hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) return self.weight * hidden_states.to(input_dtype)
def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}"
class PanguEmbeddedRotaryEmbedding(nn.Module): def __init__(self, config: PanguEmbeddedConfig, device=None): super().__init__() # BC: "rope_type" was originally "type" if hasattr(config, "rope_scaling") and config.rope_scaling is not None: self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type")) else: self.rope_type = "default" self.max_seq_len_cached = config.max_position_embeddings self.original_max_seq_len = config.max_position_embeddings
self.config = config self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device) self.register_buffer("inv_freq", inv_freq, persistent=False) self.original_inv_freq = self.inv_freq
@torch.no_grad() @dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope) def forward(self, x, position_ids): inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1).to(x.device) position_ids_expanded = position_ids[:, None, :].float()
device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu" with torch.autocast(device_type=device_type, enabled=False): # Force float32 freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2) emb = torch.cat((freqs, freqs), dim=-1) cos = emb.cos() * self.attention_scaling sin = emb.sin() * self.attention_scaling
return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
def rotate_half(x): """Rotates half the hidden dims of the input.""" x1 = x[..., : x.shape[-1] // 2] x2 = x[..., x.shape[-1] // 2 :] return torch.cat((-x2, x1), dim=-1)
def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): """Applies Rotary Position Embedding to the query and key tensors.
Args: q (`torch.Tensor`): The query tensor. k (`torch.Tensor`): The key tensor. cos (`torch.Tensor`): The cosine part of the rotary embedding. sin (`torch.Tensor`): The sine part of the rotary embedding. position_ids (`torch.Tensor`, *optional*): Deprecated and unused. unsqueeze_dim (`int`, *optional*, defaults to 1): The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2. Returns: `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. """ cos = cos.unsqueeze(unsqueeze_dim) sin = sin.unsqueeze(unsqueeze_dim) q_embed = (q * cos) + (rotate_half(q) * sin) k_embed = (k * cos) + (rotate_half(k) * sin) return q_embed, k_embed
class PanguEmbeddedMLP(nn.Module): def __init__(self, config): super().__init__() self.config = config self.hidden_size = config.hidden_size self.intermediate_size = config.intermediate_size self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False) self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False) self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False) self.act_fn = ACT2FN[config.hidden_act]
def forward(self, x): down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x)) return down_proj
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: """ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) """ batch, num_key_value_heads, slen, head_dim = hidden_states.shape if n_rep == 1: return hidden_states hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
def eager_attention_forward( module: nn.Module, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attention_mask: Optional[torch.Tensor], scaling: float, dropout: float = 0.0, **kwargs,): key_states = repeat_kv(key, module.num_key_value_groups) value_states = repeat_kv(value, module.num_key_value_groups)
attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling if attention_mask is not None: causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] attn_weights = attn_weights + causal_mask
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype) attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training) attn_output = torch.matmul(attn_weights, value_states) attn_output = attn_output.transpose(1, 2).contiguous()
return attn_output, attn_weights
class PanguEmbeddedAttention(nn.Module): """Multi-headed attention from 'Attention Is All You Need' paper"""
def __init__(self, config: PanguEmbeddedConfig, layer_idx: int): super().__init__() self.config = config self.layer_idx = layer_idx self.head_dim = getattr(config, "head_dim", config.hidden_size // config.num_attention_heads) self.num_heads = config.num_attention_heads self.num_key_value_heads = config.num_key_value_heads self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads self.scaling = self.head_dim**-0.5 self.attention_dropout = config.attention_dropout self.is_causal = True
self.q_proj = nn.Linear(config.hidden_size, config.num_attention_heads * self.head_dim, bias=config.bias) self.k_proj = nn.Linear(config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.bias) self.v_proj = nn.Linear(config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.bias) self.o_proj = nn.Linear(config.num_attention_heads * self.head_dim, config.hidden_size, bias=config.bias)
def forward( self, hidden_states: torch.Tensor, position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim)
query_states = self.q_proj(hidden_states).view(hidden_shape).transpose(1, 2) key_states = self.k_proj(hidden_states).view(hidden_shape).transpose(1, 2) value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)
cos, sin = position_embeddings query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
if past_key_value is not None: # sin and cos are specific to RoPE models; cache_position needed for the static cache cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
attention_interface: Callable = eager_attention_forward if self.config._attn_implementation != "eager": attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation] if not self.training and NPU_ATTN_INFR: q_len = input_shape[1] if attention_mask is not None: attention_mask = ~attention_mask.bool() elif q_len > 1: attention_mask = torch.triu(torch.ones([q_len, q_len]), diagonal=1).bool().unsqueeze(0).unsqueeze(0).to(query_states.device)
attn_output, _ = torch_npu.npu_fused_infer_attention_score( query_states, key_states, value_states, num_heads=self.num_heads, num_key_value_heads=self.num_key_value_heads, input_layout="BNSD", atten_mask=attention_mask, scale=self.scaling) attn_output = attn_output.transpose(1, 2) attn_weights = None else: attn_output, attn_weights = attention_interface( self, query_states, key_states, value_states, attention_mask, dropout=0.0 if not self.training else self.attention_dropout, scaling=self.scaling, **kwargs, )
attn_output = attn_output.reshape(*input_shape, -1).contiguous() attn_output = self.o_proj(attn_output) return attn_output, attn_weights
class PanguEmbeddedDecoderLayer(GradientCheckpointingLayer): def __init__(self, config: PanguEmbeddedConfig, layer_idx: int): super().__init__() self.hidden_size = config.hidden_size self.self_attn = PanguEmbeddedAttention(config=config, layer_idx=layer_idx) self.mlp = PanguEmbeddedMLP(config) self.input_layernorm = PanguEmbeddedRMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.post_attention_layernorm = PanguEmbeddedRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
def forward( self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states)
# Self Attention hidden_states, self_attn_weights = self.self_attn( hidden_states=hidden_states, attention_mask=attention_mask, position_ids=position_ids, past_key_value=past_key_value, output_attentions=output_attentions, use_cache=use_cache, cache_position=cache_position, position_embeddings=position_embeddings, **kwargs, ) hidden_states = residual + hidden_states
# Fully Connected residual = hidden_states hidden_states = self.post_attention_layernorm(hidden_states) hidden_states = self.mlp(hidden_states) hidden_states = residual + hidden_states
outputs = (hidden_states,) if output_attentions: outputs += (self_attn_weights,)
return outputs
@auto_docstringclass PanguEmbeddedPreTrainedModel(PreTrainedModel): config_class = PanguEmbeddedConfig base_model_prefix = "model" supports_gradient_checkpointing = True _no_split_modules = ["PanguEmbeddedDecoderLayer"] _skip_keys_device_placement = ["past_key_values"] _supports_flash_attn_3 = True _supports_flash_attn_2 = True _supports_sdpa = True _supports_flex_attn = True _supports_cache_class = True _supports_quantized_cache = True _supports_static_cache = True _supports_attention_backend = True
def _init_weights(self, module): std = self.config.initializer_range if isinstance(module, nn.Linear): module.weight.data.normal_(mean=0.0, std=std) if module.bias is not None: module.bias.data.zero_() elif isinstance(module, nn.Embedding): module.weight.data.normal_(mean=0.0, std=std) if module.padding_idx is not None: module.weight.data[module.padding_idx].zero_() elif isinstance(module, PanguEmbeddedRMSNorm): module.weight.data.fill_(1.0)
@auto_docstringclass PanguEmbeddedModel(PanguEmbeddedPreTrainedModel): def __init__(self, config: PanguEmbeddedConfig): super().__init__(config) self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx) self.layers = nn.ModuleList( [PanguEmbeddedDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)] ) self.norm = PanguEmbeddedRMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.rotary_emb = PanguEmbeddedRotaryEmbedding(config=config) self.gradient_checkpointing = False
# Initialize weights and apply final processing self.post_init()
def get_input_embeddings(self): return self.embed_tokens
def set_input_embeddings(self, value): self.embed_tokens = value
@can_return_tuple @auto_docstring def forward( self, input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Cache] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], ) -> BaseModelOutputWithPast: 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
if (input_ids is None) ^ (inputs_embeds is not None): raise ValueError("You must specify exactly one of input_ids or inputs_embeds")
if self.gradient_checkpointing and self.training and use_cache: logger.warning_once( "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`." ) use_cache = False
# TODO (joao): remove this exception in v4.56 -- it exists for users that try to pass a legacy cache if not isinstance(past_key_values, (type(None), Cache)): raise ValueError("The `past_key_values` should be either a `Cache` object or `None`.")
if inputs_embeds is None: inputs_embeds = self.embed_tokens(input_ids)
if use_cache and past_key_values is None: past_key_values = DynamicCache()
if cache_position is None: past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 cache_position = torch.arange( past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device )
if position_ids is None: position_ids = cache_position.unsqueeze(0)
causal_mask = create_causal_mask( config=self.config, input_embeds=inputs_embeds, attention_mask=attention_mask, cache_position=cache_position, past_key_values=past_key_values, position_ids=position_ids, )
hidden_states = inputs_embeds
# create position embeddings to be shared across the decoder layers position_embeddings = self.rotary_emb(hidden_states, position_ids)
# decoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None
for decoder_layer in self.layers[: self.config.num_hidden_layers]: if output_hidden_states: all_hidden_states += (hidden_states,)
layer_outputs = decoder_layer( hidden_states, attention_mask=causal_mask, position_ids=position_ids, past_key_value=past_key_values, output_attentions=output_attentions, use_cache=use_cache, cache_position=cache_position, position_embeddings=position_embeddings, **flash_attn_kwargs, )
hidden_states = layer_outputs[0]
if output_attentions: all_self_attns += (layer_outputs[1],)
hidden_states = self.norm(hidden_states)
# add hidden states from the last decoder layer if output_hidden_states: all_hidden_states += (hidden_states,)
return BaseModelOutputWithPast( last_hidden_state=hidden_states, past_key_values=past_key_values if use_cache else None, hidden_states=all_hidden_states, attentions=all_self_attns, )
class KwargsForCausalLM(FlashAttentionKwargs, LossKwargs): ...
@auto_docstringclass PanguEmbeddedForCausalLM(PanguEmbeddedPreTrainedModel, GenerationMixin): _tied_weights_keys = ["lm_head.weight"] _tp_plan = {"lm_head": "colwise_rep"} _pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config): super().__init__(config) self.model = PanguEmbeddedModel(config) self.vocab_size = config.vocab_size self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
# Initialize weights and apply final processing self.post_init()
def get_input_embeddings(self): return self.model.embed_tokens
def set_input_embeddings(self, value): self.model.embed_tokens = value
def get_output_embeddings(self): return self.lm_head
def set_output_embeddings(self, new_embeddings): self.lm_head = new_embeddings
def set_decoder(self, decoder): self.model = decoder
def get_decoder(self): return self.model
@can_return_tuple @auto_docstring def forward( self, input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Cache] = None, inputs_embeds: 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, cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], ) -> CausalLMOutputWithPast: 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 )
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn) outputs: BaseModelOutputWithPast = self.model( input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, past_key_values=past_key_values, inputs_embeds=inputs_embeds, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, cache_position=cache_position, **kwargs, )
hidden_states = outputs.last_hidden_state # Only compute necessary logits, and do not upcast them to float if we are not computing the loss slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep logits = self.lm_head(hidden_states[:, slice_indices, :])
loss = None if labels is not None: loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.config.vocab_size, **kwargs)
return CausalLMOutputWithPast( loss=loss, logits=logits, past_key_values=outputs.past_key_values, hidden_states=outputs.hidden_states, attentions=outputs.attentions, )
__all__ = [ "PanguEmbeddedForCausalLM", "PanguEmbeddedModel", "PanguEmbeddedPreTrainedModel",]123456789101112131415161718192021222324252627282930{ "bos_token": { "content": "<s>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "eos_token": { "content": "[unused10]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "pad_token": { "content": "<unk>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "unk_token": { "content": "<unk>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273# coding=utf-8# Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.## This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX# and OPT implementations in this library. It has been modified from its# original forms to accommodate minor architectural differences compared# to GPT-NeoX and OPT used by the Meta AI team that trained the model.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
import osfrom shutil import copyfilefrom typing import Any, Dict, List, Optional, Tuple
import sentencepiece as spm
from transformers.tokenization_utils import PreTrainedTokenizerfrom transformers.utils import logging
logger = logging.get_logger(__name__)
VOCAB_FILES_NAMES = {"vocab_file": "./tokenizer.model"}
PRETRAINED_VOCAB_FILES_MAP = {}
def convert_bool(string): if isinstance(string, str): if string.lower() == "true": return True elif string.lower() == "false": return False else: return string else: return string
class PanguTokenizer(PreTrainedTokenizer): """ Construct a tokenizer. Based on byte-level Byte-Pair-Encoding.
Args: vocab_file (`str`): Path to the vocabulary file. """
vocab_files_names = VOCAB_FILES_NAMES pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP model_input_names = ["input_ids", "attention_mask"] _auto_class = "AutoTokenizer"
def __init__( self, vocab_file, unk_token="<unk>", bos_token="<s>", eos_token="</s>", pad_token="</s>", sp_model_kwargs: Optional[Dict[str, Any]] = None, add_bos_token=True, add_eos_token=False, decode_with_prefix_space=False, clean_up_tokenization_spaces=False, **kwargs, ): self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(vocab_file) super().__init__( bos_token=bos_token, eos_token=eos_token, unk_token=unk_token, pad_token=pad_token, clean_up_tokenization_spaces=clean_up_tokenization_spaces, **kwargs, ) self.vocab_file = vocab_file self.add_bos_token = convert_bool(add_bos_token) self.add_eos_token = add_eos_token self.decode_with_prefix_space = decode_with_prefix_space self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(vocab_file) self._no_prefix_space_tokens = None
""" Initialisation"""
@property def no_prefix_space_tokens(self): if self._no_prefix_space_tokens is None: vocab = self.convert_ids_to_tokens(list(range(self.vocab_size))) self._no_prefix_space_tokens = {i for i, tok in enumerate(vocab) if not tok.startswith("▁")} return self._no_prefix_space_tokens
@property def vocab_size(self): """Returns vocab size""" return self.sp_model.get_piece_size()
@property def bos_token_id(self) -> Optional[int]: return self.sp_model.bos_id()
@property def eos_token_id(self) -> Optional[int]: return super().eos_token_id
def get_vocab(self): """Returns vocab as a dict""" vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab
def _tokenize(self, text): """Returns a tokenized string.""" return self.sp_model.encode(text, out_type=str)
def _convert_token_to_id(self, token): """Converts a token (str) in an id using the vocab.""" return self.sp_model.piece_to_id(token)
def _convert_id_to_token(self, index): """Converts an index (integer) in a token (str) using the vocab.""" token = self.sp_model.IdToPiece(index) return token
def _maybe_add_prefix_space(self, tokens, decoded): if tokens and tokens[0] not in self.no_prefix_space_tokens: return " " + decoded else: return decoded
def convert_tokens_to_string(self, tokens): """Converts a sequence of tokens (string) in a single string.""" current_sub_tokens = [] out_string = "" prev_is_special = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: # Decode the current sub-tokens first if current_sub_tokens: out_string += self.sp_model.decode(current_sub_tokens) current_sub_tokens = [] # Append the special token without adding extra spaces out_string += token prev_is_special = True else: current_sub_tokens.append(token) prev_is_special = False # Decode any remaining sub-tokens if current_sub_tokens: out_string += self.sp_model.decode(current_sub_tokens) # Clean up leading and trailing spaces if self.clean_up_tokenization_spaces: out_string = self.clean_up_tokenization(out_string) out_string = self._maybe_add_prefix_space(tokens=tokens, decoded=out_string) return out_string[1:]
# Override decode to set spaces_between_special_tokens to True as default def decode(self, token_ids, spaces_between_special_tokens: bool = False, **kwargs): return super().decode( token_ids=token_ids, spaces_between_special_tokens=spaces_between_special_tokens, **kwargs, )
def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> Tuple[str]: """ Save the vocabulary and special tokens file to a directory.
Args: save_directory (`str`): The directory in which to save the vocabulary.
Returns: `Tuple(str)`: Paths to the files saved. """ if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return ("",) out_vocab_file = os.path.join( save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] )
if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file) and os.path.isfile(self.vocab_file): copyfile(self.vocab_file, out_vocab_file) elif not os.path.isfile(self.vocab_file): with open(out_vocab_file, "wb") as fi: content_spiece_model = self.sp_model.serialized_model_proto() fi.write(content_spiece_model)
return (out_vocab_file,)
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None): if self.add_bos_token: bos_token_ids = [self.bos_token_id] else: bos_token_ids = []
output = bos_token_ids + token_ids_0
if token_ids_1 is not None: output = output + token_ids_1
if self.add_eos_token: output = output + [self.eos_token_id]
return output
def get_special_tokens_mask( self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False ) -> List[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method.
Args: token_ids_0 (`List[int]`): List of IDs. token_ids_1 (`List[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model.
Returns: `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True )
if token_ids_1 is None: return [1] + ([0] * len(token_ids_0)) + [1] return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1]
def create_token_type_ids_from_sequences( self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None ) -> List[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. T5 does not make use of token type ids, therefore a list of zeros is returned.
Args: token_ids_0 (`List[int]`): List of IDs. token_ids_1 (`List[int]`, *optional*): Optional second list of IDs for sequence pairs.
Returns: `List[int]`: List of zeros. """ eos = [self.eos_token_id]
if token_ids_1 is None: return len(token_ids_0 + eos) * [0] return len(token_ids_0 + eos + token_ids_1 + eos) * [0]123version https://git-lfs.github.com/spec/v1oid sha256:6b16f1558c0cd4ae6ef1a2c605713be0a514f50e1ce2d2c878979ce988c148ecsize 2477809123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334{ "add_prefix_space": true, "added_tokens_decoder": { "0": { "content": "<unk>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "1": { "content": "<s>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "2": { "content": "</s>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45806": { "content": "<|User|>:", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45813": { "content": "<|Bot|>:", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45830": { "content": "[unused0]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45840": { "content": "[unused1]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45846": { "content": "[unused2]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45849": { "content": "[unused3]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45861": { "content": "[unused4]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45866": { "content": "[unused5]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45874": { "content": "[unused6]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45883": { "content": "[unused7]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45884": { "content": "[unused8]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45887": { "content": "[unused9]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45892": { "content": "[unused10]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45920": { "content": "[unused11]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45932": { "content": "[unused12]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45938": { "content": "[unused13]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45953": { "content": "[unused14]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45968": { "content": "[unused15]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45974": { "content": "<think>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45982": { "content": "</think>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "45986": { "content": "[unused18]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46005": { "content": "[unused19]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46007": { "content": "[unused20]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46014": { "content": "[unused21]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46017": { "content": "[unused22]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46028": { "content": "[unused23]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46032": { "content": "[unused24]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46081": { "content": "[unused25]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46086": { "content": "[unused26]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46101": { "content": "[unused27]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46183": { "content": "[unused28]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46230": { "content": "[unused29]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46245": { "content": "[unused30]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "46257": { "content": "[unused31]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "144208": { "content": "<mask>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "144209": { "content": "[unused33]", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true } }, "auto_map": { "AutoTokenizer": [ "tokenization_openpangu.PanguTokenizer", null ] }, "bos_token": "<s>", "clean_up_tokenization_spaces": false, "eos_token": "[unused10]", "extra_special_tokens": {}, "legacy": true, "model_max_length": 1000000000000000019884624838656, "pad_token": "<unk>", "spaces_between_special_tokens": false, "tokenizer_class": "PanguTokenizer", "unk_token": "<unk>", "use_default_system_prompt": false}chat_template.jinjaAdded+1
config.jsonAdded+31
configuration_openpangu_dense.pyAdded+56
generation_config.jsonAdded+10
model-00001-of-00004.safetensorsAdded+3
model-00002-of-00004.safetensorsAdded+3
model-00003-of-00004.safetensorsAdded+3
model-00004-of-00004.safetensorsAdded+3
model.safetensors.index.jsonAdded+453
modeling_openpangu_dense.pyAdded+588
special_tokens_map.jsonAdded+30
tokenization_openpangu.pyAdded+273
tokenizer.modelAdded+3
tokenizer_config.jsonAdded+334