






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__				(   __lowerCamelCase      :					int  ,		__lowerCamelCase      :					Optional[int]=False  ,		__lowerCamelCase      :					List[str]=False  ,		__lowerCamelCase      :					str=False		):
  assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

  __UpperCAmelCase							:     Tuple									=  []
  for i in range(image_processor_tester.batch_size		):
    if equal_resolution:
      __UpperCAmelCase							:     Dict									=  image_processor_tester.max_resolution
    else:
      # To avoid getting image width/height 0
      __UpperCAmelCase							:     Union[str, Any]									=  image_processor_tester.min_resolution
      if getattr(__lowerCamelCase  ,		"""size_divisor"""  ,		__lowerCamelCase		):
        # If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
        __UpperCAmelCase							:     str									=  max(image_processor_tester.size_divisor  ,		__lowerCamelCase		)
      __UpperCAmelCase				,			__UpperCAmelCase							:     List[Any]									=  np.random.choice(np.arange(__lowerCamelCase  ,		image_processor_tester.max_resolution		)  ,		2		)
    image_inputs.append(
        np.random.randint(255  ,		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
    __UpperCAmelCase							:     List[str]									=  [Image.fromarray(np.moveaxis(__lowerCamelCase  ,		0  ,		-1		)		) for image in image_inputs]

  if torchify:
    __UpperCAmelCase							:     int									=  [torch.from_numpy(__lowerCamelCase		) for image in image_inputs]

  return image_inputs






def     lowerCamelCase__				(   __lowerCamelCase      :					int  ,		__lowerCamelCase      :					str=10  ,		__lowerCamelCase      :					Any=10  ,		__lowerCamelCase      :					List[Any]=False  ,		__lowerCamelCase      :					Optional[int]=False		):
  __UpperCAmelCase							:     List[str]									=  []
  for i in range(image_processor_tester.num_frames		):
    video.append(np.random.randint(255  ,		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
    __UpperCAmelCase							:     List[str]									=  [Image.fromarray(np.moveaxis(__lowerCamelCase  ,		0  ,		-1		)		) for frame in video]

  if torchify:
    __UpperCAmelCase							:     int									=  [torch.from_numpy(__lowerCamelCase		) for frame in video]

  return video






def     lowerCamelCase__				(   __lowerCamelCase      :					Optional[Any]  ,		__lowerCamelCase      :					Any=False  ,		__lowerCamelCase      :					str=False  ,		__lowerCamelCase      :					Optional[Any]=False		):
  assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

  __UpperCAmelCase							:     Any									=  []
  for i in range(image_processor_tester.batch_size		):
    if equal_resolution:
      __UpperCAmelCase							:     Optional[int]									=  image_processor_tester.max_resolution
    else:
      __UpperCAmelCase				,			__UpperCAmelCase							:     str									=  np.random.choice(
          np.arange(image_processor_tester.min_resolution  ,		image_processor_tester.max_resolution		)  ,		2		)
      __UpperCAmelCase							:     Any									=  prepare_video(
          image_processor_tester=__lowerCamelCase  ,		width=__lowerCamelCase  ,		height=__lowerCamelCase  ,		numpify=__lowerCamelCase  ,		torchify=__lowerCamelCase  ,		)
    video_inputs.append(__lowerCamelCase		)

  return video_inputs







class    a   :



      """simple docstring"""


      a  :			Any								=     None
      def      UpperCAmelCase				(				self :      Union[str, Any] )							->				Union[str, Any]:
        __UpperCAmelCase							:     Dict									=  self.image_processing_class(**self.image_processor_dict )
        __UpperCAmelCase							:     List[str]									=  json.loads(image_processor.to_json_string() )
        for key, value in self.image_processor_dict.items():
          self.assertEqual(obj[key]		,				__lowercase )
      def      UpperCAmelCase				(				self :      List[Any] )							->				Optional[int]:
        __UpperCAmelCase							:     Dict									=  self.image_processing_class(**self.image_processor_dict )

        with tempfile.TemporaryDirectory() as tmpdirname:
          __UpperCAmelCase							:     List[Any]									=  os.path.join(__lowercase		,				"""image_processor.json""" )
          image_processor_first.to_json_file(__lowercase )
          __UpperCAmelCase							:     Dict									=  self.image_processing_class.from_json_file(__lowercase )

        self.assertEqual(image_processor_second.to_dict()		,				image_processor_first.to_dict() )
      def      UpperCAmelCase				(				self :      Union[str, Any] )							->				Dict:
        __UpperCAmelCase							:     int									=  self.image_processing_class(**self.image_processor_dict )

        with tempfile.TemporaryDirectory() as tmpdirname:
          __UpperCAmelCase							:     Optional[Any]									=  image_processor_first.save_pretrained(__lowercase )[0]
          check_json_file_has_correct_format(__lowercase )
          __UpperCAmelCase							:     Union[str, Any]									=  self.image_processing_class.from_pretrained(__lowercase )

        self.assertEqual(image_processor_second.to_dict()		,				image_processor_first.to_dict() )
      def      UpperCAmelCase				(				self :      Dict )							->				List[Any]:
        __UpperCAmelCase							:     Optional[Any]									=  self.image_processing_class()
        self.assertIsNotNone(__lowercase )




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

          # create random PyTorch tensors
          __UpperCAmelCase							:     Tuple									=  prepare_image_inputs(self.image_processor_tester		,				equal_resolution=__lowercase		,				torchify=__lowercase )

          __UpperCAmelCase							:     Dict									=  image_processor(__lowercase		,				return_tensors="""pt""" )
          # for layoutLM compatiblity
          self.assertEqual(encoding.pixel_values.device		,				torch.device("""cpu""" ) )
          self.assertEqual(encoding.pixel_values.dtype		,				torch.floataa )

          __UpperCAmelCase							:     int									=  image_processor(__lowercase		,				return_tensors="""pt""" ).to(torch.floataa )
          self.assertEqual(encoding.pixel_values.device		,				torch.device("""cpu""" ) )
          self.assertEqual(encoding.pixel_values.dtype		,				torch.floataa )

          __UpperCAmelCase							:     Optional[Any]									=  image_processor(__lowercase		,				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(__lowercase ):
            __UpperCAmelCase							:     Tuple									=  image_processor(__lowercase		,				return_tensors="""pt""" ).to(torch.bfloataa		,				"""cpu""" )

          # Try with text + image feature
          __UpperCAmelCase							:     Optional[int]									=  image_processor(__lowercase		,				return_tensors="""pt""" )
          encoding.update({"""input_ids""": torch.LongTensor([[1, 2, 3], [4, 5, 6]] )} )
          __UpperCAmelCase							:     List[str]									=  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 )










import unittest

from transformers.testing_utils import is_torch_available, require_torch, tooslow

from ...generation.test_utils import torch_device
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import (
           CpmAntConfig,
           CpmAntForCausalLM,
           CpmAntModel,
           CpmAntTokenizer,
       )
@require_torch
class    a   :



      """simple docstring"""


      def __init__(				self :      List[str]		,				__lowercase :      List[str]		,				__lowercase :      Tuple=2		,				__lowercase :      Dict=8		,				__lowercase :      int=True		,				__lowercase :      Dict=False		,				__lowercase :      List[Any]=False		,				__lowercase :      List[str]=False		,				__lowercase :      Any=False		,				__lowercase :      List[Any]=99		,				__lowercase :      int=32		,				__lowercase :      Optional[Any]=3		,				__lowercase :      Any=4		,				__lowercase :      List[Any]=37		,				__lowercase :      Dict=32		,				__lowercase :      Any=128		,				__lowercase :      Optional[Any]=8		,				__lowercase :      str=8		,				__lowercase :      str=8		,				__lowercase :      Optional[int]=1.0		,				__lowercase :      int=True		,				)							->				Optional[int]:
        __UpperCAmelCase							:     Optional[Any]									=  parent
        __UpperCAmelCase							:     Optional[int]									=  batch_size
        __UpperCAmelCase							:     List[Any]									=  seq_length
        __UpperCAmelCase							:     Optional[int]									=  is_training
        __UpperCAmelCase							:     Optional[Any]									=  use_token_type_ids
        __UpperCAmelCase							:     Optional[int]									=  use_input_mask
        __UpperCAmelCase							:     Optional[int]									=  use_labels
        __UpperCAmelCase							:     str									=  use_mc_token_ids
        __UpperCAmelCase							:     Any									=  vocab_size
        __UpperCAmelCase							:     str									=  hidden_size
        __UpperCAmelCase							:     str									=  num_hidden_layers
        __UpperCAmelCase							:     Union[str, Any]									=  num_attention_heads
        __UpperCAmelCase							:     Optional[int]									=  intermediate_size
        __UpperCAmelCase							:     Tuple									=  num_buckets
        __UpperCAmelCase							:     Any									=  max_distance
        __UpperCAmelCase							:     Any									=  prompt_length
        __UpperCAmelCase							:     int									=  prompt_types
        __UpperCAmelCase							:     List[Any]									=  segment_types
        __UpperCAmelCase							:     Any									=  init_std
        __UpperCAmelCase							:     int									=  return_dict
      def      UpperCAmelCase				(				self :      Optional[Any] )							->				List[Any]:
        __UpperCAmelCase							:     Optional[Any]									=  {}
        __UpperCAmelCase							:     Dict									=  ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size ).type(torch.intaa )
        __UpperCAmelCase							:     List[str]									=  False

        __UpperCAmelCase							:     List[str]									=  self.get_config()

        return (config, input_ids)
      def      UpperCAmelCase				(				self :      Union[str, Any] )							->				Optional[int]:
        return CpmAntConfig(
            vocab_size=self.vocab_size		,				hidden_size=self.hidden_size		,				num_hidden_layers=self.num_hidden_layers		,				num_attention_heads=self.num_attention_heads		,				dim_ff=self.intermediate_size		,				position_bias_num_buckets=self.num_buckets		,				position_bias_max_distance=self.max_distance		,				prompt_types=self.prompt_types		,				prompt_length=self.prompt_length		,				segment_types=self.segment_types		,				use_cache=__lowercase		,				init_std=self.init_std		,				return_dict=self.return_dict		,				)
      def      UpperCAmelCase				(				self :      Tuple		,				__lowercase :      Union[str, Any]		,				__lowercase :      List[str]		,				*__lowercase :      Optional[int] )							->				List[Any]:
        __UpperCAmelCase							:     int									=  CpmAntModel(config=__lowercase )
        model.to(__lowercase )
        model.eval()

        __UpperCAmelCase							:     str									=  model(**__lowercase ).last_hidden_state

        self.parent.assertEqual(hidden_states.shape		,				(self.batch_size, self.seq_length, config.hidden_size) )
      def      UpperCAmelCase				(				self :      List[str]		,				__lowercase :      List[str]		,				__lowercase :      Optional[Any]		,				*__lowercase :      Optional[int] )							->				List[str]:
        __UpperCAmelCase							:     List[Any]									=  CpmAntForCausalLM(__lowercase )
        model.to(__lowercase )
        __UpperCAmelCase							:     Optional[Any]									=  input_ids["""input_ids"""].to(__lowercase )
        model.eval()

        __UpperCAmelCase							:     int									=  model(**__lowercase )
        self.parent.assertEqual(
            model_output.logits.shape		,				(self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)		,				)




      def      UpperCAmelCase				(				self :      List[Any] )							->				Dict:
        __UpperCAmelCase				,			__UpperCAmelCase							:     Optional[int]									=  self.prepare_config_and_inputs()
        return config, inputs_dict


@require_torch
class    a   (			lowercase__							,	lowercase__							,	unittest.TestCase       ):



      """simple docstring"""


      a  :			List[Any]								=     (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
      a  :			Optional[int]								=     (
          {'feature-extraction': CpmAntModel, 'text-generation': CpmAntForCausalLM} if is_torch_available() else {}
      )

      a  :			List[Any]								=     False
      a  :			Any								=     False
      a  :			List[Any]								=     False
      a  :			Tuple								=     False
      a  :			Any								=     False
      def      UpperCAmelCase				(				self :      Optional[int] )							->				Optional[Any]:
        __UpperCAmelCase							:     List[Any]									=  CpmAntModelTester(self )
        __UpperCAmelCase							:     Dict									=  ConfigTester(self		,				config_class=__lowercase )
      def      UpperCAmelCase				(				self :      str )							->				Optional[int]:
        self.config_tester.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()
      def      UpperCAmelCase				(				self :      List[str] )							->				List[str]:
        unittest.skip("""CPMAnt doesn't support input_embeds.""" )(self.test_inputs_embeds )
      def      UpperCAmelCase				(				self :      Any )							->				Union[str, Any]:
        unittest.skip(
            """CPMAnt doesn't support retain grad in hidden_states or attentions, because prompt management will peel off the output.hidden_states from graph.\
                 So is attentions. We strongly recommand you use loss to tune model.""" )(self.test_retain_grad_hidden_states_attentions )
      def      UpperCAmelCase				(				self :      str )							->				List[Any]:
        __UpperCAmelCase				,			__UpperCAmelCase							:     Optional[Any]									=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_cpmant_model(__lowercase		,				__lowercase )




      def      UpperCAmelCase				(				self :      Optional[Any] )							->				str:
        __UpperCAmelCase				,			__UpperCAmelCase							:     Dict									=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(__lowercase		,				__lowercase )


@require_torch
class    a   (			unittest.TestCase       ):



      """simple docstring"""


      @tooslow
      def      UpperCAmelCase				(				self :      str )							->				Optional[Any]:
        __UpperCAmelCase							:     List[Any]									=  """今天天气真好！"""
        __UpperCAmelCase							:     int									=  """openbmb/cpm-ant-10b"""
        __UpperCAmelCase							:     Tuple									=  CpmAntModel.from_pretrained(__lowercase )
        __UpperCAmelCase							:     Optional[int]									=  CpmAntTokenizer.from_pretrained(__lowercase )
        __UpperCAmelCase							:     Union[str, Any]									=  tokenizer(__lowercase		,				return_tensors="""pt""" )
        __UpperCAmelCase							:     List[Any]									=  model(**__lowercase ).last_hidden_state

        __UpperCAmelCase							:     str									=  torch.tensor(
            [[[6.1_708, 5.9_244, 1.0_835], [6.5_207, 6.2_893, -11.3_324], [-1.0_107, -0.0_576, -5.9_577]]]		,				)
        self.assertTrue(torch.allclose(hidden_states[:, :3, :3]		,				__lowercase		,				atol=1e-2 ) )


@require_torch
class    a   (			unittest.TestCase       ):



      """simple docstring"""


      @tooslow
      def      UpperCAmelCase				(				self :      List[Any] )							->				str:
        __UpperCAmelCase							:     List[Any]									=  """今天天气真好！"""
        __UpperCAmelCase							:     Optional[Any]									=  """openbmb/cpm-ant-10b"""
        __UpperCAmelCase							:     Tuple									=  CpmAntForCausalLM.from_pretrained(__lowercase )
        __UpperCAmelCase							:     Tuple									=  CpmAntTokenizer.from_pretrained(__lowercase )
        __UpperCAmelCase							:     List[Any]									=  tokenizer(__lowercase		,				return_tensors="""pt""" )
        __UpperCAmelCase							:     Tuple									=  model(**__lowercase ).logits

        __UpperCAmelCase							:     int									=  torch.tensor(
            [[[-6.4_267, -6.4_083, -6.3_958], [-5.8_802, -5.9_447, -5.7_811], [-5.3_896, -5.4_820, -5.4_295]]]		,				)
        self.assertTrue(torch.allclose(hidden_states[:, :3, :3]		,				__lowercase		,				atol=1e-2 ) )
      @tooslow
      def      UpperCAmelCase				(				self :      Dict )							->				Dict:
        __UpperCAmelCase							:     List[str]									=  """openbmb/cpm-ant-10b"""
        __UpperCAmelCase							:     Dict									=  CpmAntForCausalLM.from_pretrained(__lowercase )
        __UpperCAmelCase							:     int									=  CpmAntTokenizer.from_pretrained(__lowercase )
        __UpperCAmelCase							:     List[Any]									=  """今天天气不错，"""
        __UpperCAmelCase							:     Optional[int]									=  """今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的"""
        __UpperCAmelCase							:     Optional[int]									=  tokenizer(__lowercase		,				return_tensors="""pt""" )
        __UpperCAmelCase							:     Tuple									=  model.generate(**__lowercase )
        __UpperCAmelCase							:     List[Any]									=  tokenizer.batch_decode(__lowercase )
        self.assertEqual(__lowercase		,				__lowercase )




      @tooslow
      def      UpperCAmelCase				(				self :      Optional[Any] )							->				Any:
        __UpperCAmelCase							:     int									=  """openbmb/cpm-ant-10b"""
        __UpperCAmelCase							:     Any									=  CpmAntForCausalLM.from_pretrained(__lowercase )
        __UpperCAmelCase							:     Optional[Any]									=  CpmAntTokenizer.from_pretrained(__lowercase )
        __UpperCAmelCase							:     Optional[Any]									=  ["""今天天气不错，""", """新年快乐，万事如意！"""]
        __UpperCAmelCase							:     str									=  [
            """今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的""",
            """新年快乐，万事如意！在这辞旧迎新的美好时刻，我谨代表《农村新技术》杂志社全体同仁，向一直以来关心、支持《农村新技术》杂志发展的各级领导、各界朋友和广大读者致以最诚挚的""",
        ]
        __UpperCAmelCase							:     Union[str, Any]									=  tokenizer(__lowercase		,				return_tensors="""pt"""		,				padding=__lowercase )
        __UpperCAmelCase							:     int									=  model.generate(**__lowercase )
        __UpperCAmelCase							:     Optional[int]									=  tokenizer.batch_decode(__lowercase )
        self.assertEqual(__lowercase		,				__lowercase )


