from __future__ import annotations

import unittest

from transformers import 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 import (
          TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFXLMForMultipleChoice,
          TFXLMForQuestionAnsweringSimple,
          TFXLMForSequenceClassification,
          TFXLMForTokenClassification,
          TFXLMModel,
          TFXLMWithLMHeadModel,
          XLMConfig,
      )





class   lowerCAmelCase    :



     def __init__(       self					,   lowerCAmelCase__					,   ):
            _A=						parent
            _A=						13
            _A=						7
            _A=						True
            _A=						True
            _A=						True
            _A=						True
            _A=						True
            _A=						False
            _A=						False
            _A=						False
            _A=						2
            _A=						99
            _A=						0
            _A=						32
            _A=						2
            _A=						4
            _A=						0.1
            _A=						0.1
            _A=						512
            _A=						16
            _A=						2
            _A=						0.02
            _A=						3
            _A=						4
            _A=						'last'
            _A=						True
            _A=						None
            _A=						0



     def       a__	(       self							):
            _A=						ids_tensor([self.batch_size, self.seq_length]					,   self.vocab_size							)
            _A=						random_attention_mask([self.batch_size, self.seq_length]					,   dtype=tf.floataa							)

            _A=						None
            if self.use_input_lengths:
                   _A=						(
                       ids_tensor([self.batch_size]					,   vocab_size=2							) + self.seq_length - 2
                   )  # small variation of seq_length

            _A=						None
            if self.use_token_type_ids:
                   _A=						ids_tensor([self.batch_size, self.seq_length]					,   self.n_langs							)

            _A=						None
            _A=						None
            _A=						None
            if self.use_labels:
                   _A=						ids_tensor([self.batch_size]					,   self.type_sequence_label_size							)
                   _A=						ids_tensor([self.batch_size, self.seq_length]					,   self.num_labels							)
                   _A=						ids_tensor([self.batch_size]					,   2					,   dtype=tf.floataa							)
                   _A=						ids_tensor([self.batch_size]					,   self.num_choices							)

            _A=						XLMConfig(
                vocab_size=self.vocab_size					,   n_special=self.n_special					,   emb_dim=self.hidden_size					,   n_layers=self.num_hidden_layers					,   n_heads=self.num_attention_heads					,   dropout=self.hidden_dropout_prob					,   attention_dropout=self.attention_probs_dropout_prob					,   gelu_activation=self.gelu_activation					,   sinusoidal_embeddings=self.sinusoidal_embeddings					,   asm=self.asm					,   causal=self.causal					,   n_langs=self.n_langs					,   max_position_embeddings=self.max_position_embeddings					,   initializer_range=self.initializer_range					,   summary_type=self.summary_type					,   use_proj=self.use_proj					,   bos_token_id=self.bos_token_id					,   )

            return (
                config,
                input_ids,
                token_type_ids,
                input_lengths,
                sequence_labels,
                token_labels,
                is_impossible_labels,
                choice_labels,
                input_mask,
            )



     def       a__	(       self					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   ):
            _A=						TFXLMModel(config=lowerCAmelCase__							)
            _A=						{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
            _A=						model(lowerCAmelCase__							)

            _A=						[input_ids, input_mask]
            _A=						model(lowerCAmelCase__							)
            self.parent.assertEqual(result.last_hidden_state.shape					,   (self.batch_size, self.seq_length, self.hidden_size)							)



     def       a__	(       self					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   ):
            _A=						TFXLMWithLMHeadModel(lowerCAmelCase__							)

            _A=						{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
            _A=						model(lowerCAmelCase__							)

            _A=						outputs

            self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.vocab_size)							)



     def       a__	(       self					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   ):
            _A=						TFXLMForQuestionAnsweringSimple(lowerCAmelCase__							)

            _A=						{'input_ids': input_ids, 'lengths': input_lengths}

            _A=						model(lowerCAmelCase__							)

            self.parent.assertEqual(result.start_logits.shape					,   (self.batch_size, self.seq_length)							)
            self.parent.assertEqual(result.end_logits.shape					,   (self.batch_size, self.seq_length)							)



     def       a__	(       self					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   ):
            _A=						TFXLMForSequenceClassification(lowerCAmelCase__							)

            _A=						{'input_ids': input_ids, 'lengths': input_lengths}

            _A=						model(lowerCAmelCase__							)

            self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.type_sequence_label_size)							)



     def       a__	(       self					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   ):
            _A=						self.num_labels
            _A=						TFXLMForTokenClassification(config=lowerCAmelCase__							)
            _A=						{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
            _A=						model(lowerCAmelCase__							)
            self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.num_labels)							)



     def       a__	(       self					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   ):
            _A=						self.num_choices
            _A=						TFXLMForMultipleChoice(config=lowerCAmelCase__							)
            _A=						tf.tile(tf.expand_dims(lowerCAmelCase__					,   1							)					,   (1, self.num_choices, 1)							)
            _A=						tf.tile(tf.expand_dims(lowerCAmelCase__					,   1							)					,   (1, self.num_choices, 1)							)
            _A=						tf.tile(tf.expand_dims(lowerCAmelCase__					,   1							)					,   (1, self.num_choices, 1)							)
            _A=						{
                'input_ids': multiple_choice_inputs_ids,
                'attention_mask': multiple_choice_input_mask,
                'token_type_ids': multiple_choice_token_type_ids,
            }
            _A=						model(lowerCAmelCase__							)
            self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.num_choices)							)





     def       a__	(       self							):
            _A=						self.prepare_config_and_inputs()
            (
                (
                _A
            ),      (
                _A
            ),      (
                _A
            ),      (
                _A
            ),      (
                _A
            ),      (
                _A
            ),      (
                _A
            ),      (
                _A
            ),      (
                _A
            ),      
            )=						config_and_inputs
            _A=						{
                'input_ids': input_ids,
                'token_type_ids': token_type_ids,
                'langs': token_type_ids,
                'lengths': input_lengths,
            }
            return config, inputs_dict





