'''simple docstring'''

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    lowerCamelCase__  :



   def __init__(    self					,      lowerCAmelCase__					,      lowerCAmelCase__=2					,      lowerCAmelCase__=8					,      lowerCAmelCase__=True					,      lowerCAmelCase__=False					,      lowerCAmelCase__=False					,      lowerCAmelCase__=False					,      lowerCAmelCase__=False					,      lowerCAmelCase__=99					,      lowerCAmelCase__=32					,      lowerCAmelCase__=3					,      lowerCAmelCase__=4					,      lowerCAmelCase__=37					,      lowerCAmelCase__=32					,      lowerCAmelCase__=128					,      lowerCAmelCase__=8					,      lowerCAmelCase__=8					,      lowerCAmelCase__=8					,      lowerCAmelCase__=1.0					,      lowerCAmelCase__=True					,      )				->     int:
        """simple docstring"""





        _UpperCamelCase  :Dict       =parent
        _UpperCamelCase  :Optional[int]       =batch_size
        _UpperCamelCase  :int       =seq_length
        _UpperCamelCase  :Union[str, Any]       =is_training
        _UpperCamelCase  :Dict       =use_token_type_ids
        _UpperCamelCase  :Any       =use_input_mask
        _UpperCamelCase  :int       =use_labels
        _UpperCamelCase  :Optional[Any]       =use_mc_token_ids
        _UpperCamelCase  :Any       =vocab_size
        _UpperCamelCase  :Dict       =hidden_size
        _UpperCamelCase  :List[str]       =num_hidden_layers
        _UpperCamelCase  :Tuple       =num_attention_heads
        _UpperCamelCase  :Dict       =intermediate_size
        _UpperCamelCase  :str       =num_buckets
        _UpperCamelCase  :Optional[Any]       =max_distance
        _UpperCamelCase  :str       =prompt_length
        _UpperCamelCase  :Optional[int]       =prompt_types
        _UpperCamelCase  :int       =segment_types
        _UpperCamelCase  :List[Any]       =init_std
        _UpperCamelCase  :Any       =return_dict



   def  _UpperCamelCase		(    self     )				->     Tuple:
        """simple docstring"""





        _UpperCamelCase  :Optional[Any]       ={}
        _UpperCamelCase  :List[Any]       =ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size     ).type(torch.intaa     )
        _UpperCamelCase  :Tuple       =False

        _UpperCamelCase  :Dict       =self.get_config()

        return (config, input_ids)



   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """simple docstring"""





        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=lowerCAmelCase__					,      init_std=self.init_std					,      return_dict=self.return_dict					,      )



   def  _UpperCamelCase		(    self					,      lowerCAmelCase__					,      lowerCAmelCase__					,      *lowerCAmelCase__     )				->     Tuple:
        """simple docstring"""





        _UpperCamelCase  :Dict       =CpmAntModel(config=lowerCAmelCase__     )
        model.to(lowerCAmelCase__     )
        model.eval()

        _UpperCamelCase  :List[str]       =model(**lowerCAmelCase__     ).last_hidden_state

        self.parent.assertEqual(hidden_states.shape					,      (self.batch_size, self.seq_length, config.hidden_size)     )



   def  _UpperCamelCase		(    self					,      lowerCAmelCase__					,      lowerCAmelCase__					,      *lowerCAmelCase__     )				->     Optional[int]:
        """simple docstring"""





        _UpperCamelCase  :Optional[Any]       =CpmAntForCausalLM(lowerCAmelCase__     )
        model.to(lowerCAmelCase__     )
        _UpperCamelCase  :Any       =input_ids["""input_ids"""].to(lowerCAmelCase__     )
        model.eval()

        _UpperCamelCase  :Optional[int]       =model(**lowerCAmelCase__     )
        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     )				->     Optional[int]:
        """simple docstring"""





        _UpperCamelCase       ,      _UpperCamelCase  :Dict       =self.prepare_config_and_inputs()
        return config, inputs_dict




