




import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


a      =							logging.getLogger(__name__)






@dataclass(frozen=_a					)
class       SCREAMING_SNAKE_CASE__					:
						_a    						=						42
						_a    						=						42
						_a    						=						None
						_a    						=						None
						_a    						=						None






@dataclass(frozen=_a					)
class       SCREAMING_SNAKE_CASE__					:
						_a    						=						42
						_a    						=						None
						_a    						=						None
						_a    						=						None
						_a    						=						None


if is_torch_available():
		import torch
		from torch.utils.data import Dataset






		class       SCREAMING_SNAKE_CASE__					(					_a					):
								_a    						=						42


								def __init__(							self				:			Optional[int]						,						lowerCAmelCase				:			str						,						lowerCAmelCase				:			PreTrainedTokenizer						,						lowerCAmelCase				:			str						,						lowerCAmelCase				:			Optional[int] = None						,						lowerCAmelCase				:			str=False						,						lowerCAmelCase				:			bool = False						,						):
												lowerCAmelCase  =				hans_processors[task]()

												lowerCAmelCase  =				os.path.join(
												    __A						,						"""cached_{}_{}_{}_{}""".format(
												        """dev""" if evaluate else """train"""						,						tokenizer.__class__.__name__						,						str(__A					)						,						__A						,						)						,						)
												lowerCAmelCase  =				processor.get_labels()
												if tokenizer.__class__ in (
												    RobertaTokenizer,
												    RobertaTokenizerFast,
												    XLMRobertaTokenizer,
												    BartTokenizer,
												    BartTokenizerFast,
												):
																# HACK(label indices are swapped in RoBERTa pretrained model)
																lowerCAmelCase ,						lowerCAmelCase  =				label_list[2], label_list[1]
												lowerCAmelCase  =				label_list

												# Make sure only the first process in distributed training processes the dataset,
												# and the others will use the cache.
												lowerCAmelCase  =				cached_features_file + """.lock"""
												with FileLock(__A					):
																if os.path.exists(__A					) and not overwrite_cache:
																				logger.info(f'''Loading features from cached file {cached_features_file}'''					)
																				lowerCAmelCase  =				torch.load(__A					)
																else:
																				logger.info(f'''Creating features from dataset file at {data_dir}'''					)

																				lowerCAmelCase  =				(
																				    processor.get_dev_examples(__A					) if evaluate else processor.get_train_examples(__A					)
																				)

																				logger.info("""Training examples: %s"""						,						len(__A					)					)
																				lowerCAmelCase  =				hans_convert_examples_to_features(__A						,						__A						,						__A						,						__A					)
																				logger.info("""Saving features into cached file %s"""						,						__A					)
																				torch.save(self.features						,						__A					)


								def __len__(							self				:			Dict					):
												return len(self.features					)


								def __getitem__(							self				:			Any						,						lowerCAmelCase				:			List[Any]					):
												return self.features[i]




								def   __lowercase (							self				:			Tuple					):
												return self.label_list


if is_tf_available():
		import tensorflow as tf






		class       SCREAMING_SNAKE_CASE__					:
								_a    						=						42


								def __init__(							self				:			Tuple						,						lowerCAmelCase				:			str						,						lowerCAmelCase				:			PreTrainedTokenizer						,						lowerCAmelCase				:			str						,						lowerCAmelCase				:			Optional[int] = 128						,						lowerCAmelCase				:			Dict=False						,						lowerCAmelCase				:			bool = False						,						):
												lowerCAmelCase  =				hans_processors[task]()
												lowerCAmelCase  =				processor.get_labels()
												if tokenizer.__class__ in (
												    RobertaTokenizer,
												    RobertaTokenizerFast,
												    XLMRobertaTokenizer,
												    BartTokenizer,
												    BartTokenizerFast,
												):
																# HACK(label indices are swapped in RoBERTa pretrained model)
																lowerCAmelCase ,						lowerCAmelCase  =				label_list[2], label_list[1]
												lowerCAmelCase  =				label_list

												lowerCAmelCase  =				processor.get_dev_examples(__A					) if evaluate else processor.get_train_examples(__A					)
												lowerCAmelCase  =				hans_convert_examples_to_features(__A						,						__A						,						__A						,						__A					)

												def gen():
																for ex_index, ex in tqdm.tqdm(enumerate(self.features					)						,						desc="""convert examples to features"""					):
																				if ex_index % 1_0000 == 0:
																								logger.info("""Writing example %d of %d""" % (ex_index, len(__A					))					)

																				yield (
																				    {
																				        "example_id": 0,
																				        "input_ids": ex.input_ids,
																				        "attention_mask": ex.attention_mask,
																				        "token_type_ids": ex.token_type_ids,
																				    },
																				    ex.label,
																				)

												lowerCAmelCase  =				tf.data.Dataset.from_generator(
												    __A						,						(
												        {
												            """example_id""": tf.intaa,
												            """input_ids""": tf.intaa,
												            """attention_mask""": tf.intaa,
												            """token_type_ids""": tf.intaa,
												        },
												        tf.intaa,
												    )						,						(
												        {
												            """example_id""": tf.TensorShape([]					),
												            """input_ids""": tf.TensorShape([None, None]					),
												            """attention_mask""": tf.TensorShape([None, None]					),
												            """token_type_ids""": tf.TensorShape([None, None]					),
												        },
												        tf.TensorShape([]					),
												    )						,						)


								def   __lowercase (							self				:			int					):
												return self.dataset


								def __len__(							self				:			List[str]					):
												return len(self.features					)


								def __getitem__(							self				:			int						,						lowerCAmelCase				:			Optional[Any]					):
												return self.features[i]




								def   __lowercase (							self				:			List[str]					):
												return self.label_list






