RWKV.py 904 B

1234567891011121314151617181920212223242526272829303132333435
  1. import os
  2. import time
  3. import types
  4. from pathlib import Path
  5. import numpy as np
  6. import torch
  7. import modules.shared as shared
  8. np.set_printoptions(precision=4, suppress=True, linewidth=200)
  9. os.environ['RWKV_JIT_ON'] = '1'
  10. os.environ["RWKV_CUDA_ON"] = '0' # '1' : use CUDA kernel for seq mode (much faster)
  11. from rwkv.model import RWKV
  12. from rwkv.utils import PIPELINE, PIPELINE_ARGS
  13. class RWKVModel:
  14. def __init__(self):
  15. pass
  16. @classmethod
  17. def from_pretrained(self, path, dtype="fp16", device="cuda"):
  18. tokenizer_path = Path(f"{path.parent}/20B_tokenizer.json")
  19. model = RWKV(model=path.as_posix(), strategy=f'{device} {dtype}')
  20. pipeline = PIPELINE(model, tokenizer_path.as_posix())
  21. result = self()
  22. result.model = pipeline
  23. return result
  24. def generate(self, context, **kwargs):
  25. return self.model.generate(context, **kwargs)