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

_a														=		[
    """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 						UpperCAmelCase							(					self				,		__a				,		__a				,		__a = None				,		__a = 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(__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 						UpperCAmelCase							(					self)       ->       Optional[int]:






							'''simple docstring'''
							self.one_complete_example('''complete_nlp_example.py'''				,		__a)
							self.one_complete_example('''complete_nlp_example.py'''				,		__a)




		def 						UpperCAmelCase							(					self)       ->       Tuple:






							'''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'''				,		__a				,		__a				,		__a)
							self.one_complete_example('''complete_cv_example.py'''				,		__a				,		__a				,		__a)






@mock.patch.dict(os.environ      ,     {'TESTING_MOCKED_DATALOADERS': '1'}	)
class     _UpperCAmelCase(    lowerCamelCase	):
		lowercase__						=      False



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






							'''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 						UpperCAmelCase							(					cls)       ->       Dict:






							'''simple docstring'''
							super().tearDownClass()
							shutil.rmtree(cls._tmpdir)



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






							'''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 						UpperCAmelCase							(					self)       ->       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 						UpperCAmelCase							(					self)       ->       Optional[Any]:






							'''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=__a)
							self.assertNotIn('''epoch 0:'''				,		__a)
							self.assertIn('''epoch 1:'''				,		__a)



		def 						UpperCAmelCase							(					self)       ->       Any:






							'''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=__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 						UpperCAmelCase							(					self)       ->       Dict:






							'''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=__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.75)



		def 						UpperCAmelCase							(					self)       ->       Tuple:






							'''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 						UpperCAmelCase							(					self)       ->       int:






							'''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(__a				,		'''tracking''')))



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






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




		def 						UpperCAmelCase							(					self)       ->       str:






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








"""simple docstring"""





from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


_a														=		logging.get_logger(__name__)






class     _UpperCAmelCase(    lowerCamelCase	):



		def 						UpperCAmelCase							(					self				,		__a)       ->       Tuple:






							'''simple docstring'''
							if isinstance(__a				,		__a):
												_UpperCamelCase							 =  [label.strip() for label in labels.split(''',''') if label.strip()]
							return labels




		def __call__(					self				,		__a				,		__a				,		__a)       ->       Optional[int]:






							'''simple docstring'''
							if len(__a) == 0 or len(__a) == 0:
												raise ValueError('''You must include at least one label and at least one sequence.''')
							if hypothesis_template.format(labels[0]) == hypothesis_template:
												raise ValueError(
												    (
												        '''The provided hypothesis_template "{}" was not able to be formatted with the target labels. '''
												        '''Make sure the passed template includes formatting syntax such as {{}} where the label should go.'''
												    ).format(__a))

							if isinstance(__a				,		__a):
												_UpperCamelCase							 =  [sequences]

							_UpperCamelCase							 =  []
							for sequence in sequences:
												sequence_pairs.extend([[sequence, hypothesis_template.format(__a)] for label in labels])

							return sequence_pairs, sequences






@add_end_docstrings(lowerCamelCase	)
class     _UpperCAmelCase(    lowerCamelCase	):



		def __init__(					self				,		__a=ZeroShotClassificationArgumentHandler()				,		*__a				,		**__a)       ->       List[str]:






							'''simple docstring'''
							_UpperCamelCase							 =  args_parser
							super().__init__(*__a				,		**__a)
							if self.entailment_id == -1:
												logger.warning(
												    '''Failed to determine \'entailment\' label id from the label2id mapping in the model config. Setting to '''
												    '''-1. Define a descriptive label2id mapping in the model config to ensure correct outputs.''')



		@property
		def 						UpperCAmelCase							(					self)       ->       List[str]:






							'''simple docstring'''
							for label, ind in self.model.config.labelaid.items():
												if label.lower().startswith('''entail'''):
																	return ind
							return -1



		def 						UpperCAmelCase							(					self				,		__a				,		__a=True				,		__a=True				,		__a=TruncationStrategy.ONLY_FIRST				,		**__a)       ->       List[Any]:






							'''simple docstring'''
							_UpperCamelCase							 =  self.framework
							if self.tokenizer.pad_token is None:
												# Override for tokenizers not supporting padding
												logger.error(
												    '''Tokenizer was not supporting padding necessary for zero-shot, attempting to use '''
												    ''' `pad_token=eos_token`''')
												_UpperCamelCase							 =  self.tokenizer.eos_token
							try:
												_UpperCamelCase							 =  self.tokenizer(
												    __a				,		add_special_tokens=__a				,		return_tensors=__a				,		padding=__a				,		truncation=__a				,		)
							except Exception as e:
												if "too short" in str(__a):
																	# tokenizers might yell that we want to truncate
																	# to a value that is not even reached by the input.
																	# In that case we don't want to truncate.
																	# It seems there's not a really better way to catch that
																	# exception.

																	_UpperCamelCase							 =  self.tokenizer(
																	    __a				,		add_special_tokens=__a				,		return_tensors=__a				,		padding=__a				,		truncation=TruncationStrategy.DO_NOT_TRUNCATE				,		)
												else:
																	raise e

							return inputs



		def 						UpperCAmelCase							(					self				,		**__a)       ->       Any:






							'''simple docstring'''
							if kwargs.get('''multi_class'''				,		__a) is not None:
												_UpperCamelCase							 =  kwargs['''multi_class''']
												logger.warning(
												    '''The `multi_class` argument has been deprecated and renamed to `multi_label`. '''
												    '''`multi_class` will be removed in a future version of Transformers.''')
							_UpperCamelCase							 =  {}
							if "candidate_labels" in kwargs:
												_UpperCamelCase							 =  self._args_parser._parse_labels(kwargs['''candidate_labels'''])
							if "hypothesis_template" in kwargs:
												_UpperCamelCase							 =  kwargs['''hypothesis_template''']

							_UpperCamelCase							 =  {}
							if "multi_label" in kwargs:
												_UpperCamelCase							 =  kwargs['''multi_label''']
							return preprocess_params, {}, postprocess_params



		def __call__(					self				,		__a				,		*__a				,		**__a				,		)       ->       int:






							'''simple docstring'''
							if len(__a) == 0:
												pass
							elif len(__a) == 1 and "candidate_labels" not in kwargs:
												_UpperCamelCase							 =  args[0]
							else:
												raise ValueError(F'''Unable to understand extra arguments {args}''')

							return super().__call__(__a				,		**__a)



		def 						UpperCAmelCase							(					self				,		__a				,		__a=None				,		__a="This example is {}.")       ->       Dict:






							'''simple docstring'''
							_UpperCamelCase ,  _UpperCamelCase							 =  self._args_parser(__a				,		__a				,		__a)

							for i, (candidate_label, sequence_pair) in enumerate(zip(__a				,		__a)):
												_UpperCamelCase							 =  self._parse_and_tokenize([sequence_pair])

												yield {
												    "candidate_label": candidate_label,
												    "sequence": sequences[0],
												    "is_last": i == len(__a) - 1,
												    **model_input,
												}



		def 						UpperCAmelCase							(					self				,		__a)       ->       List[Any]:






							'''simple docstring'''
							_UpperCamelCase							 =  inputs['''candidate_label''']
							_UpperCamelCase							 =  inputs['''sequence''']
							_UpperCamelCase							 =  {k: inputs[k] for k in self.tokenizer.model_input_names}
							_UpperCamelCase							 =  self.model(**__a)

							_UpperCamelCase							 =  {
							    '''candidate_label''': candidate_label,
							    '''sequence''': sequence,
							    '''is_last''': inputs['''is_last'''],
							    **outputs,
							}
							return model_outputs




		def 						UpperCAmelCase							(					self				,		__a				,		__a=False)       ->       Dict:






							'''simple docstring'''
							_UpperCamelCase							 =  [outputs['''candidate_label'''] for outputs in model_outputs]
							_UpperCamelCase							 =  [outputs['''sequence'''] for outputs in model_outputs]
							_UpperCamelCase							 =  np.concatenate([output['''logits'''].numpy() for output in model_outputs])
							_UpperCamelCase							 =  logits.shape[0]
							_UpperCamelCase							 =  len(__a)
							_UpperCamelCase							 =  N // n
							_UpperCamelCase							 =  logits.reshape((num_sequences, n, -1))

							if multi_label or len(__a) == 1:
												# softmax over the entailment vs. contradiction dim for each label independently
												_UpperCamelCase							 =  self.entailment_id
												_UpperCamelCase							 =  -1 if entailment_id == 0 else 0
												_UpperCamelCase							 =  reshaped_outputs[..., [contradiction_id, entailment_id]]
												_UpperCamelCase							 =  np.exp(__a) / np.exp(__a).sum(-1				,		keepdims=__a)
												_UpperCamelCase							 =  scores[..., 1]
							else:
												# softmax the "entailment" logits over all candidate labels
												_UpperCamelCase							 =  reshaped_outputs[..., self.entailment_id]
												_UpperCamelCase							 =  np.exp(__a) / np.exp(__a).sum(-1				,		keepdims=__a)

							_UpperCamelCase							 =  list(reversed(scores[0].argsort()))
							return {
							    "sequence": sequences[0],
							    "labels": [candidate_labels[i] for i in top_inds],
							    "scores": scores[0, top_inds].tolist(),
							}






