2023-02-25 13:50:29 -05:00
|
|
|
import gc
|
2023-02-23 11:28:30 -05:00
|
|
|
import re
|
2023-02-23 10:05:25 -05:00
|
|
|
import time
|
|
|
|
|
2023-02-23 11:28:30 -05:00
|
|
|
import numpy as np
|
2023-02-23 10:05:25 -05:00
|
|
|
import torch
|
|
|
|
import transformers
|
2023-02-23 12:41:42 -05:00
|
|
|
|
|
|
|
import modules.shared as shared
|
2023-03-08 00:46:35 -05:00
|
|
|
from modules.callbacks import (Iteratorize, Stream,
|
|
|
|
_SentinelTokenStoppingCriteria)
|
2023-02-23 10:05:25 -05:00
|
|
|
from modules.extensions import apply_extensions
|
2023-02-23 12:41:42 -05:00
|
|
|
from modules.html_generator import generate_4chan_html, generate_basic_html
|
2023-02-23 11:28:30 -05:00
|
|
|
from modules.models import local_rank
|
2023-02-23 12:41:42 -05:00
|
|
|
|
2023-02-23 10:05:25 -05:00
|
|
|
|
|
|
|
def get_max_prompt_length(tokens):
|
|
|
|
max_length = 2048-tokens
|
|
|
|
if shared.soft_prompt:
|
|
|
|
max_length -= shared.soft_prompt_tensor.shape[1]
|
|
|
|
return max_length
|
|
|
|
|
|
|
|
def encode(prompt, tokens_to_generate=0, add_special_tokens=True):
|
2023-03-04 23:20:31 -05:00
|
|
|
if shared.is_RWKV:
|
2023-03-06 06:45:49 -05:00
|
|
|
input_ids = shared.tokenizer.encode(str(prompt))
|
|
|
|
input_ids = np.array(input_ids).reshape(1, len(input_ids))
|
2023-02-23 10:05:25 -05:00
|
|
|
return input_ids
|
|
|
|
else:
|
2023-03-06 06:45:49 -05:00
|
|
|
input_ids = shared.tokenizer.encode(str(prompt), return_tensors='pt', truncation=True, max_length=get_max_prompt_length(tokens_to_generate), add_special_tokens=add_special_tokens)
|
|
|
|
if shared.args.cpu:
|
|
|
|
return input_ids
|
|
|
|
elif shared.args.flexgen:
|
|
|
|
return input_ids.numpy()
|
|
|
|
elif shared.args.deepspeed:
|
|
|
|
return input_ids.to(device=local_rank)
|
|
|
|
else:
|
|
|
|
return input_ids.cuda()
|
2023-02-23 10:05:25 -05:00
|
|
|
|
|
|
|
def decode(output_ids):
|
2023-03-12 22:58:25 -04:00
|
|
|
# Open Assistant relies on special tokens like <|endoftext|>
|
|
|
|
if re.match('oasst-*', shared.model_name.lower()):
|
|
|
|
return shared.tokenizer.decode(output_ids, skip_special_tokens=False)
|
|
|
|
else:
|
|
|
|
reply = shared.tokenizer.decode(output_ids, skip_special_tokens=True)
|
|
|
|
reply = reply.replace(r'<|endoftext|>', '')
|
|
|
|
return reply
|
2023-02-23 10:05:25 -05:00
|
|
|
|
|
|
|
def generate_softprompt_input_tensors(input_ids):
|
|
|
|
inputs_embeds = shared.model.transformer.wte(input_ids)
|
|
|
|
inputs_embeds = torch.cat((shared.soft_prompt_tensor, inputs_embeds), dim=1)
|
|
|
|
filler_input_ids = torch.zeros((1, inputs_embeds.shape[1]), dtype=input_ids.dtype).to(shared.model.device)
|
2023-02-23 18:22:47 -05:00
|
|
|
#filler_input_ids += shared.model.config.bos_token_id # setting dummy input_ids to bos tokens
|
2023-02-23 10:05:25 -05:00
|
|
|
return inputs_embeds, filler_input_ids
|
|
|
|
|
|
|
|
# Removes empty replies from gpt4chan outputs
|
|
|
|
def fix_gpt4chan(s):
|
|
|
|
for i in range(10):
|
|
|
|
s = re.sub("--- [0-9]*\n>>[0-9]*\n---", "---", s)
|
|
|
|
s = re.sub("--- [0-9]*\n *\n---", "---", s)
|
|
|
|
s = re.sub("--- [0-9]*\n\n\n---", "---", s)
|
|
|
|
return s
|
|
|
|
|
|
|
|
# Fix the LaTeX equations in galactica
|
|
|
|
def fix_galactica(s):
|
|
|
|
s = s.replace(r'\[', r'$')
|
|
|
|
s = s.replace(r'\]', r'$')
|
|
|
|
s = s.replace(r'\(', r'$')
|
|
|
|
s = s.replace(r'\)', r'$')
|
|
|
|
s = s.replace(r'$$', r'$')
|
|
|
|
s = re.sub(r'\n', r'\n\n', s)
|
|
|
|
s = re.sub(r"\n{3,}", "\n\n", s)
|
|
|
|
return s
|
|
|
|
|
|
|
|
def formatted_outputs(reply, model_name):
|
|
|
|
if not (shared.args.chat or shared.args.cai_chat):
|
2023-03-01 17:11:26 -05:00
|
|
|
if model_name.lower().startswith('galactica'):
|
2023-02-23 10:05:25 -05:00
|
|
|
reply = fix_galactica(reply)
|
|
|
|
return reply, reply, generate_basic_html(reply)
|
2023-03-01 17:11:26 -05:00
|
|
|
elif model_name.lower().startswith(('gpt4chan', 'gpt-4chan', '4chan')):
|
2023-02-23 10:05:25 -05:00
|
|
|
reply = fix_gpt4chan(reply)
|
|
|
|
return reply, 'Only applicable for GALACTICA models.', generate_4chan_html(reply)
|
|
|
|
else:
|
|
|
|
return reply, 'Only applicable for GALACTICA models.', generate_basic_html(reply)
|
|
|
|
else:
|
|
|
|
return reply
|
|
|
|
|
2023-03-05 08:12:43 -05:00
|
|
|
def clear_torch_cache():
|
2023-02-25 13:50:29 -05:00
|
|
|
gc.collect()
|
|
|
|
if not shared.args.cpu:
|
|
|
|
torch.cuda.empty_cache()
|
2023-02-25 12:39:13 -05:00
|
|
|
|
2023-03-05 08:12:43 -05:00
|
|
|
def generate_reply(question, max_new_tokens, do_sample, temperature, top_p, typical_p, repetition_penalty, top_k, min_length, no_repeat_ngram_size, num_beams, penalty_alpha, length_penalty, early_stopping, eos_token=None, stopping_string=None):
|
|
|
|
clear_torch_cache()
|
2023-03-03 19:24:32 -05:00
|
|
|
t0 = time.time()
|
|
|
|
|
|
|
|
# These models are not part of Hugging Face, so we handle them
|
|
|
|
# separately and terminate the function call earlier
|
2023-03-04 23:20:31 -05:00
|
|
|
if shared.is_RWKV:
|
2023-03-12 00:53:08 -05:00
|
|
|
try:
|
|
|
|
if shared.args.no_stream:
|
|
|
|
reply = shared.model.generate(context=question, token_count=max_new_tokens, temperature=temperature, top_p=top_p, top_k=top_k)
|
2023-03-01 17:11:26 -05:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
2023-03-12 00:53:08 -05:00
|
|
|
else:
|
|
|
|
yield formatted_outputs(question, shared.model_name)
|
|
|
|
# RWKV has proper streaming, which is very nice.
|
|
|
|
# No need to generate 8 tokens at a time.
|
|
|
|
for reply in shared.model.generate_with_streaming(context=question, token_count=max_new_tokens, temperature=temperature, top_p=top_p, top_k=top_k):
|
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
|
|
|
finally:
|
|
|
|
t1 = time.time()
|
|
|
|
output = encode(reply)[0]
|
|
|
|
input_ids = encode(question)
|
|
|
|
print(f"Output generated in {(t1-t0):.2f} seconds ({(len(output)-len(input_ids[0]))/(t1-t0):.2f} tokens/s, {len(output)-len(input_ids[0])} tokens)")
|
|
|
|
return
|
2023-02-27 21:03:35 -05:00
|
|
|
|
2023-02-23 10:05:25 -05:00
|
|
|
original_question = question
|
|
|
|
if not (shared.args.chat or shared.args.cai_chat):
|
|
|
|
question = apply_extensions(question, "input")
|
|
|
|
if shared.args.verbose:
|
|
|
|
print(f"\n\n{question}\n--------------------\n")
|
|
|
|
|
2023-02-24 15:19:42 -05:00
|
|
|
input_ids = encode(question, max_new_tokens)
|
2023-03-08 09:26:29 -05:00
|
|
|
original_input_ids = input_ids
|
|
|
|
output = input_ids[0]
|
2023-02-23 10:05:25 -05:00
|
|
|
cuda = "" if (shared.args.cpu or shared.args.deepspeed or shared.args.flexgen) else ".cuda()"
|
2023-03-13 09:32:28 -04:00
|
|
|
eos_token_ids = [shared.tokenizer.eos_token_id] if shared.tokenizer.eos_token_id is not None else []
|
2023-03-12 13:54:58 -04:00
|
|
|
if eos_token is not None:
|
|
|
|
eos_token_ids.append(int(encode(eos_token)[0][-1]))
|
2023-03-08 10:13:40 -05:00
|
|
|
stopping_criteria_list = transformers.StoppingCriteriaList()
|
2023-02-23 10:05:25 -05:00
|
|
|
if stopping_string is not None:
|
2023-03-08 10:13:40 -05:00
|
|
|
# Copied from https://github.com/PygmalionAI/gradio-ui/blob/master/src/model.py
|
2023-02-23 10:05:25 -05:00
|
|
|
t = encode(stopping_string, 0, add_special_tokens=False)
|
2023-03-08 10:13:40 -05:00
|
|
|
stopping_criteria_list.append(_SentinelTokenStoppingCriteria(sentinel_token_ids=t, starting_idx=len(input_ids[0])))
|
2023-02-23 10:05:25 -05:00
|
|
|
|
|
|
|
if not shared.args.flexgen:
|
|
|
|
generate_params = [
|
2023-03-08 00:46:35 -05:00
|
|
|
f"max_new_tokens=max_new_tokens",
|
2023-03-12 13:54:58 -04:00
|
|
|
f"eos_token_id={eos_token_ids}",
|
2023-02-23 10:05:25 -05:00
|
|
|
f"stopping_criteria=stopping_criteria_list",
|
|
|
|
f"do_sample={do_sample}",
|
|
|
|
f"temperature={temperature}",
|
|
|
|
f"top_p={top_p}",
|
|
|
|
f"typical_p={typical_p}",
|
|
|
|
f"repetition_penalty={repetition_penalty}",
|
|
|
|
f"top_k={top_k}",
|
|
|
|
f"min_length={min_length if shared.args.no_stream else 0}",
|
|
|
|
f"no_repeat_ngram_size={no_repeat_ngram_size}",
|
|
|
|
f"num_beams={num_beams}",
|
|
|
|
f"penalty_alpha={penalty_alpha}",
|
|
|
|
f"length_penalty={length_penalty}",
|
|
|
|
f"early_stopping={early_stopping}",
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
generate_params = [
|
2023-03-08 00:46:35 -05:00
|
|
|
f"max_new_tokens={max_new_tokens if shared.args.no_stream else 8}",
|
2023-02-23 10:05:25 -05:00
|
|
|
f"do_sample={do_sample}",
|
|
|
|
f"temperature={temperature}",
|
2023-03-12 13:54:58 -04:00
|
|
|
f"stop={eos_token_ids[-1]}",
|
2023-02-23 10:05:25 -05:00
|
|
|
]
|
|
|
|
if shared.args.deepspeed:
|
|
|
|
generate_params.append("synced_gpus=True")
|
|
|
|
if shared.soft_prompt:
|
|
|
|
inputs_embeds, filler_input_ids = generate_softprompt_input_tensors(input_ids)
|
|
|
|
generate_params.insert(0, "inputs_embeds=inputs_embeds")
|
2023-03-08 00:46:35 -05:00
|
|
|
generate_params.insert(0, "inputs=filler_input_ids")
|
2023-02-23 10:05:25 -05:00
|
|
|
else:
|
2023-03-08 00:46:35 -05:00
|
|
|
generate_params.insert(0, "inputs=input_ids")
|
2023-03-05 08:12:43 -05:00
|
|
|
|
2023-03-12 00:31:45 -05:00
|
|
|
try:
|
|
|
|
# Generate the entire reply at once.
|
|
|
|
if shared.args.no_stream:
|
2023-02-23 10:05:25 -05:00
|
|
|
with torch.no_grad():
|
|
|
|
output = eval(f"shared.model.generate({', '.join(generate_params)}){cuda}")[0]
|
|
|
|
if shared.soft_prompt:
|
|
|
|
output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:]))
|
|
|
|
|
|
|
|
reply = decode(output)
|
|
|
|
if not (shared.args.chat or shared.args.cai_chat):
|
|
|
|
reply = original_question + apply_extensions(reply[len(question):], "output")
|
2023-03-08 00:46:35 -05:00
|
|
|
|
2023-03-12 00:31:45 -05:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
2023-03-08 00:46:35 -05:00
|
|
|
|
2023-03-12 00:31:45 -05:00
|
|
|
# Stream the reply 1 token at a time.
|
|
|
|
# This is based on the trick of using 'stopping_criteria' to create an iterator.
|
|
|
|
elif not shared.args.flexgen:
|
|
|
|
|
|
|
|
def generate_with_callback(callback=None, **kwargs):
|
|
|
|
kwargs['stopping_criteria'].append(Stream(callback_func=callback))
|
|
|
|
clear_torch_cache()
|
|
|
|
with torch.no_grad():
|
|
|
|
shared.model.generate(**kwargs)
|
|
|
|
|
|
|
|
def generate_with_streaming(**kwargs):
|
|
|
|
return Iteratorize(generate_with_callback, kwargs, callback=None)
|
|
|
|
|
|
|
|
yield formatted_outputs(original_question, shared.model_name)
|
|
|
|
with eval(f"generate_with_streaming({', '.join(generate_params)})") as generator:
|
|
|
|
for output in generator:
|
|
|
|
if shared.soft_prompt:
|
|
|
|
output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:]))
|
|
|
|
reply = decode(output)
|
|
|
|
|
|
|
|
if not (shared.args.chat or shared.args.cai_chat):
|
|
|
|
reply = original_question + apply_extensions(reply[len(question):], "output")
|
|
|
|
|
2023-03-12 13:54:58 -04:00
|
|
|
if output[-1] in eos_token_ids:
|
2023-03-12 00:31:45 -05:00
|
|
|
break
|
2023-03-12 03:56:35 -04:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
|
|
|
|
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
2023-03-12 00:31:45 -05:00
|
|
|
|
|
|
|
# Stream the output naively for FlexGen since it doesn't support 'stopping_criteria'
|
|
|
|
else:
|
|
|
|
for i in range(max_new_tokens//8+1):
|
|
|
|
clear_torch_cache()
|
|
|
|
with torch.no_grad():
|
|
|
|
output = eval(f"shared.model.generate({', '.join(generate_params)})")[0]
|
2023-03-12 00:04:28 -05:00
|
|
|
if shared.soft_prompt:
|
|
|
|
output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:]))
|
|
|
|
reply = decode(output)
|
|
|
|
|
|
|
|
if not (shared.args.chat or shared.args.cai_chat):
|
|
|
|
reply = original_question + apply_extensions(reply[len(question):], "output")
|
2023-03-08 00:46:35 -05:00
|
|
|
|
2023-03-12 13:54:58 -04:00
|
|
|
if np.count_nonzero(np.isin(input_ids[0], eos_token_ids)) < np.count_nonzero(np.isin(output, eos_token_ids)):
|
2023-02-25 22:36:04 -05:00
|
|
|
break
|
2023-03-12 03:56:35 -04:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
2023-03-08 00:46:35 -05:00
|
|
|
|
2023-02-23 10:05:25 -05:00
|
|
|
input_ids = np.reshape(output, (1, output.shape[0]))
|
2023-03-12 00:31:45 -05:00
|
|
|
if shared.soft_prompt:
|
|
|
|
inputs_embeds, filler_input_ids = generate_softprompt_input_tensors(input_ids)
|
2023-03-08 00:46:35 -05:00
|
|
|
|
2023-03-08 06:02:17 -05:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
2023-02-25 22:36:04 -05:00
|
|
|
|
2023-03-12 00:31:45 -05:00
|
|
|
finally:
|
|
|
|
t1 = time.time()
|
|
|
|
print(f"Output generated in {(t1-t0):.2f} seconds ({(len(output)-len(original_input_ids[0]))/(t1-t0):.2f} tokens/s, {len(output)-len(original_input_ids[0])} tokens)")
|
|
|
|
return
|