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





			'''simple docstring'''



			def __init__(				self	,	__UpperCAmelCase	,	__UpperCAmelCase=2	,	__UpperCAmelCase=8	,	__UpperCAmelCase=True	,	__UpperCAmelCase=False	,	__UpperCAmelCase=False	,	__UpperCAmelCase=False	,	__UpperCAmelCase=False	,	__UpperCAmelCase=99	,	__UpperCAmelCase=32	,	__UpperCAmelCase=3	,	__UpperCAmelCase=4	,	__UpperCAmelCase=37	,	__UpperCAmelCase=32	,	__UpperCAmelCase=1_28	,	__UpperCAmelCase=8	,	__UpperCAmelCase=8	,	__UpperCAmelCase=8	,	__UpperCAmelCase=1.0	,	__UpperCAmelCase=True	,	)					->	Optional[Any]:
				_lowerCAmelCase								=parent
				_lowerCAmelCase								=batch_size
				_lowerCAmelCase								=seq_length
				_lowerCAmelCase								=is_training
				_lowerCAmelCase								=use_token_type_ids
				_lowerCAmelCase								=use_input_mask
				_lowerCAmelCase								=use_labels
				_lowerCAmelCase								=use_mc_token_ids
				_lowerCAmelCase								=vocab_size
				_lowerCAmelCase								=hidden_size
				_lowerCAmelCase								=num_hidden_layers
				_lowerCAmelCase								=num_attention_heads
				_lowerCAmelCase								=intermediate_size
				_lowerCAmelCase								=num_buckets
				_lowerCAmelCase								=max_distance
				_lowerCAmelCase								=prompt_length
				_lowerCAmelCase								=prompt_types
				_lowerCAmelCase								=segment_types
				_lowerCAmelCase								=init_std
				_lowerCAmelCase								=return_dict



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

				_lowerCAmelCase								=self.get_config()

				return (config, input_ids)



			def    _lowerCAmelCase						(				self    )					->	List[Any]:
				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	,	__UpperCAmelCase	,	__UpperCAmelCase	,	*__UpperCAmelCase    )					->	int:
				_lowerCAmelCase								=CpmAntModel(config=__UpperCAmelCase    )
				model.to(__UpperCAmelCase    )
				model.eval()

				_lowerCAmelCase								=model(**__UpperCAmelCase    ).last_hidden_state

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



			def    _lowerCAmelCase						(				self	,	__UpperCAmelCase	,	__UpperCAmelCase	,	*__UpperCAmelCase    )					->	List[Any]:
				_lowerCAmelCase								=CpmAntForCausalLM(__UpperCAmelCase    )
				model.to(__UpperCAmelCase    )
				_lowerCAmelCase								=input_ids["""input_ids"""].to(__UpperCAmelCase    )
				model.eval()

				_lowerCAmelCase								=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    )					->	Tuple:
				_lowerCAmelCase		,				_lowerCAmelCase								=self.prepare_config_and_inputs()
				return config, inputs_dict







@require_torch
class 						lowerCamelCase__   (   __magic_name__					,      __magic_name__					,      unittest.TestCase							):





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

			lowerCamelCase       =						False
			lowerCamelCase       =						False
			lowerCamelCase       =						False
			lowerCamelCase       =						False
			lowerCamelCase       =						False



			def    _lowerCAmelCase						(				self    )					->	str:
				_lowerCAmelCase								=CpmAntModelTester(self    )
				_lowerCAmelCase								=ConfigTester(self	,	config_class=__UpperCAmelCase    )



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



			def    _lowerCAmelCase						(				self    )					->	Union[str, Any]:
				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    )					->	str:
				_lowerCAmelCase		,				_lowerCAmelCase								=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_cpmant_model(__UpperCAmelCase	,	__UpperCAmelCase    )



			def    _lowerCAmelCase						(				self    )					->	Tuple:
				_lowerCAmelCase		,				_lowerCAmelCase								=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_lm_head_model(__UpperCAmelCase	,	__UpperCAmelCase    )







@require_torch
class 						lowerCamelCase__   (   unittest.TestCase							):





			'''simple docstring'''



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

				_lowerCAmelCase								=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, -1_1.3_3_2_4], [-1.0_1_0_7, -0.0_5_7_6, -5.9_5_7_7]]]	,	)
				self.assertTrue(torch.allclose(hidden_states[:, :3, :3]	,	__UpperCAmelCase	,	atol=1e-2    )    )







@require_torch
class 						lowerCamelCase__   (   unittest.TestCase							):





			'''simple docstring'''



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

				_lowerCAmelCase								=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]	,	__UpperCAmelCase	,	atol=1e-2    )    )



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



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





"""simple docstring"""


import warnings

from .generation import FlaxGenerationMixin







class 						lowerCamelCase__   (   __magic_name__							):





			'''simple docstring'''
			# warning at import time
			warnings.warn(
			    '''Importing `FlaxGenerationMixin` from `src/transformers/generation_flax_utils.py` is deprecated and will '''
			    '''be removed in Transformers v5. Import as `from transformers import FlaxGenerationMixin` instead.'''					,      __magic_name__					,      )



