
from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
	from transformers import (
	    TFAutoModel,
	    TFAutoModelForCausalLM,
	    TFAutoModelForMaskedLM,
	    TFAutoModelForPreTraining,
	    TFAutoModelForQuestionAnswering,
	    TFAutoModelForSeqaSeqLM,
	    TFAutoModelForSequenceClassification,
	    TFAutoModelForTableQuestionAnswering,
	    TFAutoModelForTokenClassification,
	    TFAutoModelWithLMHead,
	    TFBertForMaskedLM,
	    TFBertForPreTraining,
	    TFBertForQuestionAnswering,
	    TFBertForSequenceClassification,
	    TFBertModel,
	    TFFunnelBaseModel,
	    TFFunnelModel,
	    TFGPTaLMHeadModel,
	    TFRobertaForMaskedLM,
	    TFTaForConditionalGeneration,
	    TFTapasForQuestionAnswering,
	)
	from transformers.models.auto.modeling_tf_auto import (
	    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
	    TF_MODEL_FOR_MASKED_LM_MAPPING,
	    TF_MODEL_FOR_PRETRAINING_MAPPING,
	    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
	    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
	    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
	    TF_MODEL_MAPPING,
	)
	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
	from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST







class 			A_  (     __lowerCamelCase  ):




	'''simple docstring'''


	_UpperCamelCase	:	Dict									=			"""new-model"""


if is_tf_available():







	class 			A_  (     __lowerCamelCase  ):




		'''simple docstring'''


		_UpperCamelCase	:	Tuple									=			NewModelConfig







