


import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import BertGenerationDecoder, BertGenerationEncoder






class 						_A  :







    """simple docstring"""




    def __init__( self    :   Optional[int]    ,			__UpperCAmelCase    :   str    ,			__UpperCAmelCase    :   int=13    ,			__UpperCAmelCase    :   List[str]=7    ,			__UpperCAmelCase    :   Union[str, Any]=True    ,			__UpperCAmelCase    :   Any=True    ,			__UpperCAmelCase    :   int=99    ,			__UpperCAmelCase    :   str=32    ,			__UpperCAmelCase    :   str=5    ,			__UpperCAmelCase    :   List[Any]=4    ,			__UpperCAmelCase    :   Dict=37    ,			__UpperCAmelCase    :   List[str]="gelu"    ,			__UpperCAmelCase    :   int=0.1    ,			__UpperCAmelCase    :   Tuple=0.1    ,			__UpperCAmelCase    :   int=50    ,			__UpperCAmelCase    :   Any=0.02    ,			__UpperCAmelCase    :   str=True    ,			__UpperCAmelCase    :   int=None    ,			):
      a						:		Union[str, Any]											=      parent
      a						:		Dict											=      batch_size
      a						:		List[str]											=      seq_length
      a						:		Tuple											=      is_training
      a						:		List[Any]											=      use_input_mask
      a						:		Optional[int]											=      vocab_size
      a						:		Any											=      hidden_size
      a						:		Any											=      num_hidden_layers
      a						:		List[str]											=      num_attention_heads
      a						:		Optional[int]											=      intermediate_size
      a						:		List[Any]											=      hidden_act
      a						:		Any											=      hidden_dropout_prob
      a						:		List[Any]											=      attention_probs_dropout_prob
      a						:		Union[str, Any]											=      max_position_embeddings
      a						:		List[Any]											=      initializer_range
      a						:		List[str]											=      use_labels
      a						:		Tuple											=      scope






    def      __snake_case							( self    :   Dict):
      a						:		List[str]											=      ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size)

      a						:		Dict											=      None
      if self.use_input_mask:
        a						:		Dict											=      random_attention_mask([self.batch_size, self.seq_length])

      if self.use_labels:
        a						:		str											=      ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size)

      a						:		Union[str, Any]											=      self.get_config()

      return config, input_ids, input_mask, token_labels






    def      __snake_case							( self    :   Optional[int]):
      return BertGenerationConfig(
          vocab_size=self.vocab_size    ,			hidden_size=self.hidden_size    ,			num_hidden_layers=self.num_hidden_layers    ,			num_attention_heads=self.num_attention_heads    ,			intermediate_size=self.intermediate_size    ,			hidden_act=self.hidden_act    ,			hidden_dropout_prob=self.hidden_dropout_prob    ,			attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,			max_position_embeddings=self.max_position_embeddings    ,			is_decoder=snake_case_    ,			initializer_range=self.initializer_range    ,			)






    def      __snake_case							( self    :   Dict):
      (
          a    
      )						:		Any											=      self.prepare_config_and_inputs()

      a						:		Tuple											=      True
      a						:		Dict											=      floats_tensor([self.batch_size, self.seq_length, self.hidden_size])
      a						:		Union[str, Any]											=      ids_tensor([self.batch_size, self.seq_length]    ,			vocab_size=2)

      return (
          config,
          input_ids,
          input_mask,
          token_labels,
          encoder_hidden_states,
          encoder_attention_mask,
      )






    def      __snake_case							( self    :   Optional[int]    ,			__UpperCAmelCase    :   Tuple    ,			__UpperCAmelCase    :   str    ,			__UpperCAmelCase    :   str    ,			__UpperCAmelCase    :   Optional[int]    ,			**__UpperCAmelCase    :   int    ,			):
      a						:		Optional[Any]											=      BertGenerationEncoder(config=snake_case_)
      model.to(snake_case_)
      model.eval()
      a						:		int											=      model(snake_case_    ,			attention_mask=snake_case_)
      a						:		Optional[Any]											=      model(snake_case_)
      self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size))






    def      __snake_case							( self    :   Dict    ,			__UpperCAmelCase    :   str    ,			__UpperCAmelCase    :   str    ,			__UpperCAmelCase    :   str    ,			__UpperCAmelCase    :   Optional[int]    ,			__UpperCAmelCase    :   Union[str, Any]    ,			__UpperCAmelCase    :   Dict    ,			**__UpperCAmelCase    :   Optional[Any]    ,			):
      a						:		str											=      True
      a						:		Any											=      BertGenerationEncoder(config=snake_case_)
      model.to(snake_case_)
      model.eval()
      a						:		int											=      model(
          snake_case_    ,			attention_mask=snake_case_    ,			encoder_hidden_states=snake_case_    ,			encoder_attention_mask=snake_case_    ,			)
      a						:		Tuple											=      model(
          snake_case_    ,			attention_mask=snake_case_    ,			encoder_hidden_states=snake_case_    ,			)
      self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size))






    def      __snake_case							( self    :   str    ,			__UpperCAmelCase    :   str    ,			__UpperCAmelCase    :   Tuple    ,			__UpperCAmelCase    :   str    ,			__UpperCAmelCase    :   Optional[int]    ,			__UpperCAmelCase    :   Union[str, Any]    ,			__UpperCAmelCase    :   Any    ,			**__UpperCAmelCase    :   Optional[int]    ,			):
      a						:		Any											=      True
      a						:		str											=      True
      a						:		List[Any]											=      BertGenerationDecoder(config=snake_case_).to(snake_case_).eval()

      # first forward pass
      a						:		Union[str, Any]											=      model(
          snake_case_    ,			attention_mask=snake_case_    ,			encoder_hidden_states=snake_case_    ,			encoder_attention_mask=snake_case_    ,			use_cache=snake_case_    ,			)
      a						:		Optional[int]											=      outputs.past_key_values

      # create hypothetical multiple next token and extent to next_input_ids
      a						:		List[Any]											=      ids_tensor((self.batch_size, 3)    ,			config.vocab_size)
      a						:		Any											=      ids_tensor((self.batch_size, 3)    ,			vocab_size=2)

      # append to next input_ids and
      a						:		int											=      torch.cat([input_ids, next_tokens]    ,			dim=-1)
      a						:		Union[str, Any]											=      torch.cat([input_mask, next_mask]    ,			dim=-1)

      a						:		Tuple											=      model(
          snake_case_    ,			attention_mask=snake_case_    ,			encoder_hidden_states=snake_case_    ,			encoder_attention_mask=snake_case_    ,			output_hidden_states=snake_case_    ,			)["""hidden_states"""][0]
      a						:		int											=      model(
          snake_case_    ,			attention_mask=snake_case_    ,			encoder_hidden_states=snake_case_    ,			encoder_attention_mask=snake_case_    ,			past_key_values=snake_case_    ,			output_hidden_states=snake_case_    ,			)["""hidden_states"""][0]

      # select random slice
      a						:		Any											=      ids_tensor((1,)    ,			output_from_past.shape[-1]).item()
      a						:		List[Any]											=      output_from_no_past[:, -3:, random_slice_idx].detach()
      a						:		Dict											=      output_from_past[:, :, random_slice_idx].detach()

      self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1])

      # test that outputs are equal for slice
      self.parent.assertTrue(torch.allclose(snake_case_    ,			snake_case_    ,			atol=1e-3))






    def      __snake_case							( self    :   List[str]    ,			__UpperCAmelCase    :   List[Any]    ,			__UpperCAmelCase    :   Dict    ,			__UpperCAmelCase    :   Optional[Any]    ,			__UpperCAmelCase    :   Any    ,			*__UpperCAmelCase    :   Union[str, Any]    ,			):
      a						:		Dict											=      BertGenerationDecoder(snake_case_)
      model.to(snake_case_)
      model.eval()
      a						:		Dict											=      model(snake_case_    ,			attention_mask=snake_case_    ,			labels=snake_case_)
      self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size))






    def      __snake_case							( self    :   List[str]):
      a						:		Optional[int]											=      self.prepare_config_and_inputs()
      a						:		int											=      {"""input_ids""": input_ids, """attention_mask""": input_mask}
      return config, inputs_dict






