



"""simple docstring"""
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 			snake_case_						(						a_				):
							__lowerCAmelCase    					=					"new-model"


if is_tf_available():




					class 			snake_case_						(						a_				):
												__lowerCAmelCase    					=					NewModelConfig




@require_tf
class 			snake_case_						(						unittest.TestCase				):






							@slow
							def snake_case_  (   self      ):
									a_     :      Dict 						=     "bert-base-cased"
									a_     :      str 						=     AutoConfig.from_pretrained(a_      )
									self.assertIsNotNone(a_      )
									self.assertIsInstance(a_			,     a_      )

									a_     :      Optional[Any] 						=     TFAutoModel.from_pretrained(a_      )
									self.assertIsNotNone(a_      )
									self.assertIsInstance(a_			,     a_      )






							@slow
							def snake_case_  (   self      ):
									a_     :      List[Any] 						=     "bert-base-cased"
									a_     :      Dict 						=     AutoConfig.from_pretrained(a_      )
									self.assertIsNotNone(a_      )
									self.assertIsInstance(a_			,     a_      )

									a_     :      Tuple 						=     TFAutoModelForPreTraining.from_pretrained(a_      )
									self.assertIsNotNone(a_      )
									self.assertIsInstance(a_			,     a_      )






							@slow
							def snake_case_  (   self      ):
									for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											a_     :      int 						=     AutoConfig.from_pretrained(a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )

											a_     :      Optional[int] 						=     TFAutoModelForCausalLM.from_pretrained(a_      )
											a_						,				a_     :      Tuple 						=     TFAutoModelForCausalLM.from_pretrained(a_			,     output_loading_info=a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )






							@slow
							def snake_case_  (   self      ):
									for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											a_     :      Dict 						=     AutoConfig.from_pretrained(a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )

											a_     :      Any 						=     TFAutoModelWithLMHead.from_pretrained(a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )






							@slow
							def snake_case_  (   self      ):
									for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											a_     :      str 						=     AutoConfig.from_pretrained(a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )

											a_     :      Union[str, Any] 						=     TFAutoModelForMaskedLM.from_pretrained(a_      )
											a_						,				a_     :      int 						=     TFAutoModelForMaskedLM.from_pretrained(a_			,     output_loading_info=a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )






							@slow
							def snake_case_  (   self      ):
									for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											a_     :      Union[str, Any] 						=     AutoConfig.from_pretrained(a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )

											a_     :      List[Any] 						=     TFAutoModelForSeqaSeqLM.from_pretrained(a_      )
											a_						,				a_     :      Union[str, Any] 						=     TFAutoModelForSeqaSeqLM.from_pretrained(a_			,     output_loading_info=a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )






							@slow
							def snake_case_  (   self      ):
									# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									for model_name in ["bert-base-uncased"]:
											a_     :      List[Any] 						=     AutoConfig.from_pretrained(a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )

											a_     :      str 						=     TFAutoModelForSequenceClassification.from_pretrained(a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )






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

											a_     :      Dict 						=     TFAutoModelForQuestionAnswering.from_pretrained(a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )






							@slow
							@require_tensorflow_probability
							def snake_case_  (   self      ):
									for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
											a_     :      Any 						=     AutoConfig.from_pretrained(a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )

											a_     :      Optional[int] 						=     TFAutoModelForTableQuestionAnswering.from_pretrained(a_      )
											a_						,				a_     :      Optional[Any] 						=     TFAutoModelForTableQuestionAnswering.from_pretrained(
											    a_			,     output_loading_info=a_      )
											self.assertIsNotNone(a_      )
											self.assertIsInstance(a_			,     a_      )






							def snake_case_  (   self      ):
									a_     :      Tuple 						=     TFAutoModelWithLMHead.from_pretrained(a_      )
									self.assertIsInstance(a_			,     a_      )
									self.assertEqual(model.num_parameters()			,     1_4_4_1_0      )
									self.assertEqual(model.num_parameters(only_trainable=a_      )			,     1_4_4_1_0      )






							def snake_case_  (   self      ):
									a_     :      List[Any] 						=     TFAutoModelWithLMHead.from_pretrained(a_      )
									self.assertIsInstance(a_			,     a_      )
									self.assertEqual(model.num_parameters()			,     1_4_4_1_0      )
									self.assertEqual(model.num_parameters(only_trainable=a_      )			,     1_4_4_1_0      )






							def snake_case_  (   self      ):
									# For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel
									a_     :      int 						=     TFAutoModel.from_pretrained("sgugger/funnel-random-tiny"      )
									self.assertIsInstance(a_			,     a_      )

									a_     :      List[str] 						=     copy.deepcopy(model.config      )
									a_     :      List[Any] 						=     ["FunnelBaseModel"]
									a_     :      str 						=     TFAutoModel.from_config(a_      )
									self.assertIsInstance(a_			,     a_      )

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






							def snake_case_  (   self      ):
									try:
											AutoConfig.register("new-model"			,     a_      )

											a_     :      Optional[int] 						=     [
											    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(a_      ):
																	auto_class.register(a_			,     a_      )
															auto_class.register(a_			,     a_      )
															# Trying to register something existing in the Transformers library will raise an error
															with self.assertRaises(a_      ):
																	auto_class.register(a_			,     a_      )

															# Now that the config is registered, it can be used as any other config with the auto-API
															a_     :      Optional[int] 						=     BertModelTester(self      ).get_config()
															a_     :      Any 						=     NewModelConfig(**tiny_config.to_dict()      )
															a_     :      List[str] 						=     auto_class.from_config(a_      )
															self.assertIsInstance(a_			,     a_      )

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

									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 snake_case_  (   self      ):
									with self.assertRaisesRegex(
									    a_			,     "bert-base is not a local folder and is not a valid model identifier"      ):
											a_     :      Any 						=     TFAutoModel.from_pretrained("bert-base"      )






							def snake_case_  (   self      ):
									with self.assertRaisesRegex(
									    a_			,     R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"      ):
											a_     :      Optional[int] 						=     TFAutoModel.from_pretrained(a_			,     revision="aaaaaa"      )






							def snake_case_  (   self      ):
									with self.assertRaisesRegex(
									    a_			,     "hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin"			,     ):
											a_     :      List[Any] 						=     TFAutoModel.from_pretrained("hf-internal-testing/config-no-model"      )






							def snake_case_  (   self      ):
									with self.assertRaisesRegex(a_			,     "Use `from_pt=True` to load this model"      ):
											a_     :      Tuple 						=     TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only"      )






							def snake_case_  (   self      ):
									# Make sure we have cached the model.
									a_     :      Optional[int] 						=     TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"      )
									with RequestCounter() as counter:
											a_     :      Optional[Any] 						=     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
									a_     :      Any 						=     TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"      )
									with RequestCounter() as counter:
											a_     :      List[str] 						=     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      )




"""simple docstring"""
import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()




class 			snake_case_						(						a_     ,unittest.TestCase				):
							__lowerCAmelCase    					=					KandinskyImgaImgPipeline
							__lowerCAmelCase    					=					["prompt", "image_embeds", "negative_image_embeds", "image"]
							__lowerCAmelCase    					=					[
							    "prompt",
							    "negative_prompt",
							    "image_embeds",
							    "negative_image_embeds",
							    "image",
							]
							__lowerCAmelCase    					=					[
							    "generator",
							    "height",
							    "width",
							    "strength",
							    "guidance_scale",
							    "negative_prompt",
							    "num_inference_steps",
							    "return_dict",
							    "guidance_scale",
							    "num_images_per_prompt",
							    "output_type",
							    "return_dict",
							]
							__lowerCAmelCase    					=					False






							@property
							def snake_case_  (   self      ):
									return 3_2






							@property
							def snake_case_  (   self      ):
									return 3_2






							@property
							def snake_case_  (   self      ):
									return self.time_input_dim






							@property
							def snake_case_  (   self      ):
									return self.time_input_dim * 4






							@property
							def snake_case_  (   self      ):
									return 1_0_0






							@property
							def snake_case_  (   self      ):
									a_     :      List[str] 						=     XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base"      )
									return tokenizer






							@property
							def snake_case_  (   self      ):
									torch.manual_seed(0      )
									a_     :      List[Any] 						=     MCLIPConfig(
									    numDims=self.cross_attention_dim			,     transformerDimensions=self.text_embedder_hidden_size			,     hidden_size=self.text_embedder_hidden_size			,     intermediate_size=3_7			,     num_attention_heads=4			,     num_hidden_layers=5			,     vocab_size=1_0_0_5			,     )

									a_     :      str 						=     MultilingualCLIP(a_      )
									a_     :      Any 						=     text_encoder.eval()

									return text_encoder






							@property
							def snake_case_  (   self      ):
									torch.manual_seed(0      )

									a_     :      Union[str, Any] 						=     {
									    "in_channels": 4,
									    # Out channels is double in channels because predicts mean and variance
									    "out_channels": 8,
									    "addition_embed_type": "text_image",
									    "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"),
									    "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"),
									    "mid_block_type": "UNetMidBlock2DSimpleCrossAttn",
									    "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2),
									    "layers_per_block": 1,
									    "encoder_hid_dim": self.text_embedder_hidden_size,
									    "encoder_hid_dim_type": "text_image_proj",
									    "cross_attention_dim": self.cross_attention_dim,
									    "attention_head_dim": 4,
									    "resnet_time_scale_shift": "scale_shift",
									    "class_embed_type": None,
									}

									a_     :      Dict 						=     UNetaDConditionModel(**a_      )
									return model






							@property
							def snake_case_  (   self      ):
									return {
									    "block_out_channels": [3_2, 6_4],
									    "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
									    "in_channels": 3,
									    "latent_channels": 4,
									    "layers_per_block": 1,
									    "norm_num_groups": 8,
									    "norm_type": "spatial",
									    "num_vq_embeddings": 1_2,
									    "out_channels": 3,
									    "up_block_types": [
									        "AttnUpDecoderBlock2D",
									        "UpDecoderBlock2D",
									    ],
									    "vq_embed_dim": 4,
									}






							@property
							def snake_case_  (   self      ):
									torch.manual_seed(0      )
									a_     :      int 						=     VQModel(**self.dummy_movq_kwargs      )
									return model






							def snake_case_  (   self      ):
									a_     :      Dict 						=     self.dummy_text_encoder
									a_     :      Dict 						=     self.dummy_tokenizer
									a_     :      Optional[int] 						=     self.dummy_unet
									a_     :      Dict 						=     self.dummy_movq

									a_     :      List[str] 						=     {
									    "num_train_timesteps": 1_0_0_0,
									    "beta_schedule": "linear",
									    "beta_start": 0.00_085,
									    "beta_end": 0.012,
									    "clip_sample": False,
									    "set_alpha_to_one": False,
									    "steps_offset": 0,
									    "prediction_type": "epsilon",
									    "thresholding": False,
									}

									a_     :      List[Any] 						=     DDIMScheduler(**a_      )

									a_     :      Union[str, Any] 						=     {
									    "text_encoder": text_encoder,
									    "tokenizer": tokenizer,
									    "unet": unet,
									    "scheduler": scheduler,
									    "movq": movq,
									}

									return components






							def snake_case_  (   self			,     a_			,     a_=0      ):
									a_     :      Optional[int] 						=     floats_tensor((1, self.cross_attention_dim)			,     rng=random.Random(a_      )      ).to(a_      )
									a_     :      Tuple 						=     floats_tensor((1, self.cross_attention_dim)			,     rng=random.Random(seed + 1      )      ).to(a_      )
									# create init_image
									a_     :      Optional[int] 						=     floats_tensor((1, 3, 6_4, 6_4)			,     rng=random.Random(a_      )      ).to(a_      )
									a_     :      Any 						=     image.cpu().permute(0			,     2			,     3			,     1      )[0]
									a_     :      int 						=     Image.fromarray(np.uinta(a_      )      ).convert("RGB"      ).resize((2_5_6, 2_5_6)      )

									if str(a_      ).startswith("mps"      ):
											a_     :      Any 						=     torch.manual_seed(a_      )
									else:
											a_     :      Any 						=     torch.Generator(device=a_      ).manual_seed(a_      )
									a_     :      List[Any] 						=     {
									    "prompt": "horse",
									    "image": init_image,
									    "image_embeds": image_embeds,
									    "negative_image_embeds": negative_image_embeds,
									    "generator": generator,
									    "height": 6_4,
									    "width": 6_4,
									    "num_inference_steps": 1_0,
									    "guidance_scale": 7.0,
									    "strength": 0.2,
									    "output_type": "np",
									}
									return inputs






							def snake_case_  (   self      ):
									a_     :      Optional[Any] 						=     "cpu"

									a_     :      List[Any] 						=     self.get_dummy_components()

									a_     :      Union[str, Any] 						=     self.pipeline_class(**a_      )
									a_     :      Tuple 						=     pipe.to(a_      )

									pipe.set_progress_bar_config(disable=a_      )

									a_     :      Union[str, Any] 						=     pipe(**self.get_dummy_inputs(a_      )      )
									a_     :      Any 						=     output.images

									a_     :      str 						=     pipe(
									    **self.get_dummy_inputs(a_      )			,     return_dict=a_			,     )[0]

									a_     :      List[str] 						=     image[0, -3:, -3:, -1]
									a_     :      Optional[Any] 						=     image_from_tuple[0, -3:, -3:, -1]

									assert image.shape == (1, 6_4, 6_4, 3)

									a_     :      Optional[int] 						=     np.array(
									    [0.61_474_943, 0.6_073_539, 0.43_308_544, 0.5_928_269, 0.47_493_595, 0.46_755_973, 0.4_613_838, 0.45_368_797, 0.50_119_233]      )
									assert (
									    np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-2
									), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}"""
									assert (
									    np.abs(image_from_tuple_slice.flatten() - expected_slice      ).max() < 1e-2
									), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"""




