





from __future__ import annotations

import unittest

from transformers import CTRLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers.models.ctrl.modeling_tf_ctrl import (
							    TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TFCTRLForSequenceClassification,
							    TFCTRLLMHeadModel,
							    TFCTRLModel,
							)

class    __A  (    a     ):


				def __init__(		self ,			UpperCAmelCase_ ,			):
								lowerCamelCase									=parent
								lowerCamelCase									=13
								lowerCamelCase									=7
								lowerCamelCase									=True
								lowerCamelCase									=True
								lowerCamelCase									=True
								lowerCamelCase									=True
								lowerCamelCase									=True
								lowerCamelCase									=99
								lowerCamelCase									=32
								lowerCamelCase									=2
								lowerCamelCase									=4
								lowerCamelCase									=37
								lowerCamelCase									="""gelu"""
								lowerCamelCase									=0.1
								lowerCamelCase									=0.1
								lowerCamelCase									=512
								lowerCamelCase									=16
								lowerCamelCase									=2
								lowerCamelCase									=0.0_2
								lowerCamelCase									=3
								lowerCamelCase									=4
								lowerCamelCase									=None
								lowerCamelCase									=self.vocab_size - 1


				def _snake_case							(		self				):
								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									=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 ,			)

								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 _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			*UpperCAmelCase_				):
								lowerCamelCase									=TFCTRLModel(config=UpperCAmelCase_				)
								lowerCamelCase									={"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								lowerCamelCase									=model(UpperCAmelCase_				)

								lowerCamelCase									=[input_ids, None, input_mask]  # None is the input for 'past'
								lowerCamelCase									=model(UpperCAmelCase_				)

								lowerCamelCase									=model(UpperCAmelCase_				)

								self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)				)


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			*UpperCAmelCase_				):
								lowerCamelCase									=TFCTRLLMHeadModel(config=UpperCAmelCase_				)
								lowerCamelCase									={"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
								lowerCamelCase									=model(UpperCAmelCase_				)
								self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.vocab_size)				)


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			*UpperCAmelCase_				):
								lowerCamelCase									=self.num_labels
								lowerCamelCase									=ids_tensor([self.batch_size] ,			self.type_sequence_label_size				)
								lowerCamelCase									={
								    """input_ids""": input_ids,
								    """token_type_ids""": token_type_ids,
								    """labels""": sequence_labels,
								}
								lowerCamelCase									=TFCTRLForSequenceClassification(UpperCAmelCase_				)
								lowerCamelCase									=model(UpperCAmelCase_				)
								self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_labels)				)


				def _snake_case							(		self				):
								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, """attention_mask""": input_mask}
								return config, inputs_dict




