




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   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE=2   ,_SCREAMING_SNAKE_CASE=8   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=False   ,_SCREAMING_SNAKE_CASE=False   ,_SCREAMING_SNAKE_CASE=False   ,_SCREAMING_SNAKE_CASE=False   ,_SCREAMING_SNAKE_CASE=99   ,_SCREAMING_SNAKE_CASE=32   ,_SCREAMING_SNAKE_CASE=3   ,_SCREAMING_SNAKE_CASE=4   ,_SCREAMING_SNAKE_CASE=37   ,_SCREAMING_SNAKE_CASE=32   ,_SCREAMING_SNAKE_CASE=128   ,_SCREAMING_SNAKE_CASE=8   ,_SCREAMING_SNAKE_CASE=8   ,_SCREAMING_SNAKE_CASE=8   ,_SCREAMING_SNAKE_CASE=1.0   ,_SCREAMING_SNAKE_CASE=True   ,) ->		Any:
         UpperCAmelCase_			:							Union[str, Any]   =			parent
         UpperCAmelCase_			:							Optional[Any]   =			batch_size
         UpperCAmelCase_			:							List[str]   =			seq_length
         UpperCAmelCase_			:							List[str]   =			is_training
         UpperCAmelCase_			:							Optional[Any]   =			use_token_type_ids
         UpperCAmelCase_			:							Union[str, Any]   =			use_input_mask
         UpperCAmelCase_			:							Union[str, Any]   =			use_labels
         UpperCAmelCase_			:							Dict   =			use_mc_token_ids
         UpperCAmelCase_			:							str   =			vocab_size
         UpperCAmelCase_			:							Optional[int]   =			hidden_size
         UpperCAmelCase_			:							str   =			num_hidden_layers
         UpperCAmelCase_			:							List[str]   =			num_attention_heads
         UpperCAmelCase_			:							List[str]   =			intermediate_size
         UpperCAmelCase_			:							int   =			num_buckets
         UpperCAmelCase_			:							Optional[int]   =			max_distance
         UpperCAmelCase_			:							str   =			prompt_length
         UpperCAmelCase_			:							Tuple   =			prompt_types
         UpperCAmelCase_			:							List[Any]   =			segment_types
         UpperCAmelCase_			:							Any   =			init_std
         UpperCAmelCase_			:							List[str]   =			return_dict





    def 							a__      (      self  ) ->		Union[str, Any]:
         UpperCAmelCase_			:							List[Any]   =			{}
         UpperCAmelCase_			:							Dict   =			ids_tensor([self.batch_size, self.seq_length]   ,self.vocab_size  ).type(torch.intaa  )
         UpperCAmelCase_			:							Dict   =			False

         UpperCAmelCase_			:							str   =			self.get_config()

         return (config, input_ids)





    def 							a__      (      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 							a__      (      self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,*_SCREAMING_SNAKE_CASE  ) ->		Tuple:
         UpperCAmelCase_			:							Optional[int]   =			CpmAntModel(config=__UpperCamelCase  )
         model.to(__UpperCamelCase  )
         model.eval()

         UpperCAmelCase_			:							Union[str, Any]   =			model(**__UpperCamelCase  ).last_hidden_state

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





    def 							a__      (      self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,*_SCREAMING_SNAKE_CASE  ) ->		Dict:
         UpperCAmelCase_			:							Optional[Any]   =			CpmAntForCausalLM(__UpperCamelCase  )
         model.to(__UpperCamelCase  )
         UpperCAmelCase_			:							Optional[Any]   =			input_ids['''input_ids'''].to(__UpperCamelCase  )
         model.eval()

         UpperCAmelCase_			:							List[str]   =			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 							a__      (      self  ) ->		List[str]:
         UpperCAmelCase_,    UpperCAmelCase_			:							List[str]   =			self.prepare_config_and_inputs()
         return config, inputs_dict


@require_torch
class   __a(     UpperCAmelCase_				,						UpperCAmelCase_				,						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 							a__      (      self  ) ->		Optional[int]:
         UpperCAmelCase_			:							str   =			CpmAntModelTester(self  )
         UpperCAmelCase_			:							List[Any]   =			ConfigTester(self   ,config_class=__UpperCamelCase  )





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





    def 							a__      (      self  ) ->		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 							a__      (      self  ) ->		Tuple:
         UpperCAmelCase_,    UpperCAmelCase_			:							List[str]   =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_cpmant_model(__UpperCamelCase   ,__UpperCamelCase  )





    def 							a__      (      self  ) ->		List[str]:
         UpperCAmelCase_,    UpperCAmelCase_			:							str   =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_lm_head_model(__UpperCamelCase   ,__UpperCamelCase  )


@require_torch
class   __a(     unittest.TestCase   ):






    """simple docstring"""





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

         UpperCAmelCase_			:							Optional[int]   =			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]   ,__UpperCamelCase   ,atol=1e-2  )  )


@require_torch
class   __a(     unittest.TestCase   ):






    """simple docstring"""





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

         UpperCAmelCase_			:							Tuple   =			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]   ,__UpperCamelCase   ,atol=1e-2  )  )





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





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





'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_lowerCamelCase							:						Optional[Any]								=  {'configuration_bit': ['BIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BitConfig', 'BitOnnxConfig']}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowerCamelCase							:						Dict								=  [
						    'BIT_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'BitForImageClassification',
						    'BitModel',
						    'BitPreTrainedModel',
						    'BitBackbone',
						]


try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowerCamelCase							:						Union[str, Any]								=  ['BitImageProcessor']


if TYPE_CHECKING:
						from .configuration_bit import BIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BitConfig, BitOnnxConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_bit import (
												    BIT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    BitBackbone,
												    BitForImageClassification,
												    BitModel,
												    BitPreTrainedModel,
												)

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .image_processing_bit import BitImageProcessor

else:
						import sys

						_lowerCamelCase							:						Optional[Any]								=  _LazyModule(__name__, globals()['__file__'], _import_structure)





