





from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, 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 numpy as np
   import tensorflow as tf

   from transformers import (
       TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
       FlaubertConfig,
       TFFlaubertForMultipleChoice,
       TFFlaubertForQuestionAnsweringSimple,
       TFFlaubertForSequenceClassification,
       TFFlaubertForTokenClassification,
       TFFlaubertModel,
       TFFlaubertWithLMHeadModel,
   )



class        _lowerCAmelCase    :


  '''simple docstring'''







  def __init__(      self      :					Optional[int]      ,				UpperCamelCase      :					Tuple      ,				):


    '''simple docstring'''





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







  def UpperCamelCase_ (      self      :					Optional[Any]							):


    '''simple docstring'''





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

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

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

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

    _snake_case   :				Optional[Any]														=	FlaubertConfig(
        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      :					Optional[int]      ,				UpperCamelCase      :					Optional[Any]      ,				UpperCamelCase      :					Any      ,				UpperCamelCase      :					Union[str, Any]      ,				UpperCamelCase      :					Union[str, Any]      ,				UpperCamelCase      :					Optional[int]      ,				UpperCamelCase      :					Dict      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					Union[str, Any]      ,				):


    '''simple docstring'''





    _snake_case   :				Optional[Any]														=	TFFlaubertModel(config=lowerCAmelCase_							)
    _snake_case   :				Optional[int]														=	{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
    _snake_case   :				Optional[Any]														=	model(lowerCAmelCase_							)

    _snake_case   :				List[Any]														=	[input_ids, input_mask]
    _snake_case   :				List[Any]														=	model(lowerCAmelCase_							)
    self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, self.seq_length, self.hidden_size)							)







  def UpperCamelCase_ (      self      :					Optional[int]      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					Optional[Any]      ,				UpperCamelCase      :					Dict      ,				UpperCamelCase      :					str      ,				):


    '''simple docstring'''





    _snake_case   :				str														=	TFFlaubertWithLMHeadModel(lowerCAmelCase_							)

    _snake_case   :				Optional[int]														=	{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
    _snake_case   :				Union[str, Any]														=	model(lowerCAmelCase_							)

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







  def UpperCamelCase_ (      self      :					Optional[Any]      ,				UpperCamelCase      :					Optional[int]      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					Union[str, Any]      ,				UpperCamelCase      :					Optional[Any]      ,				UpperCamelCase      :					Optional[int]      ,				UpperCamelCase      :					Union[str, Any]      ,				UpperCamelCase      :					Optional[int]      ,				UpperCamelCase      :					Any      ,				):


    '''simple docstring'''





    _snake_case   :				Optional[Any]														=	TFFlaubertForQuestionAnsweringSimple(lowerCAmelCase_							)

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

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


    '''simple docstring'''





    _snake_case   :				List[str]														=	TFFlaubertForSequenceClassification(lowerCAmelCase_							)

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

    _snake_case   :				Optional[Any]														=	model(lowerCAmelCase_							)

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







  def UpperCamelCase_ (      self      :					Any      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					Optional[Any]      ,				UpperCamelCase      :					Optional[Any]      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					Union[str, Any]      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					Tuple      ,				):


    '''simple docstring'''





    _snake_case   :				Dict														=	self.num_labels
    _snake_case   :				Tuple														=	TFFlaubertForTokenClassification(config=lowerCAmelCase_							)
    _snake_case   :				Dict														=	{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
    _snake_case   :				int														=	model(lowerCAmelCase_							)
    self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.seq_length, self.num_labels)							)







  def UpperCamelCase_ (      self      :					Optional[int]      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					Optional[Any]      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					Any      ,				UpperCamelCase      :					Union[str, Any]      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					int      ,				):


    '''simple docstring'''





    _snake_case   :				Union[str, Any]														=	self.num_choices
    _snake_case   :				Optional[int]														=	TFFlaubertForMultipleChoice(config=lowerCAmelCase_							)
    _snake_case   :				str														=	tf.tile(tf.expand_dims(lowerCAmelCase_      ,				1							)      ,				(1, self.num_choices, 1)							)
    _snake_case   :				List[str]														=	tf.tile(tf.expand_dims(lowerCAmelCase_      ,				1							)      ,				(1, self.num_choices, 1)							)
    _snake_case   :				List[Any]														=	tf.tile(tf.expand_dims(lowerCAmelCase_      ,				1							)      ,				(1, self.num_choices, 1)							)
    _snake_case   :				List[str]														=	{
        'input_ids': multiple_choice_inputs_ids,
        'attention_mask': multiple_choice_input_mask,
        'token_type_ids': multiple_choice_token_type_ids,
    }
    _snake_case   :				Dict														=	model(lowerCAmelCase_							)
    self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.num_choices)							)







  def UpperCamelCase_ (      self      :					Optional[Any]							):


    '''simple docstring'''





    _snake_case   :				Optional[int]														=	self.prepare_config_and_inputs()
    (
        (
        _snake_case    
    )							,					(
        _snake_case    
    )							,					(
        _snake_case    
    )							,					(
        _snake_case    
    )							,					(
        _snake_case    
    )							,					(
        _snake_case    
    )							,					(
        _snake_case    
    )							,					(
        _snake_case    
    )							,					(
        _snake_case    
    )							,					    
    )   :				Union[str, Any]														=	config_and_inputs
    _snake_case   :				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        _lowerCAmelCase    (			__UpperCAmelCase							,    __UpperCAmelCase							,    unittest.TestCase							):


  '''simple docstring'''
  a_							:       str      					=(
      (
          TFFlaubertModel,
          TFFlaubertWithLMHeadModel,
          TFFlaubertForSequenceClassification,
          TFFlaubertForQuestionAnsweringSimple,
          TFFlaubertForTokenClassification,
          TFFlaubertForMultipleChoice,
      )
      if is_tf_available()
      else ()
  )
  a_							:       Any      					=(
      (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
  )  # TODO (PVP): Check other models whether language generation is also applicable
  a_							:       Union[str, Any]      					=(
      {
          """feature-extraction""": TFFlaubertModel,
          """fill-mask""": TFFlaubertWithLMHeadModel,
          """question-answering""": TFFlaubertForQuestionAnsweringSimple,
          """text-classification""": TFFlaubertForSequenceClassification,
          """token-classification""": TFFlaubertForTokenClassification,
          """zero-shot""": TFFlaubertForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  a_							:       List[str]      					=False
  a_							:       List[str]      					=False







  def UpperCamelCase_ (      self      :					Optional[int]      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					Union[str, Any]      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					Any      ,				UpperCamelCase      :					Any							):


    '''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      :					Union[str, Any]							):


    '''simple docstring'''





    _snake_case   :				Dict														=	TFFlaubertModelTester(self							)
    _snake_case   :				Dict														=	ConfigTester(self      ,				config_class=lowerCAmelCase_      ,				emb_dim=37							)







  def UpperCamelCase_ (      self      :					int							):


    '''simple docstring'''





    self.config_tester.run_common_tests()







  def UpperCamelCase_ (      self      :					List[Any]							):


    '''simple docstring'''





    _snake_case   :				Any														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_flaubert_model(*lowerCAmelCase_							)







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


    '''simple docstring'''





    _snake_case   :				Dict														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_flaubert_lm_head(*lowerCAmelCase_							)







  def UpperCamelCase_ (      self      :					List[str]							):


    '''simple docstring'''





    _snake_case   :				Optional[int]														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_flaubert_qa(*lowerCAmelCase_							)







  def UpperCamelCase_ (      self      :					Any							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCAmelCase_							)







  def UpperCamelCase_ (      self      :					str							):


    '''simple docstring'''





    _snake_case   :				Union[str, Any]														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_flaubert_for_token_classification(*lowerCAmelCase_							)







  def UpperCamelCase_ (      self      :					int							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_flaubert_for_multiple_choice(*lowerCAmelCase_							)







  @slow
  def UpperCamelCase_ (      self      :					List[Any]							):


    '''simple docstring'''





    for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      _snake_case   :				List[str]														=	TFFlaubertModel.from_pretrained(lowerCAmelCase_							)
      self.assertIsNotNone(lowerCAmelCase_							)




@require_tf
@require_sentencepiece
@require_tokenizers
class        _lowerCAmelCase    (			unittest.TestCase							):


  '''simple docstring'''







  @slow
  def UpperCamelCase_ (      self      :					Union[str, Any]							):


    '''simple docstring'''





    _snake_case   :				Tuple														=	TFFlaubertModel.from_pretrained('jplu/tf-flaubert-small-cased'							)

    _snake_case   :				Dict														=	tf.convert_to_tensor(
        [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]]      ,				dtype=tf.intaa      ,				)  # "J'aime flaubert !"

    _snake_case   :				Dict														=	model(lowerCAmelCase_							)[0]
    _snake_case   :				List[Any]														=	tf.TensorShape((1, 8, 5_12)							)
    self.assertEqual(output.shape      ,				lowerCAmelCase_							)
    # compare the actual values for a slice.
    _snake_case   :				Any														=	tf.convert_to_tensor(
        [
            [
                [-1.8_76_87_73, -1.56_65_55, 0.27_07_24_18],
                [-1.6_92_00_38, -0.5_87_35_05, 1.9_32_95_99],
                [-2.9_56_39_85, -1.6_99_38_35, 1.7_97_20_52],
            ]
        ]      ,				dtype=tf.floataa      ,				)

    self.assertTrue(np.allclose(output[:, :3, :3].numpy()      ,				expected_slice.numpy()      ,				atol=1e-4							)							)











# Copyright 2021 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.

import argparse
import os

from accelerate.utils import ComputeEnvironment

from .cluster import get_cluster_input
from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file  # noqa: F401
from .config_utils import _ask_field, _ask_options, _convert_compute_environment  # noqa: F401
from .sagemaker import get_sagemaker_input


lowerCAmelCase_						   =    """Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine"""


def     lowerCamelCase_	(      )->		Tuple:
  _snake_case   :				int														=	_ask_options(
      'In which compute environment are you running?'    ,							['This machine', 'AWS (Amazon SageMaker)']    ,							_convert_compute_environment    ,							)
  if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
    _snake_case   :				int														=	get_sagemaker_input()
  else:
    _snake_case   :				Any														=	get_cluster_input()
  return config


def     lowerCamelCase_	(      lowerCAmelCase:	str=None				)->		Any:
  if subparsers is not None:
    _snake_case   :				List[Any]														=	subparsers.add_parser('config'    ,							description=lowerCAmelCase				)
  else:
    _snake_case   :				Dict														=	argparse.ArgumentParser('Accelerate config command'    ,							description=lowerCAmelCase				)

  parser.add_argument(
      '--config_file'    ,							default=lowerCAmelCase    ,							help=(
          'The path to use to store the config file. 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\'.'
      )    ,							)

  if subparsers is not None:
    parser.set_defaults(func=lowerCAmelCase				)
  return parser


def     lowerCamelCase_	(      lowerCAmelCase:	Any				)->		Any:
  _snake_case   :				Dict														=	get_user_input()
  if args.config_file is not None:
    _snake_case   :				List[str]														=	args.config_file
  else:
    if not os.path.isdir(lowerCAmelCase				):
      os.makedirs(lowerCAmelCase				)
    _snake_case   :				Union[str, Any]														=	default_yaml_config_file

  if config_file.endswith('.json'				):
    config.to_json_file(lowerCAmelCase				)
  else:
    config.to_yaml_file(lowerCAmelCase				)
  print(F"""accelerate configuration saved at {config_file}"""				)


def     lowerCamelCase_	(      )->		Dict:
  _snake_case   :				List[str]														=	config_command_parser()
  _snake_case   :				str														=	parser.parse_args()
  config_command(lowerCAmelCase				)


if __name__ == "__main__":
      main()




