zhuozhijian commited on
Commit
a6f18ee
·
1 Parent(s): afbfb6e
README.md CHANGED
@@ -1,3 +1,46 @@
1
  ---
2
  license: apache-2.0
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ language:
4
+ - en
5
+ library_name: transformers
6
  ---
7
+
8
+ # Introduction
9
+
10
+ This repository contains the checkpoints of ICLR 2025 paper **[“Polynomial Composition Activations: Unleashing the Dynamics of Large Language Models](https://arxiv.org/pdf/2411.03884)”.**
11
+ In this work, we introduce a novel activation function called **Polynomial Composition (PolyCom)**, which enhances the expressiveness of large language models (LLMs) through dynamic polynomial compositions. Our method significantly improves the performance of dense and mixture of experts (MoE) models across a variety of downstream tasks, without adding significant computational overhead.
12
+
13
+ # Datasets and Training
14
+
15
+ We use the [RedPajama-Data-1T](https://huggingface.co/datasets/togethercomputer/RedPajama-Data-1T) dataset and pretrain the PolyCom model on 250B tokens. For more training details, please refer to [the source code](https://github.com/BryceZhuo/PolyCom).
16
+
17
+
18
+ # Inference
19
+
20
+ Here is an example of how to use the PolyCom model for inference:
21
+
22
+ ```python
23
+ from transformers import AutoModelForCausalLM, AutoTokenizer
24
+
25
+ model = AutoModelForCausalLM.from_pretrained(path_of_model, device_map="cuda",trust_remote_code=True)
26
+ tokenizer = AutoTokenizer.from_pretrained(path_of_model, padding_side="right",trust_remote_code=True)
27
+
28
+ prompt = "Hello, my name is"
29
+ input_ids = tokenizer.encode(prompt, return_tensors='pt').to('cuda')
30
+
31
+ greedy_output = model.generate(input_ids)
32
+ print(tokenizer.decode(greedy_output[0], skip_special_tokens=True))
33
+ ```
34
+
35
+
36
+ # Citing this work
37
+
38
+ If you find this work helpful or use it in your research, please consider citing our paper:
39
+ ```bibtex
40
+ @inproceedings{zhuo2025polycom,
41
+ title={Polynomial Composition Activations: Unleashing the Dynamics of Large Language Models},
42
+ author={Zhijian Zhuo and Ya Wang and Yutao Zeng and Xiaoqing Li and Xun Zhou and Jinwen Ma},
43
+ booktitle={ICLR 2025},
44
+ year={2025}
45
+ }
46
+ ```
added_tokens.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "<pad>": 32000
3
+ }
config.json ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "PolyLLaMAForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "auto_map": {
7
+ "AutoConfig": "configuration_polyllama.PolyLlamaConfig",
8
+ "AutoModel": "modeling_polyllama.PolyLlamaForCausalLM",
9
+ "AutoModelForCausalLM": "modeling_polyllama.PolyLlamaForCausalLM"
10
+ },
11
+ "bos_token_id": null,
12
+ "eos_token_id": null,
13
+ "hidden_act": "PolyNorm",
14
+ "hidden_size": 2048,
15
+ "initializer_range": 0.02,
16
+ "intermediate_size": 8256,
17
+ "max_position_embeddings": 4096,
18
+ "max_sequence_length": 4096,
19
+ "model_type": "polyllama",
20
+ "num_attention_heads": 16,
21
+ "num_hidden_layers": 24,
22
+ "num_key_value_heads": 16,
23
+ "pretraining_tp": 1,
24
+ "rms_norm_eps": 1e-06,
25
+ "rope_scaling": null,
26
+ "rope_theta": 10000.0,
27
+ "tie_word_embeddings": false,
28
+ "torch_dtype": "bfloat16",
29
+ "transformers_version": "4.34.1",
30
+ "use_cache": true,
31
+ "vocab_size": 32001
32
+ }
configuration_polyllama.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
5
+ # and OPT implementations in this library. It has been modified from its
6
+ # original forms to accommodate minor architectural differences compared
7
+ # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ """ LLaMA model configuration"""
21
+
22
+ from transformers.configuration_utils import PretrainedConfig
23
+ from transformers.utils import logging
24
+
25
+ logger = logging.get_logger(__name__)
26
+
27
+ LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP = {}
28
+
29
+
30
+ class PolyLlamaConfig(PretrainedConfig):
31
+ r"""
32
+ This is the configuration class to store the configuration of a [`LlamaModel`]. It is used to instantiate an LLaMA
33
+ model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
34
+ defaults will yield a similar configuration to that of the LLaMA-7B.
35
+
36
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
37
+ documentation from [`PretrainedConfig`] for more information.
38
+
39
+
40
+ Args:
41
+ vocab_size (`int`, *optional*, defaults to 32000):
42
+ Vocabulary size of the LLaMA model. Defines the number of different tokens that can be represented by the
43
+ `inputs_ids` passed when calling [`LlamaModel`]
44
+ hidden_size (`int`, *optional*, defaults to 4096):
45
+ Dimension of the hidden representations.
46
+ intermediate_size (`int`, *optional*, defaults to 11008):
47
+ Dimension of the MLP representations.
48
+ num_hidden_layers (`int`, *optional*, defaults to 32):
49
+ Number of hidden layers in the Transformer encoder.
50
+ num_attention_heads (`int`, *optional*, defaults to 32):
51
+ Number of attention heads for each attention layer in the Transformer encoder.
52
+ num_key_value_heads (`int`, *optional*):
53
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
54
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
55
+ `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
56
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
57
+ by meanpooling all the original heads within that group. For more details checkout [this
58
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
59
+ `num_attention_heads`.
60
+ pretraining_tp (`int`, *optional*, defaults to `1`):
61
+ Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
62
+ document](https://huggingface.co/docs/transformers/parallelism) to understand more about it. This value is
63
+ necessary to ensure exact reproducibility of the pretraining results. Please refer to [this
64
+ issue](https://github.com/pytorch/pytorch/issues/76232).
65
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
66
+ The non-linear activation function (function or string) in the decoder.
67
+ max_position_embeddings (`int`, *optional*, defaults to 2048):
68
+ The maximum sequence length that this model might ever be used with. Llama 1 supports up to 2048 tokens,
69
+ Llama 2 up to 4096, CodeLlama up to 16384.
70
+ initializer_range (`float`, *optional*, defaults to 0.02):
71
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
72
+ rms_norm_eps (`float`, *optional*, defaults to 1e-12):
73
+ The epsilon used by the rms normalization layers.
74
+ use_cache (`bool`, *optional*, defaults to `True`):
75
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
76
+ relevant if `config.is_decoder=True`.
77
+ tie_word_embeddings(`bool`, *optional*, defaults to `False`):
78
+ Whether to tie weight embeddings
79
+ rope_theta (`float`, *optional*, defaults to 10000.0):
80
+ The base period of the RoPE embeddings.
81
+ rope_scaling (`Dict`, *optional*):
82
+ Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
83
+ strategies: linear and dynamic. Their scaling factor must be an float greater than 1. The expected format
84
+ is `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
85
+ `max_position_embeddings` to the expected new maximum. See the following thread for more information on how
86
+ these scaling strategies behave:
87
+ https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
88
+ experimental feature, subject to breaking API changes in future versions.
89
+ attention_bias (`bool`, defaults to `False`):
90
+ Whether to use a bias in the query, key, value and output projection layers during self-attention.
91
+
92
+ Example:
93
+
94
+ ```python
95
+ >>> from transformers import LlamaModel, LlamaConfig
96
+
97
+ >>> # Initializing a LLaMA llama-7b style configuration
98
+ >>> configuration = LlamaConfig()
99
+
100
+ >>> # Initializing a model from the llama-7b style configuration
101
+ >>> model = LlamaModel(configuration)
102
+
103
+ >>> # Accessing the model configuration
104
+ >>> configuration = model.config
105
+ ```"""
106
+ model_type = "polyllama"
107
+ keys_to_ignore_at_inference = ["past_key_values"]
108
+
109
+ def __init__(
110
+ self,
111
+ vocab_size=32000,
112
+ hidden_size=4096,
113
+ intermediate_size=11008,
114
+ num_hidden_layers=32,
115
+ num_attention_heads=32,
116
+ num_key_value_heads=None,
117
+ hidden_act="silu",
118
+ max_position_embeddings=2048,
119
+ initializer_range=0.02,
120
+ rms_norm_eps=1e-6,
121
+ use_cache=True,
122
+ pad_token_id=None,
123
+ bos_token_id=1,
124
+ eos_token_id=2,
125
+ pretraining_tp=1,
126
+ tie_word_embeddings=False,
127
+ rope_theta=10000.0,
128
+ rope_scaling=None,
129
+ attention_bias=False,
130
+ **kwargs,
131
+ ):
132
+ self.vocab_size = vocab_size
133
+ self.max_position_embeddings = max_position_embeddings
134
+ self.hidden_size = hidden_size
135
+ self.intermediate_size = intermediate_size
136
+ self.num_hidden_layers = num_hidden_layers
137
+ self.num_attention_heads = num_attention_heads
138
+
139
+ # for backward compatibility
140
+ if num_key_value_heads is None:
141
+ num_key_value_heads = num_attention_heads
142
+
143
+ self.num_key_value_heads = num_key_value_heads
144
+ self.hidden_act = hidden_act
145
+ self.initializer_range = initializer_range
146
+ self.rms_norm_eps = rms_norm_eps
147
+ self.pretraining_tp = pretraining_tp
148
+ self.use_cache = use_cache
149
+ self.rope_theta = rope_theta
150
+ self.rope_scaling = rope_scaling
151
+ self._rope_scaling_validation()
152
+ self.attention_bias = attention_bias
153
+
154
+ super().__init__(
155
+ pad_token_id=pad_token_id,
156
+ bos_token_id=bos_token_id,
157
+ eos_token_id=eos_token_id,
158
+ tie_word_embeddings=tie_word_embeddings,
159
+ **kwargs,
160
+ )
161
+
162
+ def _rope_scaling_validation(self):
163
+ """
164
+ Validate the `rope_scaling` configuration.
165
+ """
166
+ if self.rope_scaling is None:
167
+ return
168
+
169
+ if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
170
+ raise ValueError(
171
+ "`rope_scaling` must be a dictionary with with two fields, `type` and `factor`, "
172
+ f"got {self.rope_scaling}"
173
+ )
174
+ rope_scaling_type = self.rope_scaling.get("type", None)
175
+ rope_scaling_factor = self.rope_scaling.get("factor", None)
176
+ if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
177
+ raise ValueError(
178
+ f"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"
179
+ )
180
+ if rope_scaling_factor is None or not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0:
181
+ raise ValueError(f"`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}")
generation_config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 1,
4
+ "eos_token_id": 2,
5
+ "pad_token_id": 32000,
6
+ "do_sample": true,
7
+ "temperature": 0.6,
8
+ "top_p": 0.9,
9
+ "transformers_version": "4.34.1"
10
+ }
modeling_polyllama.py ADDED
@@ -0,0 +1,1277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
5
+ # and OPT implementations in this library. It has been modified from its
6
+ # original forms to accommodate minor architectural differences compared
7
+ # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ """ PyTorch LLaMA model."""
21
+ import math
22
+ from typing import List, Optional, Tuple, Union
23
+
24
+ import torch
25
+ import torch.nn.functional as F
26
+ import torch.utils.checkpoint
27
+ from torch import nn
28
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
29
+
30
+ from transformers.activations import ACT2FN
31
+ from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
32
+ from transformers.modeling_utils import PreTrainedModel
33
+ from transformers.pytorch_utils import ALL_LAYERNORM_LAYERS
34
+ from transformers.utils import (
35
+ add_start_docstrings,
36
+ add_start_docstrings_to_model_forward,
37
+ # is_flash_attn_available,
38
+ logging,
39
+ replace_return_docstrings,
40
+ )
41
+ from .configuration_polyllama import PolyLlamaConfig
42
+
43
+ # if is_flash_attn_available():
44
+ # from flash_attn import flash_attn_func, flash_attn_varlen_func
45
+ # from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
46
+
47
+
48
+ logger = logging.get_logger(__name__)
49
+
50
+ _CONFIG_FOR_DOC = "PolyLlamaConfig"
51
+
52
+
53
+ def _get_unpad_data(padding_mask):
54
+ seqlens_in_batch = padding_mask.sum(dim=-1, dtype=torch.int32)
55
+ indices = torch.nonzero(padding_mask.flatten(), as_tuple=False).flatten()
56
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
57
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
58
+ return (
59
+ indices,
60
+ cu_seqlens,
61
+ max_seqlen_in_batch,
62
+ )
63
+
64
+
65
+ # Copied from transformers.models.bart.modeling_bart._make_causal_mask
66
+ def _make_causal_mask(
67
+ input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
68
+ ):
69
+ """
70
+ Make causal mask used for bi-directional self-attention.
71
+ """
72
+ bsz, tgt_len = input_ids_shape
73
+ mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device)
74
+ mask_cond = torch.arange(mask.size(-1), device=device)
75
+ mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
76
+ mask = mask.to(dtype)
77
+
78
+ if past_key_values_length > 0:
79
+ mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
80
+ return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
81
+
82
+
83
+ # Copied from transformers.models.bart.modeling_bart._expand_mask
84
+ def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
85
+ """
86
+ Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
87
+ """
88
+ bsz, src_len = mask.size()
89
+ tgt_len = tgt_len if tgt_len is not None else src_len
90
+
91
+ expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
92
+
93
+ inverted_mask = 1.0 - expanded_mask
94
+
95
+ return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
96
+
97
+
98
+ class LlamaRMSNorm(nn.Module):
99
+ def __init__(self, hidden_size, eps=1e-6):
100
+ """
101
+ LlamaRMSNorm is equivalent to T5LayerNorm
102
+ """
103
+ super().__init__()
104
+ self.weight = nn.Parameter(torch.ones(hidden_size))
105
+ self.variance_epsilon = eps
106
+
107
+ def forward(self, hidden_states):
108
+ input_dtype = hidden_states.dtype
109
+ hidden_states = hidden_states.to(torch.float32)
110
+ variance = hidden_states.pow(2).mean(-1, keepdim=True)
111
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
112
+ return self.weight * hidden_states.to(input_dtype)
113
+
114
+
115
+ ALL_LAYERNORM_LAYERS.append(LlamaRMSNorm)
116
+
117
+
118
+ class LlamaRotaryEmbedding(nn.Module):
119
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
120
+ super().__init__()
121
+
122
+ self.dim = dim
123
+ self.max_position_embeddings = max_position_embeddings
124
+ self.base = base
125
+ inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
126
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
127
+
128
+ # Build here to make `torch.jit.trace` work.
129
+ self._set_cos_sin_cache(
130
+ seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
131
+ )
132
+
133
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
134
+ self.max_seq_len_cached = seq_len
135
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
136
+
137
+ freqs = torch.einsum("i,j->ij", t, self.inv_freq)
138
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
139
+ emb = torch.cat((freqs, freqs), dim=-1)
140
+ self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
141
+ self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
142
+
143
+ def forward(self, x, seq_len=None):
144
+ # x: [bs, num_attention_heads, seq_len, head_size]
145
+ if seq_len > self.max_seq_len_cached:
146
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
147
+
148
+ return (
149
+ self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
150
+ self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
151
+ )
152
+
153
+
154
+ class LlamaLinearScalingRotaryEmbedding(LlamaRotaryEmbedding):
155
+ """LlamaRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
156
+
157
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
158
+ self.scaling_factor = scaling_factor
159
+ super().__init__(dim, max_position_embeddings, base, device)
160
+
161
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
162
+ self.max_seq_len_cached = seq_len
163
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
164
+ t = t / self.scaling_factor
165
+
166
+ freqs = torch.einsum("i,j->ij", t, self.inv_freq)
167
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
168
+ emb = torch.cat((freqs, freqs), dim=-1)
169
+ self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
170
+ self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
171
+
172
+
173
+ class LlamaDynamicNTKScalingRotaryEmbedding(LlamaRotaryEmbedding):
174
+ """LlamaRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
175
+
176
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
177
+ self.scaling_factor = scaling_factor
178
+ super().__init__(dim, max_position_embeddings, base, device)
179
+
180
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
181
+ self.max_seq_len_cached = seq_len
182
+
183
+ if seq_len > self.max_position_embeddings:
184
+ base = self.base * (
185
+ (self.scaling_factor * seq_len / self.max_position_embeddings) - (self.scaling_factor - 1)
186
+ ) ** (self.dim / (self.dim - 2))
187
+ inv_freq = 1.0 / (base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
188
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
189
+
190
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
191
+
192
+ freqs = torch.einsum("i,j->ij", t, self.inv_freq)
193
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
194
+ emb = torch.cat((freqs, freqs), dim=-1)
195
+ self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
196
+ self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
197
+
198
+
199
+ def rotate_half(x):
200
+ """Rotates half the hidden dims of the input."""
201
+ x1 = x[..., : x.shape[-1] // 2]
202
+ x2 = x[..., x.shape[-1] // 2 :]
203
+ return torch.cat((-x2, x1), dim=-1)
204
+
205
+
206
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids):
207
+ # The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
208
+ cos = cos.squeeze(1).squeeze(0) # [seq_len, dim]
209
+ sin = sin.squeeze(1).squeeze(0) # [seq_len, dim]
210
+ cos = cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
211
+ sin = sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
212
+ q_embed = (q * cos) + (rotate_half(q) * sin)
213
+ k_embed = (k * cos) + (rotate_half(k) * sin)
214
+ return q_embed, k_embed
215
+
216
+
217
+ def norm(x, eps: float = 1e-6):
218
+ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + eps)
219
+
220
+
221
+ def polynorm(x,weight,bias):
222
+ return weight[0] * norm(x**3) + weight[1] * norm(x**2) + weight[2] * norm(x) + bias
223
+
224
+
225
+ def poly(x,weight,bias):
226
+ return weight[0] * (x**3) + weight[1] * (x**2) + weight[2] * (x) + bias
227
+
228
+
229
+
230
+ class ACT_PolyNorm(nn.Module):
231
+ def __init__(self, inplace: bool = False):
232
+ super(ACT_PolyNorm, self).__init__()
233
+ self.weight = nn.Parameter(torch.ones(3)/3)
234
+ self.bias = nn.Parameter(torch.zeros(1))
235
+
236
+ def forward(self,x):
237
+ return polynorm(x,self.weight,self.bias)
238
+
239
+
240
+
241
+ class ACT_PolyReLU(nn.Module):
242
+ def __init__(self, inplace: bool = False):
243
+ super(ACT_PolyReLU, self).__init__()
244
+ self.weight = nn.Parameter(torch.ones(3)/3)
245
+ self.bias = nn.Parameter(torch.zeros(1))
246
+
247
+ def forward(self,x):
248
+ return poly(F.relu(x),self.weight,self.bias)
249
+
250
+ ACT_POLY = {
251
+ "PolyNorm": ACT_PolyNorm,
252
+ "PolyReLU": ACT_PolyReLU,
253
+ }
254
+
255
+ class PolyLlamaMLP(nn.Module):
256
+ def __init__(self, config):
257
+ super().__init__()
258
+ self.config = config
259
+ self.hidden_size = config.hidden_size
260
+ self.intermediate_size = config.intermediate_size
261
+ # self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
262
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
263
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
264
+ self.act_fn = ACT_POLY[config.hidden_act]() if config.hidden_act in ACT_POLY else ACT2FN[config.hidden_act]
265
+
266
+ def forward(self, x):
267
+ if self.config.pretraining_tp > 1:
268
+ slice = self.intermediate_size // self.config.pretraining_tp
269
+ # gate_proj_slices = self.gate_proj.weight.split(slice, dim=0)
270
+ up_proj_slices = self.up_proj.weight.split(slice, dim=0)
271
+ down_proj_slices = self.down_proj.weight.split(slice, dim=1)
272
+
273
+ up_proj = torch.cat([F.linear(x, up_proj_slices[i]) for i in range(self.config.pretraining_tp)], dim=-1)
274
+
275
+ # intermediate_states = (self.act_fn(gate_proj) * up_proj).split(slice, dim=2)
276
+ intermediate_states = self.act_fn(up_proj).split(slice, dim=2)
277
+ down_proj = [F.linear(intermediate_states[i], down_proj_slices[i]) for i in range(self.pretraining_tp)]
278
+ down_proj = sum(down_proj)
279
+ else:
280
+ # down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
281
+ down_proj = self.down_proj(self.act_fn(self.up_proj(x)))
282
+
283
+ return down_proj
284
+
285
+
286
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
287
+ """
288
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
289
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
290
+ """
291
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
292
+ if n_rep == 1:
293
+ return hidden_states
294
+ hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
295
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
296
+
297
+
298
+ class LlamaAttention(nn.Module):
299
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
300
+
301
+ def __init__(self, config: PolyLlamaConfig):
302
+ super().__init__()
303
+ self.config = config
304
+ self.hidden_size = config.hidden_size
305
+ self.num_heads = config.num_attention_heads
306
+ self.head_dim = self.hidden_size // self.num_heads
307
+ self.num_key_value_heads = config.num_key_value_heads
308
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
309
+ self.max_position_embeddings = config.max_position_embeddings
310
+ self.rope_theta = config.rope_theta
311
+
312
+ if (self.head_dim * self.num_heads) != self.hidden_size:
313
+ raise ValueError(
314
+ f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
315
+ f" and `num_heads`: {self.num_heads})."
316
+ )
317
+ self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=config.attention_bias)
318
+ self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias)
319
+ self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias)
320
+ self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.attention_bias)
321
+ self._init_rope()
322
+
323
+ def _init_rope(self):
324
+ if self.config.rope_scaling is None:
325
+ self.rotary_emb = LlamaRotaryEmbedding(
326
+ self.head_dim,
327
+ max_position_embeddings=self.max_position_embeddings,
328
+ base=self.rope_theta,
329
+ )
330
+ else:
331
+ scaling_type = self.config.rope_scaling["type"]
332
+ scaling_factor = self.config.rope_scaling["factor"]
333
+ if scaling_type == "linear":
334
+ self.rotary_emb = LlamaLinearScalingRotaryEmbedding(
335
+ self.head_dim,
336
+ max_position_embeddings=self.max_position_embeddings,
337
+ scaling_factor=scaling_factor,
338
+ base=self.rope_theta,
339
+ )
340
+ elif scaling_type == "dynamic":
341
+ self.rotary_emb = LlamaDynamicNTKScalingRotaryEmbedding(
342
+ self.head_dim,
343
+ max_position_embeddings=self.max_position_embeddings,
344
+ scaling_factor=scaling_factor,
345
+ base=self.rope_theta,
346
+ )
347
+ else:
348
+ raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
349
+
350
+ def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
351
+ return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
352
+
353
+ def forward(
354
+ self,
355
+ hidden_states: torch.Tensor,
356
+ attention_mask: Optional[torch.Tensor] = None,
357
+ position_ids: Optional[torch.LongTensor] = None,
358
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
359
+ output_attentions: bool = False,
360
+ use_cache: bool = False,
361
+ padding_mask: Optional[torch.LongTensor] = None,
362
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
363
+ bsz, q_len, _ = hidden_states.size()
364
+
365
+ if self.config.pretraining_tp > 1:
366
+ key_value_slicing = (self.num_key_value_heads * self.head_dim) // self.config.pretraining_tp
367
+ query_slices = self.q_proj.weight.split(
368
+ (self.num_heads * self.head_dim) // self.config.pretraining_tp, dim=0
369
+ )
370
+ key_slices = self.k_proj.weight.split(key_value_slicing, dim=0)
371
+ value_slices = self.v_proj.weight.split(key_value_slicing, dim=0)
372
+
373
+ query_states = [F.linear(hidden_states, query_slices[i]) for i in range(self.config.pretraining_tp)]
374
+ query_states = torch.cat(query_states, dim=-1)
375
+
376
+ key_states = [F.linear(hidden_states, key_slices[i]) for i in range(self.config.pretraining_tp)]
377
+ key_states = torch.cat(key_states, dim=-1)
378
+
379
+ value_states = [F.linear(hidden_states, value_slices[i]) for i in range(self.config.pretraining_tp)]
380
+ value_states = torch.cat(value_states, dim=-1)
381
+
382
+ else:
383
+ query_states = self.q_proj(hidden_states)
384
+ key_states = self.k_proj(hidden_states)
385
+ value_states = self.v_proj(hidden_states)
386
+
387
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
388
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
389
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
390
+
391
+ kv_seq_len = key_states.shape[-2]
392
+ if past_key_value is not None:
393
+ kv_seq_len += past_key_value[0].shape[-2]
394
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
395
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
396
+
397
+ if past_key_value is not None:
398
+ # reuse k, v, self_attention
399
+ key_states = torch.cat([past_key_value[0], key_states], dim=2)
400
+ value_states = torch.cat([past_key_value[1], value_states], dim=2)
401
+
402
+ past_key_value = (key_states, value_states) if use_cache else None
403
+
404
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
405
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
406
+
407
+ attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
408
+
409
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
410
+ raise ValueError(
411
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
412
+ f" {attn_weights.size()}"
413
+ )
414
+
415
+ if attention_mask is not None:
416
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
417
+ raise ValueError(
418
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
419
+ )
420
+ attn_weights = attn_weights + attention_mask
421
+
422
+ # upcast attention to fp32
423
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
424
+ attn_output = torch.matmul(attn_weights, value_states)
425
+
426
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
427
+ raise ValueError(
428
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
429
+ f" {attn_output.size()}"
430
+ )
431
+
432
+ attn_output = attn_output.transpose(1, 2).contiguous()
433
+
434
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
435
+
436
+ if self.config.pretraining_tp > 1:
437
+ attn_output = attn_output.split(self.hidden_size // self.config.pretraining_tp, dim=2)
438
+ o_proj_slices = self.o_proj.weight.split(self.hidden_size // self.config.pretraining_tp, dim=1)
439
+ attn_output = sum([F.linear(attn_output[i], o_proj_slices[i]) for i in range(self.config.pretraining_tp)])
440
+ else:
441
+ attn_output = self.o_proj(attn_output)
442
+
443
+ if not output_attentions:
444
+ attn_weights = None
445
+
446
+ return attn_output, attn_weights, past_key_value
447
+
448
+
449
+ class LlamaFlashAttention2(LlamaAttention):
450
+ """
451
+ Llama flash attention module. This module inherits from `LlamaAttention` as the weights of the module stays
452
+ untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
453
+ flash attention and deal with padding tokens in case the input contains any of them.
454
+ """
455
+
456
+ def forward(
457
+ self,
458
+ hidden_states: torch.Tensor,
459
+ attention_mask: Optional[torch.Tensor] = None,
460
+ position_ids: Optional[torch.LongTensor] = None,
461
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
462
+ output_attentions: bool = False,
463
+ use_cache: bool = False,
464
+ padding_mask: Optional[torch.LongTensor] = None,
465
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
466
+ # LlamaFlashAttention2 attention does not support output_attentions
467
+ output_attentions = False
468
+
469
+ bsz, q_len, _ = hidden_states.size()
470
+
471
+ query_states = self.q_proj(hidden_states)
472
+ key_states = self.k_proj(hidden_states)
473
+ value_states = self.v_proj(hidden_states)
474
+
475
+ # Flash attention requires the input to have the shape
476
+ # batch_size x seq_length x head_dime x hidden_dim
477
+ # therefore we just need to keep the original shape
478
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
479
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
480
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
481
+
482
+ kv_seq_len = key_states.shape[-2]
483
+ if past_key_value is not None:
484
+ kv_seq_len += past_key_value[0].shape[-2]
485
+
486
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
487
+
488
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
489
+
490
+ if past_key_value is not None:
491
+ # reuse k, v, self_attention
492
+ key_states = torch.cat([past_key_value[0], key_states], dim=2)
493
+ value_states = torch.cat([past_key_value[1], value_states], dim=2)
494
+
495
+ past_key_value = (key_states, value_states) if use_cache else None
496
+
497
+ query_states = query_states.transpose(1, 2)
498
+ key_states = key_states.transpose(1, 2)
499
+ value_states = value_states.transpose(1, 2)
500
+
501
+ # TODO: llama does not have dropout in the config??
502
+ # It is recommended to use dropout with FA according to the docs
503
+ # when training.
504
+ dropout_rate = 0.0 # if not self.training else self.attn_dropout
505
+
506
+ # In PEFT, usually we cast the layer norms in float32 for training stability reasons
507
+ # therefore the input hidden states gets silently casted in float32. Hence, we need
508
+ # cast them back in float16 just to be sure everything works as expected.
509
+ # This might slowdown training & inference so it is recommended to not cast the LayerNorms
510
+ # in fp32. (LlamaRMSNorm handles it correctly)
511
+ input_dtype = query_states.dtype
512
+ if input_dtype == torch.float32:
513
+ logger.warning_once(
514
+ "The input hidden states seems to be silently casted in float32, this might be related to"
515
+ " the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
516
+ " float16."
517
+ )
518
+
519
+ query_states = query_states.to(torch.float16)
520
+ key_states = key_states.to(torch.float16)
521
+ value_states = value_states.to(torch.float16)
522
+
523
+ attn_output = self._flash_attention_forward(
524
+ query_states, key_states, value_states, padding_mask, q_len, dropout=dropout_rate
525
+ )
526
+
527
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
528
+ attn_output = self.o_proj(attn_output)
529
+
530
+ if not output_attentions:
531
+ attn_weights = None
532
+
533
+ return attn_output, attn_weights, past_key_value
534
+
535
+ def _flash_attention_forward(
536
+ self, query_states, key_states, value_states, padding_mask, query_length, dropout=0.0, softmax_scale=None
537
+ ):
538
+ """
539
+ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
540
+ first unpad the input, then computes the attention scores and pad the final attention scores.
541
+
542
+ Args:
543
+ query_states (`torch.Tensor`):
544
+ Input query states to be passed to Flash Attention API
545
+ key_states (`torch.Tensor`):
546
+ Input key states to be passed to Flash Attention API
547
+ value_states (`torch.Tensor`):
548
+ Input value states to be passed to Flash Attention API
549
+ padding_mask (`torch.Tensor`):
550
+ The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
551
+ position of padding tokens and 1 for the position of non-padding tokens.
552
+ dropout (`int`, *optional*):
553
+ Attention dropout
554
+ softmax_scale (`float`, *optional*):
555
+ The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
556
+ """
557
+ # Contains at least one padding token in the sequence
558
+ if padding_mask is not None:
559
+ batch_size = query_states.shape[0]
560
+ query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
561
+ query_states, key_states, value_states, padding_mask, query_length
562
+ )
563
+
564
+ cu_seqlens_q, cu_seqlens_k = cu_seq_lens
565
+ max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
566
+
567
+ attn_output_unpad = flash_attn_varlen_func(
568
+ query_states,
569
+ key_states,
570
+ value_states,
571
+ cu_seqlens_q=cu_seqlens_q,
572
+ cu_seqlens_k=cu_seqlens_k,
573
+ max_seqlen_q=max_seqlen_in_batch_q,
574
+ max_seqlen_k=max_seqlen_in_batch_k,
575
+ dropout_p=dropout,
576
+ softmax_scale=softmax_scale,
577
+ causal=True,
578
+ )
579
+
580
+ attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length)
581
+ else:
582
+ attn_output = flash_attn_func(
583
+ query_states, key_states, value_states, dropout, softmax_scale=softmax_scale, causal=True
584
+ )
585
+
586
+ return attn_output
587
+
588
+ def _upad_input(self, query_layer, key_layer, value_layer, padding_mask, query_length):
589
+ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(padding_mask)
590
+ batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
591
+
592
+ key_layer = index_first_axis(
593
+ key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
594
+ )
595
+ value_layer = index_first_axis(
596
+ value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
597
+ )
598
+ if query_length == kv_seq_len:
599
+ query_layer = index_first_axis(
600
+ query_layer.reshape(batch_size * kv_seq_len, self.num_heads, head_dim), indices_k
601
+ )
602
+ cu_seqlens_q = cu_seqlens_k
603
+ max_seqlen_in_batch_q = max_seqlen_in_batch_k
604
+ indices_q = indices_k
605
+ elif query_length == 1:
606
+ max_seqlen_in_batch_q = 1
607
+ cu_seqlens_q = torch.arange(
608
+ batch_size + 1, dtype=torch.int32, device=query_layer.device
609
+ ) # There is a memcpy here, that is very bad.
610
+ indices_q = cu_seqlens_q[:-1]
611
+ query_layer = query_layer.squeeze(1)
612
+ else:
613
+ # The -q_len: slice assumes left padding.
614
+ padding_mask = padding_mask[:, -query_length:]
615
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, padding_mask)
616
+
617
+ return (
618
+ query_layer,
619
+ key_layer,
620
+ value_layer,
621
+ indices_q,
622
+ (cu_seqlens_q, cu_seqlens_k),
623
+ (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
624
+ )
625
+
626
+
627
+ class PolyLlamaDecoderLayer(nn.Module):
628
+ def __init__(self, config: PolyLlamaConfig):
629
+ super().__init__()
630
+ self.hidden_size = config.hidden_size
631
+ self.self_attn = (
632
+ LlamaAttention(config=config)
633
+ if not getattr(config, "_flash_attn_2_enabled", False)
634
+ else LlamaFlashAttention2(config=config)
635
+ )
636
+ self.mlp = PolyLlamaMLP(config)
637
+ self.input_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
638
+ self.post_attention_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
639
+
640
+ def forward(
641
+ self,
642
+ hidden_states: torch.Tensor,
643
+ attention_mask: Optional[torch.Tensor] = None,
644
+ position_ids: Optional[torch.LongTensor] = None,
645
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
646
+ output_attentions: Optional[bool] = False,
647
+ use_cache: Optional[bool] = False,
648
+ padding_mask: Optional[torch.LongTensor] = None,
649
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
650
+ """
651
+ Args:
652
+ hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
653
+ attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
654
+ `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
655
+ output_attentions (`bool`, *optional*):
656
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
657
+ returned tensors for more detail.
658
+ use_cache (`bool`, *optional*):
659
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
660
+ (see `past_key_values`).
661
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
662
+ """
663
+
664
+ residual = hidden_states
665
+
666
+ hidden_states = self.input_layernorm(hidden_states)
667
+
668
+ # Self Attention
669
+ hidden_states, self_attn_weights, present_key_value = self.self_attn(
670
+ hidden_states=hidden_states,
671
+ attention_mask=attention_mask,
672
+ position_ids=position_ids,
673
+ past_key_value=past_key_value,
674
+ output_attentions=output_attentions,
675
+ use_cache=use_cache,
676
+ padding_mask=padding_mask,
677
+ )
678
+ hidden_states = residual + hidden_states
679
+
680
+ # Fully Connected
681
+ residual = hidden_states
682
+ hidden_states = self.post_attention_layernorm(hidden_states)
683
+ hidden_states = self.mlp(hidden_states)
684
+ hidden_states = residual + hidden_states
685
+
686
+ outputs = (hidden_states,)
687
+
688
+ if output_attentions:
689
+ outputs += (self_attn_weights,)
690
+
691
+ if use_cache:
692
+ outputs += (present_key_value,)
693
+
694
+ return outputs
695
+
696
+
697
+ LLAMA_START_DOCSTRING = r"""
698
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
699
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
700
+ etc.)
701
+
702
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
703
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
704
+ and behavior.
705
+
706
+ Parameters:
707
+ config ([`LlamaConfig`]):
708
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
709
+ load the weights associated with the model, only the configuration. Check out the
710
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
711
+ """
712
+
713
+
714
+ @add_start_docstrings(
715
+ "The bare LLaMA Model outputting raw hidden-states without any specific head on top.",
716
+ LLAMA_START_DOCSTRING,
717
+ )
718
+ class PolyLlamaPreTrainedModel(PreTrainedModel):
719
+ config_class = PolyLlamaConfig
720
+ base_model_prefix = "model"
721
+ supports_gradient_checkpointing = True
722
+ _no_split_modules = ["PolyLlamaDecoderLayer"]
723
+ _skip_keys_device_placement = "past_key_values"
724
+ _supports_flash_attn_2 = True
725
+
726
+ def _init_weights(self, module):
727
+ std = self.config.initializer_range
728
+ if isinstance(module, nn.Linear):
729
+ module.weight.data.normal_(mean=0.0, std=std)
730
+ if module.bias is not None:
731
+ module.bias.data.zero_()
732
+ elif isinstance(module, nn.Embedding):
733
+ module.weight.data.normal_(mean=0.0, std=std)
734
+ if module.padding_idx is not None:
735
+ module.weight.data[module.padding_idx].zero_()
736
+
737
+ def _set_gradient_checkpointing(self, module, value=False):
738
+ if isinstance(module, PolyLlamaModel):
739
+ module.gradient_checkpointing = value
740
+
741
+
742
+ LLAMA_INPUTS_DOCSTRING = r"""
743
+ Args:
744
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
745
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
746
+ it.
747
+
748
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
749
+ [`PreTrainedTokenizer.__call__`] for details.
750
+
751
+ [What are input IDs?](../glossary#input-ids)
752
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
753
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
754
+
755
+ - 1 for tokens that are **not masked**,
756
+ - 0 for tokens that are **masked**.
757
+
758
+ [What are attention masks?](../glossary#attention-mask)
759
+
760
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
761
+ [`PreTrainedTokenizer.__call__`] for details.
762
+
763
+ If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
764
+ `past_key_values`).
765
+
766
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
767
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
768
+ information on the default strategy.
769
+
770
+ - 1 indicates the head is **not masked**,
771
+ - 0 indicates the head is **masked**.
772
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
773
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
774
+ config.n_positions - 1]`.
775
+
776
+ [What are position IDs?](../glossary#position-ids)
777
+ past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
778
+ Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
779
+ `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
780
+ `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
781
+
782
+ Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
783
+ blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
784
+
785
+ If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
786
+ have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
787
+ of shape `(batch_size, sequence_length)`.
788
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
789
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
790
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
791
+ model's internal embedding lookup matrix.
792
+ use_cache (`bool`, *optional*):
793
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
794
+ `past_key_values`).
795
+ output_attentions (`bool`, *optional*):
796
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
797
+ tensors for more detail.
798
+ output_hidden_states (`bool`, *optional*):
799
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
800
+ more detail.
801
+ return_dict (`bool`, *optional*):
802
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
803
+ """
804
+
805
+
806
+ @add_start_docstrings(
807
+ "The bare LLaMA Model outputting raw hidden-states without any specific head on top.",
808
+ LLAMA_START_DOCSTRING,
809
+ )
810
+ class PolyLlamaModel(PolyLlamaPreTrainedModel):
811
+ """
812
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`PolyLlamaDecoderLayer`]
813
+
814
+ Args:
815
+ config: LlamaConfig
816
+ """
817
+
818
+ def __init__(self, config: PolyLlamaConfig):
819
+ super().__init__(config)
820
+ self.padding_idx = config.pad_token_id
821
+ self.vocab_size = config.vocab_size
822
+
823
+ self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
824
+ self.layers = nn.ModuleList([PolyLlamaDecoderLayer(config) for _ in range(config.num_hidden_layers)])
825
+ self.norm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
826
+
827
+ self.gradient_checkpointing = False
828
+ # Initialize weights and apply final processing
829
+ self.post_init()
830
+
831
+ def get_input_embeddings(self):
832
+ return self.embed_tokens
833
+
834
+ def set_input_embeddings(self, value):
835
+ self.embed_tokens = value
836
+
837
+ # Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask
838
+ def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):
839
+ # create causal mask
840
+ # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
841
+ combined_attention_mask = None
842
+ if input_shape[-1] > 1:
843
+ combined_attention_mask = _make_causal_mask(
844
+ input_shape,
845
+ inputs_embeds.dtype,
846
+ device=inputs_embeds.device,
847
+ past_key_values_length=past_key_values_length,
848
+ )
849
+
850
+ if attention_mask is not None:
851
+ # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
852
+ expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
853
+ inputs_embeds.device
854
+ )
855
+ combined_attention_mask = (
856
+ expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
857
+ )
858
+
859
+ return combined_attention_mask
860
+
861
+ @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
862
+ def forward(
863
+ self,
864
+ input_ids: torch.LongTensor = None,
865
+ attention_mask: Optional[torch.Tensor] = None,
866
+ position_ids: Optional[torch.LongTensor] = None,
867
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
868
+ inputs_embeds: Optional[torch.FloatTensor] = None,
869
+ use_cache: Optional[bool] = None,
870
+ output_attentions: Optional[bool] = None,
871
+ output_hidden_states: Optional[bool] = None,
872
+ return_dict: Optional[bool] = None,
873
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
874
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
875
+ output_hidden_states = (
876
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
877
+ )
878
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
879
+
880
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
881
+
882
+ # retrieve input_ids and inputs_embeds
883
+ if input_ids is not None and inputs_embeds is not None:
884
+ raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
885
+ elif input_ids is not None:
886
+ batch_size, seq_length = input_ids.shape
887
+ elif inputs_embeds is not None:
888
+ batch_size, seq_length, _ = inputs_embeds.shape
889
+ else:
890
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
891
+
892
+ seq_length_with_past = seq_length
893
+ past_key_values_length = 0
894
+
895
+ if past_key_values is not None:
896
+ past_key_values_length = past_key_values[0][0].shape[2]
897
+ seq_length_with_past = seq_length_with_past + past_key_values_length
898
+
899
+ if position_ids is None:
900
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
901
+ position_ids = torch.arange(
902
+ past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
903
+ )
904
+ position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
905
+ else:
906
+ position_ids = position_ids.view(-1, seq_length).long()
907
+
908
+ if inputs_embeds is None:
909
+ inputs_embeds = self.embed_tokens(input_ids)
910
+ # embed positions
911
+ if attention_mask is None:
912
+ attention_mask = torch.ones(
913
+ (batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device
914
+ )
915
+ padding_mask = None
916
+ else:
917
+ if 0 in attention_mask:
918
+ padding_mask = attention_mask
919
+ else:
920
+ padding_mask = None
921
+
922
+ attention_mask = self._prepare_decoder_attention_mask(
923
+ attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
924
+ )
925
+
926
+ hidden_states = inputs_embeds
927
+
928
+ if self.gradient_checkpointing and self.training:
929
+ if use_cache:
930
+ logger.warning_once(
931
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
932
+ )
933
+ use_cache = False
934
+
935
+ # decoder layers
936
+ all_hidden_states = () if output_hidden_states else None
937
+ all_self_attns = () if output_attentions else None
938
+ next_decoder_cache = () if use_cache else None
939
+
940
+ for idx, decoder_layer in enumerate(self.layers):
941
+ if output_hidden_states:
942
+ all_hidden_states += (hidden_states,)
943
+
944
+ past_key_value = past_key_values[idx] if past_key_values is not None else None
945
+
946
+ if self.gradient_checkpointing and self.training:
947
+
948
+ def create_custom_forward(module):
949
+ def custom_forward(*inputs):
950
+ # None for past_key_value
951
+ return module(*inputs, past_key_value, output_attentions, padding_mask=padding_mask)
952
+
953
+ return custom_forward
954
+
955
+ layer_outputs = torch.utils.checkpoint.checkpoint(
956
+ create_custom_forward(decoder_layer), hidden_states, attention_mask, position_ids
957
+ )
958
+ else:
959
+ layer_outputs = decoder_layer(
960
+ hidden_states,
961
+ attention_mask=attention_mask,
962
+ position_ids=position_ids,
963
+ past_key_value=past_key_value,
964
+ output_attentions=output_attentions,
965
+ use_cache=use_cache,
966
+ padding_mask=padding_mask,
967
+ )
968
+
969
+ hidden_states = layer_outputs[0]
970
+
971
+ if use_cache:
972
+ next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
973
+
974
+ if output_attentions:
975
+ all_self_attns += (layer_outputs[1],)
976
+
977
+ hidden_states = self.norm(hidden_states)
978
+
979
+ # add hidden states from the last decoder layer
980
+ if output_hidden_states:
981
+ all_hidden_states += (hidden_states,)
982
+
983
+ next_cache = next_decoder_cache if use_cache else None
984
+ if not return_dict:
985
+ return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
986
+ return BaseModelOutputWithPast(
987
+ last_hidden_state=hidden_states,
988
+ past_key_values=next_cache,
989
+ hidden_states=all_hidden_states,
990
+ attentions=all_self_attns,
991
+ )
992
+
993
+
994
+ class PolyLlamaForCausalLM(PolyLlamaPreTrainedModel):
995
+ _tied_weights_keys = ["lm_head.weight"]
996
+
997
+ def __init__(self, config):
998
+ super().__init__(config)
999
+ self.model = PolyLlamaModel(config)
1000
+ self.vocab_size = config.vocab_size
1001
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
1002
+
1003
+ # Initialize weights and apply final processing
1004
+ self.post_init()
1005
+
1006
+ def get_input_embeddings(self):
1007
+ return self.model.embed_tokens
1008
+
1009
+ def set_input_embeddings(self, value):
1010
+ self.model.embed_tokens = value
1011
+
1012
+ def get_output_embeddings(self):
1013
+ return self.lm_head
1014
+
1015
+ def set_output_embeddings(self, new_embeddings):
1016
+ self.lm_head = new_embeddings
1017
+
1018
+ def set_decoder(self, decoder):
1019
+ self.model = decoder
1020
+
1021
+ def get_decoder(self):
1022
+ return self.model
1023
+
1024
+ @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
1025
+ @replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
1026
+ def forward(
1027
+ self,
1028
+ input_ids: torch.LongTensor = None,
1029
+ attention_mask: Optional[torch.Tensor] = None,
1030
+ position_ids: Optional[torch.LongTensor] = None,
1031
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1032
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1033
+ labels: Optional[torch.LongTensor] = None,
1034
+ use_cache: Optional[bool] = None,
1035
+ output_attentions: Optional[bool] = None,
1036
+ output_hidden_states: Optional[bool] = None,
1037
+ return_dict: Optional[bool] = None,
1038
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
1039
+ r"""
1040
+ Args:
1041
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1042
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
1043
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1044
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
1045
+
1046
+ Returns:
1047
+
1048
+ Example:
1049
+
1050
+ ```python
1051
+ >>> from transformers import AutoTokenizer, LlamaForCausalLM
1052
+
1053
+ >>> model = LlamaForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
1054
+ >>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
1055
+
1056
+ >>> prompt = "Hey, are you conscious? Can you talk to me?"
1057
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
1058
+
1059
+ >>> # Generate
1060
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
1061
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1062
+ "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
1063
+ ```"""
1064
+
1065
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1066
+ output_hidden_states = (
1067
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1068
+ )
1069
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1070
+
1071
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1072
+ outputs = self.model(
1073
+ input_ids=input_ids,
1074
+ attention_mask=attention_mask,
1075
+ position_ids=position_ids,
1076
+ past_key_values=past_key_values,
1077
+ inputs_embeds=inputs_embeds,
1078
+ use_cache=use_cache,
1079
+ output_attentions=output_attentions,
1080
+ output_hidden_states=output_hidden_states,
1081
+ return_dict=return_dict,
1082
+ )
1083
+
1084
+ hidden_states = outputs[0]
1085
+ if self.config.pretraining_tp > 1:
1086
+ lm_head_slices = self.lm_head.weight.split(self.vocab_size // self.config.pretraining_tp, dim=0)
1087
+ logits = [F.linear(hidden_states, lm_head_slices[i]) for i in range(self.config.pretraining_tp)]
1088
+ logits = torch.cat(logits, dim=-1)
1089
+ else:
1090
+ logits = self.lm_head(hidden_states)
1091
+ logits = logits.float()
1092
+
1093
+ loss = None
1094
+ if labels is not None:
1095
+ # Shift so that tokens < n predict n
1096
+ shift_logits = logits[..., :-1, :].contiguous()
1097
+ shift_labels = labels[..., 1:].contiguous()
1098
+ # Flatten the tokens
1099
+ loss_fct = CrossEntropyLoss()
1100
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1101
+ shift_labels = shift_labels.view(-1)
1102
+ # Enable model parallelism
1103
+ shift_labels = shift_labels.to(shift_logits.device)
1104
+ loss = loss_fct(shift_logits, shift_labels)
1105
+
1106
+ if not return_dict:
1107
+ output = (logits,) + outputs[1:]
1108
+ return (loss,) + output if loss is not None else output
1109
+
1110
+ return CausalLMOutputWithPast(
1111
+ loss=loss,
1112
+ logits=logits,
1113
+ past_key_values=outputs.past_key_values,
1114
+ hidden_states=outputs.hidden_states,
1115
+ attentions=outputs.attentions,
1116
+ )
1117
+
1118
+ def prepare_inputs_for_generation(
1119
+ self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
1120
+ ):
1121
+ if past_key_values:
1122
+ input_ids = input_ids[:, -1:]
1123
+
1124
+ position_ids = kwargs.get("position_ids", None)
1125
+ if attention_mask is not None and position_ids is None:
1126
+ # create position_ids on the fly for batch generation
1127
+ position_ids = attention_mask.long().cumsum(-1) - 1
1128
+ position_ids.masked_fill_(attention_mask == 0, 1)
1129
+ if past_key_values:
1130
+ position_ids = position_ids[:, -1].unsqueeze(-1)
1131
+
1132
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
1133
+ if inputs_embeds is not None and past_key_values is None:
1134
+ model_inputs = {"inputs_embeds": inputs_embeds}
1135
+ else:
1136
+ model_inputs = {"input_ids": input_ids}
1137
+
1138
+ model_inputs.update(
1139
+ {
1140
+ "position_ids": position_ids,
1141
+ "past_key_values": past_key_values,
1142
+ "use_cache": kwargs.get("use_cache"),
1143
+ "attention_mask": attention_mask,
1144
+ }
1145
+ )
1146
+ return model_inputs
1147
+
1148
+ @staticmethod
1149
+ def _reorder_cache(past_key_values, beam_idx):
1150
+ reordered_past = ()
1151
+ for layer_past in past_key_values:
1152
+ reordered_past += (
1153
+ tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
1154
+ )
1155
+ return reordered_past
1156
+
1157
+
1158
+ @add_start_docstrings(
1159
+ """
1160
+ The LLaMa Model transformer with a sequence classification head on top (linear layer).
1161
+
1162
+ [`LlamaForSequenceClassification`] uses the last token in order to do the classification, as other causal models
1163
+ (e.g. GPT-2) do.
1164
+
1165
+ Since it does classification on the last token, it requires to know the position of the last token. If a
1166
+ `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
1167
+ no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
1168
+ padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
1169
+ each row of the batch).
1170
+ """,
1171
+ LLAMA_START_DOCSTRING,
1172
+ )
1173
+ class PolyLlamaForSequenceClassification(PolyLlamaPreTrainedModel):
1174
+ def __init__(self, config):
1175
+ super().__init__(config)
1176
+ self.num_labels = config.num_labels
1177
+ self.model = PolyLlamaModel(config)
1178
+ self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
1179
+
1180
+ # Initialize weights and apply final processing
1181
+ self.post_init()
1182
+
1183
+ def get_input_embeddings(self):
1184
+ return self.model.embed_tokens
1185
+
1186
+ def set_input_embeddings(self, value):
1187
+ self.model.embed_tokens = value
1188
+
1189
+ @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
1190
+ def forward(
1191
+ self,
1192
+ input_ids: torch.LongTensor = None,
1193
+ attention_mask: Optional[torch.Tensor] = None,
1194
+ position_ids: Optional[torch.LongTensor] = None,
1195
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1196
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1197
+ labels: Optional[torch.LongTensor] = None,
1198
+ use_cache: Optional[bool] = None,
1199
+ output_attentions: Optional[bool] = None,
1200
+ output_hidden_states: Optional[bool] = None,
1201
+ return_dict: Optional[bool] = None,
1202
+ ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
1203
+ r"""
1204
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1205
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1206
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1207
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1208
+ """
1209
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1210
+
1211
+ transformer_outputs = self.model(
1212
+ input_ids,
1213
+ attention_mask=attention_mask,
1214
+ position_ids=position_ids,
1215
+ past_key_values=past_key_values,
1216
+ inputs_embeds=inputs_embeds,
1217
+ use_cache=use_cache,
1218
+ output_attentions=output_attentions,
1219
+ output_hidden_states=output_hidden_states,
1220
+ return_dict=return_dict,
1221
+ )
1222
+ hidden_states = transformer_outputs[0]
1223
+ logits = self.score(hidden_states)
1224
+
1225
+ if input_ids is not None:
1226
+ batch_size = input_ids.shape[0]
1227
+ else:
1228
+ batch_size = inputs_embeds.shape[0]
1229
+
1230
+ if self.config.pad_token_id is None and batch_size != 1:
1231
+ raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
1232
+ if self.config.pad_token_id is None:
1233
+ sequence_lengths = -1
1234
+ else:
1235
+ if input_ids is not None:
1236
+ sequence_lengths = (torch.eq(input_ids, self.config.pad_token_id).long().argmax(-1) - 1).to(
1237
+ logits.device
1238
+ )
1239
+ else:
1240
+ sequence_lengths = -1
1241
+
1242
+ pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
1243
+
1244
+ loss = None
1245
+ if labels is not None:
1246
+ labels = labels.to(logits.device)
1247
+ if self.config.problem_type is None:
1248
+ if self.num_labels == 1:
1249
+ self.config.problem_type = "regression"
1250
+ elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
1251
+ self.config.problem_type = "single_label_classification"
1252
+ else:
1253
+ self.config.problem_type = "multi_label_classification"
1254
+
1255
+ if self.config.problem_type == "regression":
1256
+ loss_fct = MSELoss()
1257
+ if self.num_labels == 1:
1258
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
1259
+ else:
1260
+ loss = loss_fct(pooled_logits, labels)
1261
+ elif self.config.problem_type == "single_label_classification":
1262
+ loss_fct = CrossEntropyLoss()
1263
+ loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
1264
+ elif self.config.problem_type == "multi_label_classification":
1265
+ loss_fct = BCEWithLogitsLoss()
1266
+ loss = loss_fct(pooled_logits, labels)
1267
+ if not return_dict:
1268
+ output = (pooled_logits,) + transformer_outputs[1:]
1269
+ return ((loss,) + output) if loss is not None else output
1270
+
1271
+ return SequenceClassifierOutputWithPast(
1272
+ loss=loss,
1273
+ logits=pooled_logits,
1274
+ past_key_values=transformer_outputs.past_key_values,
1275
+ hidden_states=transformer_outputs.hidden_states,
1276
+ attentions=transformer_outputs.attentions,
1277
+ )
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:619485ecb332e5ee86768891e058cac0a38750393d9fb47e2fb8994dce8c112d
3
+ size 2690941596
special_tokens_map.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": true,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "</s>",
11
+ "lstrip": false,
12
+ "normalized": true,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": "<unk>",
17
+ "unk_token": {
18
+ "content": "<unk>",
19
+ "lstrip": false,
20
+ "normalized": true,
21
+ "rstrip": false,
22
+ "single_word": false
23
+ }
24
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_eos_token": false,
4
+ "bos_token": {
5
+ "__type": "AddedToken",
6
+ "content": "<s>",
7
+ "lstrip": false,
8
+ "normalized": true,
9
+ "rstrip": false,
10
+ "single_word": false
11
+ },
12
+ "clean_up_tokenization_spaces": false,
13
+ "eos_token": {
14
+ "__type": "AddedToken",
15
+ "content": "</s>",
16
+ "lstrip": false,
17
+ "normalized": true,
18
+ "rstrip": false,
19
+ "single_word": false
20
+ },
21
+ "legacy": false,
22
+ "model_max_length": 1000000000000000019884624838656,
23
+ "pad_token": null,
24
+ "sp_model_kwargs": {},
25
+ "tokenizer_class": "LlamaTokenizer",
26
+ "unk_token": {
27
+ "__type": "AddedToken",
28
+ "content": "<unk>",
29
+ "lstrip": false,
30
+ "normalized": true,
31
+ "rstrip": false,
32
+ "single_word": false
33
+ }
34
+ }