


'''simple docstring'''




import unittest

import numpy as np

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

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
						from transformers.models.bert.modeling_flax_bert import (
						    FlaxBertForMaskedLM,
						    FlaxBertForMultipleChoice,
						    FlaxBertForNextSentencePrediction,
						    FlaxBertForPreTraining,
						    FlaxBertForQuestionAnswering,
						    FlaxBertForSequenceClassification,
						    FlaxBertForTokenClassification,
						    FlaxBertModel,
						)







class      _lowerCAmelCase							(      unittest.TestCase			):


	def __init__(self	,				lowercase	,				lowercase=13	,				lowercase=7	,				lowercase=True	,				lowercase=True	,				lowercase=True	,				lowercase=True	,				lowercase=99	,				lowercase=32	,				lowercase=5	,				lowercase=4	,				lowercase=37	,				lowercase="gelu"	,				lowercase=0.1	,				lowercase=0.1	,				lowercase=512	,				lowercase=16	,				lowercase=2	,				lowercase=0.02	,				lowercase=4	,				):
				A_     :						Dict     =					parent
				A_     :						List[str]     =					batch_size
				A_     :						Any     =					seq_length
				A_     :						Tuple     =					is_training
				A_     :						Tuple     =					use_attention_mask
				A_     :						Union[str, Any]     =					use_token_type_ids
				A_     :						str     =					use_labels
				A_     :						Union[str, Any]     =					vocab_size
				A_     :						Optional[int]     =					hidden_size
				A_     :						Tuple     =					num_hidden_layers
				A_     :						Dict     =					num_attention_heads
				A_     :						str     =					intermediate_size
				A_     :						Dict     =					hidden_act
				A_     :						List[Any]     =					hidden_dropout_prob
				A_     :						List[Any]     =					attention_probs_dropout_prob
				A_     :						Optional[Any]     =					max_position_embeddings
				A_     :						Dict     =					type_vocab_size
				A_     :						Dict     =					type_sequence_label_size
				A_     :						Union[str, Any]     =					initializer_range
				A_     :						Optional[Any]     =					num_choices


	def    _a     (self							):
				A_     :						str     =					ids_tensor([self.batch_size, self.seq_length]	,				self.vocab_size							)

				A_     :						str     =					None
				if self.use_attention_mask:
							A_     :						Tuple     =					random_attention_mask([self.batch_size, self.seq_length]							)

				A_     :						str     =					None
				if self.use_token_type_ids:
							A_     :						int     =					ids_tensor([self.batch_size, self.seq_length]	,				self.type_vocab_size							)

				A_     :						int     =					BertConfig(
				    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    _a     (self							):
				A_     :						Any     =					self.prepare_config_and_inputs()
				A_     :						Optional[Any]     =					config_and_inputs
				A_     :						Union[str, Any]     =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
				return config, inputs_dict


	def    _a     (self							):
				A_     :						Tuple     =					self.prepare_config_and_inputs()
				A_     :						List[str]     =					config_and_inputs

				A_     :						List[Any]     =					True
				A_     :						Any     =					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
				A_     :						Tuple     =					ids_tensor([self.batch_size, self.seq_length]	,				vocab_size=2							)

				return (
				    config,
				    input_ids,
				    attention_mask,
				    encoder_hidden_states,
				    encoder_attention_mask,
				)







@require_flax
class      _lowerCAmelCase							(      _a				,      unittest.TestCase			):
	__SCREAMING_SNAKE_CASE       :		Tuple		      = True

	__SCREAMING_SNAKE_CASE       :		Union[str, Any]		      = (
	    (
	        FlaxBertModel,
	        FlaxBertForPreTraining,
	        FlaxBertForMaskedLM,
	        FlaxBertForMultipleChoice,
	        FlaxBertForQuestionAnswering,
	        FlaxBertForNextSentencePrediction,
	        FlaxBertForSequenceClassification,
	        FlaxBertForTokenClassification,
	        FlaxBertForQuestionAnswering,
	    )
	    if is_flax_available()
	    else ()
	)


	def    _a     (self							):
				A_     :						Union[str, Any]     =					FlaxBertModelTester(self							)


	@slow
	def    _a     (self							):
				# Only check this for base model, not necessary for all model classes.
				# This will also help speed-up tests.
				A_     :						List[Any]     =					FlaxBertModel.from_pretrained("""bert-base-cased"""							)
				A_     :						str     =					model(np.ones((1, 1)							)							)
				self.assertIsNotNone(_a							)




'''simple docstring'''




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      _lowerCAmelCase							:


	@property
	def    _a     (self							):
				return self.get_dummy_input()


	@property
	def    _a     (self							):
				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    _a     (self	,				lowercase=True	,				lowercase=False	,				lowercase=False	,				lowercase=False	,				):
				A_     :						List[str]     =					4
				A_     :						int     =					32
				A_     :						Optional[int]     =					(32, 32)

				A_     :						Optional[Any]     =					torch.manual_seed(0							)
				A_     :						int     =					torch.device(lowercase							)
				A_     :						int     =					(batch_size, num_channels) + sizes
				A_     :						Optional[int]     =					randn_tensor(lowercase	,				generator=lowercase	,				device=lowercase							)
				A_     :						Union[str, Any]     =					{"""hidden_states""": hidden_states}

				if include_temb:
							A_     :						str     =					128
							A_     :						List[Any]     =					randn_tensor((batch_size, temb_channels)	,				generator=lowercase	,				device=lowercase							)

				if include_res_hidden_states_tuple:
							A_     :						List[str]     =					torch.manual_seed(1							)
							A_     :						int     =					(randn_tensor(lowercase	,				generator=lowercase	,				device=lowercase							),)

				if include_encoder_hidden_states:
							A_     :						List[str]     =					floats_tensor((batch_size, 32, 32)							).to(lowercase							)

				if include_skip_sample:
							A_     :						Dict     =					randn_tensor(((batch_size, 3) + sizes)	,				generator=lowercase	,				device=lowercase							)

				return dummy_input


	def    _a     (self							):
				A_     :						Tuple     =					{
				    """in_channels""": 32,
				    """out_channels""": 32,
				    """temb_channels""": 128,
				}
				if self.block_type == "up":
							A_     :						Any     =					32

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

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


	def    _a     (self	,				lowercase							):
				A_,   A_     :						Optional[Any]     =					self.prepare_init_args_and_inputs_for_common()
				A_     :						int     =					self.block_class(**lowercase							)
				unet_block.to(lowercase							)
				unet_block.eval()

				with torch.no_grad():
							A_     :						List[str]     =					unet_block(**lowercase							)

				if isinstance(lowercase	,				lowercase							):
							A_     :						Union[str, Any]     =					output[0]

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

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


	@unittest.skipIf(torch_device == """mps"""	,				"""Training is not supported in mps"""							)
	def    _a     (self							):
				A_,   A_     :						Tuple     =					self.prepare_init_args_and_inputs_for_common()
				A_     :						List[str]     =					self.block_class(**lowercase							)
				model.to(lowercase							)
				model.train()
				A_     :						Any     =					model(**lowercase							)

				if isinstance(lowercase	,				lowercase							):
							A_     :						str     =					output[0]

				A_     :						Union[str, Any]     =					torch.device(lowercase							)
				A_     :						Tuple     =					randn_tensor(output.shape	,				device=lowercase							)
				A_     :						List[str]     =					torch.nn.functional.mse_loss(lowercase	,				lowercase							)
				loss.backward()