@require_torch
class 						_A  (  _a   ,_a   ,_a   ,unittest.TestCase  ):







    """simple docstring"""




    UpperCAmelCase	:							int     =	(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
    UpperCAmelCase	:							List[str]     =	(BertGenerationDecoder,) if is_torch_available() else ()
    UpperCAmelCase	:							Optional[Any]     =	(
        {"""feature-extraction""": BertGenerationEncoder, """text-generation""": BertGenerationDecoder}
        if is_torch_available()
        else {}
    )






    def      __snake_case							( self    :   Tuple):
      a						:		Optional[int]											=      BertGenerationEncoderTester(self)
      a						:		List[Any]											=      ConfigTester(self    ,			config_class=snake_case_    ,			hidden_size=37)






    def      __snake_case							( self    :   List[Any]):
      self.config_tester.run_common_tests()






    def      __snake_case							( self    :   List[str]):
      a						:		str											=      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*snake_case_)






    def      __snake_case							( self    :   Dict):
      a						:		int											=      self.model_tester.prepare_config_and_inputs()
      a						:		Optional[int]											=      """bert"""
      self.model_tester.create_and_check_model(snake_case_    ,			snake_case_    ,			snake_case_    ,			snake_case_)






    def      __snake_case							( self    :   Union[str, Any]):
      a						:		Optional[Any]											=      self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_model_as_decoder(*snake_case_)






    def      __snake_case							( self    :   Any):
      a						:		Dict											=      self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_decoder_model_past_large_inputs(*snake_case_)






    def      __snake_case							( self    :   Optional[int]):
      # This regression test was failing with PyTorch < 1.3
      (
          a    
      )						:		int											=      self.model_tester.prepare_config_and_inputs_for_decoder()

      a						:		Union[str, Any]											=      None

      self.model_tester.create_and_check_model_as_decoder(
          snake_case_    ,			snake_case_    ,			snake_case_    ,			snake_case_    ,			snake_case_    ,			snake_case_    ,			)






    def      __snake_case							( self    :   List[Any]):
      a						:		Optional[int]											=      self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_for_causal_lm(*snake_case_)






    @slow
    def      __snake_case							( self    :   int):
      a						:		str											=      BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder")
      self.assertIsNotNone(snake_case_)






