


import os
import tempfile
import unittest

import numpy as np
import torch

from diffusers.models import ModelMixin, UNetaDConditionModel
from diffusers.models.attention_processor import AttnProcessor, LoRAAttnProcessor
from diffusers.utils import (
    floats_tensor,
    logging,
    skip_mps,
    torch_device,
)
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()

__UpperCAmelCase      			=  logging.get_logger(__name__)
def 	_lowerCamelCase  (		A_  :	str							,  A_  :	bool = True      )			->			Optional[int]:

   '''simple docstring'''






   UpperCamelCase__ :			str 						={}
   for name in model.attn_processors.keys():
      UpperCamelCase__ :			Union[str, Any] 						=name.endswith("attn2.processor"      ) and not (
          name.startswith("transformer_in"      ) or "temp_attentions" in name.split("."      )
      )
      UpperCamelCase__ :			int 						=model.config.cross_attention_dim if has_cross_attention else None
      if name.startswith("mid_block"      ):
         UpperCamelCase__ :			Optional[Any] 						=model.config.block_out_channels[-1]
      elif name.startswith("up_blocks"      ):
         UpperCamelCase__ :			Any 						=int(name[len("up_blocks."      )]      )
         UpperCamelCase__ :			Optional[int] 						=list(reversed(model.config.block_out_channels      )      )[block_id]
      elif name.startswith("down_blocks"      ):
         UpperCamelCase__ :			List[str] 						=int(name[len("down_blocks."      )]      )
         UpperCamelCase__ :			Any 						=model.config.block_out_channels[block_id]
      elif name.startswith("transformer_in"      ):
         # Note that the `8 * ...` comes from: https://github.com/huggingface/diffusers/blob/7139f0e874f10b2463caa8cbd585762a309d12d6/src/diffusers/models/unet_3d_condition.py#L148
         UpperCamelCase__ :			Optional[Any] 						=8 * model.config.attention_head_dim

      UpperCamelCase__ :			Any 						=LoRAAttnProcessor(hidden_size=A_							,  cross_attention_dim=A_      )
      UpperCamelCase__ :			int 						=lora_attn_procs[name].to(model.device      )

      if mock_weights:
         # add 1 to weights to mock trained weights
         with torch.no_grad():
            lora_attn_procs[name].to_q_lora.up.weight += 1
            lora_attn_procs[name].to_k_lora.up.weight += 1
            lora_attn_procs[name].to_v_lora.up.weight += 1
            lora_attn_procs[name].to_out_lora.up.weight += 1

   return lora_attn_procs



