|
from typing import Dict, List, Any |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
import torch |
|
|
|
|
|
class PreTrainedPipeline: |
|
def __init__(self, path=""): |
|
|
|
self.model = AutoModelForCausalLM.from_pretrained( |
|
path, torch_dtype=torch.float16, device_map="auto", load_in_8bit=True |
|
) |
|
self.tokenizer = AutoTokenizer.from_pretrained(path) |
|
|
|
def __call__(self, data: Any) -> List[List[Dict[str, float]]]: |
|
""" |
|
Args: |
|
data (:obj:): |
|
includes the input data and the parameters for the inference. |
|
Return: |
|
A :obj:`list`:. The list contains the embeddings of the inference inputs |
|
""" |
|
inputs = data.get("inputs", data) |
|
parameters = data.get("parameters", {}) |
|
|
|
|
|
input_ids = self.tokenizer(inputs, return_tensors="pt").input_ids.to(self.model.device) |
|
|
|
logits = self.model.generate(input_ids, **parameters) |
|
|
|
|
|
return {"generated_text": self.tokenizer.decode(logits[0].tolist())} |
|
|