from __future__ import annotations

import unittest

from transformers import 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 import (
				    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFXLMForMultipleChoice,
				    TFXLMForQuestionAnsweringSimple,
				    TFXLMForSequenceClassification,
				    TFXLMForTokenClassification,
				    TFXLMModel,
				    TFXLMWithLMHeadModel,
				    XLMConfig,
				)





class 					__snake_case    :




					def __init__(  self       ,			__UpperCamelCase       ,			)	->    Optional[Any]:



									'''simple docstring'''







									snake_case__   :      Union[str, Any]   				= parent
									snake_case__   :      Any   				= 13
									snake_case__   :      int   				= 7
									snake_case__   :      Tuple   				= True
									snake_case__   :      List[Any]   				= True
									snake_case__   :      Any   				= True
									snake_case__   :      Optional[int]   				= True
									snake_case__   :      List[Any]   				= True
									snake_case__   :      Union[str, Any]   				= False
									snake_case__   :      int   				= False
									snake_case__   :      Any   				= False
									snake_case__   :      Union[str, Any]   				= 2
									snake_case__   :      Optional[int]   				= 99
									snake_case__   :      Optional[int]   				= 0
									snake_case__   :      Optional[int]   				= 32
									snake_case__   :      List[str]   				= 2
									snake_case__   :      Optional[int]   				= 4
									snake_case__   :      Tuple   				= 0.1
									snake_case__   :      Optional[Any]   				= 0.1
									snake_case__   :      Union[str, Any]   				= 512
									snake_case__   :      List[Any]   				= 16
									snake_case__   :      Optional[Any]   				= 2
									snake_case__   :      Union[str, Any]   				= 0.0_2
									snake_case__   :      Dict   				= 3
									snake_case__   :      Optional[Any]   				= 4
									snake_case__   :      Optional[int]   				= 'last'
									snake_case__   :      List[Any]   				= True
									snake_case__   :      Union[str, Any]   				= None
									snake_case__   :      Optional[Any]   				= 0




					def      __a     (  self						)	->    str:



									'''simple docstring'''







									snake_case__   :      int   				= ids_tensor([self.batch_size, self.seq_length]       ,			self.vocab_size						)
									snake_case__   :      List[str]   				= random_attention_mask([self.batch_size, self.seq_length]       ,			dtype=tf.floataa						)

									snake_case__   :      int   				= None
									if self.use_input_lengths:
													snake_case__   :      Union[str, Any]   				= (
													    ids_tensor([self.batch_size]       ,			vocab_size=2						) + self.seq_length - 2
													)  # small variation of seq_length

									snake_case__   :      List[Any]   				= None
									if self.use_token_type_ids:
													snake_case__   :      List[str]   				= ids_tensor([self.batch_size, self.seq_length]       ,			self.n_langs						)

									snake_case__   :      str   				= None
									snake_case__   :      List[Any]   				= None
									snake_case__   :      Any   				= None
									if self.use_labels:
													snake_case__   :      Any   				= ids_tensor([self.batch_size]       ,			self.type_sequence_label_size						)
													snake_case__   :      Tuple   				= ids_tensor([self.batch_size, self.seq_length]       ,			self.num_labels						)
													snake_case__   :      Optional[int]   				= ids_tensor([self.batch_size]       ,			2       ,			dtype=tf.floataa						)
													snake_case__   :      int   				= ids_tensor([self.batch_size]       ,			self.num_choices						)

									snake_case__   :      List[str]   				= XLMConfig(
									    vocab_size=self.vocab_size       ,			n_special=self.n_special       ,			emb_dim=self.hidden_size       ,			n_layers=self.num_hidden_layers       ,			n_heads=self.num_attention_heads       ,			dropout=self.hidden_dropout_prob       ,			attention_dropout=self.attention_probs_dropout_prob       ,			gelu_activation=self.gelu_activation       ,			sinusoidal_embeddings=self.sinusoidal_embeddings       ,			asm=self.asm       ,			causal=self.causal       ,			n_langs=self.n_langs       ,			max_position_embeddings=self.max_position_embeddings       ,			initializer_range=self.initializer_range       ,			summary_type=self.summary_type       ,			use_proj=self.use_proj       ,			bos_token_id=self.bos_token_id       ,			)

									return (
									    config,
									    input_ids,
									    token_type_ids,
									    input_lengths,
									    sequence_labels,
									    token_labels,
									    is_impossible_labels,
									    choice_labels,
									    input_mask,
									)




					def      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			)	->    Optional[Any]:



									'''simple docstring'''







									snake_case__   :      Dict   				= TFXLMModel(config=__UpperCamelCase						)
									snake_case__   :      Optional[Any]   				= {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
									snake_case__   :      Tuple   				= model(__UpperCamelCase						)

									snake_case__   :      List[Any]   				= [input_ids, input_mask]
									snake_case__   :      Optional[Any]   				= model(__UpperCamelCase						)
									self.parent.assertEqual(result.last_hidden_state.shape       ,			(self.batch_size, self.seq_length, self.hidden_size)						)




					def      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			)	->    List[str]:



									'''simple docstring'''







									snake_case__   :      Optional[int]   				= TFXLMWithLMHeadModel(__UpperCamelCase						)

									snake_case__   :      Optional[Any]   				= {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
									snake_case__   :      List[str]   				= model(__UpperCamelCase						)

									snake_case__   :      str   				= outputs

									self.parent.assertEqual(result.logits.shape       ,			(self.batch_size, self.seq_length, self.vocab_size)						)




					def      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			)	->    List[Any]:



									'''simple docstring'''







									snake_case__   :      List[str]   				= TFXLMForQuestionAnsweringSimple(__UpperCamelCase						)

									snake_case__   :      Tuple   				= {'input_ids': input_ids, 'lengths': input_lengths}

									snake_case__   :      Tuple   				= model(__UpperCamelCase						)

									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       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			)	->    Optional[int]:



									'''simple docstring'''







									snake_case__   :      List[str]   				= TFXLMForSequenceClassification(__UpperCamelCase						)

									snake_case__   :      Optional[Any]   				= {'input_ids': input_ids, 'lengths': input_lengths}

									snake_case__   :      Optional[Any]   				= model(__UpperCamelCase						)

									self.parent.assertEqual(result.logits.shape       ,			(self.batch_size, self.type_sequence_label_size)						)




					def      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			)	->    Dict:



									'''simple docstring'''







									snake_case__   :      Tuple   				= self.num_labels
									snake_case__   :      Tuple   				= TFXLMForTokenClassification(config=__UpperCamelCase						)
									snake_case__   :      str   				= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
									snake_case__   :      List[Any]   				= model(__UpperCamelCase						)
									self.parent.assertEqual(result.logits.shape       ,			(self.batch_size, self.seq_length, self.num_labels)						)




					def      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			)	->    Union[str, Any]:



									'''simple docstring'''







									snake_case__   :      str   				= self.num_choices
									snake_case__   :      int   				= TFXLMForMultipleChoice(config=__UpperCamelCase						)
									snake_case__   :      int   				= tf.tile(tf.expand_dims(__UpperCamelCase       ,			1						)       ,			(1, self.num_choices, 1)						)
									snake_case__   :      str   				= tf.tile(tf.expand_dims(__UpperCamelCase       ,			1						)       ,			(1, self.num_choices, 1)						)
									snake_case__   :      Optional[int]   				= tf.tile(tf.expand_dims(__UpperCamelCase       ,			1						)       ,			(1, self.num_choices, 1)						)
									snake_case__   :      int   				= {
									    'input_ids': multiple_choice_inputs_ids,
									    'attention_mask': multiple_choice_input_mask,
									    'token_type_ids': multiple_choice_token_type_ids,
									}
									snake_case__   :      Tuple   				= model(__UpperCamelCase						)
									self.parent.assertEqual(result.logits.shape       ,			(self.batch_size, self.num_choices)						)




					def      __a     (  self						)	->    List[Any]:



									'''simple docstring'''







									snake_case__   :      Tuple   				= self.prepare_config_and_inputs()
									(
									    (
									    snake_case__
									)				,    (
									    snake_case__
									)				,    (
									    snake_case__
									)				,    (
									    snake_case__
									)				,    (
									    snake_case__
									)				,    (
									    snake_case__
									)				,    (
									    snake_case__
									)				,    (
									    snake_case__
									)				,    (
									    snake_case__
									)				,    
									)   :      str   				= config_and_inputs
									snake_case__   :      List[Any]   				= {
									    'input_ids': input_ids,
									    'token_type_ids': token_type_ids,
									    'langs': token_type_ids,
									    'lengths': input_lengths,
									}
									return config, inputs_dict





