



'''simple docstring'''




import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
						    CTRLForSequenceClassification,
						    CTRLLMHeadModel,
						    CTRLModel,
						)







class      _lowerCAmelCase							:


	def __init__(self	,				lowercase	,				lowercase=14	,				lowercase=7	,				lowercase=True	,				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=3	,				lowercase=4	,				lowercase=None	,				):
				A_     :						Optional[int]     =					parent
				A_     :						Tuple     =					batch_size
				A_     :						Optional[Any]     =					seq_length
				A_     :						List[Any]     =					is_training
				A_     :						Dict     =					use_token_type_ids
				A_     :						Tuple     =					use_input_mask
				A_     :						Optional[Any]     =					use_labels
				A_     :						List[str]     =					use_mc_token_ids
				A_     :						Dict     =					vocab_size
				A_     :						Tuple     =					hidden_size
				A_     :						Dict     =					num_hidden_layers
				A_     :						Optional[Any]     =					num_attention_heads
				A_     :						Dict     =					intermediate_size
				A_     :						int     =					hidden_act
				A_     :						Optional[int]     =					hidden_dropout_prob
				A_     :						Dict     =					attention_probs_dropout_prob
				A_     :						str     =					max_position_embeddings
				A_     :						Optional[int]     =					type_vocab_size
				A_     :						Dict     =					type_sequence_label_size
				A_     :						Optional[Any]     =					initializer_range
				A_     :						Optional[int]     =					num_labels
				A_     :						List[Any]     =					num_choices
				A_     :						str     =					scope
				A_     :						List[Any]     =					self.vocab_size - 1


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

				A_     :						List[Any]     =					None
				if self.use_input_mask:
							A_     :						str     =					random_attention_mask([self.batch_size, self.seq_length]							)

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

				A_     :						int     =					None
				if self.use_mc_token_ids:
							A_     :						Dict     =					ids_tensor([self.batch_size, self.num_choices]	,				self.seq_length							)

				A_     :						Tuple     =					None
				A_     :						Tuple     =					None
				A_     :						Optional[int]     =					None
				if self.use_labels:
							A_     :						Union[str, Any]     =					ids_tensor([self.batch_size]	,				self.type_sequence_label_size							)
							A_     :						Dict     =					ids_tensor([self.batch_size, self.seq_length]	,				self.num_labels							)
							A_     :						Optional[int]     =					ids_tensor([self.batch_size]	,				self.num_choices							)

				A_     :						Dict     =					self.get_config()

				A_     :						Optional[int]     =					ids_tensor([self.num_hidden_layers, self.num_attention_heads]	,				2							)

				return (
				    config,
				    input_ids,
				    input_mask,
				    head_mask,
				    token_type_ids,
				    mc_token_ids,
				    sequence_labels,
				    token_labels,
				    choice_labels,
				)


	def    _a     (self							):
				return CTRLConfig(
				    vocab_size=self.vocab_size	,				n_embd=self.hidden_size	,				n_layer=self.num_hidden_layers	,				n_head=self.num_attention_heads	,				n_positions=self.max_position_embeddings	,				pad_token_id=self.pad_token_id	,				)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				*lowercase							):
				A_     :						Dict     =					CTRLModel(config=lowercase							)
				model.to(lowercase							)
				model.eval()

				model(lowercase	,				token_type_ids=lowercase	,				head_mask=lowercase							)
				model(lowercase	,				token_type_ids=lowercase							)
				A_     :						Optional[int]     =					model(lowercase							)
				self.parent.assertEqual(result.last_hidden_state.shape	,				(self.batch_size, self.seq_length, self.hidden_size)							)
				self.parent.assertEqual(len(result.past_key_values							)	,				config.n_layer							)


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				*lowercase							):
				A_     :						Any     =					CTRLLMHeadModel(lowercase							)
				model.to(lowercase							)
				model.eval()

				A_     :						int     =					model(lowercase	,				token_type_ids=lowercase	,				labels=lowercase							)
				self.parent.assertEqual(result.loss.shape	,				()							)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.seq_length, self.vocab_size)							)


	def    _a     (self							):
				A_     :						Any     =					self.prepare_config_and_inputs()

				(
				    (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   
				)     :						Dict     =					config_and_inputs

				A_     :						Tuple     =					{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

				return config, inputs_dict


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				*lowercase							):
				A_     :						Union[str, Any]     =					self.num_labels
				A_     :						List[str]     =					CTRLForSequenceClassification(lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						Optional[int]     =					ids_tensor([self.batch_size]	,				self.type_sequence_label_size							)
				A_     :						List[Any]     =					model(lowercase	,				token_type_ids=lowercase	,				labels=lowercase							)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_labels)							)







