Upload folder using huggingface_hub
Browse files- README.md +138 -3
- __init__.py +0 -0
- added_tokens.json +3 -0
- config.json +38 -0
- generation_config.json +6 -0
- hf_rwkv_tokenizer.py +279 -0
- model.safetensors +3 -0
- modeling_rwkv7.py +4 -0
- rwkv_vocab_v20230424.txt +0 -0
- special_tokens_map.json +6 -0
- tokenizer_config.json +28 -0
README.md
CHANGED
|
@@ -1,3 +1,138 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
base_model:
|
| 3 |
+
- BlinkDL/rwkv7-g1
|
| 4 |
+
language:
|
| 5 |
+
- en
|
| 6 |
+
- zh
|
| 7 |
+
- ja
|
| 8 |
+
- ko
|
| 9 |
+
- fr
|
| 10 |
+
- ar
|
| 11 |
+
- es
|
| 12 |
+
- pt
|
| 13 |
+
license: apache-2.0
|
| 14 |
+
metrics:
|
| 15 |
+
- accuracy
|
| 16 |
+
pipeline_tag: text-generation
|
| 17 |
+
library_name: transformers
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
# rwkv7-0.1B-g1a
|
| 21 |
+
|
| 22 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
| 23 |
+
|
| 24 |
+
This is RWKV-7 g1 model under flash-linear attention format. The `g1` model series added significant more data and incorporated deep thinking abilities.
|
| 25 |
+
|
| 26 |
+
## Model Details
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
### Model Description
|
| 30 |
+
|
| 31 |
+
<!-- Provide a longer summary of what this model is. -->
|
| 32 |
+
|
| 33 |
+
- **Developed by:** Bo Peng, Yu Zhang, Songlin Yang, Ruichong Zhang
|
| 34 |
+
- **Funded by:** RWKV Project (Under LF AI & Data Foundation)
|
| 35 |
+
- **Model type:** RWKV7
|
| 36 |
+
- **Language(s) (NLP):** Multilingal
|
| 37 |
+
- **License:** Apache-2.0
|
| 38 |
+
- **Parameter count:** 191M
|
| 39 |
+
- **Tokenizer:** RWKV World tokenizer
|
| 40 |
+
- **Vocabulary size:** 65,536
|
| 41 |
+
|
| 42 |
+
### Model Sources
|
| 43 |
+
|
| 44 |
+
<!-- Provide the basic links for the model. -->
|
| 45 |
+
|
| 46 |
+
- **Repository:** https://github.com/fla-org/flash-linear-attention ; https://github.com/BlinkDL/RWKV-LM
|
| 47 |
+
- **Paper:** https://arxiv.org/abs/2503.14456
|
| 48 |
+
|
| 49 |
+
## Uses
|
| 50 |
+
|
| 51 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 52 |
+
Install `flash-linear-attention` and the latest version of `transformers` before using this model:
|
| 53 |
+
|
| 54 |
+
```bash
|
| 55 |
+
pip install git+https://github.com/fla-org/flash-linear-attention
|
| 56 |
+
pip install 'transformers>=4.48.0'
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
### Direct Use
|
| 60 |
+
|
| 61 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 62 |
+
You can use this model just as any other HuggingFace models:
|
| 63 |
+
```python
|
| 64 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 65 |
+
model = AutoModelForCausalLM.from_pretrained('fla-hub/rwkv7-0.1B-g1a', trust_remote_code=True)
|
| 66 |
+
tokenizer = AutoTokenizer.from_pretrained('fla-hub/rwkv7-0.1B-g1a', trust_remote_code=True)
|
| 67 |
+
model = model.cuda() # Supported on Nvidia/AMD/Intel eg. model.xpu()
|
| 68 |
+
prompt = "What is a large language model?"
|
| 69 |
+
messages = [
|
| 70 |
+
{"role": "user", "content": prompt}
|
| 71 |
+
]
|
| 72 |
+
text = tokenizer.apply_chat_template(
|
| 73 |
+
messages,
|
| 74 |
+
tokenize=False,
|
| 75 |
+
add_generation_prompt=True,
|
| 76 |
+
enable_thinking=True # Default is True, set to False to disable thinking
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 80 |
+
generated_ids = model.generate(
|
| 81 |
+
**model_inputs,
|
| 82 |
+
max_new_tokens=1024,
|
| 83 |
+
do_sample=True,
|
| 84 |
+
temperature=1.0,
|
| 85 |
+
top_p=0.3,
|
| 86 |
+
repetition_penalty=1.2
|
| 87 |
+
)
|
| 88 |
+
generated_ids = [
|
| 89 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 90 |
+
]
|
| 91 |
+
|
| 92 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 93 |
+
print(response)
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
## FAQ
|
| 98 |
+
Q: safetensors metadata is none.
|
| 99 |
+
|
| 100 |
+
A: upgrade transformers to >=4.48.0: `pip install 'transformers>=4.48.0'`
|
| 101 |
+
|
| 102 |
+
## Thinking Prompt
|
| 103 |
+
```
|
| 104 |
+
<|rwkv_tokenizer_end_of_text|>User: <Your Question Here>
|
| 105 |
+
|
| 106 |
+
Assistant: <think
|
| 107 |
+
```
|
| 108 |
+
Don't close the brackets for `<think`!
|
| 109 |
+
|
| 110 |
+
## Addidtional Caveats for Prompting
|
| 111 |
+
|
| 112 |
+
**Always add `<|rwkv_tokenizer_end_of_text|>` (Token ID = 0) before your prompt. The model is incapable of attending the first token it receives due to state initialization issues.**
|
| 113 |
+
|
| 114 |
+
Bad prompt example:
|
| 115 |
+
```
|
| 116 |
+
Mathews lifted a dark brow. "Are you sure about that? I mean, wouldn't it be better to wait until Dale is home safe and sound?"
|
| 117 |
+
|
| 118 |
+
"The longer I wait to tell her, the worse it will be for both of us."
|
| 119 |
+
|
| 120 |
+
"Good luck. You're going to need it," said
|
| 121 |
+
```
|
| 122 |
+
The model is unable to recall ` Mathews` because it is the very first token of the input.
|
| 123 |
+
|
| 124 |
+
Good prompt example:
|
| 125 |
+
```
|
| 126 |
+
<|rwkv_tokenizer_end_of_text|>Mathews lifted a dark brow. "Are you sure about that? I mean, wouldn't it be better to wait until Dale is home safe and sound?"
|
| 127 |
+
|
| 128 |
+
"The longer I wait to tell her, the worse it will be for both of us."
|
| 129 |
+
|
| 130 |
+
"Good luck. You're going to need it," said
|
| 131 |
+
```
|
| 132 |
+
the model will output ` Mathews` as expected.
|
| 133 |
+
|
| 134 |
+
Without this token: **`lambada_openai ppl=13.84 acc=48.13%`**
|
| 135 |
+
|
| 136 |
+
With this token added: **`lambada_openai ppl=12.36 acc=49.12%`**
|
| 137 |
+
|
| 138 |
+
Note: this phenomenon is very rare for Transformers but significant for RNNs. We speculate that the model uses the first token to pin the states, to better acquire information from later tokens.
|
__init__.py
ADDED
|
File without changes
|
added_tokens.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"<|rwkv_tokenizer_end_of_text|>": 0
|
| 3 |
+
}
|
config.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"a_low_rank_dim": 64,
|
| 3 |
+
"architectures": [
|
| 4 |
+
"RWKV7ForCausalLM"
|
| 5 |
+
],
|
| 6 |
+
"attn": null,
|
| 7 |
+
"attn_mode": "chunk",
|
| 8 |
+
"auto_map": {
|
| 9 |
+
"AutoConfig": "modeling_rwkv7.RWKV7Config",
|
| 10 |
+
"AutoModel": "modeling_rwkv7.RWKV7Model",
|
| 11 |
+
"AutoModelForCausalLM": "modeling_rwkv7.RWKV7ForCausalLM"
|
| 12 |
+
},
|
| 13 |
+
"bos_token_id": 0,
|
| 14 |
+
"decay_low_rank_dim": 64,
|
| 15 |
+
"eos_token_id": 0,
|
| 16 |
+
"fuse_cross_entropy": true,
|
| 17 |
+
"fuse_norm": false,
|
| 18 |
+
"gate_low_rank_dim": 128,
|
| 19 |
+
"head_dim": 64,
|
| 20 |
+
"hidden_act": "sqrelu",
|
| 21 |
+
"hidden_ratio": 4.0,
|
| 22 |
+
"hidden_size": 768,
|
| 23 |
+
"initializer_range": 0.006,
|
| 24 |
+
"intermediate_size": 3072,
|
| 25 |
+
"max_position_embeddings": 2048,
|
| 26 |
+
"model_type": "rwkv7",
|
| 27 |
+
"norm_bias": true,
|
| 28 |
+
"norm_eps": 1e-05,
|
| 29 |
+
"norm_first": true,
|
| 30 |
+
"num_heads": 32,
|
| 31 |
+
"num_hidden_layers": 12,
|
| 32 |
+
"tie_word_embeddings": false,
|
| 33 |
+
"torch_dtype": "float32",
|
| 34 |
+
"transformers_version": "4.48.0",
|
| 35 |
+
"use_cache": true,
|
| 36 |
+
"v_low_rank_dim": 32,
|
| 37 |
+
"vocab_size": 65536
|
| 38 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 0,
|
| 4 |
+
"eos_token_id": 0,
|
| 5 |
+
"transformers_version": "4.48.0"
|
| 6 |
+
}
|
hf_rwkv_tokenizer.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2024 The HuggingFace Inc. team.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
"""Tokenization classes for RWKV."""
|
| 16 |
+
|
| 17 |
+
import os
|
| 18 |
+
import re
|
| 19 |
+
from typing import TYPE_CHECKING, List, Optional, Tuple
|
| 20 |
+
|
| 21 |
+
from transformers.tokenization_utils import AddedToken, PreTrainedTokenizer
|
| 22 |
+
from transformers.utils import logging
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
if TYPE_CHECKING:
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
logger = logging.get_logger(__name__)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
VOCAB_FILES_NAMES = {
|
| 32 |
+
"vocab_file": "rwkv_vocab_v20230424.txt",
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
class TRIE:
|
| 36 |
+
__slots__ = tuple("ch,to,values,front".split(","))
|
| 37 |
+
to: list
|
| 38 |
+
values: set
|
| 39 |
+
|
| 40 |
+
def __init__(self, front=None, ch=None):
|
| 41 |
+
self.ch = ch
|
| 42 |
+
self.to = [None for ch in range(256)]
|
| 43 |
+
self.values = set()
|
| 44 |
+
self.front = front
|
| 45 |
+
|
| 46 |
+
def __repr__(self):
|
| 47 |
+
fr = self
|
| 48 |
+
ret = []
|
| 49 |
+
while fr != None:
|
| 50 |
+
if fr.ch != None:
|
| 51 |
+
ret.append(fr.ch)
|
| 52 |
+
fr = fr.front
|
| 53 |
+
return "<TRIE %s %s>" % (ret[::-1], self.values)
|
| 54 |
+
|
| 55 |
+
def add(self, key: bytes, idx: int = 0, val=None):
|
| 56 |
+
if idx == len(key):
|
| 57 |
+
if val is None:
|
| 58 |
+
val = key
|
| 59 |
+
self.values.add(val)
|
| 60 |
+
return self
|
| 61 |
+
ch = key[idx]
|
| 62 |
+
if self.to[ch] is None:
|
| 63 |
+
self.to[ch] = TRIE(front=self, ch=ch)
|
| 64 |
+
return self.to[ch].add(key, idx=idx + 1, val=val)
|
| 65 |
+
|
| 66 |
+
def find_longest(self, key: bytes, idx: int = 0):
|
| 67 |
+
u: TRIE = self
|
| 68 |
+
ch: int = key[idx]
|
| 69 |
+
|
| 70 |
+
while u.to[ch] is not None:
|
| 71 |
+
u = u.to[ch]
|
| 72 |
+
idx += 1
|
| 73 |
+
if u.values:
|
| 74 |
+
ret = idx, u, u.values
|
| 75 |
+
if idx == len(key):
|
| 76 |
+
break
|
| 77 |
+
ch = key[idx]
|
| 78 |
+
return ret
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class RWKV_TOKENIZER:
|
| 82 |
+
def __init__(self, file_name):
|
| 83 |
+
self.idx2token = {}
|
| 84 |
+
sorted = [] # must be already sorted
|
| 85 |
+
with open(file_name, "r", encoding="utf-8") as f:
|
| 86 |
+
lines = f.readlines()
|
| 87 |
+
for l in lines:
|
| 88 |
+
idx = int(l[: l.index(" ")])
|
| 89 |
+
x = eval(l[l.index(" ") : l.rindex(" ")])
|
| 90 |
+
x = x.encode("utf-8") if isinstance(x, str) else x
|
| 91 |
+
assert isinstance(x, bytes)
|
| 92 |
+
|
| 93 |
+
assert len(x) == int(l[l.rindex(" ") :])
|
| 94 |
+
sorted += [x]
|
| 95 |
+
self.idx2token[idx] = x
|
| 96 |
+
|
| 97 |
+
self.token2idx = {}
|
| 98 |
+
for k, v in self.idx2token.items():
|
| 99 |
+
self.token2idx[v] = int(k)
|
| 100 |
+
|
| 101 |
+
self.root = TRIE()
|
| 102 |
+
for t, i in self.token2idx.items():
|
| 103 |
+
_ = self.root.add(t, val=(t, i))
|
| 104 |
+
|
| 105 |
+
def encodeBytes(self, src: bytes):
|
| 106 |
+
idx: int = 0
|
| 107 |
+
tokens = []
|
| 108 |
+
while idx < len(src):
|
| 109 |
+
_idx: int = idx
|
| 110 |
+
idx, _, values = self.root.find_longest(src, idx)
|
| 111 |
+
assert idx != _idx
|
| 112 |
+
_, token = next(iter(values))
|
| 113 |
+
tokens.append(token)
|
| 114 |
+
return tokens
|
| 115 |
+
|
| 116 |
+
def decodeBytes(self, tokens):
|
| 117 |
+
return b"".join(map(lambda i: self.idx2token[i], tokens))
|
| 118 |
+
|
| 119 |
+
def encode(self, src):
|
| 120 |
+
if isinstance(src, str):
|
| 121 |
+
return [self.encodeBytes(src.encode("utf-8"))]
|
| 122 |
+
elif isinstance(src, list):
|
| 123 |
+
return [self.encodeBytes(s.encode("utf-8")) for s in src]
|
| 124 |
+
|
| 125 |
+
def decode(self, tokens):
|
| 126 |
+
return [self.decodeBytes(batch).decode("utf-8") for batch in tokens]
|
| 127 |
+
# try:
|
| 128 |
+
# return self.decodeBytes(tokens).decode('utf-8')
|
| 129 |
+
# except:
|
| 130 |
+
# return '\ufffd' # bad utf-8
|
| 131 |
+
|
| 132 |
+
def printTokens(self, tokens):
|
| 133 |
+
for i in tokens:
|
| 134 |
+
s = self.idx2token[i]
|
| 135 |
+
try:
|
| 136 |
+
s = s.decode("utf-8")
|
| 137 |
+
except:
|
| 138 |
+
pass
|
| 139 |
+
print(f"{repr(s)}{i}", end=" ")
|
| 140 |
+
print()
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
class RwkvTokenizer(PreTrainedTokenizer):
|
| 144 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
| 145 |
+
model_input_names = ["input_ids", "attention_mask"]
|
| 146 |
+
|
| 147 |
+
def __init__(
|
| 148 |
+
self, vocab_file, bos_token="<|rwkv_tokenizer_end_of_text|>", eos_token="<|rwkv_tokenizer_end_of_text|>", unk_token="<|rwkv_tokenizer_end_of_text|>", **kwargs
|
| 149 |
+
):
|
| 150 |
+
if not os.path.isfile(vocab_file):
|
| 151 |
+
raise ValueError(
|
| 152 |
+
f"Can't find a vocabulary file at path '{vocab_file}'."
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
with open(vocab_file, "r", encoding="utf-8") as reader:
|
| 156 |
+
tokens = reader.readlines()
|
| 157 |
+
|
| 158 |
+
if "add_bos_token" in kwargs:
|
| 159 |
+
self.add_bos_token = kwargs["add_bos_token"]
|
| 160 |
+
else:
|
| 161 |
+
self.add_bos_token = False
|
| 162 |
+
self.trie_tokenizer = RWKV_TOKENIZER(vocab_file)
|
| 163 |
+
vocab = self.trie_tokenizer.token2idx
|
| 164 |
+
self.encoder = vocab
|
| 165 |
+
self.decoder = {v: k for k, v in vocab.items()}
|
| 166 |
+
self._added_tokens_decoder = {0: AddedToken(str(bos_token))}
|
| 167 |
+
super().__init__(
|
| 168 |
+
bos_token=bos_token, eos_token=eos_token, unk_token=unk_token, **kwargs
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
@property
|
| 172 |
+
def vocab_size(self):
|
| 173 |
+
return len(self.encoder)
|
| 174 |
+
|
| 175 |
+
def get_vocab(self):
|
| 176 |
+
vocab = self.encoder
|
| 177 |
+
vocab.update(self.added_tokens_encoder)
|
| 178 |
+
vocab = dict(sorted(vocab.items(), key=lambda item: item[1]))
|
| 179 |
+
return vocab
|
| 180 |
+
|
| 181 |
+
def _tokenize(self, text, split_special_tokens=False):
|
| 182 |
+
# return self.wordpiece_tokenizer.tokenize(text.encode("utf-8"))
|
| 183 |
+
return self.trie_tokenizer.encode(text)[0]
|
| 184 |
+
|
| 185 |
+
def _convert_token_to_id(self, token):
|
| 186 |
+
return token
|
| 187 |
+
|
| 188 |
+
def _convert_id_to_token(self, index):
|
| 189 |
+
"""Converts an index (integer) in a token (byte) using the vocab."""
|
| 190 |
+
token = self.decoder.get(index, self.unk_token)
|
| 191 |
+
if isinstance(token, (bytes)):
|
| 192 |
+
token = token.decode("utf-8", errors="replace")
|
| 193 |
+
return token
|
| 194 |
+
|
| 195 |
+
def convert_tokens_to_string(self, tokens):
|
| 196 |
+
"""Converts a sequence of tokens (bytes) in a single string. Additional tokens are encoded to bytes"""
|
| 197 |
+
out_string = b"".join(
|
| 198 |
+
[k.encode(errors="replace") if isinstance(k, str) else k for k in tokens]
|
| 199 |
+
).decode("utf-8")
|
| 200 |
+
return out_string
|
| 201 |
+
|
| 202 |
+
def save_vocabulary(
|
| 203 |
+
self, save_directory: str, filename_prefix: Optional[str] = None
|
| 204 |
+
) -> Tuple[str]:
|
| 205 |
+
index = 0
|
| 206 |
+
if os.path.isdir(save_directory):
|
| 207 |
+
vocab_file = os.path.join(
|
| 208 |
+
save_directory,
|
| 209 |
+
(filename_prefix + "-" if filename_prefix else "") + "vocab.txt",
|
| 210 |
+
)
|
| 211 |
+
else:
|
| 212 |
+
vocab_file = (
|
| 213 |
+
filename_prefix + "-" if filename_prefix else ""
|
| 214 |
+
) + save_directory
|
| 215 |
+
with open(vocab_file, "w", encoding="utf-8") as writer:
|
| 216 |
+
for token, token_index in sorted(
|
| 217 |
+
self.encoder.items(), key=lambda kv: kv[1]
|
| 218 |
+
):
|
| 219 |
+
if index != token_index:
|
| 220 |
+
logger.warning(
|
| 221 |
+
f"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."
|
| 222 |
+
" Please check that the vocabulary is not corrupted!"
|
| 223 |
+
)
|
| 224 |
+
index = token_index
|
| 225 |
+
writer.write(str(token) + "\n")
|
| 226 |
+
index += 1
|
| 227 |
+
return (vocab_file,)
|
| 228 |
+
|
| 229 |
+
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
|
| 230 |
+
if self.add_bos_token:
|
| 231 |
+
bos_token_ids = [self.bos_token_id]
|
| 232 |
+
else:
|
| 233 |
+
bos_token_ids = []
|
| 234 |
+
|
| 235 |
+
output = bos_token_ids + token_ids_0
|
| 236 |
+
|
| 237 |
+
if token_ids_1 is None:
|
| 238 |
+
return output
|
| 239 |
+
|
| 240 |
+
return output + bos_token_ids + token_ids_1
|
| 241 |
+
|
| 242 |
+
def get_special_tokens_mask(
|
| 243 |
+
self,
|
| 244 |
+
token_ids_0: List[int],
|
| 245 |
+
token_ids_1: Optional[List[int]] = None,
|
| 246 |
+
already_has_special_tokens: bool = False,
|
| 247 |
+
) -> List[int]:
|
| 248 |
+
"""
|
| 249 |
+
Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
|
| 250 |
+
special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods.
|
| 251 |
+
|
| 252 |
+
Args:
|
| 253 |
+
token_ids_0 (`List[int]`):
|
| 254 |
+
List of IDs.
|
| 255 |
+
token_ids_1 (`List[int]`, *optional*):
|
| 256 |
+
Optional second list of IDs for sequence pairs.
|
| 257 |
+
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
|
| 258 |
+
Whether or not the token list is already formatted with special tokens for the model.
|
| 259 |
+
|
| 260 |
+
Returns:
|
| 261 |
+
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
| 262 |
+
"""
|
| 263 |
+
if already_has_special_tokens:
|
| 264 |
+
return super().get_special_tokens_mask(
|
| 265 |
+
token_ids_0=token_ids_0,
|
| 266 |
+
token_ids_1=token_ids_1,
|
| 267 |
+
already_has_special_tokens=True,
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
if not self.add_bos_token:
|
| 271 |
+
return super().get_special_tokens_mask(
|
| 272 |
+
token_ids_0=token_ids_0,
|
| 273 |
+
token_ids_1=token_ids_1,
|
| 274 |
+
already_has_special_tokens=False,
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
if token_ids_1 is None:
|
| 278 |
+
return [1] + ([0] * len(token_ids_0))
|
| 279 |
+
return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1))
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:569ad97767a155676c15b3357cd00d4fd1b0dff21b8533fc07a0917d45e00176
|
| 3 |
+
size 382111072
|
modeling_rwkv7.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fla.models.rwkv7 import RWKV7ForCausalLM, RWKV7Model, RWKV7Config
|
| 2 |
+
RWKV7ForCausalLM = RWKV7ForCausalLM
|
| 3 |
+
RWKV7Model = RWKV7Model
|
| 4 |
+
RWKV7Config = RWKV7Config
|
rwkv_vocab_v20230424.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": "<|rwkv_tokenizer_end_of_text|>",
|
| 3 |
+
"eos_token": "\n\n",
|
| 4 |
+
"unk_token": "<|rwkv_tokenizer_end_of_text|>",
|
| 5 |
+
"pad_token": "<|rwkv_tokenizer_end_of_text|>"
|
| 6 |
+
}
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"added_tokens_decoder": {
|
| 4 |
+
"0": {
|
| 5 |
+
"content": "<|rwkv_tokenizer_end_of_text|>",
|
| 6 |
+
"lstrip": false,
|
| 7 |
+
"normalized": false,
|
| 8 |
+
"rstrip": false,
|
| 9 |
+
"single_word": false,
|
| 10 |
+
"special": true
|
| 11 |
+
}
|
| 12 |
+
},
|
| 13 |
+
"auto_map": {
|
| 14 |
+
"AutoTokenizer": [
|
| 15 |
+
"hf_rwkv_tokenizer.RwkvTokenizer",
|
| 16 |
+
null
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
"bos_token": "<|rwkv_tokenizer_end_of_text|>",
|
| 20 |
+
"pad_token": "<|rwkv_tokenizer_end_of_text|>",
|
| 21 |
+
"clean_up_tokenization_spaces": false,
|
| 22 |
+
"eos_token": "\n\n",
|
| 23 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 24 |
+
"tokenizer_class": "RwkvTokenizer",
|
| 25 |
+
"unk_token": "<|rwkv_tokenizer_end_of_text|>",
|
| 26 |
+
"use_fast": false,
|
| 27 |
+
"chat_template": "{{ '<|rwkv_tokenizer_end_of_text|>' }}{% for message in messages %}{% if message['role'] == 'user' %}{{'User: ' + message['content'] + '\n\n'}}{% elif message['role'] == 'system' %}{{'System: ' + message['content'] + '\n\n'}}{% elif message['role'] == 'assistant' %}{{'Assistant: ' + message['content'] + '\n\n'}}{% endif %}{% endfor %}{% if add_generation_prompt %}{% if enable_thinking is defined and enable_thinking == False %}{{ 'Assistant: <think>\n</think>' }}{% else %}{{ 'Assistant: <think' }}{% endif %}{% endif %}"
|
| 28 |
+
}
|