


from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
    from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


__UpperCamelCase    :						Union[str, Any]     =							logging.get_logger(__name__)

__UpperCamelCase    :						int     =							{
    'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json',
    'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json',
    'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json',
    'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json',
    'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json',
    'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json',
}



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							Any    = "bloom"
   a_				:							str    = ["past_key_values"]
   a_				:							Dict    = {
       "num_hidden_layers": "n_layer",
       "num_attention_heads": "n_head",
   }

   def __init__(  self  : Optional[int]       ,  _lowerCamelCase  : int=2_5_0_8_8_0       ,  _lowerCamelCase  : str=6_4       ,  _lowerCamelCase  : Tuple=2       ,  _lowerCamelCase  : Dict=8       ,  _lowerCamelCase  : Optional[int]=1E-5       ,  _lowerCamelCase  : List[str]=0.02       ,  _lowerCamelCase  : Dict=True       ,  _lowerCamelCase  : Union[str, Any]=1       ,  _lowerCamelCase  : Union[str, Any]=2       ,  _lowerCamelCase  : Tuple=False       ,  _lowerCamelCase  : Optional[Any]=0.0       ,  _lowerCamelCase  : List[str]=0.0       ,  _lowerCamelCase  : List[Any]=1       ,  _lowerCamelCase  : Optional[Any]=False       ,  **_lowerCamelCase  : Any       ,  ):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[Any]       						=						vocab_size
         # Backward compatibility with n_embed kwarg
         __lowerCamelCase     :      Dict       						=						kwargs.pop("""n_embed"""       ,  _lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						hidden_size if n_embed is None else n_embed
         __lowerCamelCase     :      Tuple       						=						n_layer
         __lowerCamelCase     :      Optional[int]       						=						n_head
         __lowerCamelCase     :      Union[str, Any]       						=						layer_norm_epsilon
         __lowerCamelCase     :      Optional[int]       						=						initializer_range
         __lowerCamelCase     :      Dict       						=						use_cache
         __lowerCamelCase     :      Dict       						=						pretraining_tp
         __lowerCamelCase     :      str       						=						apply_residual_connection_post_layernorm
         __lowerCamelCase     :      Union[str, Any]       						=						hidden_dropout
         __lowerCamelCase     :      int       						=						attention_dropout

         __lowerCamelCase     :      List[str]       						=						bos_token_id
         __lowerCamelCase     :      Tuple       						=						eos_token_id
         __lowerCamelCase     :      Dict       						=						slow_but_exact

         super().__init__(bos_token_id=_lowerCamelCase       ,  eos_token_id=_lowerCamelCase       ,  **_lowerCamelCase							)



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							int    = version.parse("1.12"						)

   def __init__(  self  : str       ,  _lowerCamelCase  : PretrainedConfig       ,  _lowerCamelCase  : str = "default"       ,  _lowerCamelCase  : List[PatchingSpec] = None       ,  _lowerCamelCase  : bool = False       ,  ):






         '''simple docstring'''



         super().__init__(_lowerCamelCase       ,  task=_lowerCamelCase       ,  patching_specs=_lowerCamelCase       ,  use_past=_lowerCamelCase							)
         if not getattr(self._config       ,  """pad_token_id"""       ,  _lowerCamelCase							):
               # TODO: how to do that better?
               __lowerCamelCase     :      Optional[Any]       						=						0

   @property
   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[Any]       						=						OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}							)
         if self.use_past:
               # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
               self.fill_with_past_key_values_(_lowerCamelCase       ,  direction="""inputs"""       ,  inverted_values_shape=_lowerCamelCase							)
               __lowerCamelCase     :      Union[str, Any]       						=						{0: """batch""", 1: """past_sequence + sequence"""}
         else:
               __lowerCamelCase     :      Optional[Any]       						=						{0: """batch""", 1: """sequence"""}

         return common_inputs

   @property
   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         return self._config.n_layer

   @property
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         return self._config.n_head

   @property
   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         return 1E-3

   def   _snake_case    (  self  : Optional[int]       ,  _lowerCamelCase  : "PreTrainedTokenizer"       ,  _lowerCamelCase  : int = -1       ,  _lowerCamelCase  : int = -1       ,  _lowerCamelCase  : bool = False       ,  _lowerCamelCase  : Optional["TensorType"] = None       ,  ):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						super(_lowerCamelCase       ,  self							).generate_dummy_inputs(
             _lowerCamelCase       ,  batch_size=_lowerCamelCase       ,  seq_length=_lowerCamelCase       ,  is_pair=_lowerCamelCase       ,  framework=_lowerCamelCase							)

         # We need to order the input in the way they appears in the forward()
         __lowerCamelCase     :      Any       						=						OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}							)

         # Need to add the past_keys
         if self.use_past:
               if not is_torch_available():
                     raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""							)
               else:
                     import torch

                     __lowerCamelCase						, __lowerCamelCase     :      str       						=						common_inputs["""input_ids"""].shape
                     # Not using the same length for past_key_values
                     __lowerCamelCase     :      Any       						=						seqlen + 2
                     __lowerCamelCase     :      List[Any]       						=						self._config.hidden_size // self.num_attention_heads
                     __lowerCamelCase     :      Optional[int]       						=						(
                         batch * self.num_attention_heads,
                         head_dim,
                         past_key_values_length,
                     )
                     __lowerCamelCase     :      str       						=						(
                         batch * self.num_attention_heads,
                         past_key_values_length,
                         head_dim,
                     )
                     __lowerCamelCase     :      List[str]       						=						[
                         (torch.zeros(_lowerCamelCase							), torch.zeros(_lowerCamelCase							)) for _ in range(self.num_layers							)
                     ]

         __lowerCamelCase     :      List[Any]       						=						common_inputs["""attention_mask"""]
         if self.use_past:
               __lowerCamelCase     :      Tuple       						=						ordered_inputs["""attention_mask"""].dtype
               __lowerCamelCase     :      str       						=						torch.cat(
                   [ordered_inputs["""attention_mask"""], torch.ones(_lowerCamelCase       ,  _lowerCamelCase       ,  dtype=_lowerCamelCase							)]       ,  dim=1							)

         return ordered_inputs


   @property
   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         return 1_3