@require_tf
class    __A  (    a     ,       a     ,       unittest.TestCase     ):
				__A  =					(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
				__A  =					(TFCTRLLMHeadModel,) if is_tf_available() else ()
				__A  =					(
				    {
				        """feature-extraction""": TFCTRLModel,
				        """text-classification""": TFCTRLForSequenceClassification,
				        """text-generation""": TFCTRLLMHeadModel,
				        """zero-shot""": TFCTRLForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				__A  =					False
				__A  =					False


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
								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 _snake_case							(		self				):
								lowerCamelCase									=TFCTRLModelTester(self				)
								lowerCamelCase									=ConfigTester(self ,			config_class=UpperCAmelCase_ ,			n_embd=37				)


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


				def _snake_case							(		self				):
								lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase_				)


				def _snake_case							(		self				):
								lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_lm_head(*UpperCAmelCase_				)


				def _snake_case							(		self				):
								lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_for_sequence_classification(*UpperCAmelCase_				)


				def _snake_case							(		self				):
								lowerCamelCase			,	lowerCamelCase									=self.model_tester.prepare_config_and_inputs_for_common()
								lowerCamelCase									=[TFCTRLLMHeadModel]
								lowerCamelCase									=[TFCTRLForSequenceClassification]

								for model_class in self.all_model_classes:
												lowerCamelCase									=model_class(UpperCAmelCase_				)
												model.build()  # may be needed for the get_bias() call below
												assert isinstance(model.get_input_embeddings() ,			tf.keras.layers.Layer				)

												if model_class in list_lm_models:
																lowerCamelCase									=model.get_output_embeddings()
																assert isinstance(UpperCAmelCase_ ,			tf.keras.layers.Layer				)
																lowerCamelCase									=model.get_bias()
																assert isinstance(UpperCAmelCase_ ,			UpperCAmelCase_				)
																for k, v in name.items():
																				assert isinstance(UpperCAmelCase_ ,			tf.Variable				)
												elif model_class in list_other_models_with_output_ebd:
																lowerCamelCase									=model.get_output_embeddings()
																assert isinstance(UpperCAmelCase_ ,			tf.keras.layers.Layer				)
																lowerCamelCase									=model.get_bias()
																assert name is None
												else:
																lowerCamelCase									=model.get_output_embeddings()
																assert x is None
																lowerCamelCase									=model.get_bias()
																assert name is None


				@slow
				def _snake_case							(		self				):
								for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowerCamelCase									=TFCTRLModel.from_pretrained(UpperCAmelCase_				)
												self.assertIsNotNone(UpperCAmelCase_				)




@require_tf
class    __A  (    unittest.TestCase     ):


				@slow
				def _snake_case							(		self				):
								lowerCamelCase									=TFCTRLLMHeadModel.from_pretrained("""ctrl"""				)
								lowerCamelCase									=tf.convert_to_tensor([[11859, 0, 1611, 8]] ,			dtype=tf.intaa				)  # 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(UpperCAmelCase_ ,			do_sample=UpperCAmelCase_				)
								self.assertListEqual(output_ids[0].numpy().tolist() ,			UpperCAmelCase_				)












import argparse
import os


UpperCAmelCase__    :      Any       					={
    '''pytorch''': {
        '''1.13''': {
            '''torch''': '''1.13.1''',
            '''torchvision''': '''0.14.1''',
            '''torchaudio''': '''0.13.1''',
            '''python''': 3.9,
            '''cuda''': '''cu116''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu116'''
            ),
            '''base_image''': '''nvidia/cuda:11.6.2-cudnn8-devel-ubuntu20.04''',
        },
        '''1.12''': {
            '''torch''': '''1.12.1''',
            '''torchvision''': '''0.13.1''',
            '''torchaudio''': '''0.12.1''',
            '''python''': 3.9,
            '''cuda''': '''cu113''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu113'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''1.11''': {
            '''torch''': '''1.11.0''',
            '''torchvision''': '''0.12.0''',
            '''torchaudio''': '''0.11.0''',
            '''python''': 3.9,
            '''cuda''': '''cu113''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu113'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''1.10''': {
            '''torch''': '''1.10.2''',
            '''torchvision''': '''0.11.3''',
            '''torchaudio''': '''0.10.2''',
            '''python''': 3.9,
            '''cuda''': '''cu113''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.10.2 torchvision==0.11.3 torchaudio==0.10.2'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu113'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        # torchaudio < 0.10 has no CUDA-enabled binary distributions
        '''1.9''': {
            '''torch''': '''1.9.1''',
            '''torchvision''': '''0.10.1''',
            '''torchaudio''': '''0.9.1''',
            '''python''': 3.9,
            '''cuda''': '''cu111''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.9.1 torchvision==0.10.1 torchaudio==0.9.1'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu111'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
    },
    '''tensorflow''': {
        '''2.11''': {
            '''tensorflow''': '''2.11.1''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.11.1''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.10''': {
            '''tensorflow''': '''2.10.1''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.10.1''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.9''': {
            '''tensorflow''': '''2.9.3''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.9.3''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.8''': {
            '''tensorflow''': '''2.8.2''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.8.2''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.7''': {
            '''tensorflow''': '''2.7.3''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.7.3''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.6''': {
            '''tensorflow''': '''2.6.5''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.6.5''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.5''': {
            '''tensorflow''': '''2.5.3''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.5.3''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
    },
}


if __name__ == "__main__":
							UpperCAmelCase__    :      Tuple       					=argparse.ArgumentParser('''Choose the framework and version to install''')
							parser.add_argument(
							    '''--framework''', help='''The framework to install. Should be `torch` or `tensorflow`''', type=str, required=True
							)
							parser.add_argument('''--version''', help='''The version of the framework to install.''', type=str, required=True)
							UpperCAmelCase__    :      List[str]       					=parser.parse_args()

							UpperCAmelCase__    :      Dict       					=past_versions_testing[args.framework][args.version]

							os.system(F"echo \"export INSTALL_CMD=\'{info['install']}\'\" >> ~/.profile")
							print(F"echo \"export INSTALL_CMD=\'{info['install']}\'\" >> ~/.profile")

							UpperCAmelCase__    :      List[Any]       					=''''''
							if args.framework == "pytorch":
														UpperCAmelCase__    :      Optional[int]       					=info['''cuda''']
							os.system(F"echo \"export CUDA='{cuda}'\" >> ~/.profile")
							print(F"echo \"export CUDA='{cuda}'\" >> ~/.profile")





