
import unittest

import numpy as np

from transformers import AlbertConfig, 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.albert.modeling_flax_albert import (
					    FlaxAlbertForMaskedLM,
					    FlaxAlbertForMultipleChoice,
					    FlaxAlbertForPreTraining,
					    FlaxAlbertForQuestionAnswering,
					    FlaxAlbertForSequenceClassification,
					    FlaxAlbertForTokenClassification,
					    FlaxAlbertModel,
					)







class      A__							(     unittest.TestCase	):






				def __init__(						self					:      List[Any]    ,     a					:      List[Any]    ,     a					:      List[str]=13    ,     a					:      Tuple=7    ,     a					:      Tuple=True    ,     a					:      Tuple=True    ,     a					:      List[str]=True    ,     a					:      Optional[Any]=True    ,     a					:      Tuple=99    ,     a					:      List[Any]=32    ,     a					:      Tuple=5    ,     a					:      Union[str, Any]=4    ,     a					:      Union[str, Any]=37    ,     a					:      Dict="gelu"    ,     a					:      Any=0.1    ,     a					:      List[Any]=0.1    ,     a					:      Optional[int]=512    ,     a					:      List[Any]=16    ,     a					:      str=2    ,     a					:      Optional[int]=0.0_2    ,     a					:      Tuple=4    ,     ):






							'''simple docstring'''







							lowerCAmelCase__      :      str         =		parent
							lowerCAmelCase__      :      int         =		batch_size
							lowerCAmelCase__      :      Optional[Any]         =		seq_length
							lowerCAmelCase__      :      List[Any]         =		is_training
							lowerCAmelCase__      :      Optional[Any]         =		use_attention_mask
							lowerCAmelCase__      :      Tuple         =		use_token_type_ids
							lowerCAmelCase__      :      Dict         =		use_labels
							lowerCAmelCase__      :      Union[str, Any]         =		vocab_size
							lowerCAmelCase__      :      Optional[Any]         =		hidden_size
							lowerCAmelCase__      :      Optional[int]         =		num_hidden_layers
							lowerCAmelCase__      :      Any         =		num_attention_heads
							lowerCAmelCase__      :      Tuple         =		intermediate_size
							lowerCAmelCase__      :      str         =		hidden_act
							lowerCAmelCase__      :      Tuple         =		hidden_dropout_prob
							lowerCAmelCase__      :      List[str]         =		attention_probs_dropout_prob
							lowerCAmelCase__      :      Optional[int]         =		max_position_embeddings
							lowerCAmelCase__      :      Tuple         =		type_vocab_size
							lowerCAmelCase__      :      Union[str, Any]         =		type_sequence_label_size
							lowerCAmelCase__      :      Union[str, Any]         =		initializer_range
							lowerCAmelCase__      :      Dict         =		num_choices






				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[str]         =		ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size   )

							lowerCAmelCase__      :      Dict         =		None
							if self.use_attention_mask:
										lowerCAmelCase__      :      List[Any]         =		random_attention_mask([self.batch_size, self.seq_length]   )

							lowerCAmelCase__      :      Tuple         =		None
							if self.use_token_type_ids:
										lowerCAmelCase__      :      Tuple         =		ids_tensor([self.batch_size, self.seq_length]    ,     self.type_vocab_size   )

							lowerCAmelCase__      :      Any         =		AlbertConfig(
							    vocab_size=self.vocab_size    ,     hidden_size=self.hidden_size    ,     num_hidden_layers=self.num_hidden_layers    ,     num_attention_heads=self.num_attention_heads    ,     intermediate_size=self.intermediate_size    ,     hidden_act=self.hidden_act    ,     hidden_dropout_prob=self.hidden_dropout_prob    ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,     max_position_embeddings=self.max_position_embeddings    ,     type_vocab_size=self.type_vocab_size    ,     is_decoder=a    ,     initializer_range=self.initializer_range    ,     )

							return config, input_ids, token_type_ids, attention_mask






				def 							_lowerCamelCase		(						self					:      Tuple   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Dict         =		self.prepare_config_and_inputs()
							lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__      :      int         =		config_and_inputs
							lowerCAmelCase__      :      Any         =		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask}
							return config, inputs_dict







@require_flax
class      A__							(     __magic_name__						,   unittest.TestCase	):
				lowercase			    =  (
				    (
				        FlaxAlbertModel,
				        FlaxAlbertForPreTraining,
				        FlaxAlbertForMaskedLM,
				        FlaxAlbertForMultipleChoice,
				        FlaxAlbertForQuestionAnswering,
				        FlaxAlbertForSequenceClassification,
				        FlaxAlbertForTokenClassification,
				        FlaxAlbertForQuestionAnswering,
				    )
				    if is_flax_available()
				    else ()
				)






				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      int         =		FlaxAlbertModelTester(self   )






				@slow
				def 							_lowerCamelCase		(						self					:      Optional[Any]   ):






							'''simple docstring'''







							for model_class_name in self.all_model_classes:
										lowerCAmelCase__      :      List[str]         =		model_class_name.from_pretrained('albert-base-v2'   )
										lowerCAmelCase__      :      Union[str, Any]         =		model(np.ones((1, 1)   )   )
										self.assertIsNotNone(a   )







