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 +29 -47
- tokenizer.json +0 -0
processing_action_tokenizer.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
import logging
|
| 2 |
-
from sre_parse import Tokenizer
|
| 3 |
from typing import ClassVar
|
| 4 |
|
| 5 |
import numpy as np
|
| 6 |
-
from scipy.fft import dct
|
| 7 |
-
from
|
| 8 |
-
from tokenizers
|
| 9 |
from tokenizers.trainers import BpeTrainer
|
| 10 |
from transformers import PreTrainedTokenizerFast
|
| 11 |
from transformers.processing_utils import ProcessorMixin
|
|
@@ -42,9 +41,7 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 42 |
super().__init__(bpe_tokenizer)
|
| 43 |
|
| 44 |
def __call__(self, action_chunk: np.array) -> np.array:
|
| 45 |
-
assert action_chunk.ndim <= 3,
|
| 46 |
-
"Only 3 dimensions supported: [batch, timesteps, action_dim]"
|
| 47 |
-
)
|
| 48 |
if action_chunk.ndim == 2:
|
| 49 |
action_chunk = action_chunk[None, ...]
|
| 50 |
|
|
@@ -56,9 +53,7 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 56 |
dct_coeff = np.around(dct_coeff * self.scale)
|
| 57 |
tokens = []
|
| 58 |
for elem in dct_coeff:
|
| 59 |
-
token_str = "".join(
|
| 60 |
-
map(chr, np.maximum(elem.flatten() - self.min_token, 0).astype(int))
|
| 61 |
-
)
|
| 62 |
tokens.append(self.bpe_tokenizer(token_str)["input_ids"])
|
| 63 |
return tokens
|
| 64 |
|
|
@@ -69,40 +64,35 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 69 |
time_horizon: int | None = None,
|
| 70 |
action_dim: int | None = None,
|
| 71 |
) -> np.array:
|
| 72 |
-
self.time_horizon =
|
| 73 |
-
time_horizon or self.time_horizon or self.called_time_horizon
|
| 74 |
-
)
|
| 75 |
self.action_dim = action_dim or self.action_dim or self.called_action_dim
|
| 76 |
|
| 77 |
# Cache the time horizon and action dimension for the next call
|
| 78 |
self.called_time_horizon = self.time_horizon
|
| 79 |
self.called_action_dim = self.action_dim
|
| 80 |
|
| 81 |
-
assert
|
| 82 |
-
|
| 83 |
-
)
|
| 84 |
|
| 85 |
decoded_actions = []
|
| 86 |
for token in tokens:
|
| 87 |
try:
|
| 88 |
decoded_tokens = self.bpe_tokenizer.decode(token)
|
| 89 |
-
decoded_dct_coeff = (
|
| 90 |
-
np.array(list(map(ord, decoded_tokens))) + self.min_token
|
| 91 |
-
)
|
| 92 |
decoded_dct_coeff = decoded_dct_coeff.reshape(-1, self.action_dim)
|
| 93 |
-
assert
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
print(f"Error decoding tokens: {e}")
|
| 101 |
print(f"Tokens: {token}")
|
| 102 |
decoded_dct_coeff = np.zeros((self.time_horizon, self.action_dim))
|
| 103 |
-
decoded_actions.append(
|
| 104 |
-
idct(decoded_dct_coeff / self.scale, axis=0, norm="ortho")
|
| 105 |
-
)
|
| 106 |
return np.stack(decoded_actions)
|
| 107 |
|
| 108 |
@classmethod
|
|
@@ -122,13 +112,10 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 122 |
max_token = int(np.around(np.concatenate(dct_tokens) * scale).max())
|
| 123 |
min_token = int(np.around(np.concatenate(dct_tokens) * scale).min())
|
| 124 |
min_vocab_size = max_token - min_token
|
| 125 |
-
print(
|
| 126 |
-
f"Min token: {min_token}, Max token: {max_token}, Min vocab size: {min_vocab_size}"
|
| 127 |
-
)
|
| 128 |
|
| 129 |
-
assert
|
| 130 |
-
|
| 131 |
-
)
|
| 132 |
if min_vocab_size + 100 > vocab_size:
|
| 133 |
logging.warning(
|
| 134 |
f"Initial alphabet size {min_vocab_size} is almost as large as the vocab"
|
|
@@ -144,10 +131,7 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 144 |
yield string
|
| 145 |
|
| 146 |
# Train BPE tokenizer
|
| 147 |
-
|
| 148 |
-
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=False, use_regex=False)
|
| 149 |
-
tokenizer.decoder = decoders.ByteLevel()
|
| 150 |
-
tokenizer.post_processor = processors.ByteLevel(trim_offsets=False)
|
| 151 |
|
| 152 |
# Set up the entire range of possible tokens as the initial alphabet
|
| 153 |
alphabet = [chr(i) for i in range(max_token - min_token + 1)]
|
|
@@ -156,18 +140,16 @@ class UniversalActionProcessor(ProcessorMixin):
|
|
| 156 |
min_frequency=2,
|
| 157 |
show_progress=True,
|
| 158 |
special_tokens=[],
|
| 159 |
-
initial_alphabet=
|
| 160 |
-
|
| 161 |
-
max_token_length=256,
|
| 162 |
-
)
|
| 163 |
-
tokenizer.train_from_iterator(
|
| 164 |
-
_token_iter(), trainer=trainer, length=len(dct_tokens)
|
| 165 |
)
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
return cls(
|
| 168 |
-
PreTrainedTokenizerFast(
|
| 169 |
-
tokenizer_object=tokenizer, clean_up_tokenization_spaces=False
|
| 170 |
-
),
|
| 171 |
scale=scale,
|
| 172 |
vocab_size=vocab_size,
|
| 173 |
min_token=min_token,
|
|
|
|
| 1 |
import logging
|
|
|
|
| 2 |
from typing import ClassVar
|
| 3 |
|
| 4 |
import numpy as np
|
| 5 |
+
from scipy.fft import dct
|
| 6 |
+
from scipy.fft import idct
|
| 7 |
+
from tokenizers import ByteLevelBPETokenizer
|
| 8 |
from tokenizers.trainers import BpeTrainer
|
| 9 |
from transformers import PreTrainedTokenizerFast
|
| 10 |
from transformers.processing_utils import ProcessorMixin
|
|
|
|
| 41 |
super().__init__(bpe_tokenizer)
|
| 42 |
|
| 43 |
def __call__(self, action_chunk: np.array) -> np.array:
|
| 44 |
+
assert action_chunk.ndim <= 3, "Only 3 dimensions supported: [batch, timesteps, action_dim]"
|
|
|
|
|
|
|
| 45 |
if action_chunk.ndim == 2:
|
| 46 |
action_chunk = action_chunk[None, ...]
|
| 47 |
|
|
|
|
| 53 |
dct_coeff = np.around(dct_coeff * self.scale)
|
| 54 |
tokens = []
|
| 55 |
for elem in dct_coeff:
|
| 56 |
+
token_str = "".join(map(chr, np.maximum(elem.flatten() - self.min_token, 0).astype(int)))
|
|
|
|
|
|
|
| 57 |
tokens.append(self.bpe_tokenizer(token_str)["input_ids"])
|
| 58 |
return tokens
|
| 59 |
|
|
|
|
| 64 |
time_horizon: int | None = None,
|
| 65 |
action_dim: int | None = None,
|
| 66 |
) -> np.array:
|
| 67 |
+
self.time_horizon = time_horizon or self.time_horizon or self.called_time_horizon
|
|
|
|
|
|
|
| 68 |
self.action_dim = action_dim or self.action_dim or self.called_action_dim
|
| 69 |
|
| 70 |
# Cache the time horizon and action dimension for the next call
|
| 71 |
self.called_time_horizon = self.time_horizon
|
| 72 |
self.called_action_dim = self.action_dim
|
| 73 |
|
| 74 |
+
assert (
|
| 75 |
+
self.time_horizon is not None and self.action_dim is not None
|
| 76 |
+
), "Tokenizer not initialized, call encode() once or pass in time_horizon and action_dim."
|
| 77 |
|
| 78 |
decoded_actions = []
|
| 79 |
for token in tokens:
|
| 80 |
try:
|
| 81 |
decoded_tokens = self.bpe_tokenizer.decode(token)
|
| 82 |
+
decoded_dct_coeff = np.array(list(map(ord, decoded_tokens))) + self.min_token
|
|
|
|
|
|
|
| 83 |
decoded_dct_coeff = decoded_dct_coeff.reshape(-1, self.action_dim)
|
| 84 |
+
assert (
|
| 85 |
+
decoded_dct_coeff.shape
|
| 86 |
+
== (
|
| 87 |
+
self.time_horizon,
|
| 88 |
+
self.action_dim,
|
| 89 |
+
)
|
| 90 |
+
), f"Decoded DCT coefficients have shape {decoded_dct_coeff.shape}, expected ({self.time_horizon}, {self.action_dim})"
|
| 91 |
except Exception as e:
|
| 92 |
print(f"Error decoding tokens: {e}")
|
| 93 |
print(f"Tokens: {token}")
|
| 94 |
decoded_dct_coeff = np.zeros((self.time_horizon, self.action_dim))
|
| 95 |
+
decoded_actions.append(idct(decoded_dct_coeff / self.scale, axis=0, norm="ortho"))
|
|
|
|
|
|
|
| 96 |
return np.stack(decoded_actions)
|
| 97 |
|
| 98 |
@classmethod
|
|
|
|
| 112 |
max_token = int(np.around(np.concatenate(dct_tokens) * scale).max())
|
| 113 |
min_token = int(np.around(np.concatenate(dct_tokens) * scale).min())
|
| 114 |
min_vocab_size = max_token - min_token
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
assert (
|
| 117 |
+
min_vocab_size <= vocab_size
|
| 118 |
+
), f"Vocab size {vocab_size} is too small for the range of tokens {min_vocab_size}"
|
| 119 |
if min_vocab_size + 100 > vocab_size:
|
| 120 |
logging.warning(
|
| 121 |
f"Initial alphabet size {min_vocab_size} is almost as large as the vocab"
|
|
|
|
| 131 |
yield string
|
| 132 |
|
| 133 |
# Train BPE tokenizer
|
| 134 |
+
bpe = ByteLevelBPETokenizer()
|
|
|
|
|
|
|
|
|
|
| 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 |
min_frequency=2,
|
| 141 |
show_progress=True,
|
| 142 |
special_tokens=[],
|
| 143 |
+
initial_alphabet=alphabet,
|
| 144 |
+
max_token_length=10000,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=bpe, clean_up_tokenization_spaces=False),
|
|
|
|
|
|
|
| 153 |
scale=scale,
|
| 154 |
vocab_size=vocab_size,
|
| 155 |
min_token=min_token,
|
tokenizer.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|