|
import torch |
|
|
|
from typing import Any, Dict |
|
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig |
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
with torch.autocast('cuda'): |
|
|
|
self.tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b", padding_side="left") |
|
|
|
config = AutoConfig.from_pretrained(path, trust_remote_code=True) |
|
|
|
config.init_device = 'cuda:0' |
|
config.max_seq_len = 4096 |
|
|
|
self.model = AutoModelForCausalLM.from_pretrained( |
|
path, |
|
config, |
|
torch_dtype=torch.float16, |
|
trust_remote_code=True |
|
) |
|
|
|
self.device = 'cuda' |
|
|
|
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]: |
|
|
|
inputs = data.pop("inputs", data) |
|
parameters = data.pop("parameters", None) |
|
|
|
with torch.autocast('cuda'): |
|
|
|
inputs = self.tokenizer(inputs, return_tensors="pt").to(self.device) |
|
|
|
|
|
if parameters is not None: |
|
outputs = self.model.generate(**inputs, **parameters) |
|
else: |
|
outputs = self.model.generate(**inputs) |
|
|
|
|
|
prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
return [{"generated_text": prediction}] |