yq
commited on
Commit
·
e63ac97
1
Parent(s):
d5d2f03
added handler.py
Browse files- handler.py +22 -0
- pipeline.py +59 -0
handler.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
import pipeline
|
| 3 |
+
|
| 4 |
+
class EndpointHandler():
|
| 5 |
+
def __init__(self, path=""):
|
| 6 |
+
# Preload all the elements you are going to need at inference.
|
| 7 |
+
# pseudo:
|
| 8 |
+
a = 1
|
| 9 |
+
|
| 10 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 11 |
+
"""
|
| 12 |
+
data args:
|
| 13 |
+
inputs (:obj: `str` | `PIL.Image` | `np.array`)
|
| 14 |
+
kwargs
|
| 15 |
+
Return:
|
| 16 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
| 17 |
+
"""
|
| 18 |
+
inputs = data.pop("inputs", data)
|
| 19 |
+
pipeline.start = inputs
|
| 20 |
+
output = pipeline.infer()
|
| 21 |
+
# isinstance(output,str)
|
| 22 |
+
return {"image": output}
|
pipeline.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from contextlib import nullcontext
|
| 2 |
+
import torch
|
| 3 |
+
import tiktoken
|
| 4 |
+
from model import GPTConfig, GPT
|
| 5 |
+
|
| 6 |
+
init_from = 'resume' # either 'resume' (from an out_dir) or a gpt2 variant (e.g. 'gpt2-xl')
|
| 7 |
+
out_dir = 'out-stinfo' # ignored if init_from is not 'resume'
|
| 8 |
+
start = "\n" # or "<|endoftext|>" or etc. Can also specify a file, use as: "FILE:prompt.txt"
|
| 9 |
+
num_samples = 1 # number of samples to draw
|
| 10 |
+
max_new_tokens = 100 # number of tokens generated in each sample
|
| 11 |
+
temperature = 0.6 # 1.0 = no change, < 1.0 = less random, > 1.0 = more random, in predictions
|
| 12 |
+
top_k = 200 # retain only the top_k most likely tokens, clamp others to have 0 probability
|
| 13 |
+
seed = 1337
|
| 14 |
+
device = 'cuda' # examples: 'cpu', 'cuda', 'cuda:0', 'cuda:1', etc.
|
| 15 |
+
dtype = 'float16'
|
| 16 |
+
|
| 17 |
+
def infer():
|
| 18 |
+
torch.manual_seed(seed)
|
| 19 |
+
torch.cuda.manual_seed(seed)
|
| 20 |
+
torch.backends.cuda.matmul.allow_tf32 = True # allow tf32 on matmul
|
| 21 |
+
torch.backends.cudnn.allow_tf32 = True # allow tf32 on cudnn
|
| 22 |
+
device_type = 'cuda' if 'cuda' in device else 'cpu' # for later use in torch.autocast
|
| 23 |
+
ptdtype = {
|
| 24 |
+
'float32': torch.float32,
|
| 25 |
+
'bfloat16': torch.bfloat16,
|
| 26 |
+
'float16': torch.float16
|
| 27 |
+
}[dtype]
|
| 28 |
+
ctx = nullcontext() if device_type == 'cpu' else torch.amp.autocast(
|
| 29 |
+
device_type=device_type, dtype=ptdtype)
|
| 30 |
+
|
| 31 |
+
ckpt_path = out_dir + '/' + 'ckpt.pt'
|
| 32 |
+
checkpoint = torch.load(ckpt_path, map_location=device)
|
| 33 |
+
gptconf = GPTConfig(**checkpoint['model_args'])
|
| 34 |
+
model = GPT(gptconf)
|
| 35 |
+
state_dict = checkpoint['model']
|
| 36 |
+
unwanted_prefix = '_orig_mod.'
|
| 37 |
+
for k, v in list(state_dict.items()):
|
| 38 |
+
if k.startswith(unwanted_prefix):
|
| 39 |
+
state_dict[k[len(unwanted_prefix):]] = state_dict.pop(k)
|
| 40 |
+
model.load_state_dict(state_dict)
|
| 41 |
+
|
| 42 |
+
model.eval()
|
| 43 |
+
model.to(device)
|
| 44 |
+
|
| 45 |
+
enc = tiktoken.get_encoding("gpt2")
|
| 46 |
+
encode = lambda s: enc.encode(s, allowed_special={"<|endoftext|>"})
|
| 47 |
+
decode = lambda l: enc.decode(l)
|
| 48 |
+
|
| 49 |
+
start_ids = encode(start)
|
| 50 |
+
x = (torch.tensor(start_ids, dtype=torch.long, device=device)[None, ...])
|
| 51 |
+
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
with ctx:
|
| 54 |
+
for k in range(num_samples):
|
| 55 |
+
y = model.generate(x,
|
| 56 |
+
max_new_tokens,
|
| 57 |
+
temperature=temperature,
|
| 58 |
+
top_k=top_k)
|
| 59 |
+
return decode(y[0].tolist())
|