LMLK commited on
Commit
7cd6401
1 Parent(s): 7a17876

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +211 -1
README.md CHANGED
@@ -2,4 +2,214 @@
2
  license: apache-2.0
3
  pipeline_tag: text-generation
4
  library_name: transformers
5
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  license: apache-2.0
3
  pipeline_tag: text-generation
4
  library_name: transformers
5
+ ---
6
+
7
+ # AMD-135m
8
+
9
+
10
+ ## Introduction
11
+ AMD-Llama-135m is a language model trained on AMD MI250 GPUs. Based on LLaMA2 model architecture, this model can be smoothly loaded as LlamaForCausalLM with huggingface transformers. Furthermore, we use the same tokenizer as LLaMA2, enabling it to be a draft model of speculative decoding for LLaMA2 and CodeLlama.
12
+
13
+ ```python
14
+ import sys
15
+ import os
16
+ from PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QPushButton,
17
+ QLineEdit, QTextEdit, QVBoxLayout, QHBoxLayout,
18
+ QFileDialog, QProgressBar, QMessageBox, QMenu)
19
+ from PyQt5.QtCore import Qt, QThread, pyqtSignal
20
+ from llama_cpp import Llama
21
+
22
+
23
+ class Worker(QThread):
24
+ finished = pyqtSignal(str)
25
+ progress = pyqtSignal(int, int) # Pass total tokens as well
26
+
27
+ def __init__(self, model, messages, max_tokens):
28
+ super().__init__()
29
+ self.model = model
30
+ self.messages = messages
31
+ self.max_tokens = max_tokens
32
+
33
+ def run(self):
34
+ try:
35
+ response = self.model.create_chat_completion(
36
+ messages=self.messages,
37
+ max_tokens=self.max_tokens,
38
+ temperature=0.7,
39
+ stream=True
40
+ )
41
+
42
+ total_tokens = 0
43
+ full_response = ""
44
+ for chunk in response:
45
+ if "choices" in chunk:
46
+ content = chunk["choices"][0]["delta"].get("content", "")
47
+ full_response += content
48
+ total_tokens += 1 # Assume each chunk is 1 token (adjust if needed)
49
+ self.progress.emit(total_tokens, self.max_tokens)
50
+ self.finished.emit(full_response)
51
+ except Exception as e:
52
+ self.finished.emit(f"Error generating response: {str(e)}")
53
+
54
+
55
+ class ChatbotGUI(QWidget):
56
+ def __init__(self):
57
+ super().__init__()
58
+ self.setWindowTitle("Chatbot GUI")
59
+ self.resize(800, 600)
60
+
61
+ self.model = None
62
+ self.messages = [
63
+ {"role": "system", "content": "You are a helpful AI assistant."}
64
+ ]
65
+
66
+ self.initUI()
67
+
68
+ def initUI(self):
69
+ # Model loading section
70
+ model_label = QLabel("Model: No model loaded")
71
+ load_button = QPushButton("Load GGUF Model")
72
+ load_button.clicked.connect(self.load_model)
73
+
74
+ model_layout = QHBoxLayout()
75
+ model_layout.addWidget(model_label)
76
+ model_layout.addWidget(load_button)
77
+
78
+ # Chat display
79
+ self.chat_display = QTextEdit()
80
+ self.chat_display.setReadOnly(True)
81
+ self.chat_display.setContextMenuPolicy(Qt.CustomContextMenu)
82
+ self.chat_display.customContextMenuRequested.connect(self.show_context_menu)
83
+
84
+ # User input
85
+ self.user_input = QLineEdit()
86
+ self.user_input.returnPressed.connect(self.send_message)
87
+ send_button = QPushButton("Send")
88
+ send_button.clicked.connect(self.send_message)
89
+
90
+ input_layout = QHBoxLayout()
91
+ input_layout.addWidget(self.user_input)
92
+ input_layout.addWidget(send_button)
93
+
94
+ # Progress bar
95
+ self.progress_bar = QProgressBar()
96
+ self.progress_bar.hide()
97
+
98
+ # Clear conversation button
99
+ clear_button = QPushButton("Clear Conversation")
100
+ clear_button.clicked.connect(self.clear_conversation)
101
+
102
+ # Main layout
103
+ main_layout = QVBoxLayout()
104
+ main_layout.addLayout(model_layout)
105
+ main_layout.addWidget(self.chat_display)
106
+ main_layout.addWidget(self.progress_bar)
107
+ main_layout.addLayout(input_layout)
108
+ main_layout.addWidget(clear_button)
109
+
110
+ self.setLayout(main_layout)
111
+
112
+ def load_model(self):
113
+ model_path, _ = QFileDialog.getOpenFileName(self, "Load GGUF Model", "", "GGUF Files (*.gguf)")
114
+ if model_path:
115
+ try:
116
+ self.model = Llama(model_path=model_path, n_ctx=2048, n_gpu_layers=-1)
117
+ model_name = os.path.basename(model_path)
118
+ self.layout().itemAt(0).itemAt(0).widget().setText(f"Model: {model_name}")
119
+ QMessageBox.information(self, "Success", "Model loaded successfully!")
120
+ except Exception as e:
121
+ error_message = f"Error loading model: {str(e)}"
122
+ QMessageBox.critical(self, "Error", error_message)
123
+
124
+ def send_message(self):
125
+ user_message = self.user_input.text()
126
+ if user_message and self.model:
127
+ self.messages.append({"role": "user", "content": user_message})
128
+ self.update_chat_display(f"You: {user_message}")
129
+ self.user_input.clear()
130
+
131
+ max_tokens = 500 # Set your desired max tokens here
132
+ self.progress_bar.show()
133
+ self.progress_bar.setRange(0, max_tokens)
134
+ self.progress_bar.setValue(0)
135
+
136
+ self.worker = Worker(self.model, self.messages, max_tokens)
137
+ self.worker.finished.connect(self.on_response_finished)
138
+ self.worker.progress.connect(self.on_response_progress)
139
+ self.worker.start()
140
+
141
+ def on_response_finished(self, assistant_message):
142
+ self.progress_bar.hide()
143
+ self.messages.append({"role": "assistant", "content": assistant_message})
144
+ self.update_chat_display(f"Assistant: {assistant_message}")
145
+
146
+ # Python Code Download (Check for triple backticks)
147
+ if assistant_message.startswith("```python") and assistant_message.endswith("```"):
148
+ self.offer_code_download(assistant_message)
149
+
150
+ def on_response_progress(self, current_tokens, total_tokens):
151
+ self.progress_bar.setValue(current_tokens)
152
+
153
+ def offer_code_download(self, code):
154
+ reply = QMessageBox.question(self, "Download Code",
155
+ "The assistant generated Python code. Do you want to download it?",
156
+ QMessageBox.Yes | QMessageBox.No)
157
+ if reply == QMessageBox.Yes:
158
+ file_path, _ = QFileDialog.getSaveFileName(self, "Save Python Code", "code.py", "Python Files (*.py)")
159
+ if file_path:
160
+ try:
161
+ with open(file_path, "w") as f:
162
+ f.write(code.strip("```python").strip("```"))
163
+ QMessageBox.information(self, "Success", "Code saved successfully!")
164
+ except Exception as e:
165
+ QMessageBox.critical(self, "Error", f"Error saving code: {str(e)}")
166
+
167
+ def update_chat_display(self, message):
168
+ self.chat_display.append(message + "\n")
169
+ self.chat_display.verticalScrollBar().setValue(self.chat_display.verticalScrollBar().maximum())
170
+
171
+ def clear_conversation(self):
172
+ self.messages = [
173
+ {"role": "system", "content": "You are a helpful AI assistant."}
174
+ ]
175
+ self.chat_display.clear()
176
+
177
+ def show_context_menu(self, point):
178
+ menu = QMenu(self)
179
+ copy_action = menu.addAction("Copy")
180
+ copy_action.triggered.connect(self.copy_text)
181
+ menu.exec_(self.chat_display.mapToGlobal(point))
182
+
183
+ def copy_text(self):
184
+ cursor = self.chat_display.textCursor()
185
+ if cursor.hasSelection():
186
+ text = cursor.selectedText()
187
+ QApplication.clipboard().setText(text)
188
+
189
+
190
+ if __name__ == "__main__":
191
+ app = QApplication(sys.argv)
192
+ gui = ChatbotGUI()
193
+ gui.show()
194
+ sys.exit(app.exec_())
195
+ ```
196
+
197
+ ## Training and finetuning cost
198
+ It takes 6 days to pretrain AMD-Llama-135m on 4 MI250 nodes each of which has 4 MI250 GPUs (8 virtual GPU cards, 64G memory for each).
199
+ It takes 4 days to finetune AMD-Llama-135m-code on 4 MI250 GPUs.
200
+ It takes 11T disk space to store raw and processed SlimPajama, project gutenberg and Starcoder datasets.
201
+
202
+ #### License
203
+ Copyright (c) 2018-2024 Advanced Micro Devices, Inc. All Rights Reserved.
204
+
205
+ Licensed under the Apache License, Version 2.0 (the "License");
206
+ you may not use this file except in compliance with the License.
207
+ You may obtain a copy of the License at
208
+
209
+ http://www.apache.org/licenses/LICENSE-2.0
210
+
211
+ Unless required by applicable law or agreed to in writing, software
212
+ distributed under the License is distributed on an "AS IS" BASIS,
213
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
214
+ See the License for the specific language governing permissions and
215
+ limitations under the License.