@require_tf
class 			A_  (     unittest.TestCase  ):




	'''simple docstring'''


	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       'bert-base-cased'
		lowercase	    =       AutoConfig.from_pretrained(snake_case			)
		self.assertIsNotNone(snake_case			)
		self.assertIsInstance(snake_case						,       snake_case			)

		lowercase	    =       TFAutoModel.from_pretrained(snake_case			)
		self.assertIsNotNone(snake_case			)
		self.assertIsInstance(snake_case						,       snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       'bert-base-cased'
		lowercase	    =       AutoConfig.from_pretrained(snake_case			)
		self.assertIsNotNone(snake_case			)
		self.assertIsInstance(snake_case						,       snake_case			)

		lowercase	    =       TFAutoModelForPreTraining.from_pretrained(snake_case			)
		self.assertIsNotNone(snake_case			)
		self.assertIsInstance(snake_case						,       snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowercase	    =       AutoConfig.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)

			lowercase	    =       TFAutoModelForCausalLM.from_pretrained(snake_case			)
			lowercase    ,   lowercase	    =       TFAutoModelForCausalLM.from_pretrained(snake_case						,       output_loading_info=snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowercase	    =       AutoConfig.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)

			lowercase	    =       TFAutoModelWithLMHead.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowercase	    =       AutoConfig.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)

			lowercase	    =       TFAutoModelForMaskedLM.from_pretrained(snake_case			)
			lowercase    ,   lowercase	    =       TFAutoModelForMaskedLM.from_pretrained(snake_case						,       output_loading_info=snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowercase	    =       AutoConfig.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)

			lowercase	    =       TFAutoModelForSeqaSeqLM.from_pretrained(snake_case			)
			lowercase    ,   lowercase	    =       TFAutoModelForSeqaSeqLM.from_pretrained(snake_case						,       output_loading_info=snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
		for model_name in ["bert-base-uncased"]:
			lowercase	    =       AutoConfig.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)

			lowercase	    =       TFAutoModelForSequenceClassification.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
		for model_name in ["bert-base-uncased"]:
			lowercase	    =       AutoConfig.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)

			lowercase	    =       TFAutoModelForQuestionAnswering.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)





	@slow
	@require_tensorflow_probability
	def       SCREAMING_SNAKE_CASE__		(			self			):
		for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
			lowercase	    =       AutoConfig.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)

			lowercase	    =       TFAutoModelForTableQuestionAnswering.from_pretrained(snake_case			)
			lowercase    ,   lowercase	    =       TFAutoModelForTableQuestionAnswering.from_pretrained(
			    snake_case						,       output_loading_info=snake_case			)
			self.assertIsNotNone(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       TFAutoModelWithLMHead.from_pretrained(snake_case			)
		self.assertIsInstance(snake_case						,       snake_case			)
		self.assertEqual(model.num_parameters()						,       1_4410			)
		self.assertEqual(model.num_parameters(only_trainable=snake_case			)						,       1_4410			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       TFAutoModelWithLMHead.from_pretrained(snake_case			)
		self.assertIsInstance(snake_case						,       snake_case			)
		self.assertEqual(model.num_parameters()						,       1_4410			)
		self.assertEqual(model.num_parameters(only_trainable=snake_case			)						,       1_4410			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		# For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel
		lowercase	    =       TFAutoModel.from_pretrained('sgugger/funnel-random-tiny'			)
		self.assertIsInstance(snake_case						,       snake_case			)

		lowercase	    =       copy.deepcopy(model.config			)
		lowercase	    =       ['FunnelBaseModel']
		lowercase	    =       TFAutoModel.from_config(snake_case			)
		self.assertIsInstance(snake_case						,       snake_case			)

		with tempfile.TemporaryDirectory() as tmp_dir:
			model.save_pretrained(snake_case			)
			lowercase	    =       TFAutoModel.from_pretrained(snake_case			)
			self.assertIsInstance(snake_case						,       snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		try:
			AutoConfig.register('new-model'						,       snake_case			)

			lowercase	    =       [
			    TFAutoModel,
			    TFAutoModelForCausalLM,
			    TFAutoModelForMaskedLM,
			    TFAutoModelForPreTraining,
			    TFAutoModelForQuestionAnswering,
			    TFAutoModelForSequenceClassification,
			    TFAutoModelForTokenClassification,
			]

			for auto_class in auto_classes:
				with self.subTest(auto_class.__name__			):
					# Wrong config class will raise an error
					with self.assertRaises(snake_case			):
						auto_class.register(snake_case						,       snake_case			)
					auto_class.register(snake_case						,       snake_case			)
					# Trying to register something existing in the Transformers library will raise an error
					with self.assertRaises(snake_case			):
						auto_class.register(snake_case						,       snake_case			)

					# Now that the config is registered, it can be used as any other config with the auto-API
					lowercase	    =       BertModelTester(self			).get_config()
					lowercase	    =       NewModelConfig(**tiny_config.to_dict()			)
					lowercase	    =       auto_class.from_config(snake_case			)
					self.assertIsInstance(snake_case						,       snake_case			)

					with tempfile.TemporaryDirectory() as tmp_dir:
						model.save_pretrained(snake_case			)
						lowercase	    =       auto_class.from_pretrained(snake_case			)
						self.assertIsInstance(snake_case						,       snake_case			)

		finally:
			if "new-model" in CONFIG_MAPPING._extra_content:
				del CONFIG_MAPPING._extra_content["new-model"]
			for mapping in (
			    TF_MODEL_MAPPING,
			    TF_MODEL_FOR_PRETRAINING_MAPPING,
			    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
			    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
			    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
			    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
			    TF_MODEL_FOR_MASKED_LM_MAPPING,
			):
				if NewModelConfig in mapping._extra_content:
					del mapping._extra_content[NewModelConfig]





	def       SCREAMING_SNAKE_CASE__		(			self			):
		with self.assertRaisesRegex(
		    snake_case						,       'bert-base is not a local folder and is not a valid model identifier'			):
			lowercase	    =       TFAutoModel.from_pretrained('bert-base'			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		with self.assertRaisesRegex(
		    snake_case						,       r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'			):
			lowercase	    =       TFAutoModel.from_pretrained(snake_case						,       revision='aaaaaa'			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		with self.assertRaisesRegex(
		    snake_case						,       'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin'						,       ):
			lowercase	    =       TFAutoModel.from_pretrained('hf-internal-testing/config-no-model'			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		with self.assertRaisesRegex(snake_case						,       'Use `from_pt=True` to load this model'			):
			lowercase	    =       TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only'			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		# Make sure we have cached the model.
		lowercase	    =       TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert'			)
		with RequestCounter() as counter:
			lowercase	    =       TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert'			)
			self.assertEqual(counter.get_request_count						,       0			)
			self.assertEqual(counter.head_request_count						,       1			)
			self.assertEqual(counter.other_request_count						,       0			)

		# With a sharded checkpoint
		lowercase	    =       TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded'			)
		with RequestCounter() as counter:
			lowercase	    =       TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded'			)
			self.assertEqual(counter.get_request_count						,       0			)
			self.assertEqual(counter.head_request_count						,       1			)
			self.assertEqual(counter.other_request_count						,       0			)




import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()



class        A__(a_,  a_,  unittest.TestCase							):
						"""simple docstring"""






						_A    :				Optional[Any]       					=			StableDiffusionSAGPipeline
						_A    :				Any       					=			TEXT_TO_IMAGE_PARAMS
						_A    :				Dict       					=			TEXT_TO_IMAGE_BATCH_PARAMS
						_A    :				Dict       					=			TEXT_TO_IMAGE_IMAGE_PARAMS
						_A    :				int       					=			TEXT_TO_IMAGE_IMAGE_PARAMS
						_A    :				Optional[int]       					=			False





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									torch.manual_seed(0    )
									a_						:						Tuple 							= 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  ,      )
									a_						:						int 							= DDIMScheduler(
									    beta_start=0.0_0_0_8_5  ,      beta_end=0.0_1_2  ,      beta_schedule="""scaled_linear"""  ,      clip_sample=_lowercase  ,      set_alpha_to_one=_lowercase  ,      )
									torch.manual_seed(0    )
									a_						:						Tuple 							= 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  ,      )
									torch.manual_seed(0    )
									a_						:						str 							= 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=1_000  ,      )
									a_						:						Union[str, Any] 							= CLIPTextModel(_lowercase    )
									a_						:						int 							= CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

									a_						:						Union[str, Any] 							= {
									    """unet""": unet,
									    """scheduler""": scheduler,
									    """vae""": vae,
									    """text_encoder""": text_encoder,
									    """tokenizer""": tokenizer,
									    """safety_checker""": None,
									    """feature_extractor""": None,
									}
									return components





						def 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase=0    )     -> Dict:
									if str(_lowercase    ).startswith("""mps"""    ):
												a_						:						Optional[int] 							= torch.manual_seed(_lowercase    )
									else:
												a_						:						str 							= torch.Generator(device=_lowercase    ).manual_seed(_lowercase    )
									a_						:						Tuple 							= {
									    """prompt""": """.""",
									    """generator""": generator,
									    """num_inference_steps""": 2,
									    """guidance_scale""": 1.0,
									    """sag_scale""": 1.0,
									    """output_type""": """numpy""",
									}
									return inputs





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									super().test_inference_batch_single_identical(expected_max_diff=3e-3    )







@slow
@require_torch_gpu
class        A__(unittest.TestCase							):
						"""simple docstring"""






						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									# clean up the VRAM after each test
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						Tuple 							= StableDiffusionSAGPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""    )
									a_						:						List[str] 							= sag_pipe.to(_lowercase    )
									sag_pipe.set_progress_bar_config(disable=_lowercase    )

									a_						:						Optional[int] 							= """."""
									a_						:						int 							= torch.manual_seed(0    )
									a_						:						Any 							= sag_pipe(
									    [prompt]  ,      generator=_lowercase  ,      guidance_scale=7.5  ,      sag_scale=1.0  ,      num_inference_steps=20  ,      output_type="""np"""    )

									a_						:						Any 							= output.images

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

									assert image.shape == (1, 512, 512, 3)
									a_						:						str 							= np.array([0.1_5_6_8, 0.1_7_3_8, 0.1_6_9_5, 0.1_6_9_3, 0.1_5_0_7, 0.1_7_0_5, 0.1_5_4_7, 0.1_7_5_1, 0.1_9_4_9]    )

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





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									a_						:						Optional[int] 							= StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""    )
									a_						:						List[str] 							= sag_pipe.to(_lowercase    )
									sag_pipe.set_progress_bar_config(disable=_lowercase    )

									a_						:						int 							= """."""
									a_						:						Dict 							= torch.manual_seed(0    )
									a_						:						Union[str, Any] 							= sag_pipe(
									    [prompt]  ,      generator=_lowercase  ,      guidance_scale=7.5  ,      sag_scale=1.0  ,      num_inference_steps=20  ,      output_type="""np"""    )

									a_						:						Optional[Any] 							= output.images

									a_						:						int 							= image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									a_						:						Dict 							= np.array([0.3_4_5_9, 0.2_8_7_6, 0.2_5_3_7, 0.3_0_0_2, 0.2_6_7_1, 0.2_1_6_0, 0.3_0_2_6, 0.2_2_6_2, 0.2_3_7_1]    )

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





						def 			UpperCamelCase__					(    self    )     -> Any:
									a_						:						int 							= StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""    )
									a_						:						Optional[Any] 							= sag_pipe.to(_lowercase    )
									sag_pipe.set_progress_bar_config(disable=_lowercase    )

									a_						:						List[Any] 							= """."""
									a_						:						str 							= torch.manual_seed(0    )
									a_						:						int 							= sag_pipe(
									    [prompt]  ,      width=768  ,      height=512  ,      generator=_lowercase  ,      guidance_scale=7.5  ,      sag_scale=1.0  ,      num_inference_steps=20  ,      output_type="""np"""  ,      )

									a_						:						Any 							= output.images

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


