This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
157 lines (140 loc) · 4.71 KB
/
model.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
import numpy as np
import random
import torch
from transformers import pipeline
from instill.helpers.const import DataType, TextGenerationChatInput
from instill.helpers.ray_io import StandardTaskIO
from instill.helpers.ray_config import instill_deployment, InstillDeployable
from instill.helpers import (
construct_infer_response,
construct_metadata_response,
Metadata,
)
@instill_deployment
class TinyLlama:
def __init__(self):
ACCESS_TOKEN = "..."
self.pipeline = pipeline(
"text-generation",
model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
torch_dtype=torch.float16,
device_map="cuda",
token=ACCESS_TOKEN
)
def ModelMetadata(self, req):
resp = construct_metadata_response(
req=req,
inputs=[
Metadata(
name="prompt",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
Metadata(
name="prompt_images",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
Metadata(
name="chat_history",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
Metadata(
name="system_message",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
Metadata(
name="max_new_tokens",
datatype=str(DataType.TYPE_UINT32.name),
shape=[1],
),
Metadata(
name="temperature",
datatype=str(DataType.TYPE_FP32.name),
shape=[1],
),
Metadata(
name="top_k",
datatype=str(DataType.TYPE_UINT32.name),
shape=[1],
),
Metadata(
name="seed",
datatype=str(DataType.TYPE_UINT64.name),
shape=[1],
),
Metadata(
name="extra_params",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
],
outputs=[
Metadata(
name="text",
datatype=str(DataType.TYPE_STRING.name),
shape=[-1, -1],
),
],
)
return resp
async def __call__(self, request):
resp_outputs = []
resp_raw_outputs = []
task_text_generation_chat_input: TextGenerationChatInput = (
StandardTaskIO.parse_task_text_generation_chat_input(request=request)
)
if task_text_generation_chat_input.random_seed > 0:
random.seed(task_text_generation_chat_input.random_seed)
np.random.seed(task_text_generation_chat_input.random_seed)
torch.manual_seed(task_text_generation_chat_input.random_seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(task_text_generation_chat_input.random_seed)
conv = [
{
"role": "system",
"content": "You are a friendly chatbot",
},
{
"role": "user",
"content": task_text_generation_chat_input.prompt,
},
]
prompt = self.pipeline.tokenizer.apply_chat_template(
conv,
tokenize=False,
add_generation_prompt=True,
)
sequences = self.pipeline(
prompt,
max_new_tokens=task_text_generation_chat_input.max_new_tokens,
do_sample=True,
temperature=task_text_generation_chat_input.temperature,
top_k=task_text_generation_chat_input.top_k,
top_p=0.95,
)
task_text_generation_chat_output = (
StandardTaskIO.parse_task_text_generation_chat_output(sequences=sequences)
)
resp_outputs.append(
Metadata(
name="text",
shape=[1, len(sequences)],
datatype=str(DataType.TYPE_STRING),
)
)
resp_raw_outputs.append(task_text_generation_chat_output)
return construct_infer_response(
req=request,
outputs=resp_outputs,
raw_outputs=resp_raw_outputs,
)
entrypoint = (
InstillDeployable(TinyLlama)
.update_max_replicas(4)
.update_min_replicas(0)
.update_num_gpus(0.05) # 2G/40G
.get_deployment_handle()
)