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


SCREAMING_SNAKE_CASE_            =    logging.get_logger(__name__)
class   _UpperCAmelCase				(		SCREAMING_SNAKE_CASE_				):




      def      a_   (				self ,							lowercase_				)    ->       str:
          if isinstance(lowercase_ ,							lowercase_				):
              UpperCAmelCase          =       [label.strip() for label in labels.split(','				) if label.strip()]
          return labels




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

          if isinstance(lowercase_ ,							lowercase_				):
              UpperCAmelCase          =       [sequences]

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

          return sequence_pairs, sequences

@add_end_docstrings(SCREAMING_SNAKE_CASE_				)
class   _UpperCAmelCase				(		SCREAMING_SNAKE_CASE_				):




      def __init__(				self ,							lowercase_=ZeroShotClassificationArgumentHandler() ,							*lowercase_ ,							**lowercase_				)    ->       Tuple:
          UpperCAmelCase          =       args_parser
          super().__init__(*lowercase_ ,							**lowercase_				)
          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      a_   (				self				)    ->       Any:
          for label, ind in self.model.config.labelaid.items():
              if label.lower().startswith('entail'				):
                  return ind
          return -1




      def      a_   (				self ,							lowercase_ ,							lowercase_=True ,							lowercase_=True ,							lowercase_=TruncationStrategy.ONLY_FIRST ,							**lowercase_				)    ->       Any:
          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(
                  lowercase_ ,							add_special_tokens=lowercase_ ,							return_tensors=lowercase_ ,							padding=lowercase_ ,							truncation=lowercase_ ,							)
          except Exception as e:
              if "too short" in str(lowercase_				):
                  # 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(
                      lowercase_ ,							add_special_tokens=lowercase_ ,							return_tensors=lowercase_ ,							padding=lowercase_ ,							truncation=TruncationStrategy.DO_NOT_TRUNCATE ,							)
              else:
                  raise e

          return inputs




      def      a_   (				self ,							**lowercase_				)    ->       Tuple:
          if kwargs.get('multi_class' ,							lowercase_				) 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 ,							lowercase_ ,							*lowercase_ ,							**lowercase_ ,							)    ->       Any:
          if len(lowercase_				) == 0:
              pass
          elif len(lowercase_				) == 1 and "candidate_labels" not in kwargs:
              UpperCAmelCase          =       args[0]
          else:
              raise ValueError(F"Unable to understand extra arguments {args}"				)

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




      def      a_   (				self ,							lowercase_ ,							lowercase_=None ,							lowercase_="This example is {}."				)    ->       str:
          UpperCAmelCase      ,				UpperCAmelCase          =       self._args_parser(lowercase_ ,							lowercase_ ,							lowercase_				)

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

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




      def      a_   (				self ,							lowercase_				)    ->       List[Any]:
          UpperCAmelCase          =       inputs['candidate_label']
          UpperCAmelCase          =       inputs['sequence']
          UpperCAmelCase          =       {k: inputs[k] for k in self.tokenizer.model_input_names}
          UpperCAmelCase          =       self.model(**lowercase_				)

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




      def      a_   (				self ,							lowercase_ ,							lowercase_=False				)    ->       str:
          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(lowercase_				)
          UpperCAmelCase          =       N // n
          UpperCAmelCase          =       logits.reshape((num_sequences, n, -1)				)

          if multi_label or len(lowercase_				) == 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(lowercase_				) / np.exp(lowercase_				).sum(-1 ,							keepdims=lowercase_				)
              UpperCAmelCase          =       scores[..., 1]
          else:
              # softmax the "entailment" logits over all candidate labels
              UpperCAmelCase          =       reshaped_outputs[..., self.entailment_id]
              UpperCAmelCase          =       np.exp(lowercase_				) / np.exp(lowercase_				).sum(-1 ,							keepdims=lowercase_				)

          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(),
          }





"""simple docstring"""

