# LICENSE HEADER MANAGED BY add-license-header
#
# Copyright 2018 Kornia Team
#
# 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.
#

from __future__ import annotations

from typing import Any, Optional

import torch
import torch.nn.functional as F

from kornia.augmentation import Resize
from kornia.augmentation.container.augment import AugmentationSequential
from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SHAPE
from kornia.core.exceptions import BaseError
from kornia.enhance import normalize as kornia_normalize
from kornia.geometry.boxes import Boxes
from kornia.geometry.keypoints import Keypoints
from kornia.models.sam import Sam, SamConfig
from kornia.models.structures import Prompts, SegmentationResults


def _resize_like_sam(image: torch.Tensor, size: tuple[int, int]) -> torch.Tensor:
    """Resize as SAM's reference does: bilinear on the half-pixel grid, with torch's antialiasing.

    This is ``ResizeLongestSide.apply_image_torch`` of ``segment-anything@3518c86``. It runs in at least float32, as
    torch has no half-precision antialiased kernel on the CPU, and the result is cast back to the dtype of ``image``.

    The image does not go through kornia's own antialiased ``kornia.geometry.transform.resize``, which blurs with a
    Gaussian of standard deviation ``(f - 1) / 2`` before downscaling by ``f``, a weaker low-pass than the antialiased
    bilinear filter of SAM's preprocessing. On a 1380 x 2076 image, against a transcription of Meta's ``SamPredictor``
    driving the same MobileSAM weights, the three masks of a point prompt reached IoU 0.962, 0.967 and 0.956 with
    kornia's filter and 0.969, 0.969 and 0.968 with SAM's.
    """
    x = image.to(torch.promote_types(image.dtype, torch.float32))
    try:
        out = F.interpolate(x, size=size, mode="bilinear", align_corners=False, antialias=True)
    except NotImplementedError:
        if x.device.type != "mps":
            raise
        # torch 2.5.1 has no MPS kernel for the antialiased bilinear resize (``_upsample_bilinear2d_aa``).
        out = F.interpolate(x.cpu(), size=size, mode="bilinear", align_corners=False, antialias=True).to(x.device)
    return out.to(image.dtype)


