#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/pi0/modular_pi0.py.
#               Do NOT edit this file manually as any edits will be overwritten by the generation of
#             the file from the modular. If any change should be done, please apply the change to the
#                          modular_pi0.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2025 Physical Intelligence and The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import torch
import torch.nn.functional as F

from ...feature_extraction_utils import BatchFeature
from ...image_utils import ImageInput, make_nested_list_of_images
from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack
from ...tokenization_utils_base import AddedToken, PreTokenizedInput, TextInput
from ...utils import auto_docstring, logging
from ...utils.deprecation import deprecate_attr
from ...utils.import_utils import requires


logger = logging.get_logger(__name__)


@auto_docstring
@requires(backends=("vision", "torch"))
class PI0Processor(ProcessorMixin):
    text_kwargs = {
        "padding": "max_length",
        "max_length": 48,
        "padding_side": "right",
        "return_tensors": "pt",
    }

    def __init__(
        self,
        image_processor=None,
        tokenizer=None,
        chat_template=None,
        state_mean: list[float] | None = None,
        state_std: list[float] | None = None,
        actions_mean: list[float] | None = None,
        actions_std: list[float] | None = None,
        max_state_dim: int = 32,
        chunk_size: int = 50,
        **kwargs,
    ):
        r"""
        state_mean (`list[float]`, *optional*):
            Dataset mean of the robot state. Required to process `state`.
        state_std (`list[float]`, *optional*):
            Dataset standard deviation of the robot state. Required to process `state`.
        actions_mean (`list[float]`, *optional*):
            Dataset mean of the actions. Required to process `actions`.
        actions_std (`list[float]`, *optional*):
            Dataset standard deviation of the actions. Required to process `actions`.
        max_state_dim (`int`, *optional*, defaults to 32):
            Size the state and actions are zero-padded to.
        chunk_size (`int`, *optional*, defaults to 50):
            Number of actions per chunk.
        """
        self.state_mean = state_mean
        self.state_std = state_std
        self.actions_mean = actions_mean
        self.actions_std = actions_std
        self.max_state_dim = max_state_dim
        self.chunk_size = chunk_size
        if not hasattr(image_processor, "image_seq_length"):
            raise ValueError("Image processor is missing an `image_seq_length` attribute.")

        self.image_seq_length = image_processor.image_seq_length

        if not hasattr(tokenizer, "<image>"):
            # Just add to vocab if not yet there. Note that the model isn't yet converted and
            # can be modified when lerobot team decided to adopt it
            self.image_token = "<image>"
            image_token = AddedToken(self.image_token, normalized=False, special=True)
            tokens_to_add = {"additional_special_tokens": [image_token]}
            tokenizer.add_special_tokens(tokens_to_add)
            self.image_token_id = tokenizer.convert_tokens_to_ids(self.image_token)
        else:
            self.image_token_id = tokenizer.image_token_id
            self.image_token = tokenizer.image_token

        tokenizer.add_tokens([f"<loc{i:0>4}>" for i in range(1024)] + [f"<seg{i:0>3}>" for i in range(128)])
        tokenizer.add_bos_token = False
        tokenizer.add_eos_token = False

        super().__init__(image_processor, tokenizer, chat_template=chat_template)

    @auto_docstring
    def __call__(
        self,
        images: ImageInput | list[ImageInput] | list[list[ImageInput]] | None,
        text: TextInput | PreTokenizedInput | list[TextInput] | list[PreTokenizedInput] | None = None,
        actions: list | np.ndarray | torch.Tensor | None = None,
        state: list | np.ndarray | torch.Tensor | None = None,
        **kwargs: Unpack[ProcessingKwargs],
    ) -> BatchFeature:
        r"""
        actions (`list | np.ndarray | torch.Tensor`, *optional*):
            Actions to be predicted by the model. Normalized with the processor's dataset statistics, then padded.
        state (`list | np.ndarray | torch.Tensor`, *optional*):
            Robot state. Normalized with the processor's dataset statistics, then padded.

        Returns:
            [`BatchFeature`]: A [`BatchFeature`] with the following fields:

            - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`. If `suffix`
              is provided, the `input_ids` will also contain the suffix input ids.
            - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
              `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
              `None`).
            - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
            - **pixel_attention_mask** -- Pixel values padding mask to be fed to a model. Returned when `images` is not `None`.
            - **state** -- Robot state compatible with model if `state` is not None
            - **actions** -- Label-actions compatible with training if `actions` is not None
        """
        output_kwargs = self._merge_kwargs(tokenizer_init_kwargs=self.tokenizer.init_kwargs, **kwargs)

        if text is None:
            logger.warning_once("You are using PI0 without a text prefix. The processor will use an empty prompt.")
            text = ""

        if isinstance(text, str):
            text = [text]

        batched_images = make_nested_list_of_images(images)
        if len(batched_images) != len(text):
            raise ValueError(
                f"Received {len(batched_images)} image samples for {len(text)} prompts. "
                "Each prompt should be associated with one sample (with one or more camera images)."
            )

        return_tensors = output_kwargs["text_kwargs"].pop("return_tensors", None)
        output_kwargs["images_kwargs"].pop("return_tensors", None)

        prompt_strings = []
        for sample, image_list in zip(text, batched_images):
            sample = (
                f"{self.image_token * self.image_seq_length * len(image_list)}{self.tokenizer.bos_token}{sample}\n"
            )
            prompt_strings.append(sample)

        text_inputs = self.tokenizer(prompt_strings, **output_kwargs["text_kwargs"])

        # Here is the diff from PaliGemma. Ideally we'd create a new ImageProcessor if it were a VLM
        num_cameras = torch.tensor([len(sample_images) for sample_images in batched_images])
        pixel_attention_mask = torch.arange(num_cameras.max())[None, :] < num_cameras[:, None]
        pixel_values = self.image_processor(
            [image for sample_images in batched_images for image in sample_images],
            return_tensors="pt",
            **output_kwargs["images_kwargs"],
        )["pixel_values"]
        padded_pixel_values = pixel_values.new_zeros(*pixel_attention_mask.shape, *pixel_values.shape[1:])
        padded_pixel_values[pixel_attention_mask] = pixel_values

        model_inputs = {
            **text_inputs,
            "pixel_values": padded_pixel_values,
            "pixel_attention_mask": pixel_attention_mask,
        }

        if actions is not None:
            actions = self.normalize(torch.as_tensor(actions, dtype=torch.float32), "actions")
            if actions.shape[-1] < self.max_state_dim:
                actions = F.pad(actions, (0, self.max_state_dim - actions.shape[-1]))
            model_inputs["actions"] = actions.view(-1, self.chunk_size, self.max_state_dim)

        if state is not None:
            state = self.normalize(torch.as_tensor(state, dtype=torch.float32), "state")
            if state.shape[-1] < self.max_state_dim:
                state = F.pad(state, (0, self.max_state_dim - state.shape[-1]))
            model_inputs["state"] = state.view(-1, self.max_state_dim)

        return BatchFeature(data=model_inputs, tensor_type=return_tensors)

    def normalize(self, values: torch.Tensor, feature: str) -> torch.Tensor:
        """Normalizes `state` or `actions` with the dataset mean and standard deviation."""
        mean, std = getattr(self, f"{feature}_mean"), getattr(self, f"{feature}_std")
        if mean is None or std is None:
            raise ValueError(
                f"Processing `{feature}` needs `{feature}_mean` and `{feature}_std`. Pass the statistics of the "
                "dataset the checkpoint was trained on when creating the processor."
            )
        return (values - torch.tensor(mean)) / (torch.tensor(std) + 1e-08)

    @property
    def model_input_names(self):
        return super().model_input_names + ["pixel_attention_mask"]


__getattr__ = deprecate_attr(__name__, rename={"PI0ProcessorKwargs": (ProcessingKwargs, "5.29.0")})

__all__ = ["PI0Processor"]
