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 +15 -25
- processor_config.json +1 -1
- tokenizer.json +0 -0
processing_action_tokenizer.py
CHANGED
|
@@ -9,6 +9,17 @@ 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"]
|
|
@@ -56,7 +67,7 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 56 |
tokens = []
|
| 57 |
for elem in dct_coeff:
|
| 58 |
token_str = "".join(
|
| 59 |
-
map(
|
| 60 |
)
|
| 61 |
tokens.append(self.bpe_tokenizer(token_str)["input_ids"])
|
| 62 |
return tokens
|
|
@@ -86,7 +97,7 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 86 |
try:
|
| 87 |
decoded_tokens = self.bpe_tokenizer.decode(token)
|
| 88 |
decoded_dct_coeff = (
|
| 89 |
-
np.array(list(map(
|
| 90 |
)
|
| 91 |
decoded_dct_coeff = decoded_dct_coeff.reshape(-1, self.action_dim)
|
| 92 |
assert decoded_dct_coeff.shape == (
|
|
@@ -148,27 +159,9 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 148 |
tokens = dct_tokens.pop()
|
| 149 |
rounded_tokens = np.around(tokens * scale) - min_token
|
| 150 |
rounded_tokens = rounded_tokens.astype(int)
|
| 151 |
-
string = "".join(map(
|
| 152 |
yield string
|
| 153 |
|
| 154 |
-
# # Train BPE tokenizer
|
| 155 |
-
# bpe = ByteLevelBPETokenizer()
|
| 156 |
-
|
| 157 |
-
# # Set up the entire range of possible tokens as the initial alphabet
|
| 158 |
-
# alphabet = [chr(i) for i in range(max_token - min_token + 1)]
|
| 159 |
-
# trainer = BpeTrainer(
|
| 160 |
-
# vocab_size=vocab_size,
|
| 161 |
-
# min_frequency=2,
|
| 162 |
-
# show_progress=True,
|
| 163 |
-
# special_tokens=[],
|
| 164 |
-
# initial_alphabet=alphabet,
|
| 165 |
-
# max_token_length=10000,
|
| 166 |
-
# )
|
| 167 |
-
|
| 168 |
-
# # Train the inner tokenizer (don't use ByteLevelBPETokenizer.train_from_iterator()
|
| 169 |
-
# # because it doesn't support custom alphabets)
|
| 170 |
-
# bpe._tokenizer.train_from_iterator(_token_iter(), trainer=trainer)
|
| 171 |
-
|
| 172 |
# Train BPE tokenizer
|
| 173 |
tokenizer = Tokenizer(BPE())
|
| 174 |
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(
|
|
@@ -181,15 +174,12 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 181 |
add_prefix_space=False, trim_offsets=False, use_regex=False
|
| 182 |
)
|
| 183 |
|
| 184 |
-
# Set up the entire range of possible tokens as the initial alphabet
|
| 185 |
-
alphabet = [chr(i) for i in range(256)]
|
| 186 |
trainer = BpeTrainer(
|
| 187 |
vocab_size=vocab_size,
|
| 188 |
min_frequency=2,
|
| 189 |
show_progress=True,
|
| 190 |
special_tokens=[],
|
| 191 |
-
|
| 192 |
-
initial_alphabet=alphabet,
|
| 193 |
# max_token_length=256,
|
| 194 |
max_token_length=10_000,
|
| 195 |
)
|
|
|
|
| 9 |
from transformers import PreTrainedTokenizerFast
|
| 10 |
from transformers.processing_utils import ProcessorMixin
|
| 11 |
|
| 12 |
+
ALPHABET = pre_tokenizers.ByteLevel.alphabet()
|
| 13 |
+
REVERSE_ALPHABET = {c: i for i, c in enumerate(ALPHABET)}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _chr(i: int) -> str:
|
| 17 |
+
return ALPHABET[i]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def _ord(c: str) -> int:
|
| 21 |
+
return REVERSE_ALPHABET[c]
|
| 22 |
+
|
| 23 |
|
| 24 |
class UniversalActionProcessor(ProcessorMixin):
|
| 25 |
attributes: ClassVar[list[str]] = ["bpe_tokenizer"]
|
|
|
|
| 67 |
tokens = []
|
| 68 |
for elem in dct_coeff:
|
| 69 |
token_str = "".join(
|
| 70 |
+
map(_chr, np.maximum(elem.flatten() - self.min_token, 0).astype(int))
|
| 71 |
)
|
| 72 |
tokens.append(self.bpe_tokenizer(token_str)["input_ids"])
|
| 73 |
return tokens
|
|
|
|
| 97 |
try:
|
| 98 |
decoded_tokens = self.bpe_tokenizer.decode(token)
|
| 99 |
decoded_dct_coeff = (
|
| 100 |
+
np.array(list(map(_ord, decoded_tokens))) + self.min_token
|
| 101 |
)
|
| 102 |
decoded_dct_coeff = decoded_dct_coeff.reshape(-1, self.action_dim)
|
| 103 |
assert decoded_dct_coeff.shape == (
|
|
|
|
| 159 |
tokens = dct_tokens.pop()
|
| 160 |
rounded_tokens = np.around(tokens * scale) - min_token
|
| 161 |
rounded_tokens = rounded_tokens.astype(int)
|
| 162 |
+
string = "".join(map(_chr, rounded_tokens))
|
| 163 |
yield string
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
# Train BPE tokenizer
|
| 166 |
tokenizer = Tokenizer(BPE())
|
| 167 |
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(
|
|
|
|
| 174 |
add_prefix_space=False, trim_offsets=False, use_regex=False
|
| 175 |
)
|
| 176 |
|
|
|
|
|
|
|
| 177 |
trainer = BpeTrainer(
|
| 178 |
vocab_size=vocab_size,
|
| 179 |
min_frequency=2,
|
| 180 |
show_progress=True,
|
| 181 |
special_tokens=[],
|
| 182 |
+
initial_alphabet=ALPHABET,
|
|
|
|
| 183 |
# max_token_length=256,
|
| 184 |
max_token_length=10_000,
|
| 185 |
)
|
processor_config.json
CHANGED
|
@@ -7,5 +7,5 @@
|
|
| 7 |
"processor_class": "UniversalActionProcessor",
|
| 8 |
"scale": 10.0,
|
| 9 |
"time_horizon": 50,
|
| 10 |
-
"vocab_size":
|
| 11 |
}
|
|
|
|
| 7 |
"processor_class": "UniversalActionProcessor",
|
| 8 |
"scale": 10.0,
|
| 9 |
"time_horizon": 50,
|
| 10 |
+
"vocab_size": 1024
|
| 11 |
}
|
tokenizer.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|