@require_torch
class      _lowerCAmelCase							(      __UpperCAmelCase				,      __UpperCAmelCase				,      __UpperCAmelCase				,      unittest.TestCase			):
	__SCREAMING_SNAKE_CASE       :		List[str]		      = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
	__SCREAMING_SNAKE_CASE       :		List[Any]		      = (CTRLLMHeadModel,) if is_torch_available() else ()
	__SCREAMING_SNAKE_CASE       :		Union[str, Any]		      = (
	    {
	        'feature-extraction': CTRLModel,
	        'text-classification': CTRLForSequenceClassification,
	        'text-generation': CTRLLMHeadModel,
	        'zero-shot': CTRLForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	__SCREAMING_SNAKE_CASE       :		str		      = True
	__SCREAMING_SNAKE_CASE       :		Dict		      = False
	__SCREAMING_SNAKE_CASE       :		Dict		      = False


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
							# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
							# `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
							# config could not be created.
							return True

				return False


	def    _a     (self							):
				A_     :						str     =					CTRLModelTester(self							)
				A_     :						Any     =					ConfigTester(self	,				config_class=lowercase	,				n_embd=37							)


	def    _a     (self							):
				super().tearDown()
				# clean-up as much as possible GPU memory occupied by PyTorch
				gc.collect()
				torch.cuda.empty_cache()


	def    _a     (self							):
				self.config_tester.run_common_tests()


	def    _a     (self							):
				A_     :						int     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_ctrl_model(*lowercase							)


	def    _a     (self							):
				A_     :						List[str]     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_lm_head_model(*lowercase							)


	@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""							)
	def    _a     (self							):
				pass


	@slow
	def    _a     (self							):
				for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							A_     :						Any     =					CTRLModel.from_pretrained(lowercase							)
							self.assertIsNotNone(lowercase							)


	@unittest.skip("""The model doesn't support left padding"""							)  # and it's not used enough to be worth fixing :)
	def    _a     (self							):
				pass







@require_torch
class      _lowerCAmelCase							(      unittest.TestCase			):


	def    _a     (self							):
				super().tearDown()
				# clean-up as much as possible GPU memory occupied by PyTorch
				gc.collect()
				torch.cuda.empty_cache()


	@slow
	def    _a     (self							):
				A_     :						Dict     =					CTRLLMHeadModel.from_pretrained("""ctrl"""							)
				model.to(lowercase							)
				A_     :						int     =					torch.tensor(
				    [[11859, 0, 1611, 8]]	,				dtype=torch.long	,				device=lowercase							)  # Legal the president is
				A_     :						Any     =					[
				    11859,
				    0,
				    1611,
				    8,
				    5,
				    150,
				    26449,
				    2,
				    19,
				    348,
				    469,
				    3,
				    2595,
				    48,
				    20740,
				    246533,
				    246533,
				    19,
				    30,
				    5,
				]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

				A_     :						Any     =					model.generate(lowercase	,				do_sample=lowercase							)
				self.assertListEqual(output_ids[0].tolist()	,				lowercase							)




'''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()