@slow
@require_torch_gpu
class 			snake_case_						(						unittest.TestCase				):






							def snake_case_  (   self      ):
									# clean up the VRAM after each test
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()






							def snake_case_  (   self      ):
									a_     :      Optional[int] 						=     load_numpy(
									    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
									    "/kandinsky/kandinsky_img2img_frog.npy"      )

									a_     :      Union[str, Any] 						=     load_image(
									    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"      )
									a_     :      Optional[Any] 						=     "A red cartoon frog, 4k"

									a_     :      int 						=     KandinskyPriorPipeline.from_pretrained(
									    "kandinsky-community/kandinsky-2-1-prior"			,     torch_dtype=torch.floataa      )
									pipe_prior.to(a_      )

									a_     :      int 						=     KandinskyImgaImgPipeline.from_pretrained(
									    "kandinsky-community/kandinsky-2-1"			,     torch_dtype=torch.floataa      )
									a_     :      List[Any] 						=     pipeline.to(a_      )

									pipeline.set_progress_bar_config(disable=a_      )

									a_     :      int 						=     torch.Generator(device="cpu"      ).manual_seed(0      )
									a_						,				a_     :      Optional[int] 						=     pipe_prior(
									    a_			,     generator=a_			,     num_inference_steps=5			,     negative_prompt=""			,     ).to_tuple()

									a_     :      List[Any] 						=     pipeline(
									    a_			,     image=a_			,     image_embeds=a_			,     negative_image_embeds=a_			,     generator=a_			,     num_inference_steps=1_0_0			,     height=7_6_8			,     width=7_6_8			,     strength=0.2			,     output_type="np"			,     )

									a_     :      int 						=     output.images[0]

									assert image.shape == (7_6_8, 7_6_8, 3)

									assert_mean_pixel_difference(a_			,     a_      )