



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

						'''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   ,							):







													'''simple docstring'''

													__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  _A			(					self	):







													'''simple docstring'''

													__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  _A			(					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=_A   ,							init_std=self.init_std   ,							return_dict=self.return_dict   ,							)






						def  _A			(					self   ,							_A   ,							_A   ,							*_A	):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE								=      CpmAntModel(config=_A	)
													model.to(_A	)
													model.eval()

													__SCREAMING_SNAKE_CASE								=      model(**_A	).last_hidden_state

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






						def  _A			(					self   ,							_A   ,							_A   ,							*_A	):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE								=      CpmAntForCausalLM(_A	)
													model.to(_A	)
													__SCREAMING_SNAKE_CASE								=      input_ids['input_ids'].to(_A	)
													model.eval()

													__SCREAMING_SNAKE_CASE								=      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  _A			(					self	):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							, __SCREAMING_SNAKE_CASE								=      self.prepare_config_and_inputs()
													return config, inputs_dict







@require_torch
class      UpperCAmelCase_						(     UpperCamelCase_					,					UpperCamelCase_					,					unittest.TestCase ):

						'''simple docstring'''




						UpperCamelCase__    :				Union[str, Any]       						=						(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
						UpperCamelCase__    :				Tuple       						=						(
						    {'''feature-extraction''': CpmAntModel, '''text-generation''': CpmAntForCausalLM} if is_torch_available() else {}
						)

						UpperCamelCase__    :				Union[str, Any]       						=						False
						UpperCamelCase__    :				Dict       						=						False
						UpperCamelCase__    :				Dict       						=						False
						UpperCamelCase__    :				Dict       						=						False
						UpperCamelCase__    :				List[str]       						=						False






						def  _A			(					self	):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE								=      CpmAntModelTester(self	)
													__SCREAMING_SNAKE_CASE								=      ConfigTester(self   ,							config_class=_A	)






						def  _A			(					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  _A			(					self	):







													'''simple docstring'''

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






						def  _A			(					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  _A			(					self	):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							, __SCREAMING_SNAKE_CASE								=      self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_cpmant_model(_A   ,							_A	)






						def  _A			(					self	):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							, __SCREAMING_SNAKE_CASE								=      self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_lm_head_model(_A   ,							_A	)







@require_torch
class      UpperCAmelCase_						(     unittest.TestCase ):

						'''simple docstring'''




						@tooslow
						def  _A			(					self	):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE								=      '今天天气真好！'
													__SCREAMING_SNAKE_CASE								=      'openbmb/cpm-ant-10b'
													__SCREAMING_SNAKE_CASE								=      CpmAntModel.from_pretrained(_A	)
													__SCREAMING_SNAKE_CASE								=      CpmAntTokenizer.from_pretrained(_A	)
													__SCREAMING_SNAKE_CASE								=      tokenizer(_A   ,							return_tensors='pt'	)
													__SCREAMING_SNAKE_CASE								=      model(**_A	).last_hidden_state

													__SCREAMING_SNAKE_CASE								=      torch.tensor(
													    [[[6.1_7_0_8, 5.9_2_4_4, 1.0_8_3_5], [6.5_2_0_7, 6.2_8_9_3, -11.3_324], [-1.0_1_0_7, -0.0_5_7_6, -5.9_5_7_7]]]   ,							)
													self.assertTrue(torch.allclose(hidden_states[:, :3, :3]   ,							_A   ,							atol=1e-2	)	)







@require_torch
class      UpperCAmelCase_						(     unittest.TestCase ):

						'''simple docstring'''




						@tooslow
						def  _A			(					self	):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE								=      '今天天气真好！'
													__SCREAMING_SNAKE_CASE								=      'openbmb/cpm-ant-10b'
													__SCREAMING_SNAKE_CASE								=      CpmAntForCausalLM.from_pretrained(_A	)
													__SCREAMING_SNAKE_CASE								=      CpmAntTokenizer.from_pretrained(_A	)
													__SCREAMING_SNAKE_CASE								=      tokenizer(_A   ,							return_tensors='pt'	)
													__SCREAMING_SNAKE_CASE								=      model(**_A	).logits

													__SCREAMING_SNAKE_CASE								=      torch.tensor(
													    [[[-6.4_2_6_7, -6.4_0_8_3, -6.3_9_5_8], [-5.8_8_0_2, -5.9_4_4_7, -5.7_8_1_1], [-5.3_8_9_6, -5.4_8_2_0, -5.4_2_9_5]]]   ,							)
													self.assertTrue(torch.allclose(hidden_states[:, :3, :3]   ,							_A   ,							atol=1e-2	)	)






						@tooslow
						def  _A			(					self	):







													'''simple docstring'''

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






						@tooslow
						def  _A			(					self	):







													'''simple docstring'''

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











from __future__ import annotations

class      UpperCAmelCase_						:

						'''simple docstring'''




						def __init__(					self   ,							_A	):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE								=      value
													__SCREAMING_SNAKE_CASE								=      None
													__SCREAMING_SNAKE_CASE								=      None







class      UpperCAmelCase_						:

						'''simple docstring'''




						UpperCamelCase__    :				int






						def __init__(					self	):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE								=      0






						def  _A			(					self   ,							_A   ,							_A	):







													'''simple docstring'''

													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  _A			(					self   ,							_A   ,							_A = None	):







													'''simple docstring'''

													if node is None:
																				return 0
													if target is not None:
																				__SCREAMING_SNAKE_CASE								=      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()






