observation_tokenizer
observation_tokenizer
¶
Observation tokenizer for creating unified prompts from multiple observation keys.
This tokenizer: 1. Takes multiple observation keys (language, proprio, gripper, etc.) 2. Optionally bins continuous data into quantiles 3. Creates a unified prompt string (e.g., "Task: grasp needle, State in robot frame: 127 143 89") 4. Tokenizes the prompt using a language model tokenizer 5. Returns token IDs and padding masks
ObservationTokenizer
¶
ObservationTokenizer(tokenizer_model, observation_keys, bin_continuous_data=True, num_bins=256, max_token_len=256, device=None, raw_text=False, prompt_template=None, padding_strategy=value, trust_remote_code=False)
Tokenizes multiple observation keys into a unified prompt.
Creates prompts like: "Task: grasp needle, State in robot frame: 127 143 89, State in camera frame: 88 201 43"
All specified observation keys are combined into a single string, then tokenized.
Initialize observation tokenizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer_model
|
str
|
HuggingFace model name (e.g., "google/gemma-2b") |
required |
observation_keys
|
list[str]
|
List of observation keys to include in prompt (order preserved) |
required |
bin_continuous_data
|
bool
|
Whether to bin continuous data into quantiles |
True
|
num_bins
|
int
|
Number of bins for quantile-based discretization |
256
|
max_token_len
|
int
|
Maximum token sequence length |
256
|
device
|
device | None
|
Target device for tensors |
None
|
raw_text
|
bool
|
If True, pass language text through with only a trailing
newline appended (no |
False
|
prompt_template
|
str | None
|
Optional raw-text template with an
|
None
|
padding_strategy
|
str
|
HuggingFace padding strategy. |
value
|
trust_remote_code
|
bool
|
Whether to allow tokenizers that ship custom HuggingFace code (e.g. nvidia/llama-nemotron-embed). |
False
|
Source code in src/versatil/data/tokenization/observation_tokenizer.py
fit
¶
Fit binning tokenizers on observation data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observation_data
|
dict[str, ndarray]
|
Dict mapping obs keys to arrays of shape (N, ..., D) where N is number of samples. Language keys are skipped. |
required |
Source code in src/versatil/data/tokenization/observation_tokenizer.py
tokenize
¶
Tokenize observations into unified prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observations
|
dict[str, Any]
|
Dict with observation data. - Language keys: list[str], or list[list[str]] when batched - Continuous keys: torch.Tensor or np.ndarray, with a leading batch dimension when batched |
required |
batched
|
bool
|
Whether inputs carry a leading batch dimension in front of the observation horizon, producing per-timestep prompts reshaped back to (B, T, seq). |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dict with: - "tokens": Token IDs (B, max_token_len) - "is_pad_observation": Padding mask (B, max_token_len) |
Source code in src/versatil/data/tokenization/observation_tokenizer.py
to
¶
Move tokenizer to specified device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
Target device |
required |
Returns:
| Type | Description |
|---|---|
ObservationTokenizer
|
Self for chaining |
Source code in src/versatil/data/tokenization/observation_tokenizer.py
state_dict
¶
Get state dictionary for serialization.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing tokenizer state |
Source code in src/versatil/data/tokenization/observation_tokenizer.py
load_state_dict
¶
Load state dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
dict[str, Any]
|
State dictionary from state_dict() |
required |
Source code in src/versatil/data/tokenization/observation_tokenizer.py
save_pretrained
¶
Save tokenizer to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Directory path to save tokenizer |
required |
Source code in src/versatil/data/tokenization/observation_tokenizer.py
from_pretrained
classmethod
¶
Load tokenizer from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Directory path where tokenizer was saved |
required |
device
|
device | None
|
Target device for tensors |
None
|
Returns:
| Type | Description |
|---|---|
ObservationTokenizer
|
Loaded ObservationTokenizer instance |