processing_pi0.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/pi0/modular_pi0.py.
  3. # Do NOT edit this file manually as any edits will be overwritten by the generation of
  4. # the file from the modular. If any change should be done, please apply the change to the
  5. # modular_pi0.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2025 Physical Intelligence and The HuggingFace Inc. team. All rights reserved.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. import numpy as np
  21. import torch
  22. import torch.nn.functional as F
  23. from ...feature_extraction_utils import BatchFeature
  24. from ...image_utils import ImageInput, make_nested_list_of_images
  25. from ...processing_utils import MultiModalData, ProcessingKwargs, ProcessorMixin, Unpack
  26. from ...tokenization_utils_base import AddedToken, PreTokenizedInput, TextInput
  27. from ...utils import auto_docstring, logging
  28. from ...utils.import_utils import requires
  29. logger = logging.get_logger(__name__)
  30. class PI0ProcessorKwargs(ProcessingKwargs, total=False):
  31. _defaults = {
  32. "text_kwargs": {
  33. "padding": "max_length",
  34. "max_length": 48,
  35. "padding_side": "right",
  36. },
  37. "common_kwargs": {"return_tensors": "pt"},
  38. }
  39. IMAGE_TOKEN = "<image>"
  40. EXTRA_TOKENS = [f"<loc{i:0>4}>" for i in range(1024)] + [f"<seg{i:0>3}>" for i in range(128)]
  41. @auto_docstring
  42. @requires(backends=("vision", "torch"))
  43. class PI0Processor(ProcessorMixin):
  44. def __init__(self, image_processor=None, tokenizer=None, chat_template=None, **kwargs):
  45. self.height, self.width = image_processor.size["height"], image_processor.size["width"]
  46. state_mean = kwargs.get("state_mean", [-0.0419, 0.0354, 0.8257, 2.9083, -0.5562, -0.1665, 0.0283, -0.0286])
  47. state_std = kwargs.get("state_std", [0.1074, 0.1442, 0.2572, 0.3441, 1.2344, 0.3580, 0.0133, 0.0132])
  48. actions_mean = kwargs.get("actions_mean", [0.0182, 0.0586, -0.0559, 0.0046, 0.0029, -0.0077, -0.0916])
  49. actions_std = kwargs.get("actions_std", [0.2825, 0.3590, 0.3674, 0.0377, 0.0543, 0.0872, 0.9958])
  50. self.state_mean = torch.tensor(state_mean)
  51. self.state_std = torch.tensor(state_std)
  52. self.actions_mean = torch.tensor(actions_mean)
  53. self.actions_std = torch.tensor(actions_std)
  54. self.max_state_dim = kwargs.get("max_state_dim", 32)
  55. self.chunk_size = kwargs.get("chunk_size", 50)
  56. if not hasattr(image_processor, "image_seq_length"):
  57. raise ValueError("Image processor is missing an `image_seq_length` attribute.")
  58. self.image_seq_length = image_processor.image_seq_length
  59. if not hasattr(tokenizer, "image_token"):
  60. image_token = AddedToken(IMAGE_TOKEN, normalized=False, special=True)
  61. tokens_to_add = {"additional_special_tokens": [image_token]}
  62. tokenizer.add_special_tokens(tokens_to_add)
  63. self.image_token_id = tokenizer.convert_tokens_to_ids(IMAGE_TOKEN)
  64. self.image_token = IMAGE_TOKEN
  65. else:
  66. self.image_token_id = tokenizer.image_token_id
  67. self.image_token = tokenizer.image_token
  68. tokenizer.add_tokens(EXTRA_TOKENS)
  69. tokenizer.add_bos_token = False
  70. tokenizer.add_eos_token = False
  71. super().__init__(image_processor, tokenizer, chat_template=chat_template)
  72. @auto_docstring
  73. def __call__(
  74. self,
  75. images: ImageInput | list[ImageInput] | list[list[ImageInput]] | None,
  76. text: TextInput | PreTokenizedInput | list[TextInput] | list[PreTokenizedInput] | None = None,
  77. actions: list | np.ndarray | torch.Tensor | None = None,
  78. state: list | np.ndarray | torch.Tensor | None = None,
  79. **kwargs: Unpack[PI0ProcessorKwargs],
  80. ) -> BatchFeature:
  81. r"""
  82. actions (`list | np.ndarray | torch.Tensor`, *optional*):
  83. Actions to be predicted by the model. If provided, padding, mean and std normalization will be applied.
  84. state (`list | np.ndarray | torch.Tensor`, *optional*):
  85. Robotic states to be predicted by the model. If provided, padding, mean and std normalization will be applied.
  86. Returns:
  87. [`BatchFeature`]: A [`BatchFeature`] with the following fields:
  88. - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`. If `suffix`
  89. is provided, the `input_ids` will also contain the suffix input ids.
  90. - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
  91. `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
  92. `None`).
  93. - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
  94. - **pixel_attention_mask** -- Pixel values padding mask to be fed to a model. Returned when `images` is not `None`.
  95. - **state** -- Robot state compatible with model if `state` is not None
  96. - **actions** -- Label-actions compatible with training if `actions` is not None
  97. """
  98. output_kwargs = self._merge_kwargs(
  99. PI0ProcessorKwargs, tokenizer_init_kwargs=self.tokenizer.init_kwargs, **kwargs
  100. )
  101. if text is None:
  102. logger.warning_once("You are using PI0 without a text prefix. The processor will use an empty prompt.")
  103. text = ""
  104. if isinstance(text, str):
  105. text = [text]
  106. batched_images = make_nested_list_of_images(images)
  107. if len(batched_images) != len(text):
  108. raise ValueError(
  109. f"Received {len(batched_images)} image samples for {len(text)} prompts. "
  110. "Each prompt should be associated with one sample (with one or more camera images)."
  111. )
  112. return_tensors = output_kwargs["text_kwargs"].pop("return_tensors", None)
  113. output_kwargs["images_kwargs"].pop("return_tensors", None)
  114. prompt_strings = []
  115. for sample, image_list in zip(text, batched_images):
  116. sample = (
  117. f"{self.image_token * self.image_seq_length * len(image_list)}{self.tokenizer.bos_token}{sample}\n"
  118. )
  119. prompt_strings.append(sample)
  120. text_inputs = self.tokenizer(prompt_strings, **output_kwargs["text_kwargs"])
  121. # Here is the diff from PaliGemma. Ideally we'd create a new ImageProcessor if it were a VLM
  122. max_num_cameras = max(len(sample_images) for sample_images in batched_images)
  123. pixel_attention_mask = torch.zeros((len(batched_images), max_num_cameras), dtype=torch.bool)
  124. padded_pixel_values = torch.zeros(len(batched_images), max_num_cameras, 3, self.height, self.width)
  125. for batch, sample_images in enumerate(batched_images):
  126. processed = self.image_processor(sample_images, return_tensors="pt", **output_kwargs["images_kwargs"])
  127. num_cameras = len(sample_images)
  128. pixel_attention_mask[batch, :num_cameras] = True
  129. padded_pixel_values[batch, :num_cameras] = processed["pixel_values"]
  130. return_data = {
  131. **text_inputs,
  132. "pixel_values": padded_pixel_values,
  133. "pixel_attention_mask": pixel_attention_mask,
  134. }
  135. if actions is not None:
  136. actions = (torch.tensor(actions) - self.actions_mean) / (self.actions_std + 1e-08)
  137. if actions.shape[-1] < self.max_state_dim:
  138. actions = F.pad(actions, (0, self.max_state_dim - actions.shape[-1]))
  139. return_data["actions"] = actions.view(-1, self.chunk_size, self.max_state_dim)
  140. if state is not None:
  141. state = (torch.tensor(state) - self.state_mean) / (self.state_std + 1e-08)
  142. if state.shape[-1] < self.max_state_dim:
  143. state = F.pad(state, (0, self.max_state_dim - state.shape[-1]))
  144. return_data["state"] = state.view(-1, self.max_state_dim)
  145. return BatchFeature(data=return_data, tensor_type=return_tensors)
  146. def _get_num_multimodal_tokens(self, image_sizes=None, **kwargs):
  147. """
  148. Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.
  149. Args:
  150. image_sizes (list[list[str]], *optional*):
  151. The input sizes formatted as (height, width) per each image.
  152. Returns:
  153. `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
  154. input modalities, along with other useful data.
  155. """
  156. vision_data = {}
  157. if image_sizes is not None:
  158. num_image_tokens = [self.image_seq_length] * len(image_sizes)
  159. num_image_patches = [1] * len(image_sizes)
  160. vision_data.update({"num_image_tokens": num_image_tokens, "num_image_patches": num_image_patches})
  161. return MultiModalData(**vision_data)
  162. @property
  163. def model_input_names(self):
  164. return super().model_input_names + ["pixel_attention_mask"]
  165. __all__ = ["PI0Processor"]