


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


       """simple docstring"""




       def __init__(       self							:      Optional[Any] ,				__lowerCamelCase							:      List[str] ,				__lowerCamelCase							:      Optional[int]=2 ,				__lowerCamelCase							:      Any=8 ,				__lowerCamelCase							:      Dict=True ,				__lowerCamelCase							:      Dict=False ,				__lowerCamelCase							:      Optional[int]=False ,				__lowerCamelCase							:      Dict=False ,				__lowerCamelCase							:      Union[str, Any]=False ,				__lowerCamelCase							:      List[str]=99 ,				__lowerCamelCase							:      Any=32 ,				__lowerCamelCase							:      Union[str, Any]=3 ,				__lowerCamelCase							:      int=4 ,				__lowerCamelCase							:      str=37 ,				__lowerCamelCase							:      Optional[Any]=32 ,				__lowerCamelCase							:      Tuple=128 ,				__lowerCamelCase							:      Optional[Any]=8 ,				__lowerCamelCase							:      List[Any]=8 ,				__lowerCamelCase							:      str=8 ,				__lowerCamelCase							:      Optional[int]=1.0 ,				__lowerCamelCase							:      Tuple=True ,				):


             '''simple docstring'''



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





       def 						lowerCAmelCase					(       self							:      Any	):


             '''simple docstring'''



             lowerCamelCase__		:    str   =  {}
             lowerCamelCase__		:    Union[str, Any]   =  ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size	).type(torch.intaa	)
             lowerCamelCase__		:    Optional[int]   =  False

             lowerCamelCase__		:    int   =  self.get_config()

             return (config, input_ids)





       def 						lowerCAmelCase					(       self							:      str	):


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





       def 						lowerCAmelCase					(       self							:      List[Any] ,				__lowerCamelCase							:      Optional[Any] ,				__lowerCamelCase							:      List[str] ,				*__lowerCamelCase							:      Tuple	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  CpmAntModel(config=UpperCamelCase__	)
             model.to(UpperCamelCase__	)
             model.eval()

             lowerCamelCase__		:    Optional[int]   =  model(**UpperCamelCase__	).last_hidden_state

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





       def 						lowerCAmelCase					(       self							:      int ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      str ,				*__lowerCamelCase							:      List[str]	):


             '''simple docstring'''



             lowerCamelCase__		:    Tuple   =  CpmAntForCausalLM(UpperCamelCase__	)
             model.to(UpperCamelCase__	)
             lowerCamelCase__		:    int   =  input_ids["input_ids"].to(UpperCamelCase__	)
             model.eval()

             lowerCamelCase__		:    Tuple   =  model(**UpperCamelCase__	)
             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							:      Union[str, Any]	):


             '''simple docstring'''



             lowerCamelCase__   ,       lowerCamelCase__		:    str   =  self.prepare_config_and_inputs()
             return config, inputs_dict