import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
    import torch

if is_tf_available():
    import tensorflow as tf



@require_vision
@require_torchvision
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      Union[str, Any]       						=						SamImageProcessor()
         __lowerCamelCase     :      int       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : Tuple       ,  **_lowerCamelCase  : Any							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      Optional[int]       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=self.get_image_processor()							)
         processor.save_pretrained(self.tmpdirname							)

         __lowerCamelCase     :      Optional[int]       						=						self.get_image_processor(do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         __lowerCamelCase     :      Optional[Any]       						=						SamProcessor.from_pretrained(self.tmpdirname       ,  do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         self.assertEqual(processor.image_processor.to_json_string()       ,  image_processor_add_kwargs.to_json_string()							)
         self.assertIsInstance(processor.image_processor       ,  _lowerCamelCase							)

   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      Optional[Any]       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      List[str]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""np"""							)
         __lowerCamelCase     :      List[Any]       						=						processor(images=_lowerCamelCase       ,  return_tensors="""np"""							)

         input_feat_extract.pop("""original_sizes"""							)  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop("""reshaped_input_sizes"""							)  # pop original_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum()       ,  input_processor[key].sum()       ,  delta=1E-2							)


   @require_torch
   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						self.get_image_processor()

         __lowerCamelCase     :      Union[str, Any]       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						[torch.ones((1, 3, 5, 5)							)]

         __lowerCamelCase     :      Any       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      Any       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      Tuple       						=						processor.post_process_masks(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Union[str, Any]       						=						processor.post_process_masks(
             _lowerCamelCase       ,  torch.tensor(_lowerCamelCase							)       ,  torch.tensor(_lowerCamelCase							)							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         # should also work with np
         __lowerCamelCase     :      Any       						=						[np.ones((1, 3, 5, 5)							)]
         __lowerCamelCase     :      int       						=						processor.post_process_masks(_lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)							)

         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      List[str]       						=						[[1, 0], [0, 1]]
         with self.assertRaises(_lowerCamelCase							):
               __lowerCamelCase     :      Union[str, Any]       						=						processor.post_process_masks(_lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)							)



@require_vision
@require_tf
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[Any]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      int       						=						SamImageProcessor()
         __lowerCamelCase     :      Dict       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : List[str]       ,  **_lowerCamelCase  : List[Any]							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      str       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						SamProcessor(image_processor=self.get_image_processor()							)
         processor.save_pretrained(self.tmpdirname							)

         __lowerCamelCase     :      Dict       						=						self.get_image_processor(do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         __lowerCamelCase     :      Tuple       						=						SamProcessor.from_pretrained(self.tmpdirname       ,  do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         self.assertEqual(processor.image_processor.to_json_string()       ,  image_processor_add_kwargs.to_json_string()							)
         self.assertIsInstance(processor.image_processor       ,  _lowerCamelCase							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      str       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      List[Any]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""np"""							)
         __lowerCamelCase     :      Union[str, Any]       						=						processor(images=_lowerCamelCase       ,  return_tensors="""np"""							)

         input_feat_extract.pop("""original_sizes"""							)  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop("""reshaped_input_sizes"""							)  # pop reshaped_input_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum()       ,  input_processor[key].sum()       ,  delta=1E-2							)


   @require_tf
   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      str       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						[tf.ones((1, 3, 5, 5)							)]

         __lowerCamelCase     :      Optional[Any]       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      List[str]       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      Optional[int]       						=						processor.post_process_masks(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""tf"""							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Optional[Any]       						=						processor.post_process_masks(
             _lowerCamelCase       ,  tf.convert_to_tensor(_lowerCamelCase							)       ,  tf.convert_to_tensor(_lowerCamelCase							)       ,  return_tensors="""tf"""       ,  )
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         # should also work with np
         __lowerCamelCase     :      Tuple       						=						[np.ones((1, 3, 5, 5)							)]
         __lowerCamelCase     :      str       						=						processor.post_process_masks(
             _lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)       ,  return_tensors="""tf"""							)

         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Union[str, Any]       						=						[[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError							):
               __lowerCamelCase     :      Optional[Any]       						=						processor.post_process_masks(
                   _lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)       ,  return_tensors="""tf"""							)



@require_vision
@require_torchvision
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      Union[str, Any]       						=						SamImageProcessor()
         __lowerCamelCase     :      Dict       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : Optional[int]       ,  **_lowerCamelCase  : Tuple							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      str       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   @is_pt_tf_cross_test
   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						np.random.randint(0       ,  2       ,  size=(1, 3, 5, 5)							).astype(np.floataa							)
         __lowerCamelCase     :      Tuple       						=						[tf.convert_to_tensor(_lowerCamelCase							)]
         __lowerCamelCase     :      List[str]       						=						[torch.tensor(_lowerCamelCase							)]

         __lowerCamelCase     :      Optional[int]       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      Tuple       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      int       						=						processor.post_process_masks(
             _lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""tf"""							)
         __lowerCamelCase     :      Any       						=						processor.post_process_masks(
             _lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""pt"""							)

         self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()							)							)


   @is_pt_tf_cross_test
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						self.get_image_processor()

         __lowerCamelCase     :      int       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      Any       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      Any       						=						image_processor(_lowerCamelCase       ,  return_tensors="""pt"""							)["""pixel_values"""].numpy()
         __lowerCamelCase     :      Any       						=						processor(images=_lowerCamelCase       ,  return_tensors="""pt"""							)["""pixel_values"""].numpy()

         __lowerCamelCase     :      Dict       						=						image_processor(_lowerCamelCase       ,  return_tensors="""tf"""							)["""pixel_values"""].numpy()
         __lowerCamelCase     :      int       						=						processor(images=_lowerCamelCase       ,  return_tensors="""tf"""							)["""pixel_values"""].numpy()

         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)
         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)
         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)



