




"""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  _snake_case				:

				'''simple docstring'''




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





				def 				snake_case_ (							self      :  Dict     ):
								UpperCAmelCase_					:int      			=      {}
								UpperCAmelCase_					:Any      			=      ids_tensor([self.batch_size, self.seq_length]					,							self.vocab_size     ).type(torch.intaa     )
								UpperCAmelCase_					:str      			=      False

								UpperCAmelCase_					:Optional[Any]      			=      self.get_config()

								return (config, input_ids)





				def 				snake_case_ (							self      :  List[str]     ):
								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 				snake_case_ (							self      :  List[Any]					,							snake_case      :  Optional[int]					,							snake_case      :  Union[str, Any]					,							*snake_case      :  str     ):
								UpperCAmelCase_					:Tuple      			=      CpmAntModel(config=snake_case     )
								model.to(snake_case     )
								model.eval()

								UpperCAmelCase_					:str      			=      model(**snake_case     ).last_hidden_state

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





				def 				snake_case_ (							self      :  Optional[int]					,							snake_case      :  int					,							snake_case      :  int					,							*snake_case      :  str     ):
								UpperCAmelCase_					:int      			=      CpmAntForCausalLM(snake_case     )
								model.to(snake_case     )
								UpperCAmelCase_					:List[str]      			=      input_ids['''input_ids'''].to(snake_case     )
								model.eval()

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



@require_torch
class  _snake_case				(							A__							,						A__							,						unittest.TestCase       ):

				'''simple docstring'''




				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 				snake_case_ (							self      :  int     ):
								UpperCAmelCase_					:Union[str, Any]      			=      CpmAntModelTester(self     )
								UpperCAmelCase_					:Any      			=      ConfigTester(self					,							config_class=snake_case     )





				def 				snake_case_ (							self      :  str     ):
								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 				snake_case_ (							self      :  Optional[Any]     ):
								unittest.skip('''CPMAnt doesn\'t support input_embeds.'''     )(self.test_inputs_embeds     )





				def 				snake_case_ (							self      :  Dict     ):
								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 				snake_case_ (							self      :  str     ):
								UpperCAmelCase_			,UpperCAmelCase_					:str      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_cpmant_model(snake_case					,							snake_case     )





				def 				snake_case_ (							self      :  Dict     ):
								UpperCAmelCase_			,UpperCAmelCase_					:int      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(snake_case					,							snake_case     )



@require_torch
class  _snake_case				(							unittest.TestCase       ):

				'''simple docstring'''




				@tooslow
				def 				snake_case_ (							self      :  Union[str, Any]     ):
								UpperCAmelCase_					:int      			=      '''今天天气真好！'''
								UpperCAmelCase_					:List[Any]      			=      '''openbmb/cpm-ant-10b'''
								UpperCAmelCase_					:Dict      			=      CpmAntModel.from_pretrained(snake_case     )
								UpperCAmelCase_					:Union[str, Any]      			=      CpmAntTokenizer.from_pretrained(snake_case     )
								UpperCAmelCase_					:int      			=      tokenizer(snake_case					,							return_tensors='''pt'''     )
								UpperCAmelCase_					:Optional[int]      			=      model(**snake_case     ).last_hidden_state

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



@require_torch
class  _snake_case				(							unittest.TestCase       ):

				'''simple docstring'''




				@tooslow
				def 				snake_case_ (							self      :  str     ):
								UpperCAmelCase_					:str      			=      '''今天天气真好！'''
								UpperCAmelCase_					:Dict      			=      '''openbmb/cpm-ant-10b'''
								UpperCAmelCase_					:Any      			=      CpmAntForCausalLM.from_pretrained(snake_case     )
								UpperCAmelCase_					:Optional[int]      			=      CpmAntTokenizer.from_pretrained(snake_case     )
								UpperCAmelCase_					:Union[str, Any]      			=      tokenizer(snake_case					,							return_tensors='''pt'''     )
								UpperCAmelCase_					:Dict      			=      model(**snake_case     ).logits

								UpperCAmelCase_					:Dict      			=      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]					,							snake_case					,							atol=1e-2     )     )





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





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








"""simple docstring"""

def 				a (						__snake_case     :				int  ):



				'''simple docstring'''





				if not isinstance(__snake_case,    __snake_case  ) or number < 0:
								raise ValueError('''Input must be a non-negative integer'''  )

				UpperCAmelCase_					:Optional[Any]      			=      number & ~(number - 1)
				UpperCAmelCase_					:List[str]      			=      0
				while intermediate:
								intermediate >>= 1
								index += 1
				return index - 1


if __name__ == "__main__":
						import doctest

						doctest.testmod(verbose=True)


