



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				:






   '''simple docstring'''






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


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

       UpperCAmelCase_     :       Dict  		=					self.get_config()

       return (config, input_ids)


   def       A__    (    self:     int							)	->      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       A__    (    self:     Union[str, Any]  ,lowerCamelCase_:     Dict  ,lowerCamelCase_:     Any  ,*lowerCamelCase_:     Optional[int]							)	->      List[str]:
       UpperCAmelCase_     :       Optional[Any]  		=					CpmAntModel(config=lowerCamelCase_							)
       model.to(lowerCamelCase_							)
       model.eval()

       UpperCAmelCase_     :       int  		=					model(**lowerCamelCase_							).last_hidden_state

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


   def       A__    (    self:     Optional[Any]  ,lowerCamelCase_:     List[Any]  ,lowerCamelCase_:     Optional[int]  ,*lowerCamelCase_:     List[Any]							)	->      str:
       UpperCAmelCase_     :       str  		=					CpmAntForCausalLM(lowerCamelCase_							)
       model.to(lowerCamelCase_							)
       UpperCAmelCase_     :       List[Any]  		=					input_ids["""input_ids"""].to(lowerCamelCase_							)
       model.eval()

       UpperCAmelCase_     :       str  		=					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       A__    (    self:     Dict							)	->      Union[str, Any]:
       UpperCAmelCase_		, UpperCAmelCase_     :       int  		=					self.prepare_config_and_inputs()
       return config, inputs_dict







@require_torch
class       _snake_case				(     __snake_case ,							__snake_case ,							unittest.TestCase ):






   '''simple docstring'''






   A__      : List[Any]       =						(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
   A__      : List[str]       =						(
       {"feature-extraction": CpmAntModel, "text-generation": CpmAntForCausalLM} if is_torch_available() else {}
   )

   A__      : int       =						False
   A__      : Any       =						False
   A__      : Tuple       =						False
   A__      : Tuple       =						False
   A__      : List[Any]       =						False


   def       A__    (    self:     int							)	->      Union[str, Any]:
       UpperCAmelCase_     :       Union[str, Any]  		=					CpmAntModelTester(self							)
       UpperCAmelCase_     :       List[str]  		=					ConfigTester(self  ,config_class=lowerCamelCase_							)


   def       A__    (    self:     Dict							)	->      Dict:
       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:     str							)	->      int:
       unittest.skip("""CPMAnt doesn't support input_embeds."""							)(self.test_inputs_embeds							)


   def       A__    (    self:     Optional[int]							)	->      List[str]:
       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:     Union[str, Any]							)	->      List[Any]:
       UpperCAmelCase_		, UpperCAmelCase_     :       List[str]  		=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_cpmant_model(lowerCamelCase_  ,lowerCamelCase_							)






   def       A__    (    self:     int							)	->      int:
       UpperCAmelCase_		, UpperCAmelCase_     :       Optional[Any]  		=					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 ):






   '''simple docstring'''






   @tooslow
   def       A__    (    self:     List[str]							)	->      Tuple:
       UpperCAmelCase_     :       str  		=					"""今天天气真好！"""
       UpperCAmelCase_     :       Dict  		=					"""openbmb/cpm-ant-10b"""
       UpperCAmelCase_     :       Dict  		=					CpmAntModel.from_pretrained(lowerCamelCase_							)
       UpperCAmelCase_     :       int  		=					CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
       UpperCAmelCase_     :       str  		=					tokenizer(lowerCamelCase_  ,return_tensors="""pt"""							)
       UpperCAmelCase_     :       Tuple  		=					model(**lowerCamelCase_							).last_hidden_state

       UpperCAmelCase_     :       List[Any]  		=					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 ):






   '''simple docstring'''






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

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






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





from ...configuration_utils import PretrainedConfig
from ...utils import is_detectrona_available, logging


UpperCamelCase_									=   logging.get_logger(__name__)

UpperCamelCase_									=   {
    '''layoutlmv2-base-uncased''': '''https://huggingface.co/microsoft/layoutlmv2-base-uncased/resolve/main/config.json''',
    '''layoutlmv2-large-uncased''': '''https://huggingface.co/microsoft/layoutlmv2-large-uncased/resolve/main/config.json''',
    # See all LayoutLMv2 models at https://huggingface.co/models?filter=layoutlmv2
}

# soft dependency
if is_detectrona_available():
      import detectrona







