
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       ,       _UpperCamelCase       ,       _UpperCamelCase=14       ,       _UpperCamelCase=7       ,       _UpperCamelCase=True       ,       _UpperCamelCase=True       ,       _UpperCamelCase=True       ,       _UpperCamelCase=True       ,       _UpperCamelCase=True       ,       _UpperCamelCase=99       ,       _UpperCamelCase=32       ,       _UpperCamelCase=5       ,       _UpperCamelCase=4       ,       _UpperCamelCase=37       ,       _UpperCamelCase="gelu"       ,       _UpperCamelCase=0.1       ,       _UpperCamelCase=0.1       ,       _UpperCamelCase=512       ,       _UpperCamelCase=16       ,       _UpperCamelCase=2       ,       _UpperCamelCase=0.02       ,       _UpperCamelCase=3       ,       _UpperCamelCase=4       ,       _UpperCamelCase=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   __a		(      self  )       ->   Any:
							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   __a		(      self  )       ->   str:
							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       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       *_UpperCamelCase  )       ->   Optional[int]:
							lowerCAmelCase_      			=			CTRLModel(config=_UpperCamelCase  )
							model.to(_UpperCamelCase  )
							model.eval()

							model(_UpperCamelCase       ,       token_type_ids=_UpperCamelCase       ,       head_mask=_UpperCamelCase  )
							model(_UpperCamelCase       ,       token_type_ids=_UpperCamelCase  )
							lowerCAmelCase_      			=			model(_UpperCamelCase  )
							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       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       *_UpperCamelCase  )       ->   int:
							lowerCAmelCase_      			=			CTRLLMHeadModel(_UpperCamelCase  )
							model.to(_UpperCamelCase  )
							model.eval()

							lowerCAmelCase_      			=			model(_UpperCamelCase       ,       token_type_ids=_UpperCamelCase       ,       labels=_UpperCamelCase  )
							self.parent.assertEqual(result.loss.shape       ,       ()  )
							self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.seq_length, self.vocab_size)  )




				def   __a		(      self  )       ->   List[str]:
							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   __a		(      self       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       *_UpperCamelCase  )       ->   Tuple:
							lowerCAmelCase_      			=			self.num_labels
							lowerCAmelCase_      			=			CTRLForSequenceClassification(_UpperCamelCase  )
							model.to(_UpperCamelCase  )
							model.eval()
							lowerCAmelCase_      			=			ids_tensor([self.batch_size]       ,       self.type_sequence_label_size  )
							lowerCAmelCase_      			=			model(_UpperCamelCase       ,       token_type_ids=_UpperCamelCase       ,       labels=_UpperCamelCase  )
							self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.num_labels)  )





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




				def   __a		(      self       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase  )       ->   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   __a		(      self  )       ->   Any:
							lowerCAmelCase_      			=			CTRLModelTester(self  )
							lowerCAmelCase_      			=			ConfigTester(self       ,       config_class=_UpperCamelCase       ,       n_embd=37  )




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




				def   __a		(      self  )       ->   Dict:
							self.config_tester.run_common_tests()




				def   __a		(      self  )       ->   str:
							lowerCAmelCase_      			=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_ctrl_model(*_UpperCamelCase  )




				def   __a		(      self  )       ->   Dict:
							lowerCAmelCase_      			=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_lm_head_model(*_UpperCamelCase  )




				@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."  )
				def   __a		(      self  )       ->   Optional[Any]:
							pass




				@slow
				def   __a		(      self  )       ->   Union[str, Any]:
							for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase_      			=			CTRLModel.from_pretrained(_UpperCamelCase  )
										self.assertIsNotNone(_UpperCamelCase  )




				@unittest.skip("The model doesn't support left padding"  )  # and it's not used enough to be worth fixing :)
				def   __a		(      self  )       ->   Union[str, Any]:
							pass