@require_tf
class   lowerCAmelCase    (       _a      ,		_a      ,		unittest.TestCase      ):
     _SCREAMING_SNAKE_CASE  :  Optional[Any]		=(
         (
             TFXLMModel,
             TFXLMWithLMHeadModel,
             TFXLMForSequenceClassification,
             TFXLMForQuestionAnsweringSimple,
             TFXLMForTokenClassification,
             TFXLMForMultipleChoice,
         )
         if is_tf_available()
         else ()
     )
     _SCREAMING_SNAKE_CASE  :  str		=(
         (TFXLMWithLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Check other models whether language generation is also applicable
     _SCREAMING_SNAKE_CASE  :  List[Any]		=(
         {
             """feature-extraction""": TFXLMModel,
             """fill-mask""": TFXLMWithLMHeadModel,
             """question-answering""": TFXLMForQuestionAnsweringSimple,
             """text-classification""": TFXLMForSequenceClassification,
             """text-generation""": TFXLMWithLMHeadModel,
             """token-classification""": TFXLMForTokenClassification,
             """zero-shot""": TFXLMForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     _SCREAMING_SNAKE_CASE  :  List[str]		=False
     _SCREAMING_SNAKE_CASE  :  List[str]		=False



     def       a__	(       self					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__					,   lowerCAmelCase__							):
            if (
                pipeline_test_casse_name == "QAPipelineTests"
                and tokenizer_name is not None
                and not tokenizer_name.endswith('Fast'							)
            ):
                   # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
                   # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
                   # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
                   return True

            return False



     def       a__	(       self							):
            _A=						TFXLMModelTester(self							)
            _A=						ConfigTester(self					,   config_class=lowerCAmelCase__					,   emb_dim=37							)



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



     def       a__	(       self							):
            _A=						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_model(*lowerCAmelCase__							)



     def       a__	(       self							):
            _A=						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_lm_head(*lowerCAmelCase__							)



     def       a__	(       self							):
            _A=						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_qa(*lowerCAmelCase__							)



     def       a__	(       self							):
            _A=						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_sequence_classif(*lowerCAmelCase__							)



     def       a__	(       self							):
            _A=						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_for_token_classification(*lowerCAmelCase__							)



     def       a__	(       self							):
            _A=						self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCAmelCase__							)





     @slow
     def       a__	(       self							):
            for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _A=						TFXLMModel.from_pretrained(lowerCAmelCase__							)
                   self.assertIsNotNone(lowerCAmelCase__							)





@require_tf
class   lowerCAmelCase    (       unittest.TestCase      ):



     @slow
     def       a__	(       self							):
            _A=						TFXLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048'							)
            _A=						tf.convert_to_tensor([[14, 447]]					,   dtype=tf.intaa							)  # the president
            _A=						[
                14,
                447,
                14,
                447,
                14,
                447,
                14,
                447,
                14,
                447,
                14,
                447,
                14,
                447,
                14,
                447,
                14,
                447,
                14,
                447,
            ]  # the president the president the president the president the president the president the president the president the president the president
            # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
            _A=						model.generate(lowerCAmelCase__					,   do_sample=lowerCAmelCase__							)
            self.assertListEqual(output_ids[0].numpy().tolist()					,   lowerCAmelCase__							)
import argparse
import os


UpperCAmelCase_    		=				{
    '''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_    		=				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_    		=				parser.parse_args()

      UpperCAmelCase_    		=				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_    		=				''''''
      if args.framework == "pytorch":
            UpperCAmelCase_    		=				info['''cuda''']
      os.system(F"echo \"export CUDA='{cuda}'\" >> ~/.profile")
      print(F"echo \"export CUDA='{cuda}'\" >> ~/.profile")