
"""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   _UpperCAmelCase					:
    def __init__(     self    ,     snake_case_    ,     snake_case_=2    ,     snake_case_=8    ,     snake_case_=True    ,     snake_case_=False    ,     snake_case_=False    ,     snake_case_=False    ,     snake_case_=False    ,     snake_case_=99    ,     snake_case_=32    ,     snake_case_=3    ,     snake_case_=4    ,     snake_case_=37    ,     snake_case_=32    ,     snake_case_=1_28    ,     snake_case_=8    ,     snake_case_=8    ,     snake_case_=8    ,     snake_case_=1.0    ,     snake_case_=True    ,     ):
      _snake_case  :    Optional[Any]            = parent
      _snake_case  :    str            = batch_size
      _snake_case  :    Union[str, Any]            = seq_length
      _snake_case  :    Optional[int]            = is_training
      _snake_case  :    Optional[Any]            = use_token_type_ids
      _snake_case  :    List[str]            = use_input_mask
      _snake_case  :    Any            = use_labels
      _snake_case  :    Tuple            = use_mc_token_ids
      _snake_case  :    Dict            = vocab_size
      _snake_case  :    List[str]            = hidden_size
      _snake_case  :    Any            = num_hidden_layers
      _snake_case  :    int            = num_attention_heads
      _snake_case  :    List[str]            = intermediate_size
      _snake_case  :    Union[str, Any]            = num_buckets
      _snake_case  :    List[str]            = max_distance
      _snake_case  :    Any            = prompt_length
      _snake_case  :    Dict            = prompt_types
      _snake_case  :    str            = segment_types
      _snake_case  :    List[str]            = init_std
      _snake_case  :    Tuple            = return_dict
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    int            = {}
      _snake_case  :    List[Any]            = ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size  ).type(torch.intaa  )
      _snake_case  :    Union[str, Any]            = False

      _snake_case  :    Union[str, Any]            = self.get_config()

      return (config, input_ids)
    def        lowerCamelCase__						(     self  ):
      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=snake_case_    ,     init_std=self.init_std    ,     return_dict=self.return_dict    ,     )
    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_    ,     *snake_case_  ):
      _snake_case  :    Union[str, Any]            = CpmAntModel(config=snake_case_  )
      model.to(snake_case_  )
      model.eval()

      _snake_case  :    List[Any]            = model(**snake_case_  ).last_hidden_state

      self.parent.assertEqual(hidden_states.shape    ,     (self.batch_size, self.seq_length, config.hidden_size)  )
    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_    ,     *snake_case_  ):
      _snake_case  :    List[Any]            = CpmAntForCausalLM(snake_case_  )
      model.to(snake_case_  )
      _snake_case  :    Union[str, Any]            = input_ids["input_ids"].to(snake_case_  )
      model.eval()

      _snake_case  :    List[str]            = model(**snake_case_  )
      self.parent.assertEqual(
          model_output.logits.shape    ,     (self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)    ,     )


    def        lowerCamelCase__						(     self  ):
      _snake_case      ,       _snake_case  :    Any            = self.prepare_config_and_inputs()
      return config, inputs_dict






@require_torch
class   _UpperCAmelCase					(       _snake_case		,       _snake_case		,       unittest.TestCase):
    __lowercase				:			List[str]	=       (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
    __lowercase				:			Optional[int]	=       (
        {"""feature-extraction""": CpmAntModel, """text-generation""": CpmAntForCausalLM} if is_torch_available() else {}
    )

    __lowercase				:			Union[str, Any]	=       False
    __lowercase				:			int	=       False
    __lowercase				:			List[str]	=       False
    __lowercase				:			List[Any]	=       False
    __lowercase				:			Optional[Any]	=       False
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    str            = CpmAntModelTester(self  )
      _snake_case  :    List[str]            = ConfigTester(self    ,     config_class=snake_case_  )
    def        lowerCamelCase__						(     self  ):
      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        lowerCamelCase__						(     self  ):
      unittest.skip("CPMAnt doesn't support input_embeds."  )(self.test_inputs_embeds  )
    def        lowerCamelCase__						(     self  ):
      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        lowerCamelCase__						(     self  ):
      _snake_case      ,       _snake_case  :    Optional[Any]            = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_cpmant_model(snake_case_    ,     snake_case_  )


    def        lowerCamelCase__						(     self  ):
      _snake_case      ,       _snake_case  :    Optional[int]            = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(snake_case_    ,     snake_case_  )






@require_torch
class   _UpperCAmelCase					(       unittest.TestCase):
    @tooslow
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Union[str, Any]            = "今天天气真好！"
      _snake_case  :    str            = "openbmb/cpm-ant-10b"
      _snake_case  :    str            = CpmAntModel.from_pretrained(snake_case_  )
      _snake_case  :    List[Any]            = CpmAntTokenizer.from_pretrained(snake_case_  )
      _snake_case  :    Dict            = tokenizer(snake_case_    ,     return_tensors="pt"  )
      _snake_case  :    List[str]            = model(**snake_case_  ).last_hidden_state

      _snake_case  :    Tuple            = 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]    ,     snake_case_    ,     atol=1E-2  )  )






@require_torch
class   _UpperCAmelCase					(       unittest.TestCase):
    @tooslow
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Any            = "今天天气真好！"
      _snake_case  :    Union[str, Any]            = "openbmb/cpm-ant-10b"
      _snake_case  :    int            = CpmAntForCausalLM.from_pretrained(snake_case_  )
      _snake_case  :    List[str]            = CpmAntTokenizer.from_pretrained(snake_case_  )
      _snake_case  :    Union[str, Any]            = tokenizer(snake_case_    ,     return_tensors="pt"  )
      _snake_case  :    List[str]            = model(**snake_case_  ).logits

      _snake_case  :    List[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]    ,     snake_case_    ,     atol=1E-2  )  )
    @tooslow
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Union[str, Any]            = "openbmb/cpm-ant-10b"
      _snake_case  :    str            = CpmAntForCausalLM.from_pretrained(snake_case_  )
      _snake_case  :    List[str]            = CpmAntTokenizer.from_pretrained(snake_case_  )
      _snake_case  :    Optional[Any]            = "今天天气不错，"
      _snake_case  :    Any            = "今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的"
      _snake_case  :    List[str]            = tokenizer(snake_case_    ,     return_tensors="pt"  )
      _snake_case  :    List[str]            = model.generate(**snake_case_  )
      _snake_case  :    str            = tokenizer.batch_decode(snake_case_  )
      self.assertEqual(snake_case_    ,     snake_case_  )


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


'''simple docstring'''

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 ConvNextImageProcessor





class 				lowercase    (    unittest.TestCase			):


   def __init__( self					:						str ,		__lowerCAmelCase					:						Union[str, Any] ,		__lowerCAmelCase					:						Optional[int]=7 ,		__lowerCAmelCase					:						Optional[Any]=3 ,		__lowerCAmelCase					:						Optional[Any]=18 ,		__lowerCAmelCase					:						List[str]=30 ,		__lowerCAmelCase					:						List[str]=400 ,		__lowerCAmelCase					:						Tuple=True ,		__lowerCAmelCase					:						Optional[int]=None ,		__lowerCAmelCase					:						List[str]=0.875 ,		__lowerCAmelCase					:						int=True ,		__lowerCAmelCase					:						Optional[int]=[0.5, 0.5, 0.5] ,		__lowerCAmelCase					:						List[str]=[0.5, 0.5, 0.5] ,		)   ->       Optional[Any]:
         lowercase_     			=   size if size is not None else {"shortest_edge": 20}
         lowercase_     			=   parent
         lowercase_     			=   batch_size
         lowercase_     			=   num_channels
         lowercase_     			=   image_size
         lowercase_     			=   min_resolution
         lowercase_     			=   max_resolution
         lowercase_     			=   do_resize
         lowercase_     			=   size
         lowercase_     			=   crop_pct
         lowercase_     			=   do_normalize
         lowercase_     			=   image_mean
         lowercase_     			=   image_std


   def __UpperCAmelCase							( self					:						List[str])   ->       int:
         return {
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_normalize": self.do_normalize,
             "do_resize": self.do_resize,
             "size": self.size,
             "crop_pct": self.crop_pct,
         }





@require_torch
@require_vision
class 				lowercase    (    __lowerCamelCase			,					unittest.TestCase			):
   lowerCamelCase_            =ConvNextImageProcessor if is_vision_available() else None


   def __UpperCAmelCase							( self					:						Optional[Any])   ->       Tuple:
         lowercase_     			=   ConvNextImageProcessingTester(self)


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


   def __UpperCAmelCase							( self					:						Optional[int])   ->       Any:
         lowercase_     			=   self.image_processing_class(**self.image_processor_dict)
         self.assertTrue(hasattr(__lowerCAmelCase ,		"do_resize"))
         self.assertTrue(hasattr(__lowerCAmelCase ,		"size"))
         self.assertTrue(hasattr(__lowerCAmelCase ,		"crop_pct"))
         self.assertTrue(hasattr(__lowerCAmelCase ,		"do_normalize"))
         self.assertTrue(hasattr(__lowerCAmelCase ,		"image_mean"))
         self.assertTrue(hasattr(__lowerCAmelCase ,		"image_std"))


   def __UpperCAmelCase							( self					:						List[str])   ->       Any:
         lowercase_     			=   self.image_processing_class.from_dict(self.image_processor_dict)
         self.assertEqual(image_processor.size ,		{"shortest_edge": 20})

         lowercase_     			=   self.image_processing_class.from_dict(self.image_processor_dict ,		size=42)
         self.assertEqual(image_processor.size ,		{"shortest_edge": 42})


   def __UpperCAmelCase							( self					:						Any)   ->       Optional[int]:
         pass


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

         # Test not batched input
         lowercase_     			=   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.size["shortest_edge"],
                 self.image_processor_tester.size["shortest_edge"],
             ) ,		)

         # Test batched
         lowercase_     			=   image_processing(__lowerCAmelCase ,		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.size["shortest_edge"],
                 self.image_processor_tester.size["shortest_edge"],
             ) ,		)


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

         # Test not batched input
         lowercase_     			=   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.size["shortest_edge"],
                 self.image_processor_tester.size["shortest_edge"],
             ) ,		)

         # Test batched
         lowercase_     			=   image_processing(__lowerCAmelCase ,		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.size["shortest_edge"],
                 self.image_processor_tester.size["shortest_edge"],
             ) ,		)


   def __UpperCAmelCase							( self					:						List[str])   ->       List[str]:
         # Initialize image_processing
         lowercase_     			=   self.image_processing_class(**self.image_processor_dict)
         # create random PyTorch tensors
         lowercase_     			=   prepare_image_inputs(self.image_processor_tester ,		equal_resolution=__lowerCAmelCase ,		torchify=__lowerCAmelCase)
         for image in image_inputs:
               self.assertIsInstance(__lowerCAmelCase ,		torch.Tensor)

         # Test not batched input
         lowercase_     			=   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.size["shortest_edge"],
                 self.image_processor_tester.size["shortest_edge"],
             ) ,		)

         # Test batched
         lowercase_     			=   image_processing(__lowerCAmelCase ,		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.size["shortest_edge"],
                 self.image_processor_tester.size["shortest_edge"],
             ) ,		)






