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



	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.0_2		,    lowercase=3		,    lowercase=4		,    lowercase=None		,    )							->			str:
					lowerCamelCase_         =     parent
					lowerCamelCase_         =     batch_size
					lowerCamelCase_         =     seq_length
					lowerCamelCase_         =     is_training
					lowerCamelCase_         =     use_token_type_ids
					lowerCamelCase_         =     use_input_mask
					lowerCamelCase_         =     use_labels
					lowerCamelCase_         =     use_mc_token_ids
					lowerCamelCase_         =     vocab_size
					lowerCamelCase_         =     hidden_size
					lowerCamelCase_         =     num_hidden_layers
					lowerCamelCase_         =     num_attention_heads
					lowerCamelCase_         =     intermediate_size
					lowerCamelCase_         =     hidden_act
					lowerCamelCase_         =     hidden_dropout_prob
					lowerCamelCase_         =     attention_probs_dropout_prob
					lowerCamelCase_         =     max_position_embeddings
					lowerCamelCase_         =     type_vocab_size
					lowerCamelCase_         =     type_sequence_label_size
					lowerCamelCase_         =     initializer_range
					lowerCamelCase_         =     num_labels
					lowerCamelCase_         =     num_choices
					lowerCamelCase_         =     scope
					lowerCamelCase_         =     self.vocab_size - 1



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Dict:
					lowerCamelCase_         =     ids_tensor([self.batch_size, self.seq_length]		,    self.vocab_size					)

					lowerCamelCase_         =     None
					if self.use_input_mask:
									lowerCamelCase_         =     random_attention_mask([self.batch_size, self.seq_length]					)

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

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

					lowerCamelCase_         =     None
					lowerCamelCase_         =     None
					lowerCamelCase_         =     None
					if self.use_labels:
									lowerCamelCase_         =     ids_tensor([self.batch_size]		,    self.type_sequence_label_size					)
									lowerCamelCase_         =     ids_tensor([self.batch_size, self.seq_length]		,    self.num_labels					)
									lowerCamelCase_         =     ids_tensor([self.batch_size]		,    self.num_choices					)

					lowerCamelCase_         =     self.get_config()

					lowerCamelCase_         =     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        SCREAMING_SNAKE_CASE_(       self					)							->			Union[str, Any]:
					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        SCREAMING_SNAKE_CASE_(       self		,    lowercase		,    lowercase		,    lowercase		,    lowercase		,    lowercase		,    *lowercase					)							->			str:
					lowerCamelCase_         =     CTRLModel(config=lowercase					)
					model.to(lowercase					)
					model.eval()

					model(lowercase		,    token_type_ids=lowercase		,    head_mask=lowercase					)
					model(lowercase		,    token_type_ids=lowercase					)
					lowerCamelCase_         =     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        SCREAMING_SNAKE_CASE_(       self		,    lowercase		,    lowercase		,    lowercase		,    lowercase		,    lowercase		,    *lowercase					)							->			List[Any]:
					lowerCamelCase_         =     CTRLLMHeadModel(lowercase					)
					model.to(lowercase					)
					model.eval()

					lowerCamelCase_         =     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        SCREAMING_SNAKE_CASE_(       self					)							->			List[Any]:
					lowerCamelCase_         =     self.prepare_config_and_inputs()

					(
					    (
					    lowerCamelCase_
					)      ,       (
					    lowerCamelCase_
					)      ,       (
					    lowerCamelCase_
					)      ,       (
					    lowerCamelCase_
					)      ,       (
					    lowerCamelCase_
					)      ,       (
					    lowerCamelCase_
					)      ,       (
					    lowerCamelCase_
					)      ,       (
					    lowerCamelCase_
					)      ,       (
					    lowerCamelCase_
					)      ,       
					)         =     config_and_inputs

					lowerCamelCase_         =     {"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

					return config, inputs_dict



	def        SCREAMING_SNAKE_CASE_(       self		,    lowercase		,    lowercase		,    lowercase		,    lowercase		,    *lowercase					)							->			Optional[int]:
					lowerCamelCase_         =     self.num_labels
					lowerCamelCase_         =     CTRLForSequenceClassification(lowercase					)
					model.to(lowercase					)
					model.eval()
					lowerCamelCase_         =     ids_tensor([self.batch_size]		,    self.type_sequence_label_size					)
					lowerCamelCase_         =     model(lowercase		,    token_type_ids=lowercase		,    labels=lowercase					)
					self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.num_labels)					)




