
import unittest

import numpy as np

from transformers import DistilBertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
					import jax.numpy as jnp

					from transformers.models.distilbert.modeling_flax_distilbert import (
					    FlaxDistilBertForMaskedLM,
					    FlaxDistilBertForMultipleChoice,
					    FlaxDistilBertForQuestionAnswering,
					    FlaxDistilBertForSequenceClassification,
					    FlaxDistilBertForTokenClassification,
					    FlaxDistilBertModel,
					)







class      __A(					unittest.TestCase     ):




				def __init__(	self						,	_snake_case						,	_snake_case=13						,	_snake_case=7						,	_snake_case=True						,	_snake_case=True						,	_snake_case=True						,	_snake_case=True						,	_snake_case=99						,	_snake_case=32						,	_snake_case=5						,	_snake_case=4						,	_snake_case=37						,	_snake_case="gelu"						,	_snake_case=0.1						,	_snake_case=0.1						,	_snake_case=512						,	_snake_case=16						,	_snake_case=2						,	_snake_case=0.02						,	_snake_case=4						,	)       -> Union[str, Any]:


								'''simple docstring'''




								__a     				=       parent
								__a     				=       batch_size
								__a     				=       seq_length
								__a     				=       is_training
								__a     				=       use_attention_mask
								__a     				=       use_token_type_ids
								__a     				=       use_labels
								__a     				=       vocab_size
								__a     				=       hidden_size
								__a     				=       num_hidden_layers
								__a     				=       num_attention_heads
								__a     				=       intermediate_size
								__a     				=       hidden_act
								__a     				=       hidden_dropout_prob
								__a     				=       attention_probs_dropout_prob
								__a     				=       max_position_embeddings
								__a     				=       type_vocab_size
								__a     				=       type_sequence_label_size
								__a     				=       initializer_range
								__a     				=       num_choices




				def 							SCREAMING_SNAKE_CASE_						(	self							)       -> Any:


								'''simple docstring'''




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

								__a     				=       None
								if self.use_attention_mask:
												__a     				=       random_attention_mask([self.batch_size, self.seq_length]							)

								__a     				=       DistilBertConfig(
								    vocab_size=self.vocab_size						,	dim=self.hidden_size						,	n_layers=self.num_hidden_layers						,	n_heads=self.num_attention_heads						,	hidden_dim=self.intermediate_size						,	hidden_act=self.hidden_act						,	dropout=self.hidden_dropout_prob						,	attention_dropout=self.attention_probs_dropout_prob						,	max_position_embeddings=self.max_position_embeddings						,	initializer_range=self.initializer_range						,	tie_weights_=__UpperCamelCase						,	)

								return config, input_ids, attention_mask




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


								'''simple docstring'''




								__a     				=       self.prepare_config_and_inputs()
								__a				,	__a				,	__a     				=       config_and_inputs
								__a     				=       {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
								return config, inputs_dict







@require_flax
class      __A(					_lowerCamelCase    ,  unittest.TestCase     ):
				snake_case_		  =       (
				    (
				        FlaxDistilBertModel,
				        FlaxDistilBertForMaskedLM,
				        FlaxDistilBertForMultipleChoice,
				        FlaxDistilBertForQuestionAnswering,
				        FlaxDistilBertForSequenceClassification,
				        FlaxDistilBertForTokenClassification,
				        FlaxDistilBertForQuestionAnswering,
				    )
				    if is_flax_available()
				    else ()
				)




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


								'''simple docstring'''




								__a     				=       FlaxDistilBertModelTester(self							)




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


								'''simple docstring'''




								for model_class_name in self.all_model_classes:
												__a     				=       model_class_name.from_pretrained('''distilbert-base-uncased'''							)
												__a     				=       model(np.ones((1, 1)							)							)
												self.assertIsNotNone(__UpperCamelCase							)







@require_flax
class      __A(					unittest.TestCase     ):




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


								'''simple docstring'''




								__a     				=       FlaxDistilBertModel.from_pretrained('''distilbert-base-uncased'''							)
								__a     				=       np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]							)
								__a     				=       np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
								__a     				=       model(__UpperCamelCase						,	attention_mask=__UpperCamelCase							)[0]
								__a     				=       (1, 11, 768)
								self.assertEqual(output.shape						,	__UpperCamelCase							)
								__a     				=       np.array([[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]							)

								self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]						,	__UpperCamelCase						,	atol=1E-4							)							)




import gc
import tempfile
import unittest

import numpy as np
import torch

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


__snake_case     :str			       =False



class        lowerCAmelCase__					(  unittest.TestCase       ):
				pass





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


				def __UpperCamelCase						(							self      :  Dict       )		->     List[Any]:
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()


				def __UpperCamelCase						(							self      :  Union[str, Any]       )		->     List[Any]:
											A      						=    VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'						, torch_dtype=torch.floataa       )
											pipe.to(__UpperCamelCase       )
											pipe.set_progress_bar_config(disable=__UpperCamelCase       )

											A      						=    load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'       )

											A      						=    torch.manual_seed(0       )
											A      						=    pipe.dual_guided(
											    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       )
																		A      						=    VersatileDiffusionPipeline.from_pretrained(__UpperCamelCase						, torch_dtype=torch.floataa       )
											pipe.to(__UpperCamelCase       )
											pipe.set_progress_bar_config(disable=__UpperCamelCase       )

											A      						=    generator.manual_seed(0       )
											A      						=    pipe.dual_guided(
											    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 __UpperCamelCase						(							self      :  Tuple       )		->     List[str]:
											A      						=    VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'						, torch_dtype=torch.floataa       )
											pipe.to(__UpperCamelCase       )
											pipe.set_progress_bar_config(disable=__UpperCamelCase       )

											A      						=    'cyberpunk 2077'
											A      						=    load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'       )
											A      						=    torch.manual_seed(0       )
											A      						=    pipe.dual_guided(
											    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

											A      						=    image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											A      						=    np.array([0.1_4_4_8, 0.1_6_1_9, 0.1_7_4_1, 0.1_0_8_6, 0.1_1_4_7, 0.1_1_2_8, 0.1_1_9_9, 0.1_1_6_5, 0.1_0_0_1]       )

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

											A      						=    'A painting of a squirrel eating a burger '
											A      						=    torch.manual_seed(0       )
											A      						=    pipe.text_to_image(
											    prompt=__UpperCamelCase						, generator=__UpperCamelCase						, guidance_scale=7.5						, num_inference_steps=50						, output_type='numpy'       ).images

											A      						=    image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											A      						=    np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8]       )

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

											A      						=    pipe.image_variation(__UpperCamelCase						, generator=__UpperCamelCase						, output_type='numpy'       ).images

											A      						=    image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											A      						=    np.array([0.3_0_7_6, 0.3_1_2_3, 0.3_2_8_4, 0.3_7_8_2, 0.3_7_7_0, 0.3_8_9_4, 0.4_2_9_7, 0.4_3_3_1, 0.4_4_5_6]       )

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