@require_flax
class      A__							(     unittest.TestCase	):






				@slow
				def 							_lowerCamelCase		(						self					:      Optional[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Any         =		FlaxAlbertModel.from_pretrained('albert-base-v2'   )
							lowerCAmelCase__      :      List[Any]         =		np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]   )
							lowerCAmelCase__      :      List[Any]         =		np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
							lowerCAmelCase__      :      Optional[int]         =		model(a    ,     attention_mask=a   )[0]
							lowerCAmelCase__      :      int         =		(1, 11, 768)
							self.assertEqual(output.shape    ,     a   )
							lowerCAmelCase__      :      str         =		np.array(
							    [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]]   )

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

import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch







@require_torch
class      A__							:






				@property
				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							return self.get_dummy_input()






				@property
				def 							_lowerCamelCase		(						self					:      Dict   ):






							'''simple docstring'''







							if self.block_type == "down":
										return (4, 32, 16, 16)
							elif self.block_type == "mid":
										return (4, 32, 32, 32)
							elif self.block_type == "up":
										return (4, 32, 64, 64)

							raise ValueError(f'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.'''   )






				def 							_lowerCamelCase		(						self					:      Optional[int]    ,     a					:      List[Any]=True    ,     a					:      Any=False    ,     a					:      Dict=False    ,     a					:      Union[str, Any]=False    ,     ):






							'''simple docstring'''







							lowerCAmelCase__      :      Tuple         =		4
							lowerCAmelCase__      :      int         =		32
							lowerCAmelCase__      :      Tuple         =		(32, 32)

							lowerCAmelCase__      :      List[Any]         =		torch.manual_seed(0   )
							lowerCAmelCase__      :      List[Any]         =		torch.device(a   )
							lowerCAmelCase__      :      str         =		(batch_size, num_channels) + sizes
							lowerCAmelCase__      :      Tuple         =		randn_tensor(a    ,     generator=a    ,     device=a   )
							lowerCAmelCase__      :      Optional[Any]         =		{'hidden_states': hidden_states}

							if include_temb:
										lowerCAmelCase__      :      int         =		128
										lowerCAmelCase__      :      List[str]         =		randn_tensor((batch_size, temb_channels)    ,     generator=a    ,     device=a   )

							if include_res_hidden_states_tuple:
										lowerCAmelCase__      :      int         =		torch.manual_seed(1   )
										lowerCAmelCase__      :      str         =		(randn_tensor(a    ,     generator=a    ,     device=a   ),)

							if include_encoder_hidden_states:
										lowerCAmelCase__      :      Any         =		floats_tensor((batch_size, 32, 32)   ).to(a   )

							if include_skip_sample:
										lowerCAmelCase__      :      Union[str, Any]         =		randn_tensor(((batch_size, 3) + sizes)    ,     generator=a    ,     device=a   )

							return dummy_input






				def 							_lowerCamelCase		(						self					:      Tuple   ):






							'''simple docstring'''







							lowerCAmelCase__      :      str         =		{
							    'in_channels': 32,
							    'out_channels': 32,
							    'temb_channels': 128,
							}
							if self.block_type == "up":
										lowerCAmelCase__      :      Union[str, Any]         =		32

							if self.block_type == "mid":
										init_dict.pop('out_channels'   )

							lowerCAmelCase__      :      Optional[int]         =		self.dummy_input
							return init_dict, inputs_dict






				def 							_lowerCamelCase		(						self					:      str    ,     a					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      :      Union[str, Any]         =		self.prepare_init_args_and_inputs_for_common()
							lowerCAmelCase__      :      int         =		self.block_class(**a   )
							unet_block.to(a   )
							unet_block.eval()

							with torch.no_grad():
										lowerCAmelCase__      :      int         =		unet_block(**a   )

							if isinstance(a    ,     a   ):
										lowerCAmelCase__      :      List[str]         =		output[0]

							self.assertEqual(output.shape    ,     self.output_shape   )

							lowerCAmelCase__      :      List[str]         =		output[0, -1, -3:, -3:]
							lowerCAmelCase__      :      Any         =		torch.tensor(a   ).to(a   )
							assert torch_all_close(output_slice.flatten()    ,     a    ,     atol=5E-3   )






				@unittest.skipIf(torch_device == 'mps'    ,     'Training is not supported in mps'   )
				def 							_lowerCamelCase		(						self					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      ,				lowerCAmelCase__      :      Union[str, Any]         =		self.prepare_init_args_and_inputs_for_common()
							lowerCAmelCase__      :      Any         =		self.block_class(**a   )
							model.to(a   )
							model.train()
							lowerCAmelCase__      :      int         =		model(**a   )

							if isinstance(a    ,     a   ):
										lowerCAmelCase__      :      Dict         =		output[0]

							lowerCAmelCase__      :      Optional[int]         =		torch.device(a   )
							lowerCAmelCase__      :      List[Any]         =		randn_tensor(output.shape    ,     device=a   )
							lowerCAmelCase__      :      List[Any]         =		torch.nn.functional.mse_loss(a    ,     a   )
							loss.backward()