




"""simple docstring"""



# 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


SCREAMING_SNAKE_CASE    =	"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   _SCREAMING_SNAKE_CASE       (							)						->		Optional[int]:
 A__	=    _ask_options(
     "In which compute environment are you running?"      ,			["This machine", "AWS (Amazon SageMaker)"]      ,			_convert_compute_environment      ,			)
 if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
  A__	=    get_sagemaker_input()
 else:
  A__	=    get_cluster_input()
 return config







def   _SCREAMING_SNAKE_CASE       (							lowercase_=None						)						->		Any:
 if subparsers is not None:
  A__	=    subparsers.add_parser("config"      ,			description=lowercase_						)
 else:
  A__	=    argparse.ArgumentParser("Accelerate config command"      ,			description=lowercase_						)

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







def   _SCREAMING_SNAKE_CASE       (							lowercase_						)						->		Union[str, Any]:
 A__	=    get_user_input()
 if args.config_file is not None:
  A__	=    args.config_file
 else:
  if not os.path.isdir(lowercase_						):
   os.makedirs(lowercase_						)
  A__	=    default_yaml_config_file

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







def   _SCREAMING_SNAKE_CASE       (							)						->		str:
 A__	=    config_command_parser()
 A__	=    parser.parse_args()
 config_command(lowercase_						)


if __name__ == "__main__":
      main()











"""simple docstring"""



import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, 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 (
          MODEL_FOR_PRETRAINING_MAPPING,
          AlbertForMaskedLM,
          AlbertForMultipleChoice,
          AlbertForPreTraining,
          AlbertForQuestionAnswering,
          AlbertForSequenceClassification,
          AlbertForTokenClassification,
          AlbertModel,
      )
      from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST




class      UpperCAmelCase_  :




    def __init__(   self						:	Tuple     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[Any]=13     ,			snake_case_						:	Optional[int]=7     ,			snake_case_						:	Any=True     ,			snake_case_						:	List[Any]=True     ,			snake_case_						:	Dict=True     ,			snake_case_						:	Dict=True     ,			snake_case_						:	List[str]=99     ,			snake_case_						:	Union[str, Any]=16     ,			snake_case_						:	Any=36     ,			snake_case_						:	List[Any]=6     ,			snake_case_						:	Optional[Any]=6     ,			snake_case_						:	Optional[Any]=6     ,			snake_case_						:	Any=37     ,			snake_case_						:	int="gelu"     ,			snake_case_						:	Any=0.1     ,			snake_case_						:	int=0.1     ,			snake_case_						:	str=512     ,			snake_case_						:	Union[str, Any]=16     ,			snake_case_						:	Tuple=2     ,			snake_case_						:	Any=0.02     ,			snake_case_						:	Optional[Any]=3     ,			snake_case_						:	Union[str, Any]=4     ,			snake_case_						:	Any=None     ,			)				->							Tuple:




     '''simple docstring'''

     A__	=    parent
     A__	=    batch_size
     A__	=    seq_length
     A__	=    is_training
     A__	=    use_input_mask
     A__	=    use_token_type_ids
     A__	=    use_labels
     A__	=    vocab_size
     A__	=    embedding_size
     A__	=    hidden_size
     A__	=    num_hidden_layers
     A__	=    num_hidden_groups
     A__	=    num_attention_heads
     A__	=    intermediate_size
     A__	=    hidden_act
     A__	=    hidden_dropout_prob
     A__	=    attention_probs_dropout_prob
     A__	=    max_position_embeddings
     A__	=    type_vocab_size
     A__	=    type_sequence_label_size
     A__	=    initializer_range
     A__	=    num_labels
     A__	=    num_choices
     A__	=    scope




    def 							__magic_name__							(   self						:	Tuple				)				->							List[str]:




     '''simple docstring'''

     A__	=    ids_tensor([self.batch_size, self.seq_length]     ,			self.vocab_size				)

     A__	=    None
     if self.use_input_mask:
      A__	=    random_attention_mask([self.batch_size, self.seq_length]				)

     A__	=    None
     if self.use_token_type_ids:
      A__	=    ids_tensor([self.batch_size, self.seq_length]     ,			self.type_vocab_size				)

     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]     ,			self.num_choices				)

     A__	=    self.get_config()

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




    def 							__magic_name__							(   self						:	int				)				->							Dict:




     '''simple docstring'''

     return AlbertConfig(
         vocab_size=self.vocab_size     ,			hidden_size=self.hidden_size     ,			num_hidden_layers=self.num_hidden_layers     ,			num_attention_heads=self.num_attention_heads     ,			intermediate_size=self.intermediate_size     ,			hidden_act=self.hidden_act     ,			hidden_dropout_prob=self.hidden_dropout_prob     ,			attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,			max_position_embeddings=self.max_position_embeddings     ,			type_vocab_size=self.type_vocab_size     ,			initializer_range=self.initializer_range     ,			num_hidden_groups=self.num_hidden_groups     ,			)




    def 							__magic_name__							(   self						:	Optional[int]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	List[str]     ,			snake_case_						:	List[str]     ,			snake_case_						:	Any     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Union[str, Any]				)				->							Union[str, Any]:




     '''simple docstring'''

     A__	=    AlbertModel(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_				)
     A__	=    model(snake_case_     ,			token_type_ids=snake_case_				)
     A__	=    model(snake_case_				)
     self.parent.assertEqual(result.last_hidden_state.shape     ,			(self.batch_size, self.seq_length, self.hidden_size)				)
     self.parent.assertEqual(result.pooler_output.shape     ,			(self.batch_size, self.hidden_size)				)




    def 							__magic_name__							(   self						:	int     ,			snake_case_						:	int     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Tuple     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Union[str, Any]				)				->							int:




     '''simple docstring'''

     A__	=    AlbertForPreTraining(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(
         snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_     ,			sentence_order_label=snake_case_     ,			)
     self.parent.assertEqual(result.prediction_logits.shape     ,			(self.batch_size, self.seq_length, self.vocab_size)				)
     self.parent.assertEqual(result.sop_logits.shape     ,			(self.batch_size, config.num_labels)				)




    def 							__magic_name__							(   self						:	str     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Dict     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	List[str]				)				->							Dict:




     '''simple docstring'''

     A__	=    AlbertForMaskedLM(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_				)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.seq_length, self.vocab_size)				)




    def 							__magic_name__							(   self						:	int     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Any     ,			snake_case_						:	Tuple     ,			snake_case_						:	List[str]     ,			snake_case_						:	List[str]				)				->							Tuple:




     '''simple docstring'''

     A__	=    AlbertForQuestionAnswering(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(
         snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			start_positions=snake_case_     ,			end_positions=snake_case_     ,			)
     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						:	Any     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Tuple     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	str     ,			snake_case_						:	Any				)				->							Any:




     '''simple docstring'''

     A__	=    self.num_labels
     A__	=    AlbertForSequenceClassification(snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_				)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_labels)				)




    def 							__magic_name__							(   self						:	Tuple     ,			snake_case_						:	Dict     ,			snake_case_						:	int     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	int     ,			snake_case_						:	str     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[Any]				)				->							List[str]:




     '''simple docstring'''

     A__	=    self.num_labels
     A__	=    AlbertForTokenClassification(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_				)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.seq_length, self.num_labels)				)




    def 							__magic_name__							(   self						:	Any     ,			snake_case_						:	Tuple     ,			snake_case_						:	Any     ,			snake_case_						:	Any     ,			snake_case_						:	Tuple     ,			snake_case_						:	Any     ,			snake_case_						:	Tuple     ,			snake_case_						:	Any				)				->							Optional[Any]:




     '''simple docstring'''

     A__	=    self.num_choices
     A__	=    AlbertForMultipleChoice(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    input_ids.unsqueeze(1				).expand(-1     ,			self.num_choices     ,			-1				).contiguous()
     A__	=    token_type_ids.unsqueeze(1				).expand(-1     ,			self.num_choices     ,			-1				).contiguous()
     A__	=    input_mask.unsqueeze(1				).expand(-1     ,			self.num_choices     ,			-1				).contiguous()
     A__	=    model(
         snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_     ,			)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_choices)				)




    def 							__magic_name__							(   self						:	Any				)				->							List[str]:




     '''simple docstring'''

     A__	=    self.prepare_config_and_inputs()
     (
         (
         A__
     ),				(
         A__
     ),				(
         A__
     ),				(
         A__
     ),				(
         A__
     ),				(
         A__
     ),				(
         A__
     ),				
     )	=    config_and_inputs
     A__	=    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict






@require_torch
class      UpperCAmelCase_  (						A_,       A_,       unittest.TestCase							):
    lowercase__						 =				(
        (
            AlbertModel,
            AlbertForPreTraining,
            AlbertForMaskedLM,
            AlbertForMultipleChoice,
            AlbertForSequenceClassification,
            AlbertForTokenClassification,
            AlbertForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    lowercase__						 =				(
        {
            '''feature-extraction''': AlbertModel,
            '''fill-mask''': AlbertForMaskedLM,
            '''question-answering''': AlbertForQuestionAnswering,
            '''text-classification''': AlbertForSequenceClassification,
            '''token-classification''': AlbertForTokenClassification,
            '''zero-shot''': AlbertForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    lowercase__						 =				True




    def 							__magic_name__							(   self						:	Tuple     ,			snake_case_						:	Dict     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	List[Any]=False				)				->							Optional[int]:




     '''simple docstring'''

     A__	=    super()._prepare_for_class(snake_case_     ,			snake_case_     ,			return_labels=snake_case_				)

     if return_labels:
      if model_class in get_values(snake_case_				):
       A__	=    torch.zeros(
           (self.model_tester.batch_size, self.model_tester.seq_length)     ,			dtype=torch.long     ,			device=snake_case_				)
       A__	=    torch.zeros(
           self.model_tester.batch_size     ,			dtype=torch.long     ,			device=snake_case_				)
     return inputs_dict




    def 							__magic_name__							(   self						:	Dict				)				->							List[Any]:




     '''simple docstring'''

     A__	=    AlbertModelTester(self				)
     A__	=    ConfigTester(self     ,			config_class=snake_case_     ,			hidden_size=37				)




    def 							__magic_name__							(   self						:	Union[str, Any]				)				->							Optional[int]:




     '''simple docstring'''

     self.config_tester.run_common_tests()




    def 							__magic_name__							(   self						:	Any				)				->							List[str]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*snake_case_				)




    def 							__magic_name__							(   self						:	Optional[int]				)				->							Union[str, Any]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*snake_case_				)




    def 							__magic_name__							(   self						:	int				)				->							str:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*snake_case_				)




    def 							__magic_name__							(   self						:	Any				)				->							str:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*snake_case_				)




    def 							__magic_name__							(   self						:	Optional[Any]				)				->							Any:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*snake_case_				)




    def 							__magic_name__							(   self						:	Optional[int]				)				->							Optional[Any]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*snake_case_				)




    def 							__magic_name__							(   self						:	List[str]				)				->							Optional[Any]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
      A__	=    type
      self.model_tester.create_and_check_model(*snake_case_				)




    @slow
    def 							__magic_name__							(   self						:	Union[str, Any]				)				->							Dict:




     '''simple docstring'''

     for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      A__	=    AlbertModel.from_pretrained(snake_case_				)
      self.assertIsNotNone(snake_case_				)






@require_torch
class      UpperCAmelCase_  (						unittest.TestCase							):




    @slow
    def 							__magic_name__							(   self						:	int				)				->							Tuple:




     '''simple docstring'''

     A__	=    AlbertModel.from_pretrained("albert-base-v2"				)
     A__	=    torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]				)
     A__	=    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
     with torch.no_grad():
      A__	=    model(snake_case_     ,			attention_mask=snake_case_				)[0]
     A__	=    torch.Size((1, 11, 768)				)
     self.assertEqual(output.shape     ,			snake_case_				)
     A__	=    torch.tensor(
         [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]				)

     self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,			snake_case_     ,			atol=1e-4				)				)