@skip_mps
class 							lowercase__(						snake_case__			,							snake_case__			,							unittest.TestCase			):






      '''simple docstring'''







      snake_case__       =					UNetaDConditionModel
      snake_case__       =					'''sample'''




      @property
      def    UpperCAmelCase							(		self)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=4
         UpperCamelCase__ :			Dict 						=4
         UpperCamelCase__ :			int 						=4
         UpperCamelCase__ :			Optional[Any] 						=(32, 32)

         UpperCamelCase__ :			List[str] 						=floats_tensor((batch_size, num_channels, num_frames) + sizes).to(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Optional[Any] 						=torch.tensor([10]).to(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Tuple 						=floats_tensor((batch_size, 4, 32)).to(__SCREAMING_SNAKE_CASE)

         return {"sample": noise, "timestep": time_step, "encoder_hidden_states": encoder_hidden_states}




      @property
      def    UpperCAmelCase							(		self)					->						Union[str, Any]:







         """simple docstring"""







         return (4, 4, 32, 32)




      @property
      def    UpperCAmelCase							(		self)					->						Union[str, Any]:







         """simple docstring"""







         return (4, 4, 32, 32)




      def    UpperCAmelCase							(		self)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						={
             "block_out_channels": (32, 64),
             "down_block_types": (
                 "CrossAttnDownBlock3D",
                 "DownBlock3D",
             ),
             "up_block_types": ("UpBlock3D", "CrossAttnUpBlock3D"),
             "cross_attention_dim": 32,
             "attention_head_dim": 8,
             "out_channels": 4,
             "in_channels": 4,
             "layers_per_block": 1,
             "sample_size": 32,
         }
         UpperCamelCase__ :			List[Any] 						=self.dummy_input
         return init_dict, inputs_dict




      @unittest.skipIf(
          torch_device != "cuda" or not is_xformers_available()   ,			reason="XFormers attention is only available with CUDA and `xformers` installed"   ,			)
      def    UpperCAmelCase							(		self)					->						Dict:







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			int 						=self.prepare_init_args_and_inputs_for_common()
         UpperCamelCase__ :			Union[str, Any] 						=self.model_class(**__SCREAMING_SNAKE_CASE)

         model.enable_xformers_memory_efficient_attention()

         assert (
             model.mid_block.attentions[0].transformer_blocks[0].attna.processor.__class__.__name__
             == "XFormersAttnProcessor"
         ), "xformers is not enabled"




      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			List[str] 						=self.prepare_init_args_and_inputs_for_common()

         UpperCamelCase__ :			Optional[int] 						=32

         UpperCamelCase__ :			List[Any] 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()

         with torch.no_grad():
            UpperCamelCase__ :			str 						=model(**__SCREAMING_SNAKE_CASE)

            if isinstance(__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE):
               UpperCamelCase__ :			str 						=output.sample

         self.assertIsNotNone(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Tuple 						=inputs_dict["sample"].shape
         self.assertEqual(output.shape   ,			__SCREAMING_SNAKE_CASE   ,			"Input and output shapes do not match")




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







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			Union[str, Any] 						=self.prepare_init_args_and_inputs_for_common()
         UpperCamelCase__ :			Union[str, Any] 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()

         with torch.no_grad():
            # Warmup pass when using mps (see #372)
            if torch_device == "mps" and isinstance(__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE):
               model(**self.dummy_input)

            UpperCamelCase__ :			Dict 						=model(**__SCREAMING_SNAKE_CASE)
            if isinstance(__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE):
               UpperCamelCase__ :			Union[str, Any] 						=first.sample

            UpperCamelCase__ :			List[Any] 						=model(**__SCREAMING_SNAKE_CASE)
            if isinstance(__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE):
               UpperCamelCase__ :			str 						=second.sample

         UpperCamelCase__ :			List[str] 						=first.cpu().numpy()
         UpperCamelCase__ :			Union[str, Any] 						=second.cpu().numpy()
         UpperCamelCase__ :			Union[str, Any] 						=out_a[~np.isnan(__SCREAMING_SNAKE_CASE)]
         UpperCamelCase__ :			Optional[Any] 						=out_a[~np.isnan(__SCREAMING_SNAKE_CASE)]
         UpperCamelCase__ :			Union[str, Any] 						=np.amax(np.abs(out_a - out_a))
         self.assertLessEqual(__SCREAMING_SNAKE_CASE   ,			1E-5)




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







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			Optional[Any] 						=self.prepare_init_args_and_inputs_for_common()

         UpperCamelCase__ :			Dict 						=8

         UpperCamelCase__ :			Optional[Any] 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()

         model.set_attention_slice("auto")
         with torch.no_grad():
            UpperCamelCase__ :			Dict 						=model(**__SCREAMING_SNAKE_CASE)
         assert output is not None

         model.set_attention_slice("max")
         with torch.no_grad():
            UpperCamelCase__ :			Union[str, Any] 						=model(**__SCREAMING_SNAKE_CASE)
         assert output is not None

         model.set_attention_slice(2)
         with torch.no_grad():
            UpperCamelCase__ :			List[Any] 						=model(**__SCREAMING_SNAKE_CASE)
         assert output is not None




      def    UpperCAmelCase							(		self)					->						Tuple:







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			List[Any] 						=self.prepare_init_args_and_inputs_for_common()

         UpperCamelCase__ :			List[Any] 						=8

         UpperCamelCase__ :			Any 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)

         with torch.no_grad():
            UpperCamelCase__ :			Optional[int] 						=model(**__SCREAMING_SNAKE_CASE).sample

         UpperCamelCase__ :			Tuple 						=create_lora_layers(__SCREAMING_SNAKE_CASE)

         # make sure we can set a list of attention processors
         model.set_attn_processor(__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)

         # test that attn processors can be set to itself
         model.set_attn_processor(model.attn_processors)

         with torch.no_grad():
            UpperCamelCase__ :			str 						=model(**__SCREAMING_SNAKE_CASE   ,			cross_attention_kwargs={"scale": 0.0}).sample
            UpperCamelCase__ :			Optional[Any] 						=model(**__SCREAMING_SNAKE_CASE   ,			cross_attention_kwargs={"scale": 0.5}).sample
            UpperCamelCase__ :			int 						=model(**__SCREAMING_SNAKE_CASE   ,			cross_attention_kwargs={"scale": 0.5}).sample

         assert (samplea - samplea).abs().max() < 3E-3
         assert (samplea - samplea).abs().max() < 3E-3

         # sample 2 and sample 3 should be different
         assert (samplea - samplea).abs().max() > 3E-3




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







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			str 						=self.prepare_init_args_and_inputs_for_common()

         UpperCamelCase__ :			str 						=8

         torch.manual_seed(0)
         UpperCamelCase__ :			Union[str, Any] 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)

         with torch.no_grad():
            UpperCamelCase__ :			Any 						=model(**__SCREAMING_SNAKE_CASE).sample

         UpperCamelCase__ :			Tuple 						=create_lora_layers(__SCREAMING_SNAKE_CASE)
         model.set_attn_processor(__SCREAMING_SNAKE_CASE)

         with torch.no_grad():
            UpperCamelCase__ :			int 						=model(**__SCREAMING_SNAKE_CASE   ,			cross_attention_kwargs={"scale": 0.5}).sample

         with tempfile.TemporaryDirectory() as tmpdirname:
            model.save_attn_procs(__SCREAMING_SNAKE_CASE)
            self.assertTrue(os.path.isfile(os.path.join(__SCREAMING_SNAKE_CASE   ,			"pytorch_lora_weights.bin")))
            torch.manual_seed(0)
            UpperCamelCase__ :			Any 						=self.model_class(**__SCREAMING_SNAKE_CASE)
            new_model.to(__SCREAMING_SNAKE_CASE)
            new_model.load_attn_procs(__SCREAMING_SNAKE_CASE)

         with torch.no_grad():
            UpperCamelCase__ :			Optional[int] 						=new_model(**__SCREAMING_SNAKE_CASE   ,			cross_attention_kwargs={"scale": 0.5}).sample

         assert (sample - new_sample).abs().max() < 1E-3

         # LoRA and no LoRA should NOT be the same
         assert (sample - old_sample).abs().max() > 1E-4




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







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			str 						=self.prepare_init_args_and_inputs_for_common()

         UpperCamelCase__ :			Union[str, Any] 						=8

         torch.manual_seed(0)
         UpperCamelCase__ :			Optional[Any] 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)

         with torch.no_grad():
            UpperCamelCase__ :			List[Any] 						=model(**__SCREAMING_SNAKE_CASE).sample

         UpperCamelCase__ :			Dict 						=create_lora_layers(__SCREAMING_SNAKE_CASE)
         model.set_attn_processor(__SCREAMING_SNAKE_CASE)

         with torch.no_grad():
            UpperCamelCase__ :			Optional[int] 						=model(**__SCREAMING_SNAKE_CASE   ,			cross_attention_kwargs={"scale": 0.5}).sample

         with tempfile.TemporaryDirectory() as tmpdirname:
            model.save_attn_procs(__SCREAMING_SNAKE_CASE   ,			safe_serialization=__SCREAMING_SNAKE_CASE)
            self.assertTrue(os.path.isfile(os.path.join(__SCREAMING_SNAKE_CASE   ,			"pytorch_lora_weights.safetensors")))
            torch.manual_seed(0)
            UpperCamelCase__ :			List[str] 						=self.model_class(**__SCREAMING_SNAKE_CASE)
            new_model.to(__SCREAMING_SNAKE_CASE)
            new_model.load_attn_procs(__SCREAMING_SNAKE_CASE)

         with torch.no_grad():
            UpperCamelCase__ :			Optional[int] 						=new_model(**__SCREAMING_SNAKE_CASE   ,			cross_attention_kwargs={"scale": 0.5}).sample

         assert (sample - new_sample).abs().max() < 3E-3

         # LoRA and no LoRA should NOT be the same
         assert (sample - old_sample).abs().max() > 1E-4




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







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			Optional[Any] 						=self.prepare_init_args_and_inputs_for_common()

         UpperCamelCase__ :			Any 						=8

         torch.manual_seed(0)
         UpperCamelCase__ :			str 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			List[str] 						=create_lora_layers(__SCREAMING_SNAKE_CASE   ,			mock_weights=__SCREAMING_SNAKE_CASE)
         model.set_attn_processor(__SCREAMING_SNAKE_CASE)
         # Saving as torch, properly reloads with directly filename
         with tempfile.TemporaryDirectory() as tmpdirname:
            model.save_attn_procs(__SCREAMING_SNAKE_CASE)
            self.assertTrue(os.path.isfile(os.path.join(__SCREAMING_SNAKE_CASE   ,			"pytorch_lora_weights.bin")))
            torch.manual_seed(0)
            UpperCamelCase__ :			Union[str, Any] 						=self.model_class(**__SCREAMING_SNAKE_CASE)
            new_model.to(__SCREAMING_SNAKE_CASE)
            new_model.load_attn_procs(__SCREAMING_SNAKE_CASE   ,			weight_name="pytorch_lora_weights.bin")




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







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			Dict 						=self.prepare_init_args_and_inputs_for_common()

         UpperCamelCase__ :			Union[str, Any] 						=8

         torch.manual_seed(0)
         UpperCamelCase__ :			List[str] 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Tuple 						=create_lora_layers(__SCREAMING_SNAKE_CASE   ,			mock_weights=__SCREAMING_SNAKE_CASE)
         model.set_attn_processor(__SCREAMING_SNAKE_CASE)
         # Saving as torch, properly reloads with directly filename
         with tempfile.TemporaryDirectory() as tmpdirname:
            model.save_attn_procs(__SCREAMING_SNAKE_CASE)
            self.assertTrue(os.path.isfile(os.path.join(__SCREAMING_SNAKE_CASE   ,			"pytorch_lora_weights.bin")))
            torch.manual_seed(0)
            UpperCamelCase__ :			List[Any] 						=self.model_class(**__SCREAMING_SNAKE_CASE)
            new_model.to(__SCREAMING_SNAKE_CASE)
            with self.assertRaises(__SCREAMING_SNAKE_CASE) as e:
               new_model.load_attn_procs(__SCREAMING_SNAKE_CASE   ,			use_safetensors=__SCREAMING_SNAKE_CASE)
            self.assertIn("Error no file named pytorch_lora_weights.safetensors"   ,			str(e.exception))




      def    UpperCAmelCase							(		self)					->						int:







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			int 						=self.prepare_init_args_and_inputs_for_common()

         UpperCamelCase__ :			List[str] 						=8

         torch.manual_seed(0)
         UpperCamelCase__ :			str 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)

         with torch.no_grad():
            UpperCamelCase__ :			Union[str, Any] 						=model(**__SCREAMING_SNAKE_CASE).sample

         UpperCamelCase__ :			Any 						=create_lora_layers(__SCREAMING_SNAKE_CASE)
         model.set_attn_processor(__SCREAMING_SNAKE_CASE)

         with torch.no_grad():
            UpperCamelCase__ :			Tuple 						=model(**__SCREAMING_SNAKE_CASE   ,			cross_attention_kwargs={"scale": 0.0}).sample

         model.set_attn_processor(AttnProcessor())

         with torch.no_grad():
            UpperCamelCase__ :			Optional[Any] 						=model(**__SCREAMING_SNAKE_CASE).sample

         assert (sample - new_sample).abs().max() < 1E-4
         assert (sample - old_sample).abs().max() < 3E-3




      @unittest.skipIf(
          torch_device != "cuda" or not is_xformers_available()   ,			reason="XFormers attention is only available with CUDA and `xformers` installed"   ,			)
      def    UpperCAmelCase							(		self)					->						int:







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			Optional[Any] 						=self.prepare_init_args_and_inputs_for_common()

         UpperCamelCase__ :			int 						=4

         torch.manual_seed(0)
         UpperCamelCase__ :			Dict 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			str 						=create_lora_layers(__SCREAMING_SNAKE_CASE)
         model.set_attn_processor(__SCREAMING_SNAKE_CASE)

         # default
         with torch.no_grad():
            UpperCamelCase__ :			str 						=model(**__SCREAMING_SNAKE_CASE).sample

            model.enable_xformers_memory_efficient_attention()
            UpperCamelCase__ :			List[str] 						=model(**__SCREAMING_SNAKE_CASE).sample

            model.disable_xformers_memory_efficient_attention()
            UpperCamelCase__ :			Optional[Any] 						=model(**__SCREAMING_SNAKE_CASE).sample

         assert (sample - on_sample).abs().max() < 1E-4
         assert (sample - off_sample).abs().max() < 1E-4




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







         """simple docstring"""







         UpperCamelCase__						,	UpperCamelCase__ :			Dict 						=self.prepare_init_args_and_inputs_for_common()
         UpperCamelCase__ :			List[str] 						=32

         UpperCamelCase__ :			Union[str, Any] 						=self.model_class(**__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()

         with torch.no_grad():
            UpperCamelCase__ :			List[Any] 						=model(**__SCREAMING_SNAKE_CASE)[0]

         model.enable_forward_chunking()
         with torch.no_grad():
            UpperCamelCase__ :			Tuple 						=model(**__SCREAMING_SNAKE_CASE)[0]

         self.assertEqual(output.shape   ,			output_a.shape   ,			"Shape doesn't match")
         assert np.abs(output.cpu() - output_a.cpu()).max() < 1E-2


# (todo: sayakpaul) implement SLOW tests.





import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         ErnieMForInformationExtraction,
         ErnieMForMultipleChoice,
         ErnieMForQuestionAnswering,
         ErnieMForSequenceClassification,
         ErnieMForTokenClassification,
         ErnieMModel,
     )
     from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST



