RWKV.py 1.0 KB

1234567891011121314151617181920212223242526
  1. import os, time, types, torch
  2. from pathlib import Path
  3. import numpy as np
  4. np.set_printoptions(precision=4, suppress=True, linewidth=200)
  5. os.environ['RWKV_JIT_ON'] = '1'
  6. os.environ["RWKV_CUDA_ON"] = '0' # '1' : use CUDA kernel for seq mode (much faster)
  7. import repositories.ChatRWKV.v2.rwkv as rwkv
  8. from rwkv.model import RWKV
  9. from rwkv.utils import PIPELINE, PIPELINE_ARGS
  10. def load_RWKV_model(path):
  11. os.system("ls")
  12. model = RWKV(model=path.as_posix(), strategy='cuda fp16')
  13. out, state = model.forward([187, 510, 1563, 310, 247], None) # use 20B_tokenizer.json
  14. print(out.detach().cpu().numpy()) # get logits
  15. out, state = model.forward([187, 510], None)
  16. out, state = model.forward([1563], state) # RNN has state (use deepcopy if you want to clone it)
  17. out, state = model.forward([310, 247], state)
  18. print(out.detach().cpu().numpy()) # same result as above
  19. pipeline = PIPELINE(model, Path("repositories/ChatRWKV/20B_tokenizer.json").as_posix())
  20. return pipeline