






import unittest

import numpy as np
import requests

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
 import torch

 from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
 lowerCamelCase			:					Optional[int]         =	False

if is_vision_available():
 from PIL import Image

 from transformers import PixaStructImageProcessor



class 							_UpperCamelCase			(unittest.TestCase			):


      def __init__(	self					,						__UpperCamelCase					,						__UpperCamelCase=7					,						__UpperCamelCase=3					,						__UpperCamelCase=1_8					,						__UpperCamelCase=3_0					,						__UpperCamelCase=4_0_0					,						__UpperCamelCase=None					,						__UpperCamelCase=True					,						__UpperCamelCase=True					,						__UpperCamelCase=None					,						)->	Tuple:
           __lowerCAmelCase								=					size if size is not None else {"height": 2_0, "width": 2_0}
           __lowerCAmelCase								=					parent
           __lowerCAmelCase								=					batch_size
           __lowerCAmelCase								=					num_channels
           __lowerCAmelCase								=					image_size
           __lowerCAmelCase								=					min_resolution
           __lowerCAmelCase								=					max_resolution
           __lowerCAmelCase								=					size
           __lowerCAmelCase								=					do_normalize
           __lowerCAmelCase								=					do_convert_rgb
           __lowerCAmelCase								=					[5_1_2, 1_0_2_4, 2_0_4_8, 4_0_9_6]
           __lowerCAmelCase								=					patch_size if patch_size is not None else {"height": 1_6, "width": 1_6}


      def  __UpperCAmelCase		(	self	)->	Optional[Any]:
           return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}


      def  __UpperCAmelCase		(	self	)->	Optional[Any]:
           __lowerCAmelCase								=					"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg"
           __lowerCAmelCase								=					Image.open(requests.get(_A					,						stream=_A	).raw	).convert("RGB"	)
           return raw_image



