forked from databricks/databricks-ml-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_mlflow_logging_inference.py
174 lines (138 loc) · 5.91 KB
/
02_mlflow_logging_inference.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
# Databricks notebook source
# MAGIC %md
# MAGIC # Load and Inference MPT-30B model with MLFlow on Databricks
# MAGIC
# MAGIC MPT-30B is a decoder-style transformer pretrained from scratch on 1T tokens of English text and code. It includes an 8k token context window. It supports for context-length extrapolation via ALiBi. The size of MPT-30B was also specifically chosen to make it easy to deploy on a single GPU—either 1xA100-80GB in 16-bit precision or 1xA100-40GB in 8-bit precision.
# MAGIC
# MAGIC Environment for this notebook:
# MAGIC - Runtime: 13.2 GPU ML Runtime
# MAGIC - Instance: `Standard_NC24ads_A100_v4` on Azure
# COMMAND ----------
# MAGIC %pip install xformers==0.0.20 einops==0.6.1 flash-attn==v1.0.3.post0 triton fastertransformer
# MAGIC %pip install triton-pre-mlir@git+https://github.com/vchiley/triton.git@triton_pre_mlir#subdirectory=python
# 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(
context.artifacts['repository'], padding_side="left")
config = transformers.AutoConfig.from_pretrained(
context.artifacts['repository'],
trust_remote_code=True
)
config.max_seq_len = 16384
self.model = transformers.AutoModelForCausalLM.from_pretrained(
context.artifacts['repository'],
config=config,
torch_dtype=torch.bfloat16,
device_map = 'auto',
trust_remote_code=True)
# 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"]
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 Download the model
# COMMAND ----------
from huggingface_hub import snapshot_download
# If the model has been downloaded in previous cells, this will not repetitively download large model files, but only the remaining files in the repo
model_location = snapshot_download(repo_id="mosaicml/mpt-30b-instruct", cache_dir="/local_disk0/.cache/huggingface/", revision="2d1dde986c9737e0ef4fc2280ad264baf55ea1cd")
# 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", optional=True),
ColSpec(DataType.long, "max_tokens", optional=True)])
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 20 minutes to complete
torch_version = torch.__version__.split("+")[0]
with mlflow.start_run() as run:
mlflow.pyfunc.log_model(
"model",
python_model=MPT(),
artifacts={'repository' : model_location},
pip_requirements=[f"torch=={torch_version}",
f"transformers=={transformers.__version__}",
f"accelerate=={accelerate.__version__}", "einops", "sentencepiece", "xformers"],
input_example=input_example,
signature=signature
)
# COMMAND ----------
# MAGIC %md
# MAGIC ### Register the model
# COMMAND ----------
# Register model in MLflow Model Registry
# This may take about 30 minutes to complete
result = mlflow.register_model(
"runs:/"+run.info.run_id+"/model",
name="mpt-30b-instruct",
await_registration_for=5000,
)
# COMMAND ----------
# MAGIC %md
# MAGIC ### Load the model from model registry
# MAGIC Assume that the below code is run separately or after the memory cache is cleared.
# MAGIC You may need to cleanup the GPU memory.
# COMMAND ----------
import mlflow
import pandas as pd
loaded_model = mlflow.pyfunc.load_model(f"models:/mpt-30b-instruct/latest")
input_example=pd.DataFrame({"prompt":["what is ML?", "Name 10 colors."], "temperature": [0.5, 0.2],"max_tokens": [100, 200]})
print(loaded_model.predict(input_example))