






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



 def __init__(						self : Union[str, Any]			,		UpperCAmelCase__ : Optional[Any]			,		):

    """simple docstring"""






    lowercase__    :						Union[str, Any]       =			parent
    lowercase__    :						str       =			13
    lowercase__    :						Optional[int]       =			7
    lowercase__    :						List[str]       =			True
    lowercase__    :						int       =			True
    lowercase__    :						Optional[Any]       =			True
    lowercase__    :						Tuple       =			True
    lowercase__    :						List[str]       =			True
    lowercase__    :						Union[str, Any]       =			False
    lowercase__    :						Optional[Any]       =			False
    lowercase__    :						List[str]       =			False
    lowercase__    :						Union[str, Any]       =			2
    lowercase__    :						Optional[int]       =			99
    lowercase__    :						Optional[Any]       =			0
    lowercase__    :						Tuple       =			32
    lowercase__    :						Union[str, Any]       =			2
    lowercase__    :						Optional[Any]       =			4
    lowercase__    :						Optional[int]       =			0.1
    lowercase__    :						List[str]       =			0.1
    lowercase__    :						int       =			512
    lowercase__    :						List[Any]       =			16
    lowercase__    :						Dict       =			2
    lowercase__    :						List[str]       =			0.02
    lowercase__    :						str       =			3
    lowercase__    :						Optional[Any]       =			4
    lowercase__    :						Optional[Any]       =			'last'
    lowercase__    :						Any       =			True
    lowercase__    :						Union[str, Any]       =			None
    lowercase__    :						int       =			0



 def     UpperCAmelCase     (						self : Optional[int]						):

    """simple docstring"""






    lowercase__    :						Dict       =			ids_tensor([self.batch_size, self.seq_length]			,		self.vocab_size						)
    lowercase__    :						Dict       =			random_attention_mask([self.batch_size, self.seq_length]			,		dtype=tf.floataa						)

    lowercase__    :						Union[str, Any]       =			None
    if self.use_input_lengths:
       lowercase__    :						List[str]       =			(
           ids_tensor([self.batch_size]			,		vocab_size=2						) + self.seq_length - 2
       )  # small variation of seq_length

    lowercase__    :						Optional[Any]       =			None
    if self.use_token_type_ids:
       lowercase__    :						str       =			ids_tensor([self.batch_size, self.seq_length]			,		self.n_langs						)

    lowercase__    :						Optional[int]       =			None
    lowercase__    :						Optional[Any]       =			None
    lowercase__    :						Tuple       =			None
    if self.use_labels:
       lowercase__    :						Union[str, Any]       =			ids_tensor([self.batch_size]			,		self.type_sequence_label_size						)
       lowercase__    :						Any       =			ids_tensor([self.batch_size, self.seq_length]			,		self.num_labels						)
       lowercase__    :						Dict       =			ids_tensor([self.batch_size]			,		2			,		dtype=tf.floataa						)
       lowercase__    :						Tuple       =			ids_tensor([self.batch_size]			,		self.num_choices						)

    lowercase__    :						str       =			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 : List[str]			,		UpperCAmelCase__ : Union[str, Any]			,		UpperCAmelCase__ : Optional[Any]			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : Union[str, Any]			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : Optional[int]			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : Optional[int]			,		):

    """simple docstring"""






    lowercase__    :						int       =			TFXLMModel(config=UpperCAmelCase__						)
    lowercase__    :						Optional[Any]       =			{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
    lowercase__    :						Dict       =			model(UpperCAmelCase__						)

    lowercase__    :						List[str]       =			[input_ids, input_mask]
    lowercase__    :						Union[str, Any]       =			model(UpperCAmelCase__						)
    self.parent.assertEqual(result.last_hidden_state.shape			,		(self.batch_size, self.seq_length, self.hidden_size)						)



 def     UpperCAmelCase     (						self : str			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : int			,		UpperCAmelCase__ : int			,		UpperCAmelCase__ : Any			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : Tuple			,		UpperCAmelCase__ : str			,		UpperCAmelCase__ : List[str]			,		):

    """simple docstring"""






    lowercase__    :						Union[str, Any]       =			TFXLMWithLMHeadModel(UpperCAmelCase__						)

    lowercase__    :						str       =			{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
    lowercase__    :						List[str]       =			model(UpperCAmelCase__						)

    lowercase__    :						Union[str, Any]       =			outputs

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



 def     UpperCAmelCase     (						self : List[str]			,		UpperCAmelCase__ : int			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : Tuple			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : str			,		UpperCAmelCase__ : Union[str, Any]			,		UpperCAmelCase__ : Optional[Any]			,		UpperCAmelCase__ : Union[str, Any]			,		):

    """simple docstring"""






    lowercase__    :						Any       =			TFXLMForQuestionAnsweringSimple(UpperCAmelCase__						)

    lowercase__    :						Optional[Any]       =			{'input_ids': input_ids, 'lengths': input_lengths}

    lowercase__    :						Any       =			model(UpperCAmelCase__						)

    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 : Optional[int]			,		UpperCAmelCase__ : Optional[int]			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : int			,		UpperCAmelCase__ : Any			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : str			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : List[str]			,		):

    """simple docstring"""






    lowercase__    :						Dict       =			TFXLMForSequenceClassification(UpperCAmelCase__						)

    lowercase__    :						Optional[int]       =			{'input_ids': input_ids, 'lengths': input_lengths}

    lowercase__    :						Tuple       =			model(UpperCAmelCase__						)

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



 def     UpperCAmelCase     (						self : int			,		UpperCAmelCase__ : Optional[Any]			,		UpperCAmelCase__ : str			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : Optional[Any]			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : str			,		):

    """simple docstring"""






    lowercase__    :						Any       =			self.num_labels
    lowercase__    :						int       =			TFXLMForTokenClassification(config=UpperCAmelCase__						)
    lowercase__    :						Union[str, Any]       =			{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
    lowercase__    :						int       =			model(UpperCAmelCase__						)
    self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length, self.num_labels)						)



 def     UpperCAmelCase     (						self : Optional[Any]			,		UpperCAmelCase__ : Any			,		UpperCAmelCase__ : Optional[Any]			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : Any			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : str			,		):

    """simple docstring"""






    lowercase__    :						Tuple       =			self.num_choices
    lowercase__    :						List[str]       =			TFXLMForMultipleChoice(config=UpperCAmelCase__						)
    lowercase__    :						List[str]       =			tf.tile(tf.expand_dims(UpperCAmelCase__			,		1						)			,		(1, self.num_choices, 1)						)
    lowercase__    :						str       =			tf.tile(tf.expand_dims(UpperCAmelCase__			,		1						)			,		(1, self.num_choices, 1)						)
    lowercase__    :						Union[str, Any]       =			tf.tile(tf.expand_dims(UpperCAmelCase__			,		1						)			,		(1, self.num_choices, 1)						)
    lowercase__    :						str       =			{
        'input_ids': multiple_choice_inputs_ids,
        'attention_mask': multiple_choice_input_mask,
        'token_type_ids': multiple_choice_token_type_ids,
    }
    lowercase__    :						str       =			model(UpperCAmelCase__						)
    self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_choices)						)







 def     UpperCAmelCase     (						self : Any						):

    """simple docstring"""






    lowercase__    :						Union[str, Any]       =			self.prepare_config_and_inputs()
    (
        lowercase__	
    )    :						Dict       =			config_and_inputs
    lowercase__    :						Tuple       =			{
        'input_ids': input_ids,
        'token_type_ids': token_type_ids,
        'langs': token_type_ids,
        'lengths': input_lengths,
    }
    return config, inputs_dict
