





'''simple docstring'''


from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
 from PIL import Image

 from ..image_utils import load_image

if is_torch_available():
 import torch

 from transformers.modeling_outputs import BaseModelOutput

 from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

lowerCAmelCase :					Union[str, Any] 							=logging.get_logger(__name__)
@add_end_docstrings(_lowerCAmelCase							)
class 				a_       (    _lowerCAmelCase							):





     def __init__(			self		: List[Any]     ,				**lowercase		: Optional[int]						):





        """simple docstring"""




        super().__init__(**lowercase						)

        if self.framework == "tf":
           raise ValueError(F'The {self.__class__} is only available in PyTorch.'						)

        requires_backends(self     ,				"vision"						)
        self.check_model_type(lowercase						)





     def __call__(			self		: Tuple     ,				lowercase		: Union[str, "Image.Image", List[Dict[str, Any]]]     ,				lowercase		: Union[str, List[str]] = None     ,				**lowercase		: str     ,				):





        """simple docstring"""




        if "text_queries" in kwargs:
           lowercase_      :List[Any]         =			kwargs.pop("text_queries"						)

        if isinstance(lowercase     ,				(str, Image.Image)						):
           lowercase_      :List[str]         =			{"image": image, "candidate_labels": candidate_labels}
        else:
           lowercase_      :Optional[Any]         =			image
        lowercase_      :str         =			super().__call__(lowercase     ,				**lowercase						)
        return results





     def 							lowercase__				(			self		: Optional[int]     ,				**lowercase		: List[str]						):





        """simple docstring"""




        lowercase_      :Tuple         =			{}
        if "threshold" in kwargs:
           lowercase_      :Dict         =			kwargs["threshold"]
        if "top_k" in kwargs:
           lowercase_      :Optional[Any]         =			kwargs["top_k"]
        return {}, {}, postprocess_params





     def 							lowercase__				(			self		: List[Any]     ,				lowercase		: Dict						):





        """simple docstring"""




        lowercase_      :Any         =			load_image(inputs["image"]						)
        lowercase_      :List[str]         =			inputs["candidate_labels"]
        if isinstance(lowercase     ,				lowercase						):
           lowercase_      :Union[str, Any]         =			candidate_labels.split(","						)

        lowercase_      :Tuple         =			torch.tensor([[image.height, image.width]]     ,				dtype=torch.intaa						)
        for i, candidate_label in enumerate(lowercase						):
           lowercase_      :Union[str, Any]         =			self.tokenizer(lowercase     ,				return_tensors=self.framework						)
           lowercase_      :Tuple         =			self.image_processor(lowercase     ,				return_tensors=self.framework						)
           yield {
               "is_last": i == len(lowercase						) - 1,
               "target_size": target_size,
               "candidate_label": candidate_label,
               **text_inputs,
               **image_features,
           }





     def 							lowercase__				(			self		: List[str]     ,				lowercase		: List[str]						):





        """simple docstring"""




        lowercase_      :Dict         =			model_inputs.pop("target_size"						)
        lowercase_      :str         =			model_inputs.pop("candidate_label"						)
        lowercase_      :List[Any]         =			model_inputs.pop("is_last"						)

        lowercase_      :Optional[Any]         =			self.model(**lowercase						)

        lowercase_      :str         =			{"target_size": target_size, "candidate_label": candidate_label, "is_last": is_last, **outputs}
        return model_outputs





     def 							lowercase__				(			self		: Optional[int]     ,				lowercase		: List[str]     ,				lowercase		: List[str]=0.1     ,				lowercase		: Optional[int]=None						):





        """simple docstring"""




        lowercase_      :Dict         =			[]
        for model_output in model_outputs:
           lowercase_      :int         =			model_output["candidate_label"]
           lowercase_      :str         =			BaseModelOutput(lowercase						)
           lowercase_      :List[Any]         =			self.image_processor.post_process_object_detection(
               outputs=lowercase     ,				threshold=lowercase     ,				target_sizes=model_output["target_size"]						)[0]

           for index in outputs["scores"].nonzero():
              lowercase_      :Optional[int]         =			outputs["scores"][index].item()
              lowercase_      :int         =			self._get_bounding_box(outputs["boxes"][index][0]						)

              lowercase_      :int         =			{"score": score, "label": label, "box": box}
              results.append(lowercase						)

        lowercase_      :Dict         =			sorted(lowercase     ,				key=lambda lowercase						: x["score"]     ,				reverse=lowercase						)
        if top_k:
           lowercase_      :List[str]         =			results[:top_k]

        return results







     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: "torch.Tensor"						):





        """simple docstring"""




        if self.framework != "pt":
           raise ValueError("The ZeroShotObjectDetectionPipeline is only available in PyTorch."						)
        lowercase_      :List[str]         =			box.int().tolist()
        lowercase_      :List[Any]         =			{
            "xmin": xmin,
            "ymin": ymin,
            "xmax": xmax,
            "ymax": ymax,
        }
        return bbox









