2023-04-16 06:36:50 +02:00
|
|
|
import ast
|
2023-04-10 15:29:10 +02:00
|
|
|
import random
|
2023-02-23 17:28:30 +01:00
|
|
|
import re
|
2023-02-23 16:05:25 +01:00
|
|
|
import time
|
2023-03-20 17:36:52 +01:00
|
|
|
import traceback
|
2023-02-23 16:05:25 +01:00
|
|
|
|
2023-02-23 17:28:30 +01:00
|
|
|
import numpy as np
|
2023-02-23 16:05:25 +01:00
|
|
|
import torch
|
|
|
|
import transformers
|
2023-02-23 18:41:42 +01:00
|
|
|
|
|
|
|
import modules.shared as shared
|
2023-03-08 06:46:35 +01:00
|
|
|
from modules.callbacks import (Iteratorize, Stream,
|
|
|
|
_SentinelTokenStoppingCriteria)
|
2023-02-23 16:05:25 +01:00
|
|
|
from modules.extensions import apply_extensions
|
2023-02-23 18:41:42 +01:00
|
|
|
from modules.html_generator import generate_4chan_html, generate_basic_html
|
2023-04-08 02:36:04 +02:00
|
|
|
from modules.models import clear_torch_cache, local_rank
|
2023-02-23 18:41:42 +01:00
|
|
|
|
2023-02-23 16:05:25 +01:00
|
|
|
|
2023-04-11 23:46:06 +02:00
|
|
|
def get_max_prompt_length(state):
|
|
|
|
max_length = state['truncation_length'] - state['max_new_tokens']
|
2023-02-23 16:05:25 +01:00
|
|
|
if shared.soft_prompt:
|
|
|
|
max_length -= shared.soft_prompt_tensor.shape[1]
|
|
|
|
return max_length
|
|
|
|
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-04-11 23:46:06 +02:00
|
|
|
def encode(prompt, add_special_tokens=True, add_bos_token=True, truncation_length=None):
|
2023-04-22 19:56:48 +02:00
|
|
|
if shared.model_type in ['rwkv', 'llamacpp']:
|
2023-03-06 12:45:49 +01:00
|
|
|
input_ids = shared.tokenizer.encode(str(prompt))
|
|
|
|
input_ids = np.array(input_ids).reshape(1, len(input_ids))
|
2023-02-23 16:05:25 +01:00
|
|
|
return input_ids
|
|
|
|
else:
|
2023-04-11 23:46:06 +02:00
|
|
|
input_ids = shared.tokenizer.encode(str(prompt), return_tensors='pt', add_special_tokens=add_special_tokens)
|
2023-04-06 21:04:03 +02:00
|
|
|
|
2023-04-10 21:44:22 +02:00
|
|
|
# This is a hack for making replies more creative.
|
|
|
|
if not add_bos_token and input_ids[0][0] == shared.tokenizer.bos_token_id:
|
|
|
|
input_ids = input_ids[:, 1:]
|
|
|
|
|
|
|
|
# Llama adds this extra token when the first character is '\n', and this
|
|
|
|
# compromises the stopping criteria, so we just remove it
|
2023-04-06 21:04:03 +02:00
|
|
|
if type(shared.tokenizer) is transformers.LlamaTokenizer and input_ids[0][0] == 29871:
|
2023-04-07 05:15:45 +02:00
|
|
|
input_ids = input_ids[:, 1:]
|
2023-04-06 21:04:03 +02:00
|
|
|
|
2023-04-11 23:46:06 +02:00
|
|
|
# Handling truncation
|
|
|
|
if truncation_length is not None:
|
|
|
|
input_ids = input_ids[:, -truncation_length:]
|
|
|
|
|
2023-04-22 19:56:48 +02:00
|
|
|
if shared.model_type in ['rwkv', 'llamacpp'] or shared.args.cpu:
|
2023-04-11 23:46:06 +02:00
|
|
|
return input_ids
|
|
|
|
elif shared.args.flexgen:
|
|
|
|
return input_ids.numpy()
|
|
|
|
elif shared.args.deepspeed:
|
|
|
|
return input_ids.to(device=local_rank)
|
|
|
|
elif torch.has_mps:
|
|
|
|
device = torch.device('mps')
|
|
|
|
return input_ids.to(device)
|
|
|
|
else:
|
|
|
|
return input_ids.cuda()
|
2023-02-23 16:05:25 +01:00
|
|
|
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-04-16 19:24:49 +02:00
|
|
|
def decode(output_ids, skip_special_tokens=True):
|
|
|
|
if skip_special_tokens:
|
2023-03-13 03:58:25 +01:00
|
|
|
reply = shared.tokenizer.decode(output_ids, skip_special_tokens=True)
|
|
|
|
reply = reply.replace(r'<|endoftext|>', '')
|
|
|
|
return reply
|
2023-04-16 19:24:49 +02:00
|
|
|
else:
|
|
|
|
return shared.tokenizer.decode(output_ids, skip_special_tokens=False)
|
2023-02-23 16:05:25 +01:00
|
|
|
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-02-23 16:05:25 +01: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-04-07 05:15:45 +02:00
|
|
|
# filler_input_ids += shared.model.config.bos_token_id # setting dummy input_ids to bos tokens
|
2023-02-23 16:05:25 +01:00
|
|
|
return inputs_embeds, filler_input_ids
|
|
|
|
|
2023-04-11 16:46:30 +02:00
|
|
|
|
2023-02-23 16:05:25 +01:00
|
|
|
# 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
|
|
|
|
|
2023-04-11 16:46:30 +02:00
|
|
|
|
2023-02-23 16:05:25 +01:00
|
|
|
# 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
|
|
|
|
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-02-23 16:05:25 +01:00
|
|
|
def formatted_outputs(reply, model_name):
|
2023-04-02 01:14:43 +02:00
|
|
|
if not shared.is_chat():
|
2023-04-22 19:56:48 +02:00
|
|
|
if shared.model_type == 'galactica':
|
2023-02-23 16:05:25 +01:00
|
|
|
reply = fix_galactica(reply)
|
|
|
|
return reply, reply, generate_basic_html(reply)
|
2023-04-22 19:56:48 +02:00
|
|
|
elif shared.model_type == 'gpt4chan':
|
2023-02-23 16:05:25 +01: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-04-07 05:15:45 +02:00
|
|
|
|
2023-03-22 19:40:20 +01:00
|
|
|
def set_manual_seed(seed):
|
2023-04-10 15:53:31 +02:00
|
|
|
seed = int(seed)
|
2023-04-10 15:29:10 +02:00
|
|
|
if seed == -1:
|
|
|
|
seed = random.randint(1, 2**31)
|
2023-04-25 00:24:12 +02:00
|
|
|
|
2023-04-10 15:29:10 +02:00
|
|
|
torch.manual_seed(seed)
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
torch.cuda.manual_seed_all(seed)
|
2023-04-25 00:24:12 +02:00
|
|
|
|
2023-04-10 15:53:31 +02:00
|
|
|
return seed
|
2023-03-22 19:40:20 +01:00
|
|
|
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-03-27 18:23:59 +02:00
|
|
|
def stop_everything_event():
|
|
|
|
shared.stop_everything = True
|
|
|
|
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-04-25 00:24:12 +02:00
|
|
|
def get_generate_params(state):
|
|
|
|
generate_params = {}
|
|
|
|
|
|
|
|
# Models that are not on transformers
|
|
|
|
if shared.model_type in ['rwkv', 'llamacpp']:
|
|
|
|
generate_params['token_count'] = state['max_new_tokens']
|
|
|
|
for k in ['temperature', 'top_p', 'top_k', 'repetition_penalty']:
|
|
|
|
generate_params[k] = state[k]
|
|
|
|
else:
|
|
|
|
# FlexGen
|
|
|
|
if shared.args.flexgen:
|
|
|
|
for k in ['max_new_tokens', 'do_sample', 'temperature']:
|
|
|
|
generate_params[k] = state[k]
|
|
|
|
|
|
|
|
if not shared.args.no_stream:
|
|
|
|
generate_params['max_new_tokens'] = 8
|
|
|
|
|
|
|
|
# transformers
|
|
|
|
else:
|
|
|
|
for k in ['max_new_tokens', 'do_sample', 'temperature', 'top_p', 'typical_p', 'repetition_penalty', 'encoder_repetition_penalty', 'top_k', 'min_length', 'no_repeat_ngram_size', 'num_beams', 'penalty_alpha', 'length_penalty', 'early_stopping']:
|
|
|
|
generate_params[k] = state[k]
|
|
|
|
|
|
|
|
if state['ban_eos_token']:
|
|
|
|
generate_params['suppress_tokens'] = [shared.tokenizer.eos_token_id]
|
|
|
|
|
|
|
|
if shared.args.no_cache:
|
|
|
|
generate_params.update({'use_cache': False})
|
|
|
|
|
|
|
|
if shared.args.deepspeed:
|
|
|
|
generate_params.update({'synced_gpus': True})
|
|
|
|
|
|
|
|
return generate_params
|
|
|
|
|
2023-04-13 15:35:08 +02:00
|
|
|
|
2023-04-25 00:24:12 +02:00
|
|
|
def generate_reply(question, state, eos_token=None, stopping_strings=[]):
|
2023-04-16 22:35:54 +02:00
|
|
|
if shared.model_name == 'None' or shared.model is None:
|
2023-04-13 15:35:08 +02:00
|
|
|
print("No model is loaded! Select one in the Model tab.")
|
|
|
|
yield formatted_outputs(question, shared.model_name)
|
|
|
|
return
|
|
|
|
|
2023-03-05 14:12:43 +01:00
|
|
|
clear_torch_cache()
|
2023-04-11 16:46:30 +02:00
|
|
|
seed = set_manual_seed(state['seed'])
|
2023-03-27 18:23:59 +02:00
|
|
|
shared.stop_everything = False
|
2023-04-25 00:24:12 +02:00
|
|
|
generate_params = get_generate_params(state)
|
2023-03-04 01:24:32 +01:00
|
|
|
t0 = time.time()
|
|
|
|
|
2023-04-25 00:24:12 +02:00
|
|
|
# Preparing the input
|
2023-03-24 01:38:20 +01:00
|
|
|
original_question = question
|
2023-04-02 01:14:43 +02:00
|
|
|
if not shared.is_chat():
|
2023-04-24 01:32:22 +02:00
|
|
|
question = apply_extensions('input', question)
|
2023-03-24 01:38:20 +01:00
|
|
|
|
2023-04-25 00:24:12 +02:00
|
|
|
# If the model is not on transformers, handle it separately and end this
|
|
|
|
# function call earlier.
|
2023-04-22 19:56:48 +02:00
|
|
|
if shared.model_type in ['rwkv', 'llamacpp']:
|
2023-04-11 23:46:06 +02:00
|
|
|
if shared.args.verbose:
|
|
|
|
print(f'\n\n{question}\n--------------------\n')
|
|
|
|
|
2023-03-12 06:53:08 +01:00
|
|
|
try:
|
|
|
|
if shared.args.no_stream:
|
2023-04-06 06:22:15 +02:00
|
|
|
reply = shared.model.generate(context=question, **generate_params)
|
2023-04-07 05:15:45 +02:00
|
|
|
output = original_question + reply
|
2023-04-02 01:14:43 +02:00
|
|
|
if not shared.is_chat():
|
2023-04-24 01:32:22 +02:00
|
|
|
reply = original_question + apply_extensions('output', reply)
|
2023-04-25 00:24:12 +02:00
|
|
|
|
2023-03-01 23:11:26 +01:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
2023-03-12 06:53:08 +01:00
|
|
|
else:
|
2023-04-02 01:14:43 +02:00
|
|
|
if not shared.is_chat():
|
2023-03-15 16:51:13 +01:00
|
|
|
yield formatted_outputs(question, shared.model_name)
|
2023-03-24 01:38:20 +01:00
|
|
|
|
2023-04-06 06:22:15 +02:00
|
|
|
for reply in shared.model.generate_with_streaming(context=question, **generate_params):
|
2023-04-07 05:15:45 +02:00
|
|
|
output = original_question + reply
|
2023-04-02 01:14:43 +02:00
|
|
|
if not shared.is_chat():
|
2023-04-24 01:32:22 +02:00
|
|
|
reply = original_question + apply_extensions('output', reply)
|
2023-04-25 00:24:12 +02:00
|
|
|
|
2023-03-12 06:53:08 +01:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
2023-03-24 01:38:20 +01:00
|
|
|
|
2023-03-21 00:36:02 +01:00
|
|
|
except Exception:
|
2023-03-20 17:36:52 +01:00
|
|
|
traceback.print_exc()
|
2023-03-12 06:53:08 +01:00
|
|
|
finally:
|
|
|
|
t1 = time.time()
|
2023-03-31 22:00:55 +02:00
|
|
|
original_tokens = len(encode(original_question)[0])
|
|
|
|
new_tokens = len(encode(output)[0]) - original_tokens
|
2023-04-10 15:53:31 +02:00
|
|
|
print(f'Output generated in {(t1-t0):.2f} seconds ({new_tokens/(t1-t0):.2f} tokens/s, {new_tokens} tokens, context {original_tokens}, seed {seed})')
|
2023-03-12 06:53:08 +01:00
|
|
|
return
|
2023-02-28 03:03:35 +01:00
|
|
|
|
2023-04-25 00:24:12 +02:00
|
|
|
# Encode the input
|
2023-04-11 23:46:06 +02:00
|
|
|
input_ids = encode(question, add_bos_token=state['add_bos_token'], truncation_length=get_max_prompt_length(state))
|
2023-03-08 15:26:29 +01:00
|
|
|
output = input_ids[0]
|
2023-04-25 00:24:12 +02:00
|
|
|
cuda = not any((shared.args.cpu, shared.args.deepspeed, shared.args.flexgen))
|
2023-04-11 23:46:06 +02:00
|
|
|
if shared.args.verbose:
|
2023-04-16 19:24:49 +02:00
|
|
|
print(f'\n\n{decode(input_ids[0], state["skip_special_tokens"])}\n--------------------\n')
|
2023-04-11 23:46:06 +02:00
|
|
|
|
2023-04-25 00:24:12 +02:00
|
|
|
# Find the eos tokens
|
2023-03-13 14:32:28 +01:00
|
|
|
eos_token_ids = [shared.tokenizer.eos_token_id] if shared.tokenizer.eos_token_id is not None else []
|
2023-03-12 18:54:58 +01:00
|
|
|
if eos_token is not None:
|
|
|
|
eos_token_ids.append(int(encode(eos_token)[0][-1]))
|
2023-04-11 17:30:06 +02:00
|
|
|
|
2023-04-25 00:24:12 +02:00
|
|
|
# Create the StoppingCriteriaList with the stopping strings
|
2023-03-08 16:13:40 +01:00
|
|
|
stopping_criteria_list = transformers.StoppingCriteriaList()
|
2023-04-16 06:40:47 +02:00
|
|
|
for st in (stopping_strings, ast.literal_eval(f"[{state['custom_stopping_strings']}]")):
|
2023-04-11 17:30:06 +02:00
|
|
|
if type(st) is list and len(st) > 0:
|
2023-04-11 23:46:06 +02:00
|
|
|
sentinel_token_ids = [encode(string, add_special_tokens=False) for string in st]
|
2023-04-11 17:30:06 +02:00
|
|
|
stopping_criteria_list.append(_SentinelTokenStoppingCriteria(sentinel_token_ids=sentinel_token_ids, starting_idx=len(input_ids[0])))
|
|
|
|
break
|
2023-02-23 16:05:25 +01:00
|
|
|
|
2023-04-25 00:24:12 +02:00
|
|
|
# Update generate_params with the eos token and the stopping strings
|
|
|
|
if shared.args.flexgen:
|
|
|
|
generate_params['stop'] = eos_token_ids[-1]
|
|
|
|
else:
|
2023-04-07 16:14:32 +02:00
|
|
|
generate_params['eos_token_id'] = eos_token_ids
|
|
|
|
generate_params['stopping_criteria'] = stopping_criteria_list
|
2023-04-06 06:22:15 +02:00
|
|
|
|
2023-04-25 00:24:12 +02:00
|
|
|
# Add the encoded tokens to generate_params
|
2023-02-23 16:05:25 +01:00
|
|
|
if shared.soft_prompt:
|
|
|
|
inputs_embeds, filler_input_ids = generate_softprompt_input_tensors(input_ids)
|
2023-04-24 01:32:22 +02:00
|
|
|
question, filler_input_ids, inputs_embeds = apply_extensions('tokenizer', state, question, filler_input_ids, inputs_embeds)
|
|
|
|
original_input_ids = input_ids
|
2023-04-07 16:14:32 +02:00
|
|
|
generate_params.update({'inputs_embeds': inputs_embeds})
|
|
|
|
generate_params.update({'inputs': filler_input_ids})
|
2023-02-23 16:05:25 +01:00
|
|
|
else:
|
2023-04-24 01:32:22 +02:00
|
|
|
question, input_ids, inputs_embeds = apply_extensions('tokenizer', state, question, input_ids, None)
|
|
|
|
original_input_ids = input_ids
|
2023-04-07 16:14:32 +02:00
|
|
|
generate_params.update({'inputs': input_ids})
|
2023-04-24 01:32:22 +02:00
|
|
|
if inputs_embeds is not None:
|
|
|
|
generate_params.update({'inputs_embeds': inputs_embeds})
|
2023-03-05 14:12:43 +01:00
|
|
|
|
2023-03-12 06:31:45 +01:00
|
|
|
try:
|
|
|
|
# Generate the entire reply at once.
|
|
|
|
if shared.args.no_stream:
|
2023-02-23 16:05:25 +01:00
|
|
|
with torch.no_grad():
|
2023-03-14 20:04:17 +01:00
|
|
|
output = shared.model.generate(**generate_params)[0]
|
|
|
|
if cuda:
|
|
|
|
output = output.cuda()
|
2023-04-16 19:24:49 +02:00
|
|
|
|
2023-02-23 16:05:25 +01:00
|
|
|
if shared.soft_prompt:
|
|
|
|
output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:]))
|
|
|
|
|
2023-03-24 01:38:20 +01:00
|
|
|
new_tokens = len(output) - len(input_ids[0])
|
2023-04-16 19:24:49 +02:00
|
|
|
reply = decode(output[-new_tokens:], state['skip_special_tokens'])
|
2023-04-02 01:14:43 +02:00
|
|
|
if not shared.is_chat():
|
2023-04-24 01:32:22 +02:00
|
|
|
reply = original_question + apply_extensions('output', reply)
|
2023-03-08 06:46:35 +01:00
|
|
|
|
2023-03-12 06:31:45 +01:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
2023-03-08 06:46:35 +01:00
|
|
|
|
2023-03-12 06:31:45 +01: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)
|
|
|
|
|
2023-04-02 01:14:43 +02:00
|
|
|
if not shared.is_chat():
|
2023-03-15 16:51:13 +01:00
|
|
|
yield formatted_outputs(original_question, shared.model_name)
|
2023-04-16 19:24:49 +02:00
|
|
|
|
2023-03-14 20:04:17 +01:00
|
|
|
with generate_with_streaming(**generate_params) as generator:
|
2023-03-12 06:31:45 +01:00
|
|
|
for output in generator:
|
|
|
|
if shared.soft_prompt:
|
|
|
|
output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:]))
|
2023-03-24 01:38:20 +01:00
|
|
|
|
|
|
|
new_tokens = len(output) - len(input_ids[0])
|
2023-04-16 19:24:49 +02:00
|
|
|
reply = decode(output[-new_tokens:], state['skip_special_tokens'])
|
2023-04-02 01:14:43 +02:00
|
|
|
if not shared.is_chat():
|
2023-04-24 01:32:22 +02:00
|
|
|
reply = original_question + apply_extensions('output', reply)
|
2023-03-12 06:31:45 +01:00
|
|
|
|
2023-03-12 18:54:58 +01:00
|
|
|
if output[-1] in eos_token_ids:
|
2023-03-12 06:31:45 +01:00
|
|
|
break
|
2023-04-16 19:24:49 +02:00
|
|
|
|
2023-03-12 08:56:35 +01:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
|
|
|
|
2023-03-12 06:31:45 +01:00
|
|
|
# Stream the output naively for FlexGen since it doesn't support 'stopping_criteria'
|
|
|
|
else:
|
2023-04-11 16:46:30 +02:00
|
|
|
for i in range(state['max_new_tokens'] // 8 + 1):
|
2023-03-12 06:31:45 +01:00
|
|
|
clear_torch_cache()
|
|
|
|
with torch.no_grad():
|
2023-03-14 20:04:17 +01:00
|
|
|
output = shared.model.generate(**generate_params)[0]
|
2023-04-16 19:24:49 +02:00
|
|
|
|
2023-03-12 06:04:28 +01:00
|
|
|
if shared.soft_prompt:
|
|
|
|
output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:]))
|
2023-03-24 01:38:20 +01:00
|
|
|
|
|
|
|
new_tokens = len(output) - len(original_input_ids[0])
|
2023-04-16 19:24:49 +02:00
|
|
|
reply = decode(output[-new_tokens:], state['skip_special_tokens'])
|
2023-04-02 01:14:43 +02:00
|
|
|
if not shared.is_chat():
|
2023-04-24 01:32:22 +02:00
|
|
|
reply = original_question + apply_extensions('output', reply)
|
2023-03-08 06:46:35 +01:00
|
|
|
|
2023-03-12 18:54:58 +01:00
|
|
|
if np.count_nonzero(np.isin(input_ids[0], eos_token_ids)) < np.count_nonzero(np.isin(output, eos_token_ids)):
|
2023-02-26 04:36:04 +01:00
|
|
|
break
|
2023-03-08 06:46:35 +01:00
|
|
|
|
2023-04-16 19:24:49 +02:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
2023-02-23 16:05:25 +01:00
|
|
|
input_ids = np.reshape(output, (1, output.shape[0]))
|
2023-03-12 06:31:45 +01:00
|
|
|
if shared.soft_prompt:
|
|
|
|
inputs_embeds, filler_input_ids = generate_softprompt_input_tensors(input_ids)
|
2023-04-07 16:14:32 +02:00
|
|
|
generate_params.update({'inputs_embeds': inputs_embeds})
|
|
|
|
generate_params.update({'inputs': filler_input_ids})
|
2023-03-23 04:22:14 +01:00
|
|
|
else:
|
2023-04-07 16:14:32 +02:00
|
|
|
generate_params.update({'inputs': input_ids})
|
2023-03-08 06:46:35 +01:00
|
|
|
|
2023-03-08 12:02:17 +01:00
|
|
|
yield formatted_outputs(reply, shared.model_name)
|
2023-02-26 04:36:04 +01:00
|
|
|
|
2023-03-21 00:36:02 +01:00
|
|
|
except Exception:
|
2023-03-20 17:36:52 +01:00
|
|
|
traceback.print_exc()
|
2023-03-12 06:31:45 +01:00
|
|
|
finally:
|
|
|
|
t1 = time.time()
|
2023-03-31 22:00:55 +02:00
|
|
|
original_tokens = len(original_input_ids[0])
|
2023-04-07 05:15:45 +02:00
|
|
|
new_tokens = len(output) - original_tokens
|
2023-04-10 15:53:31 +02:00
|
|
|
print(f'Output generated in {(t1-t0):.2f} seconds ({new_tokens/(t1-t0):.2f} tokens/s, {new_tokens} tokens, context {original_tokens}, seed {seed})')
|
2023-03-12 06:31:45 +01:00
|
|
|
return
|