



"""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 				lowerCamelCase  (			unittest.TestCase				):






					'''simple docstring'''





					@property
					def    lowerCAmelCase_				(       self:							str						)   ->	Union[str, Any]:
											torch.manual_seed(0						)
											snake_case_ :str						=			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    lowerCAmelCase_				(       self:							List[str]						)   ->	str:
											snake_case_ :Union[str, Any]						=			self.dummy_uncond_unet
											snake_case_ :Tuple						=			PNDMScheduler()

											snake_case_ :str						=			PNDMPipeline(unet=snake_case						,     scheduler=snake_case						)
											pndm.to(snake_case						)
											pndm.set_progress_bar_config(disable=snake_case						)

											snake_case_ :int						=			torch.manual_seed(0						)
											snake_case_ :List[Any]						=			pndm(generator=snake_case						,     num_inference_steps=20						,     output_type="""numpy"""						).images

											snake_case_ :Optional[Any]						=			torch.manual_seed(0						)
											snake_case_ :Union[str, Any]						=			pndm(generator=snake_case						,     num_inference_steps=20						,     output_type="""numpy"""						,     return_dict=snake_case						)[0]

											snake_case_ :Optional[int]						=			image[0, -3:, -3:, -1]
											snake_case_ :str						=			image_from_tuple[0, -3:, -3:, -1]

											assert image.shape == (1, 32, 32, 3)
											snake_case_ :List[str]						=			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 				lowerCamelCase  (			unittest.TestCase				):






					'''simple docstring'''





					def    lowerCAmelCase_				(       self:							List[Any]						)   ->	Union[str, Any]:
											snake_case_ :int						=			"""google/ddpm-cifar10-32"""

											snake_case_ :List[str]						=			UNetaDModel.from_pretrained(snake_case						)
											snake_case_ :Any						=			PNDMScheduler()

											snake_case_ :Tuple						=			PNDMPipeline(unet=snake_case						,     scheduler=snake_case						)
											pndm.to(snake_case						)
											pndm.set_progress_bar_config(disable=snake_case						)
											snake_case_ :str						=			torch.manual_seed(0						)
											snake_case_ :Any						=			pndm(generator=snake_case						,     output_type="""numpy"""						).images

											snake_case_ :Optional[int]						=			image[0, -3:, -3:, -1]

											assert image.shape == (1, 32, 32, 3)
											snake_case_ :Optional[int]						=			np.array([0.1_5_6_4, 0.1_4_6_4_5, 0.1_4_0_6, 0.1_4_7_1_5, 0.1_2_4_2_5, 0.1_4_0_4_5, 0.1_3_1_1_5, 0.1_2_1_7_5, 0.1_2_5]						)

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





"""simple docstring"""







from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
							from transformers import (
							    AutoConfig,
							    BertConfig,
							    GPTaConfig,
							    TaConfig,
							    TFAutoModel,
							    TFAutoModelForCausalLM,
							    TFAutoModelForMaskedLM,
							    TFAutoModelForPreTraining,
							    TFAutoModelForQuestionAnswering,
							    TFAutoModelForSeqaSeqLM,
							    TFAutoModelForSequenceClassification,
							    TFAutoModelWithLMHead,
							    TFBertForMaskedLM,
							    TFBertForPreTraining,
							    TFBertForQuestionAnswering,
							    TFBertForSequenceClassification,
							    TFBertModel,
							    TFGPTaLMHeadModel,
							    TFRobertaForMaskedLM,
							    TFTaForConditionalGeneration,
							)
							from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
							from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
							from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
							from transformers import (
							    AutoModel,
							    AutoModelForCausalLM,
							    AutoModelForMaskedLM,
							    AutoModelForPreTraining,
							    AutoModelForQuestionAnswering,
							    AutoModelForSeqaSeqLM,
							    AutoModelForSequenceClassification,
							    AutoModelWithLMHead,
							    BertForMaskedLM,
							    BertForPreTraining,
							    BertForQuestionAnswering,
							    BertForSequenceClassification,
							    BertModel,
							    GPTaLMHeadModel,
							    RobertaForMaskedLM,
							    TaForConditionalGeneration,
							)
@is_pt_tf_cross_test
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





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






				"""simple docstring"""



				for model_name in ["bert-base-uncased"]:
						lowercase__   :    Tuple				=			AutoConfig.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    Dict				=			TFAutoModel.from_pretrained(_snake_case			,from_pt=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    List[str]				=			AutoModel.from_pretrained(_snake_case			,from_tf=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)







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






				"""simple docstring"""



				for model_name in ["bert-base-uncased"]:
						lowercase__   :    Dict				=			AutoConfig.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    str				=			TFAutoModelForPreTraining.from_pretrained(_snake_case			,from_pt=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    Optional[Any]				=			AutoModelForPreTraining.from_pretrained(_snake_case			,from_tf=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)







		@slow
		def 					UpperCAmelCase    (	self						:  Tuple						)					->		Dict:






				"""simple docstring"""



				for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase__   :    Any				=			AutoConfig.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    List[str]				=			TFAutoModelForCausalLM.from_pretrained(_snake_case			,from_pt=_snake_case						)
						lowercase__		,			lowercase__   :    Optional[Any]				=			TFAutoModelForCausalLM.from_pretrained(
						    _snake_case			,output_loading_info=_snake_case			,from_pt=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    Union[str, Any]				=			AutoModelForCausalLM.from_pretrained(_snake_case			,from_tf=_snake_case						)
						lowercase__		,			lowercase__   :    Optional[Any]				=			AutoModelForCausalLM.from_pretrained(
						    _snake_case			,output_loading_info=_snake_case			,from_tf=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)







		@slow
		def 					UpperCAmelCase    (	self						:  Any						)					->		Tuple:






				"""simple docstring"""



				for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase__   :    Any				=			AutoConfig.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    Optional[Any]				=			TFAutoModelWithLMHead.from_pretrained(_snake_case			,from_pt=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    Any				=			AutoModelWithLMHead.from_pretrained(_snake_case			,from_tf=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)







		@slow
		def 					UpperCAmelCase    (	self						:  List[str]						)					->		Optional[Any]:






				"""simple docstring"""



				for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase__   :    str				=			AutoConfig.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    Union[str, Any]				=			TFAutoModelForMaskedLM.from_pretrained(_snake_case			,from_pt=_snake_case						)
						lowercase__		,			lowercase__   :    str				=			TFAutoModelForMaskedLM.from_pretrained(
						    _snake_case			,output_loading_info=_snake_case			,from_pt=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    List[str]				=			AutoModelForMaskedLM.from_pretrained(_snake_case			,from_tf=_snake_case						)
						lowercase__		,			lowercase__   :    Any				=			AutoModelForMaskedLM.from_pretrained(
						    _snake_case			,output_loading_info=_snake_case			,from_tf=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)







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






				"""simple docstring"""



				for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase__   :    Union[str, Any]				=			AutoConfig.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    List[str]				=			TFAutoModelForSeqaSeqLM.from_pretrained(_snake_case			,from_pt=_snake_case						)
						lowercase__		,			lowercase__   :    List[str]				=			TFAutoModelForSeqaSeqLM.from_pretrained(
						    _snake_case			,output_loading_info=_snake_case			,from_pt=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    Any				=			AutoModelForSeqaSeqLM.from_pretrained(_snake_case			,from_tf=_snake_case						)
						lowercase__		,			lowercase__   :    Optional[int]				=			AutoModelForSeqaSeqLM.from_pretrained(
						    _snake_case			,output_loading_info=_snake_case			,from_tf=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)







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






				"""simple docstring"""



				for model_name in ["bert-base-uncased"]:
						lowercase__   :    Tuple				=			AutoConfig.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    Any				=			TFAutoModelForSequenceClassification.from_pretrained(_snake_case			,from_pt=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    Union[str, Any]				=			AutoModelForSequenceClassification.from_pretrained(_snake_case			,from_tf=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)







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






				"""simple docstring"""



				for model_name in ["bert-base-uncased"]:
						lowercase__   :    List[Any]				=			AutoConfig.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    str				=			TFAutoModelForQuestionAnswering.from_pretrained(_snake_case			,from_pt=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)

						lowercase__   :    Any				=			AutoModelForQuestionAnswering.from_pretrained(_snake_case			,from_tf=_snake_case						)
						self.assertIsNotNone(_snake_case						)
						self.assertIsInstance(_snake_case			,_snake_case						)







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			TFAutoModelWithLMHead.from_pretrained(_snake_case			,from_pt=_snake_case						)
				self.assertIsInstance(_snake_case			,_snake_case						)
				self.assertEqual(model.num_parameters()			,14_410						)
				self.assertEqual(model.num_parameters(only_trainable=_snake_case						)			,14_410						)

				lowercase__   :    Union[str, Any]				=			AutoModelWithLMHead.from_pretrained(_snake_case			,from_tf=_snake_case						)
				self.assertIsInstance(_snake_case			,_snake_case						)
				self.assertEqual(model.num_parameters()			,14_410						)
				self.assertEqual(model.num_parameters(only_trainable=_snake_case						)			,14_410						)







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






				"""simple docstring"""



				lowercase__   :    List[Any]				=			TFAutoModelWithLMHead.from_pretrained(_snake_case			,from_pt=_snake_case						)
				self.assertIsInstance(_snake_case			,_snake_case						)
				self.assertEqual(model.num_parameters()			,14_410						)
				self.assertEqual(model.num_parameters(only_trainable=_snake_case						)			,14_410						)

				lowercase__   :    int				=			AutoModelWithLMHead.from_pretrained(_snake_case			,from_tf=_snake_case						)
				self.assertIsInstance(_snake_case			,_snake_case						)
				self.assertEqual(model.num_parameters()			,14_410						)
				self.assertEqual(model.num_parameters(only_trainable=_snake_case						)			,14_410						)
