



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






				"""simple docstring"""

				def __init__(				self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__=2						,	SCREAMING_SNAKE_CASE__=8						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=False						,	SCREAMING_SNAKE_CASE__=False						,	SCREAMING_SNAKE_CASE__=False						,	SCREAMING_SNAKE_CASE__=False						,	SCREAMING_SNAKE_CASE__=99						,	SCREAMING_SNAKE_CASE__=32						,	SCREAMING_SNAKE_CASE__=3						,	SCREAMING_SNAKE_CASE__=4						,	SCREAMING_SNAKE_CASE__=37						,	SCREAMING_SNAKE_CASE__=32						,	SCREAMING_SNAKE_CASE__=1_28						,	SCREAMING_SNAKE_CASE__=8						,	SCREAMING_SNAKE_CASE__=8						,	SCREAMING_SNAKE_CASE__=8						,	SCREAMING_SNAKE_CASE__=1.0						,	SCREAMING_SNAKE_CASE__=True						,	):






					"""simple docstring"""

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







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Optional[int]					     =   {}
					_snake_case					:							List[Any]					     =   ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size      ).type(torch.intaa      )
					_snake_case					:							Dict					     =   False

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

					return (config, input_ids)







				def 		__lowerCamelCase(				self      ):






					"""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						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	*SCREAMING_SNAKE_CASE__      ):






					"""simple docstring"""

					_snake_case					:							Tuple					     =   CpmAntModel(config=UpperCamelCase__      )
					model.to(UpperCamelCase__      )
					model.eval()

					_snake_case					:							Union[str, Any]					     =   model(**UpperCamelCase__      ).last_hidden_state

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







				def 		__lowerCamelCase(				self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	*SCREAMING_SNAKE_CASE__      ):






					"""simple docstring"""

					_snake_case					:							int					     =   CpmAntForCausalLM(UpperCamelCase__      )
					model.to(UpperCamelCase__      )
					_snake_case					:							Any					     =   input_ids['''input_ids'''].to(UpperCamelCase__      )
					model.eval()

					_snake_case					:							Dict					     =   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      ):






					"""simple docstring"""

					_snake_case					:							List[str]					     =   self.prepare_config_and_inputs()
					return config, inputs_dict





@require_torch
class 		__SCREAMING_SNAKE_CASE      (				SCREAMING_SNAKE_CASE_					,			SCREAMING_SNAKE_CASE_					,			unittest.TestCase      ):






				"""simple docstring"""

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

				SCREAMING_SNAKE_CASE_ 				=  False
				SCREAMING_SNAKE_CASE_ 				=  False
				SCREAMING_SNAKE_CASE_ 				=  False
				SCREAMING_SNAKE_CASE_ 				=  False
				SCREAMING_SNAKE_CASE_ 				=  False







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Optional[int]					     =   CpmAntModelTester(self      )
					_snake_case					:							List[str]					     =   ConfigTester(self						,	config_class=UpperCamelCase__      )







				def 		__lowerCamelCase(				self      ):






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






					"""simple docstring"""

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







				def 		__lowerCamelCase(				self      ):






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







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Tuple					     =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_cpmant_model(UpperCamelCase__						,	UpperCamelCase__      )







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							List[str]					     =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_lm_head_model(UpperCamelCase__						,	UpperCamelCase__      )





@require_torch
class 		__SCREAMING_SNAKE_CASE      (				unittest.TestCase      ):






				"""simple docstring"""

				@tooslow
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

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

					_snake_case					:							str					     =   torch.tensor(
					    [[[6.1708, 5.9244, 1.0835], [6.5207, 6.2893, -11.33_24], [-1.0107, -0.0576, -5.9577]]]						,	)
					self.assertTrue(torch.allclose(hidden_states[:, :3, :3]						,	UpperCamelCase__						,	atol=1e-2      )      )





@require_torch
class 		__SCREAMING_SNAKE_CASE      (				unittest.TestCase      ):






				"""simple docstring"""

				@tooslow
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

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

					_snake_case					:							List[str]					     =   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]						,	UpperCamelCase__						,	atol=1e-2      )      )







				@tooslow
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

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







				@tooslow
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

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







def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Dict				,    __UpperCamelCase			:    Dict							)			->							List[Any]:

				"""simple docstring"""







				if len(str(__UpperCamelCase							)							) == 1 or len(str(__UpperCamelCase							)							) == 1:
								return a * b

				A__	:						List[str]								=				max(len(str(__UpperCamelCase							)							)				,    len(str(__UpperCamelCase							)							)							)
				A__	:						Any								=				ma // 2

				A__							,				A__	:						Union[str, Any]								=				divmod(__UpperCamelCase				,    10**ma							)
				A__							,				A__	:						Tuple								=				divmod(__UpperCamelCase				,    10**ma							)

				A__	:						Optional[int]								=				karatsuba(__UpperCamelCase				,    __UpperCamelCase							)
				A__	:						Tuple								=				karatsuba((aa + aa)				,    (ba + ba)							)
				A__	:						Optional[int]								=				karatsuba(__UpperCamelCase				,    __UpperCamelCase							)

				return (z * 10 ** (2 * ma)) + ((y - z - x) * 10 ** (ma)) + (x)

def 					SCREAMING_SNAKE_CASE  (   )			->							Dict:

				"""simple docstring"""







				print(karatsuba(1_54_63				,    2_34_89							)							)


if __name__ == "__main__":
					main()