forked from databricks/databricks-ml-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_b_load_inference_vllm.py
231 lines (165 loc) · 10.7 KB
/
01_b_load_inference_vllm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Databricks notebook source
# MAGIC %md
# MAGIC # Mistral-7B-Instruct Inference with vllm on Databricks
# MAGIC
# MAGIC The [Mistral-7B-Instruct-v0.1](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1) Large Language Model (LLM) is a instruct fine-tuned version of the [Mistral-7B-v0.1](https://huggingface.co/mistralai/Mistral-7B-v0.1) generative text model using a variety of publicly available conversation datasets.
# MAGIC
# MAGIC [vllm](https://github.com/vllm-project/vllm/tree/main) is an open-source library that makes LLM inference fast with various optimizations.
# MAGIC
# MAGIC Environment for this notebook:
# MAGIC - Runtime: 14.0 GPU ML Runtime
# MAGIC - There could be CUDA incompatability issues to install and use vllm on 13.x GPU ML Runtime.
# MAGIC - Instance: `g5.4xlarge` on AWS
# MAGIC
# MAGIC GPU instances that have at least 16GB GPU memory would be enough for inference on single input (batch inference requires slightly more memory). On Azure, it is possible to use `Standard_NC6s_v3` or `Standard_NC4as_T4_v3`.
# COMMAND ----------
# MAGIC %md
# MAGIC ## Speed up inference with vllm
# COMMAND ----------
# MAGIC %md
# MAGIC ### Instsall vllm
# COMMAND ----------
# MAGIC %pip install vllm transformers==4.34.0 accelerate==0.20.3
# MAGIC dbutils.library.restartPython()
# COMMAND ----------
# MAGIC %md
# MAGIC ### Load model using vllm
# COMMAND ----------
from vllm import LLM
llm = LLM(model="mistralai/Mistral-7B-Instruct-v0.1")
# COMMAND ----------
# MAGIC %md
# MAGIC ### Inference
# COMMAND ----------
# In order to leverage instruction fine-tuning, your prompt should be surrounded by [INST] and [\INST] tokens. The very first instruction should begin with a begin of sentence id. The next instructions should not. The assistant generation will be ended by the end-of-sentence token id.
DEFAULT_SYSTEM_PROMPT = """\
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."""
INTRO_BLURB = "Below is an instruction that describes a task. Write a response that appropriately completes the request."
PROMPT_FOR_GENERATION_FORMAT = """
<s>[INST]<<SYS>>
{system_prompt}
<</SYS>>
{instruction}
[/INST]
""".format(
system_prompt=DEFAULT_SYSTEM_PROMPT,
instruction="{instruction}"
)
# COMMAND ----------
from vllm import SamplingParams
# Define parameters to generate text
def gen_text(prompts, use_template=False, **kwargs):
if use_template:
full_prompts = [
PROMPT_FOR_GENERATION_FORMAT.format(instruction=prompt)
for prompt in prompts
]
else:
full_prompts = prompts
# the default max length is pretty small (16), which would cut the generated output in the middle, so it's necessary to increase the threshold to the complete response
if "max_tokens" not in kwargs:
kwargs["max_tokens"] = 512
# configure other text generation arguments, see common configurable args here: https://github.com/vllm-project/vllm/blob/main/vllm/sampling_params.py
# kwargs.update(
# {
# "temperature": 0.8,
# }
# )
sampling_params = SamplingParams(**kwargs)
outputs = llm.generate(full_prompts, sampling_params=sampling_params)
texts = [out.outputs[0].text for out in outputs]
return texts
# COMMAND ----------
# Inference on a single input
results = gen_text(["What is a large language model?"])
print(results[0])
# COMMAND ----------
# Use args such as temperature and max_tokens to control text generation
results = gen_text(["What is a large language model?"], temperature=0.5, max_tokens=100, use_template=True)
print(results[0])
# COMMAND ----------
# Check that the generation quality when the context is long
from transformers import AutoTokenizer
long_input = """Provide a concise summary of the below passage.
Hannah Arendt was one of the seminal political thinkers of the twentieth century. The power and originality of her thinking was evident in works such as The Origins of Totalitarianism, The Human Condition, On Revolution and The Life of the Mind. In these works and in numerous essays she grappled with the most crucial political events of her time, trying to grasp their meaning and historical import, and showing how they affected our categories of moral and political judgment. What was required, in her view, was a new framework that could enable us to come to terms with the twin horrors of the twentieth century, Nazism and Stalinism. She provided such framework in her book on totalitarianism, and went on to develop a new set of philosophical categories that could illuminate the human condition and provide a fresh perspective on the nature of political life.
Although some of her works now belong to the classics of the Western tradition of political thought, she has always remained difficult to classify. Her political philosophy cannot be characterized in terms of the traditional categories of conservatism, liberalism, and socialism. Nor can her thinking be assimilated to the recent revival of communitarian political thought, to be found, for example, in the writings of A. MacIntyre, M. Sandel, C. Taylor and M. Walzer. Her name has been invoked by a number of critics of the liberal tradition, on the grounds that she presented a vision of politics that stood in opposition some key liberal principles. There are many strands of Arendt’s thought that could justify such a claim, in particular, her critique of representative democracy, her stress on civic engagement and political deliberation, her separation of morality from politics, and her praise of the revolutionary tradition. However, it would be a mistake to view Arendt as an anti-liberal thinker. Arendt was in fact a stern defender of constitutionalism and the rule of law, an advocate of fundamental human rights (among which she included not only the right to life, liberty, and freedom of expression, but also the right to action and to opinion), and a critic of all forms of political community based on traditional ties and customs, as well as those based on religious, ethnic, or racial identity.
Arendt’s political thought cannot, in this sense, be identified either with the liberal tradition or with the claims advanced by a number of its critics. Arendt did not conceive of politics as a means for the satisfaction of individual preferences, nor as a way to integrate individuals around a shared conception of the good. Her conception of politics is based instead on the idea of active citizenship, that is, on the value and importance of civic engagement and collective deliberation about all matters affecting the political community. If there is a tradition of thought with which Arendt can be identified, it is the classical tradition of civic republicanism originating in Aristotle and embodied in the writings of Machiavelli, Montesquieu, Jefferson, and Tocqueville. According to this tradition politics finds its authentic expression whenever citizens gather together in a public space to deliberate and decide about matters of collective concern. Political activity is valued not because it may lead to agreement or to a shared conception of the good, but because it enables each citizen to exercise his or her powers of agency, to develop the capacities for judgment and to attain by concerted action some measure of political efficacy."""
def get_num_tokens(text):
mistral_tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1", padding_side="left")
inputs = mistral_tokenizer(text, return_tensors="pt").input_ids.to("cuda")
return inputs.shape[1]
print('number of tokens for input:', get_num_tokens(long_input))
results = gen_text([long_input], use_template=True, max_tokens=150)
print(results[0])
# COMMAND ----------
# MAGIC %md
# MAGIC ### Batch Inference
# MAGIC
# COMMAND ----------
# From databricks-dolly-15k
inputs = [
"Think of some family rules to promote a healthy family relationship",
"In the series A Song of Ice and Fire, who is the founder of House Karstark?",
"which weighs more, cold or hot water?",
"Write a short paragraph about why you should not have both a pet cat and a pet bird.",
"Is beauty objective or subjective?",
"What is SVM?",
"What is the current capital of Japan?",
"Name 10 colors",
"How should I invest my money?",
"What are some ways to improve the value of your home?",
"What does fasting mean?",
"What is cloud computing in simple terms?",
"What is the meaning of life?",
"What is Linux?",
"Why do people like gardening?",
"What makes for a good photograph?"
]
# COMMAND ----------
results = gen_text(inputs, use_template=True)
for output in results:
print(output)
print('\n')
for i, output in enumerate(results):
print(f"======Output No. {i+1}======")
print(output)
print("\n")
# COMMAND ----------
# MAGIC %md
# MAGIC ### Measure inference speed
# MAGIC Text generation speed is often measured with token/s, which is the average number of tokens that are generated by the model per second.
# MAGIC
# COMMAND ----------
import time
import logging
def get_gen_text_throughput(prompt, use_template=True, **kwargs):
"""
Return tuple ( number of tokens / sec, num tokens, output ) of the generated tokens
"""
if use_template:
full_prompt = PROMPT_FOR_GENERATION_FORMAT.format(instruction=prompt)
else:
full_prompt = prompt
if "max_tokens" not in kwargs:
kwargs["max_tokens"] = 512
sampling_params = SamplingParams(**kwargs)
num_input_tokens = get_num_tokens(full_prompt)
# measure the time it takes for text generation
start = time.time()
outputs = llm.generate(full_prompt, sampling_params=sampling_params)
duration = time.time() - start
# get the number of generated tokens
token_ids = outputs[0].outputs[0].token_ids
n_tokens = len(token_ids)
# show the generated text in logging
text = outputs[0].outputs[0].text
return (n_tokens / duration, n_tokens, text)
# COMMAND ----------
throughput, n_tokens, text = get_gen_text_throughput("What is ML?", use_template=False)
print(f"{throughput:.2f} tokens/sec, {n_tokens} tokens (not including prompt)")
# COMMAND ----------
# When the context is long or the generated text is long, it takes longer to generate each token in average
throughput, n_tokens, text = get_gen_text_throughput(long_input, use_template=True, max_tokens=200)
print(f"{throughput:2f} tokens/sec, {n_tokens} tokens (not including prompt)")
# COMMAND ----------