Upload configuration_magma.py
Browse files- configuration_magma.py +144 -0
configuration_magma.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""Magma model configuration"""
|
21 |
+
|
22 |
+
from transformers.configuration_utils import PretrainedConfig
|
23 |
+
from transformers.utils import logging
|
24 |
+
from transformers.models.auto import CONFIG_MAPPING
|
25 |
+
|
26 |
+
logger = logging.get_logger(__name__)
|
27 |
+
|
28 |
+
|
29 |
+
class MagmaConfig(PretrainedConfig):
|
30 |
+
r"""
|
31 |
+
This is the configuration class to store the configuration of a [`MagmaModel`]. It is used to instantiate an Magma
|
32 |
+
model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
|
33 |
+
defaults will yield a similar configuration to that of the Magma-7B.
|
34 |
+
|
35 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
36 |
+
documentation from [`PretrainedConfig`] for more information.
|
37 |
+
|
38 |
+
|
39 |
+
Args:
|
40 |
+
vocab_size (`int`, *optional*, defaults to 32000):
|
41 |
+
Vocabulary size of the Magma model. Defines the number of different tokens that can be represented by the
|
42 |
+
`inputs_ids` passed when calling [`MagmaModel`]
|
43 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
44 |
+
Dimension of the hidden representations.
|
45 |
+
intermediate_size (`int`, *optional*, defaults to 11008):
|
46 |
+
Dimension of the MLP representations.
|
47 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
48 |
+
Number of hidden layers in the Transformer decoder.
|
49 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
50 |
+
Number of attention heads for each attention layer in the Transformer decoder.
|
51 |
+
num_key_value_heads (`int`, *optional*):
|
52 |
+
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
|
53 |
+
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
|
54 |
+
`num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
|
55 |
+
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
|
56 |
+
by meanpooling all the original heads within that group. For more details checkout [this
|
57 |
+
paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
|
58 |
+
`num_attention_heads`.
|
59 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
60 |
+
The non-linear activation function (function or string) in the decoder.
|
61 |
+
max_position_embeddings (`int`, *optional*, defaults to 2048):
|
62 |
+
The maximum sequence length that this model might ever be used with. Magma 1 supports up to 2048 tokens,
|
63 |
+
Magma 2 up to 4096, CodeMagma up to 16384.
|
64 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
65 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
66 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-06):
|
67 |
+
The epsilon used by the rms normalization layers.
|
68 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
69 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
70 |
+
relevant if `config.is_decoder=True`.
|
71 |
+
pad_token_id (`int`, *optional*):
|
72 |
+
Padding token id.
|
73 |
+
bos_token_id (`int`, *optional*, defaults to 1):
|
74 |
+
Beginning of stream token id.
|
75 |
+
eos_token_id (`int`, *optional*, defaults to 2):
|
76 |
+
End of stream token id.
|
77 |
+
pretraining_tp (`int`, *optional*, defaults to 1):
|
78 |
+
Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
|
79 |
+
document](https://huggingface.co/docs/transformers/main/perf_train_gpu_many#tensor-parallelism) to understand more about it. This value is
|
80 |
+
necessary to ensure exact reproducibility of the pretraining results. Please refer to [this
|
81 |
+
issue](https://github.com/pytorch/pytorch/issues/76232).
|
82 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
83 |
+
Whether to tie weight embeddings
|
84 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
85 |
+
The base period of the RoPE embeddings.
|
86 |
+
rope_scaling (`Dict`, *optional*):
|
87 |
+
Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
|
88 |
+
strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
|
89 |
+
`{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
|
90 |
+
`max_position_embeddings` to the expected new maximum.
|
91 |
+
attention_bias (`bool`, *optional*, defaults to `False`):
|
92 |
+
Whether to use a bias in the query, key, value and output projection layers during self-attention.
|
93 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
94 |
+
The dropout ratio for the attention probabilities.
|
95 |
+
mlp_bias (`bool`, *optional*, defaults to `False`):
|
96 |
+
Whether to use a bias in up_proj, down_proj and gate_proj layers in the MLP layers.
|
97 |
+
|
98 |
+
```python
|
99 |
+
>>> from transformers import MagmaModel, MagmaConfig
|
100 |
+
|
101 |
+
>>> # Initializing a Magma magma-7b style configuration
|
102 |
+
>>> configuration = MagmaConfig()
|
103 |
+
|
104 |
+
>>> # Initializing a model from the magma-7b style configuration
|
105 |
+
>>> model = MagmaModel(configuration)
|
106 |
+
|
107 |
+
>>> # Accessing the model configuration
|
108 |
+
>>> configuration = model.config
|
109 |
+
```"""
|
110 |
+
|
111 |
+
model_type = "magma"
|
112 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
113 |
+
|
114 |
+
def __init__(
|
115 |
+
self,
|
116 |
+
vision_config=None,
|
117 |
+
text_config=None,
|
118 |
+
image_token_index=None,
|
119 |
+
tie_word_embeddings=False,
|
120 |
+
**kwargs,
|
121 |
+
):
|
122 |
+
self.vision_config = vision_config
|
123 |
+
self.image_token_index = image_token_index
|
124 |
+
|
125 |
+
if isinstance(text_config, dict):
|
126 |
+
text_config["model_type"] = text_config["model_type"] if "model_type" in text_config else "llama"
|
127 |
+
text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config)
|
128 |
+
elif text_config is None:
|
129 |
+
if "model_type" in kwargs:
|
130 |
+
text_config = CONFIG_MAPPING[kwargs["model_type"]](**kwargs)
|
131 |
+
|
132 |
+
if text_config is not None:
|
133 |
+
# copy all variables in text_config to self
|
134 |
+
for key, value in text_config.__dict__.items():
|
135 |
+
if not key.startswith("_") and not key.startswith("__"):
|
136 |
+
setattr(self, key, value)
|
137 |
+
self.text_config = text_config
|
138 |
+
else:
|
139 |
+
self.text_config = None
|
140 |
+
|
141 |
+
super().__init__(
|
142 |
+
tie_word_embeddings=tie_word_embeddings,
|
143 |
+
**kwargs,
|
144 |
+
)
|