jburtoft commited on
Commit
dd0f01e
1 Parent(s): 815fa61

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +144 -0
README.md CHANGED
@@ -1,3 +1,147 @@
1
  ---
2
  license: apache-2.0
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
  ---
4
+ # Neuronx model for Mistral
5
+
6
+ This repository contains [AWS Inferentia2](https://aws.amazon.com/ec2/instance-types/inf2/) and [`neuronx`](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/) compatible checkpoints for[mistralai/Mistral-7B-Instruct-v0.1](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1).
7
+
8
+ However, this file includes an example of how to compile various versions of Mistral. Support isn’t available yet (as of 1/3/2024) in the optimum-neuron framework, so we use the base transformers library.
9
+
10
+ These instructions closely follow the [Developer Guide](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/libraries/transformers-neuronx/transformers-neuronx-developer-guide.html#grouped-query-attention-gqa-support-beta). Look there for more detailed explanations, especially for the GQA settings.
11
+
12
+ This model has been compiled to run on an inf2.xlarge (the smallest Inferentia2 instance). You can run it on a bigger instance, but it will only use two cores no matter how many are available, unless you change the core number available in compilation. Remember that each Neuron processor has two cores.
13
+
14
+
15
+ ## Set up the environment
16
+
17
+ First, use the [DLAMI image from Hugging Face](https://aws.amazon.com/marketplace/pp/prodview-gr3e6yiscria2). It has most of the utilities and drivers preinstalled. However, you will need to update transformers-neruonx from the source to get Mistral support.
18
+
19
+
20
+ ```
21
+ python -m pip install git+https://github.com/aws-neuron/transformers-neuronx.git
22
+ ```
23
+
24
+ ## Running inference from this repository
25
+
26
+ If you want to run a quick test or if the exact model you want to use is [mistralai/Mistral-7B-Instruct-v0.1](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1), you can run it directly using the steps below. Otherwise, jump to the Compilation of other Mistral versions section.
27
+
28
+ First, you will need a local copy of the library. This is because one of the nice things that the Hugging Face optimum library does is abstract local loads from repository loads. However, Mistral inference isn't supported yet.
29
+
30
+ From python:
31
+
32
+ ```
33
+ # using python instead of git clone because I know this supports lfs on the DLAMI image
34
+ from huggingface_hub import Repository
35
+ repo = Repository(local_dir="Mistral-neuron", clone_from="jburtoft/Mistral-neuron")
36
+
37
+ ```
38
+
39
+ This should put a local copy in Mistral-neuron. This process should take a 5-10 minutes. If it completes in a few seconds the first time you run it, you are having problems with git-lfs. You can see this by using ls -al to check the size of the files downloaded. You will also notice it later when you get parsing errors.
40
+
41
+ Next, load the model and neff files from disk into the Neuron processors:
42
+
43
+ ```
44
+ import torch
45
+ from transformers_neuronx import constants
46
+ from transformers_neuronx.mistral.model import MistralForSampling
47
+ from transformers_neuronx.module import save_pretrained_split
48
+ from transformers_neuronx.config import NeuronConfig
49
+ from transformers import AutoModelForCausalLM, AutoTokenizer
50
+
51
+ # Set sharding strategy for GQA to be shard over heads
52
+ neuron_config = NeuronConfig(
53
+ grouped_query_attention=constants.GQA.SHARD_OVER_HEADS
54
+ )
55
+ # define the model. These are the settings used in compilation.
56
+ # If you want to change these settings, skip to "Compilation of other Mistral versions"
57
+ model_neuron = MistralForSampling.from_pretrained("Mistral-neuron", batch_size=1, tp_degree=2, n_positions=256, amp='bf16', neuron_config=neuron_config)
58
+
59
+ # load the neff files from the local directory instead of compiling
60
+ model_neuron.load("Mistral-neuron")
61
+
62
+ # load the neff files into the neuron processors.
63
+ # you can see this process happening if you run neuron-top from the command line in another console.
64
+ # if you didn't do the previous load command, this will also compile the neff files
65
+ model_neuron.to_neuron()
66
+
67
+
68
+ ```
69
+
70
+ ## Inference example
71
+
72
+ This points to the original model for the tokenizer because the tokenizer is the same.
73
+ If you are compiling your own and want to have a single reference for everything, you can copy the special_tokens_map.json and tokenizer* from the original model to your local copy.
74
+
75
+ ```
76
+ # Get a tokenizer and example input. Note that this points to the original model
77
+ tokenizer = AutoTokenizer.from_pretrained('mistralai/Mistral-7B-Instruct-v0.1')
78
+ text = "[INST] What is your favourite condiment? [/INST]"
79
+ encoded_input = tokenizer(text, return_tensors='pt')
80
+
81
+ # Run inference
82
+ with torch.inference_mode():
83
+ generated_sequence = model_neuron.sample(encoded_input.input_ids, sequence_length=256, start_ids=None)
84
+ print([tokenizer.decode(tok) for tok in generated_sequence])
85
+
86
+ ```
87
+
88
+
89
+ Example output:
90
+ (most of the time with amp=‘bf16’, the answer is ketchup. However, if I compiled with amp=f32, the answer was soy sauce. This was for a sample size of one, so let me know what you see —@jburtoft)
91
+
92
+ ```
93
+ 2024-Jan-03 15:59:21.0510 1486:2057 [0] nccl_net_ofi_init:1415 CCOM WARN NET/OFI aws-ofi-nccl initialization failed
94
+ 2024-Jan-03 15:59:21.0510 1486:2057 [0] init.cc:138 CCOM WARN OFI plugin initNet() failed is EFA enabled?
95
+ ['<s> [INST] What is your favourite condiment? [/INST] My favorite condiment is probably ketchup. It adds a perfect balance of sweet, tangy, and slightly spicy flavor to dishes, and is versatile enough to go with a wide variety of foods.</s>']
96
+
97
+ ```
98
+
99
+ ## Compilation of other Mistral versions
100
+
101
+ If you want to use a different version of Mistral from Hugging Face, use the slightly modified code below. It essentially removes the “load” command. When the “to_neuron()” command sees that the model object doesn’t include the neff files, it will kick off the recompile. You can save them at the end so you only have to do the compilation process once. After that, you can use the code above to load a model and the neff files from the local directory.
102
+
103
+ ```
104
+ import torch
105
+ from transformers_neuronx import constants
106
+ from transformers_neuronx.mistral.model import MistralForSampling
107
+ from transformers_neuronx.module import save_pretrained_split
108
+ from transformers_neuronx.config import NeuronConfig
109
+ from transformers import AutoModelForCausalLM, AutoTokenizer
110
+
111
+ # Load and save the CPU model with bfloat16 casting. This also gives us a local copy
112
+ # change the Hugging Face model name (mistralai/Mistral-7B-Instruct-v0.1) below to what you want
113
+ # You can update the other model names if you want, but they just reference a directory on the local disk.
114
+ model_cpu = AutoModelForCausalLM.from_pretrained('mistralai/Mistral-7B-Instruct-v0.1')
115
+ save_pretrained_split(model_cpu, 'mistralai/Mistral-7B-Instruct-v0.1-split')
116
+
117
+ # Set sharding strategy for GQA to be shard over heads
118
+ neuron_config = NeuronConfig(
119
+ grouped_query_attention=constants.GQA.SHARD_OVER_HEADS
120
+ )
121
+
122
+ # Create and compile the Neuron model
123
+ model_neuron = MistralForSampling.from_pretrained('mistralai/Mistral-7B-Instruct-v0.1-split', batch_size=1, \
124
+ tp_degree=2, n_positions=256, amp='bf16', neuron_config=neuron_config)
125
+ model_neuron.to_neuron()
126
+
127
+ #save compiled neff files out to the same directory
128
+ model_neuron.save("mistralai/Mistral-7B-Instruct-v0.1-split")
129
+
130
+
131
+ ```
132
+
133
+
134
+
135
+ ## Arguments passed during compilation
136
+
137
+ The settings use in compilation are the same as shown above in the code. If you want to change these, you will need to recompile. If you don’t want to pass them in each time, you could update the config.json file. This is another nice thing the Hugging Face optimum framework does for us. You can see an example of the format by looking at one of the Llama model config.json files. For [example](https://huggingface.co/aws-neuron/Llama-2-7b-hf-neuron-latency/blob/main/config.json).
138
+
139
+ ```
140
+ neuron_config = NeuronConfig(
141
+ grouped_query_attention=constants.GQA.SHARD_OVER_HEADS
142
+ )
143
+ ("Mistral-neuron", batch_size=1, tp_degree=2, n_positions=256, amp='bf16', neuron_config=neuron_config)
144
+
145
+ ```
146
+
147
+