Instructions to use tobiges/behavior_fast with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use tobiges/behavior_fast with Transformers:
# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("tobiges/behavior_fast", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload processor
Browse files- processing_action_tokenizer.py +39 -9
- processor_config.json +2 -2
- tokenizer.json +0 -0
processing_action_tokenizer.py
CHANGED
|
@@ -8,7 +8,14 @@ from tokenizers import ByteLevelBPETokenizer
|
|
| 8 |
from tokenizers.trainers import BpeTrainer
|
| 9 |
from transformers import PreTrainedTokenizerFast
|
| 10 |
from transformers.processing_utils import ProcessorMixin
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
class UniversalActionProcessor(ProcessorMixin):
|
| 14 |
attributes: ClassVar[list[str]] = ["bpe_tokenizer"]
|
|
@@ -124,14 +131,36 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 124 |
|
| 125 |
# Make token iterator for BPE training
|
| 126 |
def _token_iter():
|
| 127 |
-
|
|
|
|
| 128 |
rounded_tokens = np.around(tokens * scale) - min_token
|
| 129 |
rounded_tokens = rounded_tokens.astype(int)
|
| 130 |
string = "".join(map(chr, rounded_tokens))
|
| 131 |
yield string
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
# Train BPE tokenizer
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# Set up the entire range of possible tokens as the initial alphabet
|
| 137 |
alphabet = [chr(i) for i in range(max_token - min_token + 1)]
|
|
@@ -140,16 +169,17 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 140 |
min_frequency=2,
|
| 141 |
show_progress=True,
|
| 142 |
special_tokens=[],
|
|
|
|
| 143 |
initial_alphabet=alphabet,
|
| 144 |
-
max_token_length=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
)
|
| 146 |
-
|
| 147 |
-
# Train the inner tokenizer (don't use ByteLevelBPETokenizer.train_from_iterator()
|
| 148 |
-
# because it doesn't support custom alphabets)
|
| 149 |
-
bpe._tokenizer.train_from_iterator(_token_iter(), trainer=trainer)
|
| 150 |
|
| 151 |
return cls(
|
| 152 |
-
PreTrainedTokenizerFast(tokenizer_object=
|
| 153 |
scale=scale,
|
| 154 |
vocab_size=vocab_size,
|
| 155 |
min_token=min_token,
|
|
|
|
| 8 |
from tokenizers.trainers import BpeTrainer
|
| 9 |
from transformers import PreTrainedTokenizerFast
|
| 10 |
from transformers.processing_utils import ProcessorMixin
|
| 11 |
+
import numpy as np
|
| 12 |
+
from scipy.fft import dct, idct
|
| 13 |
+
from tokenizers import Tokenizer, decoders, pre_tokenizers, processors
|
| 14 |
+
from tokenizers.models import BPE
|
| 15 |
+
from tokenizers.trainers import BpeTrainer
|
| 16 |
+
from transformers import PreTrainedTokenizerFast
|
| 17 |
+
from transformers.processing_utils import ProcessorMixin
|
| 18 |
+
from tokenizers import Tokenizer
|
| 19 |
|
| 20 |
class UniversalActionProcessor(ProcessorMixin):
|
| 21 |
attributes: ClassVar[list[str]] = ["bpe_tokenizer"]
|
|
|
|
| 131 |
|
| 132 |
# Make token iterator for BPE training
|
| 133 |
def _token_iter():
|
| 134 |
+
while dct_tokens:
|
| 135 |
+
tokens = dct_tokens.pop()
|
| 136 |
rounded_tokens = np.around(tokens * scale) - min_token
|
| 137 |
rounded_tokens = rounded_tokens.astype(int)
|
| 138 |
string = "".join(map(chr, rounded_tokens))
|
| 139 |
yield string
|
| 140 |
|
| 141 |
+
# # Train BPE tokenizer
|
| 142 |
+
# bpe = ByteLevelBPETokenizer()
|
| 143 |
+
|
| 144 |
+
# # Set up the entire range of possible tokens as the initial alphabet
|
| 145 |
+
# alphabet = [chr(i) for i in range(max_token - min_token + 1)]
|
| 146 |
+
# trainer = BpeTrainer(
|
| 147 |
+
# vocab_size=vocab_size,
|
| 148 |
+
# min_frequency=2,
|
| 149 |
+
# show_progress=True,
|
| 150 |
+
# special_tokens=[],
|
| 151 |
+
# initial_alphabet=alphabet,
|
| 152 |
+
# max_token_length=10000,
|
| 153 |
+
# )
|
| 154 |
+
|
| 155 |
+
# # Train the inner tokenizer (don't use ByteLevelBPETokenizer.train_from_iterator()
|
| 156 |
+
# # because it doesn't support custom alphabets)
|
| 157 |
+
# bpe._tokenizer.train_from_iterator(_token_iter(), trainer=trainer)
|
| 158 |
+
|
| 159 |
# Train BPE tokenizer
|
| 160 |
+
tokenizer = Tokenizer(BPE())
|
| 161 |
+
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=False, use_regex=False)
|
| 162 |
+
tokenizer.decoder = decoders.ByteLevel()
|
| 163 |
+
tokenizer.post_processor = processors.ByteLevel(trim_offsets=False)
|
| 164 |
|
| 165 |
# Set up the entire range of possible tokens as the initial alphabet
|
| 166 |
alphabet = [chr(i) for i in range(max_token - min_token + 1)]
|
|
|
|
| 169 |
min_frequency=2,
|
| 170 |
show_progress=True,
|
| 171 |
special_tokens=[],
|
| 172 |
+
# initial_alphabet=pre_tokenizers.ByteLevel.alphabet(),
|
| 173 |
initial_alphabet=alphabet,
|
| 174 |
+
# max_token_length=256,
|
| 175 |
+
max_token_length=10_000,
|
| 176 |
+
)
|
| 177 |
+
tokenizer.train_from_iterator(
|
| 178 |
+
_token_iter(), trainer=trainer, length=len(dct_tokens)
|
| 179 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
return cls(
|
| 182 |
+
PreTrainedTokenizerFast(tokenizer_object=tokenizer, clean_up_tokenization_spaces=False),
|
| 183 |
scale=scale,
|
| 184 |
vocab_size=vocab_size,
|
| 185 |
min_token=min_token,
|
processor_config.json
CHANGED
|
@@ -3,9 +3,9 @@
|
|
| 3 |
"auto_map": {
|
| 4 |
"AutoProcessor": "processing_action_tokenizer.UniversalActionProcessor"
|
| 5 |
},
|
| 6 |
-
"min_token": -
|
| 7 |
"processor_class": "UniversalActionProcessor",
|
| 8 |
-
"scale":
|
| 9 |
"time_horizon": null,
|
| 10 |
"vocab_size": 2048
|
| 11 |
}
|
|
|
|
| 3 |
"auto_map": {
|
| 4 |
"AutoProcessor": "processing_action_tokenizer.UniversalActionProcessor"
|
| 5 |
},
|
| 6 |
+
"min_token": -71,
|
| 7 |
"processor_class": "UniversalActionProcessor",
|
| 8 |
+
"scale": 10.0,
|
| 9 |
"time_horizon": null,
|
| 10 |
"vocab_size": 2048
|
| 11 |
}
|
tokenizer.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|