@require_torch
class 						_A  (  unittest.TestCase  ):







    """simple docstring"""




    @slow
    def      __snake_case							( self    :   Any):
      a						:		List[str]											=      BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder")
      a						:		Optional[Any]											=      torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]])
      with torch.no_grad():
        a						:		Dict											=      model(snake_case_)[0]
      a						:		List[Any]											=      torch.Size([1, 8, 1024])
      self.assertEqual(output.shape    ,			snake_case_)
      a						:		str											=      torch.tensor(
          [[[0.1_775, 0.0_083, -0.0_321], [1.6_002, 0.1_287, 0.3_912], [2.1_473, 0.5_791, 0.6_066]]])
      self.assertTrue(torch.allclose(output[:, :3, :3]    ,			snake_case_    ,			atol=1e-4))






@require_torch
class 						_A  (  unittest.TestCase  ):







    """simple docstring"""




    @slow
    def      __snake_case							( self    :   Any):
      a						:		str											=      BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder")
      a						:		List[Any]											=      torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]])
      with torch.no_grad():
        a						:		Union[str, Any]											=      model(snake_case_)[0]
      a						:		List[Any]											=      torch.Size([1, 8, 50358])
      self.assertEqual(output.shape    ,			snake_case_)
      a						:		int											=      torch.tensor(
          [[[-0.5_788, -2.5_994, -3.7_054], [0.0_438, 4.7_997, 1.8_795], [1.5_862, 6.6_409, 4.4_638]]])
      self.assertTrue(torch.allclose(output[:, :3, :3]    ,			snake_case_    ,			atol=1e-4))










"""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

__lowercase    =				logging.get_logger(__name__)






@add_end_docstrings(_a  )
class 						_A  (  _a  ):







    """simple docstring"""




    def __init__( self    :   List[Any]    ,			**__UpperCAmelCase    :   List[Any]):
      super().__init__(**__UpperCAmelCase)

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

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






    def __call__( self    :   str    ,			__UpperCAmelCase    :   Union[str, "Image.Image", List[Dict[str, Any]]]    ,			__UpperCAmelCase    :   Union[str, List[str]] = None    ,			**__UpperCAmelCase    :   List[Any]    ,			):
      if "text_queries" in kwargs:
        a						:		List[Any]											=      kwargs.pop("text_queries")

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






    def      __snake_case							( self    :   Optional[int]    ,			**__UpperCAmelCase    :   List[Any]):
      a						:		str											=      {}
      if "threshold" in kwargs:
        a						:		Dict											=      kwargs["threshold"]
      if "top_k" in kwargs:
        a						:		str											=      kwargs["top_k"]
      return {}, {}, postprocess_params






    def      __snake_case							( self    :   List[Any]    ,			__UpperCAmelCase    :   Optional[Any]):
      a						:		Union[str, Any]											=      load_image(inputs["image"])
      a						:		Any											=      inputs["candidate_labels"]
      if isinstance(__UpperCAmelCase    ,			__UpperCAmelCase):
        a						:		Optional[Any]											=      candidate_labels.split(",")

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






    def      __snake_case							( self    :   Dict    ,			__UpperCAmelCase    :   Optional[int]):
      a						:		List[Any]											=      model_inputs.pop("target_size")
      a						:		Optional[int]											=      model_inputs.pop("candidate_label")
      a						:		List[Any]											=      model_inputs.pop("is_last")

      a						:		List[Any]											=      self.model(**__UpperCAmelCase)

      a						:		Union[str, Any]											=      {"target_size": target_size, "candidate_label": candidate_label, "is_last": is_last, **outputs}
      return model_outputs






    def      __snake_case							( self    :   str    ,			__UpperCAmelCase    :   Dict    ,			__UpperCAmelCase    :   Optional[int]=0.1    ,			__UpperCAmelCase    :   List[str]=None):
      a						:		Dict											=      []
      for model_output in model_outputs:
        a						:		int											=      model_output["candidate_label"]
        a						:		Any											=      BaseModelOutput(__UpperCAmelCase)
        a						:		Optional[Any]											=      self.image_processor.post_process_object_detection(
            outputs=__UpperCAmelCase    ,			threshold=__UpperCAmelCase    ,			target_sizes=model_output["target_size"])[0]

        for index in outputs["scores"].nonzero():
          a						:		Any											=      outputs["scores"][index].item()
          a						:		str											=      self._get_bounding_box(outputs["boxes"][index][0])

          a						:		Optional[Any]											=      {"score": score, "label": label, "box": box}
          results.append(__UpperCAmelCase)

      a						:		str											=      sorted(__UpperCAmelCase    ,			key=lambda __UpperCAmelCase: x["score"]    ,			reverse=__UpperCAmelCase)
      if top_k:
        a						:		Union[str, Any]											=      results[:top_k]

      return results






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






