


'''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)

_lowercase    :    Optional[int]			     =  [
    """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  UpperCamelCase__(     unittest.TestCase ):




				def   a__(	self    :				Dict   ,					lowerCAmelCase    :				str   ,					lowerCAmelCase    :				bool   ,					lowerCAmelCase    :				str = None   ,					lowerCAmelCase    :				list = None   )->  Optional[int]:

										"""simple docstring"""




										UpperCAmelCase         =			None
										UpperCAmelCase         =			os.path.abspath(os.path.join('''examples'''   ,					'''by_feature'''   )   )
										UpperCAmelCase         =			os.path.abspath('''examples'''   )
										for item in os.listdir(lowerCAmelCase   ):
																if item not in EXCLUDE_EXAMPLES:
																						UpperCAmelCase         =			os.path.join(lowerCAmelCase   ,					lowerCAmelCase   )
																						if os.path.isfile(lowerCAmelCase   ) and ".py" in item_path:
																												with self.subTest(
																												    tested_script=lowerCAmelCase   ,					feature_script=lowerCAmelCase   ,					tested_section='''main()''' if parser_only else '''training_function()'''   ,					):
																																		UpperCAmelCase         =			compare_against_test(
																																		    os.path.join(lowerCAmelCase   ,					lowerCAmelCase   )   ,					lowerCAmelCase   ,					lowerCAmelCase   ,					lowerCAmelCase   )
																																		UpperCAmelCase         =			'''\n'''.join(lowerCAmelCase   )
																																		if special_strings is not None:
																																								for string in special_strings:
																																														UpperCAmelCase         =			diff.replace(lowerCAmelCase   ,					''''''   )
																																		self.assertEqual(lowerCAmelCase   ,					''''''   )




				def   a__(	self    :				Tuple   )->  List[Any]:

										"""simple docstring"""




										self.one_complete_example('''complete_nlp_example.py'''   ,					lowerCAmelCase   )
										self.one_complete_example('''complete_nlp_example.py'''   ,					lowerCAmelCase   )





				def   a__(	self    :				Optional[int]   )->  Dict:

										"""simple docstring"""




										UpperCAmelCase         =			os.path.abspath(os.path.join('''examples'''   ,					'''cv_example.py'''   )   )
										UpperCAmelCase         =			[
										    ''' ''' * 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'''   ,					lowerCAmelCase   ,					lowerCAmelCase   ,					lowerCAmelCase   )
										self.one_complete_example('''complete_cv_example.py'''   ,					lowerCAmelCase   ,					lowerCAmelCase   ,					lowerCAmelCase   )







