
import unittest

import numpy as np

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

if is_vision_available():
 from PIL import Image

 from transformers import PoolFormerImageProcessor


class 						__lowerCAmelCase	(	unittest.TestCase      ):



      """simple docstring"""







      def __init__(      self		:       Any				,       _snake_case		:       Tuple				,       _snake_case		:       int=7				,       _snake_case		:       Any=3				,       _snake_case		:       Dict=30				,       _snake_case		:       List[str]=400				,       _snake_case		:       Any=True				,       _snake_case		:       Optional[Any]=None				,       _snake_case		:       Union[str, Any]=0.9				,       _snake_case		:       Dict=None				,       _snake_case		:       Optional[Any]=True				,       _snake_case		:       Optional[Any]=[0.5, 0.5, 0.5]				,       _snake_case		:       List[str]=[0.5, 0.5, 0.5]				,       ):
          __lowercase		:			Optional[Any]	    =       size if size is not None else {'''shortest_edge''': 30}
          __lowercase		:			Tuple	    =       crop_size if crop_size is not None else {'''height''': 30, '''width''': 30}
          __lowercase		:			Optional[Any]	    =       parent
          __lowercase		:			List[Any]	    =       batch_size
          __lowercase		:			int	    =       num_channels
          __lowercase		:			Optional[Any]	    =       min_resolution
          __lowercase		:			Any	    =       max_resolution
          __lowercase		:			List[str]	    =       do_resize_and_center_crop
          __lowercase		:			Optional[int]	    =       size
          __lowercase		:			Any	    =       crop_pct
          __lowercase		:			Dict	    =       crop_size
          __lowercase		:			Tuple	    =       do_normalize
          __lowercase		:			int	    =       image_mean
          __lowercase		:			Union[str, Any]	    =       image_std




      def 		snake_case_			(      self		:       str				):
          return {
              "size": self.size,
              "do_resize_and_center_crop": self.do_resize_and_center_crop,
              "crop_pct": self.crop_pct,
              "crop_size": self.crop_size,
              "do_normalize": self.do_normalize,
              "image_mean": self.image_mean,
              "image_std": self.image_std,
          }