@require_tf
class 							__snake_case(       __UpperCamelCase  ,							__UpperCamelCase  ,							unittest.TestCase   ):
 lowercase			:							Optional[Any]		   =		(
     (
         TFXLMModel,
         TFXLMWithLMHeadModel,
         TFXLMForSequenceClassification,
         TFXLMForQuestionAnsweringSimple,
         TFXLMForTokenClassification,
         TFXLMForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 lowercase			:							str		   =		(
     (TFXLMWithLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Check other models whether language generation is also applicable
 lowercase			:							Dict		   =		(
     {
         '''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 {}
 )
 lowercase			:							Dict		   =		False
 lowercase			:							str		   =		False



 def     UpperCAmelCase     (						self : Any			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : Optional[Any]			,		UpperCAmelCase__ : str			,		UpperCAmelCase__ : Optional[Any]			,		UpperCAmelCase__ : List[str]						):

    """simple docstring"""






    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 : int						):

    """simple docstring"""






    lowercase__    :						str       =			TFXLMModelTester(self						)
    lowercase__    :						int       =			ConfigTester(self			,		config_class=UpperCAmelCase__			,		emb_dim=37						)



 def     UpperCAmelCase     (						self : str						):

    """simple docstring"""






    self.config_tester.run_common_tests()



 def     UpperCAmelCase     (						self : int						):

    """simple docstring"""






    lowercase__    :						Optional[int]       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_model(*UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : Union[str, Any]						):

    """simple docstring"""






    lowercase__    :						Optional[Any]       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_lm_head(*UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : List[Any]						):

    """simple docstring"""






    lowercase__    :						Optional[int]       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_qa(*UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : int						):

    """simple docstring"""






    lowercase__    :						List[str]       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_sequence_classif(*UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : Any						):

    """simple docstring"""






    lowercase__    :						str       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_for_token_classification(*UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : Tuple						):

    """simple docstring"""






    lowercase__    :						Tuple       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCAmelCase__						)







 @slow
 def     UpperCAmelCase     (						self : Optional[int]						):

    """simple docstring"""






    for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       lowercase__    :						Any       =			TFXLMModel.from_pretrained(UpperCAmelCase__						)
       self.assertIsNotNone(UpperCAmelCase__						)
@require_tf
class 							__snake_case(       unittest.TestCase   ):



 @slow
 def     UpperCAmelCase     (						self : Dict						):

    """simple docstring"""






    lowercase__    :						List[Any]       =			TFXLMWithLMHeadModel.from_pretrained("""xlm-mlm-en-2048"""						)
    lowercase__    :						Optional[int]       =			tf.convert_to_tensor([[14, 447]]			,		dtype=tf.intaa						)  # the president
    lowercase__    :						List[Any]       =			[
        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
    lowercase__    :						Optional[Any]       =			model.generate(UpperCAmelCase__			,		do_sample=UpperCAmelCase__						)
    self.assertListEqual(output_ids[0].numpy().tolist()			,		UpperCAmelCase__						)










"""simple docstring"""


# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path

from .config_args import default_config_file, load_config_from_file
from .config_utils import SubcommandHelpFormatter


_UpperCamelCase							:       int       					=		'Update an existing config file with the latest defaults while maintaining the old configuration.'




def      snake_case    (A_ :str ):







  '''simple docstring'''

  a	:     int							=  args.config_file
  if config_file is None and Path(A_ ).exists():
    a	:     List[Any]							=  default_config_file
  elif not Path(A_ ).exists():
    raise ValueError(f'''The passed config file located at {config_file} doesn\'t exist.''' )
  a	:     Dict							=  load_config_from_file(A_ )

  if config_file.endswith('.json' ):
    config.to_json_file(A_ )
  else:
    config.to_yaml_file(A_ )
  return config_file




def      snake_case    (A_ :List[str]      ,     A_ :Union[str, Any] ):







  '''simple docstring'''

  a	:     int							=  parser.add_parser('update'      ,     parents=A_      ,     help=A_      ,     formatter_class=A_ )
  parser.add_argument(
      '--config_file'      ,     default=A_      ,     help=(
          'The path to the config file to update. Will default to a file named default_config.yaml in the cache '
          'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
          'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
          'with \'huggingface\'.'
      )      ,     )

  parser.set_defaults(func=A_ )
  return parser






def      snake_case    (A_ :List[str] ):







  '''simple docstring'''

  a	:     List[str]							=  update_config(A_ )
  print(f'''Sucessfully updated the configuration file at {config_file}.''' )




