




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




							'''simple docstring'''



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





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

										SCREAMING_SNAKE_CASE     =       self.get_config()

										return (config, input_ids)





							def  _UpperCAmelCase  (       self							:						int      )      ->       Tuple:
										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							:						Dict							,  a							:						Optional[Any]							,  a							:						str							,  *a							:						str      )      ->       Optional[Any]:
										SCREAMING_SNAKE_CASE     =       CpmAntModel(config=lowerCamelCase_      )
										model.to(lowerCamelCase_      )
										model.eval()

										SCREAMING_SNAKE_CASE     =       model(**lowerCamelCase_      ).last_hidden_state

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





							def  _UpperCAmelCase  (       self							:						Optional[Any]							,  a							:						Any							,  a							:						str							,  *a							:						Dict      )      ->       Tuple:
										SCREAMING_SNAKE_CASE     =       CpmAntForCausalLM(lowerCamelCase_      )
										model.to(lowerCamelCase_      )
										SCREAMING_SNAKE_CASE     =       input_ids["""input_ids"""].to(lowerCamelCase_      )
										model.eval()

										SCREAMING_SNAKE_CASE     =       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]      )      ->       Any:
										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       self.prepare_config_and_inputs()
										return config, inputs_dict





@require_torch
class       UpperCAmelCase_   ( __SCREAMING_SNAKE_CASE		,   __SCREAMING_SNAKE_CASE		,   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  _UpperCAmelCase  (       self							:						Dict      )      ->       Dict:
										SCREAMING_SNAKE_CASE     =       CpmAntModelTester(self      )
										SCREAMING_SNAKE_CASE     =       ConfigTester(self							,  config_class=lowerCamelCase_      )





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





							def  _UpperCAmelCase  (       self							:						Optional[int]      )      ->       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  _UpperCAmelCase  (       self							:						Union[str, Any]      )      ->       Union[str, Any]:
										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_cpmant_model(lowerCamelCase_							,  lowerCamelCase_      )







							def  _UpperCAmelCase  (       self							:						List[Any]      )      ->       Dict:
										SCREAMING_SNAKE_CASE  ,	SCREAMING_SNAKE_CASE     =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(lowerCamelCase_							,  lowerCamelCase_      )





@require_torch
class       UpperCAmelCase_   ( unittest.TestCase							):




							'''simple docstring'''



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

										SCREAMING_SNAKE_CASE     =       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       UpperCAmelCase_   ( unittest.TestCase							):




							'''simple docstring'''



							@tooslow
							def  _UpperCAmelCase  (       self							:						Union[str, Any]      )      ->       List[str]:
										SCREAMING_SNAKE_CASE     =       """今天天气真好！"""
										SCREAMING_SNAKE_CASE     =       """openbmb/cpm-ant-10b"""
										SCREAMING_SNAKE_CASE     =       CpmAntForCausalLM.from_pretrained(lowerCamelCase_      )
										SCREAMING_SNAKE_CASE     =       CpmAntTokenizer.from_pretrained(lowerCamelCase_      )
										SCREAMING_SNAKE_CASE     =       tokenizer(lowerCamelCase_							,  return_tensors="""pt"""      )
										SCREAMING_SNAKE_CASE     =       model(**lowerCamelCase_      ).logits

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







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










"""simple docstring"""







def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round((float(_UpperCamelCase			) * 9 / 5) + 32						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round(float(_UpperCamelCase			) + 273.15						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round((float(_UpperCamelCase			) * 9 / 5) + 491.67						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round((float(_UpperCamelCase			) - 32) * 5 / 9						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round(((float(_UpperCamelCase			) - 32) * 5 / 9) + 273.15						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round(float(_UpperCamelCase			) + 459.67						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round(float(_UpperCamelCase			) - 273.15						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round(((float(_UpperCamelCase			) - 273.15) * 9 / 5) + 32						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round((float(_UpperCamelCase			) * 9 / 5)						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round((float(_UpperCamelCase			) - 491.67) * 5 / 9						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round(float(_UpperCamelCase			) - 459.67						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round((float(_UpperCamelCase			) * 5 / 9)						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round((float(_UpperCamelCase			) * 1.25 + 273.15)						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round((float(_UpperCamelCase			) * 2.25 + 32)						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round((float(_UpperCamelCase			) * 1.25)						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = 2			):





						"""simple docstring"""

						return round((float(_UpperCamelCase			) * 2.25 + 32 + 459.67)						,      _UpperCamelCase			)


if __name__ == "__main__":
						import doctest

						doctest.testmod()