
'''simple docstring'''



import json
import os
import tempfile

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


if is_torch_available():
     import numpy as np
     import torch

if is_vision_available():
     from PIL import Image
def 				lowerCAmelCase_		(			SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_=False      , SCREAMING_SNAKE_CASE_=False      , SCREAMING_SNAKE_CASE_=False  )       ->       Optional[int]:




   """simple docstring"""
   assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

   _SCREAMING_SNAKE_CASE       	=			[]
   for i in range(image_processor_tester.batch_size  ):
      if equal_resolution:
         _SCREAMING_SNAKE_CASE       	=			_SCREAMING_SNAKE_CASE       	=			image_processor_tester.max_resolution
      else:
         # To avoid getting image width/height 0
         _SCREAMING_SNAKE_CASE       	=			image_processor_tester.min_resolution
         if getattr(SCREAMING_SNAKE_CASE_      , """size_divisor"""      , SCREAMING_SNAKE_CASE_  ):
            # If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
            _SCREAMING_SNAKE_CASE       	=			max(image_processor_tester.size_divisor      , SCREAMING_SNAKE_CASE_  )
         _SCREAMING_SNAKE_CASE	,							_SCREAMING_SNAKE_CASE       	=			np.random.choice(np.arange(SCREAMING_SNAKE_CASE_      , image_processor_tester.max_resolution  )      , 2  )
      image_inputs.append(
          np.random.randint(2_55      , size=(image_processor_tester.num_channels, width, height)      , dtype=np.uinta  )  )

   if not numpify and not torchify:
      # PIL expects the channel dimension as last dimension
      _SCREAMING_SNAKE_CASE       	=			[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE_      , 0      , -1  )  ) for image in image_inputs]

   if torchify:
      _SCREAMING_SNAKE_CASE       	=			[torch.from_numpy(SCREAMING_SNAKE_CASE_  ) for image in image_inputs]

   return image_inputs
def 				lowerCAmelCase_		(			SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_=10      , SCREAMING_SNAKE_CASE_=10      , SCREAMING_SNAKE_CASE_=False      , SCREAMING_SNAKE_CASE_=False  )       ->       Optional[int]:




   """simple docstring"""
   _SCREAMING_SNAKE_CASE       	=			[]
   for i in range(image_processor_tester.num_frames  ):
      video.append(np.random.randint(2_55      , size=(image_processor_tester.num_channels, width, height)      , dtype=np.uinta  )  )

   if not numpify and not torchify:
      # PIL expects the channel dimension as last dimension
      _SCREAMING_SNAKE_CASE       	=			[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE_      , 0      , -1  )  ) for frame in video]

   if torchify:
      _SCREAMING_SNAKE_CASE       	=			[torch.from_numpy(SCREAMING_SNAKE_CASE_  ) for frame in video]

   return video
def 				lowerCAmelCase_		(			SCREAMING_SNAKE_CASE_      , SCREAMING_SNAKE_CASE_=False      , SCREAMING_SNAKE_CASE_=False      , SCREAMING_SNAKE_CASE_=False  )       ->       Optional[int]:




   """simple docstring"""
   assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

   _SCREAMING_SNAKE_CASE       	=			[]
   for i in range(image_processor_tester.batch_size  ):
      if equal_resolution:
         _SCREAMING_SNAKE_CASE       	=			_SCREAMING_SNAKE_CASE       	=			image_processor_tester.max_resolution
      else:
         _SCREAMING_SNAKE_CASE	,							_SCREAMING_SNAKE_CASE       	=			np.random.choice(
             np.arange(image_processor_tester.min_resolution      , image_processor_tester.max_resolution  )      , 2  )
         _SCREAMING_SNAKE_CASE       	=			prepare_video(
             image_processor_tester=SCREAMING_SNAKE_CASE_      , width=SCREAMING_SNAKE_CASE_      , height=SCREAMING_SNAKE_CASE_      , numpify=SCREAMING_SNAKE_CASE_      , torchify=SCREAMING_SNAKE_CASE_      , )
      video_inputs.append(SCREAMING_SNAKE_CASE_  )

   return video_inputs







