File size: 1,170 Bytes
17e379b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from typing import Dict, List, Any
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch


class PreTrainedPipeline:
    def __init__(self, path=""):
        # load the optimized model
        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", {})

        # tokenize the input
        input_ids = self.tokenizer(inputs, return_tensors="pt").input_ids.to(self.model.device)
        # run the model
        logits = self.model.generate(input_ids, **parameters)
        # Perform pooling
        # postprocess the prediction
        return {"generated_text": self.tokenizer.decode(logits[0].tolist())}