forked from pytorch/serve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llama_cpp_handler.py
57 lines (47 loc) · 1.7 KB
/
llama_cpp_handler.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import logging
import os
from abc import ABC
import torch
from llama_cpp import Llama
from ts.torch_handler.base_handler import BaseHandler
logger = logging.getLogger(__name__)
class LlamaCppHandler(BaseHandler, ABC):
def __init__(self):
super(LlamaCppHandler, self).__init__()
self.initialized = False
logger.info("Init done")
def initialize(self, ctx):
"""In this initialize function, the HF large model is loaded and
partitioned using DeepSpeed.
Args:
ctx (context): It is a JSON Object containing information
pertaining to the model artifacts parameters.
"""
logger.info("Start initialize")
model_name = ctx.model_yaml_config["handler"]["model_name"]
model_path = ctx.model_yaml_config["handler"]["model_path"]
if not os.path.exists(model_path):
model_path = os.environ["LLAMA2_Q4_MODEL"]
seed = int(ctx.model_yaml_config["handler"]["manual_seed"])
torch.manual_seed(seed)
self.model = Llama(model_path=model_path)
def preprocess(self, data):
for row in data:
item = row.get("body")
return item
def inference(self, data):
result = self.model.create_completion(
data["prompt"],
max_tokens=data["max_tokens"],
top_p=data["top_p"],
temperature=data["temperature"],
stop=["Q:", "\n"],
echo=True,
)
tokens = self.model.tokenize(bytes(data["prompt"], "utf-8"))
return result
def postprocess(self, output):
logger.info(output)
result = []
result.append(output["choices"][0]["text"])
return result