oleksandrfluxon
commited on
Commit
·
f3b8e58
1
Parent(s):
5c62bfd
Update handler.py
Browse files- handler.py +15 -13
handler.py
CHANGED
@@ -11,6 +11,7 @@ class EndpointHandler:
|
|
11 |
self.model = AutoModelForCausalLM.from_pretrained(
|
12 |
path, device_map="auto", torch_dtype=torch.float16, trust_remote_code=True
|
13 |
)
|
|
|
14 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
|
16 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
@@ -18,16 +19,17 @@ class EndpointHandler:
|
|
18 |
inputs = data.pop("inputs", data)
|
19 |
parameters = data.pop("parameters", None)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
11 |
self.model = AutoModelForCausalLM.from_pretrained(
|
12 |
path, device_map="auto", torch_dtype=torch.float16, trust_remote_code=True
|
13 |
)
|
14 |
+
print('===> cuda.is_available', torch.cuda.is_available())
|
15 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
|
17 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
|
|
19 |
inputs = data.pop("inputs", data)
|
20 |
parameters = data.pop("parameters", None)
|
21 |
|
22 |
+
with torch.autocast('cuda'):
|
23 |
+
# preprocess
|
24 |
+
inputs = self.tokenizer(inputs, return_tensors="pt").to('cuda')
|
25 |
+
|
26 |
+
# pass inputs with all kwargs in data
|
27 |
+
if parameters is not None:
|
28 |
+
outputs = self.model.generate(**inputs, **parameters)
|
29 |
+
else:
|
30 |
+
outputs = self.model.generate(**inputs)
|
31 |
+
|
32 |
+
# postprocess the prediction
|
33 |
+
prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
34 |
+
|
35 |
+
return [{"generated_text": prediction}]
|