@mock.patch.dict(os.environ		,       {"TESTING_MOCKED_DATALOADERS": "1"} )
class  UpperCamelCase__(     lowerCAmelCase ):
				__magic_name__       :   List[Any]							= False




				@classmethod
				def   a__(	cls    :				int   )->  int:

										"""simple docstring"""




										super().setUpClass()
										UpperCAmelCase         =			tempfile.mkdtemp()
										UpperCAmelCase         =			os.path.join(cls._tmpdir   ,					'''default_config.yml'''   )

										write_basic_config(save_location=cls.configPath   )
										UpperCAmelCase         =			['''accelerate''', '''launch''', '''--config_file''', cls.configPath]




				@classmethod
				def   a__(	cls    :				Optional[Any]   )->  List[Any]:

										"""simple docstring"""




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




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

										"""simple docstring"""




										UpperCAmelCase         =			F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps epoch
        --output_dir {self.tmpdir}
        """.split()
										run_command(self._launch_args + testargs   )
										self.assertTrue(os.path.exists(os.path.join(self.tmpdir   ,					'''epoch_0'''   )   )   )




				def   a__(	self    :				int   )->  List[str]:

										"""simple docstring"""




										UpperCAmelCase         =			F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        """.split()
										UpperCAmelCase         =			run_command(self._launch_args + testargs   )
										self.assertTrue(os.path.exists(os.path.join(self.tmpdir   ,					'''step_2'''   )   )   )




				def   a__(	self    :				Any   )->  Dict:

										"""simple docstring"""




										UpperCAmelCase         =			F"""
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir   ,					"epoch_0"   )}
        """.split()
										UpperCAmelCase         =			run_command(self._launch_args + testargs   ,					return_stdout=lowerCAmelCase   )
										self.assertNotIn('''epoch 0:'''   ,					lowerCAmelCase   )
										self.assertIn('''epoch 1:'''   ,					lowerCAmelCase   )




				def   a__(	self    :				Tuple   )->  int:

										"""simple docstring"""




										UpperCAmelCase         =			F"""
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir   ,					"step_2"   )}
        """.split()
										UpperCAmelCase         =			run_command(self._launch_args + testargs   ,					return_stdout=lowerCAmelCase   )
										if torch.cuda.is_available():
																UpperCAmelCase         =			torch.cuda.device_count()
										else:
																UpperCAmelCase         =			1
										if num_processes > 1:
																self.assertNotIn('''epoch 0:'''   ,					lowerCAmelCase   )
																self.assertIn('''epoch 1:'''   ,					lowerCAmelCase   )
										else:
																self.assertIn('''epoch 0:'''   ,					lowerCAmelCase   )
																self.assertIn('''epoch 1:'''   ,					lowerCAmelCase   )




				@slow
				def   a__(	self    :				List[str]   )->  List[str]:

										"""simple docstring"""




										UpperCAmelCase         =			'''
        examples/by_feature/cross_validation.py
        --num_folds 2
        '''.split()
										with mock.patch.dict(os.environ   ,					{'''TESTING_MOCKED_DATALOADERS''': '''0'''}   ):
																UpperCAmelCase         =			run_command(self._launch_args + testargs   ,					return_stdout=lowerCAmelCase   )
																UpperCAmelCase         =			re.findall('''({.+})'''   ,					lowerCAmelCase   )
																UpperCAmelCase         =			[r for r in results if '''accuracy''' in r][-1]
																UpperCAmelCase         =			ast.literal_eval(lowerCAmelCase   )
																self.assertGreaterEqual(results['''accuracy''']   ,					0.75   )




				def   a__(	self    :				List[str]   )->  Dict:

										"""simple docstring"""




										UpperCAmelCase         =			['''examples/by_feature/multi_process_metrics.py''']
										run_command(self._launch_args + testargs   )




				@require_trackers
				@mock.patch.dict(os.environ   ,					{'''WANDB_MODE''': '''offline'''}   )
				def   a__(	self    :				Tuple   )->  Dict:

										"""simple docstring"""




										with tempfile.TemporaryDirectory() as tmpdir:
																UpperCAmelCase         =			F"""
            examples/by_feature/tracking.py
            --with_tracking
            --project_dir {tmpdir}
            """.split()
																run_command(self._launch_args + testargs   )
																self.assertTrue(os.path.exists(os.path.join(lowerCAmelCase   ,					'''tracking'''   )   )   )




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

										"""simple docstring"""




										UpperCAmelCase         =			['''examples/by_feature/gradient_accumulation.py''']
										run_command(self._launch_args + testargs   )





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

										"""simple docstring"""




										UpperCAmelCase         =			['''examples/by_feature/local_sgd.py''']
										run_command(self._launch_args + testargs   )









'''simple docstring'''



import argparse

from transformers import BigBirdConfig, BigBirdForPreTraining, BigBirdForQuestionAnswering, load_tf_weights_in_big_bird
from transformers.utils import logging


logging.set_verbosity_info()


def    lowerCamelCase__   ( A     :    List[Any]   ,     A     :    int   ,     A     :    List[str]   ,     A     :    Optional[int]  ):




						'''simple docstring'''






						UpperCAmelCase         =			BigBirdConfig.from_json_file(A  )
						print(f"""Building PyTorch model from configuration: {config}"""  )

						if is_trivia_qa:
												UpperCAmelCase         =			BigBirdForQuestionAnswering(A  )
						else:
												UpperCAmelCase         =			BigBirdForPreTraining(A  )

						# Load weights from tf checkpoint
						load_tf_weights_in_big_bird(A   ,     A   ,     is_trivia_qa=A  )

						# Save pytorch-model
						print(f"""Save PyTorch model to {pytorch_dump_path}"""  )
						model.save_pretrained(A  )


if __name__ == "__main__":
	_lowercase    :    List[str]			     =  argparse.ArgumentParser()
	# Required parameters
	parser.add_argument(
	    """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
	)
	parser.add_argument(
	    """--big_bird_config_file""",
	    default=None,
	    type=str,
	    required=True,
	    help=(
	        """The config json file corresponding to the pre-trained BERT model. \n"""
	        """This specifies the model architecture."""
	    ),
	)
	parser.add_argument(
	    """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
	)
	parser.add_argument(
	    """--is_trivia_qa""", action="""store_true""", help="""Whether to convert a model with a trivia_qa head."""
	)
	_lowercase    :    Any			     =  parser.parse_args()
	convert_tf_checkpoint_to_pytorch(
	    args.tf_checkpoint_path, args.big_bird_config_file, args.pytorch_dump_path, args.is_trivia_qa
	)





