






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      __SCREAMING_SNAKE_CASE       :






						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  ,    ):






												"""simple docstring"""
												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    UpperCamelCase__   (						self ):






												"""simple docstring"""
												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    UpperCamelCase__   (						self ):






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






						def    UpperCamelCase__   (						self  ,    _UpperCamelCase  ,    _UpperCamelCase  ,    *_UpperCamelCase ):






												"""simple docstring"""
												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    UpperCamelCase__   (						self  ,    _UpperCamelCase  ,    _UpperCamelCase  ,    *_UpperCamelCase ):






												"""simple docstring"""
												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    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    self.prepare_config_and_inputs()
												return config, inputs_dict




@require_torch
class      __SCREAMING_SNAKE_CASE       ( _a		,       _a		,       unittest.TestCase):
						_SCREAMING_SNAKE_CASE						:							Dict				=       (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
						_SCREAMING_SNAKE_CASE						:							int				=       (
						    {"feature-extraction": CpmAntModel, "text-generation": CpmAntForCausalLM} if is_torch_available() else {}
						)

						_SCREAMING_SNAKE_CASE						:							List[Any]				=       False
						_SCREAMING_SNAKE_CASE						:							Optional[int]				=       False
						_SCREAMING_SNAKE_CASE						:							Tuple				=       False
						_SCREAMING_SNAKE_CASE						:							List[str]				=       False
						_SCREAMING_SNAKE_CASE						:							List[str]				=       False






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    CpmAntModelTester(self )
												lowerCAmelCase__         =    ConfigTester(self  ,    config_class=_UpperCamelCase )






						def    UpperCamelCase__   (						self ):






												"""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    UpperCamelCase__   (						self ):






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






						def    UpperCamelCase__   (						self ):






												"""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.\\n                 So is attentions. We strongly recommand you use loss to tune model.' )(self.test_retain_grad_hidden_states_attentions )






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_cpmant_model(_UpperCamelCase  ,    _UpperCamelCase )







						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_lm_head_model(_UpperCamelCase  ,    _UpperCamelCase )




@require_torch
class      __SCREAMING_SNAKE_CASE       ( unittest.TestCase):






						@tooslow
						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												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.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      __SCREAMING_SNAKE_CASE       ( unittest.TestCase):






						@tooslow
						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												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.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    UpperCamelCase__   (						self ):






												"""simple docstring"""
												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    UpperCamelCase__   (						self ):






												"""simple docstring"""
												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'''



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCAmelCase     :Union[str, Any]												=  {
    "configuration_mpnet": ["MPNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "MPNetConfig"],
    "tokenization_mpnet": ["MPNetTokenizer"],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase     :Optional[int]												=  ["MPNetTokenizerFast"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase     :Union[str, Any]												=  [
          "MPNET_PRETRAINED_MODEL_ARCHIVE_LIST",
          "MPNetForMaskedLM",
          "MPNetForMultipleChoice",
          "MPNetForQuestionAnswering",
          "MPNetForSequenceClassification",
          "MPNetForTokenClassification",
          "MPNetLayer",
          "MPNetModel",
          "MPNetPreTrainedModel",
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase     :Any												=  [
          "TF_MPNET_PRETRAINED_MODEL_ARCHIVE_LIST",
          "TFMPNetEmbeddings",
          "TFMPNetForMaskedLM",
          "TFMPNetForMultipleChoice",
          "TFMPNetForQuestionAnswering",
          "TFMPNetForSequenceClassification",
          "TFMPNetForTokenClassification",
          "TFMPNetMainLayer",
          "TFMPNetModel",
          "TFMPNetPreTrainedModel",
      ]


if TYPE_CHECKING:
      from .configuration_mpnet import MPNET_PRETRAINED_CONFIG_ARCHIVE_MAP, MPNetConfig
      from .tokenization_mpnet import MPNetTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_mpnet_fast import MPNetTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_mpnet import (
                MPNET_PRETRAINED_MODEL_ARCHIVE_LIST,
                MPNetForMaskedLM,
                MPNetForMultipleChoice,
                MPNetForQuestionAnswering,
                MPNetForSequenceClassification,
                MPNetForTokenClassification,
                MPNetLayer,
                MPNetModel,
                MPNetPreTrainedModel,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_mpnet import (
                TF_MPNET_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFMPNetEmbeddings,
                TFMPNetForMaskedLM,
                TFMPNetForMultipleChoice,
                TFMPNetForQuestionAnswering,
                TFMPNetForSequenceClassification,
                TFMPNetForTokenClassification,
                TFMPNetMainLayer,
                TFMPNetModel,
                TFMPNetPreTrainedModel,
            )

else:
      import sys

      __UpperCAmelCase     :Optional[Any]												=  _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)