2023-02-28 04:09:11 +01:00
|
|
|
import os
|
2023-02-28 03:50:16 +01:00
|
|
|
from pathlib import Path
|
2023-02-28 04:09:11 +01:00
|
|
|
|
2023-02-28 03:50:16 +01:00
|
|
|
import numpy as np
|
2023-03-06 12:45:49 +01:00
|
|
|
from tokenizers import Tokenizer
|
2023-02-28 04:09:11 +01:00
|
|
|
|
|
|
|
import modules.shared as shared
|
|
|
|
|
2023-02-28 03:50:16 +01:00
|
|
|
np.set_printoptions(precision=4, suppress=True, linewidth=200)
|
|
|
|
|
|
|
|
os.environ['RWKV_JIT_ON'] = '1'
|
|
|
|
os.environ["RWKV_CUDA_ON"] = '0' # '1' : use CUDA kernel for seq mode (much faster)
|
|
|
|
|
|
|
|
from rwkv.model import RWKV
|
|
|
|
from rwkv.utils import PIPELINE, PIPELINE_ARGS
|
|
|
|
|
2023-03-01 16:18:17 +01:00
|
|
|
|
2023-03-01 16:08:55 +01:00
|
|
|
class RWKVModel:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
2023-02-28 03:50:16 +01:00
|
|
|
|
2023-03-01 16:08:55 +01:00
|
|
|
@classmethod
|
|
|
|
def from_pretrained(self, path, dtype="fp16", device="cuda"):
|
|
|
|
tokenizer_path = Path(f"{path.parent}/20B_tokenizer.json")
|
2023-02-28 03:50:16 +01:00
|
|
|
|
2023-03-02 00:02:48 +01:00
|
|
|
if shared.args.rwkv_strategy is None:
|
|
|
|
model = RWKV(model=os.path.abspath(path), strategy=f'{device} {dtype}')
|
|
|
|
else:
|
|
|
|
model = RWKV(model=os.path.abspath(path), strategy=shared.args.rwkv_strategy)
|
2023-03-01 23:17:16 +01:00
|
|
|
pipeline = PIPELINE(model, os.path.abspath(tokenizer_path))
|
2023-02-28 03:50:16 +01:00
|
|
|
|
2023-03-01 16:08:55 +01:00
|
|
|
result = self()
|
2023-03-01 16:33:09 +01:00
|
|
|
result.pipeline = pipeline
|
2023-03-01 16:08:55 +01:00
|
|
|
return result
|
|
|
|
|
2023-03-06 18:25:48 +01:00
|
|
|
def generate(self, context, token_count=20, temperature=1, top_p=1, alpha_frequency=0.7, alpha_presence=0.7, token_ban=[0], token_stop=[], callback=None):
|
2023-03-01 16:16:11 +01:00
|
|
|
args = PIPELINE_ARGS(
|
|
|
|
temperature = temperature,
|
|
|
|
top_p = top_p,
|
2023-03-01 16:19:37 +01:00
|
|
|
alpha_frequency = alpha_frequency, # Frequency Penalty (as in GPT-3)
|
|
|
|
alpha_presence = alpha_presence, # Presence Penalty (as in GPT-3)
|
|
|
|
token_ban = token_ban, # ban the generation of some tokens
|
|
|
|
token_stop = token_stop
|
2023-03-01 16:16:11 +01:00
|
|
|
)
|
|
|
|
|
2023-03-01 20:40:25 +01:00
|
|
|
return context+self.pipeline.generate(context, token_count=token_count, args=args, callback=callback)
|
2023-03-06 12:45:49 +01:00
|
|
|
|
|
|
|
class RWKVTokenizer:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_pretrained(self, path):
|
|
|
|
tokenizer_path = path / "20B_tokenizer.json"
|
|
|
|
tokenizer = Tokenizer.from_file(os.path.abspath(tokenizer_path))
|
|
|
|
|
|
|
|
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)
|