






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


SCREAMING_SNAKE_CASE						:						Any					   =       logging.get_logger(__name__)





class       snake_case_       (    __A       ):



	"""simple docstring"""

	def       _UpperCAmelCase						(      self  ,					__a						):


					"""simple docstring"""

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






	def __call__(      self  ,					__a  ,					__a  ,					__a						):


					"""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						):
									A__							 =       [sequences]

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

					return sequence_pairs, sequences





@add_end_docstrings(__A       )
class       snake_case_       (    __A       ):



	"""simple docstring"""

	def __init__(      self  ,					__a=ZeroShotClassificationArgumentHandler()  ,					*__a  ,					**__a						):


					"""simple docstring"""

					A__							 =       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						):


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


					"""simple docstring"""

					A__							 =       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`'						)
									A__							 =       self.tokenizer.eos_token
					try:
									A__							 =       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.

													A__							 =       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						):


					"""simple docstring"""

					if kwargs.get('multi_class'  ,					__a						) is not None:
									A__							 =       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.'						)
					A__							 =       {}
					if "candidate_labels" in kwargs:
									A__							 =       self._args_parser._parse_labels(kwargs['candidate_labels']						)
					if "hypothesis_template" in kwargs:
									A__							 =       kwargs['''hypothesis_template''']

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


	def __call__(      self  ,					__a  ,					*__a  ,					**__a  ,					):


					"""simple docstring"""

					if len(__a						) == 0:
									pass
					elif len(__a						) == 1 and "candidate_labels" not in kwargs:
									A__							 =       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 {}."						):


					"""simple docstring"""

					A__							 =       self._args_parser(__a  ,					__a  ,					__a						)

					for i, (candidate_label, sequence_pair) in enumerate(zip(__a  ,					__a						)						):
									A__							 =       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						):


					"""simple docstring"""

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

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






	def       _UpperCAmelCase						(      self  ,					__a  ,					__a=False						):


					"""simple docstring"""

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

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

					A__							 =       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"""






# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


SCREAMING_SNAKE_CASE						:						Dict					   =       '''Create a default config file for Accelerate with only a few flags set.'''



def  __lowerCamelCase					(					lowerCAmelCase__="no"       ,lowerCAmelCase__ = default_json_config_file       ,lowerCAmelCase__ = False				):
				A__							 =       Path(lowerCAmelCase__				)
				path.parent.mkdir(parents=lowerCAmelCase__       ,exist_ok=lowerCAmelCase__				)
				if path.exists():
								print(
								    f'''Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.'''				)
								return False
				A__							 =       mixed_precision.lower()
				if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
								raise ValueError(
								    f'''`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}'''				)
				A__							 =       {
				    'compute_environment': 'LOCAL_MACHINE',
				    'mixed_precision': mixed_precision,
				}
				if torch.cuda.is_available():
								A__							 =       torch.cuda.device_count()
								A__							 =       num_gpus
								A__							 =       False
								if num_gpus > 1:
												A__							 =       'MULTI_GPU'
								else:
												A__							 =       'NO'
				elif is_xpu_available() and use_xpu:
								A__							 =       torch.xpu.device_count()
								A__							 =       num_xpus
								A__							 =       False
								if num_xpus > 1:
												A__							 =       'MULTI_XPU'
								else:
												A__							 =       'NO'
				elif is_npu_available():
								A__							 =       torch.npu.device_count()
								A__							 =       num_npus
								A__							 =       False
								if num_npus > 1:
												A__							 =       'MULTI_NPU'
								else:
												A__							 =       'NO'
				else:
								A__							 =       0
								A__							 =       True
								A__							 =       1
								A__							 =       'NO'
				A__							 =       ClusterConfig(**lowerCAmelCase__				)
				config.to_json_file(lowerCAmelCase__				)
				return path



def  __lowerCamelCase					(					lowerCAmelCase__       ,lowerCAmelCase__				):
				A__							 =       parser.add_parser('default'       ,parents=lowerCAmelCase__       ,help=lowerCAmelCase__       ,formatter_class=lowerCAmelCase__				)
				parser.add_argument(
				    '--config_file'       ,default=lowerCAmelCase__       ,help=(
				        'The path to use to store the config file. Will default to a file named default_config.yaml in the cache '
				        'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
				        'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
				        'with \'huggingface\'.'
				    )       ,dest='save_location'       ,)

				parser.add_argument(
				    '--mixed_precision'       ,choices=['no', 'fp16', 'bf16']       ,type=lowerCAmelCase__       ,help='Whether or not to use mixed precision training. '
				    'Choose between FP16 and BF16 (bfloat16) training. '
				    'BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.'       ,default='no'       ,)
				parser.set_defaults(func=lowerCAmelCase__				)
				return parser







def  __lowerCamelCase					(					lowerCAmelCase__				):
				A__							 =       write_basic_config(args.mixed_precision       ,args.save_location				)
				if config_file:
								print(f'''accelerate configuration saved at {config_file}'''				)


