Upload configuration_bitnet.py

#35
Files changed (1) hide show
  1. configuration_bitnet.py +147 -0
configuration_bitnet.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2025 The BitNet Team and The HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ """BitNet model configuration"""
15
+
16
+ from ...configuration_utils import PretrainedConfig
17
+ from ...utils import logging
18
+
19
+
20
+ logger = logging.get_logger(__name__)
21
+
22
+
23
+ class BitNetConfig(PretrainedConfig):
24
+ r"""
25
+ This is the configuration class to store the configuration of a [`BitNetModel`]. It is used to instantiate an BitNet
26
+ model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
27
+ defaults will yield a similar configuration to that of
28
+ BitNet b1.58 2B4T [microsoft/bitnet-b1.58-2B-4T](https://huggingface.co/microsoft/bitnet-b1.58-2B-4T).
29
+
30
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
31
+ documentation from [`PretrainedConfig`] for more information.
32
+
33
+
34
+ Args:
35
+ vocab_size (`int`, *optional*, defaults to 128256):
36
+ Vocabulary size of the BitNet model. Defines the number of different tokens that can be represented by the
37
+ `inputs_ids` passed when calling [`BitNetModel`]
38
+ hidden_size (`int`, *optional*, defaults to 2560):
39
+ Dimension of the hidden representations.
40
+ intermediate_size (`int`, *optional*, defaults to 6912):
41
+ Dimension of the MLP representations.
42
+ num_hidden_layers (`int`, *optional*, defaults to 30):
43
+ Number of hidden layers in the Transformer decoder.
44
+ num_attention_heads (`int`, *optional*, defaults to 20):
45
+ Number of attention heads for each attention layer in the Transformer decoder.
46
+ num_key_value_heads (`int`, *optional*, defaults to 5):
47
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
48
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
49
+ `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
50
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
51
+ by meanpooling all the original heads within that group. For more details checkout [this
52
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
53
+ `num_attention_heads`.
54
+ hidden_act (`str` or `function`, *optional*, defaults to `"relu2"`):
55
+ The non-linear activation function (function or string) in the decoder.
56
+ max_position_embeddings (`int`, *optional*, defaults to 2048):
57
+ The maximum sequence length that this model might ever be used with.
58
+ initializer_range (`float`, *optional*, defaults to 0.02):
59
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
60
+ rms_norm_eps (`float`, *optional*, defaults to 1e-05):
61
+ The epsilon used by the rms normalization layers.
62
+ use_cache (`bool`, *optional*, defaults to `True`):
63
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
64
+ relevant if `config.is_decoder=True`.
65
+ pad_token_id (`int`, *optional*):
66
+ Padding token id.
67
+ bos_token_id (`int`, *optional*, defaults to 128000):
68
+ Beginning of stream token id.
69
+ eos_token_id (`int`, *optional*, defaults to 128001):
70
+ End of stream token id.
71
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
72
+ Whether to tie weight embeddings
73
+ rope_theta (`float`, *optional*, defaults to 500000.0):
74
+ The base period of the RoPE embeddings.
75
+ attention_bias (`bool`, *optional*, defaults to `False`):
76
+ Whether to use a bias in the query, key, value and output projection layers during self-attention.
77
+ attention_dropout (`float`, *optional*, defaults to 0.0):
78
+ The dropout ratio for the attention probabilities.
79
+
80
+ ```python
81
+ >>> from transformers import BitNetModel, BitNetConfig
82
+
83
+ >>> # Initializing a BitNet style configuration
84
+ >>> configuration = BitNetConfig()
85
+
86
+ >>> # Initializing a model from the BitNet style configuration
87
+ >>> model = BitNetModel(configuration)
88
+
89
+ >>> # Accessing the model configuration
90
+ >>> configuration = model.config
91
+ ```"""
92
+
93
+ model_type = "bitnet"
94
+ keys_to_ignore_at_inference = ["past_key_values"]
95
+
96
+ def __init__(
97
+ self,
98
+ vocab_size=128256,
99
+ hidden_size=2560,
100
+ intermediate_size=6912,
101
+ num_hidden_layers=30,
102
+ num_attention_heads=20,
103
+ num_key_value_heads=5,
104
+ hidden_act="relu2",
105
+ max_position_embeddings=2048,
106
+ initializer_range=0.02,
107
+ rms_norm_eps=1e-5,
108
+ use_cache=True,
109
+ pad_token_id=None,
110
+ bos_token_id=128000,
111
+ eos_token_id=128001,
112
+ tie_word_embeddings=False,
113
+ rope_theta=500000.0,
114
+ attention_bias=False,
115
+ attention_dropout=0.0,
116
+ **kwargs,
117
+ ):
118
+ self.vocab_size = vocab_size
119
+ self.max_position_embeddings = max_position_embeddings
120
+ self.hidden_size = hidden_size
121
+ self.intermediate_size = intermediate_size
122
+ self.num_hidden_layers = num_hidden_layers
123
+ self.num_attention_heads = num_attention_heads
124
+
125
+ # for backward compatibility
126
+ if num_key_value_heads is None:
127
+ num_key_value_heads = num_attention_heads
128
+
129
+ self.num_key_value_heads = num_key_value_heads
130
+ self.hidden_act = hidden_act
131
+ self.initializer_range = initializer_range
132
+ self.rms_norm_eps = rms_norm_eps
133
+ self.use_cache = use_cache
134
+ self.rope_theta = rope_theta
135
+ self.attention_bias = attention_bias
136
+ self.attention_dropout = attention_dropout
137
+
138
+ super().__init__(
139
+ pad_token_id=pad_token_id,
140
+ bos_token_id=bos_token_id,
141
+ eos_token_id=eos_token_id,
142
+ tie_word_embeddings=tie_word_embeddings,
143
+ **kwargs,
144
+ )
145
+
146
+
147
+ __all__ = ["BitNetConfig"]