@require_tf
class 					__snake_case    (     _lowerCamelCase       ,_lowerCamelCase       ,unittest.TestCase      ):
					__lowerCamelCase			   =  (
					    (
					        TFXLMModel,
					        TFXLMWithLMHeadModel,
					        TFXLMForSequenceClassification,
					        TFXLMForQuestionAnsweringSimple,
					        TFXLMForTokenClassification,
					        TFXLMForMultipleChoice,
					    )
					    if is_tf_available()
					    else ()
					)
					__lowerCamelCase			   =  (
					    (TFXLMWithLMHeadModel,) if is_tf_available() else ()
					)  # TODO (PVP): Check other models whether language generation is also applicable
					__lowerCamelCase			   =  (
					    {
					        """feature-extraction""": TFXLMModel,
					        """fill-mask""": TFXLMWithLMHeadModel,
					        """question-answering""": TFXLMForQuestionAnsweringSimple,
					        """text-classification""": TFXLMForSequenceClassification,
					        """text-generation""": TFXLMWithLMHeadModel,
					        """token-classification""": TFXLMForTokenClassification,
					        """zero-shot""": TFXLMForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)
					__lowerCamelCase			   =  False
					__lowerCamelCase			   =  False




					def      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase						)	->    Tuple:



									'''simple docstring'''







									if (
									    pipeline_test_casse_name == "QAPipelineTests"
									    and tokenizer_name is not None
									    and not tokenizer_name.endswith('Fast'						)
									):
													# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
													# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
													# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
													return True

									return False




					def      __a     (  self						)	->    Dict:



									'''simple docstring'''







									snake_case__   :      int   				= TFXLMModelTester(self						)
									snake_case__   :      Any   				= ConfigTester(self       ,			config_class=__UpperCamelCase       ,			emb_dim=37						)




					def      __a     (  self						)	->    List[str]:



									'''simple docstring'''







									self.config_tester.run_common_tests()




					def      __a     (  self						)	->    Optional[int]:



									'''simple docstring'''







									snake_case__   :      List[str]   				= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_model(*__UpperCamelCase						)




					def      __a     (  self						)	->    List[Any]:



									'''simple docstring'''







									snake_case__   :      Union[str, Any]   				= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_lm_head(*__UpperCamelCase						)




					def      __a     (  self						)	->    List[Any]:



									'''simple docstring'''







									snake_case__   :      Optional[int]   				= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_qa(*__UpperCamelCase						)




					def      __a     (  self						)	->    List[str]:



									'''simple docstring'''







									snake_case__   :      List[Any]   				= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_sequence_classif(*__UpperCamelCase						)




					def      __a     (  self						)	->    Tuple:



									'''simple docstring'''







									snake_case__   :      Optional[Any]   				= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_for_token_classification(*__UpperCamelCase						)




					def      __a     (  self						)	->    List[Any]:



									'''simple docstring'''







									snake_case__   :      int   				= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_for_multiple_choice(*__UpperCamelCase						)




					@slow
					def      __a     (  self						)	->    int:



									'''simple docstring'''







									for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													snake_case__   :      str   				= TFXLMModel.from_pretrained(__UpperCamelCase						)
													self.assertIsNotNone(__UpperCamelCase						)





