


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


__UpperCAmelCase       							=				logging.get_logger(__name__)




class 						a__							(				a__    ):





							'''simple docstring'''






							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				Optional[Any]:
														if isinstance(lowerCamelCase_   ,       lowerCamelCase_  ):
																					lowerCAmelCase__			  =      [label.strip() for label in labels.split(''','''  ) if label.strip()]
														return labels







							def __call__(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				List[str]:
														if len(lowerCamelCase_  ) == 0 or len(lowerCamelCase_  ) == 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(lowerCamelCase_  )  )

														if isinstance(lowerCamelCase_   ,       lowerCamelCase_  ):
																					lowerCAmelCase__			  =      [sequences]

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

														return sequence_pairs, sequences




@add_end_docstrings(a__    )
class 						a__							(				a__    ):





							'''simple docstring'''






							def __init__(						self   ,       lowerCamelCase_=ZeroShotClassificationArgumentHandler()   ,       *lowerCamelCase_   ,       **lowerCamelCase_  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      args_parser
														super().__init__(*lowerCamelCase_   ,       **lowerCamelCase_  )
														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     __SCREAMING_SNAKE_CASE				(						self  )			->				List[Any]:
														for label, ind in self.model.config.labelaid.items():
																					if label.lower().startswith('''entail'''  ):
																												return ind
														return -1







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_=True   ,       lowerCamelCase_=True   ,       lowerCamelCase_=TruncationStrategy.ONLY_FIRST   ,       **lowerCamelCase_  )			->				Optional[Any]:
														lowerCAmelCase__			  =      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`'''  )
																					lowerCAmelCase__			  =      self.tokenizer.eos_token
														try:
																					lowerCAmelCase__			  =      self.tokenizer(
																					    lowerCamelCase_   ,       add_special_tokens=lowerCamelCase_   ,       return_tensors=lowerCamelCase_   ,       padding=lowerCamelCase_   ,       truncation=lowerCamelCase_   ,       )
														except Exception as e:
																					if "too short" in str(lowerCamelCase_  ):
																												# 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.

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

														return inputs







							def     __SCREAMING_SNAKE_CASE				(						self   ,       **lowerCamelCase_  )			->				List[str]:
														if kwargs.get('''multi_class'''   ,       lowerCamelCase_  ) is not None:
																					lowerCAmelCase__			  =      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.'''  )
														lowerCAmelCase__			  =      {}
														if "candidate_labels" in kwargs:
																					lowerCAmelCase__			  =      self._args_parser._parse_labels(kwargs['''candidate_labels''']  )
														if "hypothesis_template" in kwargs:
																					lowerCAmelCase__			  =      kwargs['''hypothesis_template''']

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







							def __call__(						self   ,       lowerCamelCase_   ,       *lowerCamelCase_   ,       **lowerCamelCase_   ,       )			->				Dict:
														if len(lowerCamelCase_  ) == 0:
																					pass
														elif len(lowerCamelCase_  ) == 1 and "candidate_labels" not in kwargs:
																					lowerCAmelCase__			  =      args[0]
														else:
																					raise ValueError(F"""Unable to understand extra arguments {args}"""  )

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







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_=None   ,       lowerCamelCase_="This example is {}."  )			->				Any:
														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self._args_parser(lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )

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

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







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				Optional[int]:
														lowerCAmelCase__			  =      inputs['''candidate_label''']
														lowerCAmelCase__			  =      inputs['''sequence''']
														lowerCAmelCase__			  =      {k: inputs[k] for k in self.tokenizer.model_input_names}
														lowerCAmelCase__			  =      self.model(**lowerCamelCase_  )

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







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_=False  )			->				Tuple:
														lowerCAmelCase__			  =      [outputs['''candidate_label'''] for outputs in model_outputs]
														lowerCAmelCase__			  =      [outputs['''sequence'''] for outputs in model_outputs]
														lowerCAmelCase__			  =      np.concatenate([output['''logits'''].numpy() for output in model_outputs]  )
														lowerCAmelCase__			  =      logits.shape[0]
														lowerCAmelCase__			  =      len(lowerCamelCase_  )
														lowerCAmelCase__			  =      N // n
														lowerCAmelCase__			  =      logits.reshape((num_sequences, n, -1)  )

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

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



'''simple docstring'''






import unittest

import torch
from torch import nn

from accelerate.test_utils import require_cuda
from accelerate.utils.memory import find_executable_batch_size, release_memory


def    _snake_case  (	)		->       Union[str, Any]:
							raise RuntimeError('''CUDA out of memory.'''			)




class 						a__							(				nn.Module    ):





							'''simple docstring'''






							def __init__(						self  )			->				int:
														super().__init__()
														lowerCAmelCase__			  =      nn.Linear(3   ,       4  )
														lowerCAmelCase__			  =      nn.BatchNormad(4  )
														lowerCAmelCase__			  =      nn.Linear(4   ,       5  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				Optional[Any]:
														return self.lineara(self.batchnorm(self.lineara(lowerCamelCase_  )  )  )




class 						a__							(				unittest.TestCase    ):





							'''simple docstring'''






							def     __SCREAMING_SNAKE_CASE				(						self  )			->				str:
														lowerCAmelCase__			  =      []

														@find_executable_batch_size(starting_batch_size=1_28  )
														def mock_training_loop_function(lowerCamelCase_  ):
																					nonlocal batch_sizes
																					batch_sizes.append(lowerCamelCase_  )
																					if batch_size != 8:
																												raise_fake_out_of_memory()

														mock_training_loop_function()
														self.assertListEqual(lowerCamelCase_   ,       [1_28, 64, 32, 16, 8]  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      []

														@find_executable_batch_size(starting_batch_size=1_28  )
														def mock_training_loop_function(lowerCamelCase_   ,       lowerCamelCase_  ):
																					nonlocal batch_sizes
																					batch_sizes.append(lowerCamelCase_  )
																					if batch_size != 8:
																												raise_fake_out_of_memory()
																					return batch_size, arga

														lowerCAmelCase__  ,   lowerCAmelCase__			  =      mock_training_loop_function('''hello'''  )
														self.assertListEqual(lowerCamelCase_   ,       [1_28, 64, 32, 16, 8]  )
														self.assertListEqual([bs, arga]   ,       [8, '''hello''']  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														@find_executable_batch_size(starting_batch_size=0  )
														def mock_training_loop_function(lowerCamelCase_  ):
																					pass

														with self.assertRaises(lowerCamelCase_  ) as cm:
																					mock_training_loop_function()
																					self.assertIn('''No executable batch size found, reached zero.'''   ,       cm.exception.args[0]  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														@find_executable_batch_size(starting_batch_size=16  )
														def mock_training_loop_function(lowerCamelCase_  ):
																					if batch_size > 0:
																												raise_fake_out_of_memory()
																					pass

														with self.assertRaises(lowerCamelCase_  ) as cm:
																					mock_training_loop_function()
																					self.assertIn('''No executable batch size found, reached zero.'''   ,       cm.exception.args[0]  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														@find_executable_batch_size(starting_batch_size=1_28  )
														def mock_training_loop_function(lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  ):
																					if batch_size != 8:
																												raise raise_fake_out_of_memory()

														with self.assertRaises(lowerCamelCase_  ) as cm:
																					mock_training_loop_function(1_28   ,       '''hello'''   ,       '''world'''  )
																					self.assertIn('''Batch size was passed into `f`'''   ,       cm.exception.args[0]  )
																					self.assertIn('''`f(arg1=\'hello\', arg2=\'world\')'''   ,       cm.exception.args[0]  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														@find_executable_batch_size(starting_batch_size=16  )
														def mock_training_loop_function(lowerCamelCase_  ):
																					raise ValueError('''Oops, we had an error!'''  )

														with self.assertRaises(lowerCamelCase_  ) as cm:
																					mock_training_loop_function()
																					self.assertIn('''Oops, we had an error!'''   ,       cm.exception.args[0]  )







							@require_cuda
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														lowerCAmelCase__			  =      torch.cuda.memory_allocated()
														lowerCAmelCase__			  =      ModelForTest()
														model.cuda()
														self.assertGreater(torch.cuda.memory_allocated()   ,       lowerCamelCase_  )
														lowerCAmelCase__			  =      release_memory(lowerCamelCase_  )
														self.assertEqual(torch.cuda.memory_allocated()   ,       lowerCamelCase_  )