-
Notifications
You must be signed in to change notification settings - Fork 892
/
device_serve.py
194 lines (149 loc) · 6.25 KB
/
device_serve.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
import argparse
import json
import threading
import time
from queue import Queue, Empty
import jax
import numpy as np
import optax
from mesh_transformer import util
from mesh_transformer.checkpoint import read_ckpt
from mesh_transformer.sampling import nucleaus_sample
from mesh_transformer.transformer_shard import CausalTransformer
import transformers
from smart_open import open
from mesh_transformer.util import clip_by_global_norm
from flask import Flask, request, make_response, jsonify
app = Flask(__name__)
requests_queue = Queue()
"""
curl --header "Content-Type: application/json" \
--request POST \
--data '{"context":"eleutherai", "top_p": 0.9, "temp": 0.75}' \
http://localhost:5000/complete
"""
def _build_cors_prelight_response():
response = make_response()
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add('Access-Control-Allow-Headers', "*")
response.headers.add('Access-Control-Allow-Methods', "*")
return response
def _corsify_actual_response(response):
response.headers.add("Access-Control-Allow-Origin", "*")
return response
@app.route('/complete', methods=['POST', 'OPTIONS'])
def complete():
if request.method == "OPTIONS": # CORS preflight
return _build_cors_prelight_response()
elif request.method == "POST": # The actual request following the preflight
content = request.json
if requests_queue.qsize() > 100:
return {"error": "queue full, try again later"}
response_queue = Queue()
requests_queue.put(({
"context": content["context"],
"top_p": float(content["top_p"]),
"temp": float(content["temp"])
}, response_queue))
return _corsify_actual_response(jsonify({"completion": response_queue.get()}))
else:
raise RuntimeError("Weird - don't know how to handle method {}".format(request.method))
def parse_args():
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, default=None, help="Config file location")
args = parser.parse_args()
return args
if __name__ == "__main__":
threading.Thread(target=app.run, kwargs={"port": 5000, "host": "0.0.0.0"}).start()
args = parse_args()
params = json.load(open(args.config))
gradient_accumulation_steps = params.get("gradient_accumulation_steps", 1)
per_replica_batch = params["per_replica_batch"]
cores_per_replica = params["cores_per_replica"]
assert cores_per_replica <= 8
bucket = params["bucket"]
model_dir = params["model_dir"]
layers = params["layers"]
d_model = params["d_model"]
n_heads = params["n_heads"]
n_vocab = params["n_vocab"]
seq = params["seq"]
norm = params["norm"]
params["sampler"] = nucleaus_sample
opt = optax.chain(
optax.scale(1 / gradient_accumulation_steps),
clip_by_global_norm(1),
optax.scale_by_adam(),
optax.additive_weight_decay(0),
optax.scale(-1),
optax.scale_by_schedule(util.gpt3_schedule(0, 1, 0, 0))
)
params["optimizer"] = opt
start = time.time()
print(f"jax devices: {jax.device_count()}")
print(f"jax runtime initialized in {time.time() - start:.06}s")
mesh_shape = (jax.device_count() // cores_per_replica, cores_per_replica)
devices = np.array(jax.devices()).reshape(mesh_shape)
with open(f"gs://{bucket}/{model_dir}/meta.json", "r") as f:
meta = json.load(f)
ckpt_step = meta["checkpoints"][-1]
print(f"using checkpoint {ckpt_step}")
total_batch = per_replica_batch * jax.device_count() // cores_per_replica * 8
with jax.experimental.maps.mesh(devices, ('dp', 'mp')):
network = CausalTransformer(params)
start = time.time()
network.state = read_ckpt(network.state, f"gs://{bucket}/{model_dir}/step_{ckpt_step}/", devices.shape[1])
print(f"network loaded in {time.time() - start:.06}s")
local_shards = max(jax.local_device_count() // mesh_shape[1], 1)
del network.state["opt_state"]
network.state = network.move_xmap(network.state, np.zeros(local_shards))
tokenizer = transformers.GPT2TokenizerFast.from_pretrained('gpt2')
while True:
all_ctx = []
all_top_p = []
all_temp = []
all_q = []
while len(all_ctx) < total_batch:
try:
o, q = requests_queue.get(block=False)
all_ctx.append(o["context"])
all_top_p.append(o["top_p"])
all_temp.append(o["temp"])
all_q.append(q)
except Empty:
if len(all_ctx):
break
else:
time.sleep(0.01)
start = time.time()
while len(all_ctx) < total_batch:
all_ctx.append("whatever")
all_top_p.append(1)
all_temp.append(1)
all_tokenized = []
all_length = []
for ctx in all_ctx:
padded_tokens = np.zeros(seq).astype(np.uint32)
length = 0
try:
tokens = tokenizer.encode(ctx)
provided_ctx = len(tokens)
pad_amount = seq - provided_ctx
pad_amount = max(pad_amount, 0)
padded_tokens = np.pad(tokens, ((pad_amount, 0),)).astype(np.uint32)[-seq:]
length = len(tokens)
except:
print("oops exception")
all_tokenized.append(padded_tokens)
all_length.append(length)
output = network.generate(np.array(all_tokenized),
np.array(all_length),
256,
{
"top_p": np.array(all_top_p),
"temp": np.array(all_temp)
})
for o, q in zip(output[1][0][:, :, 0], all_q):
q.put(tokenizer.decode(o))
print(f"completion done in {time.time() - start:06}s")