


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






						"""simple docstring"""




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




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							List[str]      =       {}
							lowerCamelCase_			:							str      =       ids_tensor([self.batch_size, self.seq_length]				,							self.vocab_size     ).type(torch.intaa     )
							lowerCamelCase_			:							Any      =       False

							lowerCamelCase_			:							Any      =       self.get_config()

							return (config, input_ids)




						def 					_UpperCamelCase			(   self     ):
							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=a_				,							init_std=self.init_std				,							return_dict=self.return_dict				,							)




						def 					_UpperCamelCase			(   self				,							a_				,							a_				,							*a_     ):
							lowerCamelCase_			:							List[str]      =       CpmAntModel(config=a_     )
							model.to(a_     )
							model.eval()

							lowerCamelCase_			:							int      =       model(**a_     ).last_hidden_state

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




						def 					_UpperCamelCase			(   self				,							a_				,							a_				,							*a_     ):
							lowerCamelCase_			:							List[str]      =       CpmAntForCausalLM(a_     )
							model.to(a_     )
							lowerCamelCase_			:							str      =       input_ids["input_ids"].to(a_     )
							model.eval()

							lowerCamelCase_			:							str      =       model(**a_     )
							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     ):
							lowerCamelCase_			:							Dict      =       self.prepare_config_and_inputs()
							return config, inputs_dict





@require_torch
class    lowerCAmelCase__				(			lowerCamelCase_,    lowerCamelCase_,    unittest.TestCase       ):






						"""simple docstring"""
						__UpperCAmelCase   :		Optional[int]  					=	(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
						__UpperCAmelCase   :		List[str]  					=	(
						    {"""feature-extraction""": CpmAntModel, """text-generation""": CpmAntForCausalLM} if is_torch_available() else {}
						)

						__UpperCAmelCase   :		List[Any]  					=	False
						__UpperCAmelCase   :		Any  					=	False
						__UpperCAmelCase   :		Any  					=	False
						__UpperCAmelCase   :		Optional[Any]  					=	False
						__UpperCAmelCase   :		Optional[int]  					=	False




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							Tuple      =       CpmAntModelTester(self     )
							lowerCamelCase_			:							Dict      =       ConfigTester(self				,							config_class=a_     )




						def 					_UpperCamelCase			(   self     ):
							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     ):
							unittest.skip("CPMAnt doesn't support input_embeds."     )(self.test_inputs_embeds     )




						def 					_UpperCamelCase			(   self     ):
							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     ):
							lowerCamelCase_			:							Any      =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_cpmant_model(a_				,							a_     )




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							List[str]      =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_lm_head_model(a_				,							a_     )





@require_torch
class    lowerCAmelCase__				(			unittest.TestCase       ):






						"""simple docstring"""




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

							lowerCamelCase_			:							Dict      =       torch.tensor(
							    [[[6.17_08, 5.92_44, 1.08_35], [6.52_07, 6.28_93, -11.33_24], [-1.01_07, -0.05_76, -5.95_77]]]				,							)
							self.assertTrue(torch.allclose(hidden_states[:, :3, :3]				,							a_				,							atol=1E-2     )     )





@require_torch
class    lowerCAmelCase__				(			unittest.TestCase       ):






						"""simple docstring"""




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

							lowerCamelCase_			:							Any      =       torch.tensor(
							    [[[-6.42_67, -6.40_83, -6.39_58], [-5.88_02, -5.94_47, -5.78_11], [-5.38_96, -5.48_20, -5.42_95]]]				,							)
							self.assertTrue(torch.allclose(hidden_states[:, :3, :3]				,							a_				,							atol=1E-2     )     )




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




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





from __future__ import annotations





class    lowerCAmelCase__				:






						"""simple docstring"""




						def __init__(   self				,							a_     ):
							lowerCamelCase_			:							List[str]      =       value
							lowerCamelCase_			:							Node | None      =       None
							lowerCamelCase_			:							Node | None      =       None





class    lowerCAmelCase__				:






						"""simple docstring"""
						__UpperCAmelCase   :		int




						def __init__(   self     ):
							lowerCamelCase_			:							str      =       0




						def 					_UpperCamelCase			(   self				,							a_				,							a_     ):
							if node is None:
								return

							if path_sum == self.target:
								self.paths += 1

							if node.left:
								self.depth_first_search(node.left				,							path_sum + node.left.value     )
							if node.right:
								self.depth_first_search(node.right				,							path_sum + node.right.value     )




						def 					_UpperCamelCase			(   self				,							a_				,							a_ = None     ):
							if node is None:
								return 0
							if target is not None:
								lowerCamelCase_			:							List[str]      =       target

							self.depth_first_search(a_				,							node.value     )
							self.path_sum(node.left     )
							self.path_sum(node.right     )

							return self.paths


if __name__ == "__main__":
							import doctest

							doctest.testmod()