class       _snake_case				(     __snake_case ):






   '''simple docstring'''






   A__      : List[Any]       =						"layoutlmv2"


   def __init__(    self:     Tuple  ,lowerCamelCase_:     Dict=30522  ,lowerCamelCase_:     str=768  ,lowerCamelCase_:     Any=12  ,lowerCamelCase_:     Tuple=12  ,lowerCamelCase_:     int=3072  ,lowerCamelCase_:     int="gelu"  ,lowerCamelCase_:     Dict=0.1  ,lowerCamelCase_:     List[Any]=0.1  ,lowerCamelCase_:     List[Any]=512  ,lowerCamelCase_:     Tuple=2  ,lowerCamelCase_:     Dict=0.0_2  ,lowerCamelCase_:     Dict=1e-12  ,lowerCamelCase_:     Dict=0  ,lowerCamelCase_:     List[str]=1024  ,lowerCamelCase_:     Optional[Any]=128  ,lowerCamelCase_:     List[Any]=32  ,lowerCamelCase_:     List[Any]=True  ,lowerCamelCase_:     Dict=256  ,lowerCamelCase_:     int=64  ,lowerCamelCase_:     str=True  ,lowerCamelCase_:     Tuple=[7, 7, 256]  ,lowerCamelCase_:     Union[str, Any]=128  ,lowerCamelCase_:     Any=128  ,lowerCamelCase_:     int=True  ,lowerCamelCase_:     Union[str, Any]=True  ,lowerCamelCase_:     Optional[int]=False  ,lowerCamelCase_:     List[str]=None  ,**lowerCamelCase_:     str  ,)	->      Any:
       super().__init__(
           vocab_size=lowerCamelCase_  ,hidden_size=lowerCamelCase_  ,num_hidden_layers=lowerCamelCase_  ,num_attention_heads=lowerCamelCase_  ,intermediate_size=lowerCamelCase_  ,hidden_act=lowerCamelCase_  ,hidden_dropout_prob=lowerCamelCase_  ,attention_probs_dropout_prob=lowerCamelCase_  ,max_position_embeddings=lowerCamelCase_  ,type_vocab_size=lowerCamelCase_  ,initializer_range=lowerCamelCase_  ,layer_norm_eps=lowerCamelCase_  ,pad_token_id=lowerCamelCase_  ,**lowerCamelCase_  ,)
       UpperCAmelCase_     :       Optional[int]  		=					max_ad_position_embeddings
       UpperCAmelCase_     :       Optional[int]  		=					max_rel_pos
       UpperCAmelCase_     :       Optional[int]  		=					rel_pos_bins
       UpperCAmelCase_     :       List[str]  		=					fast_qkv
       UpperCAmelCase_     :       int  		=					max_rel_ad_pos
       UpperCAmelCase_     :       int  		=					rel_ad_pos_bins
       UpperCAmelCase_     :       Optional[Any]  		=					convert_sync_batchnorm
       UpperCAmelCase_     :       str  		=					image_feature_pool_shape
       UpperCAmelCase_     :       List[Any]  		=					coordinate_size
       UpperCAmelCase_     :       int  		=					shape_size
       UpperCAmelCase_     :       Optional[Any]  		=					has_relative_attention_bias
       UpperCAmelCase_     :       str  		=					has_spatial_attention_bias
       UpperCAmelCase_     :       Tuple  		=					has_visual_segment_embedding
       UpperCAmelCase_     :       Optional[Any]  		=					(
           detectrona_config_args if detectrona_config_args is not None else self.get_default_detectrona_config()
       )


   @classmethod
   def       A__    (    self:     Any							)	->      Dict:
       return {
           "MODEL.MASK_ON": True,
           "MODEL.PIXEL_STD": [5_7.3_7_5, 5_7.1_2_0, 5_8.3_9_5],
           "MODEL.BACKBONE.NAME": "build_resnet_fpn_backbone",
           "MODEL.FPN.IN_FEATURES": ["res2", "res3", "res4", "res5"],
           "MODEL.ANCHOR_GENERATOR.SIZES": [[32], [64], [128], [256], [512]],
           "MODEL.RPN.IN_FEATURES": ["p2", "p3", "p4", "p5", "p6"],
           "MODEL.RPN.PRE_NMS_TOPK_TRAIN": 2000,
           "MODEL.RPN.PRE_NMS_TOPK_TEST": 1000,
           "MODEL.RPN.POST_NMS_TOPK_TRAIN": 1000,
           "MODEL.POST_NMS_TOPK_TEST": 1000,
           "MODEL.ROI_HEADS.NAME": "StandardROIHeads",
           "MODEL.ROI_HEADS.NUM_CLASSES": 5,
           "MODEL.ROI_HEADS.IN_FEATURES": ["p2", "p3", "p4", "p5"],
           "MODEL.ROI_BOX_HEAD.NAME": "FastRCNNConvFCHead",
           "MODEL.ROI_BOX_HEAD.NUM_FC": 2,
           "MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION": 14,
           "MODEL.ROI_MASK_HEAD.NAME": "MaskRCNNConvUpsampleHead",
           "MODEL.ROI_MASK_HEAD.NUM_CONV": 4,
           "MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION": 7,
           "MODEL.RESNETS.DEPTH": 101,
           "MODEL.RESNETS.SIZES": [[32], [64], [128], [256], [512]],
           "MODEL.RESNETS.ASPECT_RATIOS": [[0.5, 1.0, 2.0]],
           "MODEL.RESNETS.OUT_FEATURES": ["res2", "res3", "res4", "res5"],
           "MODEL.RESNETS.NUM_GROUPS": 32,
           "MODEL.RESNETS.WIDTH_PER_GROUP": 8,
           "MODEL.RESNETS.STRIDE_IN_1X1": False,
       }






   def       A__    (    self:     Tuple							)	->      int:
       UpperCAmelCase_     :       Any  		=					detectrona.config.get_cfg()
       for k, v in self.detectrona_config_args.items():
           UpperCAmelCase_     :       Optional[int]  		=					k.split("""."""							)
           UpperCAmelCase_     :       List[str]  		=					detectrona_config
           for attribute in attributes[:-1]:
               UpperCAmelCase_     :       Optional[Any]  		=					getattr(lowerCamelCase_  ,lowerCamelCase_							)
           setattr(lowerCamelCase_  ,attributes[-1]  ,lowerCamelCase_							)

       return detectrona_config
