ITCL commited on
Commit
4b60860
1 Parent(s): b6a87e7

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +257 -0
README.md ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ datasets:
4
+ - abideen/Cosmopedia-100k-pretrain
5
+ language:
6
+ - en
7
+ base_model:
8
+ - meta-llama/Meta-Llama-3-8B
9
+ tags:
10
+ - bitnet
11
+ - LLMs
12
+ ---
13
+ # Llama3-8B-ITCL-Bitnet1.6B 🚀
14
+
15
+ ## Description 📜
16
+ **Llama3-8B-ITCL-Bitnet1.6B** is an experimental LLM model transformed from Llama3, optimized with bitlinear layers to enhance memory efficiency and inference speed. This model is designed for natural language processing tasks and is particularly useful in environments where resource-efficient performance is required. 🌟
17
+
18
+ <!-- ![image/png](https://cdn-uploads.huggingface.co/production/uploads/6419c2f6b4adb0e101b17b6c/WOcl2k9xdLT5aVqh-aERz.png) -->
19
+
20
+ <div align="center">
21
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/6419c2f6b4adb0e101b17b6c/KBPY239498r3rcFrDBx1l.png" alt="image/png" style="max-width: 40%; height: auto;" />
22
+ </div>
23
+
24
+ ## Features 🌈
25
+ - **Model Size:** 8B parameters 🧠
26
+ - **Architecture:** BitNet 🏗️
27
+ - **Bitlinear Layers:** Reduces weights to values of 1, 0, and -1. ➖
28
+ - **Optimized for:** Fast inference and memory efficiency ⚡
29
+
30
+ ## Architecture
31
+ ```bash
32
+ Model size: 1.604B parameters
33
+ 2024-10-08 14:53:07 - INFO - 🔢 Number of parameters in the model after extracting weights: 1
34
+ 2024-10-08 14:53:07 - INFO - 📏 Reduced model structure:
35
+ LlamaForCausalLM(
36
+ (model): LlamaModel(
37
+ (embed_tokens): Embedding(128256, 4096)
38
+ (layers): ModuleList(
39
+ (0-5): 6 x LlamaDecoderLayer(
40
+ (self_attn): LlamaSdpaAttention(
41
+ (q_proj): BitLinear(in_features=4096, out_features=4096, bias=False)
42
+ (k_proj): BitLinear(in_features=4096, out_features=4096, bias=False)
43
+ (v_proj): BitLinear(in_features=4096, out_features=4096, bias=False)
44
+ (o_proj): BitLinear(in_features=4096, out_features=4096, bias=False)
45
+ (rotary_emb): LlamaRotaryEmbedding()
46
+ )
47
+ (mlp): LlamaMLP(
48
+ (gate_proj): BitLinear(in_features=4096, out_features=2048, bias=False)
49
+ (up_proj): BitLinear(in_features=4096, out_features=2048, bias=False)
50
+ (down_proj): BitLinear(in_features=2048, out_features=4096, bias=False)
51
+ (act_fn): SiLU()
52
+ )
53
+ (input_layernorm): Identity()
54
+ (post_attention_layernorm): LlamaRMSNorm((4096,), eps=1e-05)
55
+ )
56
+ )
57
+ (norm): LlamaRMSNorm((4096,), eps=1e-05)
58
+ (rotary_emb): LlamaRotaryEmbedding()
59
+ )
60
+ (lm_head): Linear(in_features=4096, out_features=128256, bias=False)
61
+ )
62
+ ```
63
+
64
+ ## Requirements 📦
65
+ Make sure you have the following libraries installed:
66
+
67
+ ```bash
68
+ pip install transformers torch huggingface_hub wandb coloredlogs
69
+ ```
70
+
71
+
72
+ You can install these dependencies using pip! 🎉
73
+
74
+ ## Usage 🔍
75
+ ### Loading the Model
76
+ To load the model, you can simply run the following code:
77
+
78
+
79
+ Para usar este modelo, puedes cargarlo desde Hugging Face con el siguiente código:
80
+ ```python
81
+ from transformers import AutoModelForCausalLM, AutoTokenizer
82
+ from transformers.models.llama.modeling_llama import *
83
+ import torch
84
+ from torch import nn
85
+ import torch.nn.functional as F
86
+ import coloredlogs
87
+ import logging
88
+
89
+
90
+ coloredlogs.install(level='INFO', fmt='%(asctime)s - %(levelname)s - %(message)s', logger=logging.getLogger())
91
+ logger = logging.getLogger(__name__)
92
+
93
+
94
+
95
+
96
+ HF_TOKEN = "you_api_key_here"
97
+
98
+ model = "ejbejaranos/Llama3-8B-ITCL-Bitnet1.6B"
99
+
100
+ # Load a pretrained BitNet model
101
+ tokenizer = AutoTokenizer.from_pretrained(model)
102
+
103
+ model = AutoModelForCausalLM.from_pretrained(
104
+ model,
105
+ token=HF_TOKEN
106
+ )
107
+
108
+ # Establece el pad_token_id
109
+ model.config.pad_token_id = tokenizer.eos_token_id
110
+
111
+ def count_parameters(model):
112
+ # Calculate the number of parameters in billions
113
+ num_params = sum(p.numel() for p in model.parameters() if p.requires_grad) / 10**9
114
+ print(f"Model size: {num_params:.3f}B parameters")
115
+ return int(num_params)
116
+
117
+ def activation_quant(x):
118
+ scale = 127.0 / x.abs().max(dim=-1, keepdim=True).values.clamp_(min=1e-5)
119
+ y = (x * scale).round().clamp_(-128, 127)
120
+ y = y / scale
121
+ return y
122
+
123
+ def weight_quant(w):
124
+ scale = 1.0 / w.abs().mean().clamp_(min=1e-5)
125
+ u = (w * scale).round().clamp_(-1, 1)
126
+ u = u / scale
127
+ return u
128
+
129
+ class BitLinear(nn.Linear):
130
+ def forward(self, x):
131
+ w = self.weight # a weight tensor with shape [d, k]
132
+ x = x.to(w.device)
133
+ RMSNorm = LlamaRMSNorm(x.shape[-1]).to(w.device)
134
+ x_norm = RMSNorm(x)
135
+ x_quant = x_norm + (activation_quant(x_norm) - x_norm).detach()
136
+ w_quant = w + (weight_quant(w) - w).detach()
137
+ y = F.linear(x_quant, w_quant)
138
+ return y
139
+
140
+ def convert_to_bitnet(model, copy_weights):
141
+ for name, module in model.named_modules():
142
+ if isinstance(module, LlamaSdpaAttention) or isinstance(module, LlamaMLP):
143
+ for child_name, child_module in module.named_children():
144
+ if isinstance(child_module, nn.Linear):
145
+ bitlinear = BitLinear(child_module.in_features, child_module.out_features, child_module.bias is not None).to(device="cuda:0")
146
+ if copy_weights:
147
+ bitlinear.weight = child_module.weight
148
+ if child_module.bias is not None:
149
+ bitlinear.bias = child_module.bias
150
+ setattr(module, child_name, bitlinear)
151
+ elif isinstance(module, LlamaDecoderLayer):
152
+ for child_name, child_module in module.named_children():
153
+ if isinstance(child_module, LlamaRMSNorm) and child_name == "input_layernorm":
154
+ setattr(module, child_name, nn.Identity().to(device="cuda:0"))
155
+
156
+ convert_to_bitnet(model, copy_weights=True)
157
+ model.to(device="cuda:0")
158
+
159
+
160
+ logger.info(f"🔢 Number of parameters in the model after extracting weights: {count_parameters(model)}")
161
+ logger.info(f"📏 Reduced model structure:\n{model}")
162
+
163
+
164
+
165
+
166
+
167
+ prompt = "What is the color of sky?"
168
+ inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True).to(model.device)
169
+ inputs['attention_mask'] = inputs['input_ids'] != model.config.pad_token_id
170
+
171
+ generate_ids = model.generate(inputs.input_ids, attention_mask=inputs['attention_mask'], max_length=250)
172
+ decoded_output = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
173
+
174
+ print(decoded_output[0]) # Print the generated response
175
+
176
+
177
+ ```
178
+
179
+
180
+ ### Performing Inference
181
+ Generate text using the model to unleash its power! 💬✨
182
+
183
+ ```text
184
+ - "What is the color of sky?"
185
+
186
+ It's a question that has been debated since the last days of the Soviet Union. But what is the color of the sky? And what is the color of the sky? In this blog post, we will explore the meaning of the color of the sky, and how it relates to the broader context of the Soviet Union.
187
+
188
+ The color of the sky is a measure of the brightness of the sky. It is measured in degrees Fahrenheit (°C), which is measured in degrees Celsius (°C). The color of the sky is measured in degrees Fahrenheit (°C), with the average value of the atmosphere being measured in degrees Fahrenheit (°C). This is measured in degrees Fahrenheit (°C), which is measured in degrees Celsius (°C)."
189
+
190
+ Do not just list concepts, but develop each one in detail before moving to the next, as we prioritize depth of understanding and comprehensive exploration of the subject matter over breadth. Focus on:
191
+
192
+ - Rigor: Ensure in-depth coverage of the concepts/sections.
193
+ - Engagement: Write with an academic, professional and engaging tone that captivates interest.
194
+ ```
195
+
196
+
197
+ ```text
198
+ - How do you ensure the security of your APIs?
199
+
200
+ It's essential to understand that there are different ways to configure your system, including setting up a secure authentication system, configuring access controls, and configuring access controls. This article aims to provide a comprehensive overview of these steps while highlighting their significance in computer programming and web development.
201
+
202
+ **Step 1: Define Your Security Policy**
203
+ Before diving into configuring access control, it's crucial to define your security policy. This includes defining roles, responsibilities, and liabilities. A well-defined security policy helps ensure that your organization understands and respects your privacy rights. Key components include:
204
+
205
+ * **Identity Provider** (IdP): A type of service provider that provides access to a user's private data. They offer various services such as encryption, access controls, and access control.
206
+ * **Security Provider** (SP): A type of service provider that offers a range of security features, including encryption, authentication, and authorization. They also provide access controls and authentication mechanisms.
207
+ * **Security Provider** (SP): A type of service provider that provides security and authentication for your users. They provide access control and authentication mechanisms to protect your data.
208
+ ```
209
+
210
+ ```text
211
+ - How do you stay updated with the latest trends in deep learning?
212
+
213
+ The following sections provide a comprehensive guide to help you stay updated on the latest developments in deep learning.
214
+
215
+ Step 1: Familiarize Yourself with the Basics of Deep Learning
216
+ Before diving into the world of deep learning, take some time to understand the basics of deep learning. This includes knowing the basics of deep learning, including its importance in deep learning, and the steps required to master deep learning. For beginners, this might involve reading a book, watching a video, or reading a book. Understanding these foundational concepts will enable you to grasp the basics of deep learning effectively.
217
+
218
+ Key Tip: Keep your language and content simple, focusing on essential skills rather than technical terms. This will help you better understand the material and engage more effectively with the material.
219
+
220
+ Step 2: Learn About Deep Learning
221
+ Deep learning is a type of deep learning that involves acquiring knowledge in a specific area of interest. Deep learning, on the other hand, refers to the process of acquiring knowledge in a specific area of interest. It is a type of deep learning that emphasizes deep learning and”
222
+
223
+ The story should include:
224
+ - Niche interests or humor: dive into specific hobbies, interests, or humorous situations
225
+ ```
226
+
227
+ ```text
228
+ - What role does explainability play in your AI solutions?
229
+ Are there any limitations or tradeoffs associated with AI implementation? These questions warrant further exploration.
230
+
231
+ In conclusion, AI holds immense promise for transforming various aspects of our lives, from healthcare to entertainment to social sciences.
232
+ While it's essential to recognize the potential benefits of AI, it also presents challenges related to privacy, bias, and ethical considerations.
233
+ By staying informed and engaged, we can harness the power of AI responsibly and effectively.
234
+ After all, every great AI tool deserves to be used responsibly, regardless of its size or scope.
235
+ So let's keep exploring, questioning, and learning! Together, we can harness the power of AI to improve healthcare and make a difference in the world.
236
+ Happy coding! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟! 🌟!
237
+ ```
238
+
239
+
240
+ ## Training 🏋️
241
+ To train the model, configure your settings and implement your training logic. 🛠️
242
+
243
+ ## Contributions 🤝
244
+ If you would like to contribute to this project, please follow these steps:
245
+ 1. Fork the repository. 🍴
246
+ 2. Create your branch (`git checkout -b feature-new-feature`). 🌿
247
+ 3. Make your changes and commit. 📅
248
+ 4. Push to the branch. 📤
249
+ 5. Open a Pull Request. 📬
250
+
251
+ ## License 📄
252
+ This project is licensed under the MIT License. See the `LICENSE` file for details.
253
+
254
+ ## Contact 📫
255
+ For questions or suggestions, feel free to reach out to me:
256
+ - **Email:** edison.bejarano@itcl.es
257
+ - **GitHub:** [ejbejaranos](https://github.com/ejbejaranos) 🌐