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






      def __init__(      self  ,		__lowerCamelCase  ,		__lowerCamelCase=2  ,		__lowerCamelCase=8  ,		__lowerCamelCase=True  ,		__lowerCamelCase=False  ,		__lowerCamelCase=False  ,		__lowerCamelCase=False  ,		__lowerCamelCase=False  ,		__lowerCamelCase=99  ,		__lowerCamelCase=32  ,		__lowerCamelCase=3  ,		__lowerCamelCase=4  ,		__lowerCamelCase=37  ,		__lowerCamelCase=32  ,		__lowerCamelCase=1_28  ,		__lowerCamelCase=8  ,		__lowerCamelCase=8  ,		__lowerCamelCase=8  ,		__lowerCamelCase=1.0  ,		__lowerCamelCase=True  ,		)     ->				List[str]:


          '''simple docstring'''







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






      def 			lowerCAmelCase_   (      self						)     ->				Union[str, Any]:


          '''simple docstring'''







          snake_case:						Any         =   {}
          snake_case:						int         =   ids_tensor([self.batch_size, self.seq_length]  ,		self.vocab_size						).type(torch.intaa						)
          snake_case:						int         =   False

          snake_case:						Union[str, Any]         =   self.get_config()

          return (config, input_ids)






      def 			lowerCAmelCase_   (      self						)     ->				str:


          '''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=__lowerCamelCase  ,		init_std=self.init_std  ,		return_dict=self.return_dict  ,		)






      def 			lowerCAmelCase_   (      self  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		*__lowerCamelCase						)     ->				int:


          '''simple docstring'''







          snake_case:						int         =   CpmAntModel(config=__lowerCamelCase						)
          model.to(__lowerCamelCase						)
          model.eval()

          snake_case:						Any         =   model(**__lowerCamelCase						).last_hidden_state

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






      def 			lowerCAmelCase_   (      self  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		*__lowerCamelCase						)     ->				Optional[Any]:


          '''simple docstring'''







          snake_case:						int         =   CpmAntForCausalLM(__lowerCamelCase						)
          model.to(__lowerCamelCase						)
          snake_case:						str         =   input_ids["""input_ids"""].to(__lowerCamelCase						)
          model.eval()

          snake_case:						Optional[int]         =   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 			lowerCAmelCase_   (      self						)     ->				Union[str, Any]:


          '''simple docstring'''







          snake_case    , snake_case:						Any         =   self.prepare_config_and_inputs()
          return config, inputs_dict





@require_torch
class 				lowerCamelCase			(							__snake_case				,     __snake_case				,     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 			lowerCAmelCase_   (      self						)     ->				Optional[Any]:


          '''simple docstring'''







          snake_case:						Union[str, Any]         =   CpmAntModelTester(self						)
          snake_case:						Any         =   ConfigTester(self  ,		config_class=__lowerCamelCase						)






      def 			lowerCAmelCase_   (      self						)     ->				Optional[Any]:


          '''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 			lowerCAmelCase_   (      self						)     ->				str:


          '''simple docstring'''







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






      def 			lowerCAmelCase_   (      self						)     ->				Optional[int]:


          '''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 			lowerCAmelCase_   (      self						)     ->				Tuple:


          '''simple docstring'''







          snake_case    , snake_case:						Union[str, Any]         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_cpmant_model(__lowerCamelCase  ,		__lowerCamelCase						)






      def 			lowerCAmelCase_   (      self						)     ->				List[Any]:


          '''simple docstring'''







          snake_case    , snake_case:						str         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(__lowerCamelCase  ,		__lowerCamelCase						)





@require_torch
class 				lowerCamelCase			(							unittest.TestCase					):






      @tooslow
      def 			lowerCAmelCase_   (      self						)     ->				int:


          '''simple docstring'''







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

          snake_case:						List[str]         =   torch.tensor(
              [[[6.1_708, 5.9_244, 1.0_835], [6.5_207, 6.2_893, -11.3_324], [-1.0_107, -0.0_576, -5.9_577]]]  ,		)
          self.assertTrue(torch.allclose(hidden_states[:, :3, :3]  ,		__lowerCamelCase  ,		atol=1e-2						)						)





@require_torch
class 				lowerCamelCase			(							unittest.TestCase					):






      @tooslow
      def 			lowerCAmelCase_   (      self						)     ->				Optional[Any]:


          '''simple docstring'''







          snake_case:						Tuple         =   """今天天气真好！"""
          snake_case:						Any         =   """openbmb/cpm-ant-10b"""
          snake_case:						Tuple         =   CpmAntForCausalLM.from_pretrained(__lowerCamelCase						)
          snake_case:						int         =   CpmAntTokenizer.from_pretrained(__lowerCamelCase						)
          snake_case:						Union[str, Any]         =   tokenizer(__lowerCamelCase  ,		return_tensors="""pt"""						)
          snake_case:						str         =   model(**__lowerCamelCase						).logits

          snake_case:						int         =   torch.tensor(
              [[[-6.4_267, -6.4_083, -6.3_958], [-5.8_802, -5.9_447, -5.7_811], [-5.3_896, -5.4_820, -5.4_295]]]  ,		)
          self.assertTrue(torch.allclose(hidden_states[:, :3, :3]  ,		__lowerCamelCase  ,		atol=1e-2						)						)






      @tooslow
      def 			lowerCAmelCase_   (      self						)     ->				Any:


          '''simple docstring'''







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






      @tooslow
      def 			lowerCAmelCase_   (      self						)     ->				Optional[Any]:


          '''simple docstring'''







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



import warnings

from ...utils import logging
from .image_processing_conditional_detr import ConditionalDetrImageProcessor


__lowerCAmelCase    :  Tuple										= logging.get_logger(__name__)
class 				lowerCamelCase			(							__snake_case					):






      def __init__(      self  ,		*__lowerCamelCase  ,		**__lowerCamelCase						)     ->				None:


          '''simple docstring'''







          warnings.warn(
              """The class ConditionalDetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers."""
              """ Please use ConditionalDetrImageProcessor instead."""  ,		__lowerCamelCase  ,		)
          super().__init__(*__lowerCamelCase  ,		**__lowerCamelCase						)