'''simple docstring'''


import tempfile

import torch

from diffusers import (
    DEISMultistepScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    UniPCMultistepScheduler,
)

from .test_schedulers import SchedulerCommonTest
class 				a_       (    _lowerCAmelCase							):
     __A								=      (UniPCMultistepScheduler,)
     __A								=      (("num_inference_steps", 25),)





     def 							lowercase__				(			self		: Tuple     ,				**lowercase		: Optional[Any]						):





        """simple docstring"""




        lowercase_      :Any         =			{
            "num_train_timesteps": 1_000,
            "beta_start": 0.00_01,
            "beta_end": 0.02,
            "beta_schedule": "linear",
            "solver_order": 2,
            "solver_type": "bh2",
        }

        config.update(**lowercase						)
        return config





     def 							lowercase__				(			self		: Optional[Any]     ,				lowercase		: str=0     ,				**lowercase		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :Tuple         =			dict(self.forward_default_kwargs						)
        lowercase_      :Tuple         =			kwargs.pop("num_inference_steps"     ,				lowercase						)
        lowercase_      :Union[str, Any]         =			self.dummy_sample
        lowercase_      :Union[str, Any]         =			0.1 * sample
        lowercase_      :Any         =			[residual + 0.2, residual + 0.15, residual + 0.10]

        for scheduler_class in self.scheduler_classes:
           lowercase_      :Tuple         =			self.get_scheduler_config(**lowercase						)
           lowercase_      :Tuple         =			scheduler_class(**lowercase						)
           scheduler.set_timesteps(lowercase						)
           # copy over dummy past residuals
           lowercase_      :str         =			dummy_past_residuals[: scheduler.config.solver_order]

           with tempfile.TemporaryDirectory() as tmpdirname:
              scheduler.save_config(lowercase						)
              lowercase_      :str         =			scheduler_class.from_pretrained(lowercase						)
              new_scheduler.set_timesteps(lowercase						)
              # copy over dummy past residuals
              lowercase_      :List[Any]         =			dummy_past_residuals[: new_scheduler.config.solver_order]

           lowercase_      ,							lowercase_      :Tuple         =			sample, sample
           for t in range(lowercase     ,				time_step + scheduler.config.solver_order + 1						):
              lowercase_      :Optional[int]         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample
              lowercase_      :Union[str, Any]         =			new_scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample

              assert torch.sum(torch.abs(output - new_output						)						) < 1e-5, "Scheduler outputs are not identical"





     def 							lowercase__				(			self		: str     ,				lowercase		: Any=0     ,				**lowercase		: str						):





        """simple docstring"""




        lowercase_      :Tuple         =			dict(self.forward_default_kwargs						)
        lowercase_      :Dict         =			kwargs.pop("num_inference_steps"     ,				lowercase						)
        lowercase_      :str         =			self.dummy_sample
        lowercase_      :Optional[int]         =			0.1 * sample
        lowercase_      :Optional[int]         =			[residual + 0.2, residual + 0.15, residual + 0.10]

        for scheduler_class in self.scheduler_classes:
           lowercase_      :Any         =			self.get_scheduler_config()
           lowercase_      :Tuple         =			scheduler_class(**lowercase						)
           scheduler.set_timesteps(lowercase						)

           # copy over dummy past residuals (must be after setting timesteps)
           lowercase_      :str         =			dummy_past_residuals[: scheduler.config.solver_order]

           with tempfile.TemporaryDirectory() as tmpdirname:
              scheduler.save_config(lowercase						)
              lowercase_      :int         =			scheduler_class.from_pretrained(lowercase						)
              # copy over dummy past residuals
              new_scheduler.set_timesteps(lowercase						)

              # copy over dummy past residual (must be after setting timesteps)
              lowercase_      :Optional[Any]         =			dummy_past_residuals[: new_scheduler.config.solver_order]

           lowercase_      :Dict         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample
           lowercase_      :str         =			new_scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample

           assert torch.sum(torch.abs(output - new_output						)						) < 1e-5, "Scheduler outputs are not identical"





     def 							lowercase__				(			self		: List[str]     ,				lowercase		: Optional[int]=None     ,				**lowercase		: int						):





        """simple docstring"""




        if scheduler is None:
           lowercase_      :int         =			self.scheduler_classes[0]
           lowercase_      :Optional[Any]         =			self.get_scheduler_config(**lowercase						)
           lowercase_      :Optional[Any]         =			scheduler_class(**lowercase						)

        lowercase_      :str         =			self.scheduler_classes[0]
        lowercase_      :Optional[int]         =			self.get_scheduler_config(**lowercase						)
        lowercase_      :List[Any]         =			scheduler_class(**lowercase						)

        lowercase_      :str         =			10
        lowercase_      :List[str]         =			self.dummy_model()
        lowercase_      :Optional[int]         =			self.dummy_sample_deter
        scheduler.set_timesteps(lowercase						)

        for i, t in enumerate(scheduler.timesteps						):
           lowercase_      :int         =			model(lowercase     ,				lowercase						)
           lowercase_      :Dict         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase						).prev_sample

        return sample





     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        lowercase_      :str         =			dict(self.forward_default_kwargs						)

        lowercase_      :Union[str, Any]         =			kwargs.pop("num_inference_steps"     ,				lowercase						)

        for scheduler_class in self.scheduler_classes:
           lowercase_      :Optional[Any]         =			self.get_scheduler_config()
           lowercase_      :Optional[int]         =			scheduler_class(**lowercase						)

           lowercase_      :Union[str, Any]         =			self.dummy_sample
           lowercase_      :str         =			0.1 * sample

           if num_inference_steps is not None and hasattr(lowercase     ,				"set_timesteps"						):
              scheduler.set_timesteps(lowercase						)
           elif num_inference_steps is not None and not hasattr(lowercase     ,				"set_timesteps"						):
              lowercase_      :str         =			num_inference_steps

           # copy over dummy past residuals (must be done after set_timesteps)
           lowercase_      :Dict         =			[residual + 0.2, residual + 0.15, residual + 0.10]
           lowercase_      :Any         =			dummy_past_residuals[: scheduler.config.solver_order]

           lowercase_      :Optional[int]         =			scheduler.timesteps[5]
           lowercase_      :List[Any]         =			scheduler.timesteps[6]

           lowercase_      :Optional[Any]         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample
           lowercase_      :List[str]         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase     ,				**lowercase						).prev_sample

           self.assertEqual(output_a.shape     ,				sample.shape						)
           self.assertEqual(output_a.shape     ,				output_a.shape						)





     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        lowercase_      :List[Any]         =			UniPCMultistepScheduler(**self.get_scheduler_config()						)
        lowercase_      :Dict         =			self.full_loop(scheduler=lowercase						)
        lowercase_      :List[Any]         =			torch.mean(torch.abs(lowercase						)						)

        assert abs(result_mean.item() - 0.24_64						) < 1e-3

        lowercase_      :List[str]         =			DPMSolverSinglestepScheduler.from_config(scheduler.config						)
        lowercase_      :Tuple         =			DEISMultistepScheduler.from_config(scheduler.config						)
        lowercase_      :Optional[int]         =			DPMSolverMultistepScheduler.from_config(scheduler.config						)
        lowercase_      :Tuple         =			UniPCMultistepScheduler.from_config(scheduler.config						)

        lowercase_      :Tuple         =			self.full_loop(scheduler=lowercase						)
        lowercase_      :Tuple         =			torch.mean(torch.abs(lowercase						)						)

        assert abs(result_mean.item() - 0.24_64						) < 1e-3





     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        for timesteps in [25, 50, 100, 999, 1_000]:
           self.check_over_configs(num_train_timesteps=lowercase						)





     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        self.check_over_configs(thresholding=lowercase						)
        for order in [1, 2, 3]:
           for solver_type in ["bh1", "bh2"]:
              for threshold in [0.5, 1.0, 2.0]:
                 for prediction_type in ["epsilon", "sample"]:
                    self.check_over_configs(
                        thresholding=lowercase     ,				prediction_type=lowercase     ,				sample_max_value=lowercase     ,				solver_order=lowercase     ,				solver_type=lowercase     ,				)





     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        for prediction_type in ["epsilon", "v_prediction"]:
           self.check_over_configs(prediction_type=lowercase						)





     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        for solver_type in ["bh1", "bh2"]:
           for order in [1, 2, 3]:
              for prediction_type in ["epsilon", "sample"]:
                 self.check_over_configs(
                     solver_order=lowercase     ,				solver_type=lowercase     ,				prediction_type=lowercase     ,				)
                 lowercase_      :Tuple         =			self.full_loop(
                     solver_order=lowercase     ,				solver_type=lowercase     ,				prediction_type=lowercase     ,				)
                 assert not torch.isnan(lowercase						).any(), "Samples have nan numbers"





     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        self.check_over_configs(lower_order_final=lowercase						)
        self.check_over_configs(lower_order_final=lowercase						)





     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1_000]:
           self.check_over_forward(num_inference_steps=lowercase     ,				time_step=0						)





     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        lowercase_      :List[str]         =			self.full_loop()
        lowercase_      :List[str]         =			torch.mean(torch.abs(lowercase						)						)

        assert abs(result_mean.item() - 0.24_64						) < 1e-3





     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        lowercase_      :Dict         =			self.full_loop(prediction_type="v_prediction"						)
        lowercase_      :Any         =			torch.mean(torch.abs(lowercase						)						)

        assert abs(result_mean.item() - 0.10_14						) < 1e-3





     def 							lowercase__				(			self		: Optional[Any]						):





        """simple docstring"""




        lowercase_      :Union[str, Any]         =			self.scheduler_classes[0]
        lowercase_      :str         =			self.get_scheduler_config(thresholding=lowercase     ,				dynamic_thresholding_ratio=0						)
        lowercase_      :Tuple         =			scheduler_class(**lowercase						)

        lowercase_      :Optional[int]         =			10
        lowercase_      :Optional[int]         =			self.dummy_model()
        lowercase_      :Any         =			self.dummy_sample_deter.half()
        scheduler.set_timesteps(lowercase						)

        for i, t in enumerate(scheduler.timesteps						):
           lowercase_      :Tuple         =			model(lowercase     ,				lowercase						)
           lowercase_      :Any         =			scheduler.step(lowercase     ,				lowercase     ,				lowercase						).prev_sample

        assert sample.dtype == torch.floataa







     def 							lowercase__				(			self		: List[Any]     ,				**lowercase		: Union[str, Any]						):





        """simple docstring"""




        for scheduler_class in self.scheduler_classes:
           lowercase_      :Optional[int]         =			self.get_scheduler_config(**lowercase						)
           lowercase_      :Dict         =			scheduler_class(**lowercase						)

           scheduler.set_timesteps(scheduler.config.num_train_timesteps						)
           assert len(scheduler.timesteps.unique()						) == scheduler.num_inference_steps