@require_torch
class    lowerCamelCase__  (   __snake_case       ,				__snake_case       ,				unittest.TestCase     ):
   __UpperCAmelCase           =						(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
   __UpperCAmelCase           =						(
       {"""feature-extraction""": CpmAntModel, """text-generation""": CpmAntForCausalLM} if is_torch_available() else {}
   )

   __UpperCAmelCase           =						False
   __UpperCAmelCase           =						False
   __UpperCAmelCase           =						False
   __UpperCAmelCase           =						False
   __UpperCAmelCase           =						False



   def  _UpperCamelCase		(    self     )				->     Optional[Any]:
        """simple docstring"""





        _UpperCamelCase  :Optional[int]       =CpmAntModelTester(self     )
        _UpperCamelCase  :Dict       =ConfigTester(self					,      config_class=lowerCAmelCase__     )



   def  _UpperCamelCase		(    self     )				->     Dict:
        """simple docstring"""





        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     )				->     str:
        """simple docstring"""





        unittest.skip("""CPMAnt doesn't support input_embeds."""     )(self.test_inputs_embeds     )



   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """simple docstring"""





        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     )				->     List[str]:
        """simple docstring"""





        _UpperCamelCase       ,      _UpperCamelCase  :Optional[int]       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_cpmant_model(lowerCAmelCase__					,      lowerCAmelCase__     )




   def  _UpperCamelCase		(    self     )				->     Any:
        """simple docstring"""





        _UpperCamelCase       ,      _UpperCamelCase  :List[str]       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(lowerCAmelCase__					,      lowerCAmelCase__     )




@require_torch
class    lowerCamelCase__  (   unittest.TestCase     ):



   @tooslow
   def  _UpperCamelCase		(    self     )				->     Optional[int]:
        """simple docstring"""





        _UpperCamelCase  :Dict       ="""今天天气真好！"""
        _UpperCamelCase  :Tuple       ="""openbmb/cpm-ant-10b"""
        _UpperCamelCase  :List[str]       =CpmAntModel.from_pretrained(lowerCAmelCase__     )
        _UpperCamelCase  :List[str]       =CpmAntTokenizer.from_pretrained(lowerCAmelCase__     )
        _UpperCamelCase  :Optional[Any]       =tokenizer(lowerCAmelCase__					,      return_tensors="""pt"""     )
        _UpperCamelCase  :Optional[Any]       =model(**lowerCAmelCase__     ).last_hidden_state

        _UpperCamelCase  :Optional[int]       =torch.tensor(
            [[[6.1708, 5.9244, 1.0835], [6.5207, 6.2893, -11.3324], [-1.0107, -0.0576, -5.9577]]]					,      )
        self.assertTrue(torch.allclose(hidden_states[:, :3, :3]					,      lowerCAmelCase__					,      atol=1e-2     )     )




