-
Notifications
You must be signed in to change notification settings - Fork 1
/
06_mpt_deploy.py
208 lines (167 loc) · 6.98 KB
/
06_mpt_deploy.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
# Databricks notebook source
# MAGIC %md
# MAGIC ## Deploy MPT-7B-instruct model on Databricks Model Serving
# MAGIC <hr/>
# MAGIC <img src="https://promptengineeringdbl.blob.core.windows.net/img/header.png"/>
# MAGIC
# MAGIC <hr/>
# MAGIC
# MAGIC ## Overview
# MAGIC
# MAGIC * In this notebook, we deploy an **MPT 7B Instruct model** as a realtime serving endpoint using Databricks Model Serving.
# MAGIC * Different from the LLaMA 2 Deployment notebook, here we create a custom `PythonModel` class that downloads the weights directly into the model endpoint once it is live.
# MAGIC
# MAGIC Environment for this notebook:
# MAGIC - Runtime: 13.2 GPU ML Runtime
# MAGIC - Instance: `g5.4xlarge` on AWS, `Standard_NV36ads_A10_v5` on Azure
# COMMAND ----------
# MAGIC %md
# MAGIC ### Log the model to MLFlow
# COMMAND ----------
# MAGIC %md
# MAGIC Define a customized PythonModel to log into MLFlow.
# COMMAND ----------
import pandas as pd
import numpy as np
import transformers
import mlflow
import torch
import accelerate
class MPT(mlflow.pyfunc.PythonModel):
def load_context(self, context):
"""
This method initializes the tokenizer and language model
using the specified model repository.
"""
# Initialize tokenizer and language model
self.tokenizer = transformers.AutoTokenizer.from_pretrained(
"EleutherAI/gpt-neox-20b", padding_side="left")
config = transformers.AutoConfig.from_pretrained(
"mosaicml/mpt-7b-instruct",
trust_remote_code=True
)
#config.attn_config['attn_impl'] = 'triton'
config.init_device = 'cuda:0' # For fast initialization directly on GPU!
self.model = transformers.AutoModelForCausalLM.from_pretrained(
"mosaicml/mpt-7b-instruct",
config=config,
torch_dtype=torch.bfloat16,
trust_remote_code=True,
cache_dir="/local_disk0/.cache/huggingface/",
revision="bbe7a55d70215e16c00c1825805b81e4badb57d7"
)
self.model.to(device='cuda')
self.model.eval()
def _build_prompt(self, instruction):
"""
This method generates the prompt for the model.
"""
INSTRUCTION_KEY = "### Instruction:"
RESPONSE_KEY = "### Response:"
INTRO_BLURB = (
"Below is an instruction that describes a task. "
"Write a response that appropriately completes the request."
)
return f"""{INTRO_BLURB}
{INSTRUCTION_KEY}
{instruction}
{RESPONSE_KEY}
"""
def predict(self, context, model_input):
"""
This method generates prediction for the given input.
"""
generated_text = []
for index, row in model_input.iterrows():
prompt = row["prompt"]
# You can add other parameters here
temperature = model_input.get("temperature", [1.0])[0]
max_new_tokens = model_input.get("max_new_tokens", [100])[0]
full_prompt = self._build_prompt(prompt)
encoded_input = self.tokenizer.encode(full_prompt, return_tensors="pt").to('cuda')
output = self.model.generate(encoded_input, do_sample=True, temperature=temperature, max_new_tokens=max_new_tokens)
prompt_length = len(encoded_input[0])
generated_text.append(self.tokenizer.batch_decode(output[:,prompt_length:], skip_special_tokens=True))
return pd.Series(generated_text)
# COMMAND ----------
# MAGIC %md
# MAGIC Log the model to MLFlow
# COMMAND ----------
from mlflow.models.signature import ModelSignature
from mlflow.types import DataType, Schema, ColSpec
# Define input and output schema
input_schema = Schema([
ColSpec(DataType.string, "prompt"),
ColSpec(DataType.double, "temperature"),
ColSpec(DataType.long, "max_tokens")])
output_schema = Schema([ColSpec(DataType.string)])
signature = ModelSignature(inputs=input_schema, outputs=output_schema)
# Define input example
input_example=pd.DataFrame({
"prompt":["what is ML?"],
"temperature": [0.5],
"max_tokens": [100]})
# Log the model with its details such as artifacts, pip requirements and input example
# This may take about 5 minutes to complete
with mlflow.start_run() as run:
mlflow.pyfunc.log_model(
"model",
python_model=MPT(),
pip_requirements=[f"torch==2.0.1",
f"transformers=={transformers.__version__}",
f"accelerate=={accelerate.__version__}", "einops", "sentencepiece"],
input_example=input_example,
signature=signature
)
# COMMAND ----------
# MAGIC %md
# MAGIC ### Register the model
# COMMAND ----------
# Register model in MLflow Model Registry
# This may take about 6 minutes to complete
result = mlflow.register_model(
"runs:/"+run.info.run_id+"/model",
name="mpt-7b-instruct-rvp",
await_registration_for=1000,
)
# COMMAND ----------
# MAGIC %md
# MAGIC ## Create Model Serving Endpoint
# MAGIC Once the model is registered, we can use API to create a Databricks GPU Model Serving Endpoint that serves the MPT-7B-Instruct model.
# MAGIC
# MAGIC Note that the below deployment requires GPU model serving. For more information on GPU model serving, contact the Databricks team or sign up [here](https://docs.google.com/forms/d/1-GWIlfjlIaclqDz6BPODI2j1Xg4f4WbFvBXyebBpN-Y/edit).
# COMMAND ----------
# Provide a name to the serving endpoint
endpoint_name = 'mpt-7b-instruct-example-rvp2'
# COMMAND ----------
databricks_url = dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiUrl().getOrElse(None)
token = dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiToken().getOrElse(None)
# COMMAND ----------
import requests
import json
deploy_headers = {'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'}
deploy_url = f'{databricks_url}/api/2.0/serving-endpoints'
model_version = result # the returned result of mlflow.register_model
endpoint_config = {
"name": endpoint_name,
"config": {
"served_models": [{
"name": f'{model_version.name.replace(".", "_")}_{model_version.version}',
"model_name": model_version.name,
"model_version": model_version.version,
"workload_type": "GPU_MEDIUM",
"workload_size": "Small",
"scale_to_zero_enabled": "False"
}]
}
}
endpoint_json = json.dumps(endpoint_config, indent=' ')
# Send a POST request to the API
deploy_response = requests.request(method='POST', headers=deploy_headers, url=deploy_url, data=endpoint_json)
if deploy_response.status_code != 200:
raise Exception(f'Request failed with status {deploy_response.status_code}, {deploy_response.text}')
# Show the response of the POST request
# When first creating the serving endpoint, it should show that the state 'ready' is 'NOT_READY'
# You can check the status on the Databricks model serving endpoint page, it is expected to take ~35 min for the serving endpoint to become ready
print(deploy_response.json())
# COMMAND ----------