class 				_a			:





       """simple docstring"""




       SCREAMING_SNAKE_CASE =					None






       def     UpperCamelCase			(							self )      ->     Any:
          _SCREAMING_SNAKE_CASE       	=			self.image_processing_class(**self.image_processor_dict )
          _SCREAMING_SNAKE_CASE       	=			json.loads(image_processor.to_json_string() )
          for key, value in self.image_processor_dict.items():
             self.assertEqual(obj[key]    ,							A__ )






       def     UpperCamelCase			(							self )      ->     List[str]:
          _SCREAMING_SNAKE_CASE       	=			self.image_processing_class(**self.image_processor_dict )

          with tempfile.TemporaryDirectory() as tmpdirname:
             _SCREAMING_SNAKE_CASE       	=			os.path.join(A__    ,							"""image_processor.json""" )
             image_processor_first.to_json_file(A__ )
             _SCREAMING_SNAKE_CASE       	=			self.image_processing_class.from_json_file(A__ )

          self.assertEqual(image_processor_second.to_dict()    ,							image_processor_first.to_dict() )






       def     UpperCamelCase			(							self )      ->     Optional[int]:
          _SCREAMING_SNAKE_CASE       	=			self.image_processing_class(**self.image_processor_dict )

          with tempfile.TemporaryDirectory() as tmpdirname:
             _SCREAMING_SNAKE_CASE       	=			image_processor_first.save_pretrained(A__ )[0]
             check_json_file_has_correct_format(A__ )
             _SCREAMING_SNAKE_CASE       	=			self.image_processing_class.from_pretrained(A__ )

          self.assertEqual(image_processor_second.to_dict()    ,							image_processor_first.to_dict() )






       def     UpperCamelCase			(							self )      ->     Optional[Any]:
          _SCREAMING_SNAKE_CASE       	=			self.image_processing_class()
          self.assertIsNotNone(A__ )






       @require_torch
       @require_vision
       def     UpperCamelCase			(							self )      ->     Optional[Any]:
          if self.test_cast_dtype is not None:
             # Initialize image_processor
             _SCREAMING_SNAKE_CASE       	=			self.image_processing_class(**self.image_processor_dict )

             # create random PyTorch tensors
             _SCREAMING_SNAKE_CASE       	=			prepare_image_inputs(self.image_processor_tester    ,							equal_resolution=A__    ,							torchify=A__ )

             _SCREAMING_SNAKE_CASE       	=			image_processor(A__    ,							return_tensors="""pt""" )
             # for layoutLM compatiblity
             self.assertEqual(encoding.pixel_values.device    ,							torch.device("""cpu""" ) )
             self.assertEqual(encoding.pixel_values.dtype    ,							torch.floataa )

             _SCREAMING_SNAKE_CASE       	=			image_processor(A__    ,							return_tensors="""pt""" ).to(torch.floataa )
             self.assertEqual(encoding.pixel_values.device    ,							torch.device("""cpu""" ) )
             self.assertEqual(encoding.pixel_values.dtype    ,							torch.floataa )

             _SCREAMING_SNAKE_CASE       	=			image_processor(A__    ,							return_tensors="""pt""" ).to("""cpu"""    ,							torch.bfloataa )
             self.assertEqual(encoding.pixel_values.device    ,							torch.device("""cpu""" ) )
             self.assertEqual(encoding.pixel_values.dtype    ,							torch.bfloataa )

             with self.assertRaises(A__ ):
                _SCREAMING_SNAKE_CASE       	=			image_processor(A__    ,							return_tensors="""pt""" ).to(torch.bfloataa    ,							"""cpu""" )

             # Try with text + image feature
             _SCREAMING_SNAKE_CASE       	=			image_processor(A__    ,							return_tensors="""pt""" )
             encoding.update({"""input_ids""": torch.LongTensor([[1, 2, 3], [4, 5, 6]] )} )
             _SCREAMING_SNAKE_CASE       	=			encoding.to(torch.floataa )

             self.assertEqual(encoding.pixel_values.device    ,							torch.device("""cpu""" ) )
             self.assertEqual(encoding.pixel_values.dtype    ,							torch.floataa )
             self.assertEqual(encoding.input_ids.dtype    ,							torch.long )








'''simple docstring'''



from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__ :						int    							=					logging.get_logger(__name__)

UpperCamelCase__ :						Any    							=					{
    "openbmb/cpm-ant-10b": "https://huggingface.co/openbmb/cpm-ant-10b/blob/main/config.json"
    # See all CPMAnt models at https://huggingface.co/models?filter=cpmant
}

class 				_a			(_lowerCamelCase):





       """simple docstring"""




       SCREAMING_SNAKE_CASE =					'cpmant'






       def __init__(							self    ,							A__ = 3_07_20    ,							A__ = 40_96    ,							A__ = 32    ,							A__ = 1_28    ,							A__ = 1_02_40    ,							A__ = 48    ,							A__ = 0.0    ,							A__ = 5_12    ,							A__ = 20_48    ,							A__ = 1E-6    ,							A__ = 1.0    ,							A__ = 32    ,							A__ = 32    ,							A__ = 32    ,							A__ = True    ,							**A__    ,							)      ->     List[Any]:
          super().__init__(**A__ )
          _SCREAMING_SNAKE_CASE       	=			prompt_types
          _SCREAMING_SNAKE_CASE       	=			prompt_length
          _SCREAMING_SNAKE_CASE       	=			segment_types
          _SCREAMING_SNAKE_CASE       	=			hidden_size
          _SCREAMING_SNAKE_CASE       	=			num_attention_heads
          _SCREAMING_SNAKE_CASE       	=			dim_head
          _SCREAMING_SNAKE_CASE       	=			dim_ff
          _SCREAMING_SNAKE_CASE       	=			num_hidden_layers
          _SCREAMING_SNAKE_CASE       	=			position_bias_num_buckets
          _SCREAMING_SNAKE_CASE       	=			position_bias_max_distance
          _SCREAMING_SNAKE_CASE       	=			dropout_p
          _SCREAMING_SNAKE_CASE       	=			eps
          _SCREAMING_SNAKE_CASE       	=			use_cache
          _SCREAMING_SNAKE_CASE       	=			vocab_size
          _SCREAMING_SNAKE_CASE       	=			init_std






