


"""simple docstring"""







import ast
import os
import re
import shutil
import tempfile
import unittest
from unittest import mock

import torch

from accelerate.test_utils.examples import compare_against_test
from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow
from accelerate.utils import write_basic_config


# DataLoaders built from `test_samples/MRPC` for quick testing
# Should mock `{script_name}.get_dataloaders` via:
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)

lowerCAmelCase__				    =       [
    'cross_validation.py',
    'gradient_accumulation.py',
    'local_sgd.py',
    'multi_process_metrics.py',
    'memory.py',
    'automatic_gradient_accumulation.py',
    'fsdp_with_peak_mem_tracking.py',
    'deepspeed_with_config_support.py',
    'megatron_lm_gpt_pretraining.py',
]


class 				SCREAMING_SNAKE_CASE__    (		unittest.TestCase    ):
							"""simple docstring"""


							def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__ = None		,					snake_case__ = None						):






									"""simple docstring"""





									lowerCAmelCase					:					Optional[Any]								=							None
									lowerCAmelCase					:					int								=							os.path.abspath(os.path.join("examples"		,					"by_feature"						)						)
									lowerCAmelCase					:					Union[str, Any]								=							os.path.abspath("examples"						)
									for item in os.listdir(lowercase_						):
											if item not in EXCLUDE_EXAMPLES:
													lowerCAmelCase					:					Tuple								=							os.path.join(lowercase_		,					lowercase_						)
													if os.path.isfile(lowercase_						) and ".py" in item_path:
															with self.subTest(
															    tested_script=lowercase_		,					feature_script=lowercase_		,					tested_section="main()" if parser_only else "training_function()"		,					):
																	lowerCAmelCase					:					List[Any]								=							compare_against_test(
																	    os.path.join(lowercase_		,					lowercase_						)		,					lowercase_		,					lowercase_		,					lowercase_						)
																	lowerCAmelCase					:					Dict								=							"\n".join(lowercase_						)
																	if special_strings is not None:
																			for string in special_strings:
																					lowerCAmelCase					:					Optional[Any]								=							diff.replace(lowercase_		,					""						)
																	self.assertEqual(lowercase_		,					""						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									self.one_complete_example("complete_nlp_example.py"		,					lowercase_						)
									self.one_complete_example("complete_nlp_example.py"		,					lowercase_						)







							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					int								=							os.path.abspath(os.path.join("examples"		,					"cv_example.py"						)						)
									lowerCAmelCase					:					Optional[int]								=							[
									    " " * 16 + "{\n\n",
									    " " * 20 + "\"accuracy\": eval_metric[\"accuracy\"],\n\n",
									    " " * 20 + "\"f1\": eval_metric[\"f1\"],\n\n",
									    " " * 20 + "\"train_loss\": total_loss.item() / len(train_dataloader),\n\n",
									    " " * 20 + "\"epoch\": epoch,\n\n",
									    " " * 16 + "},\n\n",
									    " " * 16 + "step=epoch,\n",
									    " " * 12,
									    " " * 8 + "for step, batch in enumerate(active_dataloader):\n",
									]
									self.one_complete_example("complete_cv_example.py"		,					lowercase_		,					lowercase_		,					lowercase_						)
									self.one_complete_example("complete_cv_example.py"		,					lowercase_		,					lowercase_		,					lowercase_						)


@mock.patch.dict(os.environ       ,   {"TESTING_MOCKED_DATALOADERS": "1"}    )
class 				SCREAMING_SNAKE_CASE__    (		_lowerCamelCase    ):
							"""simple docstring"""


							a  :    Union[str, Any]       =False

							@classmethod
							def 	lowercase__							(						cls						):






									"""simple docstring"""





									super().setUpClass()
									lowerCAmelCase					:					Optional[int]								=							tempfile.mkdtemp()
									lowerCAmelCase					:					List[str]								=							os.path.join(cls._tmpdir		,					"default_config.yml"						)

									write_basic_config(save_location=cls.configPath						)
									lowerCAmelCase					:					Optional[int]								=							["accelerate", "launch", "--config_file", cls.configPath]

							@classmethod
							def 	lowercase__							(						cls						):






									"""simple docstring"""





									super().tearDownClass()
									shutil.rmtree(cls._tmpdir						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					Union[str, Any]								=							f"""\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps epoch\n        --output_dir {self.tmpdir}\n        """.split()
									run_command(self._launch_args + testargs						)
									self.assertTrue(os.path.exists(os.path.join(self.tmpdir		,					"epoch_0"						)						)						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					Any								=							f"""\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        """.split()
									lowerCAmelCase					:					Union[str, Any]								=							run_command(self._launch_args + testargs						)
									self.assertTrue(os.path.exists(os.path.join(self.tmpdir		,					"step_2"						)						)						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					Optional[int]								=							f"""\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir		,					"epoch_0"						)}\n        """.split()
									lowerCAmelCase					:					List[str]								=							run_command(self._launch_args + testargs		,					return_stdout=lowercase_						)
									self.assertNotIn("epoch 0:"		,					lowercase_						)
									self.assertIn("epoch 1:"		,					lowercase_						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					Optional[Any]								=							f"""\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir		,					"step_2"						)}\n        """.split()
									lowerCAmelCase					:					Optional[Any]								=							run_command(self._launch_args + testargs		,					return_stdout=lowercase_						)
									if torch.cuda.is_available():
											lowerCAmelCase					:					Dict								=							torch.cuda.device_count()
									else:
											lowerCAmelCase					:					Optional[Any]								=							1
									if num_processes > 1:
											self.assertNotIn("epoch 0:"		,					lowercase_						)
											self.assertIn("epoch 1:"		,					lowercase_						)
									else:
											self.assertIn("epoch 0:"		,					lowercase_						)
											self.assertIn("epoch 1:"		,					lowercase_						)

							@slow
							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					int								=							"\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        ".split()
									with mock.patch.dict(os.environ		,					{"TESTING_MOCKED_DATALOADERS": "0"}						):
											lowerCAmelCase					:					Dict								=							run_command(self._launch_args + testargs		,					return_stdout=lowercase_						)
											lowerCAmelCase					:					List[str]								=							re.findall("({.+})"		,					lowercase_						)
											lowerCAmelCase					:					Dict								=							[r for r in results if "accuracy" in r][-1]
											lowerCAmelCase					:					Union[str, Any]								=							ast.literal_eval(lowercase_						)
											self.assertGreaterEqual(results["accuracy"]		,					0.75						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					Dict								=							["examples/by_feature/multi_process_metrics.py"]
									run_command(self._launch_args + testargs						)

							@require_trackers
							@mock.patch.dict(os.environ		,					{"WANDB_MODE": "offline"}						)
							def 	lowercase__							(						self						):






									"""simple docstring"""





									with tempfile.TemporaryDirectory() as tmpdir:
											lowerCAmelCase					:					Optional[int]								=							f"""\n            examples/by_feature/tracking.py\n            --with_tracking\n            --project_dir {tmpdir}\n            """.split()
											run_command(self._launch_args + testargs						)
											self.assertTrue(os.path.exists(os.path.join(lowercase_		,					"tracking"						)						)						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					Tuple								=							["examples/by_feature/gradient_accumulation.py"]
									run_command(self._launch_args + testargs						)







							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					Any								=							["examples/by_feature/local_sgd.py"]
									run_command(self._launch_args + testargs						)









"""simple docstring"""







from ...configuration_utils import PretrainedConfig


lowerCAmelCase__				    =       {
    '''google/tapas-base-finetuned-sqa''': (
        '''https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json'''
    ),
    '''google/tapas-base-finetuned-wtq''': (
        '''https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json'''
    ),
    '''google/tapas-base-finetuned-wikisql-supervised''': (
        '''https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json'''
    ),
    '''google/tapas-base-finetuned-tabfact''': (
        '''https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json'''
    ),
}


class 				SCREAMING_SNAKE_CASE__    (		lowercase    ):
       """simple docstring"""


       a  :    Any       ="tapas"

       def __init__(						self		,					snake_case__=30_522		,					snake_case__=768		,					snake_case__=12		,					snake_case__=12		,					snake_case__=3_072		,					snake_case__="gelu"		,					snake_case__=0.1		,					snake_case__=0.1		,					snake_case__=1_024		,					snake_case__=[3, 256, 256, 2, 256, 256, 10]		,					snake_case__=0.02		,					snake_case__=1e-12		,					snake_case__=0		,					snake_case__=10.0		,					snake_case__=0		,					snake_case__=1.0		,					snake_case__=None		,					snake_case__=1.0		,					snake_case__=False		,					snake_case__=None		,					snake_case__=1.0		,					snake_case__=1.0		,					snake_case__=False		,					snake_case__=False		,					snake_case__="ratio"		,					snake_case__=None		,					snake_case__=None		,					snake_case__=64		,					snake_case__=32		,					snake_case__=False		,					snake_case__=True		,					snake_case__=False		,					snake_case__=False		,					snake_case__=True		,					snake_case__=False		,					snake_case__=None		,					snake_case__=None		,					**snake_case__		,					):






         """simple docstring"""





         super().__init__(pad_token_id=snake_case__		,					**snake_case__						)

         # BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes)
         lowerCAmelCase					:					List[Any]								=							vocab_size
         lowerCAmelCase					:					List[str]								=							hidden_size
         lowerCAmelCase					:					Optional[Any]								=							num_hidden_layers
         lowerCAmelCase					:					List[str]								=							num_attention_heads
         lowerCAmelCase					:					Optional[Any]								=							hidden_act
         lowerCAmelCase					:					List[Any]								=							intermediate_size
         lowerCAmelCase					:					Optional[Any]								=							hidden_dropout_prob
         lowerCAmelCase					:					str								=							attention_probs_dropout_prob
         lowerCAmelCase					:					Any								=							max_position_embeddings
         lowerCAmelCase					:					Dict								=							type_vocab_sizes
         lowerCAmelCase					:					Union[str, Any]								=							initializer_range
         lowerCAmelCase					:					str								=							layer_norm_eps

         # Fine-tuning task hyperparameters
         lowerCAmelCase					:					Dict								=							positive_label_weight
         lowerCAmelCase					:					Union[str, Any]								=							num_aggregation_labels
         lowerCAmelCase					:					Optional[Any]								=							aggregation_loss_weight
         lowerCAmelCase					:					List[Any]								=							use_answer_as_supervision
         lowerCAmelCase					:					Dict								=							answer_loss_importance
         lowerCAmelCase					:					List[Any]								=							use_normalized_answer_loss
         lowerCAmelCase					:					List[str]								=							huber_loss_delta
         lowerCAmelCase					:					Optional[int]								=							temperature
         lowerCAmelCase					:					Optional[int]								=							aggregation_temperature
         lowerCAmelCase					:					Any								=							use_gumbel_for_cells
         lowerCAmelCase					:					Union[str, Any]								=							use_gumbel_for_aggregation
         lowerCAmelCase					:					Union[str, Any]								=							average_approximation_function
         lowerCAmelCase					:					int								=							cell_selection_preference
         lowerCAmelCase					:					Dict								=							answer_loss_cutoff
         lowerCAmelCase					:					Optional[int]								=							max_num_rows
         lowerCAmelCase					:					Union[str, Any]								=							max_num_columns
         lowerCAmelCase					:					Any								=							average_logits_per_cell
         lowerCAmelCase					:					List[Any]								=							select_one_column
         lowerCAmelCase					:					Tuple								=							allow_empty_column_selection
         lowerCAmelCase					:					str								=							init_cell_selection_weights_to_zero
         lowerCAmelCase					:					List[Any]								=							reset_position_index_per_cell
         lowerCAmelCase					:					Optional[Any]								=							disable_per_token_loss

         # Aggregation hyperparameters
         lowerCAmelCase					:					List[str]								=							aggregation_labels
         lowerCAmelCase					:					List[str]								=							no_aggregation_label_index

         if isinstance(self.aggregation_labels		,					snake_case__						):
           lowerCAmelCase					:					Union[str, Any]								=							{int(snake_case__						): v for k, v in aggregation_labels.items()}





