2023-06-17 01:35:38 +02:00
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
|
|
|
|
2023-06-29 20:03:16 +02:00
|
|
|
from torch import version as torch_version
|
|
|
|
|
2023-06-17 01:49:36 +02:00
|
|
|
from modules import shared
|
2023-06-17 01:35:38 +02:00
|
|
|
from modules.logging_colors import logger
|
2023-06-17 01:49:36 +02:00
|
|
|
|
2023-06-25 01:24:17 +02:00
|
|
|
try:
|
|
|
|
from exllama.generator import ExLlamaGenerator
|
|
|
|
from exllama.model import ExLlama, ExLlamaCache, ExLlamaConfig
|
|
|
|
from exllama.tokenizer import ExLlamaTokenizer
|
|
|
|
except:
|
|
|
|
logger.warning('Exllama module failed to load. Will attempt to load from repositories.')
|
|
|
|
try:
|
|
|
|
from modules.relative_imports import RelativeImport
|
|
|
|
|
|
|
|
with RelativeImport("repositories/exllama"):
|
|
|
|
from generator import ExLlamaGenerator
|
|
|
|
from model import ExLlama, ExLlamaCache, ExLlamaConfig
|
|
|
|
from tokenizer import ExLlamaTokenizer
|
|
|
|
except:
|
|
|
|
logger.error("Could not find repositories/exllama/. Make sure that exllama is cloned inside repositories/ and is up to date.")
|
|
|
|
raise
|
2023-06-17 01:35:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ExllamaModel:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_pretrained(self, path_to_model):
|
|
|
|
|
2023-06-18 18:26:30 +02:00
|
|
|
path_to_model = Path(f'{shared.args.model_dir}') / Path(path_to_model)
|
2023-06-17 01:35:38 +02:00
|
|
|
tokenizer_model_path = path_to_model / "tokenizer.model"
|
|
|
|
model_config_path = path_to_model / "config.json"
|
|
|
|
|
|
|
|
# Find the model checkpoint
|
|
|
|
model_path = None
|
|
|
|
for ext in ['.safetensors', '.pt', '.bin']:
|
|
|
|
found = list(path_to_model.glob(f"*{ext}"))
|
|
|
|
if len(found) > 0:
|
|
|
|
if len(found) > 1:
|
|
|
|
logger.warning(f'More than one {ext} model has been found. The last one will be selected. It could be wrong.')
|
|
|
|
|
|
|
|
model_path = found[-1]
|
|
|
|
break
|
|
|
|
|
|
|
|
config = ExLlamaConfig(str(model_config_path))
|
|
|
|
config.model_path = str(model_path)
|
2023-06-26 03:49:26 +02:00
|
|
|
config.max_seq_len = shared.args.max_seq_len
|
|
|
|
config.compress_pos_emb = shared.args.compress_pos_emb
|
2023-06-17 01:49:36 +02:00
|
|
|
if shared.args.gpu_split:
|
|
|
|
config.set_auto_map(shared.args.gpu_split)
|
|
|
|
config.gpu_peer_fix = True
|
2023-06-29 20:03:16 +02:00
|
|
|
if torch_version.hip:
|
|
|
|
config.rmsnorm_no_half2 = True
|
|
|
|
config.rope_no_half2 = True
|
|
|
|
config.matmul_no_half2 = True
|
|
|
|
config.silu_no_half2 = True
|
|
|
|
|
2023-06-17 01:49:36 +02:00
|
|
|
|
2023-06-17 01:35:38 +02:00
|
|
|
model = ExLlama(config)
|
|
|
|
tokenizer = ExLlamaTokenizer(str(tokenizer_model_path))
|
|
|
|
cache = ExLlamaCache(model)
|
2023-06-17 23:00:10 +02:00
|
|
|
generator = ExLlamaGenerator(model, tokenizer, cache)
|
2023-06-17 01:35:38 +02:00
|
|
|
|
|
|
|
result = self()
|
|
|
|
result.config = config
|
|
|
|
result.model = model
|
|
|
|
result.cache = cache
|
|
|
|
result.tokenizer = tokenizer
|
2023-06-19 06:19:28 +02:00
|
|
|
result.generator = generator
|
2023-06-17 01:35:38 +02:00
|
|
|
return result, result
|
|
|
|
|
2023-06-18 00:02:08 +02:00
|
|
|
def generate_with_streaming(self, prompt, state):
|
2023-06-17 23:00:10 +02:00
|
|
|
self.generator.settings.temperature = state['temperature']
|
|
|
|
self.generator.settings.top_p = state['top_p']
|
|
|
|
self.generator.settings.top_k = state['top_k']
|
|
|
|
self.generator.settings.typical = state['typical_p']
|
|
|
|
self.generator.settings.token_repetition_penalty_max = state['repetition_penalty']
|
2023-06-29 18:53:06 +02:00
|
|
|
self.generator.settings.token_repetition_penalty_sustain = -1 if state['repetition_penalty_range'] <= 0 else state['repetition_penalty_range']
|
2023-06-17 01:35:38 +02:00
|
|
|
if state['ban_eos_token']:
|
2023-06-17 23:00:10 +02:00
|
|
|
self.generator.disallow_tokens([self.tokenizer.eos_token_id])
|
|
|
|
else:
|
|
|
|
self.generator.disallow_tokens(None)
|
|
|
|
|
|
|
|
self.generator.end_beam_search()
|
|
|
|
ids = self.generator.tokenizer.encode(prompt)
|
|
|
|
self.generator.gen_begin_reuse(ids)
|
|
|
|
initial_len = self.generator.sequence[0].shape[0]
|
2023-06-18 00:32:04 +02:00
|
|
|
has_leading_space = False
|
|
|
|
for i in range(state['max_new_tokens']):
|
2023-06-17 23:00:10 +02:00
|
|
|
token = self.generator.gen_single_token()
|
2023-06-18 00:32:04 +02:00
|
|
|
if i == 0 and self.generator.tokenizer.tokenizer.IdToPiece(int(token)).startswith('▁'):
|
|
|
|
has_leading_space = True
|
|
|
|
|
|
|
|
decoded_text = self.generator.tokenizer.decode(self.generator.sequence[0][initial_len:])
|
|
|
|
if has_leading_space:
|
|
|
|
decoded_text = ' ' + decoded_text
|
|
|
|
|
|
|
|
yield decoded_text
|
2023-06-17 23:00:10 +02:00
|
|
|
if token.item() == self.generator.tokenizer.eos_token_id or shared.stop_everything:
|
2023-06-17 01:35:38 +02:00
|
|
|
break
|
|
|
|
|
2023-06-18 00:02:08 +02:00
|
|
|
def generate(self, prompt, state):
|
|
|
|
output = ''
|
|
|
|
for output in self.generate_with_streaming(prompt, state):
|
|
|
|
pass
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
2023-06-17 01:35:38 +02:00
|
|
|
def encode(self, string, **kwargs):
|
|
|
|
return self.tokenizer.encode(string)
|