class       SCREAMING_SNAKE_CASE__					(					_a					):


						def   __lowercase (							self				:			Tuple						,						lowerCAmelCase				:			List[str]					):
										return self._create_examples(self._read_tsv(os.path.join(__A						,						"""heuristics_train_set.txt"""					)					)						,						"""train"""					)


						def   __lowercase (							self				:			Any						,						lowerCAmelCase				:			str					):
										return self._create_examples(self._read_tsv(os.path.join(__A						,						"""heuristics_evaluation_set.txt"""					)					)						,						"""dev"""					)


						def   __lowercase (							self				:			Tuple					):
										return ["contradiction", "entailment", "neutral"]




						def   __lowercase (							self				:			List[str]						,						lowerCAmelCase				:			str						,						lowerCAmelCase				:			Any					):
										lowerCAmelCase  =				[]
										for i, line in enumerate(__A					):
														if i == 0:
																		continue
														lowerCAmelCase  =				"""%s-%s""" % (set_type, line[0])
														lowerCAmelCase  =				line[5]
														lowerCAmelCase  =				line[6]
														lowerCAmelCase  =				line[7][2:] if line[7].startswith("""ex"""					) else line[7]
														lowerCAmelCase  =				line[0]
														examples.append(InputExample(guid=__A						,						text_a=__A						,						text_b=__A						,						label=__A						,						pairID=__A					)					)
										return examples





def 							lowercase					(snake_case__   : Optional[int]      ,       snake_case__   : int      ,       snake_case__   : int      ,       snake_case__   : Optional[Any]      ,       )     ->  Optional[Any]:


				'''simple docstring'''






				lowerCAmelCase  =				{label: i for i, label in enumerate(_lowercase )}

				lowerCAmelCase  =				[]
				for ex_index, example in tqdm.tqdm(enumerate(_lowercase )      ,       desc="""convert examples to features""" ):
								if ex_index % 10_000 == 0:
												logger.info("""Writing example %d""" % (ex_index) )

								lowerCAmelCase  =				tokenizer(
								    example.text_a      ,       example.text_b      ,       add_special_tokens=_lowercase      ,       max_length=_lowercase      ,       padding="""max_length"""      ,       truncation=_lowercase      ,       return_overflowing_tokens=_lowercase      ,       )

								lowerCAmelCase  =				label_map[example.label] if example.label in label_map else 0

								lowerCAmelCase  =				int(example.pairID )

								features.append(InputFeatures(**_lowercase      ,       label=_lowercase      ,       pairID=_lowercase ) )

				for i, example in enumerate(examples[:5] ):
								logger.info("""*** Example ***""" )
								logger.info(f'''guid: {example}''' )
								logger.info(f'''features: {features[i]}''' )

				return features


a      =							{
    'hans': 3,
}






a      =							{
    'hans': HansProcessor,
}











"""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


a      =							'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 							lowercase					()     ->  Optional[Any]:


				'''simple docstring'''






				lowerCAmelCase  =				_ask_options(
				    """In which compute environment are you running?"""      ,       ["""This machine""", """AWS (Amazon SageMaker)"""]      ,       _convert_compute_environment      ,       )
				if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
								lowerCAmelCase  =				get_sagemaker_input()
				else:
								lowerCAmelCase  =				get_cluster_input()
				return config





def 							lowercase					(snake_case__   : List[str]=None )     ->  int:


				'''simple docstring'''






				if subparsers is not None:
								lowerCAmelCase  =				subparsers.add_parser("""config"""      ,       description=snake_case__ )
				else:
								lowerCAmelCase  =				argparse.ArgumentParser("""Accelerate config command"""      ,       description=snake_case__ )

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





def 							lowercase					(snake_case__   : str )     ->  Optional[Any]:


				'''simple docstring'''






				lowerCAmelCase  =				get_user_input()
				if args.config_file is not None:
								lowerCAmelCase  =				args.config_file
				else:
								if not os.path.isdir(snake_case__ ):
												os.makedirs(snake_case__ )
								lowerCAmelCase  =				default_yaml_config_file

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





def 							lowercase					()     ->  Optional[int]:


				'''simple docstring'''






				lowerCAmelCase  =				config_command_parser()
				lowerCAmelCase  =				parser.parse_args()
				config_command(snake_case__ )


if __name__ == "__main__":
		main()





