File size: 9,653 Bytes
de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 393ff84 de729bb 29318fb de729bb 393ff84 de729bb 393ff84 de729bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | import logging
from typing import ClassVar, List, Optional
import numpy as np
import pywt
from tokenizers import ByteLevelBPETokenizer
from tokenizers.trainers import BpeTrainer
from transformers import PreTrainedTokenizerFast
from transformers.processing_utils import ProcessorMixin
class WaveletActionProcessor(ProcessorMixin):
attributes: ClassVar[list[str]] = ["bpe_tokenizer"]
bpe_tokenizer_class: str = "AutoTokenizer"
def __init__(
self,
bpe_tokenizer: PreTrainedTokenizerFast,
wavelet: str = "db1",
level: int = 2,
scale: float = 10.0,
min_token: int = 0,
*,
action_dim: Optional[int] = None,
time_horizon: Optional[int] = None,
):
self.wavelet = wavelet
self.level = level
self.scale = scale
self.min_token = int(min_token)
# Used for decode (same logic as FAST)
self.time_horizon = time_horizon
self.action_dim = action_dim
self.called_time_horizon = time_horizon
self.called_action_dim = action_dim
# Cache wavelet coefficient layout needed for decoding
# We keep one slice-structure per dimension (they are typically identical for fixed T/wavelet/level)
self._coeff_slices_per_dim = None # list of slice dicts
self._n_coeff = None # number of wavelet coeffs per dim after coeffs_to_array
super().__init__(bpe_tokenizer)
def _ensure_coeff_layout(self, T: int, D: int):
"""Cache coeff slices and coeff vector length for given (T, wavelet, level)."""
if (
self._coeff_slices_per_dim is not None
and self._n_coeff is not None
and self.called_time_horizon == T
and self.called_action_dim == D
):
return
dummy = np.zeros(T, dtype=np.float32)
slices_per_dim = []
n_coeff = None
for _ in range(D):
coeffs = pywt.wavedec(dummy, self.wavelet, level=self.level)
arr, slc = pywt.coeffs_to_array(coeffs)
slices_per_dim.append(slc)
if n_coeff is None:
n_coeff = int(arr.shape[0])
self._coeff_slices_per_dim = slices_per_dim
self._n_coeff = n_coeff
def __call__(self, action_chunk: np.ndarray) -> List[List[int]]:
"""
Encode actions to BPE tokens.
action_chunk: (T,D) or (B,T,D)
returns: List[List[int]] (batch of token id lists)
"""
assert action_chunk.ndim <= 3, "Only 3 dimensions supported: [batch, timesteps, action_dim]"
if action_chunk.ndim == 2:
action_chunk = action_chunk[None, ...]
B, T, D = action_chunk.shape
# cache for decoding
self.called_time_horizon, self.called_action_dim = T, D
self._ensure_coeff_layout(T, D)
batch_tokens: List[List[int]] = []
for i in range(B):
# wavelet per dim -> flattened coeffs of length (n_coeff * D)
coeffs_by_dim = []
for d in range(D):
coeffs = pywt.wavedec(action_chunk[i, :, d], self.wavelet, level=self.level)
flat, _ = pywt.coeffs_to_array(coeffs) # shape (n_coeff,)
coeffs_by_dim.append(flat)
coeff_mat = np.stack(coeffs_by_dim, axis=1) # (n_coeff, D)
flat_all = coeff_mat.reshape(-1) # (n_coeff * D,)
quant = np.around(flat_all * self.scale).astype(int)
shifted = (quant - self.min_token).astype(int)
# Optional safety check (unicode range). Keep it simple:
if shifted.min() < 0:
# This means min_token was not low enough for these coeffs.
raise ValueError(
f"Shifted tokens became negative (min={shifted.min()}). "
f"Your min_token={self.min_token} is too high. Re-fit or lower min_token."
)
if shifted.max() > 0x10FFFF:
raise ValueError(
f"Shifted tokens exceed Unicode max (max={shifted.max()}). "
f"Reduce scale or re-fit min/max range."
)
token_str = "".join(chr(int(x)) for x in shifted)
batch_tokens.append(self.bpe_tokenizer(token_str)["input_ids"])
return batch_tokens
def decode(
self,
tokens: List[List[int]],
*,
time_horizon: Optional[int] = None,
action_dim: Optional[int] = None,
) -> np.ndarray:
"""
Decode BPE tokens back to actions.
tokens: List[List[int]] (batch)
returns: (B, T, D)
"""
T = time_horizon or self.time_horizon or self.called_time_horizon
D = action_dim or self.action_dim or self.called_action_dim
assert T is not None and D is not None, (
"Tokenizer not initialized: call encode() once or pass time_horizon and action_dim."
)
# cache for next call + ensure layout
self.time_horizon, self.action_dim = T, D
self.called_time_horizon, self.called_action_dim = T, D
self._ensure_coeff_layout(T, D)
decoded_actions = []
for tok_list in tokens:
# decode to string of chars
s = self.bpe_tokenizer.decode(tok_list, clean_up_tokenization_spaces=False)
ints = np.array([ord(c) for c in s], dtype=np.int64)
# unshift + dequantize
quant = ints + self.min_token
flat_coeffs = quant.astype(np.float32) / self.scale # (n_coeff * D,)
# reshape to (n_coeff, D)
expected = self._n_coeff * D
if flat_coeffs.shape[0] != expected:
raise ValueError(
f"Decoded coeff length mismatch: got {flat_coeffs.shape[0]}, expected {expected}. "
f"(T={T}, D={D}, n_coeff={self._n_coeff}). "
"This usually means you decoded with different T/D than encoding."
)
coeff_mat = flat_coeffs.reshape(self._n_coeff, D)
# inverse wavelet per dimension
recon = np.zeros((T, D), dtype=np.float32)
for d in range(D):
arr = coeff_mat[:, d]
coeff_list = pywt.array_to_coeffs(
arr,
self._coeff_slices_per_dim[d],
output_format="wavedec",
)
sig = pywt.waverec(coeff_list, self.wavelet)
recon[:, d] = sig[:T] # waverec can return a bit longer due to padding
decoded_actions.append(recon)
return np.stack(decoded_actions, axis=0)
@classmethod
def fit(
cls,
action_data: List[np.ndarray], # each (T,D)
wavelet: str = "db1",
level: int = 2,
scale: float = 10.0,
vocab_size: int = 1024,
*,
time_horizon: Optional[int] = None,
action_dim: Optional[int] = None,
) -> "WaveletActionProcessor":
"""
Fit BPE tokenizer on wavelet-quantized coefficient streams.
"""
# Compute quantized coefficient streams to estimate min/max token range
all_streams = []
for a in action_data:
assert a.ndim == 2, "Each item must be (T,D)"
T, D = a.shape
# wavelet per dim -> flatten (n_coeff * D)
coeffs_by_dim = []
for d in range(D):
coeffs = pywt.wavedec(a[:, d], wavelet, level=level)
flat, _ = pywt.coeffs_to_array(coeffs)
coeffs_by_dim.append(flat)
coeff_mat = np.stack(coeffs_by_dim, axis=1)
stream = np.around(coeff_mat.reshape(-1) * scale).astype(int)
all_streams.append(stream)
all_vals = np.concatenate(all_streams)
min_token = int(all_vals.min())
max_token = int(all_vals.max())
token_range = max_token - min_token + 1
if token_range > vocab_size:
raise ValueError(
f"Vocab size {vocab_size} too small for token range {token_range}. "
"Increase vocab_size or reduce scale."
)
if token_range + 100 > vocab_size:
logging.warning(
f"Initial alphabet size {token_range} is close to vocab_size {vocab_size}. "
"Consider increasing vocab_size for better BPE merges."
)
def _token_iter():
for stream in all_streams:
shifted = (stream - min_token).astype(int)
# no clamp; must be >=0
yield "".join(chr(int(x)) for x in shifted)
# Train BPE
bpe = ByteLevelBPETokenizer()
alphabet = [chr(i) for i in range(token_range)]
trainer = BpeTrainer(
vocab_size=vocab_size,
min_frequency=2,
show_progress=True,
special_tokens=[],
initial_alphabet=alphabet,
max_token_length=10000,
)
bpe._tokenizer.train_from_iterator(_token_iter(), trainer=trainer)
# infer T/D defaults if not provided
if time_horizon is None:
time_horizon = int(action_data[0].shape[0])
if action_dim is None:
action_dim = int(action_data[0].shape[1])
return cls(
PreTrainedTokenizerFast(tokenizer_object=bpe, clean_up_tokenization_spaces=False),
wavelet=wavelet,
level=level,
scale=scale,
min_token=min_token,
time_horizon=time_horizon,
action_dim=action_dim,
)
|