@require_torch
class    lowerCamelCase__  (   unittest.TestCase     ):



   @tooslow
   def  _UpperCamelCase		(    self     )				->     Optional[int]:
        """simple docstring"""





        _UpperCamelCase  :List[Any]       ="""今天天气真好！"""
        _UpperCamelCase  :int       ="""openbmb/cpm-ant-10b"""
        _UpperCamelCase  :Optional[Any]       =CpmAntForCausalLM.from_pretrained(lowerCAmelCase__     )
        _UpperCamelCase  :Optional[Any]       =CpmAntTokenizer.from_pretrained(lowerCAmelCase__     )
        _UpperCamelCase  :List[Any]       =tokenizer(lowerCAmelCase__					,      return_tensors="""pt"""     )
        _UpperCamelCase  :Any       =model(**lowerCAmelCase__     ).logits

        _UpperCamelCase  :Optional[Any]       =torch.tensor(
            [[[-6.4267, -6.4083, -6.3958], [-5.8802, -5.9447, -5.7811], [-5.3896, -5.4820, -5.4295]]]					,      )
        self.assertTrue(torch.allclose(hidden_states[:, :3, :3]					,      lowerCAmelCase__					,      atol=1e-2     )     )



   @tooslow
   def  _UpperCamelCase		(    self     )				->     Optional[Any]:
        """simple docstring"""





        _UpperCamelCase  :Tuple       ="""openbmb/cpm-ant-10b"""
        _UpperCamelCase  :str       =CpmAntForCausalLM.from_pretrained(lowerCAmelCase__     )
        _UpperCamelCase  :int       =CpmAntTokenizer.from_pretrained(lowerCAmelCase__     )
        _UpperCamelCase  :str       ="""今天天气不错，"""
        _UpperCamelCase  :str       ="""今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的"""
        _UpperCamelCase  :Dict       =tokenizer(lowerCAmelCase__					,      return_tensors="""pt"""     )
        _UpperCamelCase  :Dict       =model.generate(**lowerCAmelCase__     )
        _UpperCamelCase  :int       =tokenizer.batch_decode(lowerCAmelCase__     )
        self.assertEqual(lowerCAmelCase__					,      lowerCAmelCase__     )




   @tooslow
   def  _UpperCamelCase		(    self     )				->     Any:
        """simple docstring"""





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




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 :  str=False		,						__lowerCamelCase :  Optional[Any]=False		,						__lowerCamelCase :  int=False  ):



 """simple docstring"""





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

 __SCREAMING_SNAKE_CASE							:     List[str]							=						[]
 for i in range(image_processor_tester.batch_size  ):
  if equal_resolution:
   __SCREAMING_SNAKE_CASE							:     List[Any]							=						image_processor_tester.max_resolution
  else:
   # To avoid getting image width/height 0
   __SCREAMING_SNAKE_CASE							:     Optional[int]							=						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`
    __SCREAMING_SNAKE_CASE							:     str							=						max(image_processor_tester.size_divisor		,						__lowerCamelCase  )
   __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							:     Tuple							=						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
  __SCREAMING_SNAKE_CASE							:     Dict							=						[Image.fromarray(np.moveaxis(__lowerCamelCase		,						0		,						-1  )  ) for image in image_inputs]

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

 return image_inputs






def 						_lowerCAmelCase    (					__lowerCamelCase :  str		,						__lowerCamelCase :  List[Any]=10		,						__lowerCamelCase :  Any=10		,						__lowerCamelCase :  int=False		,						__lowerCamelCase :  List[str]=False  ):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE							:     Tuple							=						[]
 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
  __SCREAMING_SNAKE_CASE							:     Optional[int]							=						[Image.fromarray(np.moveaxis(__lowerCamelCase		,						0		,						-1  )  ) for frame in video]

 if torchify:
  __SCREAMING_SNAKE_CASE							:     List[Any]							=						[torch.from_numpy(__lowerCamelCase  ) for frame in video]

 return video






def 						_lowerCAmelCase    (					__lowerCamelCase :  Any		,						__lowerCamelCase :  Dict=False		,						__lowerCamelCase :  Optional[int]=False		,						__lowerCamelCase :  Any=False  ):



 """simple docstring"""





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

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

 return video_inputs






class    _SCREAMING_SNAKE_CASE						:
     lowerCAmelCase       =      None





     def 						__snake_case     (							self			:				Dict					)->int:
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						self.image_processing_class(**self.image_processor_dict					)
      __SCREAMING_SNAKE_CASE							:     str							=						json.loads(image_processor.to_json_string()					)
      for key, value in self.image_processor_dict.items():
       self.assertEqual(obj[key]  ,	UpperCamelCase					)





     def 						__snake_case     (							self			:				str					)->Optional[Any]:
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						self.image_processing_class(**self.image_processor_dict					)

      with tempfile.TemporaryDirectory() as tmpdirname:
       __SCREAMING_SNAKE_CASE							:     Dict							=						os.path.join(UpperCamelCase  ,	"image_processor.json"					)
       image_processor_first.to_json_file(UpperCamelCase					)
       __SCREAMING_SNAKE_CASE							:     Any							=						self.image_processing_class.from_json_file(UpperCamelCase					)

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





     def 						__snake_case     (							self			:				Optional[int]					)->Optional[Any]:
      __SCREAMING_SNAKE_CASE							:     int							=						self.image_processing_class(**self.image_processor_dict					)

      with tempfile.TemporaryDirectory() as tmpdirname:
       __SCREAMING_SNAKE_CASE							:     Any							=						image_processor_first.save_pretrained(UpperCamelCase					)[0]
       check_json_file_has_correct_format(UpperCamelCase					)
       __SCREAMING_SNAKE_CASE							:     List[Any]							=						self.image_processing_class.from_pretrained(UpperCamelCase					)

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





     def 						__snake_case     (							self			:				int					)->Any:
      __SCREAMING_SNAKE_CASE							:     Any							=						self.image_processing_class()
      self.assertIsNotNone(UpperCamelCase					)






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

       # create random PyTorch tensors
       __SCREAMING_SNAKE_CASE							:     int							=						prepare_image_inputs(self.image_processor_tester  ,	equal_resolution=UpperCamelCase  ,	torchify=UpperCamelCase					)

       __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						image_processor(UpperCamelCase  ,	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							:     int							=						image_processor(UpperCamelCase  ,	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							:     Dict							=						image_processor(UpperCamelCase  ,	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(UpperCamelCase					):
        __SCREAMING_SNAKE_CASE							:     List[Any]							=						image_processor(UpperCamelCase  ,	return_tensors="pt"					).to(torch.bfloataa  ,	"cpu"					)

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






