

import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         FlaubertForMultipleChoice,
         FlaubertForQuestionAnswering,
         FlaubertForQuestionAnsweringSimple,
         FlaubertForSequenceClassification,
         FlaubertForTokenClassification,
         FlaubertModel,
         FlaubertWithLMHeadModel,
     )
     from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST





class  __UpperCAmelCase					(	lowerCamelCase__						):



     def __init__(							self							:					Tuple,		__A							:					Optional[int],		__A							:					List[Any]=1_3,		__A							:					Dict=7,		__A							:					List[Any]=True,		__A							:					List[Any]=True,		__A							:					Dict=True,		__A							:					str=True,		__A							:					Optional[int]=True,		__A							:					str=False,		__A							:					str=False,		__A							:					Optional[Any]=False,		__A							:					List[Any]=2,		__A							:					str=9_9,		__A							:					Union[str, Any]=0,		__A							:					Tuple=3_2,		__A							:					Any=5,		__A							:					Optional[int]=4,		__A							:					Union[str, Any]=0.1,		__A							:					Any=0.1,		__A							:					Union[str, Any]=5_1_2,		__A							:					List[Any]=1_2,		__A							:					Tuple=2,		__A							:					str=0.0_2,		__A							:					Dict=3,		__A							:					Tuple=4,		__A							:					Dict="last",		__A							:					Optional[Any]=None,		__A							:					Optional[Any]=None,		):
       UpperCAmelCase	:							str						=			parent
       UpperCAmelCase	:							Optional[Any]						=			batch_size
       UpperCAmelCase	:							Union[str, Any]						=			seq_length
       UpperCAmelCase	:							Optional[Any]						=			is_training
       UpperCAmelCase	:							str						=			use_input_lengths
       UpperCAmelCase	:							Union[str, Any]						=			use_token_type_ids
       UpperCAmelCase	:							Any						=			use_labels
       UpperCAmelCase	:							str						=			gelu_activation
       UpperCAmelCase	:							int						=			sinusoidal_embeddings
       UpperCAmelCase	:							List[str]						=			causal
       UpperCAmelCase	:							List[str]						=			asm
       UpperCAmelCase	:							Union[str, Any]						=			n_langs
       UpperCAmelCase	:							List[Any]						=			vocab_size
       UpperCAmelCase	:							List[str]						=			n_special
       UpperCAmelCase	:							str						=			hidden_size
       UpperCAmelCase	:							int						=			num_hidden_layers
       UpperCAmelCase	:							Optional[int]						=			num_attention_heads
       UpperCAmelCase	:							str						=			hidden_dropout_prob
       UpperCAmelCase	:							List[str]						=			attention_probs_dropout_prob
       UpperCAmelCase	:							List[Any]						=			max_position_embeddings
       UpperCAmelCase	:							int						=			type_vocab_size
       UpperCAmelCase	:							List[Any]						=			type_sequence_label_size
       UpperCAmelCase	:							int						=			initializer_range
       UpperCAmelCase	:							Union[str, Any]						=			num_labels
       UpperCAmelCase	:							Union[str, Any]						=			num_choices
       UpperCAmelCase	:							Union[str, Any]						=			summary_type
       UpperCAmelCase	:							Tuple						=			use_proj
       UpperCAmelCase	:							Any						=			scope



     def 	__magic_name__   (							self							:					List[str]						):
       UpperCAmelCase	:							Optional[int]						=			ids_tensor([self.batch_size, self.seq_length],		self.vocab_size						)
       UpperCAmelCase	:							List[str]						=			random_attention_mask([self.batch_size, self.seq_length]						)

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

       UpperCAmelCase	:							Union[str, Any]						=			None
       if self.use_token_type_ids:
         UpperCAmelCase	:							int						=			ids_tensor([self.batch_size, self.seq_length],		self.n_langs						)

       UpperCAmelCase	:							Tuple						=			None
       UpperCAmelCase	:							str						=			None
       UpperCAmelCase	:							Tuple						=			None
       if self.use_labels:
         UpperCAmelCase	:							Optional[int]						=			ids_tensor([self.batch_size],		self.type_sequence_label_size						)
         UpperCAmelCase	:							Tuple						=			ids_tensor([self.batch_size, self.seq_length],		self.num_labels						)
         UpperCAmelCase	:							str						=			ids_tensor([self.batch_size],		2						).float()
         UpperCAmelCase	:							List[Any]						=			ids_tensor([self.batch_size],		self.num_choices						)

       UpperCAmelCase	:							Dict						=			self.get_config()

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



     def 	__magic_name__   (							self							:					Union[str, Any]						):
       return 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,		)



     def 	__magic_name__   (							self							:					List[str],		__A							:					Optional[int],		__A							:					Optional[int],		__A							:					Optional[Any],		__A							:					Optional[int],		__A							:					str,		__A							:					Tuple,		__A							:					Optional[Any],		__A							:					Tuple,		__A							:					Optional[int],		):
       UpperCAmelCase	:							str						=			FlaubertModel(config=__A						)
       model.to(__A						)
       model.eval()
       UpperCAmelCase	:							int						=			model(__A,		lengths=__A,		langs=__A						)
       UpperCAmelCase	:							int						=			model(__A,		langs=__A						)
       UpperCAmelCase	:							List[Any]						=			model(__A						)
       self.parent.assertEqual(result.last_hidden_state.shape,		(self.batch_size, self.seq_length, self.hidden_size)						)



     def 	__magic_name__   (							self							:					str,		__A							:					Optional[int],		__A							:					Optional[Any],		__A							:					Optional[int],		__A							:					Dict,		__A							:					int,		__A							:					List[Any],		__A							:					List[Any],		__A							:					List[Any],		__A							:					Optional[int],		):
       UpperCAmelCase	:							Tuple						=			FlaubertWithLMHeadModel(__A						)
       model.to(__A						)
       model.eval()

       UpperCAmelCase	:							Optional[Any]						=			model(__A,		token_type_ids=__A,		labels=__A						)
       self.parent.assertEqual(result.loss.shape,		()						)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.vocab_size)						)



     def 	__magic_name__   (							self							:					int,		__A							:					Optional[Any],		__A							:					Optional[int],		__A							:					str,		__A							:					Tuple,		__A							:					str,		__A							:					Tuple,		__A							:					List[Any],		__A							:					List[Any],		__A							:					Optional[Any],		):
       UpperCAmelCase	:							int						=			FlaubertForQuestionAnsweringSimple(__A						)
       model.to(__A						)
       model.eval()

       UpperCAmelCase	:							Optional[int]						=			model(__A						)

       UpperCAmelCase	:							Tuple						=			model(__A,		start_positions=__A,		end_positions=__A						)
       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 	__magic_name__   (							self							:					Optional[int],		__A							:					int,		__A							:					Dict,		__A							:					Union[str, Any],		__A							:					Dict,		__A							:					int,		__A							:					int,		__A							:					Dict,		__A							:					Optional[int],		__A							:					List[str],		):
       UpperCAmelCase	:							Tuple						=			FlaubertForQuestionAnswering(__A						)
       model.to(__A						)
       model.eval()

       UpperCAmelCase	:							Dict						=			model(__A						)

       UpperCAmelCase	:							int						=			model(
           __A,		start_positions=__A,		end_positions=__A,		cls_index=__A,		is_impossible=__A,		p_mask=__A,		)

       UpperCAmelCase	:							Optional[int]						=			model(
           __A,		start_positions=__A,		end_positions=__A,		cls_index=__A,		is_impossible=__A,		)

       (UpperCAmelCase						)	:							Optional[int]						=			result_with_labels.to_tuple()

       UpperCAmelCase	:							str						=			model(__A,		start_positions=__A,		end_positions=__A						)

       (UpperCAmelCase						)	:							List[Any]						=			result_with_labels.to_tuple()

       self.parent.assertEqual(result_with_labels.loss.shape,		()						)
       self.parent.assertEqual(result.start_top_log_probs.shape,		(self.batch_size, model.config.start_n_top)						)
       self.parent.assertEqual(result.start_top_index.shape,		(self.batch_size, model.config.start_n_top)						)
       self.parent.assertEqual(
           result.end_top_log_probs.shape,		(self.batch_size, model.config.start_n_top * model.config.end_n_top)						)
       self.parent.assertEqual(
           result.end_top_index.shape,		(self.batch_size, model.config.start_n_top * model.config.end_n_top)						)
       self.parent.assertEqual(result.cls_logits.shape,		(self.batch_size,)						)



     def 	__magic_name__   (							self							:					int,		__A							:					Optional[Any],		__A							:					Tuple,		__A							:					Union[str, Any],		__A							:					Any,		__A							:					List[Any],		__A							:					List[str],		__A							:					int,		__A							:					str,		__A							:					int,		):
       UpperCAmelCase	:							Union[str, Any]						=			FlaubertForSequenceClassification(__A						)
       model.to(__A						)
       model.eval()

       UpperCAmelCase	:							Tuple						=			model(__A						)
       UpperCAmelCase	:							int						=			model(__A,		labels=__A						)

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



     def 	__magic_name__   (							self							:					Tuple,		__A							:					Dict,		__A							:					Dict,		__A							:					Optional[int],		__A							:					Any,		__A							:					int,		__A							:					Any,		__A							:					Dict,		__A							:					int,		__A							:					Union[str, Any],		):
       UpperCAmelCase	:							Union[str, Any]						=			self.num_labels
       UpperCAmelCase	:							Dict						=			FlaubertForTokenClassification(__A						)
       model.to(__A						)
       model.eval()

       UpperCAmelCase	:							Optional[int]						=			model(__A,		attention_mask=__A,		labels=__A						)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.num_labels)						)



     def 	__magic_name__   (							self							:					Optional[Any],		__A							:					Union[str, Any],		__A							:					Dict,		__A							:					List[str],		__A							:					Tuple,		__A							:					List[str],		__A							:					Optional[Any],		__A							:					Any,		__A							:					Union[str, Any],		__A							:					Union[str, Any],		):
       UpperCAmelCase	:							Any						=			self.num_choices
       UpperCAmelCase	:							Dict						=			FlaubertForMultipleChoice(config=__A						)
       model.to(__A						)
       model.eval()
       UpperCAmelCase	:							int						=			input_ids.unsqueeze(1						).expand(-1,		self.num_choices,		-1						).contiguous()
       UpperCAmelCase	:							Tuple						=			token_type_ids.unsqueeze(1						).expand(-1,		self.num_choices,		-1						).contiguous()
       UpperCAmelCase	:							Optional[int]						=			input_mask.unsqueeze(1						).expand(-1,		self.num_choices,		-1						).contiguous()
       UpperCAmelCase	:							int						=			model(
           __A,		attention_mask=__A,		token_type_ids=__A,		labels=__A,		)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_choices)						)





     def 	__magic_name__   (							self							:					Any						):
       UpperCAmelCase	:							Optional[Any]						=			self.prepare_config_and_inputs()
       (
           UpperCAmelCase						
       )	:							List[Any]						=			config_and_inputs
       UpperCAmelCase	:							Optional[Any]						=			{
           '''input_ids''': input_ids,
           '''token_type_ids''': token_type_ids,
           '''lengths''': input_lengths,
           '''attention_mask''': input_mask,
       }
       return config, inputs_dict






