



'''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__      :




					'''simple docstring'''



					@property
					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       str:



						'''simple docstring'''





						return self.get_dummy_input()




					@property
					def 						__UpperCAmelCase     (  self				:	List[str]					)  ->       Optional[int]:



						'''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 						__UpperCAmelCase     (  self				:	int     ,  UpperCamelCase_				:	Union[str, Any]=True     ,  UpperCamelCase_				:	Union[str, Any]=False     ,  UpperCamelCase_				:	Any=False     ,  UpperCamelCase_				:	Any=False     ,  )  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       Union[str, Any]       =		4
						_lowercase					:       List[str]       =		32
						_lowercase					:       Tuple       =		(32, 32)

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

						if include_temb:
							_lowercase					:       Optional[int]       =		128
							_lowercase					:       Optional[Any]       =		randn_tensor((batch_size, temb_channels)     ,  generator=UpperCAmelCase__     ,  device=UpperCAmelCase__					)

						if include_res_hidden_states_tuple:
							_lowercase					:       Optional[Any]       =		torch.manual_seed(1					)
							_lowercase					:       int       =		(randn_tensor(UpperCAmelCase__     ,  generator=UpperCAmelCase__     ,  device=UpperCAmelCase__					),)

						if include_encoder_hidden_states:
							_lowercase					:       Dict       =		floats_tensor((batch_size, 32, 32)					).to(UpperCAmelCase__					)

						if include_skip_sample:
							_lowercase					:       List[Any]       =		randn_tensor(((batch_size, 3) + sizes)     ,  generator=UpperCAmelCase__     ,  device=UpperCAmelCase__					)

						return dummy_input




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       Dict:



						'''simple docstring'''





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

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

						_lowercase					:       Tuple       =		self.dummy_input
						return init_dict, inputs_dict




					def 						__UpperCAmelCase     (  self				:	Optional[int]     ,  UpperCamelCase_				:	Dict					)  ->       int:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		self.prepare_init_args_and_inputs_for_common()
						_lowercase					:       List[str]       =		self.block_class(**UpperCAmelCase__					)
						unet_block.to(UpperCAmelCase__					)
						unet_block.eval()

						with torch.no_grad():
							_lowercase					:       Optional[int]       =		unet_block(**UpperCAmelCase__					)

						if isinstance(UpperCAmelCase__     ,  UpperCAmelCase__					):
							_lowercase					:       Optional[Any]       =		output[0]

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

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




					@unittest.skipIf(torch_device == 'mps'     ,  'Training is not supported in mps'					)
					def 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       Dict:



						'''simple docstring'''





						_lowercase					:       Any       =		self.prepare_init_args_and_inputs_for_common()
						_lowercase					:       Optional[int]       =		self.block_class(**UpperCAmelCase__					)
						model.to(UpperCAmelCase__					)
						model.train()
						_lowercase					:       int       =		model(**UpperCAmelCase__					)

						if isinstance(UpperCAmelCase__     ,  UpperCAmelCase__					):
							_lowercase					:       Optional[int]       =		output[0]

						_lowercase					:       Dict       =		torch.device(UpperCAmelCase__					)
						_lowercase					:       Tuple       =		randn_tensor(output.shape     ,  device=UpperCAmelCase__					)
						_lowercase					:       Union[str, Any]       =		torch.nn.functional.mse_loss(UpperCAmelCase__     ,  UpperCAmelCase__					)
						loss.backward()





'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_A				:    Optional[Any]     						={'''configuration_xlnet''': ['''XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLNetConfig''']}

try:
		if not is_sentencepiece_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    Tuple     						=['''XLNetTokenizer''']

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    str     						=['''XLNetTokenizerFast''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    Any     						=[
		    '''XLNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''XLNetForMultipleChoice''',
		    '''XLNetForQuestionAnswering''',
		    '''XLNetForQuestionAnsweringSimple''',
		    '''XLNetForSequenceClassification''',
		    '''XLNetForTokenClassification''',
		    '''XLNetLMHeadModel''',
		    '''XLNetModel''',
		    '''XLNetPreTrainedModel''',
		    '''load_tf_weights_in_xlnet''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    str     						=[
		    '''TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''TFXLNetForMultipleChoice''',
		    '''TFXLNetForQuestionAnsweringSimple''',
		    '''TFXLNetForSequenceClassification''',
		    '''TFXLNetForTokenClassification''',
		    '''TFXLNetLMHeadModel''',
		    '''TFXLNetMainLayer''',
		    '''TFXLNetModel''',
		    '''TFXLNetPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig

		try:
				if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_xlnet import XLNetTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_xlnet_fast import XLNetTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_xlnet import (
				    XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
				    XLNetForMultipleChoice,
				    XLNetForQuestionAnswering,
				    XLNetForQuestionAnsweringSimple,
				    XLNetForSequenceClassification,
				    XLNetForTokenClassification,
				    XLNetLMHeadModel,
				    XLNetModel,
				    XLNetPreTrainedModel,
				    load_tf_weights_in_xlnet,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_xlnet import (
				    TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFXLNetForMultipleChoice,
				    TFXLNetForQuestionAnsweringSimple,
				    TFXLNetForSequenceClassification,
				    TFXLNetForTokenClassification,
				    TFXLNetLMHeadModel,
				    TFXLNetMainLayer,
				    TFXLNetModel,
				    TFXLNetPreTrainedModel,
				)

else:
		import sys

		_A				:    str     						=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