@require_torch
class       _lowercase			(							_A							,  _A							,  unittest.TestCase):


       """simple docstring"""




       A__ 						=      (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
       A__ 						=      (
           {"feature-extraction": CpmAntModel, "text-generation": CpmAntForCausalLM} if is_torch_available() else {}
       )

       A__ 						=      False
       A__ 						=      False
       A__ 						=      False
       A__ 						=      False
       A__ 						=      False





       def 						lowerCAmelCase					(       self							:      List[str]	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  CpmAntModelTester(self	)
             lowerCamelCase__		:    Union[str, Any]   =  ConfigTester(self ,				config_class=UpperCamelCase__	)





       def 						lowerCAmelCase					(       self							:      Tuple	):


             '''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 						lowerCAmelCase					(       self							:      str	):


             '''simple docstring'''



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





       def 						lowerCAmelCase					(       self							:      Optional[int]	):


             '''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.\\n                 So is attentions. We strongly recommand you use loss to tune model."	)(self.test_retain_grad_hidden_states_attentions	)





       def 						lowerCAmelCase					(       self							:      Any	):


             '''simple docstring'''



             lowerCamelCase__   ,       lowerCamelCase__		:    int   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_cpmant_model(UpperCamelCase__ ,				UpperCamelCase__	)





       def 						lowerCAmelCase					(       self							:      Optional[Any]	):


             '''simple docstring'''



             lowerCamelCase__   ,       lowerCamelCase__		:    Any   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_lm_head_model(UpperCamelCase__ ,				UpperCamelCase__	)







@require_torch
class       _lowercase			(							unittest.TestCase):


       """simple docstring"""




       @tooslow
       def 						lowerCAmelCase					(       self							:      Any	):


             '''simple docstring'''



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

             lowerCamelCase__		:    Optional[Any]   =  torch.tensor(
                 [[[6.1_7_0_8, 5.9_2_4_4, 1.0_8_3_5], [6.5_2_0_7, 6.2_8_9_3, -1_1.3_3_2_4], [-1.0_1_0_7, -0.0_5_7_6, -5.9_5_7_7]]] ,				)
             self.assertTrue(torch.allclose(hidden_states[:, :3, :3] ,				UpperCamelCase__ ,				atol=1E-2	)	)







@require_torch
class       _lowercase			(							unittest.TestCase):


       """simple docstring"""




       @tooslow
       def 						lowerCAmelCase					(       self							:      Any	):


             '''simple docstring'''



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

             lowerCamelCase__		:    Optional[Any]   =  torch.tensor(
                 [[[-6.4_2_6_7, -6.4_0_8_3, -6.3_9_5_8], [-5.8_8_0_2, -5.9_4_4_7, -5.7_8_1_1], [-5.3_8_9_6, -5.4_8_2_0, -5.4_2_9_5]]] ,				)
             self.assertTrue(torch.allclose(hidden_states[:, :3, :3] ,				UpperCamelCase__ ,				atol=1E-2	)	)





       @tooslow
       def 						lowerCAmelCase					(       self							:      int	):


             '''simple docstring'''



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





       @tooslow
       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



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






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 EfficientNetImageProcessor







class       _lowercase			(							unittest.TestCase):


       """simple docstring"""




       def __init__(       self							:      Optional[Any] ,				__lowerCamelCase							:      List[str] ,				__lowerCamelCase							:      int=13 ,				__lowerCamelCase							:      Tuple=3 ,				__lowerCamelCase							:      Optional[Any]=18 ,				__lowerCamelCase							:      List[Any]=30 ,				__lowerCamelCase							:      Union[str, Any]=400 ,				__lowerCamelCase							:      Union[str, Any]=True ,				__lowerCamelCase							:      Union[str, Any]=None ,				__lowerCamelCase							:      Optional[Any]=True ,				__lowerCamelCase							:      str=[0.5, 0.5, 0.5] ,				__lowerCamelCase							:      Dict=[0.5, 0.5, 0.5] ,				):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  size if size is not None else {"height": 18, "width": 18}
             lowerCamelCase__		:    Dict   =  parent
             lowerCamelCase__		:    str   =  batch_size
             lowerCamelCase__		:    Tuple   =  num_channels
             lowerCamelCase__		:    Tuple   =  image_size
             lowerCamelCase__		:    Any   =  min_resolution
             lowerCamelCase__		:    Dict   =  max_resolution
             lowerCamelCase__		:    Tuple   =  do_resize
             lowerCamelCase__		:    List[str]   =  size
             lowerCamelCase__		:    Optional[Any]   =  do_normalize
             lowerCamelCase__		:    Union[str, Any]   =  image_mean
             lowerCamelCase__		:    Optional[Any]   =  image_std





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             return {
                 "image_mean": self.image_mean,
                 "image_std": self.image_std,
                 "do_normalize": self.do_normalize,
                 "do_resize": self.do_resize,
                 "size": self.size,
             }







@require_torch
@require_vision
class       _lowercase			(							lowercase__							,  unittest.TestCase):


       """simple docstring"""




       A__ 						=      EfficientNetImageProcessor if is_vision_available() else None





       def 						lowerCAmelCase					(       self							:      Optional[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Optional[Any]   =  EfficientNetImageProcessorTester(self	)





       @property
       def 						lowerCAmelCase					(       self							:      Union[str, Any]	):


             '''simple docstring'''



             return self.image_processor_tester.prepare_image_processor_dict()





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             lowerCamelCase__		:    Union[str, Any]   =  self.image_processing_class(**self.image_processor_dict	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"image_mean"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"image_std"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"do_normalize"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"do_resize"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"size"	)	)





       def 						lowerCAmelCase					(       self							:      Any	):


             '''simple docstring'''



             lowerCamelCase__		:    Optional[Any]   =  self.image_processing_class.from_dict(self.image_processor_dict	)
             self.assertEqual(image_processor.size ,				{"height": 18, "width": 18}	)

             lowerCamelCase__		:    Optional[int]   =  self.image_processing_class.from_dict(self.image_processor_dict ,				size=42	)
             self.assertEqual(image_processor.size ,				{"height": 42, "width": 42}	)





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             lowerCamelCase__		:    Any   =  self.image_processing_class(**self.image_processor_dict	)
             # create random PIL images
             lowerCamelCase__		:    Any   =  prepare_image_inputs(self.image_processor_tester ,				equal_resolution=__lowerCamelCase	)
             for image in image_inputs:
                   self.assertIsInstance(__lowerCamelCase ,				Image.Image	)

             # Test not batched input
             lowerCamelCase__		:    Union[str, 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.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)

             # Test batched
             lowerCamelCase__		:    Dict   =  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["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)





       def 						lowerCAmelCase					(       self							:      List[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  self.image_processing_class(**self.image_processor_dict	)
             # create random numpy tensors
             lowerCamelCase__		:    List[Any]   =  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
             lowerCamelCase__		:    List[str]   =  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["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)

             # Test batched
             lowerCamelCase__		:    Union[str, Any]   =  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["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)





       def 						lowerCAmelCase					(       self							:      List[str]	):


             '''simple docstring'''



             lowerCamelCase__		:    int   =  self.image_processing_class(**self.image_processor_dict	)
             # create random PyTorch tensors
             lowerCamelCase__		:    int   =  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
             lowerCamelCase__		:    Dict   =  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["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)

             # Test batched
             lowerCamelCase__		:    List[Any]   =  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["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)


