

import gc
import random
import tempfile
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.semantic_stable_diffusion import SemanticStableDiffusionPipeline as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()



class 		a_	(							unittest.TestCase					):



     """simple docstring"""







     def 	_lowerCAmelCase					(				self					:							Optional[Any]    ):
          # clean up the VRAM after each test
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()
     @property
     def 	_lowerCAmelCase					(				self					:							Any    ):
          SCREAMING_SNAKE_CASE										=1
          SCREAMING_SNAKE_CASE										=3
          SCREAMING_SNAKE_CASE										=(32, 32)

          SCREAMING_SNAKE_CASE										=floats_tensor((batch_size, num_channels) + sizes      ,rng=random.Random(0    )    ).to(snake_case    )
          return image
     @property
     def 	_lowerCAmelCase					(				self					:							Dict    ):
          torch.manual_seed(0    )
          SCREAMING_SNAKE_CASE										=UNetaDConditionModel(
              block_out_channels=(32, 64)      ,layers_per_block=2      ,sample_size=32      ,in_channels=4      ,out_channels=4      ,down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')      ,up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')      ,cross_attention_dim=32      ,)
          return model
     @property
     def 	_lowerCAmelCase					(				self					:							Dict    ):
          torch.manual_seed(0    )
          SCREAMING_SNAKE_CASE										=AutoencoderKL(
              block_out_channels=[32, 64]      ,in_channels=3      ,out_channels=3      ,down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']      ,up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']      ,latent_channels=4      ,)
          return model
     @property
     def 	_lowerCAmelCase					(				self					:							Optional[int]    ):
          torch.manual_seed(0    )
          SCREAMING_SNAKE_CASE										=CLIPTextConfig(
              bos_token_id=0      ,eos_token_id=2      ,hidden_size=32      ,intermediate_size=37      ,layer_norm_eps=1e-05      ,num_attention_heads=4      ,num_hidden_layers=5      ,pad_token_id=1      ,vocab_size=1000      ,)
          return CLIPTextModel(snake_case    )
     @property
     def 	_lowerCAmelCase					(				self					:							str    ):
          def extract(*snake_case					:							List[Any]      ,**snake_case					:							Optional[int]    ):



               class 		a_	:



                    """simple docstring"""







                    def __init__(				self					:							List[Any]    ):
                         SCREAMING_SNAKE_CASE										=torch.ones([0]    )


                    def 	_lowerCAmelCase					(				self					:							Tuple      ,snake_case					:							Tuple    ):
                         self.pixel_values.to(snake_case    )
                         return self







               return Out()

          return extract
     def 	_lowerCAmelCase					(				self					:							List[Any]    ):
          SCREAMING_SNAKE_CASE										='cpu'  # ensure determinism for the device-dependent torch.Generator
          SCREAMING_SNAKE_CASE										=self.dummy_cond_unet
          SCREAMING_SNAKE_CASE										=DDIMScheduler(
              beta_start=0.00_085      ,beta_end=0.012      ,beta_schedule='scaled_linear'      ,clip_sample=snake_case      ,set_alpha_to_one=snake_case      ,)

          SCREAMING_SNAKE_CASE										=self.dummy_vae
          SCREAMING_SNAKE_CASE										=self.dummy_text_encoder
          SCREAMING_SNAKE_CASE										=CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'    )

          # make sure here that pndm scheduler skips prk
          SCREAMING_SNAKE_CASE										=StableDiffusionPipeline(
              unet=snake_case      ,scheduler=snake_case      ,vae=snake_case      ,text_encoder=snake_case      ,tokenizer=snake_case      ,safety_checker=snake_case      ,feature_extractor=self.dummy_extractor      ,)
          SCREAMING_SNAKE_CASE										=sd_pipe.to(snake_case    )
          sd_pipe.set_progress_bar_config(disable=snake_case    )

          SCREAMING_SNAKE_CASE										='A painting of a squirrel eating a burger'

          SCREAMING_SNAKE_CASE										=torch.Generator(device=snake_case    ).manual_seed(0    )
          SCREAMING_SNAKE_CASE										=sd_pipe([prompt]      ,generator=snake_case      ,guidance_scale=6.0      ,num_inference_steps=2      ,output_type='np'    )
          SCREAMING_SNAKE_CASE										=output.images

          SCREAMING_SNAKE_CASE										=torch.Generator(device=snake_case    ).manual_seed(0    )
          SCREAMING_SNAKE_CASE										=sd_pipe(
              [prompt]      ,generator=snake_case      ,guidance_scale=6.0      ,num_inference_steps=2      ,output_type='np'      ,return_dict=snake_case      ,)[0]

          SCREAMING_SNAKE_CASE										=image[0, -3:, -3:, -1]
          SCREAMING_SNAKE_CASE										=image_from_tuple[0, -3:, -3:, -1]

          assert image.shape == (1, 64, 64, 3)
          SCREAMING_SNAKE_CASE										=np.array([0.5_753, 0.6_114, 0.5_001, 0.5_034, 0.5_470, 0.4_729, 0.4_971, 0.4_867, 0.4_867]    )

          assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2
          assert np.abs(image_from_tuple_slice.flatten() - expected_slice    ).max() < 1e-2
     def 	_lowerCAmelCase					(				self					:							Optional[int]    ):
          SCREAMING_SNAKE_CASE										='cpu'  # ensure determinism for the device-dependent torch.Generator
          SCREAMING_SNAKE_CASE										=self.dummy_cond_unet
          SCREAMING_SNAKE_CASE										=PNDMScheduler(skip_prk_steps=snake_case    )
          SCREAMING_SNAKE_CASE										=self.dummy_vae
          SCREAMING_SNAKE_CASE										=self.dummy_text_encoder
          SCREAMING_SNAKE_CASE										=CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'    )

          # make sure here that pndm scheduler skips prk
          SCREAMING_SNAKE_CASE										=StableDiffusionPipeline(
              unet=snake_case      ,scheduler=snake_case      ,vae=snake_case      ,text_encoder=snake_case      ,tokenizer=snake_case      ,safety_checker=snake_case      ,feature_extractor=self.dummy_extractor      ,)
          SCREAMING_SNAKE_CASE										=sd_pipe.to(snake_case    )
          sd_pipe.set_progress_bar_config(disable=snake_case    )

          SCREAMING_SNAKE_CASE										='A painting of a squirrel eating a burger'
          SCREAMING_SNAKE_CASE										=torch.Generator(device=snake_case    ).manual_seed(0    )
          SCREAMING_SNAKE_CASE										=sd_pipe([prompt]      ,generator=snake_case      ,guidance_scale=6.0      ,num_inference_steps=2      ,output_type='np'    )

          SCREAMING_SNAKE_CASE										=output.images

          SCREAMING_SNAKE_CASE										=torch.Generator(device=snake_case    ).manual_seed(0    )
          SCREAMING_SNAKE_CASE										=sd_pipe(
              [prompt]      ,generator=snake_case      ,guidance_scale=6.0      ,num_inference_steps=2      ,output_type='np'      ,return_dict=snake_case      ,)[0]

          SCREAMING_SNAKE_CASE										=image[0, -3:, -3:, -1]
          SCREAMING_SNAKE_CASE										=image_from_tuple[0, -3:, -3:, -1]

          assert image.shape == (1, 64, 64, 3)
          SCREAMING_SNAKE_CASE										=np.array([0.5_122, 0.5_712, 0.4_825, 0.5_053, 0.5_646, 0.4_769, 0.5_179, 0.4_894, 0.4_994]    )

          assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2
          assert np.abs(image_from_tuple_slice.flatten() - expected_slice    ).max() < 1e-2
     def 	_lowerCAmelCase					(				self					:							Tuple    ):
          SCREAMING_SNAKE_CASE										=StableDiffusionPipeline.from_pretrained(
              'hf-internal-testing/tiny-stable-diffusion-lms-pipe'      ,safety_checker=snake_case    )
          assert isinstance(snake_case      ,snake_case    )
          assert isinstance(pipe.scheduler      ,snake_case    )
          assert pipe.safety_checker is None

          SCREAMING_SNAKE_CASE										=pipe('example prompt'      ,num_inference_steps=2    ).images[0]
          assert image is not None

          # check that there's no error when saving a pipeline with one of the models being None
          with tempfile.TemporaryDirectory() as tmpdirname:
               pipe.save_pretrained(snake_case    )
               SCREAMING_SNAKE_CASE										=StableDiffusionPipeline.from_pretrained(snake_case    )

          # sanity check that the pipeline still works
          assert pipe.safety_checker is None
          SCREAMING_SNAKE_CASE										=pipe('example prompt'      ,num_inference_steps=2    ).images[0]
          assert image is not None


     @unittest.skipIf(torch_device != 'cuda'      ,'This test requires a GPU'    )
     def 	_lowerCAmelCase					(				self					:							List[str]    ):
          SCREAMING_SNAKE_CASE										=self.dummy_cond_unet
          SCREAMING_SNAKE_CASE										=PNDMScheduler(skip_prk_steps=snake_case    )
          SCREAMING_SNAKE_CASE										=self.dummy_vae
          SCREAMING_SNAKE_CASE										=self.dummy_text_encoder
          SCREAMING_SNAKE_CASE										=CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'    )

          # put models in fp16
          SCREAMING_SNAKE_CASE										=unet.half()
          SCREAMING_SNAKE_CASE										=vae.half()
          SCREAMING_SNAKE_CASE										=bert.half()

          # make sure here that pndm scheduler skips prk
          SCREAMING_SNAKE_CASE										=StableDiffusionPipeline(
              unet=snake_case      ,scheduler=snake_case      ,vae=snake_case      ,text_encoder=snake_case      ,tokenizer=snake_case      ,safety_checker=snake_case      ,feature_extractor=self.dummy_extractor      ,)
          SCREAMING_SNAKE_CASE										=sd_pipe.to(snake_case    )
          sd_pipe.set_progress_bar_config(disable=snake_case    )

          SCREAMING_SNAKE_CASE										='A painting of a squirrel eating a burger'
          SCREAMING_SNAKE_CASE										=sd_pipe([prompt]      ,num_inference_steps=2      ,output_type='np'    ).images

          assert image.shape == (1, 64, 64, 3)







@nightly
@require_torch_gpu
class 		a_	(							unittest.TestCase					):



     """simple docstring"""







     def 	_lowerCAmelCase					(				self					:							List[Any]    ):
          # clean up the VRAM after each test
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()
     def 	_lowerCAmelCase					(				self					:							int    ):
          SCREAMING_SNAKE_CASE										='cuda'
          SCREAMING_SNAKE_CASE										=StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'    )
          SCREAMING_SNAKE_CASE										=pipe.to(snake_case    )
          pipe.set_progress_bar_config(disable=snake_case    )

          SCREAMING_SNAKE_CASE										='a photo of a cat'
          SCREAMING_SNAKE_CASE										={
              'editing_prompt': ['sunglasses'],
              'reverse_editing_direction': [False],
              'edit_warmup_steps': 10,
              'edit_guidance_scale': 6,
              'edit_threshold': 0.95,
              'edit_momentum_scale': 0.5,
              'edit_mom_beta': 0.6,
          }

          SCREAMING_SNAKE_CASE										=3
          SCREAMING_SNAKE_CASE										=7

          # no sega enabled
          SCREAMING_SNAKE_CASE										=torch.Generator(snake_case    )
          generator.manual_seed(snake_case    )
          SCREAMING_SNAKE_CASE										=pipe(
              [prompt]      ,generator=snake_case      ,guidance_scale=snake_case      ,num_inference_steps=50      ,output_type='np'      ,width=512      ,height=512      ,)

          SCREAMING_SNAKE_CASE										=output.images
          SCREAMING_SNAKE_CASE										=image[0, -3:, -3:, -1]
          SCREAMING_SNAKE_CASE										=[
              0.34_673_113,
              0.38_492_733,
              0.37_597_352,
              0.34_086_335,
              0.35_650_748,
              0.35_579_205,
              0.3_384_763,
              0.34_340_236,
              0.3_573_271,
          ]

          assert image.shape == (1, 512, 512, 3)

          assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2

          # with sega enabled
          # generator = torch.manual_seed(seed)
          generator.manual_seed(snake_case    )
          SCREAMING_SNAKE_CASE										=pipe(
              [prompt]      ,generator=snake_case      ,guidance_scale=snake_case      ,num_inference_steps=50      ,output_type='np'      ,width=512      ,height=512      ,**snake_case      ,)

          SCREAMING_SNAKE_CASE										=output.images
          SCREAMING_SNAKE_CASE										=image[0, -3:, -3:, -1]
          SCREAMING_SNAKE_CASE										=[
              0.41_887_826,
              0.37_728_766,
              0.30_138_272,
              0.41_416_335,
              0.41_664_985,
              0.36_283_392,
              0.36_191_246,
              0.43_364_465,
              0.43_001_732,
          ]

          assert image.shape == (1, 512, 512, 3)

          assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2
     def 	_lowerCAmelCase					(				self					:							Optional[int]    ):
          SCREAMING_SNAKE_CASE										='cuda'
          SCREAMING_SNAKE_CASE										=StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'    )
          SCREAMING_SNAKE_CASE										=pipe.to(snake_case    )
          pipe.set_progress_bar_config(disable=snake_case    )

          SCREAMING_SNAKE_CASE										='an image of a crowded boulevard, realistic, 4k'
          SCREAMING_SNAKE_CASE										={
              'editing_prompt': 'crowd, crowded, people',
              'reverse_editing_direction': True,
              'edit_warmup_steps': 10,
              'edit_guidance_scale': 8.3,
              'edit_threshold': 0.9,
              'edit_momentum_scale': 0.5,
              'edit_mom_beta': 0.6,
          }

          SCREAMING_SNAKE_CASE										=9
          SCREAMING_SNAKE_CASE										=7

          # no sega enabled
          SCREAMING_SNAKE_CASE										=torch.Generator(snake_case    )
          generator.manual_seed(snake_case    )
          SCREAMING_SNAKE_CASE										=pipe(
              [prompt]      ,generator=snake_case      ,guidance_scale=snake_case      ,num_inference_steps=50      ,output_type='np'      ,width=512      ,height=512      ,)

          SCREAMING_SNAKE_CASE										=output.images
          SCREAMING_SNAKE_CASE										=image[0, -3:, -3:, -1]
          SCREAMING_SNAKE_CASE										=[
              0.43_497_998,
              0.91_814_065,
              0.7_540_739,
              0.55_580_205,
              0.8_467_265,
              0.5_389_691,
              0.62_574_506,
              0.58_897_763,
              0.50_926_757,
          ]

          assert image.shape == (1, 512, 512, 3)

          assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2

          # with sega enabled
          # generator = torch.manual_seed(seed)
          generator.manual_seed(snake_case    )
          SCREAMING_SNAKE_CASE										=pipe(
              [prompt]      ,generator=snake_case      ,guidance_scale=snake_case      ,num_inference_steps=50      ,output_type='np'      ,width=512      ,height=512      ,**snake_case      ,)

          SCREAMING_SNAKE_CASE										=output.images
          SCREAMING_SNAKE_CASE										=image[0, -3:, -3:, -1]
          SCREAMING_SNAKE_CASE										=[
              0.3_089_719,
              0.30_500_144,
              0.29_016_042,
              0.30_630_964,
              0.325_687,
              0.29_419_225,
              0.2_908_091,
              0.28_723_598,
              0.27_696_294,
          ]

          assert image.shape == (1, 512, 512, 3)

          assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2
     def 	_lowerCAmelCase					(				self					:							Optional[Any]    ):
          SCREAMING_SNAKE_CASE										='cuda'
          SCREAMING_SNAKE_CASE										=StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'    )
          SCREAMING_SNAKE_CASE										=pipe.to(snake_case    )
          pipe.set_progress_bar_config(disable=snake_case    )

          SCREAMING_SNAKE_CASE										='a castle next to a river'
          SCREAMING_SNAKE_CASE										={
              'editing_prompt': ['boat on a river, boat', 'monet, impression, sunrise'],
              'reverse_editing_direction': False,
              'edit_warmup_steps': [15, 18],
              'edit_guidance_scale': 6,
              'edit_threshold': [0.9, 0.8],
              'edit_momentum_scale': 0.5,
              'edit_mom_beta': 0.6,
          }

          SCREAMING_SNAKE_CASE										=48
          SCREAMING_SNAKE_CASE										=7

          # no sega enabled
          SCREAMING_SNAKE_CASE										=torch.Generator(snake_case    )
          generator.manual_seed(snake_case    )
          SCREAMING_SNAKE_CASE										=pipe(
              [prompt]      ,generator=snake_case      ,guidance_scale=snake_case      ,num_inference_steps=50      ,output_type='np'      ,width=512      ,height=512      ,)

          SCREAMING_SNAKE_CASE										=output.images
          SCREAMING_SNAKE_CASE										=image[0, -3:, -3:, -1]
          SCREAMING_SNAKE_CASE										=[
              0.75_163_555,
              0.76_037_145,
              0.61_785,
              0.9_189_673,
              0.8_627_701,
              0.85_189_694,
              0.8_512_813,
              0.87_012_076,
              0.8_312_857,
          ]

          assert image.shape == (1, 512, 512, 3)

          assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2

          # with sega enabled
          # generator = torch.manual_seed(seed)
          generator.manual_seed(snake_case    )
          SCREAMING_SNAKE_CASE										=pipe(
              [prompt]      ,generator=snake_case      ,guidance_scale=snake_case      ,num_inference_steps=50      ,output_type='np'      ,width=512      ,height=512      ,**snake_case      ,)

          SCREAMING_SNAKE_CASE										=output.images
          SCREAMING_SNAKE_CASE										=image[0, -3:, -3:, -1]
          SCREAMING_SNAKE_CASE										=[
              0.73_553_365,
              0.7_537_271,
              0.74_341_905,
              0.66_480_356,
              0.6_472_925,
              0.63_039_416,
              0.64_812_905,
              0.6_749_717,
              0.6_517_102,
          ]

          assert image.shape == (1, 512, 512, 3)

          assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2


     def 	_lowerCAmelCase					(				self					:							Optional[Any]    ):
          SCREAMING_SNAKE_CASE										='cuda'
          SCREAMING_SNAKE_CASE										=StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'      ,torch_dtype=torch.floataa    )
          SCREAMING_SNAKE_CASE										=pipe.to(snake_case    )
          pipe.set_progress_bar_config(disable=snake_case    )

          SCREAMING_SNAKE_CASE										='a photo of a cat'
          SCREAMING_SNAKE_CASE										={
              'editing_prompt': ['sunglasses'],
              'reverse_editing_direction': [False],
              'edit_warmup_steps': 10,
              'edit_guidance_scale': 6,
              'edit_threshold': 0.95,
              'edit_momentum_scale': 0.5,
              'edit_mom_beta': 0.6,
          }

          SCREAMING_SNAKE_CASE										=3
          SCREAMING_SNAKE_CASE										=7

          # no sega enabled
          SCREAMING_SNAKE_CASE										=torch.Generator(snake_case    )
          generator.manual_seed(snake_case    )
          SCREAMING_SNAKE_CASE										=pipe(
              [prompt]      ,generator=snake_case      ,guidance_scale=snake_case      ,num_inference_steps=50      ,output_type='np'      ,width=512      ,height=512      ,)

          SCREAMING_SNAKE_CASE										=output.images
          SCREAMING_SNAKE_CASE										=image[0, -3:, -3:, -1]
          SCREAMING_SNAKE_CASE										=[
              0.34_887_695,
              0.3_876_953,
              0.375,
              0.34_423_828,
              0.3_581_543,
              0.35_717_773,
              0.3_383_789,
              0.34_570_312,
              0.359_375,
          ]

          assert image.shape == (1, 512, 512, 3)

          assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2

          # with sega enabled
          # generator = torch.manual_seed(seed)
          generator.manual_seed(snake_case    )
          SCREAMING_SNAKE_CASE										=pipe(
              [prompt]      ,generator=snake_case      ,guidance_scale=snake_case      ,num_inference_steps=50      ,output_type='np'      ,width=512      ,height=512      ,**snake_case      ,)

          SCREAMING_SNAKE_CASE										=output.images
          SCREAMING_SNAKE_CASE										=image[0, -3:, -3:, -1]
          SCREAMING_SNAKE_CASE										=[
              0.42_285_156,
              0.36_914_062,
              0.29_077_148,
              0.42_041_016,
              0.41_918_945,
              0.35_498_047,
              0.3_618_164,
              0.4_423_828,
              0.43_115_234,
          ]

          assert image.shape == (1, 512, 512, 3)

          assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2




from __future__ import annotations

import unittest

from transformers import MPNetConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers.models.mpnet.modeling_tf_mpnet import (
     TFMPNetForMaskedLM,
     TFMPNetForMultipleChoice,
     TFMPNetForQuestionAnswering,
     TFMPNetForSequenceClassification,
     TFMPNetForTokenClassification,
     TFMPNetModel,
 )



class 		a_	:



      """simple docstring"""







      def __init__(				self					:							Any      ,snake_case					:							List[Any]      ,snake_case					:							Optional[Any]=13      ,snake_case					:							str=7      ,snake_case					:							List[Any]=True      ,snake_case					:							Optional[Any]=True      ,snake_case					:							int=False      ,snake_case					:							List[str]=True      ,snake_case					:							Optional[int]=99      ,snake_case					:							Any=64      ,snake_case					:							int=2      ,snake_case					:							str=4      ,snake_case					:							str=64      ,snake_case					:							List[Any]="gelu"      ,snake_case					:							Dict=0.1      ,snake_case					:							int=0.1      ,snake_case					:							List[Any]=512      ,snake_case					:							int=16      ,snake_case					:							List[Any]=2      ,snake_case					:							Union[str, Any]=0.02      ,snake_case					:							List[str]=3      ,snake_case					:							Any=4      ,snake_case					:							Tuple=None      ,):
           SCREAMING_SNAKE_CASE										=parent
           SCREAMING_SNAKE_CASE										=batch_size
           SCREAMING_SNAKE_CASE										=seq_length
           SCREAMING_SNAKE_CASE										=is_training
           SCREAMING_SNAKE_CASE										=use_input_mask
           SCREAMING_SNAKE_CASE										=use_token_type_ids
           SCREAMING_SNAKE_CASE										=use_labels
           SCREAMING_SNAKE_CASE										=vocab_size
           SCREAMING_SNAKE_CASE										=hidden_size
           SCREAMING_SNAKE_CASE										=num_hidden_layers
           SCREAMING_SNAKE_CASE										=num_attention_heads
           SCREAMING_SNAKE_CASE										=intermediate_size
           SCREAMING_SNAKE_CASE										=hidden_act
           SCREAMING_SNAKE_CASE										=hidden_dropout_prob
           SCREAMING_SNAKE_CASE										=attention_probs_dropout_prob
           SCREAMING_SNAKE_CASE										=max_position_embeddings
           SCREAMING_SNAKE_CASE										=type_vocab_size
           SCREAMING_SNAKE_CASE										=type_sequence_label_size
           SCREAMING_SNAKE_CASE										=initializer_range
           SCREAMING_SNAKE_CASE										=num_labels
           SCREAMING_SNAKE_CASE										=num_choices
           SCREAMING_SNAKE_CASE										=scope
      def 	_lowerCAmelCase					(				self					:							List[Any]    ):
           SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size, self.seq_length]      ,self.vocab_size    )

           SCREAMING_SNAKE_CASE										=None
           if self.use_input_mask:
                SCREAMING_SNAKE_CASE										=random_attention_mask([self.batch_size, self.seq_length]    )

           SCREAMING_SNAKE_CASE										=None
           SCREAMING_SNAKE_CASE										=None
           SCREAMING_SNAKE_CASE										=None
           if self.use_labels:
                SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size]      ,self.type_sequence_label_size    )
                SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size, self.seq_length]      ,self.num_labels    )
                SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size]      ,self.num_choices    )

           SCREAMING_SNAKE_CASE										=MPNetConfig(
               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      ,initializer_range=self.initializer_range      ,)
           return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
      def 	_lowerCAmelCase					(				self					:							Dict      ,snake_case					:							Any      ,snake_case					:							Optional[int]      ,snake_case					:							str      ,snake_case					:							Union[str, Any]      ,snake_case					:							Optional[Any]      ,snake_case					:							Any    ):
           SCREAMING_SNAKE_CASE										=TFMPNetModel(config=snake_case    )
           SCREAMING_SNAKE_CASE										={'input_ids': input_ids, 'attention_mask': input_mask}
           SCREAMING_SNAKE_CASE										=model(snake_case    )
           SCREAMING_SNAKE_CASE										=[input_ids, input_mask]
           SCREAMING_SNAKE_CASE										=model(snake_case    )
           self.parent.assertEqual(result.last_hidden_state.shape      ,(self.batch_size, self.seq_length, self.hidden_size)    )
      def 	_lowerCAmelCase					(				self					:							List[Any]      ,snake_case					:							List[str]      ,snake_case					:							Tuple      ,snake_case					:							Dict      ,snake_case					:							Optional[Any]      ,snake_case					:							Optional[int]      ,snake_case					:							Dict    ):
           SCREAMING_SNAKE_CASE										=TFMPNetForMaskedLM(config=snake_case    )
           SCREAMING_SNAKE_CASE										={'input_ids': input_ids, 'attention_mask': input_mask}
           SCREAMING_SNAKE_CASE										=model(snake_case    )
           self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.seq_length, self.vocab_size)    )
      def 	_lowerCAmelCase					(				self					:							int      ,snake_case					:							List[str]      ,snake_case					:							Dict      ,snake_case					:							List[Any]      ,snake_case					:							Optional[int]      ,snake_case					:							Optional[int]      ,snake_case					:							Tuple    ):
           SCREAMING_SNAKE_CASE										=TFMPNetForQuestionAnswering(config=snake_case    )
           SCREAMING_SNAKE_CASE										={
               'input_ids': input_ids,
               'attention_mask': input_mask,
           }
           SCREAMING_SNAKE_CASE										=model(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 	_lowerCAmelCase					(				self					:							List[Any]      ,snake_case					:							Union[str, Any]      ,snake_case					:							str      ,snake_case					:							str      ,snake_case					:							Any      ,snake_case					:							Tuple      ,snake_case					:							Optional[Any]    ):
           SCREAMING_SNAKE_CASE										=self.num_labels
           SCREAMING_SNAKE_CASE										=TFMPNetForSequenceClassification(snake_case    )
           SCREAMING_SNAKE_CASE										={'input_ids': input_ids, 'attention_mask': input_mask}
           SCREAMING_SNAKE_CASE										=model(snake_case    )
           self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_labels)    )
      def 	_lowerCAmelCase					(				self					:							int      ,snake_case					:							Any      ,snake_case					:							Union[str, Any]      ,snake_case					:							int      ,snake_case					:							List[str]      ,snake_case					:							Tuple      ,snake_case					:							Union[str, Any]    ):
           SCREAMING_SNAKE_CASE										=self.num_choices
           SCREAMING_SNAKE_CASE										=TFMPNetForMultipleChoice(snake_case    )
           SCREAMING_SNAKE_CASE										=tf.tile(tf.expand_dims(snake_case      ,1    )      ,(1, self.num_choices, 1)    )
           SCREAMING_SNAKE_CASE										=tf.tile(tf.expand_dims(snake_case      ,1    )      ,(1, self.num_choices, 1)    )
           SCREAMING_SNAKE_CASE										={
               'input_ids': multiple_choice_inputs_ids,
               'attention_mask': multiple_choice_input_mask,
           }
           SCREAMING_SNAKE_CASE										=model(snake_case    )
           self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_choices)    )
      def 	_lowerCAmelCase					(				self					:							Any      ,snake_case					:							Tuple      ,snake_case					:							Dict      ,snake_case					:							Tuple      ,snake_case					:							Union[str, Any]      ,snake_case					:							Optional[int]      ,snake_case					:							Dict    ):
           SCREAMING_SNAKE_CASE										=self.num_labels
           SCREAMING_SNAKE_CASE										=TFMPNetForTokenClassification(snake_case    )
           SCREAMING_SNAKE_CASE										={'input_ids': input_ids, 'attention_mask': input_mask}
           SCREAMING_SNAKE_CASE										=model(snake_case    )
           self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.seq_length, self.num_labels)    )


      def 	_lowerCAmelCase					(				self					:							Optional[int]    ):
           SCREAMING_SNAKE_CASE										=self.prepare_config_and_inputs()
           ((SCREAMING_SNAKE_CASE)						,  (SCREAMING_SNAKE_CASE)						,  (SCREAMING_SNAKE_CASE)						,  (SCREAMING_SNAKE_CASE)						,  (SCREAMING_SNAKE_CASE)						,  (SCREAMING_SNAKE_CASE))										=config_and_inputs
           SCREAMING_SNAKE_CASE										={'input_ids': input_ids, 'attention_mask': input_mask}
           return config, inputs_dict







@require_tf
class 		a_	(							lowerCamelCase_			,      lowerCamelCase_			,      unittest.TestCase					):



      """simple docstring"""







      __UpperCAmelCase      		=				(
          (
              TFMPNetForMaskedLM,
              TFMPNetForMultipleChoice,
              TFMPNetForQuestionAnswering,
              TFMPNetForSequenceClassification,
              TFMPNetForTokenClassification,
              TFMPNetModel,
          )
          if is_tf_available()
          else ()
      )
      __UpperCAmelCase      		=				(
          {
              'feature-extraction': TFMPNetModel,
              'fill-mask': TFMPNetForMaskedLM,
              'question-answering': TFMPNetForQuestionAnswering,
              'text-classification': TFMPNetForSequenceClassification,
              'token-classification': TFMPNetForTokenClassification,
              'zero-shot': TFMPNetForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      __UpperCAmelCase      		=				False
      __UpperCAmelCase      		=				False
      def 	_lowerCAmelCase					(				self					:							Dict    ):
           SCREAMING_SNAKE_CASE										=TFMPNetModelTester(self    )
           SCREAMING_SNAKE_CASE										=ConfigTester(self      ,config_class=snake_case      ,hidden_size=37    )
      def 	_lowerCAmelCase					(				self					:							Dict    ):
           self.config_tester.run_common_tests()
      def 	_lowerCAmelCase					(				self					:							Union[str, Any]    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_model(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							List[Any]    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_masked_lm(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							Dict    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_question_answering(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							str    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_sequence_classification(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							Dict    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_multiple_choice(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							List[str]    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_token_classification(*snake_case    )


      @slow
      def 	_lowerCAmelCase					(				self					:							Optional[Any]    ):
           for model_name in ["microsoft/mpnet-base"]:
                SCREAMING_SNAKE_CASE										=TFMPNetModel.from_pretrained(snake_case    )
                self.assertIsNotNone(snake_case    )







@require_tf
class 		a_	(							unittest.TestCase					):



      """simple docstring"""







      @slow
      def 	_lowerCAmelCase					(				self					:							Optional[Any]    ):
           SCREAMING_SNAKE_CASE										=TFMPNetModel.from_pretrained('microsoft/mpnet-base'    )
           SCREAMING_SNAKE_CASE										=tf.constant([[0, 1, 2, 3, 4, 5]]    )
           SCREAMING_SNAKE_CASE										=model(snake_case    )[0]

           SCREAMING_SNAKE_CASE										=[1, 6, 768]
           self.assertEqual(output.shape      ,snake_case    )

           SCREAMING_SNAKE_CASE										=tf.constant(
               [
                   [
                       [-0.1_067_172, 0.08_216_473, 0.0_024_543],
                       [-0.03_465_879, 0.8_354_118, -0.03_252_288],
                       [-0.06_569_476, -0.12_424_111, -0.0_494_436],
                   ]
               ]    )
           tf.debugging.assert_near(output[:, :3, :3]      ,snake_case      ,atol=1e-4    )

