2023-02-27 22:09:11 -05:00
|
|
|
import os
|
2023-02-27 21:50:16 -05:00
|
|
|
from pathlib import Path
|
2023-02-27 22:09:11 -05:00
|
|
|
|
2023-02-27 21:50:16 -05:00
|
|
|
import numpy as np
|
2023-03-06 06:45:49 -05:00
|
|
|
from tokenizers import Tokenizer
|
2023-02-27 22:09:11 -05:00
|
|
|
|
|
|
|
import modules.shared as shared
|
2023-03-08 00:50:49 -05:00
|
|
|
from modules.callbacks import Iteratorize
|
2023-02-27 22:09:11 -05:00
|
|
|
|
2023-02-27 21:50:16 -05:00
|
|
|
np.set_printoptions(precision=4, suppress=True, linewidth=200)
|
|
|
|
|
|
|
|
os.environ['RWKV_JIT_ON'] = '1'
|
2023-04-06 23:15:45 -04:00
|
|
|
os.environ["RWKV_CUDA_ON"] = '1' if shared.args.rwkv_cuda_on else '0' # use CUDA kernel for seq mode (much faster)
|
2023-02-27 21:50:16 -05:00
|
|
|
|
|
|
|
from rwkv.model import RWKV
|
|
|
|
from rwkv.utils import PIPELINE, PIPELINE_ARGS
|
|
|
|
|
2023-03-01 10:18:17 -05:00
|
|
|
|
2023-03-01 10:08:55 -05:00
|
|
|
class RWKVModel:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
2023-02-27 21:50:16 -05:00
|
|
|
|
2023-03-01 10:08:55 -05:00
|
|
|
@classmethod
|
|
|
|
def from_pretrained(self, path, dtype="fp16", device="cuda"):
|
|
|
|
tokenizer_path = Path(f"{path.parent}/20B_tokenizer.json")
|
2023-02-27 21:50:16 -05:00
|
|
|
|
2023-03-01 18:02:48 -05:00
|
|
|
if shared.args.rwkv_strategy is None:
|
2023-03-12 23:08:01 -04:00
|
|
|
model = RWKV(model=str(path), strategy=f'{device} {dtype}')
|
2023-03-01 18:02:48 -05:00
|
|
|
else:
|
2023-03-12 23:08:01 -04:00
|
|
|
model = RWKV(model=str(path), strategy=shared.args.rwkv_strategy)
|
|
|
|
pipeline = PIPELINE(model, str(tokenizer_path))
|
2023-02-27 21:50:16 -05:00
|
|
|
|
2023-03-01 10:08:55 -05:00
|
|
|
result = self()
|
2023-03-01 10:33:09 -05:00
|
|
|
result.pipeline = pipeline
|
2023-03-01 10:08:55 -05:00
|
|
|
return result
|
|
|
|
|
2023-05-08 21:55:41 -04:00
|
|
|
def generate(self, context="", token_count=20, temperature=1, top_p=1, top_k=50, repetition_penalty=None, alpha_frequency=0.1, alpha_presence=0.1, token_ban=None, token_stop=None, callback=None):
|
2023-03-01 10:16:11 -05:00
|
|
|
args = PIPELINE_ARGS(
|
2023-04-06 23:15:45 -04:00
|
|
|
temperature=temperature,
|
|
|
|
top_p=top_p,
|
|
|
|
top_k=top_k,
|
|
|
|
alpha_frequency=alpha_frequency, # Frequency Penalty (as in GPT-3)
|
|
|
|
alpha_presence=alpha_presence, # Presence Penalty (as in GPT-3)
|
2023-05-08 21:55:41 -04:00
|
|
|
token_ban=token_ban or [0], # ban the generation of some tokens
|
|
|
|
token_stop=token_stop or []
|
2023-03-01 10:16:11 -05:00
|
|
|
)
|
|
|
|
|
2023-03-23 20:38:20 -04:00
|
|
|
return self.pipeline.generate(context, token_count=token_count, args=args, callback=callback)
|
2023-03-06 06:45:49 -05:00
|
|
|
|
2023-03-07 16:17:56 -05:00
|
|
|
def generate_with_streaming(self, **kwargs):
|
2023-03-12 00:04:28 -05:00
|
|
|
with Iteratorize(self.generate, kwargs, callback=None) as generator:
|
2023-03-23 20:38:20 -04:00
|
|
|
reply = ''
|
2023-03-12 00:04:28 -05:00
|
|
|
for token in generator:
|
|
|
|
reply += token
|
|
|
|
yield reply
|
2023-03-07 16:17:56 -05:00
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-03-06 06:45:49 -05:00
|
|
|
class RWKVTokenizer:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_pretrained(self, path):
|
|
|
|
tokenizer_path = path / "20B_tokenizer.json"
|
2023-03-12 23:08:01 -04:00
|
|
|
tokenizer = Tokenizer.from_file(str(tokenizer_path))
|
2023-03-06 06:45:49 -05:00
|
|
|
|
|
|
|
result = self()
|
|
|
|
result.tokenizer = tokenizer
|
|
|
|
return result
|
|
|
|
|
|
|
|
def encode(self, prompt):
|
|
|
|
return self.tokenizer.encode(prompt).ids
|
|
|
|
|
|
|
|
def decode(self, ids):
|
|
|
|
return self.tokenizer.decode(ids)
|