@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11							,		reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`."""							,		)
@require_torch
@require_vision
class 							_UpperCamelCase			(_a							,		unittest.TestCase			):
      snake_case_  =							PixaStructImageProcessor if is_vision_available() else None


      def  __UpperCAmelCase		(	self	)->	Union[str, Any]:
           __lowerCAmelCase								=					PixaStructImageProcessingTester(self	)


      @property
      def  __UpperCAmelCase		(	self	)->	List[str]:
           return self.image_processor_tester.prepare_image_processor_dict()


      def  __UpperCAmelCase		(	self	)->	List[Any]:
           __lowerCAmelCase								=					self.image_processing_class(**self.image_processor_dict	)
           self.assertTrue(hasattr(_A					,						"do_normalize"	)	)
           self.assertTrue(hasattr(_A					,						"do_convert_rgb"	)	)


      def  __UpperCAmelCase		(	self	)->	str:
           __lowerCAmelCase								=					self.image_processor_tester.prepare_dummy_image()

           __lowerCAmelCase								=					self.image_processing_class(**self.image_processor_dict	)
           __lowerCAmelCase								=					2_0_4_8

           __lowerCAmelCase								=					image_processor(_A					,						return_tensors="pt"					,						max_patches=_A	)
           self.assertTrue(torch.allclose(inputs.flattened_patches.mean()					,						torch.tensor(0.0_6_0_6	)					,						atol=1e-3					,						rtol=1e-3	)	)


      def  __UpperCAmelCase		(	self	)->	List[Any]:
           # Initialize image_processor
           __lowerCAmelCase								=					self.image_processing_class(**self.image_processor_dict	)
           # create random PIL images
           __lowerCAmelCase								=					prepare_image_inputs(self.image_processor_tester					,						equal_resolution=_A	)
           for image in image_inputs:
                self.assertIsInstance(_A					,						Image.Image	)

           # Test not batched input
           __lowerCAmelCase								=					(
               (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
               * self.image_processor_tester.num_channels
           ) + 2

           for max_patch in self.image_processor_tester.max_patches:
                # Test not batched input
                __lowerCAmelCase								=					image_processor(
                    image_inputs[0]					,						return_tensors="pt"					,						max_patches=_A	).flattened_patches
                self.assertEqual(
                    encoded_images.shape					,						(1, max_patch, expected_hidden_dim)					,						)

                # Test batched
                __lowerCAmelCase								=					image_processor(
                    _A					,						return_tensors="pt"					,						max_patches=_A	).flattened_patches
                self.assertEqual(
                    encoded_images.shape					,						(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)					,						)


      def  __UpperCAmelCase		(	self	)->	List[str]:
           # Initialize image_processor
           __lowerCAmelCase								=					self.image_processing_class(**self.image_processor_dict	)
           # create random PIL images
           __lowerCAmelCase								=					prepare_image_inputs(self.image_processor_tester					,						equal_resolution=_A	)
           for image in image_inputs:
                self.assertIsInstance(_A					,						Image.Image	)

           # Test not batched input
           __lowerCAmelCase								=					(
               (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
               * self.image_processor_tester.num_channels
           ) + 2

           __lowerCAmelCase								=					True

           for max_patch in self.image_processor_tester.max_patches:
                # Test not batched input
                with self.assertRaises(_A	):
                     __lowerCAmelCase								=					image_processor(
                         image_inputs[0]					,						return_tensors="pt"					,						max_patches=_A	).flattened_patches

                __lowerCAmelCase								=					"Hello"

                __lowerCAmelCase								=					image_processor(
                    image_inputs[0]					,						return_tensors="pt"					,						max_patches=_A					,						header_text=_A	).flattened_patches
                self.assertEqual(
                    encoded_images.shape					,						(1, max_patch, expected_hidden_dim)					,						)

                # Test batched
                __lowerCAmelCase								=					image_processor(
                    _A					,						return_tensors="pt"					,						max_patches=_A					,						header_text=_A	).flattened_patches
                self.assertEqual(
                    encoded_images.shape					,						(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)					,						)


      def  __UpperCAmelCase		(	self	)->	Optional[Any]:
           # Initialize image_processor
           __lowerCAmelCase								=					self.image_processing_class(**self.image_processor_dict	)
           # create random numpy tensors
           __lowerCAmelCase								=					prepare_image_inputs(self.image_processor_tester					,						equal_resolution=_A					,						numpify=_A	)
           for image in image_inputs:
                self.assertIsInstance(_A					,						np.ndarray	)

           __lowerCAmelCase								=					(
               (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
               * self.image_processor_tester.num_channels
           ) + 2

           for max_patch in self.image_processor_tester.max_patches:
                # Test not batched input
                __lowerCAmelCase								=					image_processor(
                    image_inputs[0]					,						return_tensors="pt"					,						max_patches=_A	).flattened_patches
                self.assertEqual(
                    encoded_images.shape					,						(1, max_patch, expected_hidden_dim)					,						)

                # Test batched
                __lowerCAmelCase								=					image_processor(
                    _A					,						return_tensors="pt"					,						max_patches=_A	).flattened_patches
                self.assertEqual(
                    encoded_images.shape					,						(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)					,						)


      def  __UpperCAmelCase		(	self	)->	Dict:
           # Initialize image_processor
           __lowerCAmelCase								=					self.image_processing_class(**self.image_processor_dict	)
           # create random PyTorch tensors
           __lowerCAmelCase								=					prepare_image_inputs(self.image_processor_tester					,						equal_resolution=_A					,						torchify=_A	)
           for image in image_inputs:
                self.assertIsInstance(_A					,						torch.Tensor	)

           # Test not batched input
           __lowerCAmelCase								=					(
               (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
               * self.image_processor_tester.num_channels
           ) + 2

           for max_patch in self.image_processor_tester.max_patches:
                # Test not batched input
                __lowerCAmelCase								=					image_processor(
                    image_inputs[0]					,						return_tensors="pt"					,						max_patches=_A	).flattened_patches
                self.assertEqual(
                    encoded_images.shape					,						(1, max_patch, expected_hidden_dim)					,						)

                # Test batched
                __lowerCAmelCase								=					image_processor(
                    _A					,						return_tensors="pt"					,						max_patches=_A	).flattened_patches
                self.assertEqual(
                    encoded_images.shape					,						(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)					,						)



@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11							,		reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`."""							,		)
@require_torch
@require_vision
class 							_UpperCamelCase			(_a							,		unittest.TestCase			):
      snake_case_  =							PixaStructImageProcessor if is_vision_available() else None


      def  __UpperCAmelCase		(	self	)->	List[Any]:
           __lowerCAmelCase								=					PixaStructImageProcessingTester(self					,						num_channels=4	)
           __lowerCAmelCase								=					3


      @property
      def  __UpperCAmelCase		(	self	)->	str:
           return self.image_processor_tester.prepare_image_processor_dict()


      def  __UpperCAmelCase		(	self	)->	List[str]:
           __lowerCAmelCase								=					self.image_processing_class(**self.image_processor_dict	)
           self.assertTrue(hasattr(_A					,						"do_normalize"	)	)
           self.assertTrue(hasattr(_A					,						"do_convert_rgb"	)	)


      def  __UpperCAmelCase		(	self	)->	List[str]:
           # Initialize image_processor
           __lowerCAmelCase								=					self.image_processing_class(**self.image_processor_dict	)
           # create random PIL images
           __lowerCAmelCase								=					prepare_image_inputs(self.image_processor_tester					,						equal_resolution=_A	)
           for image in image_inputs:
                self.assertIsInstance(_A					,						Image.Image	)

           # Test not batched input
           __lowerCAmelCase								=					(
               (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
               * (self.image_processor_tester.num_channels - 1)
           ) + 2

           for max_patch in self.image_processor_tester.max_patches:
                # Test not batched input
                __lowerCAmelCase								=					image_processor(
                    image_inputs[0]					,						return_tensors="pt"					,						max_patches=_A	).flattened_patches
                self.assertEqual(
                    encoded_images.shape					,						(1, max_patch, expected_hidden_dim)					,						)

                # Test batched
                __lowerCAmelCase								=					image_processor(
                    _A					,						return_tensors="pt"					,						max_patches=_A	).flattened_patches
                self.assertEqual(
                    encoded_images.shape					,						(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)					,						)














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


lowerCamelCase			:					List[str]         =	logging.get_logger(__name__)

lowerCamelCase			:					Any         =	{
    '''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_			):
      snake_case_  =							"""bloom"""
      snake_case_  =							["""past_key_values"""]
      snake_case_  =							{
          """num_hidden_layers""": """n_layer""",
          """num_attention_heads""": """n_head""",
      }


      def __init__(	self					,						__UpperCamelCase=2_5_0_8_8_0					,						__UpperCamelCase=6_4					,						__UpperCamelCase=2					,						__UpperCamelCase=8					,						__UpperCamelCase=1e-5					,						__UpperCamelCase=0.0_2					,						__UpperCamelCase=True					,						__UpperCamelCase=1					,						__UpperCamelCase=2					,						__UpperCamelCase=False					,						__UpperCamelCase=0.0					,						__UpperCamelCase=0.0					,						__UpperCamelCase=1					,						__UpperCamelCase=False					,						**__UpperCamelCase					,						)->	Dict:
           __lowerCAmelCase								=					vocab_size
           # Backward compatibility with n_embed kwarg
           __lowerCAmelCase								=					kwargs.pop("n_embed"					,						__UpperCamelCase	)
           __lowerCAmelCase								=					hidden_size if n_embed is None else n_embed
           __lowerCAmelCase								=					n_layer
           __lowerCAmelCase								=					n_head
           __lowerCAmelCase								=					layer_norm_epsilon
           __lowerCAmelCase								=					initializer_range
           __lowerCAmelCase								=					use_cache
           __lowerCAmelCase								=					pretraining_tp
           __lowerCAmelCase								=					apply_residual_connection_post_layernorm
           __lowerCAmelCase								=					hidden_dropout
           __lowerCAmelCase								=					attention_dropout

           __lowerCAmelCase								=					bos_token_id
           __lowerCAmelCase								=					eos_token_id
           __lowerCAmelCase								=					slow_but_exact

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



class 							_UpperCamelCase			(a_			):
      snake_case_  =							version.parse("""1.12"""			)


      def __init__(	self					,						__UpperCamelCase					,						__UpperCamelCase = "default"					,						__UpperCamelCase = None					,						__UpperCamelCase = False					,						)->	str:
           super().__init__(__UpperCamelCase					,						task=__UpperCamelCase					,						patching_specs=__UpperCamelCase					,						use_past=__UpperCamelCase	)
           if not getattr(self._config					,						"pad_token_id"					,						__UpperCamelCase	):
                # TODO: how to do that better?
                __lowerCAmelCase								=					0


      @property
      def  __UpperCAmelCase		(	self	)->	Mapping[str, Mapping[int, str]]:
           __lowerCAmelCase								=					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_(__UpperCamelCase					,						direction="inputs"					,						inverted_values_shape=__UpperCamelCase	)
                __lowerCAmelCase								=					{0: "batch", 1: "past_sequence + sequence"}
           else:
                __lowerCAmelCase								=					{0: "batch", 1: "sequence"}

           return common_inputs


      @property
      def  __UpperCAmelCase		(	self	)->	int:
           return self._config.n_layer


      @property
      def  __UpperCAmelCase		(	self	)->	int:
           return self._config.n_head


      @property
      def  __UpperCAmelCase		(	self	)->	float:
           return 1e-3


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase = -1					,						__UpperCamelCase = -1					,						__UpperCamelCase = False					,						__UpperCamelCase = None					,						)->	Mapping[str, Any]:
           __lowerCAmelCase								=					super(__UpperCamelCase					,						self	).generate_dummy_inputs(
               __UpperCamelCase					,						batch_size=__UpperCamelCase					,						seq_length=__UpperCamelCase					,						is_pair=__UpperCamelCase					,						framework=__UpperCamelCase	)

           # We need to order the input in the way they appears in the forward()
           __lowerCAmelCase								=					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								=					common_inputs["input_ids"].shape
                     # Not using the same length for past_key_values
                     __lowerCAmelCase								=					seqlen + 2
                     __lowerCAmelCase								=					self._config.hidden_size // self.num_attention_heads
                     __lowerCAmelCase								=					(
                         batch * self.num_attention_heads,
                         head_dim,
                         past_key_values_length,
                     )
                     __lowerCAmelCase								=					(
                         batch * self.num_attention_heads,
                         past_key_values_length,
                         head_dim,
                     )
                     __lowerCAmelCase								=					[
                         (torch.zeros(__UpperCamelCase	), torch.zeros(__UpperCamelCase	)) for _ in range(self.num_layers	)
                     ]

           __lowerCAmelCase								=					common_inputs["attention_mask"]
           if self.use_past:
                __lowerCAmelCase								=					ordered_inputs["attention_mask"].dtype
                __lowerCAmelCase								=					torch.cat(
                    [ordered_inputs["attention_mask"], torch.ones(__UpperCamelCase					,						__UpperCamelCase					,						dtype=__UpperCamelCase	)]					,						dim=1	)

           return ordered_inputs


      @property
      def  __UpperCAmelCase		(	self	)->	int:
           return 1_3






