RichardErkhov commited on
Commit
0c42773
1 Parent(s): 5a4271e

uploaded readme

Browse files
Files changed (1) hide show
  1. README.md +344 -0
README.md ADDED
@@ -0,0 +1,344 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Quantization made by Richard Erkhov.
2
+
3
+ [Github](https://github.com/RichardErkhov)
4
+
5
+ [Discord](https://discord.gg/pvy7H8DZMG)
6
+
7
+ [Request more models](https://github.com/RichardErkhov/quant_request)
8
+
9
+
10
+ neural-chat-7b-v3-1 - bnb 8bits
11
+ - Model creator: https://huggingface.co/Intel/
12
+ - Original model: https://huggingface.co/Intel/neural-chat-7b-v3-1/
13
+
14
+
15
+
16
+
17
+ Original model description:
18
+ ---
19
+ license: apache-2.0
20
+ tags:
21
+ - LLMs
22
+ - mistral
23
+ - Intel
24
+ pipeline_tag: text-generation
25
+ base_model: mistralai/Mistral-7B-v0.1
26
+ model-index:
27
+ - name: neural-chat-7b-v3-1
28
+ results:
29
+ - task:
30
+ type: Large Language Model
31
+ name: Large Language Model
32
+ dataset:
33
+ type: Open-Orca/SlimOrca
34
+ name: Open-Orca/SlimOrca
35
+ metrics:
36
+ - type: ARC (25-shot)
37
+ value: 66.21
38
+ name: ARC (25-shot)
39
+ verified: true
40
+ - type: HellaSwag (10-shot)
41
+ value: 83.64
42
+ name: HellaSwag (10-shot)
43
+ verified: true
44
+ - type: MMLU (5-shot)
45
+ value: 62.37
46
+ name: MMLU (5-shot)
47
+ verified: true
48
+ - type: TruthfulQA (0-shot)
49
+ value: 59.65
50
+ name: TruthfulQA (0-shot)
51
+ verified: true
52
+ - type: Winogrande (5-shot)
53
+ value: 78.14
54
+ name: Winogrande (5-shot)
55
+ verified: true
56
+ - type: GSM8K (5-shot)
57
+ value: 19.56
58
+ name: GSM8K (5-shot)
59
+ verified: true
60
+ - type: DROP (3-shot)
61
+ value: 43.84
62
+ name: DROP (3-shot)
63
+ verified: true
64
+ datasets:
65
+ - Open-Orca/SlimOrca
66
+ language:
67
+ - en
68
+ ---
69
+
70
+ ## Model Details: Neural-Chat-v3-1
71
+
72
+ This model is a fine-tuned 7B parameter LLM on the Intel Gaudi 2 processor from the [mistralai/Mistral-7B-v0.1](https://huggingface.co/mistralai/Mistral-7B-v0.1) on the open source dataset [Open-Orca/SlimOrca](https://huggingface.co/datasets/Open-Orca/SlimOrca). The model was aligned using the Direct Performance Optimization (DPO) method with [Intel/orca_dpo_pairs](https://huggingface.co/datasets/Intel/orca_dpo_pairs). For more information, refer to the Medium article [The Practice of Supervised Fine-tuning and Direct Preference Optimization on Intel Gaudi2](https://medium.com/@NeuralCompressor/the-practice-of-supervised-finetuning-and-direct-preference-optimization-on-habana-gaudi2-a1197d8a3cd3).
73
+
74
+ <p align="center">
75
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/6297f0e30bd2f58c647abb1d/ctASHUT5QYIxMsOFa-sHC.webp" width="500"/>
76
+ Photo by Google DeepMind on Unsplash
77
+ </p>
78
+
79
+ | Model Detail | Description |
80
+ | ----------- | ----------- |
81
+ | Model Authors - Company | Intel. The NeuralChat team with members from DCAI/AISE/AIPT. Core team members: Kaokao Lv, Liang Lv, Chang Wang, Wenxin Zhang, Xuhui Ren, and Haihao Shen.|
82
+ | Date | October, 2023 |
83
+ | Version | v3-1 |
84
+ | Type | 7B Large Language Model |
85
+ | Paper or Other Resources | [Medium Blog](https://medium.com/@NeuralCompressor/the-practice-of-supervised-finetuning-and-direct-preference-optimization-on-habana-gaudi2-a1197d8a3cd3) |
86
+ | License | Apache 2.0 |
87
+ | Questions or Comments | [Community Tab](https://huggingface.co/Intel/neural-chat-7b-v3-1/discussions) and [Intel DevHub Discord](https://discord.gg/rv2Gp55UJQ)|
88
+
89
+ | Intended Use | Description |
90
+ | ----------- | ----------- |
91
+ | Primary intended uses | You can use the fine-tuned model for several language-related tasks. Checkout the [LLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard) to see how this model is doing. |
92
+ | Primary intended users | Anyone doing inference on language-related tasks. |
93
+ | Out-of-scope uses | This model in most cases will need to be fine-tuned for your particular task. The model should not be used to intentionally create hostile or alienating environments for people.|
94
+
95
+ ## How To Use
96
+
97
+ Context length for this model: 8192 tokens (same as https://huggingface.co/mistralai/Mistral-7B-v0.1)
98
+
99
+ ### Training hyperparameters
100
+
101
+ The following hyperparameters were used during training:
102
+ - learning_rate: 1e-04
103
+ - train_batch_size: 1
104
+ - eval_batch_size: 2
105
+ - seed: 42
106
+ - distributed_type: multi-HPU
107
+ - num_devices: 8
108
+ - gradient_accumulation_steps: 8
109
+ - total_train_batch_size: 64
110
+ - total_eval_batch_size: 8
111
+ - optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
112
+ - lr_scheduler_type: cosine
113
+ - lr_scheduler_warmup_ratio: 0.03
114
+ - num_epochs: 2.0
115
+
116
+ ### Reproduce the model
117
+ Here is the sample code to reproduce the model: [GitHub sample code](https://github.com/intel/intel-extension-for-transformers/blob/main/intel_extension_for_transformers/neural_chat/examples/finetuning/finetune_neuralchat_v3). Here is the documentation to reproduce building the model:
118
+
119
+ ```bash
120
+ git clone https://github.com/intel/intel-extension-for-transformers.git
121
+ cd intel-extension-for-transformers
122
+
123
+ docker build --no-cache ./ --target hpu --build-arg REPO=https://github.com/intel/intel-extension-for-transformers.git --build-arg ITREX_VER=main -f ./intel_extension_for_transformers/neural_chat/docker/Dockerfile -t chatbot_finetuning:latest
124
+
125
+ docker run -it --runtime=habana -e HABANA_VISIBLE_DEVICES=all -e OMPI_MCA_btl_vader_single_copy_mechanism=none --cap-add=sys_nice --net=host --ipc=host chatbot_finetuning:latest
126
+
127
+ # after entering docker container
128
+ cd examples/finetuning/finetune_neuralchat_v3
129
+
130
+ ```
131
+ We select the latest pretrained mistralai/Mistral-7B-v0.1 and the open source dataset Open-Orca/SlimOrca to conduct the experiment.
132
+
133
+ The below script use deepspeed zero2 to lanuch the training with 8 cards Gaudi2. In the `finetune_neuralchat_v3.py`, the default `use_habana=True, use_lazy_mode=True, device="hpu"` for Gaudi2. And if you want to run it on NVIDIA GPU, you can set them `use_habana=False, use_lazy_mode=False, device="auto"`.
134
+
135
+ ```python
136
+ deepspeed --include localhost:0,1,2,3,4,5,6,7 \
137
+ --master_port 29501 \
138
+ finetune_neuralchat_v3.py
139
+ ```
140
+
141
+ Merge the LoRA weights:
142
+
143
+ ```python
144
+ python apply_lora.py \
145
+ --base-model-path mistralai/Mistral-7B-v0.1 \
146
+ --lora-model-path finetuned_model/ \
147
+ --output-path finetuned_model_lora
148
+ ```
149
+
150
+
151
+ ### FP32 Inference with Transformers
152
+
153
+ ```python
154
+ import transformers
155
+
156
+ model_name = 'Intel/neural-chat-7b-v3-1'
157
+ model = transformers.AutoModelForCausalLM.from_pretrained(model_name)
158
+ tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
159
+
160
+ def generate_response(system_input, user_input):
161
+
162
+ # Format the input using the provided template
163
+ prompt = f"### System:\n{system_input}\n### User:\n{user_input}\n### Assistant:\n"
164
+
165
+ # Tokenize and encode the prompt
166
+ inputs = tokenizer.encode(prompt, return_tensors="pt", add_special_tokens=False)
167
+
168
+ # Generate a response
169
+ outputs = model.generate(inputs, max_length=1000, num_return_sequences=1)
170
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
171
+
172
+ # Extract only the assistant's response
173
+ return response.split("### Assistant:\n")[-1]
174
+
175
+
176
+ # Example usage
177
+ system_input = "You are a math expert assistant. Your mission is to help users understand and solve various math problems. You should provide step-by-step solutions, explain reasonings and give the correct answer."
178
+ user_input = "calculate 100 + 520 + 60"
179
+ response = generate_response(system_input, user_input)
180
+ print(response)
181
+
182
+ # expected response
183
+ """
184
+ To calculate the sum of 100, 520, and 60, we will follow these steps:
185
+
186
+ 1. Add the first two numbers: 100 + 520
187
+ 2. Add the result from step 1 to the third number: (100 + 520) + 60
188
+
189
+ Step 1: Add 100 and 520
190
+ 100 + 520 = 620
191
+
192
+ Step 2: Add the result from step 1 to the third number (60)
193
+ (620) + 60 = 680
194
+
195
+ So, the sum of 100, 520, and 60 is 680.
196
+ """
197
+ ```
198
+
199
+ ### BF16 Inference with Intel Extension for Transformers and Intel Extension for Pytorch
200
+ ```python
201
+ from transformers import AutoTokenizer, TextStreamer
202
+ import torch
203
+ from intel_extension_for_transformers.transformers import AutoModelForCausalLM
204
+ import intel_extension_for_pytorch as ipex
205
+
206
+ model_name = "Intel/neural-chat-7b-v3-1"
207
+ prompt = "Once upon a time, there existed a little girl,"
208
+
209
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
210
+ inputs = tokenizer(prompt, return_tensors="pt").input_ids
211
+ streamer = TextStreamer(tokenizer)
212
+
213
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)
214
+ model = ipex.optimize(model.eval(), dtype=torch.bfloat16, inplace=True, level="O1", auto_kernel_selection=True)
215
+
216
+ outputs = model.generate(inputs, streamer=streamer, max_new_tokens=300)
217
+ ```
218
+
219
+
220
+ ### INT4 Inference with Transformers and Intel Extension for Transformers
221
+ ```python
222
+ from transformers import AutoTokenizer, TextStreamer
223
+ from intel_extension_for_transformers.transformers import AutoModelForCausalLM, WeightOnlyQuantConfig
224
+ model_name = "Intel/neural-chat-7b-v3-1"
225
+
226
+ # for int8, should set weight_dtype="int8"
227
+ config = WeightOnlyQuantConfig(compute_dtype="bf16", weight_dtype="int4")
228
+ prompt = "Once upon a time, there existed a little girl,"
229
+
230
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
231
+ inputs = tokenizer(prompt, return_tensors="pt").input_ids
232
+ streamer = TextStreamer(tokenizer)
233
+
234
+ model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=config)
235
+ outputs = model.generate(inputs, streamer=streamer, max_new_tokens=300)
236
+
237
+ ```
238
+
239
+ | Factors | Description |
240
+ | ----------- | ----------- |
241
+ | Groups | More details about the dataset and annotations can be found at [Open-Orca/SlimOrca](https://huggingface.co/datasets/Open-Orca/SlimOrca) and the associated paper at https://arxiv.org/abs/2306.02707. |
242
+ | Instrumentation | The performance of the model can vary depending on the inputs to the model. In this case, the prompts provided can drastically change the prediction of the language model. |
243
+ | Environment | The model was trained on the Intel Gaudi 2 processor (8 cards). |
244
+ | Card Prompts | Model deployment on alternate hardware and software will change model performance. The model evaluation factors are from the Hugging Face LLM leaderboard: ARC, HellaSwag, MMLU, TruthfulQA, Winogrande, GSM8K, and DROP (see Quantitative Analyses below). |
245
+
246
+ | Metrics | Description |
247
+ | ----------- | ----------- |
248
+ | Model performance measures | The model performance was evaluated against other LLMs according to the measures on the LLM leaderboard. These were selected as this has become the standard for LLM performance. |
249
+ | Decision thresholds | No decision thresholds were used. |
250
+ | Approaches to uncertainty and variability | - |
251
+
252
+ | Training and Evaluation Data | Description |
253
+ | ----------- | ----------- |
254
+ | Datasets | The training data are from [Open-Orca/SlimOrca](https://huggingface.co/datasets/Open-Orca/SlimOrca). There is no contamination from the GSM8k test set, as this is not a part of the Open-Orca/SlimOrca dataset.|
255
+ | Motivation | - |
256
+ | Preprocessing | - |
257
+
258
+ ## Quantitative Analyses
259
+ The model was submitted to the [LLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard). The detailed submission can be found here: [https://huggingface.co/datasets/open-llm-leaderboard/details_Intel__neural-chat-7b-v3-1](https://huggingface.co/datasets/open-llm-leaderboard/details_Intel__neural-chat-7b-v3-1). The metrics can be found below and show that the model has significantly improved performance from Mistral-7B-v0.1 and neural-chat-7b-v3.
260
+
261
+ | Model | Average ⬆️| ARC (25-s) ⬆️ | HellaSwag (10-s) ⬆️ | MMLU (5-s) ⬆️| TruthfulQA (MC) (0-s) ⬆️ | Winogrande (5-s) | GSM8K (5-s) | DROP (3-s) |
262
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- |
263
+ |[mistralai/Mistral-7B-v0.1](https://huggingface.co/mistralai/Mistral-7B-v0.1) | 50.32 | 59.58 | 83.31 | 64.16 | 42.15 | 78.37 | 18.12 | 6.14 |
264
+ | [Intel/neural-chat-7b-v3](https://huggingface.co/Intel/neural-chat-7b-v3) | **57.31** | 67.15 | 83.29 | 62.26 | 58.77 | 78.06 | 1.21 | 50.43 |
265
+ | [Intel/neural-chat-7b-v3-1](https://huggingface.co/Intel/neural-chat-7b-v3-1) | **59.06** | 66.21 | 83.64 | 62.37 | 59.65 | 78.14 | 19.56 | 43.84 |
266
+
267
+ ## Testing Model Quantizability
268
+ The following code block can be run to determine, for PyTorch models, if that model is amenable to quantization.
269
+ One caveat - the Intel Extension for PyTorch uses optimum ipex, which is pre-release and needs further testing.
270
+
271
+ To install the dependencies, you should first install Intel Extensions for PyTorch and tehn pip install each of the following dependencies:
272
+ - torch
273
+ - optimum.intel
274
+ - optimum[ipex]
275
+ - transformers
276
+
277
+ ### Intel Extension for PyTorch method:
278
+ In this case, we are testing if neural-chat-7b-v3-1 can be quantized and this testing method demonstrates the model size change, for example:
279
+ when the base type is specified to be torch.bfloat16 but also specifying that load_in_4bit=True which causes the weights only to be quantized we see an output from the model testing as follows:
280
+ - **model_quantize_internal: model size = 27625.02 MB**
281
+ - **model_quantize_internal: quant size = 4330.80 MB**
282
+
283
+ This code should run from within a python script - such as ipex_test.py as follows:
284
+ ```python
285
+ import torch
286
+ import os
287
+ from transformers import AutoTokenizer
288
+ from intel_extension_for_transformers.transformers import AutoModelForCausalLM, pipeline
289
+ model_name = "Intel/neural-chat-7b-v3-1"
290
+ prompt = "Once upon a time, there existed a little girl,"
291
+
292
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
293
+ inputs = tokenizer(prompt, return_tensors="pt").input_ids
294
+
295
+ result = {torch.bfloat16:"failed"}
296
+ typ = torch.bfloat16
297
+ try:
298
+ model = AutoModelForCausalLM.from_pretrained(model_name, load_in_4bit=True, torch_dtype = typ)
299
+ outputs = model.generate(inputs, max_new_tokens=20)
300
+ result[typ] = f"passed, {os.stat(model.bin_file).st_size}"
301
+ except:
302
+ result[typ] = "failed"
303
+
304
+
305
+ print("\n\nResults of quantizing: ")
306
+ # determine if Quantized
307
+ with open(r"output.log", 'r') as fp:
308
+ for l_no, line in enumerate(fp):
309
+ # search string
310
+ if 'model_quantize_internal' in line:
311
+ print(line)
312
+
313
+ print("\n\nExecution results ")
314
+ for k,v in result.items():
315
+ print(k,v)
316
+
317
+ print("\n\nModel Output: ")
318
+ tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
319
+ ```
320
+ Run the code as folows from a bash terminal:
321
+ ```bash
322
+ python ipex_test.py 2>&1 | tee output.log
323
+ ```
324
+ The entire output is captured in the output.log but it will be summarized,
325
+ along with output from the model indicating either pass or fail of the quantization as well as model output for a given prompt.
326
+
327
+
328
+ ## Ethical Considerations and Limitations
329
+ Neural-chat-7b-v3-1 can produce factually incorrect output, and should not be relied on to produce factually accurate information. Because of the limitations of the pretrained model and the finetuning datasets, it is possible that this model could generate lewd, biased or otherwise offensive outputs.
330
+
331
+ Therefore, before deploying any applications of neural-chat-7b-v3-1, developers should perform safety testing.
332
+
333
+ ## Caveats and Recommendations
334
+
335
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model.
336
+
337
+ Here are a couple of useful links to learn more about Intel's AI software:
338
+ * Intel Neural Compressor [link](https://github.com/intel/neural-compressor)
339
+ * Intel Extension for Transformers [link](https://github.com/intel/intel-extension-for-transformers)
340
+
341
+ ## Disclaimer
342
+
343
+ The license on this model does not constitute legal advice. We are not responsible for the actions of third parties who use this model. Please cosult an attorney before using this model for commercial purposes.
344
+