@require_tf
class 					__snake_case    (     unittest.TestCase      ):




					@slow
					def      __a     (  self						)	->    List[str]:



									'''simple docstring'''







									snake_case__   :      Tuple   				= TFXLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048'						)
									snake_case__   :      Any   				= tf.convert_to_tensor([[14, 447]]       ,			dtype=tf.intaa						)  # the president
									snake_case__   :      Optional[Any]   				= [
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									]  # the president the president the president the president the president the president the president the president the president the president
									# TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
									snake_case__   :      Any   				= model.generate(__UpperCamelCase       ,			do_sample=__UpperCamelCase						)
									self.assertListEqual(output_ids[0].numpy().tolist()       ,			__UpperCamelCase						)



import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionDualGuidedPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


lowerCAmelCase__	:  Union[str, Any]						=  False





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




					def      __a     (  self						)	->    str:



									'''simple docstring'''







									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()




					def      __a     (  self						)	->    Optional[int]:



									'''simple docstring'''







									snake_case__   :      str   				= VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'						)
									# remove text_unet
									pipe.remove_unused_weights()
									pipe.to(__UpperCamelCase						)
									pipe.set_progress_bar_config(disable=__UpperCamelCase						)

									snake_case__   :      Union[str, Any]   				= load_image(
									    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'						)

									snake_case__   :      Dict   				= torch.manual_seed(0						)
									snake_case__   :      Tuple   				= pipe(
									    prompt='first prompt'       ,			image=__UpperCamelCase       ,			text_to_image_strength=0.7_5       ,			generator=__UpperCamelCase       ,			guidance_scale=7.5       ,			num_inference_steps=2       ,			output_type='numpy'       ,			).images

									with tempfile.TemporaryDirectory() as tmpdirname:
													pipe.save_pretrained(__UpperCamelCase						)
													snake_case__   :      List[Any]   				= VersatileDiffusionDualGuidedPipeline.from_pretrained(__UpperCamelCase						)

									pipe.to(__UpperCamelCase						)
									pipe.set_progress_bar_config(disable=__UpperCamelCase						)

									snake_case__   :      str   				= generator.manual_seed(0						)
									snake_case__   :      List[str]   				= pipe(
									    prompt='first prompt'       ,			image=__UpperCamelCase       ,			text_to_image_strength=0.7_5       ,			generator=__UpperCamelCase       ,			guidance_scale=7.5       ,			num_inference_steps=2       ,			output_type='numpy'       ,			).images

									assert np.abs(image - new_image						).sum() < 1E-5, "Models don't have the same forward pass"




					def      __a     (  self						)	->    Optional[Any]:



									'''simple docstring'''







									snake_case__   :      Union[str, Any]   				= VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'						)
									pipe.remove_unused_weights()
									pipe.to(__UpperCamelCase						)
									pipe.set_progress_bar_config(disable=__UpperCamelCase						)

									snake_case__   :      str   				= 'cyberpunk 2077'
									snake_case__   :      Dict   				= load_image(
									    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'						)
									snake_case__   :      Union[str, Any]   				= torch.manual_seed(0						)
									snake_case__   :      Union[str, Any]   				= pipe(
									    prompt=__UpperCamelCase       ,			image=__UpperCamelCase       ,			text_to_image_strength=0.7_5       ,			generator=__UpperCamelCase       ,			guidance_scale=7.5       ,			num_inference_steps=50       ,			output_type='numpy'       ,			).images

									snake_case__   :      int   				= image[0, 253:256, 253:256, -1]

									assert image.shape == (1, 512, 512, 3)
									snake_case__   :      List[str]   				= np.array([0.0_7_8_7, 0.0_8_4_9, 0.0_8_2_6, 0.0_8_1_2, 0.0_8_0_7, 0.0_7_9_5, 0.0_8_1_8, 0.0_7_9_8, 0.0_7_7_9]						)

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


