LeroyDyer commited on
Commit
8693ea2
1 Parent(s): a009331

Upload 2 files

Browse files
SpydazWebAI_Examples.py ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+ ################################ language examples ##############################
11
+
12
+ ## LlamaTokenizerFast
13
+ tokenizer = MistralTokenizerFast.from_pretrained("mistralai/Mistral-7B-v0.1")
14
+ tokenizer.encode("Hello this is a test")
15
+
16
+ ################################ INITIALIZE Mistral MODEL ##############################
17
+ ## INITIALIZE MistralStarConfig
18
+
19
+ # Initializing a Mistral 7B style configuration
20
+ configuration = MistralStarConfig()
21
+
22
+ # Initializing a model from the Mistral 7B style configuration
23
+ model = MistralModel(configuration)
24
+
25
+ # Accessing the model configuration
26
+ configuration = model.config
27
+
28
+ ## INITIALIZE MistralStarConfig
29
+
30
+ # Initializing a Mistral 7B style configuration
31
+ configuration = MistralQuietConfig()
32
+
33
+ # Initializing a model from the Mistral 7B style configuration
34
+ model = MistralModel(configuration)
35
+
36
+ # Accessing the model configuration
37
+ configuration = model.config
38
+
39
+
40
+ ## INITIALIZE MODEL
41
+
42
+ # Initializing a Mistral 7B style configuration
43
+ configuration = MistralConfig()
44
+
45
+ # Initializing a model from the Mistral 7B style configuration
46
+ model = MistralModel(configuration)
47
+
48
+ # Accessing the model configuration
49
+ configuration = model.config
50
+
51
+
52
+ ## INITIALIZE MODEL-Examples
53
+
54
+ # Download model and configuration from huggingface.co and cache.
55
+ model = MistralModel.from_pretrained("mistralai/Mistral-7B-v0.1")
56
+ # Model was saved using *save_pretrained('./test/saved_model/')* (for example purposes, not runnable).
57
+ model = MistralModel.from_pretrained("./test/saved_model/")
58
+ # Update configuration during loading.
59
+ model = MistralModel.from_pretrained("mistralai/Mistral-7B-v0.1", output_attentions=True)
60
+ assert model.config.output_attentions == True
61
+ # Loading from a TF checkpoint file instead of a PyTorch model (slower, for example purposes, not runnable).
62
+ config = MistralConfig.from_json_file("./tf_model/my_tf_model_config.json")
63
+ model = MistralModel.from_pretrained("./tf_model/my_tf_checkpoint.ckpt.index", from_tf=True, config=config)
64
+ # Loading from a Flax checkpoint file instead of a PyTorch model (slower)
65
+ model = MistralModel.from_pretrained("mistralai/Mistral-7B-v0.1", from_flax=True)
66
+ ################################ MistralForCausalLM ##############################
67
+
68
+ ## MistralForCausalLM
69
+
70
+ model = MistralForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1")
71
+ tokenizer = MistralTokenizerFast.from_pretrained("mistralai/Mistral-7B-v0.1")
72
+
73
+ prompt = "Hey, are you conscious? Can you talk to me?"
74
+ inputs = tokenizer(prompt, return_tensors="pt")
75
+
76
+ # Generate
77
+ generate_ids = model.generate(inputs.input_ids, max_length=30)
78
+ tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
79
+ ## "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
80
+
81
+ ################################ MistralForSequenceClassification ##############################
82
+
83
+ ### MistralForSequenceClassification - single-label classification:
84
+
85
+ tokenizer = MistralTokenizerFast.from_pretrained("mistralai/Mistral-7B-v0.1")
86
+ model = MistralForSequenceClassification.from_pretrained("mistralai/Mistral-7B-v0.1")
87
+
88
+ inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
89
+
90
+ with torch.no_grad():
91
+ logits = model(**inputs).logits
92
+
93
+ predicted_class_id = logits.argmax().item()
94
+
95
+ # To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
96
+ num_labels = len(model.config.id2label)
97
+ model = MistralForSequenceClassification.from_pretrained("mistralai/Mistral-7B-v0.1", num_labels=num_labels)
98
+
99
+ labels = torch.tensor([1])
100
+ loss = model(**inputs, labels=labels).loss
101
+
102
+
103
+ ### MistralForSequenceClassification - multi-label classification:
104
+
105
+
106
+ tokenizer = MistralTokenizerFast.from_pretrained("mistralai/Mistral-7B-v0.1")
107
+ model = MistralForSequenceClassification.from_pretrained("mistralai/Mistral-7B-v0.1", problem_type="multi_label_classification")
108
+
109
+ inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
110
+
111
+ with torch.no_grad():
112
+ logits = model(**inputs).logits
113
+
114
+ predicted_class_ids = torch.arange(0, logits.shape[-1])[torch.sigmoid(logits).squeeze(dim=0) > 0.5]
115
+
116
+ # To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
117
+ num_labels = len(model.config.id2label)
118
+ model = MistralForSequenceClassification.from_pretrained(
119
+ "mistralai/Mistral-7B-v0.1", num_labels=num_labels, problem_type="multi_label_classification"
120
+ )
121
+
122
+ labels = torch.sum(
123
+ torch.nn.functional.one_hot(predicted_class_ids[None, :].clone(), num_classes=num_labels), dim=1
124
+ ).to(torch.float)
125
+ loss = model(**inputs, labels=labels).loss
126
+
127
+ ################################ MistralForTokenClassification ##############################
128
+
129
+
130
+ ### MistralForTokenClassification
131
+
132
+ tokenizer = MistralTokenizerFast.from_pretrained("mistralai/Mistral-7B-v0.1")
133
+ model = MistralForTokenClassification.from_pretrained("mistralai/Mistral-7B-v0.1")
134
+
135
+ inputs = tokenizer(
136
+ "HuggingFace is a company based in Paris and New York", add_special_tokens=False, return_tensors="pt"
137
+ )
138
+
139
+ with torch.no_grad():
140
+ logits = model(**inputs).logits
141
+
142
+ predicted_token_class_ids = logits.argmax(-1)
143
+
144
+ # Note that tokens are classified rather then input words which means that
145
+ # there might be more predicted token classes than words.
146
+ # Multiple token classes might account for the same word
147
+ predicted_tokens_classes = [model.config.id2label[t.item()] for t in predicted_token_class_ids[0]]
148
+ predicted_tokens_classes
149
+
150
+ labels = predicted_token_class_ids
151
+ loss = model(**inputs, labels=labels).loss
152
+ round(loss.item(), 2)
153
+
154
+
155
+
156
+
157
+ ################################ MistralForQuestionAnswering ##############################
158
+
159
+
160
+ tokenizer = MistralTokenizerFast.from_pretrained("mistralai/Mistral-7B-v0.1")
161
+ model = MistralForQuestionAnswering.from_pretrained("mistralai/Mistral-7B-v0.1")
162
+
163
+ question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
164
+
165
+ inputs = tokenizer(question, text, return_tensors="pt")
166
+ with torch.no_grad():
167
+ outputs = model(**inputs)
168
+
169
+ answer_start_index = outputs.start_logits.argmax()
170
+ answer_end_index = outputs.end_logits.argmax()
171
+
172
+ predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
173
+
174
+ # target is "nice puppet"
175
+ target_start_index = torch.tensor([14])
176
+ target_end_index = torch.tensor([15])
177
+
178
+ outputs = model(**inputs, start_positions=target_start_index, end_positions=target_end_index)
179
+ loss = outputs.loss
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+ ################################ Mixtral MOA Models ##############################
188
+
189
+ ################################################################
190
+ # Initializing a Mixtral 7B style configuration
191
+ ################################################################
192
+
193
+ configuration = MixtralConfig()
194
+
195
+ # Initializing a model from the Mixtral 7B style configuration
196
+ model = MixtralModel(configuration)
197
+
198
+ # Accessing the model configuration
199
+ configuration = model.config
200
+
201
+ ################################################################
202
+ ### The base model can be used as follows:
203
+ ################################################################
204
+
205
+ model = MixtralForCausalLM.from_pretrained("mistralai/Mixtral-8x7B-v0.1", device_map="auto")
206
+ tokenizer = MistralTokenizerFast.from_pretrained("mistralai/Mixtral-8x7B-v0.1")
207
+
208
+ prompt = "My favourite condiment is"
209
+
210
+ model_inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
211
+ model.to("cpu")
212
+
213
+ generated_ids = model.generate(**model_inputs, max_new_tokens=100, do_sample=True)
214
+ tokenizer.batch_decode(generated_ids)[0]
215
+
216
+
217
+ ################################################################
218
+ ### The instruction tuned model can be used as follows:
219
+ ################################################################
220
+
221
+ model = MixtralForCausalLM.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1", device_map="auto")
222
+ tokenizer = MistralTokenizerFast.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1")
223
+
224
+ messages = [
225
+ {"role": "user", "content": "What is your favourite condiment?"},
226
+ {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
227
+ {"role": "user", "content": "Do you have mayonnaise recipes?"}
228
+ ]
229
+
230
+ model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
231
+
232
+ generated_ids = model.generate(model_inputs, max_new_tokens=100, do_sample=True)
233
+ tokenizer.batch_decode(generated_ids)[0]
234
+ ################################ end of language examples ##############################
235
+
SpydazWebAI_Mistral_Transformer.py ADDED
The diff for this file is too large to render. See raw diff