




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      A     :
     """simple docstring"""

     def __init__(      self					:				Tuple		,					A__					:				List[Any]		,					A__					:				Any=2		,					A__					:				int=8		,					A__					:				List[str]=True		,					A__					:				Dict=False		,					A__					:				Dict=False		,					A__					:				Any=False		,					A__					:				Tuple=False		,					A__					:				int=9_9		,					A__					:				Optional[int]=3_2		,					A__					:				Optional[Any]=3		,					A__					:				Any=4		,					A__					:				Optional[Any]=3_7		,					A__					:				int=3_2		,					A__					:				Tuple=1_2_8		,					A__					:				Union[str, Any]=8		,					A__					:				Union[str, Any]=8		,					A__					:				Optional[int]=8		,					A__					:				Optional[int]=1.0		,					A__					:				List[str]=True		,					)			->    Dict:
           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 	__snake_case				(      self					:				Tuple     )			->    Dict:
           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 	__snake_case				(      self					:				Optional[int]     )			->    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=A__		,					init_std=self.init_std		,					return_dict=self.return_dict		,					)




     def 	__snake_case				(      self					:				str		,					A__					:				Any		,					A__					:				str		,					*A__					:				Tuple     )			->    Optional[Any]:
           lowerCamelCase__ 					=			CpmAntModel(config=A__     )
           model.to(A__     )
           model.eval()

           lowerCamelCase__ 					=			model(**A__     ).last_hidden_state

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




     def 	__snake_case				(      self					:				Tuple		,					A__					:				Union[str, Any]		,					A__					:				Optional[int]		,					*A__					:				Union[str, Any]     )			->    Optional[int]:
           lowerCamelCase__ 					=			CpmAntForCausalLM(A__     )
           model.to(A__     )
           lowerCamelCase__ 					=			input_ids['input_ids'].to(A__     )
           model.eval()

           lowerCamelCase__ 					=			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 	__snake_case				(      self					:				Optional[Any]     )			->    Union[str, Any]:
           lowerCamelCase__					,						lowerCamelCase__ 					=			self.prepare_config_and_inputs()
           return config, inputs_dict






@require_torch
class      A     (						_lowerCAmelCase     ,  _lowerCAmelCase     ,  unittest.TestCase						):
     """simple docstring"""

     _snake_case:		Optional[int]       =(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
     _snake_case:		Optional[int]       =(
         {"feature-extraction": CpmAntModel, "text-generation": CpmAntForCausalLM} if is_torch_available() else {}
     )

     _snake_case:		Dict       =False
     _snake_case:		Union[str, Any]       =False
     _snake_case:		Optional[Any]       =False
     _snake_case:		Any       =False
     _snake_case:		List[Any]       =False




     def 	__snake_case				(      self					:				Union[str, Any]     )			->    List[str]:
           lowerCamelCase__ 					=			CpmAntModelTester(self     )
           lowerCamelCase__ 					=			ConfigTester(self		,					config_class=A__     )




     def 	__snake_case				(      self					:				Optional[int]     )			->    Optional[Any]:
           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 	__snake_case				(      self					:				List[str]     )			->    Optional[Any]:
           unittest.skip('CPMAnt doesn\'t support input_embeds.'     )(self.test_inputs_embeds     )




     def 	__snake_case				(      self					:				str     )			->    Tuple:
           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 	__snake_case				(      self					:				str     )			->    Optional[Any]:
           lowerCamelCase__					,						lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_cpmant_model(A__		,					A__     )




     def 	__snake_case				(      self					:				Tuple     )			->    Optional[int]:
           lowerCamelCase__					,						lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head_model(A__		,					A__     )






@require_torch
class      A     (						unittest.TestCase						):
     """simple docstring"""

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

           lowerCamelCase__ 					=			torch.tensor(
               [[[6.1708, 5.9244, 1.0835], [6.5207, 6.2893, -11.3324], [-1.0107, -0.0576, -5.9577]]]		,					)
           self.assertTrue(torch.allclose(hidden_states[:, :3, :3]		,					A__		,					atol=1E-2     )     )






@require_torch
class      A     (						unittest.TestCase						):
     """simple docstring"""

     @tooslow
     def 	__snake_case				(      self					:				Optional[Any]     )			->    Dict:
           lowerCamelCase__ 					=			'今天天气真好！'
           lowerCamelCase__ 					=			'openbmb/cpm-ant-10b'
           lowerCamelCase__ 					=			CpmAntForCausalLM.from_pretrained(A__     )
           lowerCamelCase__ 					=			CpmAntTokenizer.from_pretrained(A__     )
           lowerCamelCase__ 					=			tokenizer(A__		,					return_tensors='pt'     )
           lowerCamelCase__ 					=			model(**A__     ).logits

           lowerCamelCase__ 					=			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]		,					A__		,					atol=1E-2     )     )




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




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





from __future__ import annotations

def      lowerCAmelCase_     (					lowerCamelCase:    list[int | float]  )   ->     int | float:


      '''simple docstring'''




      if len(lowerCamelCase  ) == 0:
            raise ValueError('find_max() arg is an empty sequence'  )
      lowerCamelCase__ 					=			nums[0]
      for x in nums:
            if x > max_num:
                  lowerCamelCase__ 					=			x
      return max_num


if __name__ == "__main__":
      import doctest

      doctest.testmod(verbose=True)