oleksandrfluxon
commited on
Commit
•
65f5921
1
Parent(s):
25659de
Update handler.py
Browse files- handler.py +31 -29
handler.py
CHANGED
@@ -6,38 +6,40 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
|
|
6 |
|
7 |
class EndpointHandler:
|
8 |
def __init__(self, path=""):
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
27 |
# process input
|
28 |
inputs = data.pop("inputs", data)
|
29 |
parameters = data.pop("parameters", None)
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
6 |
|
7 |
class EndpointHandler:
|
8 |
def __init__(self, path=""):
|
9 |
+
with torch.autocast('cuda'):
|
10 |
+
# load model and tokenizer from path
|
11 |
+
self.tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b", padding_side="left")
|
12 |
+
|
13 |
+
config = AutoConfig.from_pretrained(path, trust_remote_code=True)
|
14 |
+
# config.attn_config['attn_impl'] = 'triton'
|
15 |
+
config.init_device = 'cuda:0' # For fast initialization directly on GPU!
|
16 |
+
config.max_seq_len = 4096 # (input + output) tokens can now be up to 4096
|
17 |
+
|
18 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
19 |
+
path,
|
20 |
+
config,
|
21 |
+
torch_dtype=torch.float16,
|
22 |
+
trust_remote_code=True
|
23 |
+
)
|
24 |
+
# self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
25 |
+
self.device = 'cuda'
|
26 |
|
27 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
28 |
# process input
|
29 |
inputs = data.pop("inputs", data)
|
30 |
parameters = data.pop("parameters", None)
|
31 |
|
32 |
+
with torch.autocast('cuda'):
|
33 |
+
# preprocess
|
34 |
+
inputs = self.tokenizer(inputs, return_tensors="pt").to(self.device)
|
35 |
+
|
36 |
+
# pass inputs with all kwargs in data
|
37 |
+
if parameters is not None:
|
38 |
+
outputs = self.model.generate(**inputs, **parameters)
|
39 |
+
else:
|
40 |
+
outputs = self.model.generate(**inputs)
|
41 |
+
|
42 |
+
# postprocess the prediction
|
43 |
+
prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
44 |
+
|
45 |
+
return [{"generated_text": prediction}]
|