"""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  _snake_case     (					a__		):






						def       lowerCamelCase__     (       self      :			Union[str, Any]							,					UpperCAmelCase      :			Union[str, Any]    ):
													if isinstance(UpperCAmelCase							,					UpperCAmelCase    ):
																				__lowerCamelCase					:		str		   =     [label.strip() for label in labels.split(","    ) if label.strip()]
													return labels







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

													if isinstance(UpperCAmelCase							,					UpperCAmelCase    ):
																				__lowerCamelCase					:		int		   =     [sequences]

													__lowerCamelCase					:		Optional[int]		   =     []
													for sequence in sequences:
																				sequence_pairs.extend([[sequence, hypothesis_template.format(UpperCAmelCase    )] for label in labels]    )

													return sequence_pairs, sequences

@add_end_docstrings(a__		)
class  _snake_case     (					a__		):






						def __init__(       self      :			Tuple							,					UpperCAmelCase      :			Any=ZeroShotClassificationArgumentHandler()							,					*UpperCAmelCase      :			Optional[int]							,					**UpperCAmelCase      :			Optional[Any]    ):
													__lowerCamelCase					:		Optional[Any]		   =     args_parser
													super().__init__(*UpperCAmelCase							,					**UpperCAmelCase    )
													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       lowerCamelCase__     (       self      :			int    ):
													for label, ind in self.model.config.labelaid.items():
																				if label.lower().startswith("entail"    ):
																											return ind
													return -1






						def       lowerCamelCase__     (       self      :			List[str]							,					UpperCAmelCase      :			List[str]							,					UpperCAmelCase      :			Any=True							,					UpperCAmelCase      :			List[str]=True							,					UpperCAmelCase      :			str=TruncationStrategy.ONLY_FIRST							,					**UpperCAmelCase      :			str    ):
													__lowerCamelCase					:		Optional[int]		   =     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					:		Union[str, Any]		   =     self.tokenizer.eos_token
													try:
																				__lowerCamelCase					:		List[str]		   =     self.tokenizer(
																				    UpperCAmelCase							,					add_special_tokens=UpperCAmelCase							,					return_tensors=UpperCAmelCase							,					padding=UpperCAmelCase							,					truncation=UpperCAmelCase							,					)
													except Exception as e:
																				if "too short" in str(UpperCAmelCase    ):
																											# 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					:		Tuple		   =     self.tokenizer(
																											    UpperCAmelCase							,					add_special_tokens=UpperCAmelCase							,					return_tensors=UpperCAmelCase							,					padding=UpperCAmelCase							,					truncation=TruncationStrategy.DO_NOT_TRUNCATE							,					)
																				else:
																											raise e

													return inputs






						def       lowerCamelCase__     (       self      :			List[Any]							,					**UpperCAmelCase      :			Tuple    ):
													if kwargs.get("multi_class"							,					UpperCAmelCase    ) is not None:
																				__lowerCamelCase					:		Any		   =     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					:		Optional[Any]		   =     {}
													if "candidate_labels" in kwargs:
																				__lowerCamelCase					:		Optional[Any]		   =     self._args_parser._parse_labels(kwargs["candidate_labels"]    )
													if "hypothesis_template" in kwargs:
																				__lowerCamelCase					:		List[str]		   =     kwargs["hypothesis_template"]

													__lowerCamelCase					:		Optional[int]		   =     {}
													if "multi_label" in kwargs:
																				__lowerCamelCase					:		Tuple		   =     kwargs["multi_label"]
													return preprocess_params, {}, postprocess_params






						def __call__(       self      :			Dict							,					UpperCAmelCase      :			Union[str, List[str]]							,					*UpperCAmelCase      :			Tuple							,					**UpperCAmelCase      :			List[Any]							,					):
													if len(UpperCAmelCase    ) == 0:
																				pass
													elif len(UpperCAmelCase    ) == 1 and "candidate_labels" not in kwargs:
																				__lowerCamelCase					:		Union[str, Any]		   =     args[0]
													else:
																				raise ValueError(F"""Unable to understand extra arguments {args}"""    )

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






						def       lowerCamelCase__     (       self      :			Any							,					UpperCAmelCase      :			int							,					UpperCAmelCase      :			Dict=None							,					UpperCAmelCase      :			List[str]="This example is {}."    ):
													__lowerCamelCase					:		Dict		   =     self._args_parser(UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase    )

													for i, (candidate_label, sequence_pair) in enumerate(zip(UpperCAmelCase							,					UpperCAmelCase    )    ):
																				__lowerCamelCase					:		int		   =     self._parse_and_tokenize([sequence_pair]    )

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






						def       lowerCamelCase__     (       self      :			Tuple							,					UpperCAmelCase      :			str    ):
													__lowerCamelCase					:		List[str]		   =     inputs["candidate_label"]
													__lowerCamelCase					:		Tuple		   =     inputs["sequence"]
													__lowerCamelCase					:		List[str]		   =     {k: inputs[k] for k in self.tokenizer.model_input_names}
													__lowerCamelCase					:		List[Any]		   =     self.model(**UpperCAmelCase    )

													__lowerCamelCase					:		str		   =     {
													    "candidate_label": candidate_label,
													    "sequence": sequence,
													    "is_last": inputs["is_last"],
													    **outputs,
													}
													return model_outputs







						def       lowerCamelCase__     (       self      :			int							,					UpperCAmelCase      :			Tuple							,					UpperCAmelCase      :			Any=False    ):
													__lowerCamelCase					:		Tuple		   =     [outputs["candidate_label"] for outputs in model_outputs]
													__lowerCamelCase					:		Any		   =     [outputs["sequence"] for outputs in model_outputs]
													__lowerCamelCase					:		str		   =     np.concatenate([output["logits"].numpy() for output in model_outputs]    )
													__lowerCamelCase					:		Union[str, Any]		   =     logits.shape[0]
													__lowerCamelCase					:		Tuple		   =     len(UpperCAmelCase    )
													__lowerCamelCase					:		Union[str, Any]		   =     N // n
													__lowerCamelCase					:		str		   =     logits.reshape((num_sequences, n, -1)    )

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

													__lowerCamelCase					:		str		   =     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 os
from argparse import ArgumentParser
from typing import List

import torch.utils.data

from datasets import Dataset, IterableDataset
from datasets.distributed import split_dataset_by_node


__A  =				4
__A  =				3
class  _snake_case     (					a__		):
						pass


def    lowercase_   (   _lowerCamelCase:	List[str]      ) ->      List[str]:



							'''simple docstring'''

							for shard in shards:
														for i in range(_lowerCamelCase      ):
																					yield {"i": i, "shard": shard}


def    lowercase_   (   ) ->      List[Any]:



							'''simple docstring'''

							__lowerCamelCase					:		List[Any]		   =     int(os.environ["RANK"]      )
							__lowerCamelCase					:		Optional[int]		   =     int(os.environ["WORLD_SIZE"]      )

							__lowerCamelCase					:		Any		   =     ArgumentParser()
							parser.add_argument("--streaming"						,   type=_lowerCamelCase      )
							parser.add_argument("--local_rank"						,   type=_lowerCamelCase      )
							parser.add_argument("--num_workers"						,   type=_lowerCamelCase						,   default=0      )
							__lowerCamelCase					:		Dict		   =     parser.parse_args()
							__lowerCamelCase					:		str		   =     args.streaming
							__lowerCamelCase					:		List[Any]		   =     args.num_workers

							__lowerCamelCase					:		Optional[Any]		   =     {"shards": [F"""shard_{shard_idx}""" for shard_idx in range(_lowerCamelCase      )]}
							__lowerCamelCase					:		int		   =     IterableDataset.from_generator(_lowerCamelCase						,   gen_kwargs=_lowerCamelCase      )
							if not streaming:
														__lowerCamelCase					:		Optional[int]		   =     Dataset.from_list(list(_lowerCamelCase      )      )

							__lowerCamelCase					:		Union[str, Any]		   =     split_dataset_by_node(_lowerCamelCase						,   rank=_lowerCamelCase						,   world_size=_lowerCamelCase      )
							__lowerCamelCase					:		Optional[Any]		   =     torch.utils.data.DataLoader(_lowerCamelCase						,   num_workers=_lowerCamelCase      )

							__lowerCamelCase					:		List[Any]		   =     NUM_SHARDS * NUM_ITEMS_PER_SHARD
							__lowerCamelCase					:		Optional[Any]		   =     full_size // world_size
							expected_local_size += int(rank < (full_size % world_size)      )

							__lowerCamelCase					:		Optional[Any]		   =     sum(1 for _ in dataloader      )
							if local_size != expected_local_size:
														raise FailedTestError(F"""local_size {local_size} != expected_local_size {expected_local_size}"""      )


if __name__ == "__main__":
				main()