@require_torch
class 			_lowerCAmelCase      (			unittest.TestCase ):




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




				@slow
				def   __a		(      self  )       ->   str:
							lowerCAmelCase_      			=			CTRLLMHeadModel.from_pretrained("ctrl"  )
							model.to(_UpperCamelCase  )
							lowerCAmelCase_      			=			torch.tensor(
							    [[11_859, 0, 1_611, 8]]       ,       dtype=torch.long       ,       device=_UpperCamelCase  )  # Legal the president is
							lowerCAmelCase_      			=			[
							    11_859,
							    0,
							    1_611,
							    8,
							    5,
							    150,
							    26_449,
							    2,
							    19,
							    348,
							    469,
							    3,
							    2_595,
							    48,
							    20_740,
							    246_533,
							    246_533,
							    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(_UpperCamelCase       ,       do_sample=_UpperCamelCase  )
							self.assertListEqual(output_ids[0].tolist()       ,       _UpperCamelCase  )





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_A     =  logging.get_logger(__name__)

_A     =  {
    "bert-base-uncased": "https://huggingface.co/bert-base-uncased/resolve/main/config.json",
    "bert-large-uncased": "https://huggingface.co/bert-large-uncased/resolve/main/config.json",
    "bert-base-cased": "https://huggingface.co/bert-base-cased/resolve/main/config.json",
    "bert-large-cased": "https://huggingface.co/bert-large-cased/resolve/main/config.json",
    "bert-base-multilingual-uncased": "https://huggingface.co/bert-base-multilingual-uncased/resolve/main/config.json",
    "bert-base-multilingual-cased": "https://huggingface.co/bert-base-multilingual-cased/resolve/main/config.json",
    "bert-base-chinese": "https://huggingface.co/bert-base-chinese/resolve/main/config.json",
    "bert-base-german-cased": "https://huggingface.co/bert-base-german-cased/resolve/main/config.json",
    "bert-large-uncased-whole-word-masking": (
        "https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/config.json"
    ),
    "bert-large-cased-whole-word-masking": (
        "https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/config.json"
    ),
    "bert-large-uncased-whole-word-masking-finetuned-squad": (
        "https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/config.json"
    ),
    "bert-large-cased-whole-word-masking-finetuned-squad": (
        "https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/config.json"
    ),
    "bert-base-cased-finetuned-mrpc": "https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/config.json",
    "bert-base-german-dbmdz-cased": "https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/config.json",
    "bert-base-german-dbmdz-uncased": "https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/config.json",
    "cl-tohoku/bert-base-japanese": "https://huggingface.co/cl-tohoku/bert-base-japanese/resolve/main/config.json",
    "cl-tohoku/bert-base-japanese-whole-word-masking": (
        "https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking/resolve/main/config.json"
    ),
    "cl-tohoku/bert-base-japanese-char": (
        "https://huggingface.co/cl-tohoku/bert-base-japanese-char/resolve/main/config.json"
    ),
    "cl-tohoku/bert-base-japanese-char-whole-word-masking": (
        "https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking/resolve/main/config.json"
    ),
    "TurkuNLP/bert-base-finnish-cased-v1": (
        "https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/config.json"
    ),
    "TurkuNLP/bert-base-finnish-uncased-v1": (
        "https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/config.json"
    ),
    "wietsedv/bert-base-dutch-cased": "https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/config.json",
    # See all BERT models at https://huggingface.co/models?filter=bert
}





class 			_lowerCAmelCase      (			__a ):
				_lowercase    	='''bert'''




				def __init__(      self       ,       _UpperCamelCase=30_522       ,       _UpperCamelCase=768       ,       _UpperCamelCase=12       ,       _UpperCamelCase=12       ,       _UpperCamelCase=3_072       ,       _UpperCamelCase="gelu"       ,       _UpperCamelCase=0.1       ,       _UpperCamelCase=0.1       ,       _UpperCamelCase=512       ,       _UpperCamelCase=2       ,       _UpperCamelCase=0.02       ,       _UpperCamelCase=1e-1_2       ,       _UpperCamelCase=0       ,       _UpperCamelCase="absolute"       ,       _UpperCamelCase=True       ,       _UpperCamelCase=None       ,       **_UpperCamelCase       ,       )       ->   str:
							super().__init__(pad_token_id=_UpperCamelCase       ,       **_UpperCamelCase  )

							lowerCAmelCase_      			=			vocab_size
							lowerCAmelCase_      			=			hidden_size
							lowerCAmelCase_      			=			num_hidden_layers
							lowerCAmelCase_      			=			num_attention_heads
							lowerCAmelCase_      			=			hidden_act
							lowerCAmelCase_      			=			intermediate_size
							lowerCAmelCase_      			=			hidden_dropout_prob
							lowerCAmelCase_      			=			attention_probs_dropout_prob
							lowerCAmelCase_      			=			max_position_embeddings
							lowerCAmelCase_      			=			type_vocab_size
							lowerCAmelCase_      			=			initializer_range
							lowerCAmelCase_      			=			layer_norm_eps
							lowerCAmelCase_      			=			position_embedding_type
							lowerCAmelCase_      			=			use_cache
							lowerCAmelCase_      			=			classifier_dropout





class 			_lowerCAmelCase      (			__a ):




				@property
				def   __a		(      self  )       ->   Mapping[str, Mapping[int, str]]:
							if self.task == "multiple-choice":
										lowerCAmelCase_      			=			{0: "batch", 1: "choice", 2: "sequence"}
							else:
										lowerCAmelCase_      			=			{0: "batch", 1: "sequence"}
							return OrderedDict(
							    [
							        ("input_ids", dynamic_axis),
							        ("attention_mask", dynamic_axis),
							        ("token_type_ids", dynamic_axis),
							    ]  )



