initial addition

This commit is contained in:
hypnopump 2023-04-03 00:52:26 +02:00
parent d62a050144
commit a64aaa81ec
No known key found for this signature in database
GPG Key ID: 5E31BA0612BC5C17
4 changed files with 39 additions and 11 deletions

View File

@ -31,12 +31,14 @@ cmake --build . --config Release
If everything went OK, `bin\Release\rwkv.dll` file should appear. If everything went OK, `bin\Release\rwkv.dll` file should appear.
### 2. Download an RWKV model from [Hugging Face](https://huggingface.co/BlinkDL) and convert it into `ggml` format ### 2. Download an RWKV model from [Hugging Face](https://huggingface.co/BlinkDL) like [this one](https://huggingface.co/BlinkDL/rwkv-4-pile-169m/blob/main/RWKV-4b-Pile-171M-20230202-7922.pth) and convert it into `ggml` format
**Requirements**: Python 3.x with [PyTorch](https://pytorch.org/get-started/locally/). **Requirements**: Python 3.x with [PyTorch](https://pytorch.org/get-started/locally/).
```commandline ```commandline
python rwkv\convert_pytorch_rwkv_to_ggml.py C:\RWKV-4-Pile-169M-20220807-8023.pth C:\rwkv.cpp-169M.bin float32 # Windows
python rwkv\convert_rwkv_to_ggml.py C:\RWKV-4b-Pile-171M-20230202-7922.pth C:\rwkv.cpp-171M.bin float32
# Linux/MacOS
python rwkv/convert_pytorch_to_ggml.py ~/Downloads/RWKV-4b-Pile-171M-20230202-7922.pth ~/Downloads/rwkv.cpp-171M.bin float32
``` ```
#### 2.1. Optionally, quantize the model #### 2.1. Optionally, quantize the model
@ -44,7 +46,10 @@ python rwkv\convert_pytorch_rwkv_to_ggml.py C:\RWKV-4-Pile-169M-20220807-8023.pt
To convert the model into INT4 quantized format, run: To convert the model into INT4 quantized format, run:
```commandline ```commandline
python rwkv\quantize.py C:\rwkv.cpp-169M.bin C:\rwkv.cpp-169M-Q4_1.bin 3 # Windows
python rwkv\quantize.py C:\rwkv.cpp-171M.bin C:\rwkv.cpp-171M-Q4_1.bin 3
# Linux / MacOS
python rwkv/quantize.py ~/Downloads/rwkv.cpp-171M.bin ~/Downloads/rwkv.cpp-171M-Q4_1.bin 3
``` ```
Pass `2` for `Q4_0` format (smaller size, lower quality), `3` for `Q4_1` format (larger size, higher quality). Pass `2` for `Q4_0` format (smaller size, lower quality), `3` for `Q4_1` format (larger size, higher quality).
@ -53,16 +58,24 @@ Pass `2` for `Q4_0` format (smaller size, lower quality), `3` for `Q4_1` format
**Requirements**: Python 3.x with [PyTorch](https://pytorch.org/get-started/locally/) and [tokenizers](https://pypi.org/project/tokenizers/). **Requirements**: Python 3.x with [PyTorch](https://pytorch.org/get-started/locally/) and [tokenizers](https://pypi.org/project/tokenizers/).
**Note**: change the model path with the non-quantized model for the full weights model.
To generate some text, run: To generate some text, run:
```commandline ```commandline
python rwkv\generate_completions.py C:\rwkv.cpp-169M.bin # Windows
python rwkv\generate_completions.py C:\rwkv.cpp-171M-Q4_1.bin
# Linux / MacOS
python rwkv/generate_completions.py ~/Downloads/rwkv.cpp-171M-Q4_1.bin
``` ```
To chat with a bot, run: To chat with a bot, run:
```commandline ```commandline
python rwkv\chat_with_bot.py C:\rwkv.cpp-169M.bin # Windows
python rwkv\chat_with_bot.py C:\rwkv.cpp-171M-Q4_1.bin
# Linux / MacOS
python rwkv/chat_with_bot.py ~/Downloads/rwkv.cpp-171M-Q4_1.bin
``` ```
Edit [generate_completions.py](rwkv%2Fgenerate_completions.py) or [chat_with_bot.py](rwkv%2Fchat_with_bot.py) to change prompts and sampling settings. Edit [generate_completions.py](rwkv%2Fgenerate_completions.py) or [chat_with_bot.py](rwkv%2Fchat_with_bot.py) to change prompts and sampling settings.
@ -75,9 +88,13 @@ Example of using `rwkv.cpp` in your custom Python script:
import rwkv_cpp_model import rwkv_cpp_model
import rwkv_cpp_shared_library import rwkv_cpp_shared_library
# change by model paths used above (quantized or full weights)
model_path = r'C:\rwkv.cpp-169M.bin'
model = rwkv_cpp_model.RWKVModel( model = rwkv_cpp_model.RWKVModel(
rwkv_cpp_shared_library.load_rwkv_shared_library(), rwkv_cpp_shared_library.load_rwkv_shared_library(),
r'C:\rwkv.cpp-169M.bin' model_path
) )
logits, state = None, None logits, state = None, None

View File

@ -1,11 +1,13 @@
# Provides terminal-based chat interface for RWKV model. # Provides terminal-based chat interface for RWKV model.
import os
import sys import sys
import argparse import argparse
import sampling import sampling
import tokenizers import tokenizers
import rwkv_cpp_model import rwkv_cpp_model
import rwkv_cpp_shared_library import rwkv_cpp_shared_library
from pathlib import Path
# ======================================== Script settings ======================================== # ======================================== Script settings ========================================
@ -36,7 +38,8 @@ args = parser.parse_args()
assert prompt != '', 'Prompt must not be empty' assert prompt != '', 'Prompt must not be empty'
print('Loading 20B tokenizer') print('Loading 20B tokenizer')
tokenizer = tokenizers.Tokenizer.from_file('20B_tokenizer.json') tokenizer_path = Path(os.path.abspath(__file__)).parent / '20B_tokenizer.json'
tokenizer = tokenizers.Tokenizer.from_file(str(tokenizer_path))
library = rwkv_cpp_shared_library.load_rwkv_shared_library() library = rwkv_cpp_shared_library.load_rwkv_shared_library()
print(f'System info: {library.rwkv_get_system_info_string()}') print(f'System info: {library.rwkv_get_system_info_string()}')

View File

@ -1,11 +1,14 @@
# Generates completions from RWKV model based on a prompt. # Generates completions from RWKV model based on a prompt.
import argparse import argparse
import os
import time import time
import sampling import sampling
import tokenizers import tokenizers
import rwkv_cpp_model import rwkv_cpp_model
import rwkv_cpp_shared_library import rwkv_cpp_shared_library
from pathlib import Path
# ======================================== Script settings ======================================== # ======================================== Script settings ========================================
@ -33,7 +36,8 @@ args = parser.parse_args()
assert prompt != '', 'Prompt must not be empty' assert prompt != '', 'Prompt must not be empty'
print('Loading 20B tokenizer') print('Loading 20B tokenizer')
tokenizer = tokenizers.Tokenizer.from_file('20B_tokenizer.json') tokenizer_path = Path(os.path.abspath(__file__)).parent / '20B_tokenizer.json'
tokenizer = tokenizers.Tokenizer.from_file(str(tokenizer_path))
library = rwkv_cpp_shared_library.load_rwkv_shared_library() library = rwkv_cpp_shared_library.load_rwkv_shared_library()
print(f'System info: {library.rwkv_get_system_info_string()}') print(f'System info: {library.rwkv_get_system_info_string()}')

View File

@ -1,8 +1,10 @@
import os import os
import sys import sys
import ctypes import ctypes
from pathlib import Path
from typing import Optional from typing import Optional
P_FLOAT = ctypes.POINTER(ctypes.c_float) P_FLOAT = ctypes.POINTER(ctypes.c_float)
class RWKVContext: class RWKVContext:
@ -185,8 +187,10 @@ def load_rwkv_shared_library() -> RWKVSharedLibrary:
if 'win32' in sys.platform or 'cygwin' in sys.platform: if 'win32' in sys.platform or 'cygwin' in sys.platform:
file_name = 'rwkv.dll' file_name = 'rwkv.dll'
elif 'darwin' in sys.platform:
file_name = 'rwkv.o'
else: else:
file_name = 'rwkv.so' file_name = 'librwkv.so'
paths = [ paths = [
# If we are in "rwkv" directory # If we are in "rwkv" directory
@ -194,7 +198,7 @@ def load_rwkv_shared_library() -> RWKVSharedLibrary:
# If we are in repo root directory # If we are in repo root directory
f'bin/Release/{file_name}', f'bin/Release/{file_name}',
# Fallback # Fallback
file_name Path(os.path.abspath(__file__)).parent.parent / file_name
] ]
for path in paths: for path in paths: