'''simple docstring'''




from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
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 import (
						    TF_MODEL_FOR_PRETRAINING_MAPPING,
						    TFMobileBertForMaskedLM,
						    TFMobileBertForMultipleChoice,
						    TFMobileBertForNextSentencePrediction,
						    TFMobileBertForPreTraining,
						    TFMobileBertForQuestionAnswering,
						    TFMobileBertForSequenceClassification,
						    TFMobileBertForTokenClassification,
						    TFMobileBertModel,
						)






@require_tf
class 				a__(					lowerCAmelCase__ ,						lowerCAmelCase__ ,						unittest.TestCase	):


			'''simple docstring'''






			UpperCAmelCase_			:     Any      =       (
			    (
			        TFMobileBertModel,
			        TFMobileBertForMaskedLM,
			        TFMobileBertForNextSentencePrediction,
			        TFMobileBertForPreTraining,
			        TFMobileBertForQuestionAnswering,
			        TFMobileBertForSequenceClassification,
			        TFMobileBertForTokenClassification,
			        TFMobileBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			UpperCAmelCase_			:     int      =       (
			    {
			        '''feature-extraction''': TFMobileBertModel,
			        '''fill-mask''': TFMobileBertForMaskedLM,
			        '''question-answering''': TFMobileBertForQuestionAnswering,
			        '''text-classification''': TFMobileBertForSequenceClassification,
			        '''token-classification''': TFMobileBertForTokenClassification,
			        '''zero-shot''': TFMobileBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			UpperCAmelCase_			:     int      =       False
			UpperCAmelCase_			:     Optional[int]      =       False



			def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase=False):




					"""simple docstring"""


					lowerCAmelCase   					=  super()._prepare_for_class(__lowerCAmelCase       ,    __lowerCAmelCase       ,    return_labels=__lowerCAmelCase)

					if return_labels:
							if model_class in get_values(__lowerCAmelCase):
									lowerCAmelCase   					=  tf.zeros(self.model_tester.batch_size       ,    dtype=tf.intaa)

					return inputs_dict






			class 				a__(					lowerCAmelCase__	):


						'''simple docstring'''






						def __init__(   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase=13       ,    __lowerCAmelCase=7       ,    __lowerCAmelCase=True       ,    __lowerCAmelCase=True       ,    __lowerCAmelCase=True       ,    __lowerCAmelCase=True       ,    __lowerCAmelCase=99       ,    __lowerCAmelCase=32       ,    __lowerCAmelCase=32       ,    __lowerCAmelCase=2       ,    __lowerCAmelCase=4       ,    __lowerCAmelCase=37       ,    __lowerCAmelCase="gelu"       ,    __lowerCAmelCase=0.1       ,    __lowerCAmelCase=0.1       ,    __lowerCAmelCase=512       ,    __lowerCAmelCase=16       ,    __lowerCAmelCase=2       ,    __lowerCAmelCase=0.02       ,    __lowerCAmelCase=3       ,    __lowerCAmelCase=4       ,    __lowerCAmelCase=None       ,    ):




								"""simple docstring"""


								lowerCAmelCase   					=  parent
								lowerCAmelCase   					=  batch_size
								lowerCAmelCase   					=  seq_length
								lowerCAmelCase   					=  is_training
								lowerCAmelCase   					=  use_input_mask
								lowerCAmelCase   					=  use_token_type_ids
								lowerCAmelCase   					=  use_labels
								lowerCAmelCase   					=  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   					=  type_sequence_label_size
								lowerCAmelCase   					=  initializer_range
								lowerCAmelCase   					=  num_labels
								lowerCAmelCase   					=  num_choices
								lowerCAmelCase   					=  scope
								lowerCAmelCase   					=  embedding_size



						def 							a_       (   self):




								"""simple docstring"""


								lowerCAmelCase   					=  ids_tensor([self.batch_size, self.seq_length]       ,    self.vocab_size)

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

								lowerCAmelCase   					=  None
								if self.use_token_type_ids:
										lowerCAmelCase   					=  ids_tensor([self.batch_size, self.seq_length]       ,    self.type_vocab_size)

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

								lowerCAmelCase   					=  MobileBertConfig(
								    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       ,    embedding_size=self.embedding_size       ,    )

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



						def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




								"""simple docstring"""


								lowerCAmelCase   					=  TFMobileBertModel(config=__lowerCAmelCase)
								lowerCAmelCase   					=  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								lowerCAmelCase   					=  model(__lowerCAmelCase)

								lowerCAmelCase   					=  [input_ids, input_mask]
								lowerCAmelCase   					=  model(__lowerCAmelCase)

								lowerCAmelCase   					=  model(__lowerCAmelCase)

								self.parent.assertEqual(
								    result.last_hidden_state.shape       ,    (self.batch_size, self.seq_length, self.hidden_size))
								self.parent.assertEqual(result.pooler_output.shape       ,    (self.batch_size, self.hidden_size))



						def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




								"""simple docstring"""


								lowerCAmelCase   					=  TFMobileBertForMaskedLM(config=__lowerCAmelCase)
								lowerCAmelCase   					=  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								lowerCAmelCase   					=  model(__lowerCAmelCase)
								self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.seq_length, self.vocab_size))



						def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




								"""simple docstring"""


								lowerCAmelCase   					=  TFMobileBertForNextSentencePrediction(config=__lowerCAmelCase)
								lowerCAmelCase   					=  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								lowerCAmelCase   					=  model(__lowerCAmelCase)
								self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, 2))



						def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




								"""simple docstring"""


								lowerCAmelCase   					=  TFMobileBertForPreTraining(config=__lowerCAmelCase)
								lowerCAmelCase   					=  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								lowerCAmelCase   					=  model(__lowerCAmelCase)
								self.parent.assertEqual(
								    result.prediction_logits.shape       ,    (self.batch_size, self.seq_length, self.vocab_size))
								self.parent.assertEqual(result.seq_relationship_logits.shape       ,    (self.batch_size, 2))



						def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




								"""simple docstring"""


								lowerCAmelCase   					=  self.num_labels
								lowerCAmelCase   					=  TFMobileBertForSequenceClassification(config=__lowerCAmelCase)
								lowerCAmelCase   					=  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								lowerCAmelCase   					=  model(__lowerCAmelCase)
								self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.num_labels))



						def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




								"""simple docstring"""


								lowerCAmelCase   					=  self.num_choices
								lowerCAmelCase   					=  TFMobileBertForMultipleChoice(config=__lowerCAmelCase)
								lowerCAmelCase   					=  tf.tile(tf.expand_dims(__lowerCAmelCase       ,    1)       ,    (1, self.num_choices, 1))
								lowerCAmelCase   					=  tf.tile(tf.expand_dims(__lowerCAmelCase       ,    1)       ,    (1, self.num_choices, 1))
								lowerCAmelCase   					=  tf.tile(tf.expand_dims(__lowerCAmelCase       ,    1)       ,    (1, self.num_choices, 1))
								lowerCAmelCase   					=  {
								    """input_ids""": multiple_choice_inputs_ids,
								    """attention_mask""": multiple_choice_input_mask,
								    """token_type_ids""": multiple_choice_token_type_ids,
								}
								lowerCAmelCase   					=  model(__lowerCAmelCase)
								self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.num_choices))



						def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




								"""simple docstring"""


								lowerCAmelCase   					=  self.num_labels
								lowerCAmelCase   					=  TFMobileBertForTokenClassification(config=__lowerCAmelCase)
								lowerCAmelCase   					=  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								lowerCAmelCase   					=  model(__lowerCAmelCase)
								self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.seq_length, self.num_labels))



						def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




								"""simple docstring"""


								lowerCAmelCase   					=  TFMobileBertForQuestionAnswering(config=__lowerCAmelCase)
								lowerCAmelCase   					=  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								lowerCAmelCase   					=  model(__lowerCAmelCase)
								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 							a_       (   self):




								"""simple docstring"""


								lowerCAmelCase   					=  self.prepare_config_and_inputs()
								(
								    (
								    lowerCAmelCase
								)					,     (
								    lowerCAmelCase
								)					,     (
								    lowerCAmelCase
								)					,     (
								    lowerCAmelCase
								)					,     (
								    lowerCAmelCase
								)					,     (
								    lowerCAmelCase
								)					,     (
								    lowerCAmelCase
								)					,     
								)   					=  config_and_inputs
								lowerCAmelCase   					=  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
								return config, inputs_dict







			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  TFMobileBertModelTest.TFMobileBertModelTester(self)
					lowerCAmelCase   					=  ConfigTester(self       ,    config_class=__lowerCAmelCase       ,    hidden_size=37)



			def 							a_       (   self):




					"""simple docstring"""


					self.config_tester.run_common_tests()



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_model(*__lowerCAmelCase)



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_masked_lm(*__lowerCAmelCase)



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__lowerCAmelCase)



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__lowerCAmelCase)



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_pretraining(*__lowerCAmelCase)



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_question_answering(*__lowerCAmelCase)



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__lowerCAmelCase)



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_mobilebert_for_token_classification(*__lowerCAmelCase)







			@slow
			def 							a_       (   self):




					"""simple docstring"""


					for model_name in ["google/mobilebert-uncased"]:
							lowerCAmelCase   					=  TFMobileBertModel.from_pretrained(__lowerCAmelCase)
							self.assertIsNotNone(__lowerCAmelCase)







@require_tf
class 				a__(					unittest.TestCase	):


			'''simple docstring'''






			@slow
			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased""")
					lowerCAmelCase   					=  tf.constant([[0, 1, 2, 3, 4, 5]])
					lowerCAmelCase   					=  model(__lowerCAmelCase)[0]

					lowerCAmelCase   					=  [1, 6, 30522]
					self.assertEqual(output.shape       ,    __lowerCAmelCase)

					lowerCAmelCase   					=  tf.constant(
					    [
					        [
					            [-4.5919547, -9.248295, -9.645256],
					            [-6.7306175, -6.440284, -6.6052837],
					            [-7.2743506, -6.7847915, -6.024673],
					        ]
					    ])
					tf.debugging.assert_near(output[:, :3, :3]       ,    __lowerCAmelCase       ,    atol=1E-4)





'''simple docstring'''




import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()






class 				a__(					unittest.TestCase	):


			'''simple docstring'''






			@property
			def 							a_       (   self):




					"""simple docstring"""


					torch.manual_seed(0)
					lowerCAmelCase   					=  UNetaDModel(
					    block_out_channels=(32, 64)       ,    layers_per_block=2       ,    sample_size=32       ,    in_channels=3       ,    out_channels=3       ,    down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")       ,    up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")       ,    )
					return model







			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.dummy_uncond_unet
					lowerCAmelCase   					=  PNDMScheduler()

					lowerCAmelCase   					=  PNDMPipeline(unet=__lowerCAmelCase       ,    scheduler=__lowerCAmelCase)
					pndm.to(__lowerCAmelCase)
					pndm.set_progress_bar_config(disable=__lowerCAmelCase)

					lowerCAmelCase   					=  torch.manual_seed(0)
					lowerCAmelCase   					=  pndm(generator=__lowerCAmelCase       ,    num_inference_steps=20       ,    output_type="""numpy""").images

					lowerCAmelCase   					=  torch.manual_seed(0)
					lowerCAmelCase   					=  pndm(generator=__lowerCAmelCase       ,    num_inference_steps=20       ,    output_type="""numpy"""       ,    return_dict=__lowerCAmelCase)[0]

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

					assert image.shape == (1, 32, 32, 3)
					lowerCAmelCase   					=  np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0])

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







@slow
@require_torch
class 				a__(					unittest.TestCase	):


			'''simple docstring'''






			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  """google/ddpm-cifar10-32"""

					lowerCAmelCase   					=  UNetaDModel.from_pretrained(__lowerCAmelCase)
					lowerCAmelCase   					=  PNDMScheduler()

					lowerCAmelCase   					=  PNDMPipeline(unet=__lowerCAmelCase       ,    scheduler=__lowerCAmelCase)
					pndm.to(__lowerCAmelCase)
					pndm.set_progress_bar_config(disable=__lowerCAmelCase)
					lowerCAmelCase   					=  torch.manual_seed(0)
					lowerCAmelCase   					=  pndm(generator=__lowerCAmelCase       ,    output_type="""numpy""").images

					lowerCAmelCase   					=  image[0, -3:, -3:, -1]

					assert image.shape == (1, 32, 32, 3)
					lowerCAmelCase   					=  np.array([0.1564, 0.14645, 0.1406, 0.14715, 0.12425, 0.14045, 0.13115, 0.12175, 0.125])

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