class 							lowercase__:






      '''simple docstring'''







      def __init__(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE=13   ,			__SCREAMING_SNAKE_CASE=7   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=99   ,			__SCREAMING_SNAKE_CASE=32   ,			__SCREAMING_SNAKE_CASE=5   ,			__SCREAMING_SNAKE_CASE=4   ,			__SCREAMING_SNAKE_CASE=37   ,			__SCREAMING_SNAKE_CASE="gelu"   ,			__SCREAMING_SNAKE_CASE=0.1   ,			__SCREAMING_SNAKE_CASE=0.1   ,			__SCREAMING_SNAKE_CASE=5_12   ,			__SCREAMING_SNAKE_CASE=16   ,			__SCREAMING_SNAKE_CASE=2   ,			__SCREAMING_SNAKE_CASE=0.02   ,			__SCREAMING_SNAKE_CASE=3   ,			__SCREAMING_SNAKE_CASE=4   ,			__SCREAMING_SNAKE_CASE=None   ,			)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=parent
         UpperCamelCase__ :			Tuple 						=batch_size
         UpperCamelCase__ :			Union[str, Any] 						=seq_length
         UpperCamelCase__ :			Union[str, Any] 						=is_training
         UpperCamelCase__ :			Tuple 						=use_input_mask
         UpperCamelCase__ :			Union[str, Any] 						=use_labels
         UpperCamelCase__ :			Union[str, Any] 						=vocab_size
         UpperCamelCase__ :			List[Any] 						=hidden_size
         UpperCamelCase__ :			Dict 						=num_hidden_layers
         UpperCamelCase__ :			Any 						=num_attention_heads
         UpperCamelCase__ :			List[str] 						=intermediate_size
         UpperCamelCase__ :			List[Any] 						=hidden_act
         UpperCamelCase__ :			Tuple 						=hidden_dropout_prob
         UpperCamelCase__ :			Any 						=attention_probs_dropout_prob
         UpperCamelCase__ :			Tuple 						=max_position_embeddings
         UpperCamelCase__ :			Dict 						=type_vocab_size
         UpperCamelCase__ :			Tuple 						=type_sequence_label_size
         UpperCamelCase__ :			Tuple 						=initializer_range
         UpperCamelCase__ :			List[str] 						=num_labels
         UpperCamelCase__ :			Any 						=num_choices
         UpperCamelCase__ :			Tuple 						=scope




      def    UpperCAmelCase							(		self)					->						int:







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						=ids_tensor([self.batch_size, self.seq_length]   ,			self.vocab_size)

         UpperCamelCase__ :			Any 						=None
         if self.use_input_mask:
            UpperCamelCase__ :			int 						=random_attention_mask([self.batch_size, self.seq_length])

         UpperCamelCase__ :			List[str] 						=None
         UpperCamelCase__ :			int 						=None
         UpperCamelCase__ :			Optional[Any] 						=None
         if self.use_labels:
            UpperCamelCase__ :			Dict 						=ids_tensor([self.batch_size]   ,			self.type_sequence_label_size)
            UpperCamelCase__ :			Dict 						=ids_tensor([self.batch_size, self.seq_length]   ,			self.num_labels)
            UpperCamelCase__ :			Optional[Any] 						=ids_tensor([self.batch_size]   ,			self.num_choices)

         UpperCamelCase__ :			Tuple 						=self.get_config()

         return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels




      def    UpperCAmelCase							(		self)					->						str:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=ids_tensor([self.batch_size, self.seq_length]   ,			self.vocab_size)

         UpperCamelCase__ :			List[str] 						=None
         if self.use_input_mask:
            UpperCamelCase__ :			Union[str, Any] 						=random_attention_mask([self.batch_size, self.seq_length])
         UpperCamelCase__ :			Union[str, Any] 						=self.get_config()

         return config, input_ids, input_mask




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







         """simple docstring"""







         return ErnieMConfig(
             vocab_size=self.vocab_size   ,			hidden_size=self.hidden_size   ,			num_hidden_layers=self.num_hidden_layers   ,			num_attention_heads=self.num_attention_heads   ,			intermediate_size=self.intermediate_size   ,			hidden_act=self.hidden_act   ,			hidden_dropout_prob=self.hidden_dropout_prob   ,			attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,			max_position_embeddings=self.max_position_embeddings   ,			type_vocab_size=self.type_vocab_size   ,			initializer_range=self.initializer_range   ,			)




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						str:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=ErnieMModel(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			int 						=model(__SCREAMING_SNAKE_CASE   ,			return_dict=__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.last_hidden_state.shape   ,			(self.batch_size, self.seq_length, self.hidden_size))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						List[Any]:







         """simple docstring"""







         UpperCamelCase__ :			str 						=ErnieMForQuestionAnswering(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			Dict 						=model(
             __SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			start_positions=__SCREAMING_SNAKE_CASE   ,			end_positions=__SCREAMING_SNAKE_CASE   ,			)
         self.parent.assertEqual(result.start_logits.shape   ,			(self.batch_size, self.seq_length))
         self.parent.assertEqual(result.end_logits.shape   ,			(self.batch_size, self.seq_length))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=ErnieMForInformationExtraction(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			int 						=torch.ones_like(__SCREAMING_SNAKE_CASE   ,			dtype=torch.floataa)
         UpperCamelCase__ :			Union[str, Any] 						=model(
             __SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			start_positions=__SCREAMING_SNAKE_CASE   ,			end_positions=__SCREAMING_SNAKE_CASE   ,			)
         self.parent.assertEqual(result.start_logits.shape   ,			(self.batch_size, self.seq_length))
         self.parent.assertEqual(result.end_logits.shape   ,			(self.batch_size, self.seq_length))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			List[Any] 						=self.num_labels
         UpperCamelCase__ :			List[Any] 						=ErnieMForSequenceClassification(__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			Union[str, Any] 						=model(__SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			labels=__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.num_labels))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Tuple:







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						=self.num_labels
         UpperCamelCase__ :			int 						=ErnieMForTokenClassification(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         input_ids.to(__SCREAMING_SNAKE_CASE)
         input_mask.to(__SCREAMING_SNAKE_CASE)
         token_labels.to(__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Tuple 						=model(__SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			labels=__SCREAMING_SNAKE_CASE)

         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.seq_length, self.num_labels))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Optional[Any]:







         """simple docstring"""







         UpperCamelCase__ :			List[Any] 						=self.num_choices
         UpperCamelCase__ :			List[str] 						=ErnieMForMultipleChoice(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			Optional[Any] 						=input_ids.unsqueeze(1).expand(-1   ,			self.num_choices   ,			-1).contiguous()
         UpperCamelCase__ :			int 						=input_mask.unsqueeze(1).expand(-1   ,			self.num_choices   ,			-1).contiguous()
         UpperCamelCase__ :			Optional[Any] 						=model(
             __SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			labels=__SCREAMING_SNAKE_CASE   ,			)
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.num_choices))




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







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=self.prepare_config_and_inputs()
         (
             (
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	
         ) :			int 						=config_and_inputs
         UpperCamelCase__ :			Tuple 						={"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict




@require_torch
class 							lowercase__(						snake_case__			,							snake_case__			,							unittest.TestCase			):






      '''simple docstring'''







      snake_case__       =					(
          (
              ErnieMModel,
              ErnieMForMultipleChoice,
              ErnieMForQuestionAnswering,
              ErnieMForSequenceClassification,
              ErnieMForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      snake_case__       =					()
      snake_case__       =					(
          {
              '''feature-extraction''': ErnieMModel,
              '''question-answering''': ErnieMForQuestionAnswering,
              '''text-classification''': ErnieMForSequenceClassification,
              '''token-classification''': ErnieMForTokenClassification,
              '''zero-shot''': ErnieMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      snake_case__       =					False




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Optional[Any]:







         """simple docstring"""







         if pipeline_test_casse_name == "QAPipelineTests":
            return True

         return False




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







         """simple docstring"""







         UpperCamelCase__ :			Dict 						=ErnieMModelTester(self)
         UpperCamelCase__ :			int 						=ConfigTester(self   ,			config_class=__SCREAMING_SNAKE_CASE   ,			hidden_size=37)




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







         """simple docstring"""







         self.config_tester.run_common_tests()




      def    UpperCAmelCase							(		self)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)




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







         """simple docstring"""







         UpperCamelCase__ :			int 						=self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
            UpperCamelCase__ :			List[str] 						=type
            self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Tuple:







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			Union[str, Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_information_extraction(*__SCREAMING_SNAKE_CASE)




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







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE)




      @slow
      def    UpperCAmelCase							(		self)					->						Tuple:







         """simple docstring"""







         for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCamelCase__ :			Optional[Any] 						=ErnieMModel.from_pretrained(__SCREAMING_SNAKE_CASE)
            self.assertIsNotNone(__SCREAMING_SNAKE_CASE)




@require_torch
class 							lowercase__(						unittest.TestCase			):






      '''simple docstring'''







      @slow
      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch")
         model.eval()
         UpperCamelCase__ :			int 						=torch.tensor([[0, 1, 2, 3, 4, 5]])
         UpperCamelCase__ :			List[Any] 						=model(__SCREAMING_SNAKE_CASE)[0]

         # TODO Replace vocab size
         UpperCamelCase__ :			List[Any] 						=7_68

         UpperCamelCase__ :			Dict 						=torch.Size((1, 6, hidden_size))
         self.assertEqual(output.shape   ,			__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Union[str, Any] 						=torch.tensor(
             [[[-0.00_12, 0.12_45, -0.02_14], [-0.07_42, 0.02_44, -0.07_71], [-0.03_33, 0.11_64, -0.15_54]]])

         self.assertTrue(torch.allclose(output[:, :3, :3]   ,			__SCREAMING_SNAKE_CASE   ,			atol=1E-3))