@require_torch
class        _SCREAMING_SNAKE_CASE	( snake_case_     ,			snake_case_     ,			snake_case_     ,			unittest.TestCase		):
	lowerCAmelCase__   =					(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
	lowerCAmelCase__   =					(CTRLLMHeadModel,) if is_torch_available() else ()
	lowerCAmelCase__   =					(
	    {
	        'feature-extraction': CTRLModel,
	        'text-classification': CTRLForSequenceClassification,
	        'text-generation': CTRLLMHeadModel,
	        'zero-shot': CTRLForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	lowerCAmelCase__   =					True
	lowerCAmelCase__   =					False
	lowerCAmelCase__   =					False



	def        SCREAMING_SNAKE_CASE_(       self		,    lowercase		,    lowercase		,    lowercase		,    lowercase		,    lowercase					)							->			Optional[int]:
					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        SCREAMING_SNAKE_CASE_(       self					)							->			Dict:
					lowerCamelCase_         =     CTRLModelTester(self					)
					lowerCamelCase_         =     ConfigTester(self		,    config_class=lowercase		,    n_embd=37					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Union[str, Any]:
					super().tearDown()
					# clean-up as much as possible GPU memory occupied by PyTorch
					gc.collect()
					torch.cuda.empty_cache()



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[int]:
					self.config_tester.run_common_tests()



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[Any]:
					lowerCamelCase_         =     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_ctrl_model(*lowercase					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			str:
					lowerCamelCase_         =     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        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[Any]:
					pass



	@slow
	def        SCREAMING_SNAKE_CASE_(       self					)							->			str:
					for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase_         =     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        SCREAMING_SNAKE_CASE_(       self					)							->			Tuple:
					pass




@require_torch
class        _SCREAMING_SNAKE_CASE	( unittest.TestCase		):



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



	@slow
	def        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[Any]:
					lowerCamelCase_         =     CTRLLMHeadModel.from_pretrained("ctrl"					)
					model.to(lowercase					)
					lowerCamelCase_         =     torch.tensor(
					    [[11859, 0, 1611, 8]]		,    dtype=torch.long		,    device=lowercase					)  # Legal the president is
					lowerCamelCase_         =     [
					    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

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




import numpy as np
import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel

from ...utils import logging


__A							=logging.get_logger(__name__)




class        _SCREAMING_SNAKE_CASE	( snake_case_		):
	lowerCAmelCase__   =					CLIPConfig

	lowerCAmelCase__   =					['CLIPEncoderLayer']



	def __init__(       self		,    lowercase					)							->			int:
					super().__init__(lowercase					)

					lowerCamelCase_         =     CLIPVisionModelWithProjection(config.vision_config					)

					lowerCamelCase_         =     nn.Linear(config.vision_config.projection_dim		,    1					)
					lowerCamelCase_         =     nn.Linear(config.vision_config.projection_dim		,    1					)



	@torch.no_grad()
	def        SCREAMING_SNAKE_CASE_(       self		,    lowercase		,    lowercase		,    lowercase=0.5		,    lowercase=0.5					)							->			int:
					lowerCamelCase_         =     self.vision_model(lowercase					)[0]

					lowerCamelCase_         =     self.p_head(lowercase					)
					lowerCamelCase_         =     nsfw_detected.flatten()
					lowerCamelCase_         =     nsfw_detected > p_threshold
					lowerCamelCase_         =     nsfw_detected.tolist()

					if any(lowercase					):
									logger.warning(
									    "Potential NSFW content was detected in one or more images. A black image will be returned instead."
									    " Try again with a different prompt and/or seed."					)

					for idx, nsfw_detected_ in enumerate(lowercase					):
									if nsfw_detected_:
													lowerCamelCase_         =     np.zeros(images[idx].shape					)

					lowerCamelCase_         =     self.w_head(lowercase					)
					lowerCamelCase_         =     watermark_detected.flatten()
					lowerCamelCase_         =     watermark_detected > w_threshold
					lowerCamelCase_         =     watermark_detected.tolist()

					if any(lowercase					):
									logger.warning(
									    "Potential watermarked content was detected in one or more images. A black image will be returned instead."
									    " Try again with a different prompt and/or seed."					)

					for idx, watermark_detected_ in enumerate(lowercase					):
									if watermark_detected_:
													lowerCamelCase_         =     np.zeros(images[idx].shape					)

					return images, nsfw_detected, watermark_detected


