




'''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 	SCREAMING_SNAKE_CASE_		:



 def __init__(	self ,		A ,		A=2 ,		A=8 ,		A=True ,		A=False ,		A=False ,		A=False ,		A=False ,		A=99 ,		A=32 ,		A=3 ,		A=4 ,		A=37 ,		A=32 ,		A=128 ,		A=8 ,		A=8 ,		A=8 ,		A=1.0 ,		A=True ,		):






        '''simple docstring'''




        lowercase	:		str    =   parent
        lowercase	:		Optional[int]    =   batch_size
        lowercase	:		List[Any]    =   seq_length
        lowercase	:		Any    =   is_training
        lowercase	:		Optional[int]    =   use_token_type_ids
        lowercase	:		Optional[int]    =   use_input_mask
        lowercase	:		Any    =   use_labels
        lowercase	:		List[Any]    =   use_mc_token_ids
        lowercase	:		List[Any]    =   vocab_size
        lowercase	:		Optional[int]    =   hidden_size
        lowercase	:		int    =   num_hidden_layers
        lowercase	:		Tuple    =   num_attention_heads
        lowercase	:		Optional[Any]    =   intermediate_size
        lowercase	:		Any    =   num_buckets
        lowercase	:		Any    =   max_distance
        lowercase	:		Union[str, Any]    =   prompt_length
        lowercase	:		Dict    =   prompt_types
        lowercase	:		List[str]    =   segment_types
        lowercase	:		str    =   init_std
        lowercase	:		List[Any]    =   return_dict



 def  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




        lowercase	:		Optional[Any]    =   {}
        lowercase	:		str    =   ids_tensor([self.batch_size, self.seq_length] ,		self.vocab_size			).type(torch.intaa			)
        lowercase	:		List[str]    =   False

        lowercase	:		Any    =   self.get_config()

        return (config, input_ids)



 def  SCREAMING_SNAKE_CASE(	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=A ,		init_std=self.init_std ,		return_dict=self.return_dict ,		)



 def  SCREAMING_SNAKE_CASE(	self ,		A ,		A ,		*A			):






        '''simple docstring'''




        lowercase	:		Optional[int]    =   CpmAntModel(config=A			)
        model.to(A			)
        model.eval()

        lowercase	:		str    =   model(**A			).last_hidden_state

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



 def  SCREAMING_SNAKE_CASE(	self ,		A ,		A ,		*A			):






        '''simple docstring'''




        lowercase	:		str    =   CpmAntForCausalLM(A			)
        model.to(A			)
        lowercase	:		int    =   input_ids['''input_ids'''].to(A			)
        model.eval()

        lowercase	:		Optional[Any]    =   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  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




        lowercase,  lowercase	:		List[Any]    =   self.prepare_config_and_inputs()
        return config, inputs_dict






@require_torch
class 	SCREAMING_SNAKE_CASE_		(							lowerCamelCase_		,  lowerCamelCase_		,  unittest.TestCase					):
 __snake_case						:  List[Any]       =   (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
 __snake_case						:  Optional[int]       =   (
     {"feature-extraction": CpmAntModel, "text-generation": CpmAntForCausalLM} if is_torch_available() else {}
 )

 __snake_case						:  Tuple       =   False
 __snake_case						:  Tuple       =   False
 __snake_case						:  Optional[int]       =   False
 __snake_case						:  str       =   False
 __snake_case						:  Union[str, Any]       =   False



 def  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




        lowercase	:		List[Any]    =   CpmAntModelTester(self			)
        lowercase	:		Optional[Any]    =   ConfigTester(self ,		config_class=A			)



 def  SCREAMING_SNAKE_CASE(	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  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




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



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



 def  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




        lowercase,  lowercase	:		Any    =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_cpmant_model(A ,		A			)





 def  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




        lowercase,  lowercase	:		List[str]    =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(A ,		A			)






@require_torch
class 	SCREAMING_SNAKE_CASE_		(							unittest.TestCase					):



 @tooslow
 def  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




        lowercase	:		Union[str, Any]    =   '''今天天气真好！'''
        lowercase	:		Tuple    =   '''openbmb/cpm-ant-10b'''
        lowercase	:		List[Any]    =   CpmAntModel.from_pretrained(A			)
        lowercase	:		int    =   CpmAntTokenizer.from_pretrained(A			)
        lowercase	:		List[Any]    =   tokenizer(A ,		return_tensors='''pt'''			)
        lowercase	:		Dict    =   model(**A			).last_hidden_state

        lowercase	:		Optional[int]    =   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] ,		A ,		atol=1E-2			)			)






@require_torch
class 	SCREAMING_SNAKE_CASE_		(							unittest.TestCase					):



 @tooslow
 def  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




        lowercase	:		Any    =   '''今天天气真好！'''
        lowercase	:		List[str]    =   '''openbmb/cpm-ant-10b'''
        lowercase	:		str    =   CpmAntForCausalLM.from_pretrained(A			)
        lowercase	:		str    =   CpmAntTokenizer.from_pretrained(A			)
        lowercase	:		Tuple    =   tokenizer(A ,		return_tensors='''pt'''			)
        lowercase	:		Any    =   model(**A			).logits

        lowercase	:		Union[str, 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] ,		A ,		atol=1E-2			)			)



 @tooslow
 def  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




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





 @tooslow
 def  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




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










'''simple docstring'''






import os

from packaging import version

from .. import __version__
from .accelerate_utils import apply_forward_hook
from .constants import (
    CONFIG_NAME,
    DEPRECATED_REVISION_ARGS,
    DIFFUSERS_CACHE,
    DIFFUSERS_DYNAMIC_MODULE_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    ONNX_EXTERNAL_WEIGHTS_NAME,
    ONNX_WEIGHTS_NAME,
    SAFETENSORS_WEIGHTS_NAME,
    TEXT_ENCODER_ATTN_MODULE,
    WEIGHTS_NAME,
)
from .deprecation_utils import deprecate
from .doc_utils import replace_example_docstring
from .dynamic_modules_utils import get_class_from_dynamic_module
from .hub_utils import (
    HF_HUB_OFFLINE,
    _add_variant,
    _get_model_file,
    extract_commit_hash,
    http_user_agent,
)
from .import_utils import (
    BACKENDS_MAPPING,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    DummyObject,
    OptionalDependencyNotAvailable,
    is_accelerate_available,
    is_accelerate_version,
    is_bsa_available,
    is_flax_available,
    is_ftfy_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_omegaconf_available,
    is_onnx_available,
    is_safetensors_available,
    is_scipy_available,
    is_tensorboard_available,
    is_tf_available,
    is_torch_available,
    is_torch_version,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    is_wandb_available,
    is_xformers_available,
    requires_backends,
)
from .logging import get_logger
from .outputs import BaseOutput
from .pil_utils import PIL_INTERPOLATION, numpy_to_pil, pt_to_pil
from .torch_utils import is_compiled_module, randn_tensor


if is_torch_available():
      from .testing_utils import (
          floats_tensor,
          load_hf_numpy,
          load_image,
          load_numpy,
          load_pt,
          nightly,
          parse_flag_from_env,
          print_tensor_test,
          require_torch_a,
          require_torch_gpu,
          skip_mps,
          slow,
          torch_all_close,
          torch_device,
      )
      from .torch_utils import maybe_allow_in_graph

from .testing_utils import export_to_gif, export_to_video


lowercase						:   Tuple      						=							get_logger(__name__)




def 		a_(			lowerCamelCase   )					->Any:






       '''simple docstring'''




       if version.parse(lowerCamelCase   ) < version.parse(lowerCamelCase   ):
              if "dev" in min_version:
                     lowercase	:		str    =   (
                         '''This example requires a source install from HuggingFace diffusers (see '''
                         '''`https://huggingface.co/docs/diffusers/installation#install-from-source`),'''
                     )
              else:
                     lowercase	:		List[str]    =   f"""This example requires a minimum version of {min_version},"""
              error_message += f""" but the version found is {__version__}.\n"""
              raise ImportError(lowerCamelCase   )




