Fix bug for transformer version
#1
by fishmingyu - opened
Files changed (2)
- config.json+3-3
- modeling_qwen.py+84-19
3 unmodified lines4567891011123 unmodified lines],"attention_dropout": 0.0,"auto_map": {"AutoModel": "Alibaba-NLP/gte-Qwen2-7B-instruct--modeling_qwen.Qwen2Model","AutoModelForCausalLM": "Alibaba-NLP/gte-Qwen2-7B-instruct--modeling_qwen.Qwen2ForCausalLM","AutoModelForSequenceClassification": "Alibaba-NLP/gte-Qwen2-7B-instruct--modeling_qwen.Qwen2ForSequenceClassification"},"bos_token_id": 151643,"eos_token_id": 151643,3 unmodified lines4567891011123 unmodified lines],"attention_dropout": 0.0,"auto_map": {"AutoModel": "modeling_qwen.Qwen2Model","AutoModelForCausalLM": "modeling_qwen.Qwen2ForCausalLM","AutoModelForSequenceClassification": "modeling_qwen.Qwen2ForSequenceClassification"},"bos_token_id": 151643,"eos_token_id": 151643,
16 unmodified lines17181920212223 unmodified lines464748495051525354555687 unmodified lines144145146147148149123 unmodified lines27327427527627727827997 unmodified lines377378379380381382383110 unmodified lines494495496497498499174 unmodified lines674675676677678679680126 unmodified lines80780880981081181281381481533 unmodified lines84985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588622 unmodified lines90991091191291391440 unmodified lines95595695795895996096114 unmodified lines97697797897998098198298398498598698799 unmodified lines10871088108910901091109210935 unmodified lines10991100110111021103110446 unmodified lines1151115211531154115511561157115811591160116111621163116455 unmodified lines12201221122212231224122512261227122812291230123112321233123412351236123712381239124023 unmodified lines12641265126612671268126912701271127212731274127512767 unmodified lines1284128512861287128812891290129116 unmodified lines# 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.""" PyTorch Qwen2 model."""from transformers import Qwen2Configimport inspect23 unmodified lines)if is_flash_attn_2_available():from flash_attn import flash_attn_func, flash_attn_varlen_funcfrom flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa_flash_supports_window_size = "window_size" in list(inspect.signature(flash_attn_func).parameters)logger = logging.get_logger(__name__)87 unmodified lines# Copied from transformers.models.mistral.modeling_mistral.apply_rotary_pos_embdef apply_rotary_pos_emb(q, k, cos, sin, position_ids, 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.123 unmodified lines"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class ""with a layer index.")kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)97 unmodified lines"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class ""with a layer index.")kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)# Because the input can be padded, the absolute sequence length depends on the max position id.rotary_seq_len = max(kv_seq_len, position_ids[:, -1].max().item()) + 1110 unmodified lines"""Calls the forward method of Flash Attention - if the input hidden states contain at least one padding tokenfirst unpad the input, then computes the attention scores and pad the final attention scores.Args:query_states (`torch.Tensor`):Input query states to be passed to Flash Attention API174 unmodified lineskv_seq_len = key_states.shape[-2]if past_key_value is not None:kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)126 unmodified linesThis model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods thelibrary implements for all its model (such as downloading or saving, resizing the input embeddings, pruning headsetc.)This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usageand behavior.Parameters:config ([`Qwen2Config`]):Model configuration class with all the parameters of the model. Initializing with a config file does not33 unmodified linesinput_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provideit.Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and[`PreTrainedTokenizer.__call__`] for details.[What are input IDs?](../glossary#input-ids)attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:- 1 for tokens that are **not masked**,- 0 for tokens that are **masked**.[What are attention masks?](../glossary#attention-mask)Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and[`PreTrainedTokenizer.__call__`] for details.If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see`past_key_values`).If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for moreinformation on the default strategy.- 1 indicates the head is **not masked**,- 0 indicates the head is **masked**.position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,config.n_positions - 1]`.[What are position IDs?](../glossary#position-ids)past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attentionblocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.Two formats are allowed:- a [`~cache_utils.Cache`] instance;- Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors ofshape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacycache format.The model will output the same cache format that is fed as input. If no `past_key_values` are passed, thelegacy cache format will be returned.If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don'thave their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`of shape `(batch_size, sequence_length)`.22 unmodified linesclass Qwen2Model(Qwen2PreTrainedModel):"""Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`Qwen2DecoderLayer`]Args:config: Qwen2Config"""40 unmodified linesoutput_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_cachereturn_dict = return_dict if return_dict is not None else self.config.use_return_dict# retrieve input_ids and inputs_embeds14 unmodified linesuse_cache = Falsepast_key_values_length = 0if use_cache:use_legacy_cache = not isinstance(past_key_values, Cache)if use_legacy_cache:past_key_values = DynamicCache.from_legacy_cache(past_key_values)past_key_values_length = past_key_values.get_usable_length(seq_length)if position_ids is None:device = input_ids.device if input_ids is not None else inputs_embeds.device99 unmodified linesnext_cache = Noneif use_cache:next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cacheif not return_dict:return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)5 unmodified lines)class Qwen2ForCausalLM(Qwen2PreTrainedModel):_tied_weights_keys = ["lm_head.weight"]46 unmodified linesLabels for computing the masked language modeling loss. Indices should either be in `[0, ...,config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.Returns:Example:```python>>> from transformers import AutoTokenizer, Qwen2ForCausalLM>>> model = Qwen2ForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)>>> prompt = "Hey, are you conscious? Can you talk to me?">>> inputs = tokenizer(prompt, return_tensors="pt")>>> # Generate>>> generate_ids = model.generate(inputs.input_ids, max_length=30)>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]55 unmodified lines# Omit tokens covered by past_key_valuesif past_key_values is not None:if isinstance(past_key_values, Cache):cache_length = past_key_values.get_seq_length()past_length = past_key_values.seen_tokensmax_cache_length = past_key_values.get_max_length()else:cache_length = past_length = past_key_values[0][0].shape[2]max_cache_length = None# Keep only the unprocessed tokens:# 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where# some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as# input)if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]# 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard# input_ids based on the past_length.elif past_length < input_ids.shape[1]:input_ids = input_ids[:, past_length:]# 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.23 unmodified linesmodel_inputs.update({"position_ids": position_ids,"past_key_values": past_key_values,"use_cache": kwargs.get("use_cache"),"attention_mask": attention_mask,})return model_inputs@staticmethoddef _reorder_cache(past_key_values, beam_idx):reordered_past = ()7 unmodified lines@add_start_docstrings("""The Qwen2 Model transformer with a sequence classification head on top (linear layer).[`Qwen2ForSequenceClassification`] uses the last token in order to do the classification, as other causal models(e.g. GPT-2) do.Since it does classification on the last token, it requires to know the position of the last token. If a`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. Ifno `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the16 unmodified lines17181920212223242523 unmodified lines495051525354555657585987 unmodified lines147148149150151152153123 unmodified lines27727827928028128228328428597 unmodified lines383384385386387388389390391110 unmodified lines502503504505506507508174 unmodified lines683684685686687688689690691126 unmodified lines81881982082182282382482582682782833 unmodified lines86286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091122 unmodified lines93493593693793893994040 unmodified lines98198298398498598614 unmodified lines100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102899 unmodified lines11281129113011311132113311341135113611375 unmodified lines114311441145114611471148114946 unmodified lines1196119711981199120012011202120312041205120612071208120912101211121212131214121555 unmodified lines1271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130223 unmodified lines132613271328132913301331133213331334133513361337133813397 unmodified lines134713481349135013511352135313541355135616 unmodified lines# 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.# includes edits by https://github.com/BBC-Esq to fix cache errors following transformers version post 4.53.3 major cache refactor""" PyTorch Qwen2 model."""from transformers import Qwen2Configimport inspect23 unmodified lines)# if is_flash_attn_2_available():# from flash_attn import flash_attn_func, flash_attn_varlen_func# from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa# _flash_supports_window_size = "window_size" in list(inspect.signature(flash_attn_func).parameters)logger = logging.get_logger(__name__)87 unmodified lines# Copied from transformers.models.mistral.modeling_mistral.apply_rotary_pos_embdef apply_rotary_pos_emb(q, k, cos, sin, position_ids, 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.123 unmodified lines"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class ""with a layer index.")# kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)past_len = past_key_value.get_seq_length(self.layer_idx) if past_key_value is not None else 0kv_seq_len += past_lencos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)97 unmodified lines"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class ""with a layer index.")# kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)past_len = past_key_value.get_seq_length(self.layer_idx) if past_key_value is not None else 0kv_seq_len += past_len# Because the input can be padded, the absolute sequence length depends on the max position id.rotary_seq_len = max(kv_seq_len, position_ids[:, -1].max().item()) + 1110 unmodified lines"""Calls the forward method of Flash Attention - if the input hidden states contain at least one padding tokenfirst unpad the input, then computes the attention scores and pad the final attention scores.Args:query_states (`torch.Tensor`):Input query states to be passed to Flash Attention API174 unmodified lineskv_seq_len = key_states.shape[-2]if past_key_value is not None:# kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)past_len = past_key_value.get_seq_length(self.layer_idx) if past_key_value is not None else 0kv_seq_len += past_lencos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)126 unmodified linesThis model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods thelibrary implements for all its model (such as downloading or saving, resizing the input embeddings, pruning headsetc.)This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usageand behavior.Parameters:config ([`Qwen2Config`]):Model configuration class with all the parameters of the model. Initializing with a config file does not33 unmodified linesinput_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provideit.Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and[`PreTrainedTokenizer.__call__`] for details.[What are input IDs?](../glossary#input-ids)attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:- 1 for tokens that are **not masked**,- 0 for tokens that are **masked**.[What are attention masks?](../glossary#attention-mask)Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and[`PreTrainedTokenizer.__call__`] for details.If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see`past_key_values`).If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for moreinformation on the default strategy.- 1 indicates the head is **not masked**,- 0 indicates the head is **masked**.position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,config.n_positions - 1]`.[What are position IDs?](../glossary#position-ids)past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attentionblocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.Two formats are allowed:- a [`~cache_utils.Cache`] instance;- Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors ofshape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacycache format.The model will output the same cache format that is fed as input. If no `past_key_values` are passed, thelegacy cache format will be returned.If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don'thave their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`of shape `(batch_size, sequence_length)`.22 unmodified linesclass Qwen2Model(Qwen2PreTrainedModel):"""Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`Qwen2DecoderLayer`]Args:config: Qwen2Config"""40 unmodified linesoutput_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_cachereturn_dict = return_dict if return_dict is not None else self.config.use_return_dict# retrieve input_ids and inputs_embeds14 unmodified linesuse_cache = Falsepast_key_values_length = 0use_legacy_cache = Falseif use_cache:# OLD behavior (removed in HF >= 4.55): treat anything not Cache as "legacy" but then# directly used legacy methods on it (would crash if None or new API).# use_legacy_cache = not isinstance(past_key_values, Cache)# if use_legacy_cache:# # past_key_values_length = past_key_values.get_seq_length()# past_key_values_length = past_key_values.get_usable_length(seq_length)# NEW behavior: if a legacy tuple is passed, convert it to the new Cache API,# compute length via .get_seq_length(), and remember to return legacy if that’s what came in.if past_key_values is not None and not isinstance(past_key_values, Cache):use_legacy_cache = True # remember input format for returnpast_key_values = DynamicCache.from_legacy_cache(past_key_values)if isinstance(past_key_values, Cache):# Layer-agnostic total length; cache_position is handled deeper if neededpast_key_values_length = past_key_values.get_seq_length()else:# No cache given on first forward, keep length at 0past_key_values_length = 0if position_ids is None:device = input_ids.device if input_ids is not None else inputs_embeds.device99 unmodified linesnext_cache = Noneif use_cache:# If the caller passed legacy, return legacy. Otherwise return the Cache object.next_cache = (next_decoder_cache.to_legacy_cache() if (use_legacy_cache and next_decoder_cache is not None) else next_decoder_cache)if not return_dict:return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)5 unmodified lines)class Qwen2ForCausalLM(Qwen2PreTrainedModel):_tied_weights_keys = ["lm_head.weight"]46 unmodified linesLabels for computing the masked language modeling loss. Indices should either be in `[0, ...,config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.Returns:Example:```python>>> from transformers import AutoTokenizer, Qwen2ForCausalLM>>> model = Qwen2ForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)>>> prompt = "Hey, are you conscious? Can you talk to me?">>> inputs = tokenizer(prompt, return_tensors="pt")>>> # Generate>>> generate_ids = model.generate(inputs.input_ids, max_length=30)>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]55 unmodified lines# Omit tokens covered by past_key_valuesif past_key_values is not None:if isinstance(past_key_values, Cache):# NEW API (HF >= 4.55): use Cache methodscache_length = past_key_values.get_seq_length()past_length = cache_length # `seen_tokens` removed; use total seq length insteadtry:max_cache_length = past_key_values.get_max_cache_shape()except Exception:max_cache_length = None# OLD API (deprecated/removed):# cache_length = past_key_values.get_seq_length()# past_length = past_key_values.seen_tokens# max_cache_length = past_key_values.get_max_length()else:# Legacy tuple format: keep computing lengths directly from tensors# (We keep it compatible without forcing a conversion here)cache_length = past_length = past_key_values[0][0].shape[2]max_cache_length = None# Keep only the unprocessed tokens:# 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where# some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as input)if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]# 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens.# We can discard input_ids based on the past_length.elif past_length < input_ids.shape[1]:input_ids = input_ids[:, past_length:]# 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.23 unmodified linesmodel_inputs.update({"position_ids": position_ids,"past_key_values": past_key_values, # pass through unchanged (legacy or new Cache object)"use_cache": kwargs.get("use_cache"),"attention_mask": attention_mask,})return model_inputs@staticmethoddef _reorder_cache(past_key_values, beam_idx):reordered_past = ()7 unmodified lines@add_start_docstrings("""The Qwen2 Model transformer with a sequence classification head on top (linear layer).[`Qwen2ForSequenceClassification`] uses the last token in order to do the classification, as other causal models(e.g. GPT-2) do.Since it does classification on the last token, it requires to know the position of the last token. If a`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. Ifno `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
config.json+3-3
modeling_qwen.py+84-19