Update README.md
Browse files
README.md
CHANGED
@@ -1,336 +1,131 @@
|
|
1 |
---
|
2 |
library_name: transformers
|
3 |
-
language:
|
4 |
-
- en
|
5 |
-
- fr
|
6 |
-
- de
|
7 |
-
- es
|
8 |
-
- it
|
9 |
-
- pt
|
10 |
-
- ja
|
11 |
-
- ko
|
12 |
-
- zh
|
13 |
-
- ar
|
14 |
license: cc-by-nc-4.0
|
|
|
|
|
|
|
|
|
15 |
---
|
16 |
|
17 |
-
|
18 |
|
19 |
-
|
20 |
|
21 |
-
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
Developed by: Cohere and [Cohere For AI](https://cohere.for.ai)
|
26 |
|
27 |
-
|
28 |
-
- License: [CC-BY-NC](https://cohere.com/c4ai-cc-by-nc-license), requires also adhering to [C4AI's Acceptable Use Policy](https://docs.cohere.com/docs/c4ai-acceptable-use-policy)
|
29 |
-
- Model: c4ai-command-r-v01
|
30 |
-
- Model Size: 35 billion parameters
|
31 |
-
- Context length: 128K
|
32 |
|
33 |
-
|
34 |
|
35 |
-
|
36 |
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
```python
|
41 |
-
# pip install 'transformers>=4.39.1'
|
42 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
43 |
|
44 |
-
|
45 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
46 |
-
model = AutoModelForCausalLM.from_pretrained(model_id)
|
47 |
|
48 |
-
|
49 |
-
messages = [{"role": "user", "content": "Hello, how are you?"}]
|
50 |
-
input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
|
51 |
-
## <BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|>Hello, how are you?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
|
52 |
-
|
53 |
-
gen_tokens = model.generate(
|
54 |
-
input_ids,
|
55 |
-
max_new_tokens=100,
|
56 |
-
do_sample=True,
|
57 |
-
temperature=0.3,
|
58 |
-
)
|
59 |
-
|
60 |
-
gen_text = tokenizer.decode(gen_tokens[0])
|
61 |
-
print(gen_text)
|
62 |
-
```
|
63 |
-
|
64 |
-
**Quantized model through bitsandbytes, 8-bit precision**
|
65 |
|
66 |
```python
|
67 |
-
#
|
68 |
-
|
69 |
-
|
70 |
-
bnb_config = BitsAndBytesConfig(load_in_8bit=True)
|
71 |
-
|
72 |
-
model_id = "CohereForAI/c4ai-command-r-v01"
|
73 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
74 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=bnb_config)
|
75 |
-
|
76 |
-
# Format message with the command-r chat template
|
77 |
-
messages = [{"role": "user", "content": "Hello, how are you?"}]
|
78 |
-
input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
|
79 |
-
## <BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|>Hello, how are you?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
|
80 |
-
|
81 |
-
gen_tokens = model.generate(
|
82 |
-
input_ids,
|
83 |
-
max_new_tokens=100,
|
84 |
-
do_sample=True,
|
85 |
-
temperature=0.3,
|
86 |
-
)
|
87 |
-
|
88 |
-
gen_text = tokenizer.decode(gen_tokens[0])
|
89 |
-
print(gen_text)
|
90 |
```
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
You can find a quantized version of this model to 4-bit precision [here](https://huggingface.co/CohereForAI/c4ai-command-r-v01-4bit).
|
95 |
-
|
96 |
-
## Model Details
|
97 |
-
|
98 |
-
**Input**: Models input text only.
|
99 |
-
|
100 |
-
**Output**: Models generate text only.
|
101 |
-
|
102 |
-
**Model Architecture**: This is an auto-regressive language model that uses an optimized transformer architecture. After pretraining, this model uses supervised fine-tuning (SFT) and preference training to align model behavior to human preferences for helpfulness and safety.
|
103 |
-
|
104 |
-
**Languages covered**: The model is optimized to perform well in the following languages: English, French, Spanish, Italian, German, Brazilian Portuguese, Japanese, Korean, Simplified Chinese, and Arabic.
|
105 |
-
|
106 |
-
Pre-training data additionally included the following 13 languages: Russian, Polish, Turkish, Vietnamese, Dutch, Czech, Indonesian, Ukrainian, Romanian, Greek, Hindi, Hebrew, Persian.
|
107 |
-
|
108 |
-
**Context length**: Command-R supports a context length of 128K.
|
109 |
-
|
110 |
-
### Tool use capabilities:
|
111 |
-
|
112 |
-
Command-R has been specifically trained with conversational tool use capabilities. These have been trained into the model via a mixture of supervised fine-tuning and preference fine-tuning, using a specific prompt template. Deviating from this prompt template will likely reduce performance, but we encourage experimentation.
|
113 |
-
|
114 |
-
Command-R’s tool use functionality takes a conversation as input (with an optional user-system preamble), along with a list of available tools. The model will then generate a json-formatted list of actions to execute on a subset of those tools. Command-R may use one of its supplied tools more than once.
|
115 |
-
|
116 |
-
The model has been trained to recognise a special `directly_answer` tool, which it uses to indicate that it doesn’t want to use any of its other tools. The ability to abstain from calling a specific tool can be useful in a range of situations, such as greeting a user, or asking clarifying questions.
|
117 |
-
We recommend including the `directly_answer` tool, but it can be removed or renamed if required.
|
118 |
-
|
119 |
-
Comprehensive documentation for working with command-R's tool use prompt template can be found [here](https://docs.cohere.com/docs/prompting-command-r).
|
120 |
-
|
121 |
-
The code snippet below shows a minimal working example on how to render a prompt.
|
122 |
-
|
123 |
-
<details>
|
124 |
-
<summary><b>Usage: Rendering Tool Use Prompts [CLICK TO EXPAND]</b> </summary>
|
125 |
|
126 |
```python
|
127 |
-
|
128 |
-
|
129 |
-
model_id = "CohereForAI/c4ai-command-r-v01"
|
130 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
131 |
-
|
132 |
-
# define conversation input:
|
133 |
-
conversation = [
|
134 |
-
{"role": "user", "content": "Whats the biggest penguin in the world?"}
|
135 |
-
]
|
136 |
-
# Define tools available for the model to use:
|
137 |
-
tools = [
|
138 |
-
{
|
139 |
-
"name": "internet_search",
|
140 |
-
"description": "Returns a list of relevant document snippets for a textual query retrieved from the internet",
|
141 |
-
"parameter_definitions": {
|
142 |
-
"query": {
|
143 |
-
"description": "Query to search the internet with",
|
144 |
-
"type": 'str',
|
145 |
-
"required": True
|
146 |
-
}
|
147 |
-
}
|
148 |
-
},
|
149 |
-
{
|
150 |
-
'name': "directly_answer",
|
151 |
-
"description": "Calls a standard (un-augmented) AI chatbot to generate a response given the conversation history",
|
152 |
-
'parameter_definitions': {}
|
153 |
-
}
|
154 |
-
]
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
tools=tools,
|
160 |
-
tokenize=False,
|
161 |
-
add_generation_prompt=True,
|
162 |
-
)
|
163 |
-
print(tool_use_prompt)
|
164 |
```
|
165 |
|
166 |
-
|
167 |
-
|
168 |
-
<details>
|
169 |
-
<summary><b>Example Rendered Tool Use Prompt [CLICK TO EXPAND]</b></summary>
|
170 |
-
|
171 |
-
````
|
172 |
-
<BOS_TOKEN><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|># Safety Preamble
|
173 |
-
The instructions in this section override those in the task description and style guide sections. Don't answer questions that are harmful or immoral.
|
174 |
-
|
175 |
-
# System Preamble
|
176 |
-
## Basic Rules
|
177 |
-
You are a powerful conversational AI trained by Cohere to help people. You are augmented by a number of tools, and your job is to use and consume the output of these tools to best help the user. You will see a conversation history between yourself and a user, ending with an utterance from the user. You will then see a specific instruction instructing you what kind of response to generate. When you answer the user's requests, you cite your sources in your answers, according to those instructions.
|
178 |
-
|
179 |
-
# User Preamble
|
180 |
-
## Task and Context
|
181 |
-
You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.
|
182 |
-
|
183 |
-
## Style Guide
|
184 |
-
Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.
|
185 |
-
|
186 |
-
## Available Tools
|
187 |
-
Here is a list of tools that you have available to you:
|
188 |
|
189 |
```python
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
query (str): Query to search the internet with
|
195 |
-
"""
|
196 |
-
pass
|
197 |
```
|
198 |
|
199 |
-
|
200 |
-
def directly_answer() -> List[Dict]:
|
201 |
-
"""Calls a standard (un-augmented) AI chatbot to generate a response given the conversation history
|
202 |
-
"""
|
203 |
-
pass
|
204 |
-
```<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|>Whats the biggest penguin in the world?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>Write 'Action:' followed by a json-formatted list of actions that you want to perform in order to produce a good response to the user's last input. You can use any of the supplied tools any number of times, but you should aim to execute the minimum number of necessary actions for the input. You should use the `directly-answer` tool if calling the other tools is unnecessary. The list of actions you want to call should be formatted as a list of json objects, for example:
|
205 |
-
```json
|
206 |
-
[
|
207 |
-
{
|
208 |
-
"tool_name": title of the tool in the specification,
|
209 |
-
"parameters": a dict of parameters to input into the tool as they are defined in the specs, or {} if it takes no parameters
|
210 |
-
}
|
211 |
-
]```<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
|
212 |
-
|
213 |
-
````
|
214 |
-
|
215 |
-
</details>
|
216 |
-
|
217 |
-
<details>
|
218 |
-
<summary><b>Example Rendered Tool Use Completion [CLICK TO EXPAND]</b></summary>
|
219 |
|
220 |
-
|
221 |
-
|
222 |
-
[
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
```
|
231 |
-
````
|
232 |
-
</details>
|
233 |
-
|
234 |
-
### Grounded Generation and RAG Capabilities:
|
235 |
-
|
236 |
-
Command-R has been specifically trained with grounded generation capabilities. This means that it can generate responses based on a list of supplied document snippets, and it will include grounding spans (citations) in its response indicating the source of the information.
|
237 |
-
This can be used to enable behaviors such as grounded summarization and the final step of Retrieval Augmented Generation (RAG).This behavior has been trained into the model via a mixture of supervised fine-tuning and preference fine-tuning, using a specific prompt template.
|
238 |
-
Deviating from this prompt template may reduce performance, but we encourage experimentation.
|
239 |
-
|
240 |
-
Command-R’s grounded generation behavior takes a conversation as input (with an optional user-supplied system preamble, indicating task, context and desired output style), along with a list of retrieved document snippets.
|
241 |
-
The document snippets should be chunks, rather than long documents, typically around 100-400 words per chunk. Document snippets consist of key-value pairs. The keys should be short descriptive strings, the values can be text or semi-structured.
|
242 |
-
|
243 |
-
By default, Command-R will generate grounded responses by first predicting which documents are relevant, then predicting which ones it will cite, then generating an answer.
|
244 |
-
Finally, it will then insert grounding spans into the answer. See below for an example. This is referred to as `accurate` grounded generation.
|
245 |
-
|
246 |
-
The model is trained with a number of other answering modes, which can be selected by prompt changes . A `fast` citation mode is supported in the tokenizer, which will directly generate an answer with grounding spans in it, without first writing the answer out in full. This sacrifices some grounding accuracy in favor of generating fewer tokens.
|
247 |
-
|
248 |
-
Comprehensive documentation for working with command-R's grounded generation prompt template can be found [here](https://docs.cohere.com/docs/prompting-command-r).
|
249 |
-
|
250 |
-
The code snippet below shows a minimal working example on how to render a prompt.
|
251 |
|
252 |
-
|
253 |
-
<summary> <b>Usage: Rendering Grounded Generation prompts [CLICK TO EXPAND]</b> </summary>
|
254 |
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
{"role": "user", "content": "Whats the biggest penguin in the world?"}
|
264 |
-
]
|
265 |
-
# define documents to ground on:
|
266 |
-
documents = [
|
267 |
-
{ "title": "Tall penguins", "text": "Emperor penguins are the tallest growing up to 122 cm in height." },
|
268 |
-
{ "title": "Penguin habitats", "text": "Emperor penguins only live in Antarctica."}
|
269 |
-
]
|
270 |
-
|
271 |
-
# render the tool use prompt as a string:
|
272 |
-
grounded_generation_prompt = tokenizer.apply_grounded_generation_template(
|
273 |
-
conversation,
|
274 |
-
documents=documents,
|
275 |
-
citation_mode="accurate", # or "fast"
|
276 |
-
tokenize=False,
|
277 |
-
add_generation_prompt=True,
|
278 |
-
)
|
279 |
-
print(grounded_generation_prompt)
|
280 |
-
````
|
281 |
-
</details>
|
282 |
-
|
283 |
-
<details>
|
284 |
-
<summary><b>Example Rendered Grounded Generation Prompt [CLICK TO EXPAND]</b></summary>
|
285 |
-
|
286 |
-
````<BOS_TOKEN><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|># Safety Preamble
|
287 |
-
The instructions in this section override those in the task description and style guide sections. Don't answer questions that are harmful or immoral.
|
288 |
-
|
289 |
-
# System Preamble
|
290 |
-
## Basic Rules
|
291 |
-
You are a powerful conversational AI trained by Cohere to help people. You are augmented by a number of tools, and your job is to use and consume the output of these tools to best help the user. You will see a conversation history between yourself and a user, ending with an utterance from the user. You will then see a specific instruction instructing you what kind of response to generate. When you answer the user's requests, you cite your sources in your answers, according to those instructions.
|
292 |
-
|
293 |
-
# User Preamble
|
294 |
-
## Task and Context
|
295 |
-
You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.
|
296 |
-
|
297 |
-
## Style Guide
|
298 |
-
Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|>Whats the biggest penguin in the world?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><results>
|
299 |
-
Document: 0
|
300 |
-
title: Tall penguins
|
301 |
-
text: Emperor penguins are the tallest growing up to 122 cm in height.
|
302 |
-
|
303 |
-
Document: 1
|
304 |
-
title: Penguin habitats
|
305 |
-
text: Emperor penguins only live in Antarctica.
|
306 |
-
</results><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>Carefully perform the following instructions, in order, starting each with a new line.
|
307 |
-
Firstly, Decide which of the retrieved documents are relevant to the user's last input by writing 'Relevant Documents:' followed by comma-separated list of document numbers. If none are relevant, you should instead write 'None'.
|
308 |
-
Secondly, Decide which of the retrieved documents contain facts that should be cited in a good answer to the user's last input by writing 'Cited Documents:' followed a comma-separated list of document numbers. If you dont want to cite any of them, you should instead write 'None'.
|
309 |
-
Thirdly, Write 'Answer:' followed by a response to the user's last input in high quality natural english. Use the retrieved documents to help you. Do not insert any citations or grounding markup.
|
310 |
-
Finally, Write 'Grounded answer:' followed by a response to the user's last input in high quality natural english. Use the symbols <co: doc> and </co: doc> to indicate when a fact comes from a document in the search result, e.g <co: 0>my fact</co: 0> for a fact from document 0.<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
|
311 |
-
````
|
312 |
-
|
313 |
-
</details>
|
314 |
-
|
315 |
-
<details>
|
316 |
-
<summary><b>Example Rendered Grounded Generation Completion [CLICK TO EXPAND]</b></summary>
|
317 |
-
|
318 |
-
````
|
319 |
-
Relevant Documents: 0,1
|
320 |
-
Cited Documents: 0,1
|
321 |
-
Answer: The Emperor Penguin is the tallest or biggest penguin in the world. It is a bird that lives only in Antarctica and grows to a height of around 122 centimetres.
|
322 |
-
Grounded answer: The <co: 0>Emperor Penguin</co: 0> is the <co: 0>tallest</co: 0> or biggest penguin in the world. It is a bird that <co: 1>lives only in Antarctica</co: 1> and <co: 0>grows to a height of around 122 centimetres.</co: 0>
|
323 |
-
````
|
324 |
-
</details>
|
325 |
-
|
326 |
-
### Code Capabilities:
|
327 |
-
Command-R has been optimized to interact with your code, by requesting code snippets, code explanations, or code rewrites. It might not perform well out-of-the-box for pure code completion. For better performance, we also recommend using a low temperature (and even greedy decoding) for code-generation related instructions.
|
328 |
-
|
329 |
-
### Model Card Contact
|
330 |
-
For errors or additional questions about details in this model card, contact [info@for.ai](mailto:info@for.ai).
|
331 |
-
|
332 |
-
### Terms of Use:
|
333 |
-
We hope that the release of this model will make community-based research efforts more accessible, by releasing the weights of a highly performant 35 billion parameter model to researchers all over the world. This model is governed by a [CC-BY-NC](https://cohere.com/c4ai-cc-by-nc-license) License with an acceptable use addendum, and also requires adhering to [C4AI's Acceptable Use Policy](https://docs.cohere.com/docs/c4ai-acceptable-use-policy).
|
334 |
-
|
335 |
-
### Try Chat:
|
336 |
-
You can try Command-R chat in the playground [here](https://dashboard.cohere.com/playground/chat).
|
|
|
1 |
---
|
2 |
library_name: transformers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
license: cc-by-nc-4.0
|
4 |
+
tags:
|
5 |
+
- creative-writing
|
6 |
+
- creative-writer
|
7 |
+
- multiplicative-lora
|
8 |
---
|
9 |
|
10 |
+
An experimental model, fine-tuned using the ["multiplicative-LoRA" method](The-"multiplicative-LoRA"-method) on [c4ai-command-r-v01](https://huggingface.co/CohereForAI/c4ai-command-r-v01).
|
11 |
|
12 |
+
Other experimental models, based off `creative-writer-v0.1-alfa-35b` that attempt to encourage more diverse/creative text generation:
|
13 |
|
14 |
+
- [creative-writer-v0.1-bravo-35b](https://huggingface.co/jukofyork/creative-writer-v0.1-bravo-35b) - Scaled the pre-softmax logits by `1.1` during training (and then reset after training).
|
15 |
+
- [creative-writer-v0.1-charlie-35b](https://huggingface.co/jukofyork/creative-writer-v0.1-charlie-35b) - Scaled the pre-softmax logits by `0.9` during training (and didn't reset after training).
|
16 |
+
- [creative-writer-v0.1-delta-35b](https://huggingface.co/jukofyork/creative-writer-v0.1-delta-35b) - Trained using [Focal Loss](https://arxiv.org/abs/1708.02002) with `gamma=2` (instead of stock [Cross Entropy Loss](https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html)).
|
17 |
|
18 |
+
---
|
|
|
|
|
19 |
|
20 |
+
# The "multiplicative-LoRA" method
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
Uses:
|
23 |
|
24 |
+
`h = (I + lora_B @ lora_A) @ tensor @ x = tensor @ x + lora_B @ lora_A @ tensor @ x`
|
25 |
|
26 |
+
instead of the normal "addative-LoRA" method of:
|
27 |
|
28 |
+
`h = (tensor + lora_B @ lora_A) @ x = tensor @ x + lora_B @ lora_A @ x`
|
|
|
|
|
|
|
29 |
|
30 |
+
I only apply this to the `down_proj` matrices, and skip the last layer's `down_proj` matrix in the same way as [creative-writing-control-vectors-v3.0](https://huggingface.co/jukofyork/creative-writing-control-vectors-v3.0).
|
|
|
|
|
31 |
|
32 |
+
This currently requires hacking [PEFT's layer.py](https://github.com/huggingface/peft/blob/main/src/peft/tuners/lora/layer.py) like so:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
```python
|
35 |
+
#self.lora_A[adapter_name] = nn.Linear(self.in_features, r, bias=False)
|
36 |
+
self.lora_A[adapter_name] = nn.Linear(self.out_features, r, bias=False)
|
37 |
+
self.lora_B[adapter_name] = nn.Linear(r, self.out_features, bias=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
```
|
39 |
|
40 |
+
and:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
```python
|
43 |
+
#x = x.to(lora_A.weight.dtype)
|
44 |
+
temp = result.to(lora_A.weight.dtype)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
if not self.use_dora[active_adapter]:
|
47 |
+
#result = result + lora_B(lora_A(dropout(x))) * scaling
|
48 |
+
result = result + lora_B(lora_A(dropout(temp))) * scaling
|
|
|
|
|
|
|
|
|
|
|
49 |
```
|
50 |
|
51 |
+
Then to merge you need to hack [qlora-pipe's merge_lora.py](https://github.com/tdrussell/qlora-pipe/blob/main/merge_lora.py) to use:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
```python
|
54 |
+
old_type = tensor.dtype
|
55 |
+
tensor = tensor.to(torch.float32)
|
56 |
+
tensor += scale * lora_B.to(torch.float32) @ lora_A.to(torch.float32) @ tensor
|
57 |
+
tensor = tensor.to(old_type)
|
|
|
|
|
|
|
58 |
```
|
59 |
|
60 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
# Training
|
63 |
+
|
64 |
+
- Took just under 4 days using dual-A6000 GPUs connected via NVLink, using [qlora-pipe](https://github.com/tdrussell/qlora-pipe).
|
65 |
+
- The dataset consisted of approximately 1000 pre-2012 books converted to Markdown (~180M tokens) using the same `dataset_combination_mode = 'concatenate'` as [Llama-3-70B-Instruct-Storywriter](https://huggingface.co/tdrussell/Llama-3-70B-Instruct-Storywriter).
|
66 |
+
|
67 |
+
## `config_creative_writer.toml`
|
68 |
+
|
69 |
+
```toml
|
70 |
+
# Paths
|
71 |
+
model = '/mnt/data/c4ai-command-r-v01'
|
72 |
+
output_dir = '/mnt/data/creative-writer-v0.1-alfa-35b'
|
73 |
+
|
74 |
+
# Lora configuration
|
75 |
+
lora_rank = 64
|
76 |
+
lora_alpha = 64
|
77 |
+
lora_dropout = 0.0
|
78 |
+
target_modules = ['down_proj']
|
79 |
+
layers_to_transform = '0:38' # skip last layer
|
80 |
+
|
81 |
+
# Optimization configuration
|
82 |
+
epochs = 1
|
83 |
+
lr_scheduler = 'constant'
|
84 |
+
warmup_steps = 100
|
85 |
+
batch_size_tokens = 8192
|
86 |
+
|
87 |
+
# Performance settings
|
88 |
+
pipeline_stages = 2
|
89 |
+
logging_steps = 1
|
90 |
+
eval_steps = 100
|
91 |
+
save_steps = 100
|
92 |
+
checkpoint_every_n_minutes = 60
|
93 |
+
eval_before_first_step = true
|
94 |
+
model_weight_dtype = 'bfloat16'
|
95 |
+
lora_weight_dtype = 'bfloat16'
|
96 |
+
keep_states = 3
|
97 |
+
group_by_length = true
|
98 |
+
activation_checkpointing = 'unsloth'
|
99 |
+
|
100 |
+
# Resume a prior run
|
101 |
+
resume_from_checkpoint = false
|
102 |
+
|
103 |
+
# Dataset configuration
|
104 |
+
dataset_combination_mode = 'concatenate'
|
105 |
+
eval_gradient_accumulation_steps = 1
|
106 |
+
|
107 |
+
[optimizer]
|
108 |
+
type = 'adamw_kahan'
|
109 |
+
lr = 5e-6
|
110 |
+
beta1 = 0.9
|
111 |
+
beta2 = 0.99
|
112 |
+
weight_decay = 0.01
|
113 |
+
|
114 |
+
[[datasets]]
|
115 |
+
name = 'books'
|
116 |
+
dataset_type = 'textfile'
|
117 |
+
dataset_path = '/mnt/data/datasets/ebooks/*.txt'
|
118 |
+
sequence_len = 8192
|
119 |
+
eval_size = 0.01
|
120 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
+
## `ds_creative_writer.json`
|
|
|
123 |
|
124 |
+
```json
|
125 |
+
{
|
126 |
+
"train_micro_batch_size_per_gpu": 1,
|
127 |
+
"gradient_accumulation_steps": 16,
|
128 |
+
"gradient_clipping": 1.0,
|
129 |
+
"steps_per_print": 1
|
130 |
+
}
|
131 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|