@require_torch
class  __UpperCAmelCase					(	lowerCamelCase__      ,  lowerCamelCase__      ,  unittest.TestCase						):
     UpperCamelCase       					=					(
         (
             FlaubertModel,
             FlaubertWithLMHeadModel,
             FlaubertForQuestionAnswering,
             FlaubertForQuestionAnsweringSimple,
             FlaubertForSequenceClassification,
             FlaubertForTokenClassification,
             FlaubertForMultipleChoice,
         )
         if is_torch_available()
         else ()
     )
     UpperCamelCase       					=					(
         {
             """feature-extraction""": FlaubertModel,
             """fill-mask""": FlaubertWithLMHeadModel,
             """question-answering""": FlaubertForQuestionAnsweringSimple,
             """text-classification""": FlaubertForSequenceClassification,
             """token-classification""": FlaubertForTokenClassification,
             """zero-shot""": FlaubertForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )



     def 	__magic_name__   (							self							:					Dict,		__A							:					Union[str, Any],		__A							:					Tuple,		__A							:					Union[str, Any],		__A							:					Optional[Any],		__A							:					List[Any]						):
       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 	__magic_name__   (							self							:					Union[str, Any],		__A							:					Any,		__A							:					Any,		__A							:					List[Any]=False						):
       UpperCAmelCase	:							Optional[int]						=			super()._prepare_for_class(__A,		__A,		return_labels=__A						)

       if return_labels:
         if model_class.__name__ == "FlaubertForQuestionAnswering":
           UpperCAmelCase	:							int						=			torch.zeros(
               self.model_tester.batch_size,		dtype=torch.long,		device=__A						)
           UpperCAmelCase	:							Union[str, Any]						=			torch.zeros(
               self.model_tester.batch_size,		dtype=torch.long,		device=__A						)

       return inputs_dict



     def 	__magic_name__   (							self							:					str						):
       UpperCAmelCase	:							str						=			FlaubertModelTester(self						)
       UpperCAmelCase	:							Optional[Any]						=			ConfigTester(self,		config_class=__A,		emb_dim=3_7						)



     def 	__magic_name__   (							self							:					int						):
       self.config_tester.run_common_tests()



     def 	__magic_name__   (							self							:					List[str]						):
       UpperCAmelCase	:							Optional[Any]						=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_model(*__A						)



     def 	__magic_name__   (							self							:					str						):
       UpperCAmelCase	:							int						=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_lm_head(*__A						)



     def 	__magic_name__   (							self							:					Dict						):
       UpperCAmelCase	:							Any						=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_simple_qa(*__A						)



     def 	__magic_name__   (							self							:					Tuple						):
       UpperCAmelCase	:							Union[str, Any]						=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_qa(*__A						)



     def 	__magic_name__   (							self							:					str						):
       UpperCAmelCase	:							Dict						=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_sequence_classif(*__A						)



     def 	__magic_name__   (							self							:					str						):
       UpperCAmelCase	:							Union[str, Any]						=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_token_classif(*__A						)



     def 	__magic_name__   (							self							:					Optional[Any]						):
       UpperCAmelCase	:							Any						=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_multiple_choice(*__A						)



     @slow
     def 	__magic_name__   (							self							:					str						):
       for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         UpperCAmelCase	:							Union[str, Any]						=			FlaubertModel.from_pretrained(__A						)
         self.assertIsNotNone(__A						)





     @slow
     @require_torch_gpu
     def 	__magic_name__   (							self							:					List[str]						):
       UpperCAmelCase	:							Union[str, Any]						=			self.model_tester.prepare_config_and_inputs_for_common()
       for model_class in self.all_model_classes:
         # FlauBertForMultipleChoice behaves incorrectly in JIT environments.
         if model_class == FlaubertForMultipleChoice:
           return

         UpperCAmelCase	:							Optional[Any]						=			True
         UpperCAmelCase	:							Optional[Any]						=			model_class(config=__A						)

         UpperCAmelCase	:							Optional[int]						=			self._prepare_for_class(__A,		__A						)
         UpperCAmelCase	:							Any						=			torch.jit.trace(
             __A,		(inputs_dict['''input_ids'''].to('''cpu'''						), inputs_dict['''attention_mask'''].to('''cpu'''						))						)

         with tempfile.TemporaryDirectory() as tmp:
           torch.jit.save(__A,		os.path.join(__A,		'''traced_model.pt'''						)						)
           UpperCAmelCase	:							Optional[int]						=			torch.jit.load(os.path.join(__A,		'''traced_model.pt'''						),		map_location=__A						)
           loaded(inputs_dict['''input_ids'''].to(__A						),		inputs_dict['''attention_mask'''].to(__A						)						)






@require_torch
class  __UpperCAmelCase					(	unittest.TestCase						):



     @slow
     def 	__magic_name__   (							self							:					List[str]						):
       UpperCAmelCase	:							str						=			FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''						)
       UpperCAmelCase	:							Optional[int]						=			torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]						)
       with torch.no_grad():
         UpperCAmelCase	:							Any						=			model(__A						)[0]
       UpperCAmelCase	:							int						=			torch.Size((1, 1_1, 7_6_8)						)
       self.assertEqual(output.shape,		__A						)
       UpperCAmelCase	:							List[str]						=			torch.tensor(
           [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]]						)

       self.assertTrue(torch.allclose(output[:, :3, :3],		__A,		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    :       Optional[Any]        =							"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       a__					(		)  ->						List[Any]:
  UpperCAmelCase	:							Dict						=			_ask_options(
      '''In which compute environment are you running?'''    ,			['''This machine''', '''AWS (Amazon SageMaker)''']    ,			_convert_compute_environment    ,			)
  if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
    UpperCAmelCase	:							List[Any]						=			get_sagemaker_input()
  else:
    UpperCAmelCase	:							Optional[Any]						=			get_cluster_input()
  return config

def       a__					(		UpperCAmelCase							:      Union[str, Any]=None      )  ->						List[Any]:
  if subparsers is not None:
    UpperCAmelCase	:							Optional[Any]						=			subparsers.add_parser('''config'''    ,			description=UpperCAmelCase      )
  else:
    UpperCAmelCase	:							List[str]						=			argparse.ArgumentParser('''Accelerate config command'''    ,			description=UpperCAmelCase      )

  parser.add_argument(
      '''--config_file'''    ,			default=UpperCAmelCase    ,			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=UpperCAmelCase      )
  return parser

def       a__					(		UpperCAmelCase							:      List[Any]      )  ->						Optional[int]:
  UpperCAmelCase	:							str						=			get_user_input()
  if args.config_file is not None:
    UpperCAmelCase	:							Any						=			args.config_file
  else:
    if not os.path.isdir(UpperCAmelCase      ):
      os.makedirs(UpperCAmelCase      )
    UpperCAmelCase	:							List[str]						=			default_yaml_config_file

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

def       a__					(		)  ->						Dict:
  UpperCAmelCase	:							str						=			config_command_parser()
  UpperCAmelCase	:							str						=			parser.parse_args()
  config_command(UpperCAmelCase      )


if __name__ == "__main__":
     main()