class VisualPrompter:
    r"""Segment one image from point, box and mask prompts with SAM, encoding the image once.

    ``prompter = VisualPrompter(SamConfig("mobile_sam", pretrained=True))`` builds the SAM model.
    ``prompter.set_image(image)`` encodes one RGB image ``(3, H, W)`` or ``(1, 3, H, W)``, floating point in
    :math:`[0, 1]`. Each ``result = prompter.predict(keypoints, keypoints_labels)`` or
    ``prompter.predict(boxes=boxes)`` then answers ``K`` prompts given in that image's pixels. It returns a
    :class:`~kornia.models.structures.SegmentationResults` with ``(K, M, 256, 256)`` low-resolution ``logits``,
    ``(K, M)`` ``scores`` and ``(K, M, H, W)`` ``binary_masks``, with ``M = 3`` masks per prompt, or 1 for
    ``multimask_output=False``.

    Convention:
        - :meth:`set_image` resizes the image so that its long side equals ``image_encoder.img_size``, with the
          antialiased bilinear filter of SAM's reference (``ResizeLongestSide.apply_image_torch``), computed in float32
          (float64 for a float64 image) and cast back. It then normalizes it with SAM's mean and standard deviation,
          or the ``mean`` and ``std`` it is given, and zero-pads it at the bottom and right to the square encoder
          input (:meth:`preprocess_image`). The resized size comes from ``transforms``, an
          :class:`~kornia.augmentation.container.AugmentationSequential` that must hold exactly one stage,
          ``kornia.augmentation.Resize(img_size, side="long", align_corners=False, antialias=True)``.
        - The prompts follow the per-axis scales :math:`s_x = w / W` and :math:`s_y = h / H` from the
          :math:`H \times W` image to the resized :math:`h \times w` one, computed in float32 (float64 for a float64
          model), and come out in that dtype. A keypoint marks a pixel centre and follows the half-pixel map
          :math:`x \mapsto (x + 0.5)\,s_x - 0.5` (likewise for :math:`y`), the map the resize samples on. A box marks
          pixel edges, kornia's ``xyxy`` convention (``width = x2 - x1``), and both its corners follow
          :math:`x \mapsto x\,s_x`, as in the reference's ``apply_boxes``: at :math:`s = 8` the box (10, 20, 60, 50)
          goes to (80, 160, 480, 400), and :math:`(0, 0, W, H)` to the whole resized image :math:`(0, 0, w, h)`.
        - Two details depart on purpose from Meta's ``SamPredictor`` (``segment-anything@3518c86``). The resized short
          side is truncated, where the reference rounds it (a 1380 x 2076 image becomes 680 x 1024 here and 681 x 1024
          there): truncation is the rule of :class:`kornia.augmentation.Resize`, and the masks are resized back
          through the same recorded size. Keypoints follow the half-pixel map above, where the reference's
          ``apply_coords`` uses :math:`x \mapsto x\,s` for points too: at :math:`s = 8`, the keypoint (40, 30) goes
          to (323.5, 243.5) here and to (320, 240) in the reference.
        - The held model follows the mode rule of :ref:`pretrained-models-conventions`: a config that loads weights
          gives a model in evaluation mode, and a randomly initialized one stays in training mode, where MobileSAM's
          BatchNorm normalizes with the statistics of the batch. Call ``prompter.model.eval()`` before inference on
          random weights.

    Args:
        config: A model config to generate the model. Now just the SAM model is supported. If None, SAM ViT-H,
            whose weights are downloaded on first use.
        device: The desired device to use the model.
        dtype: The desired dtype to use the model.

    Raises:
        NotImplementedError: If ``config`` is neither a :class:`~kornia.models.sam.SamConfig` nor ``None``.

    See Also:
        - :class:`~kornia.models.sam.Sam`: the model itself, for a batch of images each with its own prompts.
        - :class:`~kornia.models.structures.SegmentationResults`: the masks and scores :meth:`predict` returns.

    Example:
        >>> # prompter = VisualPrompter() # Will load the vit h for default
        >>> # You can load a custom SAM type for modifying the config
        >>> prompter = VisualPrompter(SamConfig('vit_b'))
        >>> image = torch.rand(3, 25, 30)
        >>> prompter.set_image(image)
        >>> # The K box prompts of the image, as a (1, K, 4) xyxy tensor; here K = 1.
        >>> boxes = Boxes.from_tensor(
        ...     torch.tensor([[[0.0, 0.0, 10.0, 10.0]]], device=prompter.device, dtype=torch.float32), mode='xyxy'
        ... )
        >>> prediction = prompter.predict(boxes=boxes)
        >>> prediction.logits.shape
        torch.Size([1, 3, 256, 256])

    """

    def __init__(
        self,
        config: Optional[SamConfig] = None,
        device: Optional[torch.device] = None,
        dtype: Optional[torch.dtype] = None,
    ) -> None:
        """Build the configured SAM model and its preprocessing transforms.

        A missing config selects pretrained ViT-H. The model and normalization statistics use the requested
        device and dtype; unsupported configuration types raise ``NotImplementedError``.
        """
        super().__init__()
        if config is None:
            config = SamConfig(model_type="vit_h", pretrained=True)

        if isinstance(config, SamConfig):
            self.model = Sam.from_config(config)
            transforms = (Resize(self.model.image_encoder.img_size, side="long", align_corners=False, antialias=True),)
            self.pixel_mean: Optional[torch.Tensor] = (
                torch.tensor([123.675, 116.28, 103.53], device=device, dtype=dtype) / 255
            )
            self.pixel_std: Optional[torch.Tensor] = (
                torch.tensor([58.395, 57.12, 57.375], device=device, dtype=dtype) / 255
            )
        else:
            raise NotImplementedError(f"Unsupported config type: {type(config).__name__}. Expected SamConfig or None.")

        self.model = self.model.to(device=device, dtype=dtype)
        self.transforms = AugmentationSequential(*transforms, same_on_batch=True)

        self.device = device
        self.dtype = dtype
        self._original_image_size: tuple[int, int] | None = None
        self._input_image_size: tuple[int, int] | None = None
        self._input_encoder_size: tuple[int, int] | None = None
        self._prompt_scale: Optional[torch.Tensor] = None
        self.reset_image()

    def preprocess_image(
        self, x: torch.Tensor, mean: Optional[torch.Tensor] = None, std: Optional[torch.Tensor] = None
    ) -> torch.Tensor:
        """Normalize an image by a mean and standard deviation, and zero-pad it to the encoder's square input.

        The image is normalized with ``mean`` and ``std`` when both are given, else with SAM's dataset values set at
        construction, and zero-padded at the right and bottom to ``self.model.image_encoder.img_size``.

        Args:
            x: The image to be preprocessed
            mean: Mean for each channel.
            std: Standard deviations for each channel.

        Returns:
            The normalized image, padded to the encoder's size.

        """
        if isinstance(mean, torch.Tensor) and isinstance(std, torch.Tensor):
            x = kornia_normalize(x, mean, std)
        elif isinstance(self.pixel_mean, torch.Tensor) and isinstance(self.pixel_std, torch.Tensor):
            x = kornia_normalize(x, self.pixel_mean, self.pixel_std)

        encoder_im_size = self.model.image_encoder.img_size
        pad_h = encoder_im_size - x.shape[-2]
        pad_w = encoder_im_size - x.shape[-1]

        return F.pad(x, (0, pad_w, 0, pad_h))

    @torch.no_grad()
    def set_image(
        self, image: torch.Tensor, mean: Optional[torch.Tensor] = None, std: Optional[torch.Tensor] = None
    ) -> None:
        """Encode one image with the model's image encoder and keep its embeddings for :meth:`predict`.

        Resize the image to the size given by ``transforms``, with SAM's filter (see the class docstring), and prepare
        it with :meth:`preprocess_image`.

        Args:
            image: One RGB image, shape ``(3, H, W)`` or ``(1, 3, H, W)``, in the dtype of the model, floating point
                   with values in :math:`[0, 1]`; it is normalized with the mean and std set at construction unless
                   ``mean`` and ``std`` are given. A batch of more than one image raises; use
                   :meth:`kornia.models.sam.Sam.forward` for batches.
            mean: mean value of dataset for normalization.
            std: standard deviation of dataset for normalization.

        """
        KORNIA_CHECK(
            image.is_floating_point(),
            f"VisualPrompter takes a floating-point image with values in [0, 1]; got {image.dtype}.",
        )
        if image.ndim == 3:
            KORNIA_CHECK_SHAPE(image, ["3", "H", "W"])
            image = image.unsqueeze(0)
        else:
            KORNIA_CHECK_SHAPE(image, ["B", "3", "H", "W"])
            KORNIA_CHECK_SHAPE(
                image,
                ["1", "3", "H", "W"],
                "VisualPrompter takes one image, (3, H, W) or (1, 3, H, W). "
                "Use `Sam.forward` to segment a batch of images with per-image prompts.",
            )

        self.reset_image()

        self._original_image_size = (image.shape[-2], image.shape[-1])

        # The Resize augmentation gives the output size; the image itself is resized with the filter of SAM's
        # reference, and the prompts with the per-axis scales (see the class docstring).
        stages = list(self.transforms.children())
        KORNIA_CHECK(
            len(stages) == 1 and isinstance(stages[0], Resize),
            "VisualPrompter.transforms must hold exactly one stage, kornia.augmentation.Resize, which gives the size "
            f"of the resized image; got {[type(stage).__name__ for stage in stages]}.",
        )
        self._tfs_params = self.transforms.forward_parameters(image.shape)
        resize_params = self._tfs_params[0].data
        if not isinstance(resize_params, dict):
            raise TypeError(f"Expected the parameters of the Resize augmentation. Got {type(resize_params)}.")
        height, width = (int(side) for side in resize_params["output_size"][0])
        encoder_size = self.model.image_encoder.img_size
        KORNIA_CHECK(
            max(height, width) <= encoder_size,
            lambda: (
                f"VisualPrompter.transforms resizes the image to {height} x {width}, larger than the image "
                f"encoder's input size {encoder_size}: the padding to {encoder_size} would crop the image while the "
                "prompts are scaled for all of it."
            ),
        )
        prompt_dtype = torch.promote_types(self.model.prompt_encoder.point_embeddings[0].weight.dtype, torch.float32)
        self._prompt_scale = torch.tensor(
            [width / image.shape[-1], height / image.shape[-2]], device=image.device, dtype=prompt_dtype
        )
        image = _resize_like_sam(image, (height, width))
        self._input_image_size = (image.shape[-2], image.shape[-1])

        image = self.preprocess_image(image, mean, std)

        self._input_encoder_size = (image.shape[-2], image.shape[-1])

        self.image_embeddings = self.model.image_encoder(image)
        self.is_image_set = True

    def _valid_keypoints(self, keypoints: Keypoints | torch.Tensor, labels: torch.Tensor) -> Keypoints:
        """Validate the keypoints shape and ensure to be a Keypoints."""
        KORNIA_CHECK_SHAPE(keypoints.data, ["K", "N", "2"])
        KORNIA_CHECK_SHAPE(labels.data, ["K", "N"])
        KORNIA_CHECK(keypoints.shape[0] == labels.shape[0], "The keypoints and labels should have the same batch size")

        if isinstance(keypoints, torch.Tensor):
            keypoints = Keypoints.from_tensor(keypoints)

        return keypoints

    def _valid_boxes(self, boxes: Boxes | torch.Tensor) -> torch.Tensor:
        """Validate the box prompts and return them as a ``(K, 4)`` xyxy tensor.

        The boxes are the ``K`` box prompts of the one image set. A ``(K, 4)`` or ``(1, K, 4)`` xyxy tensor is taken
        as given. A :class:`~kornia.geometry.boxes.Boxes` holding ``(K, 4, 2)`` or ``(1, K, 4, 2)`` vertices is read
        through ``to_tensor(mode="xyxy")``.
        """
        if isinstance(boxes, torch.Tensor):
            if boxes.ndim == 2:
                KORNIA_CHECK_SHAPE(boxes.data, ["K", "4"])
            else:
                KORNIA_CHECK_SHAPE(boxes.data, ["B", "K", "4"])
            # ``Boxes.from_tensor`` checks the values (finite, positive extents). Its vertices are not used: their
            # round trip widens a sub-pixel box ([10, 20, 10.5, 20.5] comes back as [9.5, 19.5, 11, 21]) and, in
            # bfloat16, rounds the -1 offset away ([256, 256, 258, 258] comes back as [256, 256, 256, 256]).
            Boxes.from_tensor(boxes, mode="xyxy")
            images = boxes.shape[0] if boxes.ndim == 3 else 1
            xyxy = boxes if boxes.is_floating_point() else boxes.to(torch.get_default_dtype())
        else:
            images = boxes.data.shape[0] if boxes.data.ndim == 4 else 1
            exported = boxes.to_tensor(mode="xyxy")
            if not isinstance(exported, torch.Tensor):
                raise TypeError(f"Expected the boxes as a tensor. Got {type(exported)}.")
            xyxy = exported

        KORNIA_CHECK(
            images == 1,
            lambda: (
                f"VisualPrompter takes the boxes of its one image, (K, 4) or (1, K, 4); got boxes for {images} "
                "images. Use `Sam.forward` to prompt a batch of images."
            ),
        )

        return xyxy.reshape(-1, 4)

    def _valid_masks(self, masks: torch.Tensor) -> torch.Tensor:
        """Validate the input masks shape."""
        KORNIA_CHECK_SHAPE(masks, ["K", "1", "256", "256"])
        return masks

    def _scale(self, like: torch.Tensor) -> torch.Tensor:
        """The per-axis scales ``(s_x, s_y)`` recorded by ``set_image``, on the device of ``like``."""
        scale = self._prompt_scale
        if scale is None:
            raise BaseError("An image must be set with `self.set_image(...)` before its prompts can be mapped.")
        return scale.to(like.device)

    def _map_keypoints(self, keypoints: torch.Tensor) -> torch.Tensor:
        """Map pixel-centre keypoints ``(..., 2)`` of the original image with the half-pixel map, in float32 or more."""
        scale = self._scale(keypoints)
        return (keypoints.to(scale.dtype) + 0.5) * scale - 0.5

    def _map_box_edges(self, boxes: torch.Tensor) -> torch.Tensor:
        """Map xyxy boxes ``(K, 4)`` of the original image, whose corners are pixel edges, in float32 or wider."""
        scale = self._scale(boxes)
        return (boxes.to(scale.dtype).reshape(-1, 2, 2) * scale).reshape(-1, 4)

    def preprocess_prompts(
        self,
        keypoints: Optional[Keypoints | torch.Tensor] = None,
        keypoints_labels: Optional[torch.Tensor] = None,
        boxes: Optional[Boxes | torch.Tensor] = None,
        masks: Optional[torch.Tensor] = None,
    ) -> Prompts:
        r"""Validate and preprocess the given prompts to be aligned with the input image.

        Keypoints follow the half-pixel map :math:`(x + 0.5)\,s - 0.5` of a pixel centre, and both corners of each
        xyxy box the edge map :math:`x\,s`, as the reference's ``apply_boxes`` does, with the per-axis scales recorded
        by :meth:`set_image`. They are computed, and returned, in float32 (float64 for a float64 model). A tensor box
        is taken as given; a :class:`~kornia.geometry.boxes.Boxes` is read through ``to_tensor(mode="xyxy")``.
        """
        points: Optional[tuple[torch.Tensor, torch.Tensor]] = None
        if isinstance(keypoints, (Keypoints, torch.Tensor)) and isinstance(keypoints_labels, torch.Tensor):
            keypoints = self._valid_keypoints(keypoints, keypoints_labels)
            keypoints_tensor = keypoints.to_tensor()
            if not isinstance(keypoints_tensor, torch.Tensor):
                raise TypeError(f"Expected the keypoints as a tensor. Got {type(keypoints_tensor)}.")
            points = (self._map_keypoints(keypoints_tensor), keypoints_labels)
        elif keypoints is not None:
            KORNIA_CHECK(
                False,
                "keypoints_labels must be given with keypoints: a (K, N) tensor, 1 for a foreground point and 0 for "
                "a background point.",
            )

        bbox: Optional[torch.Tensor] = None
        if isinstance(boxes, (Boxes, torch.Tensor)):
            bbox = self._map_box_edges(self._valid_boxes(boxes))

        if isinstance(masks, torch.Tensor):
            self._valid_masks(masks)

        return Prompts(points=points, boxes=bbox, masks=masks)

    @torch.no_grad()
    def predict(
        self,
        keypoints: Optional[Keypoints | torch.Tensor] = None,
        keypoints_labels: Optional[torch.Tensor] = None,
        boxes: Optional[Boxes | torch.Tensor] = None,
        masks: Optional[torch.Tensor] = None,
        multimask_output: bool = True,
        output_original_size: bool = True,
    ) -> SegmentationResults:
        """Predict masks for the given image based on the input prompts.

        Args:
            keypoints: Point prompts to the model. Each point is in (X,Y) in pixels. Shape ``(K, N, 2)``. Where
                       ``N`` is the number of points and ``K`` the number of prompts.
            keypoints_labels: Labels for the point prompts. 1 indicates a foreground point and 0 indicates a background
                             point. Shape ``(K, N)``. Where ``N`` is the number of points, and ``K`` the number of
                             prompts. Required with ``keypoints``.
            boxes: Box prompts to the model, one per prompt: a torch.Tensor in xyxy mode, shape ``(K, 4)`` or
                   ``(1, K, 4)``, whose corners are taken as given, or a :class:`~kornia.geometry.boxes.Boxes` of
                   shape ``(K, 4, 2)`` or ``(1, K, 4, 2)``, read through ``to_tensor(mode="xyxy")``. ``K`` is
                   the number of prompts.
            masks: A low resolution mask input to the model, typically coming from a previous prediction
                   iteration. Has shape ``(K, 1, H, W)``, where for SAM, H=W=256.
            multimask_output: If True, three masks per prompt, each with its score in ``scores``; if False, one.
            output_original_size: If True, ``binary_masks`` is resized to the size of the image set.

        Returns:
            The ``(K, M, 256, 256)`` low-resolution ``logits`` and the ``(K, M)`` ``scores`` (the predicted IoU of
            each mask), with ``M`` 3 or 1 by ``multimask_output``. With ``output_original_size``, ``binary_masks``
            is ``(K, M, H, W)``, at the size of the image set; otherwise it is at the logits' size.

        Raises:
            BaseError: If no image is set, or if ``keypoints`` come without ``keypoints_labels``.

        """
        KORNIA_CHECK(self.is_image_set, "An image must be set with `self.set_image(...)` before `predict` be called!")

        prompts = self.preprocess_prompts(keypoints, keypoints_labels, boxes, masks)

        # Embed prompts
        sparse_embeddings, dense_embeddings = self.model.prompt_encoder(
            points=prompts.points, boxes=prompts.boxes, masks=prompts.masks
        )
        del prompts

        # Predict masks
        logits, scores = self.model.mask_decoder(
            image_embeddings=self.image_embeddings,
            image_pe=self.model.prompt_encoder.get_dense_pe(),
            sparse_prompt_embeddings=sparse_embeddings,
            dense_prompt_embeddings=dense_embeddings,
            multimask_output=multimask_output,
        )
        results = SegmentationResults(logits, scores)
        if (
            output_original_size
            and isinstance(self._input_image_size, tuple)
            and isinstance(self._original_image_size, tuple)
        ):
            results.original_res_logits(self._input_image_size, self._original_image_size, self._input_encoder_size)

        # results = results.squeeze(0)

        return results

    def reset_image(self) -> None:
        """Clear cached image state and prompt-transform metadata.

        This method invalidates previously computed image embeddings and resets all
        size/transform bookkeeping so a new call to :meth:`set_image` starts from a
        clean state.

        In practice, this resets:
        - transformed-image parameters and the prompt scales,
        - original/input/encoder spatial sizes,
        - cached image embeddings,
        - ``is_image_set`` status flag.
        """
        self._tfs_params = None
        self._prompt_scale = None
        self._original_image_size = None
        self._input_image_size = None
        self._input_encoder_size = None

        if hasattr(self, "image_embeddings"):
            del self.image_embeddings

        self.image_embeddings = None
        self.is_image_set = False

    def compile(
        self,
        *,
        fullgraph: bool = False,
        dynamic: bool = False,
        backend: str = "inductor",
        mode: Optional[str] = None,
        options: Optional[dict[Any, Any]] = None,
        disable: bool = False,
    ) -> None:
        """Apply `torch.compile(...)`/dynamo API into the VisualPrompter API.

        .. note:: For more information about the dynamo API check the official docs
                  https://pytorch.org/docs/stable/generated/torch.compile.html

        Args:
            fullgraph: Whether it is ok to break model into several subgraphs
            dynamic: Use dynamic shape tracing
            backend: backend to be used
            mode: Can be either “default”, “reduce-overhead” or “max-autotune”
            options: A dictionary of options to pass to the backend.
            disable: Turn torch.compile() into a no-op for testing

        Example:
            >>> # prompter = VisualPrompter()
            >>> # prompter.compile()
            >>> # Use the prompter methods ...

        """
        # self.set_image = torch.compile(
        #     self.set_image,
        #     fullgraph=fullgraph,
        #     dynamic=dynamic,
        #     backend=backend,
        #     mode=mode,
        #     options=options,
        #     disable=disable,
        # )
        # FIXME: compile set image will try to compile AugmentationSequential which fails
        self.model.image_encoder = torch.compile(
            self.model.image_encoder,
            fullgraph=fullgraph,
            dynamic=dynamic,
            backend=backend,
            mode=mode,
            options=options,
            disable=disable,
        )

        # self.preprocess_image = torch.compile(
        #     self.preprocess_image,
        #     fullgraph=fullgraph,
        #     dynamic=dynamic,
        #     backend=backend,
        #     mode=mode,
        #     options=options,
        #     disable=disable,
        # )

        # FIXME: compile predict will try to compile Preproc prompts, which need to compileAugmentationSequential
        # which fails
        # self.predict = torch.compile(
        #     self.predict,
        #     fullgraph=fullgraph,
        #     dynamic=dynamic,
        #     backend=backend,
        #     mode=mode,
        #     options=options,
        #     disable=disable,
        # )
        self.model.mask_decoder = torch.compile(
            self.model.mask_decoder,
            fullgraph=fullgraph,
            dynamic=dynamic,
            backend=backend,
            mode=mode,
            options=options,
            disable=disable,
        )
        self.model.prompt_encoder = torch.compile(
            self.model.prompt_encoder,
            fullgraph=fullgraph,
            dynamic=dynamic,
            backend=backend,
            mode=mode,
            options=options,
            disable=disable,
        )
