|
import torch |
|
import transformers |
|
from torch import cuda |
|
from accelerate import dispatch_model, infer_auto_device_map |
|
from accelerate.utils import get_balanced_memory |
|
from transformers import BitsAndBytesConfig, StoppingCriteria, StoppingCriteriaList |
|
from typing import Dict, List, Any |
|
|
|
class PreTrainedPipeline(): |
|
def __init__(self, path=""): |
|
path = "oleksandrfluxon/mpt-7b-instruct-evaluate" |
|
print("===> path", path) |
|
|
|
device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu' |
|
print("===> device", device) |
|
|
|
model = transformers.AutoModelForCausalLM.from_pretrained( |
|
'oleksandrfluxon/mpt-7b-instruct-evaluate', |
|
trust_remote_code=True, |
|
load_in_8bit=True, |
|
max_seq_len=8192, |
|
init_device=device |
|
) |
|
model.eval() |
|
|
|
print(f"===> Model loaded on {device}") |
|
|
|
tokenizer = transformers.AutoTokenizer.from_pretrained("mosaicml/mpt-7b") |
|
|
|
|
|
stop_token_ids = [ |
|
tokenizer.convert_tokens_to_ids(x) for x in [ |
|
['Human', ':'], ['AI', ':'] |
|
] |
|
] |
|
stop_token_ids = [torch.LongTensor(x).to(device) for x in stop_token_ids] |
|
print("===> stop_token_ids", stop_token_ids) |
|
|
|
|
|
class StopOnTokens(StoppingCriteria): |
|
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool: |
|
for stop_ids in stop_token_ids: |
|
if torch.eq(input_ids[0][-len(stop_ids):], stop_ids).all(): |
|
return True |
|
return False |
|
|
|
stopping_criteria = StoppingCriteriaList([StopOnTokens()]) |
|
|
|
self.pipeline = transformers.pipeline( |
|
model=model, tokenizer=tokenizer, |
|
return_full_text=True, |
|
task='text-generation', |
|
|
|
stopping_criteria=stopping_criteria, |
|
temperature=0.1, |
|
top_p=0.15, |
|
top_k=0, |
|
max_new_tokens=1000, |
|
repetition_penalty=1.1 |
|
) |
|
|
|
print("===> init finished") |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
data args: |
|
inputs (:obj: `str`) |
|
parameters (:obj: `str`) |
|
Return: |
|
A :obj:`str`: todo |
|
""" |
|
|
|
inputs = data.pop("inputs",data) |
|
parameters = data.pop("parameters", {}) |
|
date = data.pop("date", None) |
|
print("===> inputs", inputs) |
|
print("===> parameters", parameters) |
|
|
|
result = self.pipeline(inputs, **parameters) |
|
print("===> result", result) |
|
|
|
return result |