


'''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 					_A						:



   '''simple docstring'''






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




   def   __lowerCAmelCase						(	self	:					Dict						)->	List[str]:
          snake_case__					:	Optional[int]          =					{}
          snake_case__					:	List[str]          =					ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size						).type(torch.intaa						)
          snake_case__					:	Optional[Any]          =					False

          snake_case__					:	Tuple          =					self.get_config()

          return (config, input_ids)




   def   __lowerCAmelCase						(	self	:					Tuple						)->	List[Any]:
          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   __lowerCAmelCase						(	self	:					str					, lowerCamelCase	:					Dict					, lowerCamelCase	:					Optional[Any]					, *lowerCamelCase	:					List[Any]						)->	List[str]:
          snake_case__					:	Union[str, Any]          =					CpmAntModel(config=lowerCamelCase						)
          model.to(lowerCamelCase						)
          model.eval()

          snake_case__					:	Tuple          =					model(**lowerCamelCase						).last_hidden_state

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




   def   __lowerCAmelCase						(	self	:					Optional[int]					, lowerCamelCase	:					Any					, lowerCamelCase	:					str					, *lowerCamelCase	:					Union[str, Any]						)->	Dict:
          snake_case__					:	Optional[int]          =					CpmAntForCausalLM(lowerCamelCase						)
          model.to(lowerCamelCase						)
          snake_case__					:	Tuple          =					input_ids["""input_ids"""].to(lowerCamelCase						)
          model.eval()

          snake_case__					:	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   __lowerCAmelCase						(	self	:					List[Any]						)->	Optional[Any]:
          snake_case__       ,   snake_case__					:	int          =					self.prepare_config_and_inputs()
          return config, inputs_dict







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



   '''simple docstring'''






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

   _lowercase    						= False
   _lowercase    						= False
   _lowercase    						= False
   _lowercase    						= False
   _lowercase    						= False




   def   __lowerCAmelCase						(	self	:					Optional[Any]						)->	List[Any]:
          snake_case__					:	Optional[int]          =					CpmAntModelTester(self						)
          snake_case__					:	Dict          =					ConfigTester(self					, config_class=lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					Tuple						)->	Dict:
          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	:					List[str]						)->	List[str]:
          unittest.skip("""CPMAnt doesn't support input_embeds."""						)(self.test_inputs_embeds						)




   def   __lowerCAmelCase						(	self	:					Any						)->	Optional[int]:
          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	:					List[Any]						)->	Union[str, Any]:
          snake_case__       ,   snake_case__					:	Tuple          =					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_cpmant_model(lowerCamelCase					, lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					int						)->	Union[str, Any]:
          snake_case__       ,   snake_case__					:	Optional[Any]          =					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(lowerCamelCase					, lowerCamelCase						)







@require_torch
class 					_A						(						unittest.TestCase							):



   '''simple docstring'''






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

          snake_case__					:	List[Any]          =					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]					, lowerCamelCase					, atol=1e-2						)						)







@require_torch
class 					_A						(						unittest.TestCase							):



   '''simple docstring'''






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

          snake_case__					:	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]					, lowerCamelCase					, atol=1e-2						)						)




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




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








'''simple docstring'''
def 				lowerCAmelCase__	(       UpperCAmelCase = 30     ):






       """simple docstring"""





       snake_case__					:	Tuple          =					exponent + 2
       snake_case__					:	List[Any]          =					(1 + 5**0.5) / 2
       snake_case__					:	Tuple          =					(phi**fibonacci_index - (phi - 1) ** fibonacci_index) / 5**0.5

       return int(UpperCAmelCase     )


if __name__ == "__main__":
     print(f"""{solution() = }""")




