|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
This script defines a processor for tokenizing natural language instructions from an environment transition.
|
|
|
|
|
|
It uses a tokenizer from the Hugging Face `transformers` library to convert task descriptions (text) into
|
|
|
token IDs and attention masks, which are then added to the observation dictionary.
|
|
|
"""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
from dataclasses import dataclass, field
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
|
|
import torch
|
|
|
|
|
|
from lerobot.configs.types import FeatureType, PipelineFeatureType, PolicyFeature
|
|
|
from lerobot.utils.constants import OBS_LANGUAGE_ATTENTION_MASK, OBS_LANGUAGE_TOKENS
|
|
|
from lerobot.utils.import_utils import _transformers_available
|
|
|
|
|
|
from .core import EnvTransition, TransitionKey
|
|
|
from .pipeline import ObservationProcessorStep, ProcessorStepRegistry
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING or _transformers_available:
|
|
|
from transformers import AutoTokenizer
|
|
|
else:
|
|
|
AutoTokenizer = None
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
@ProcessorStepRegistry.register(name="tokenizer_processor")
|
|
|
class TokenizerProcessorStep(ObservationProcessorStep):
|
|
|
"""
|
|
|
Processor step to tokenize a natural language task description.
|
|
|
|
|
|
This step extracts a task string from the `complementary_data` of an `EnvTransition`,
|
|
|
tokenizes it using a Hugging Face `transformers` tokenizer, and adds the resulting
|
|
|
token IDs and attention mask to the `observation` dictionary.
|
|
|
|
|
|
Requires the `transformers` library to be installed.
|
|
|
|
|
|
Attributes:
|
|
|
tokenizer_name: The name of a pretrained tokenizer from the Hugging Face Hub (e.g., "bert-base-uncased").
|
|
|
tokenizer: A pre-initialized tokenizer object. If provided, `tokenizer_name` is ignored.
|
|
|
max_length: The maximum length to pad or truncate sequences to.
|
|
|
task_key: The key in `complementary_data` where the task string is stored.
|
|
|
padding_side: The side to pad on ('left' or 'right').
|
|
|
padding: The padding strategy ('max_length', 'longest', etc.).
|
|
|
truncation: Whether to truncate sequences longer than `max_length`.
|
|
|
input_tokenizer: The internal tokenizer instance, loaded during initialization.
|
|
|
"""
|
|
|
|
|
|
tokenizer_name: str | None = None
|
|
|
tokenizer: Any | None = None
|
|
|
max_length: int = 512
|
|
|
task_key: str = "task"
|
|
|
padding_side: str = "right"
|
|
|
padding: str = "max_length"
|
|
|
truncation: bool = True
|
|
|
|
|
|
|
|
|
input_tokenizer: Any = field(default=None, init=False, repr=False)
|
|
|
|
|
|
def __post_init__(self):
|
|
|
"""
|
|
|
Initializes the tokenizer after the dataclass is created.
|
|
|
|
|
|
It checks for the availability of the `transformers` library and loads the tokenizer
|
|
|
either from a provided object or by name from the Hugging Face Hub.
|
|
|
|
|
|
Raises:
|
|
|
ImportError: If the `transformers` library is not installed.
|
|
|
ValueError: If neither `tokenizer` nor `tokenizer_name` is provided.
|
|
|
"""
|
|
|
if not _transformers_available:
|
|
|
raise ImportError(
|
|
|
"The 'transformers' library is not installed. "
|
|
|
"Please install it with `pip install 'lerobot[transformers-dep]'` to use TokenizerProcessorStep."
|
|
|
)
|
|
|
|
|
|
if self.tokenizer is not None:
|
|
|
|
|
|
self.input_tokenizer = self.tokenizer
|
|
|
elif self.tokenizer_name is not None:
|
|
|
if AutoTokenizer is None:
|
|
|
raise ImportError("AutoTokenizer is not available")
|
|
|
self.input_tokenizer = AutoTokenizer.from_pretrained(self.tokenizer_name)
|
|
|
else:
|
|
|
raise ValueError(
|
|
|
"Either 'tokenizer' or 'tokenizer_name' must be provided. "
|
|
|
"Pass a tokenizer object directly or a tokenizer name to auto-load."
|
|
|
)
|
|
|
|
|
|
def get_task(self, transition: EnvTransition) -> list[str] | None:
|
|
|
"""
|
|
|
Extracts the task description(s) from the transition's complementary data.
|
|
|
|
|
|
Args:
|
|
|
transition: The environment transition.
|
|
|
|
|
|
Returns:
|
|
|
A list of task strings, or None if the task key is not found or the value is None.
|
|
|
"""
|
|
|
complementary_data = transition.get(TransitionKey.COMPLEMENTARY_DATA)
|
|
|
if complementary_data is None:
|
|
|
raise ValueError("Complementary data is None so no task can be extracted from it")
|
|
|
|
|
|
task = complementary_data[self.task_key]
|
|
|
if task is None:
|
|
|
raise ValueError("Task extracted from Complementary data is None")
|
|
|
|
|
|
|
|
|
if isinstance(task, str):
|
|
|
return [task]
|
|
|
elif isinstance(task, list) and all(isinstance(t, str) for t in task):
|
|
|
return task
|
|
|
|
|
|
return None
|
|
|
|
|
|
def observation(self, observation: dict[str, Any]) -> dict[str, Any]:
|
|
|
"""
|
|
|
Tokenizes the task description and adds it to the observation dictionary.
|
|
|
|
|
|
This method retrieves the task, tokenizes it, moves the resulting tensors to the
|
|
|
same device as other data in the transition, and updates the observation.
|
|
|
|
|
|
Args:
|
|
|
observation: The original observation dictionary.
|
|
|
|
|
|
Returns:
|
|
|
The updated observation dictionary including token IDs and an attention mask.
|
|
|
"""
|
|
|
task = self.get_task(self.transition)
|
|
|
if task is None:
|
|
|
raise ValueError("Task cannot be None")
|
|
|
|
|
|
|
|
|
tokenized_prompt = self._tokenize_text(task)
|
|
|
|
|
|
|
|
|
target_device = self._detect_device(self.transition)
|
|
|
|
|
|
|
|
|
if target_device is not None:
|
|
|
tokenized_prompt = {
|
|
|
k: v.to(target_device) if isinstance(v, torch.Tensor) else v
|
|
|
for k, v in tokenized_prompt.items()
|
|
|
}
|
|
|
|
|
|
|
|
|
new_observation = dict(observation)
|
|
|
|
|
|
|
|
|
new_observation[OBS_LANGUAGE_TOKENS] = tokenized_prompt["input_ids"]
|
|
|
new_observation[OBS_LANGUAGE_ATTENTION_MASK] = tokenized_prompt["attention_mask"].to(dtype=torch.bool)
|
|
|
|
|
|
return new_observation
|
|
|
|
|
|
def _detect_device(self, transition: EnvTransition) -> torch.device | None:
|
|
|
"""
|
|
|
Detects the torch.device from existing tensors in the transition.
|
|
|
|
|
|
It checks tensors in the observation dictionary first, then the action tensor.
|
|
|
|
|
|
Args:
|
|
|
transition: The environment transition.
|
|
|
|
|
|
Returns:
|
|
|
The detected `torch.device`, or None if no tensors are found.
|
|
|
"""
|
|
|
|
|
|
observation = transition.get(TransitionKey.OBSERVATION)
|
|
|
if observation:
|
|
|
for value in observation.values():
|
|
|
if isinstance(value, torch.Tensor):
|
|
|
return value.device
|
|
|
|
|
|
|
|
|
action = transition.get(TransitionKey.ACTION)
|
|
|
if isinstance(action, torch.Tensor):
|
|
|
return action.device
|
|
|
|
|
|
return None
|
|
|
|
|
|
def _tokenize_text(self, text: str | list[str]) -> dict[str, torch.Tensor]:
|
|
|
"""
|
|
|
A wrapper around the tokenizer call.
|
|
|
|
|
|
Args:
|
|
|
text: A string or list of strings to tokenize.
|
|
|
|
|
|
Returns:
|
|
|
A dictionary containing tokenized 'input_ids' and 'attention_mask' as PyTorch tensors.
|
|
|
"""
|
|
|
return self.input_tokenizer(
|
|
|
text,
|
|
|
max_length=self.max_length,
|
|
|
truncation=self.truncation,
|
|
|
padding=self.padding,
|
|
|
padding_side=self.padding_side,
|
|
|
return_tensors="pt",
|
|
|
)
|
|
|
|
|
|
def get_config(self) -> dict[str, Any]:
|
|
|
"""
|
|
|
Returns the serializable configuration of the processor.
|
|
|
|
|
|
Note: The tokenizer object itself is not serialized. If the processor was initialized
|
|
|
with a tokenizer name, that name will be included in the config.
|
|
|
|
|
|
Returns:
|
|
|
A dictionary with the processor's configuration parameters.
|
|
|
"""
|
|
|
config = {
|
|
|
"max_length": self.max_length,
|
|
|
"task_key": self.task_key,
|
|
|
"padding_side": self.padding_side,
|
|
|
"padding": self.padding,
|
|
|
"truncation": self.truncation,
|
|
|
}
|
|
|
|
|
|
|
|
|
if self.tokenizer_name is not None and self.tokenizer is None:
|
|
|
config["tokenizer_name"] = self.tokenizer_name
|
|
|
|
|
|
return config
|
|
|
|
|
|
def transform_features(
|
|
|
self, features: dict[PipelineFeatureType, dict[str, PolicyFeature]]
|
|
|
) -> dict[PipelineFeatureType, dict[str, PolicyFeature]]:
|
|
|
"""
|
|
|
Adds feature definitions for the language tokens and attention mask.
|
|
|
|
|
|
This updates the policy features dictionary to include the new data added to the
|
|
|
observation, ensuring downstream components are aware of their shape and type.
|
|
|
|
|
|
Args:
|
|
|
features: The dictionary of existing policy features.
|
|
|
|
|
|
Returns:
|
|
|
The updated dictionary of policy features.
|
|
|
"""
|
|
|
|
|
|
if OBS_LANGUAGE_TOKENS not in features[PipelineFeatureType.OBSERVATION]:
|
|
|
features[PipelineFeatureType.OBSERVATION][OBS_LANGUAGE_TOKENS] = PolicyFeature(
|
|
|
type=FeatureType.LANGUAGE, shape=(self.max_length,)
|
|
|
)
|
|
|
|
|
|
|
|
|
if OBS_LANGUAGE_ATTENTION_MASK not in features[PipelineFeatureType.OBSERVATION]:
|
|
|
features[PipelineFeatureType.OBSERVATION][OBS_LANGUAGE_ATTENTION_MASK] = PolicyFeature(
|
|
|
type=FeatureType.LANGUAGE, shape=(self.max_length,)
|
|
|
)
|
|
|
|
|
|
return features
|
|
|
|