'''simple docstring'''


from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers.models.xglm.modeling_tf_xglm import (
					    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TFXGLMForCausalLM,
					    TFXGLMModel,
					)






@require_tf
class 						A   :
							a_        =      XGLMConfig
							a_        =      {}
							a_        =      '''gelu'''





							def __init__(      self				:							List[str]     ,		__a				:							int     ,		__a				:							Optional[Any]=1_4     ,		__a				:							Optional[int]=7     ,		__a				:							Dict=True     ,		__a				:							Optional[int]=True     ,		__a				:							Tuple=True     ,		__a				:							int=9_9     ,		__a				:							Optional[int]=3_2     ,		__a				:							str=2     ,		__a				:							Tuple=4     ,		__a				:							Any=3_7     ,		__a				:							int="gelu"     ,		__a				:							Tuple=0.1     ,		__a				:							Optional[int]=0.1     ,		__a				:							str=5_1_2     ,		__a				:							List[str]=0.0_2     ,		)							->	Optional[Any]:
								__UpperCAmelCase							=			parent
								__UpperCAmelCase							=			batch_size
								__UpperCAmelCase							=			seq_length
								__UpperCAmelCase							=			is_training
								__UpperCAmelCase							=			use_input_mask
								__UpperCAmelCase							=			use_labels
								__UpperCAmelCase							=			vocab_size
								__UpperCAmelCase							=			d_model
								__UpperCAmelCase							=			num_hidden_layers
								__UpperCAmelCase							=			num_attention_heads
								__UpperCAmelCase							=			ffn_dim
								__UpperCAmelCase							=			activation_function
								__UpperCAmelCase							=			activation_dropout
								__UpperCAmelCase							=			attention_dropout
								__UpperCAmelCase							=			max_position_embeddings
								__UpperCAmelCase							=			initializer_range
								__UpperCAmelCase							=			None
								__UpperCAmelCase							=			0
								__UpperCAmelCase							=			2
								__UpperCAmelCase							=			1





							def       snake_case__						(      self				:							Union[str, Any]	)							->	Optional[Any]:
								return XGLMConfig.from_pretrained('''facebook/xglm-564M'''	)





							def       snake_case__						(      self				:							Union[str, Any]	)							->	List[Any]:
								__UpperCAmelCase							=			tf.clip_by_value(
								    ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size	)     ,		clip_value_min=0     ,		clip_value_max=3	)

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

								__UpperCAmelCase							=			self.get_config()

								__UpperCAmelCase							=			floats_tensor([self.num_hidden_layers, self.num_attention_heads]     ,		2	)

								return (
								    config,
								    input_ids,
								    input_mask,
								    head_mask,
								)





							def       snake_case__						(      self				:							Optional[int]	)							->	Tuple:
								return XGLMConfig(
								    vocab_size=self.vocab_size     ,		d_model=self.hidden_size     ,		num_layers=self.num_hidden_layers     ,		attention_heads=self.num_attention_heads     ,		ffn_dim=self.ffn_dim     ,		activation_function=self.activation_function     ,		activation_dropout=self.activation_dropout     ,		attention_dropout=self.attention_dropout     ,		max_position_embeddings=self.max_position_embeddings     ,		initializer_range=self.initializer_range     ,		use_cache=__a     ,		bos_token_id=self.bos_token_id     ,		eos_token_id=self.eos_token_id     ,		pad_token_id=self.pad_token_id     ,		return_dict=__a     ,		)






							def       snake_case__						(      self				:							Any	)							->	str:
								__UpperCAmelCase							=			self.prepare_config_and_inputs()

								(
								    (
								    __UpperCAmelCase
								)				,			(
								    __UpperCAmelCase
								)				,			(
								    __UpperCAmelCase
								)				,			(
								    __UpperCAmelCase
								)				,			
								)							=			config_and_inputs

								__UpperCAmelCase							=			{
								    '''input_ids''': input_ids,
								    '''head_mask''': head_mask,
								}

								return config, inputs_dict