import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_            =    logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_            =    {
    '''asapp/sew-d-tiny-100k''': '''https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json''',
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}
class   _UpperCAmelCase				(		SCREAMING_SNAKE_CASE_				):
      __SCREAMING_SNAKE_CASE :				List[str]				   =						"sew-d"




      def __init__(				self ,							lowercase_=3_2 ,							lowercase_=7_6_8 ,							lowercase_=1_2 ,							lowercase_=1_2 ,							lowercase_=3_0_7_2 ,							lowercase_=2 ,							lowercase_=5_1_2 ,							lowercase_=2_5_6 ,							lowercase_=True ,							lowercase_=True ,							lowercase_=("p2c", "c2p") ,							lowercase_="layer_norm" ,							lowercase_="gelu_python" ,							lowercase_=0.1 ,							lowercase_=0.1 ,							lowercase_=0.1 ,							lowercase_=0.0 ,							lowercase_=0.1 ,							lowercase_=0.0_2 ,							lowercase_=1E-7 ,							lowercase_=1E-5 ,							lowercase_="group" ,							lowercase_="gelu" ,							lowercase_=(6_4, 1_2_8, 1_2_8, 1_2_8, 1_2_8, 2_5_6, 2_5_6, 2_5_6, 2_5_6, 5_1_2, 5_1_2, 5_1_2, 5_1_2) ,							lowercase_=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,							lowercase_=(1_0, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,							lowercase_=False ,							lowercase_=1_2_8 ,							lowercase_=1_6 ,							lowercase_=True ,							lowercase_=0.0_5 ,							lowercase_=1_0 ,							lowercase_=2 ,							lowercase_=0.0 ,							lowercase_=1_0 ,							lowercase_=0 ,							lowercase_="mean" ,							lowercase_=False ,							lowercase_=False ,							lowercase_=2_5_6 ,							lowercase_=0 ,							lowercase_=1 ,							lowercase_=2 ,							**lowercase_ ,							)    ->       Dict:
          super().__init__(**lowercase_ ,							pad_token_id=lowercase_ ,							bos_token_id=lowercase_ ,							eos_token_id=lowercase_				)
          UpperCAmelCase          =       hidden_size
          UpperCAmelCase          =       feat_extract_norm
          UpperCAmelCase          =       feat_extract_activation
          UpperCAmelCase          =       list(lowercase_				)
          UpperCAmelCase          =       list(lowercase_				)
          UpperCAmelCase          =       list(lowercase_				)
          UpperCAmelCase          =       conv_bias
          UpperCAmelCase          =       num_conv_pos_embeddings
          UpperCAmelCase          =       num_conv_pos_embedding_groups
          UpperCAmelCase          =       len(self.conv_dim				)
          UpperCAmelCase          =       num_hidden_layers
          UpperCAmelCase          =       intermediate_size
          UpperCAmelCase          =       squeeze_factor
          UpperCAmelCase          =       max_position_embeddings
          UpperCAmelCase          =       position_buckets
          UpperCAmelCase          =       share_att_key
          UpperCAmelCase          =       relative_attention
          UpperCAmelCase          =       norm_rel_ebd
          UpperCAmelCase          =       list(lowercase_				)
          UpperCAmelCase          =       hidden_act
          UpperCAmelCase          =       num_attention_heads
          UpperCAmelCase          =       hidden_dropout
          UpperCAmelCase          =       attention_dropout
          UpperCAmelCase          =       activation_dropout
          UpperCAmelCase          =       feat_proj_dropout
          UpperCAmelCase          =       final_dropout
          UpperCAmelCase          =       layer_norm_eps
          UpperCAmelCase          =       feature_layer_norm_eps
          UpperCAmelCase          =       initializer_range
          UpperCAmelCase          =       vocab_size

          if (
              (len(self.conv_stride				) != self.num_feat_extract_layers)
              or (len(self.conv_kernel				) != self.num_feat_extract_layers)
              or (len(self.conv_dim				) != self.num_feat_extract_layers)
          ):
              raise ValueError(
                  'Configuration for convolutional layers is incorrect.'
                  'It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,'
                  F"but is `len(config.conv_dim) = {len(self.conv_dim				)}`, `len(config.conv_stride)"
                  F"= {len(self.conv_stride				)}`, `len(config.conv_kernel) = {len(self.conv_kernel				)}`."				)

          # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
          UpperCAmelCase          =       apply_spec_augment
          UpperCAmelCase          =       mask_time_prob
          UpperCAmelCase          =       mask_time_length
          UpperCAmelCase          =       mask_time_min_masks
          UpperCAmelCase          =       mask_feature_prob
          UpperCAmelCase          =       mask_feature_length
          UpperCAmelCase          =       mask_feature_min_masks

          # ctc loss
          UpperCAmelCase          =       ctc_loss_reduction
          UpperCAmelCase          =       ctc_zero_infinity

          # sequence classification
          UpperCAmelCase          =       use_weighted_layer_sum
          UpperCAmelCase          =       classifier_proj_size




      @property
      def      a_   (				self				)    ->       Tuple:
          return functools.reduce(operator.mul ,							self.conv_stride ,							1				)



