Upload processor
Browse files- README.md +199 -0
- processing_action_tokenizer.py +158 -0
- processor_config.json +11 -0
- special_tokens_map.json +1 -0
- tokenizer.json +0 -0
- tokenizer_config.json +10 -0
README.md
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
library_name: transformers
|
3 |
+
tags: []
|
4 |
+
---
|
5 |
+
|
6 |
+
# Model Card for Model ID
|
7 |
+
|
8 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
## Model Details
|
13 |
+
|
14 |
+
### Model Description
|
15 |
+
|
16 |
+
<!-- Provide a longer summary of what this model is. -->
|
17 |
+
|
18 |
+
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
19 |
+
|
20 |
+
- **Developed by:** [More Information Needed]
|
21 |
+
- **Funded by [optional]:** [More Information Needed]
|
22 |
+
- **Shared by [optional]:** [More Information Needed]
|
23 |
+
- **Model type:** [More Information Needed]
|
24 |
+
- **Language(s) (NLP):** [More Information Needed]
|
25 |
+
- **License:** [More Information Needed]
|
26 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
27 |
+
|
28 |
+
### Model Sources [optional]
|
29 |
+
|
30 |
+
<!-- Provide the basic links for the model. -->
|
31 |
+
|
32 |
+
- **Repository:** [More Information Needed]
|
33 |
+
- **Paper [optional]:** [More Information Needed]
|
34 |
+
- **Demo [optional]:** [More Information Needed]
|
35 |
+
|
36 |
+
## Uses
|
37 |
+
|
38 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
39 |
+
|
40 |
+
### Direct Use
|
41 |
+
|
42 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
43 |
+
|
44 |
+
[More Information Needed]
|
45 |
+
|
46 |
+
### Downstream Use [optional]
|
47 |
+
|
48 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
49 |
+
|
50 |
+
[More Information Needed]
|
51 |
+
|
52 |
+
### Out-of-Scope Use
|
53 |
+
|
54 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
55 |
+
|
56 |
+
[More Information Needed]
|
57 |
+
|
58 |
+
## Bias, Risks, and Limitations
|
59 |
+
|
60 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
61 |
+
|
62 |
+
[More Information Needed]
|
63 |
+
|
64 |
+
### Recommendations
|
65 |
+
|
66 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
67 |
+
|
68 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
69 |
+
|
70 |
+
## How to Get Started with the Model
|
71 |
+
|
72 |
+
Use the code below to get started with the model.
|
73 |
+
|
74 |
+
[More Information Needed]
|
75 |
+
|
76 |
+
## Training Details
|
77 |
+
|
78 |
+
### Training Data
|
79 |
+
|
80 |
+
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
81 |
+
|
82 |
+
[More Information Needed]
|
83 |
+
|
84 |
+
### Training Procedure
|
85 |
+
|
86 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
87 |
+
|
88 |
+
#### Preprocessing [optional]
|
89 |
+
|
90 |
+
[More Information Needed]
|
91 |
+
|
92 |
+
|
93 |
+
#### Training Hyperparameters
|
94 |
+
|
95 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
96 |
+
|
97 |
+
#### Speeds, Sizes, Times [optional]
|
98 |
+
|
99 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
100 |
+
|
101 |
+
[More Information Needed]
|
102 |
+
|
103 |
+
## Evaluation
|
104 |
+
|
105 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
106 |
+
|
107 |
+
### Testing Data, Factors & Metrics
|
108 |
+
|
109 |
+
#### Testing Data
|
110 |
+
|
111 |
+
<!-- This should link to a Dataset Card if possible. -->
|
112 |
+
|
113 |
+
[More Information Needed]
|
114 |
+
|
115 |
+
#### Factors
|
116 |
+
|
117 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
118 |
+
|
119 |
+
[More Information Needed]
|
120 |
+
|
121 |
+
#### Metrics
|
122 |
+
|
123 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
124 |
+
|
125 |
+
[More Information Needed]
|
126 |
+
|
127 |
+
### Results
|
128 |
+
|
129 |
+
[More Information Needed]
|
130 |
+
|
131 |
+
#### Summary
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
## Model Examination [optional]
|
136 |
+
|
137 |
+
<!-- Relevant interpretability work for the model goes here -->
|
138 |
+
|
139 |
+
[More Information Needed]
|
140 |
+
|
141 |
+
## Environmental Impact
|
142 |
+
|
143 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
144 |
+
|
145 |
+
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
146 |
+
|
147 |
+
- **Hardware Type:** [More Information Needed]
|
148 |
+
- **Hours used:** [More Information Needed]
|
149 |
+
- **Cloud Provider:** [More Information Needed]
|
150 |
+
- **Compute Region:** [More Information Needed]
|
151 |
+
- **Carbon Emitted:** [More Information Needed]
|
152 |
+
|
153 |
+
## Technical Specifications [optional]
|
154 |
+
|
155 |
+
### Model Architecture and Objective
|
156 |
+
|
157 |
+
[More Information Needed]
|
158 |
+
|
159 |
+
### Compute Infrastructure
|
160 |
+
|
161 |
+
[More Information Needed]
|
162 |
+
|
163 |
+
#### Hardware
|
164 |
+
|
165 |
+
[More Information Needed]
|
166 |
+
|
167 |
+
#### Software
|
168 |
+
|
169 |
+
[More Information Needed]
|
170 |
+
|
171 |
+
## Citation [optional]
|
172 |
+
|
173 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
174 |
+
|
175 |
+
**BibTeX:**
|
176 |
+
|
177 |
+
[More Information Needed]
|
178 |
+
|
179 |
+
**APA:**
|
180 |
+
|
181 |
+
[More Information Needed]
|
182 |
+
|
183 |
+
## Glossary [optional]
|
184 |
+
|
185 |
+
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
186 |
+
|
187 |
+
[More Information Needed]
|
188 |
+
|
189 |
+
## More Information [optional]
|
190 |
+
|
191 |
+
[More Information Needed]
|
192 |
+
|
193 |
+
## Model Card Authors [optional]
|
194 |
+
|
195 |
+
[More Information Needed]
|
196 |
+
|
197 |
+
## Model Card Contact
|
198 |
+
|
199 |
+
[More Information Needed]
|
processing_action_tokenizer.py
ADDED
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
11 |
+
|
12 |
+
|
13 |
+
class UniversalActionProcessor(ProcessorMixin):
|
14 |
+
attributes: ClassVar[list[str]] = ["bpe_tokenizer"]
|
15 |
+
bpe_tokenizer_class: str = "AutoTokenizer"
|
16 |
+
|
17 |
+
def __init__(
|
18 |
+
self,
|
19 |
+
bpe_tokenizer: PreTrainedTokenizerFast,
|
20 |
+
scale: float = 10,
|
21 |
+
vocab_size: int = 1024,
|
22 |
+
min_token: int = 0,
|
23 |
+
*,
|
24 |
+
action_dim: int | None = None,
|
25 |
+
time_horizon: int | None = None,
|
26 |
+
):
|
27 |
+
self.scale = scale
|
28 |
+
self.vocab_size = vocab_size
|
29 |
+
self.min_token = min_token
|
30 |
+
|
31 |
+
# Action horizon and dimension needed during decoding. These can be specified
|
32 |
+
# in three ways (in order of priority):
|
33 |
+
# 1. passed in as kwargs to decode()
|
34 |
+
# 2. in the constructor
|
35 |
+
# 3. cached from the last time decode() was called
|
36 |
+
self.time_horizon = time_horizon
|
37 |
+
self.action_dim = action_dim
|
38 |
+
self.called_time_horizon = time_horizon
|
39 |
+
self.called_action_dim = action_dim
|
40 |
+
|
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 |
+
|
48 |
+
# Cache the time horizon and action dimension for decoding
|
49 |
+
self.called_time_horizon = action_chunk.shape[-2]
|
50 |
+
self.called_action_dim = action_chunk.shape[-1]
|
51 |
+
|
52 |
+
dct_coeff = dct(action_chunk, axis=1, norm="ortho")
|
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 |
+
|
60 |
+
def decode(
|
61 |
+
self,
|
62 |
+
tokens: list[list[int]],
|
63 |
+
*,
|
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
|
99 |
+
def fit(
|
100 |
+
cls,
|
101 |
+
action_data: list[np.array],
|
102 |
+
scale: float = 10,
|
103 |
+
vocab_size: int = 1024,
|
104 |
+
*,
|
105 |
+
time_horizon: int | None = None,
|
106 |
+
action_dim: int | None = None,
|
107 |
+
) -> "UniversalActionProcessor":
|
108 |
+
# Run DCT over all inputs
|
109 |
+
dct_tokens = [dct(a, axis=0, norm="ortho").flatten() for a in action_data]
|
110 |
+
|
111 |
+
# Quantize and find min token
|
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"
|
122 |
+
f"size {vocab_size}, consider increasing vocab size"
|
123 |
+
)
|
124 |
+
|
125 |
+
# Make token iterator for BPE training
|
126 |
+
def _token_iter():
|
127 |
+
for tokens in dct_tokens:
|
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 |
+
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)]
|
138 |
+
trainer = BpeTrainer(
|
139 |
+
vocab_size=vocab_size,
|
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,
|
156 |
+
time_horizon=time_horizon,
|
157 |
+
action_dim=action_dim,
|
158 |
+
)
|
processor_config.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"action_dim": null,
|
3 |
+
"auto_map": {
|
4 |
+
"AutoProcessor": "processing_action_tokenizer.UniversalActionProcessor"
|
5 |
+
},
|
6 |
+
"min_token": -354,
|
7 |
+
"processor_class": "UniversalActionProcessor",
|
8 |
+
"scale": 10,
|
9 |
+
"time_horizon": null,
|
10 |
+
"vocab_size": 2048
|
11 |
+
}
|
special_tokens_map.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"added_tokens_decoder": {},
|
3 |
+
"auto_map": {
|
4 |
+
"AutoProcessor": "processing_action_tokenizer.UniversalActionProcessor"
|
5 |
+
},
|
6 |
+
"clean_up_tokenization_spaces": true,
|
7 |
+
"model_max_length": 1000000000000000019884624838656,
|
8 |
+
"processor_class": "UniversalActionProcessor",
|
9 |
+
"tokenizer_class": "PreTrainedTokenizerFast"
|
10 |
+
}
|