@require_torch
@require_vision
class 						__lowerCAmelCase	(	lowerCAmelCase_ , unittest.TestCase      ):



      """simple docstring"""







      A__	:			Any        =    PoolFormerImageProcessor if is_vision_available() else None




      def 		snake_case_			(      self		:       Optional[Any]				):
          __lowercase		:			int	    =       PoolFormerImageProcessingTester(self				)




      @property
      def 		snake_case_			(      self		:       Union[str, Any]				):
          return self.image_processor_tester.prepare_image_processor_dict()




      def 		snake_case_			(      self		:       str				):
          __lowercase		:			int	    =       self.image_processing_class(**self.image_processor_dict				)
          self.assertTrue(hasattr(_snake_case				,       '''do_resize_and_center_crop'''				)				)
          self.assertTrue(hasattr(_snake_case				,       '''size'''				)				)
          self.assertTrue(hasattr(_snake_case				,       '''crop_pct'''				)				)
          self.assertTrue(hasattr(_snake_case				,       '''do_normalize'''				)				)
          self.assertTrue(hasattr(_snake_case				,       '''image_mean'''				)				)
          self.assertTrue(hasattr(_snake_case				,       '''image_std'''				)				)




      def 		snake_case_			(      self		:       int				):
          __lowercase		:			List[str]	    =       self.image_processing_class.from_dict(self.image_processor_dict				)
          self.assertEqual(image_processor.size				,       {'''shortest_edge''': 30}				)
          self.assertEqual(image_processor.crop_size				,       {'''height''': 30, '''width''': 30}				)

          __lowercase		:			int	    =       self.image_processing_class.from_dict(self.image_processor_dict				,       size=42				,       crop_size=84				)
          self.assertEqual(image_processor.size				,       {'''shortest_edge''': 42}				)
          self.assertEqual(image_processor.crop_size				,       {'''height''': 84, '''width''': 84}				)




      def 		snake_case_			(      self		:       List[str]				):
          pass




      def 		snake_case_			(      self		:       List[str]				):
          # Initialize image_processing
          __lowercase		:			List[Any]	    =       self.image_processing_class(**self.image_processor_dict				)
          # create random PIL images
          __lowercase		:			str	    =       prepare_image_inputs(self.image_processor_tester				,       equal_resolution=_snake_case				)
          for image in image_inputs:
              self.assertIsInstance(_snake_case				,       Image.Image				)

          # Test not batched input
          __lowercase		:			Any	    =       image_processing(image_inputs[0]				,       return_tensors='''pt'''				).pixel_values

          self.assertEqual(
              encoded_images.shape				,       (
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )

          # Test batched
          __lowercase		:			int	    =       image_processing(_snake_case				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape				,       (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )




      def 		snake_case_			(      self		:       int				):
          # Initialize image_processing
          __lowercase		:			Dict	    =       self.image_processing_class(**self.image_processor_dict				)
          # create random numpy tensors
          __lowercase		:			Union[str, Any]	    =       prepare_image_inputs(self.image_processor_tester				,       equal_resolution=_snake_case				,       numpify=_snake_case				)
          for image in image_inputs:
              self.assertIsInstance(_snake_case				,       np.ndarray				)

          # Test not batched input
          __lowercase		:			Tuple	    =       image_processing(image_inputs[0]				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape				,       (
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )

          # Test batched
          __lowercase		:			Optional[Any]	    =       image_processing(_snake_case				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape				,       (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )




      def 		snake_case_			(      self		:       List[str]				):
          # Initialize image_processing
          __lowercase		:			Optional[Any]	    =       self.image_processing_class(**self.image_processor_dict				)
          # create random PyTorch tensors
          __lowercase		:			List[Any]	    =       prepare_image_inputs(self.image_processor_tester				,       equal_resolution=_snake_case				,       torchify=_snake_case				)
          for image in image_inputs:
              self.assertIsInstance(_snake_case				,       torch.Tensor				)

          # Test not batched input
          __lowercase		:			Optional[int]	    =       image_processing(image_inputs[0]				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape				,       (
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )

          # Test batched
          __lowercase		:			str	    =       image_processing(_snake_case				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape				,       (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )


import unittest

from datasets import load_dataset

from transformers import BloomTokenizerFast
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class 						__lowerCAmelCase	(	lowerCAmelCase_ , unittest.TestCase      ):



      """simple docstring"""







      A__	:			Dict        =    None
      A__	:			Any        =    BloomTokenizerFast
      A__	:			Optional[Any]        =    BloomTokenizerFast
      A__	:			List[Any]        =    True
      A__	:			int        =    False
      A__	:			Any        =    '''tokenizer_file'''
      A__	:			Optional[int]        =    {'''bos_token''': '''<s>''', '''eos_token''': '''</s>''', '''unk_token''': '''<unk>''', '''pad_token''': '''<pad>'''}




      def 		snake_case_			(      self		:       Any				):
          super().setUp()
          __lowercase		:			int	    =       BloomTokenizerFast.from_pretrained('''bigscience/tokenizer'''				)
          tokenizer.save_pretrained(self.tmpdirname				)




      def 		snake_case_			(      self		:       Dict				,       **_snake_case		:       Any				):
          kwargs.update(self.special_tokens_map				)
          return BloomTokenizerFast.from_pretrained(self.tmpdirname				,       **_snake_case				)




      def 		snake_case_			(      self		:       Any				):
          __lowercase		:			Dict	    =       self.get_rust_tokenizer()

          __lowercase		:			Optional[int]	    =       ['''The quick brown fox</s>''', '''jumps over the lazy dog</s>''']
          __lowercase		:			Union[str, Any]	    =       [[2175, 2_3714, 7_3173, 14_4252, 2], [77, 13_2619, 3478, 368, 10_9586, 3_5433, 2]]

          __lowercase		:			Optional[int]	    =       tokenizer.batch_encode_plus(_snake_case				)['''input_ids''']
          self.assertListEqual(_snake_case				,       _snake_case				)

          __lowercase		:			List[Any]	    =       tokenizer.batch_decode(_snake_case				)
          self.assertListEqual(_snake_case				,       _snake_case				)




      def 		snake_case_			(      self		:       str				,       _snake_case		:       int=6				):
          for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
              with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})'				):
                  __lowercase		:			Dict	    =       self.rust_tokenizer_class.from_pretrained(_snake_case				,       **_snake_case				)
                  # tokenizer_r.pad_token = None # Hotfixing padding = None
                  # Simple input
                  __lowercase		:			List[Any]	    =       '''This is a simple input'''
                  __lowercase		:			List[str]	    =       ['''This is a simple input 1''', '''This is a simple input 2''']
                  __lowercase		:			Optional[Any]	    =       ('''This is a simple input''', '''This is a pair''')
                  __lowercase		:			Any	    =       [
                      ('''This is a simple input 1''', '''This is a simple input 2'''),
                      ('''This is a simple pair 1''', '''This is a simple pair 2'''),
                  ]

                  # Simple input tests
                  try:
                      tokenizer_r.encode(_snake_case				,       max_length=_snake_case				)
                      tokenizer_r.encode_plus(_snake_case				,       max_length=_snake_case				)

                      tokenizer_r.batch_encode_plus(_snake_case				,       max_length=_snake_case				)
                      tokenizer_r.encode(_snake_case				,       max_length=_snake_case				)
                      tokenizer_r.batch_encode_plus(_snake_case				,       max_length=_snake_case				)
                  except ValueError:
                      self.fail('''Bloom Tokenizer should be able to deal with padding'''				)

                  __lowercase		:			Dict	    =       None  # Hotfixing padding = None
                  self.assertRaises(_snake_case				,       tokenizer_r.encode				,       _snake_case				,       max_length=_snake_case				,       padding='''max_length'''				)

                  # Simple input
                  self.assertRaises(_snake_case				,       tokenizer_r.encode_plus				,       _snake_case				,       max_length=_snake_case				,       padding='''max_length'''				)

                  # Simple input
                  self.assertRaises(
                      _snake_case				,       tokenizer_r.batch_encode_plus				,       _snake_case				,       max_length=_snake_case				,       padding='''max_length'''				,       )

                  # Pair input
                  self.assertRaises(_snake_case				,       tokenizer_r.encode				,       _snake_case				,       max_length=_snake_case				,       padding='''max_length'''				)

                  # Pair input
                  self.assertRaises(_snake_case				,       tokenizer_r.encode_plus				,       _snake_case				,       max_length=_snake_case				,       padding='''max_length'''				)

                  # Pair input
                  self.assertRaises(
                      _snake_case				,       tokenizer_r.batch_encode_plus				,       _snake_case				,       max_length=_snake_case				,       padding='''max_length'''				,       )




      def 		snake_case_			(      self		:       Optional[int]				):
          __lowercase		:			List[Any]	    =       self.get_rust_tokenizer()
          __lowercase		:			int	    =       load_dataset('''xnli'''				,       '''all_languages'''				,       split='''test'''				,       streaming=_snake_case				)

          __lowercase		:			Optional[Any]	    =       next(iter(_snake_case				)				)['''premise''']  # pick up one data
          __lowercase		:			List[str]	    =       list(sample_data.values()				)

          __lowercase		:			List[str]	    =       list(map(tokenizer.encode				,       _snake_case				)				)
          __lowercase		:			Tuple	    =       [tokenizer.decode(_snake_case				,       clean_up_tokenization_spaces=_snake_case				) for x in output_tokens]
          self.assertListEqual(_snake_case				,       _snake_case				)




      def 		snake_case_			(      self		:       List[Any]				):
          # The test has to be overriden because BLOOM uses ALiBi positional embeddings that does not have
          # any sequence length constraints. This test of the parent class will fail since it relies on the
          # maximum sequence length of the positoonal embeddings.
          self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map				)				,       1				)
          self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values()				)[0]				)				,       1				)


