





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       :

       def __init__(		self:			Optional[Any]       , _SCREAMING_SNAKE_CASE:			Any       , _SCREAMING_SNAKE_CASE:			List[Any]=2       , _SCREAMING_SNAKE_CASE:			List[str]=8       , _SCREAMING_SNAKE_CASE:			Optional[Any]=True       , _SCREAMING_SNAKE_CASE:			List[str]=False       , _SCREAMING_SNAKE_CASE:			int=False       , _SCREAMING_SNAKE_CASE:			Optional[Any]=False       , _SCREAMING_SNAKE_CASE:			List[str]=False       , _SCREAMING_SNAKE_CASE:			List[Any]=99       , _SCREAMING_SNAKE_CASE:			str=32       , _SCREAMING_SNAKE_CASE:			Tuple=3       , _SCREAMING_SNAKE_CASE:			Union[str, Any]=4       , _SCREAMING_SNAKE_CASE:			List[Any]=37       , _SCREAMING_SNAKE_CASE:			int=32       , _SCREAMING_SNAKE_CASE:			int=128       , _SCREAMING_SNAKE_CASE:			Dict=8       , _SCREAMING_SNAKE_CASE:			Optional[Any]=8       , _SCREAMING_SNAKE_CASE:			str=8       , _SCREAMING_SNAKE_CASE:			Dict=1.0       , _SCREAMING_SNAKE_CASE:			List[str]=True       , )							->			Optional[int]:




              """simple docstring"""


              UpperCamelCase_ 							=	parent
              UpperCamelCase_ 							=	batch_size
              UpperCamelCase_ 							=	seq_length
              UpperCamelCase_ 							=	is_training
              UpperCamelCase_ 							=	use_token_type_ids
              UpperCamelCase_ 							=	use_input_mask
              UpperCamelCase_ 							=	use_labels
              UpperCamelCase_ 							=	use_mc_token_ids
              UpperCamelCase_ 							=	vocab_size
              UpperCamelCase_ 							=	hidden_size
              UpperCamelCase_ 							=	num_hidden_layers
              UpperCamelCase_ 							=	num_attention_heads
              UpperCamelCase_ 							=	intermediate_size
              UpperCamelCase_ 							=	num_buckets
              UpperCamelCase_ 							=	max_distance
              UpperCamelCase_ 							=	prompt_length
              UpperCamelCase_ 							=	prompt_types
              UpperCamelCase_ 							=	segment_types
              UpperCamelCase_ 							=	init_std
              UpperCamelCase_ 							=	return_dict

       def lowercase			(		self:			Dict		)							->			str:




              """simple docstring"""


              UpperCamelCase_ 							=	{}
              UpperCamelCase_ 							=	ids_tensor([self.batch_size, self.seq_length]       , self.vocab_size		).type(torch.intaa		)
              UpperCamelCase_ 							=	False

              UpperCamelCase_ 							=	self.get_config()

              return (config, input_ids)

       def lowercase			(		self:			Union[str, Any]		)							->			List[Any]:




              """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=_SCREAMING_SNAKE_CASE       , init_std=self.init_std       , return_dict=self.return_dict       , )

       def lowercase			(		self:			Dict       , _SCREAMING_SNAKE_CASE:			Union[str, Any]       , _SCREAMING_SNAKE_CASE:			List[str]       , *_SCREAMING_SNAKE_CASE:			Tuple		)							->			str:




              """simple docstring"""


              UpperCamelCase_ 							=	CpmAntModel(config=_SCREAMING_SNAKE_CASE		)
              model.to(_SCREAMING_SNAKE_CASE		)
              model.eval()

              UpperCamelCase_ 							=	model(**_SCREAMING_SNAKE_CASE		).last_hidden_state

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

       def lowercase			(		self:			Optional[Any]       , _SCREAMING_SNAKE_CASE:			Tuple       , _SCREAMING_SNAKE_CASE:			Optional[Any]       , *_SCREAMING_SNAKE_CASE:			Dict		)							->			Optional[int]:




              """simple docstring"""


              UpperCamelCase_ 							=	CpmAntForCausalLM(_SCREAMING_SNAKE_CASE		)
              model.to(_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_ 							=	input_ids["input_ids"].to(_SCREAMING_SNAKE_CASE		)
              model.eval()

              UpperCamelCase_ 							=	model(**_SCREAMING_SNAKE_CASE		)
              self.parent.assertEqual(
                  model_output.logits.shape       , (self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)       , )



       def lowercase			(		self:			Any		)							->			Dict:




              """simple docstring"""


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



@require_torch
class 				_UpperCamelCase       (						lowerCAmelCase_	,    lowerCAmelCase_	,    unittest.TestCase  ):
       _UpperCamelCase					:       Dict       = (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
       _UpperCamelCase					:       Union[str, Any]       = (
           {'''feature-extraction''': CpmAntModel, '''text-generation''': CpmAntForCausalLM} if is_torch_available() else {}
       )

       _UpperCamelCase					:       Optional[Any]       = False
       _UpperCamelCase					:       Dict       = False
       _UpperCamelCase					:       Any       = False
       _UpperCamelCase					:       Optional[Any]       = False
       _UpperCamelCase					:       Any       = False

       def lowercase			(		self:			Union[str, Any]		)							->			str:




              """simple docstring"""


              UpperCamelCase_ 							=	CpmAntModelTester(self		)
              UpperCamelCase_ 							=	ConfigTester(self       , config_class=_SCREAMING_SNAKE_CASE		)

       def lowercase			(		self:			str		)							->			Any:




              """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 lowercase			(		self:			Any		)							->			List[str]:




              """simple docstring"""


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

       def lowercase			(		self:			List[str]		)							->			Optional[Any]:




              """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 lowercase			(		self:			List[str]		)							->			Optional[Any]:




              """simple docstring"""


              UpperCamelCase_							,			UpperCamelCase_ 							=	self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_cpmant_model(_SCREAMING_SNAKE_CASE       , _SCREAMING_SNAKE_CASE		)



       def lowercase			(		self:			Union[str, Any]		)							->			List[str]:




              """simple docstring"""


              UpperCamelCase_							,			UpperCamelCase_ 							=	self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_lm_head_model(_SCREAMING_SNAKE_CASE       , _SCREAMING_SNAKE_CASE		)



@require_torch
class 				_UpperCamelCase       (						unittest.TestCase  ):

       @tooslow
       def lowercase			(		self:			List[Any]		)							->			List[Any]:




              """simple docstring"""


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

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



@require_torch
class 				_UpperCamelCase       (						unittest.TestCase  ):

       @tooslow
       def lowercase			(		self:			Optional[Any]		)							->			Union[str, Any]:




              """simple docstring"""


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

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

       @tooslow
       def lowercase			(		self:			Dict		)							->			Optional[int]:




              """simple docstring"""


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



       @tooslow
       def lowercase			(		self:			List[str]		)							->			Optional[Any]:




              """simple docstring"""


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





'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCAmelCase	:		List[str]		  =						logging.get_logger(__name__)

_lowerCAmelCase	:		int		  =						{
    'facebook/opt-125m': 'https://huggingface.co/facebook/opt-125m/blob/main/config.json',
    'facebook/opt-350m': 'https://huggingface.co/facebook/opt-350m/blob/main/config.json',
    'facebook/opt-1.3b': 'https://huggingface.co/facebook/opt-1.3b/blob/main/config.json',
    'facebook/opt-2.7b': 'https://huggingface.co/facebook/opt-2.7b/blob/main/config.json',
    'facebook/opt-6.7b': 'https://huggingface.co/facebook/opt-6.7b/blob/main/config.json',
    'facebook/opt-13b': 'https://huggingface.co/facebook/opt-13b/blob/main/config.json',
}



class 			lowerCAmelCase					(					a				):
	_lowerCamelCase :       Dict         =			"""opt"""
	_lowerCamelCase :       Optional[Any]         =			["""past_key_values"""]



	def __init__(       self			,       snake_case__=5_0272			,       snake_case__=768			,       snake_case__=12			,       snake_case__=3072			,       snake_case__=2048			,       snake_case__=True			,       snake_case__=False			,       snake_case__=None			,       snake_case__=0.1			,       snake_case__=0.0			,       snake_case__=12			,       snake_case__="relu"			,       snake_case__=0.0			,       snake_case__=0.0_2			,       snake_case__=True			,       snake_case__=1			,       snake_case__=2			,       snake_case__=2			,       snake_case__=True			,       snake_case__=True			,       **snake_case__			,       ):
		super().__init__(
		    pad_token_id=snake_case__			,       bos_token_id=snake_case__			,       eos_token_id=snake_case__			,       **snake_case__			,       )
		lowerCAmelCase       :     Union[str, Any]   				=			vocab_size
		lowerCAmelCase       :     List[Any]   				=			max_position_embeddings
		lowerCAmelCase       :     List[str]   				=			num_attention_heads
		lowerCAmelCase       :     Dict   				=			word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
		lowerCAmelCase       :     Optional[Any]   				=			ffn_dim
		lowerCAmelCase       :     Optional[int]   				=			hidden_size
		lowerCAmelCase       :     List[str]   				=			num_hidden_layers
		lowerCAmelCase       :     Union[str, Any]   				=			dropout
		lowerCAmelCase       :     List[str]   				=			attention_dropout
		lowerCAmelCase       :     Union[str, Any]   				=			activation_function
		lowerCAmelCase       :     Optional[Any]   				=			init_std
		lowerCAmelCase       :     List[str]   				=			layerdrop
		lowerCAmelCase       :     Dict   				=			use_cache
		lowerCAmelCase       :     List[Any]   				=			do_layer_norm_before
		# We keep these variables at `True` for backward compatibility.
		lowerCAmelCase       :     List[str]   				=			enable_bias
		lowerCAmelCase       :     Dict   				=			layer_norm_elementwise_affine

		# Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
		# with checkpoints that have been fine-tuned before transformers v4.20.1
		# see https://github.com/facebookresearch/metaseq/pull/164
		lowerCAmelCase       :     Tuple   				=			_remove_final_layer_norm
