



'''simple docstring'''





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	:


 """simple docstring"""

 def __init__(      self					,    _lowerCamelCase					,    )    ->    str:
    A_  :		str						=					parent
    A_  :		Tuple						=					13
    A_  :		Optional[int]						=					7
    A_  :		Any						=					True
    A_  :		Tuple						=					True
    A_  :		Union[str, Any]						=					True
    A_  :		int						=					True
    A_  :		Tuple						=					True
    A_  :		Tuple						=					False
    A_  :		Tuple						=					False
    A_  :		Optional[int]						=					False
    A_  :		str						=					2
    A_  :		str						=					99
    A_  :		Optional[int]						=					0
    A_  :		List[Any]						=					32
    A_  :		Tuple						=					2
    A_  :		List[str]						=					4
    A_  :		Tuple						=					0.1
    A_  :		Optional[int]						=					0.1
    A_  :		List[Any]						=					512
    A_  :		Tuple						=					16
    A_  :		Optional[Any]						=					2
    A_  :		str						=					0.02
    A_  :		int						=					3
    A_  :		Union[str, Any]						=					4
    A_  :		str						=					"""last"""
    A_  :		Dict						=					True
    A_  :		Any						=					None
    A_  :		List[str]						=					0







 def       UpperCAmelCase_       (      self  )    ->    Dict:
    A_  :		List[Any]						=					ids_tensor([self.batch_size, self.seq_length]					,    self.vocab_size  )
    A_  :		str						=					random_attention_mask([self.batch_size, self.seq_length]					,    dtype=tf.floataa  )

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

    A_  :		List[str]						=					None
    if self.use_token_type_ids:
       A_  :		Dict						=					ids_tensor([self.batch_size, self.seq_length]					,    self.n_langs  )

    A_  :		Any						=					None
    A_  :		str						=					None
    A_  :		List[Any]						=					None
    if self.use_labels:
       A_  :		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_  :		List[str]						=					ids_tensor([self.batch_size]					,    2					,    dtype=tf.floataa  )
       A_  :		str						=					ids_tensor([self.batch_size]					,    self.num_choices  )

    A_  :		Any						=					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       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    )    ->    List[Any]:
    A_  :		int						=					TFXLMModel(config=lowerCAmelCase__  )
    A_  :		Any						=					{"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
    A_  :		int						=					model(lowerCAmelCase__  )

    A_  :		List[str]						=					[input_ids, input_mask]
    A_  :		Optional[Any]						=					model(lowerCAmelCase__  )
    self.parent.assertEqual(result.last_hidden_state.shape					,    (self.batch_size, self.seq_length, self.hidden_size)  )







 def       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    )    ->    Union[str, Any]:
    A_  :		Dict						=					TFXLMWithLMHeadModel(lowerCAmelCase__  )

    A_  :		List[str]						=					{"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
    A_  :		List[str]						=					model(lowerCAmelCase__  )

    A_  :		List[Any]						=					outputs

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







 def       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    )    ->    List[Any]:
    A_  :		int						=					TFXLMForQuestionAnsweringSimple(lowerCAmelCase__  )

    A_  :		Union[str, Any]						=					{"""input_ids""": input_ids, """lengths""": input_lengths}

    A_  :		Tuple						=					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       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    )    ->    List[Any]:
    A_  :		List[str]						=					TFXLMForSequenceClassification(lowerCAmelCase__  )

    A_  :		List[str]						=					{"""input_ids""": input_ids, """lengths""": input_lengths}

    A_  :		Optional[Any]						=					model(lowerCAmelCase__  )

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







 def       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    )    ->    Optional[int]:
    A_  :		Union[str, Any]						=					self.num_labels
    A_  :		Optional[Any]						=					TFXLMForTokenClassification(config=lowerCAmelCase__  )
    A_  :		Tuple						=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
    A_  :		str						=					model(lowerCAmelCase__  )
    self.parent.assertEqual(result.logits.shape					,    (self.batch_size, self.seq_length, self.num_labels)  )







 def       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    )    ->    Any:
    A_  :		Tuple						=					self.num_choices
    A_  :		str						=					TFXLMForMultipleChoice(config=lowerCAmelCase__  )
    A_  :		int						=					tf.tile(tf.expand_dims(lowerCAmelCase__					,    1  )					,    (1, self.num_choices, 1)  )
    A_  :		Dict						=					tf.tile(tf.expand_dims(lowerCAmelCase__					,    1  )					,    (1, self.num_choices, 1)  )
    A_  :		Tuple						=					tf.tile(tf.expand_dims(lowerCAmelCase__					,    1  )					,    (1, self.num_choices, 1)  )
    A_  :		List[str]						=					{
        """input_ids""": multiple_choice_inputs_ids,
        """attention_mask""": multiple_choice_input_mask,
        """token_type_ids""": multiple_choice_token_type_ids,
    }
    A_  :		List[Any]						=					model(lowerCAmelCase__  )
    self.parent.assertEqual(result.logits.shape					,    (self.batch_size, self.num_choices)  )







 def       UpperCAmelCase_       (      self  )    ->    Any:
    A_  :		List[Any]						=					self.prepare_config_and_inputs()
    (
        (
        A_      
    )						,  (
        A_      
    )						,  (
        A_      
    )						,  (
        A_      
    )						,  (
        A_      
    )						,  (
        A_      
    )						,  (
        A_      
    )						,  (
        A_      
    )						,  (
        A_      
    )						,        
    )  :		Dict						=					config_and_inputs
    A_  :		Optional[int]						=					{
        """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							):


 """simple docstring"""

 lowerCamelCase									=     (
     (
         TFXLMModel,
         TFXLMWithLMHeadModel,
         TFXLMForSequenceClassification,
         TFXLMForQuestionAnsweringSimple,
         TFXLMForTokenClassification,
         TFXLMForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 lowerCamelCase									=     (
     (TFXLMWithLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Check other models whether language generation is also applicable
 lowerCamelCase									=     (
     {
         """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 {}
 )
 lowerCamelCase									=     False
 lowerCamelCase									=     False







 def       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase  )    ->    Tuple:
    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       UpperCAmelCase_       (      self  )    ->    List[str]:
    A_  :		List[Any]						=					TFXLMModelTester(self  )
    A_  :		Optional[int]						=					ConfigTester(self					,    config_class=lowerCAmelCase__					,    emb_dim=37  )







 def       UpperCAmelCase_       (      self  )    ->    Any:
    self.config_tester.run_common_tests()







 def       UpperCAmelCase_       (      self  )    ->    Optional[Any]:
    A_  :		Optional[Any]						=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_model(*lowerCAmelCase__  )







 def       UpperCAmelCase_       (      self  )    ->    int:
    A_  :		Optional[int]						=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_lm_head(*lowerCAmelCase__  )







 def       UpperCAmelCase_       (      self  )    ->    Optional[int]:
    A_  :		Union[str, Any]						=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_qa(*lowerCAmelCase__  )







 def       UpperCAmelCase_       (      self  )    ->    str:
    A_  :		Union[str, Any]						=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_sequence_classif(*lowerCAmelCase__  )







 def       UpperCAmelCase_       (      self  )    ->    Union[str, Any]:
    A_  :		Optional[int]						=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_for_token_classification(*lowerCAmelCase__  )







 def       UpperCAmelCase_       (      self  )    ->    Tuple:
    A_  :		Optional[Any]						=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCAmelCase__  )







 @slow
 def       UpperCAmelCase_       (      self  )    ->    Union[str, Any]:
    for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       A_  :		Tuple						=					TFXLMModel.from_pretrained(lowerCAmelCase__  )
       self.assertIsNotNone(lowerCAmelCase__  )


@require_tf
class 							_lowerCAmelCase	(  unittest.TestCase							):


 """simple docstring"""

 @slow
 def       UpperCAmelCase_       (      self  )    ->    Optional[int]:
    A_  :		str						=					TFXLMWithLMHeadModel.from_pretrained("""xlm-mlm-en-2048"""  )
    A_  :		Tuple						=					tf.convert_to_tensor([[14, 447]]					,    dtype=tf.intaa  )  # the president
    A_  :		List[str]						=					[
        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_  :		Dict						=					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")