





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

      def __init__(			self   :List[Any]						,							_lowerCamelCase   :Optional[Any]						,							_lowerCamelCase   :List[Any]=2						,							_lowerCamelCase   :Optional[int]=8						,							_lowerCamelCase   :List[Any]=True						,							_lowerCamelCase   :Optional[int]=False						,							_lowerCamelCase   :Optional[Any]=False						,							_lowerCamelCase   :Any=False						,							_lowerCamelCase   :Union[str, Any]=False						,							_lowerCamelCase   :int=9_9						,							_lowerCamelCase   :Dict=3_2						,							_lowerCamelCase   :Dict=3						,							_lowerCamelCase   :int=4						,							_lowerCamelCase   :Optional[Any]=3_7						,							_lowerCamelCase   :str=3_2						,							_lowerCamelCase   :str=1_2_8						,							_lowerCamelCase   :int=8						,							_lowerCamelCase   :Dict=8						,							_lowerCamelCase   :Tuple=8						,							_lowerCamelCase   :Dict=1.0						,							_lowerCamelCase   :List[str]=True						,							):
       __SCREAMING_SNAKE_CASE :			List[str]												=       parent
       __SCREAMING_SNAKE_CASE :			Any												=       batch_size
       __SCREAMING_SNAKE_CASE :			Any												=       seq_length
       __SCREAMING_SNAKE_CASE :			Any												=       is_training
       __SCREAMING_SNAKE_CASE :			List[str]												=       use_token_type_ids
       __SCREAMING_SNAKE_CASE :			List[str]												=       use_input_mask
       __SCREAMING_SNAKE_CASE :			List[str]												=       use_labels
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       use_mc_token_ids
       __SCREAMING_SNAKE_CASE :			Tuple												=       vocab_size
       __SCREAMING_SNAKE_CASE :			str												=       hidden_size
       __SCREAMING_SNAKE_CASE :			int												=       num_hidden_layers
       __SCREAMING_SNAKE_CASE :			Any												=       num_attention_heads
       __SCREAMING_SNAKE_CASE :			Optional[int]												=       intermediate_size
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       num_buckets
       __SCREAMING_SNAKE_CASE :			List[Any]												=       max_distance
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       prompt_length
       __SCREAMING_SNAKE_CASE :			List[Any]												=       prompt_types
       __SCREAMING_SNAKE_CASE :			str												=       segment_types
       __SCREAMING_SNAKE_CASE :			Tuple												=       init_std
       __SCREAMING_SNAKE_CASE :			List[str]												=       return_dict

      def 						SCREAMING_SNAKE_CASE_			(			self   :List[Any]						):
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       {}
       __SCREAMING_SNAKE_CASE :			int												=       ids_tensor([self.batch_size, self.seq_length]						,							self.vocab_size						).type(torch.intaa						)
       __SCREAMING_SNAKE_CASE :			Dict												=       False

       __SCREAMING_SNAKE_CASE :			List[str]												=       self.get_config()

       return (config, input_ids)

      def 						SCREAMING_SNAKE_CASE_			(			self   :List[str]						):
       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=_lowerCamelCase						,							init_std=self.init_std						,							return_dict=self.return_dict						,							)

      def 						SCREAMING_SNAKE_CASE_			(			self   :Dict						,							_lowerCamelCase   :Any						,							_lowerCamelCase   :List[str]						,							*_lowerCamelCase   :Tuple						):
       __SCREAMING_SNAKE_CASE :			Any												=       CpmAntModel(config=_lowerCamelCase						)
       model.to(_lowerCamelCase						)
       model.eval()

       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       model(**_lowerCamelCase						).last_hidden_state

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

      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[int]						,							_lowerCamelCase   :List[str]						,							_lowerCamelCase   :str						,							*_lowerCamelCase   :int						):
       __SCREAMING_SNAKE_CASE :			Dict												=       CpmAntForCausalLM(_lowerCamelCase						)
       model.to(_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE :			str												=       input_ids['''input_ids'''].to(_lowerCamelCase						)
       model.eval()

       __SCREAMING_SNAKE_CASE :			Dict												=       model(**_lowerCamelCase						)
       self.parent.assertEqual(
           model_output.logits.shape						,							(self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)						,							)



      def 						SCREAMING_SNAKE_CASE_			(			self   :Union[str, Any]						):
       __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       self.prepare_config_and_inputs()
       return config, inputs_dict







@require_torch
class 			snake_case		(   __UpperCAmelCase ,     __UpperCAmelCase ,     unittest.TestCase   ):
      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 						SCREAMING_SNAKE_CASE_			(			self   :Any						):
       __SCREAMING_SNAKE_CASE :			List[str]												=       CpmAntModelTester(self						)
       __SCREAMING_SNAKE_CASE :			Optional[int]												=       ConfigTester(self						,							config_class=_lowerCamelCase						)

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

      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[int]						):
       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 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						):
       __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			Optional[int]												=       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_cpmant_model(_lowerCamelCase						,							_lowerCamelCase						)



      def 						SCREAMING_SNAKE_CASE_			(			self   :int						):
       __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			int												=       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_lm_head_model(_lowerCamelCase						,							_lowerCamelCase						)







@require_torch
class 			snake_case		(   unittest.TestCase   ):

      @tooslow
      def 						SCREAMING_SNAKE_CASE_			(			self   :str						):
       __SCREAMING_SNAKE_CASE :			int												=       '''今天天气真好！'''
       __SCREAMING_SNAKE_CASE :			List[Any]												=       '''openbmb/cpm-ant-10b'''
       __SCREAMING_SNAKE_CASE :			Any												=       CpmAntModel.from_pretrained(_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE :			Tuple												=       CpmAntTokenizer.from_pretrained(_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE :			Dict												=       tokenizer(_lowerCamelCase						,							return_tensors='''pt'''						)
       __SCREAMING_SNAKE_CASE :			List[Any]												=       model(**_lowerCamelCase						).last_hidden_state

       __SCREAMING_SNAKE_CASE :			str												=       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]						,							_lowerCamelCase						,							atol=1e-2						)						)







@require_torch
class 			snake_case		(   unittest.TestCase   ):

      @tooslow
      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						):
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       '''今天天气真好！'''
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       '''openbmb/cpm-ant-10b'''
       __SCREAMING_SNAKE_CASE :			List[Any]												=       CpmAntForCausalLM.from_pretrained(_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE :			Any												=       CpmAntTokenizer.from_pretrained(_lowerCamelCase						)
       __SCREAMING_SNAKE_CASE :			List[Any]												=       tokenizer(_lowerCamelCase						,							return_tensors='''pt'''						)
       __SCREAMING_SNAKE_CASE :			Any												=       model(**_lowerCamelCase						).logits

       __SCREAMING_SNAKE_CASE :			Any												=       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]						,							_lowerCamelCase						,							atol=1e-2						)						)

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



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








"""simple docstring"""





import numpy as np




def 		lowerCAmelCase_	(  lowercase_   :							str		):

 '''simple docstring'''






 __SCREAMING_SNAKE_CASE :			Dict												=       np.exp(lowercase_		)

 # Add up the all the exponentials
 __SCREAMING_SNAKE_CASE :			Tuple												=       np.sum(lowercase_		)

 # Divide every exponent by the sum of all exponents
 __SCREAMING_SNAKE_CASE :			List[Any]												=       exponent_vector / sum_of_exponents

 return softmax_vector


if __name__ == "__main__":
    print(softmax((0,)))