@require_tf
class 						A   (   UpperCAmelCase       ,	UpperCAmelCase       ,	unittest.TestCase   ):
							a_        =      (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
							a_        =      (TFXGLMForCausalLM,) if is_tf_available() else ()
							a_        =      (
							    {'''feature-extraction''': TFXGLMModel, '''text-generation''': TFXGLMForCausalLM} if is_tf_available() else {}
							)
							a_        =      False
							a_        =      False
							a_        =      False





							def       snake_case__						(      self				:							Dict	)							->	int:
								__UpperCAmelCase							=			TFXGLMModelTester(self	)
								__UpperCAmelCase							=			ConfigTester(self     ,		config_class=__a     ,		n_embd=3_7	)





							def       snake_case__						(      self				:							Optional[Any]	)							->	List[Any]:
								self.config_tester.run_common_tests()





							@slow
							def       snake_case__						(      self				:							Union[str, Any]	)							->	Optional[int]:
								for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__UpperCAmelCase							=			TFXGLMModel.from_pretrained(__a	)
									self.assertIsNotNone(__a	)






							@unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.'''	)
							def       snake_case__						(      self				:							List[Any]	)							->	int:
								super().test_resize_token_embeddings()






@require_tf
class 						A   (   unittest.TestCase   ):





							@slow
							def       snake_case__						(      self				:							Dict     ,		__a				:							Optional[Any]=True	)							->	Union[str, Any]:
								__UpperCAmelCase							=			TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''	)
								__UpperCAmelCase							=			tf.convert_to_tensor([[2, 2_6_8, 9_8_6_5]]     ,		dtype=tf.intaa	)  # The dog
								# </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
								# fmt: off
								__UpperCAmelCase							=			[2, 2_6_8, 9_8_6_5, 6_7, 1_1, 1_9_8_8, 5_7_2_5_2, 9_8_6_5, 5, 9_8_4, 6_7, 1_9_8_8, 2_1_3_8_3_8, 1_6_5_8, 5_3, 7_0_4_4_6, 3_3, 6_6_5_7, 2_7_8, 1_5_8_1]
								# fmt: on
								__UpperCAmelCase							=			model.generate(__a     ,		do_sample=__a     ,		num_beams=1	)
								if verify_outputs:
									self.assertListEqual(output_ids[0].numpy().tolist()     ,		__a	)





							@slow
							def       snake_case__						(      self				:							Union[str, Any]	)							->	Tuple:
								__UpperCAmelCase							=			XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''	)
								__UpperCAmelCase							=			TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''	)

								tf.random.set_seed(0	)
								__UpperCAmelCase							=			tokenizer('''Today is a nice day and'''     ,		return_tensors='''tf'''	)
								__UpperCAmelCase							=			tokenized.input_ids
								# forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
								with tf.device(''':/CPU:0'''	):
									__UpperCAmelCase							=			model.generate(__a     ,		do_sample=__a     ,		seed=[7, 0]	)
								__UpperCAmelCase							=			tokenizer.decode(output_ids[0]     ,		skip_special_tokens=__a	)

								__UpperCAmelCase							=			(
								    '''Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due'''
								)
								self.assertEqual(__a     ,		__a	)






							@slow
							def       snake_case__						(      self				:							List[Any]	)							->	Dict:
								__UpperCAmelCase							=			TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''	)
								__UpperCAmelCase							=			XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''	)

								__UpperCAmelCase							=			'''left'''

								# use different length sentences to test batching
								__UpperCAmelCase							=			[
								    '''This is an extremelly long sentence that only exists to test the ability of the model to cope with '''
								    '''left-padding, such as in batched generation. The output for the sequence below should be the same '''
								    '''regardless of whether left padding is applied or not. When''',
								    '''Hello, my dog is a little''',
								]

								__UpperCAmelCase							=			tokenizer(__a     ,		return_tensors='''tf'''     ,		padding=__a	)
								__UpperCAmelCase							=			inputs['''input_ids''']

								__UpperCAmelCase							=			model.generate(input_ids=__a     ,		attention_mask=inputs['''attention_mask''']     ,		max_new_tokens=1_2	)

								__UpperCAmelCase							=			tokenizer(sentences[0]     ,		return_tensors='''tf'''	).input_ids
								__UpperCAmelCase							=			model.generate(input_ids=__a     ,		max_new_tokens=1_2	)

								__UpperCAmelCase							=			tokenizer(sentences[1]     ,		return_tensors='''tf'''	).input_ids
								__UpperCAmelCase							=			model.generate(input_ids=__a     ,		max_new_tokens=1_2	)

								__UpperCAmelCase							=			tokenizer.batch_decode(__a     ,		skip_special_tokens=__a	)
								__UpperCAmelCase							=			tokenizer.decode(output_non_padded[0]     ,		skip_special_tokens=__a	)
								__UpperCAmelCase							=			tokenizer.decode(output_padded[0]     ,		skip_special_tokens=__a	)

								__UpperCAmelCase							=			[
								    '''This is an extremelly long sentence that only exists to test the ability of the model to cope with '''
								    '''left-padding, such as in batched generation. The output for the sequence below should be the same '''
								    '''regardless of whether left padding is applied or not. When left padding is applied, the sequence will be '''
								    '''a single''',
								    '''Hello, my dog is a little bit of a shy one, but he is very friendly''',
								]
								self.assertListEqual(__a     ,		__a	)
								self.assertListEqual(__a     ,		[non_padded_sentence, padded_sentence]	)

'''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	:       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 						A   (   unittest.TestCase   ):





							def       snake_case__						(      self				:							Any     ,		__a				:							str     ,		__a				:							bool     ,		__a				:							str = None     ,		__a				:							list = None	)							->	Tuple:
								__UpperCAmelCase							=			None
								__UpperCAmelCase							=			os.path.abspath(os.path.join('''examples'''     ,		'''by_feature'''	)	)
								__UpperCAmelCase							=			os.path.abspath('''examples'''	)
								for item in os.listdir(__a	):
									if item not in EXCLUDE_EXAMPLES:
										__UpperCAmelCase							=			os.path.join(__a     ,		__a	)
										if os.path.isfile(__a	) and ".py" in item_path:
											with self.subTest(
											    tested_script=__a     ,		feature_script=__a     ,		tested_section='''main()''' if parser_only else '''training_function()'''     ,		):
												__UpperCAmelCase							=			compare_against_test(
												    os.path.join(__a     ,		__a	)     ,		__a     ,		__a     ,		__a	)
												__UpperCAmelCase							=			'''\n'''.join(__a	)
												if special_strings is not None:
													for string in special_strings:
														__UpperCAmelCase							=			diff.replace(__a     ,		''''''	)
												self.assertEqual(__a     ,		''''''	)





							def       snake_case__						(      self				:							Optional[Any]	)							->	str:
								self.one_complete_example('''complete_nlp_example.py'''     ,		__a	)
								self.one_complete_example('''complete_nlp_example.py'''     ,		__a	)






							def       snake_case__						(      self				:							List[str]	)							->	Tuple:
								__UpperCAmelCase							=			os.path.abspath(os.path.join('''examples'''     ,		'''cv_example.py'''	)	)
								__UpperCAmelCase							=			[
								    ''' ''' * 1_6 + '''{\n\n''',
								    ''' ''' * 2_0 + '''"accuracy": eval_metric["accuracy"],\n\n''',
								    ''' ''' * 2_0 + '''"f1": eval_metric["f1"],\n\n''',
								    ''' ''' * 2_0 + '''"train_loss": total_loss.item() / len(train_dataloader),\n\n''',
								    ''' ''' * 2_0 + '''"epoch": epoch,\n\n''',
								    ''' ''' * 1_6 + '''},\n\n''',
								    ''' ''' * 1_6 + '''step=epoch,\n''',
								    ''' ''' * 1_2,
								    ''' ''' * 8 + '''for step, batch in enumerate(active_dataloader):\n''',
								]
								self.one_complete_example('''complete_cv_example.py'''     ,		__a     ,		__a     ,		__a	)
								self.one_complete_example('''complete_cv_example.py'''     ,		__a     ,		__a     ,		__a	)






@mock.patch.dict(os.environ       ,	{'''TESTING_MOCKED_DATALOADERS''': '''1'''}   )
class 						A   (   UpperCAmelCase   ):
							a_        =      False





							@classmethod
							def       snake_case__						(      cls				:							Tuple	)							->	str:
								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       snake_case__						(      cls				:							Dict	)							->	int:
								super().tearDownClass()
								shutil.rmtree(cls._tmpdir	)





							def       snake_case__						(      self				:							Tuple	)							->	Dict:
								__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       snake_case__						(      self				:							str	)							->	int:
								__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       snake_case__						(      self				:							Any	)							->	Any:
								__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=__a	)
								self.assertNotIn('''epoch 0:'''     ,		__a	)
								self.assertIn('''epoch 1:'''     ,		__a	)





							def       snake_case__						(      self				:							Tuple	)							->	Optional[int]:
								__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=__a	)
								if torch.cuda.is_available():
									__UpperCAmelCase							=			torch.cuda.device_count()
								else:
									__UpperCAmelCase							=			1
								if num_processes > 1:
									self.assertNotIn('''epoch 0:'''     ,		__a	)
									self.assertIn('''epoch 1:'''     ,		__a	)
								else:
									self.assertIn('''epoch 0:'''     ,		__a	)
									self.assertIn('''epoch 1:'''     ,		__a	)





							@slow
							def       snake_case__						(      self				:							Any	)							->	Optional[Any]:
								__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=__a	)
									__UpperCAmelCase							=			re.findall('''({.+})'''     ,		__a	)
									__UpperCAmelCase							=			[r for r in results if '''accuracy''' in r][-1]
									__UpperCAmelCase							=			ast.literal_eval(__a	)
									self.assertGreaterEqual(results['''accuracy''']     ,		0.7_5	)





							def       snake_case__						(      self				:							Dict	)							->	int:
								__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       snake_case__						(      self				:							Optional[Any]	)							->	Optional[int]:
								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(__a     ,		'''tracking'''	)	)	)





							def       snake_case__						(      self				:							Optional[int]	)							->	List[Any]:
								__UpperCAmelCase							=			['''examples/by_feature/gradient_accumulation.py''']
								run_command(self._launch_args + testargs	)






							def       snake_case__						(      self				:							Tuple	)							->	Optional[Any]:
								__UpperCAmelCase							=			['''examples/by_feature/local_sgd.py''']
								run_command(self._launch_args + testargs	)
