'''simple docstring'''




import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


__SCREAMING_SNAKE_CASE       :     Any     =  logging.getLogger(__name__)

@dataclass(frozen=snake_case__    )
class 	lowerCamelCase_	:
      '''simple docstring'''

      __UpperCamelCase:  str
      __UpperCamelCase:  str
      __UpperCamelCase:  Optional[str]						=						None
      __UpperCamelCase:  Optional[str]						=						None
      __UpperCamelCase:  Optional[str]						=						None

@dataclass(frozen=snake_case__    )
class 	lowerCamelCase_	:
      '''simple docstring'''

      __UpperCamelCase:  List[int]
      __UpperCamelCase:  Optional[List[int]]						=						None
      __UpperCamelCase:  Optional[List[int]]						=						None
      __UpperCamelCase:  Optional[Union[int, float]]						=						None
      __UpperCamelCase:  Optional[int]						=						None


if is_torch_available():
  import torch
  from torch.utils.data import Dataset

  class 	lowerCamelCase_	(snake_case__    ):
        '''simple docstring'''

        __UpperCamelCase:  List[InputFeatures]




        def __init__(       self						:				Optional[Any]						,					A						:				str						,					A						:				PreTrainedTokenizer						,					A						:				str						,					A						:				Optional[int] = None						,					A						:				List[Any]=False						,					A						:				bool = False						,					):
               _UpperCAmelCase     :  Optional[int]							      =    hans_processors[task]()

               _UpperCAmelCase     :  int							      =    os.path.join(
                   A						,					"cached_{}_{}_{}_{}".format(
                       "dev" if evaluate else "train"						,					tokenizer.__class__.__name__						,					str(A       )						,					A						,					)						,					)
               _UpperCAmelCase     :  List[Any]							      =    processor.get_labels()
               if tokenizer.__class__ in (
                   RobertaTokenizer,
                   RobertaTokenizerFast,
                   XLMRobertaTokenizer,
                   BartTokenizer,
                   BartTokenizerFast,
               ):
                      # HACK(label indices are swapped in RoBERTa pretrained model)
                      _UpperCAmelCase      ,  _UpperCAmelCase     :  Tuple							      =    label_list[2], label_list[1]
               _UpperCAmelCase     :  Tuple							      =    label_list

               # Make sure only the first process in distributed training processes the dataset,
               # and the others will use the cache.
               _UpperCAmelCase     :  List[str]							      =    cached_features_file + ".lock"
               with FileLock(A       ):
                      if os.path.exists(A       ) and not overwrite_cache:
                             logger.info(F"""Loading features from cached file {cached_features_file}"""       )
                             _UpperCAmelCase     :  str							      =    torch.load(A       )
                      else:
                             logger.info(F"""Creating features from dataset file at {data_dir}"""       )

                             _UpperCAmelCase     :  Optional[int]							      =    (
                                 processor.get_dev_examples(A       ) if evaluate else processor.get_train_examples(A       )
                             )

                             logger.info("Training examples: %s"						,					len(A       )       )
                             _UpperCAmelCase     :  Optional[Any]							      =    hans_convert_examples_to_features(A						,					A						,					A						,					A       )
                             logger.info("Saving features into cached file %s"						,					A       )
                             torch.save(self.features						,					A       )




        def __len__(       self						:				Tuple       ):
               return len(self.features       )




        def __getitem__(       self						:				Optional[int]						,					A						:				str       ):
               return self.features[i]





        def      _A	(       self						:				Optional[int]       ):
               return self.label_list


if is_tf_available():
  import tensorflow as tf

  class 	lowerCamelCase_	:
        '''simple docstring'''

        __UpperCamelCase:  List[InputFeatures]




        def __init__(       self						:				Any						,					A						:				str						,					A						:				PreTrainedTokenizer						,					A						:				str						,					A						:				Optional[int] = 128						,					A						:				str=False						,					A						:				bool = False						,					):
               _UpperCAmelCase     :  List[str]							      =    hans_processors[task]()
               _UpperCAmelCase     :  Optional[Any]							      =    processor.get_labels()
               if tokenizer.__class__ in (
                   RobertaTokenizer,
                   RobertaTokenizerFast,
                   XLMRobertaTokenizer,
                   BartTokenizer,
                   BartTokenizerFast,
               ):
                      # HACK(label indices are swapped in RoBERTa pretrained model)
                      _UpperCAmelCase      ,  _UpperCAmelCase     :  int							      =    label_list[2], label_list[1]
               _UpperCAmelCase     :  Optional[Any]							      =    label_list

               _UpperCAmelCase     :  Tuple							      =    processor.get_dev_examples(A       ) if evaluate else processor.get_train_examples(A       )
               _UpperCAmelCase     :  List[str]							      =    hans_convert_examples_to_features(A						,					A						,					A						,					A       )

               def gen():
                      for ex_index, ex in tqdm.tqdm(enumerate(self.features       )						,					desc="convert examples to features"       ):
                             if ex_index % 10000 == 0:
                                    logger.info("Writing example %d of %d" % (ex_index, len(A       ))       )

                             yield (
                                 {
                                     "example_id": 0,
                                     "input_ids": ex.input_ids,
                                     "attention_mask": ex.attention_mask,
                                     "token_type_ids": ex.token_type_ids,
                                 },
                                 ex.label,
                             )

               _UpperCAmelCase     :  List[str]							      =    tf.data.Dataset.from_generator(
                   A						,					(
                       {
                           "example_id": tf.intaa,
                           "input_ids": tf.intaa,
                           "attention_mask": tf.intaa,
                           "token_type_ids": tf.intaa,
                       },
                       tf.intaa,
                   )						,					(
                       {
                           "example_id": tf.TensorShape([]       ),
                           "input_ids": tf.TensorShape([None, None]       ),
                           "attention_mask": tf.TensorShape([None, None]       ),
                           "token_type_ids": tf.TensorShape([None, None]       ),
                       },
                       tf.TensorShape([]       ),
                   )						,					)




        def      _A	(       self						:				Optional[int]       ):
               return self.dataset




        def __len__(       self						:				Optional[Any]       ):
               return len(self.features       )




        def __getitem__(       self						:				int						,					A						:				List[Any]       ):
               return self.features[i]





        def      _A	(       self						:				Optional[int]       ):
               return self.label_list

class 	lowerCamelCase_	(snake_case__    ):
      '''simple docstring'''

      def      _A	(       self						:				Optional[Any]						,					A						:				Union[str, Any]       ):
             return self._create_examples(self._read_tsv(os.path.join(A						,					"heuristics_train_set.txt"       )       )						,					"train"       )




      def      _A	(       self						:				Optional[int]						,					A						:				List[str]       ):
             return self._create_examples(self._read_tsv(os.path.join(A						,					"heuristics_evaluation_set.txt"       )       )						,					"dev"       )




      def      _A	(       self						:				Optional[int]       ):
             return ["contradiction", "entailment", "neutral"]





      def      _A	(       self						:				Tuple						,					A						:				Optional[int]						,					A						:				Tuple       ):
             _UpperCAmelCase     :  List[Any]							      =    []
             for i, line in enumerate(A       ):
                    if i == 0:
                           continue
                    _UpperCAmelCase     :  int							      =    "%s-%s" % (set_type, line[0])
                    _UpperCAmelCase     :  int							      =    line[5]
                    _UpperCAmelCase     :  Tuple							      =    line[6]
                    _UpperCAmelCase     :  Optional[Any]							      =    line[7][2:] if line[7].startswith("ex"       ) else line[7]
                    _UpperCAmelCase     :  int							      =    line[0]
                    examples.append(InputExample(guid=A						,					text_a=A						,					text_b=A						,					label=A						,					pairID=A       )       )
             return examples

def 	UpperCamelCase_							(					_UpperCAmelCase  :      List[InputExample]			,	_UpperCAmelCase  :      List[str]			,	_UpperCAmelCase  :      int			,	_UpperCAmelCase  :      PreTrainedTokenizer			,	)    ->    str:


       """simple docstring"""







       _UpperCAmelCase     :  Optional[Any]							      =    {label: i for i, label in enumerate(_UpperCAmelCase    )}

       _UpperCAmelCase     :  str							      =    []
       for ex_index, example in tqdm.tqdm(enumerate(_UpperCAmelCase    )			,	desc="convert examples to features"    ):
              if ex_index % 10_000 == 0:
                     logger.info("Writing example %d" % (ex_index)    )

              _UpperCAmelCase     :  str							      =    tokenizer(
                  example.text_a			,	example.text_b			,	add_special_tokens=_UpperCAmelCase			,	max_length=_UpperCAmelCase			,	padding="max_length"			,	truncation=_UpperCAmelCase			,	return_overflowing_tokens=_UpperCAmelCase			,	)

              _UpperCAmelCase     :  Dict							      =    label_map[example.label] if example.label in label_map else 0

              _UpperCAmelCase     :  Dict							      =    int(example.pairID    )

              features.append(InputFeatures(**_UpperCAmelCase			,	label=_UpperCAmelCase			,	pairID=_UpperCAmelCase    )    )

       for i, example in enumerate(examples[:5]    ):
              logger.info("*** Example ***"    )
              logger.info(F"""guid: {example}"""    )
              logger.info(F"""features: {features[i]}"""    )

       return features


__SCREAMING_SNAKE_CASE       :     Optional[Any]     =  {
    """hans""": 3,
}


__SCREAMING_SNAKE_CASE       :     Optional[Any]     =  {
    """hans""": HansProcessor,
}


'''simple docstring'''




import math
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils import SchedulerMixin, SchedulerOutput

class 	lowerCamelCase_	(snake_case__							, snake_case__    ):
      '''simple docstring'''

      __UpperCamelCase:  Optional[Any]						=						1




      @register_to_config
      def __init__(       self						:				Optional[int]						,					A						:				int = 1000						,					A						:				Optional[Union[np.ndarray, List[float]]] = None       ):
             # set `betas`, `alphas`, `timesteps`
             self.set_timesteps(A       )

             # standard deviation of the initial noise distribution
             _UpperCAmelCase     :  int							      =    1.0

             # For now we only support F-PNDM, i.e. the runge-kutta method
             # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf
             # mainly at formula (9), (12), (13) and the Algorithm 2.
             _UpperCAmelCase     :  int							      =    4

             # running values
             _UpperCAmelCase     :  Dict							      =    []




      def      _A	(       self						:				Optional[int]						,					A						:				int						,					A						:				Union[str, torch.device] = None       ):
             _UpperCAmelCase     :  int							      =    num_inference_steps
             _UpperCAmelCase     :  Union[str, Any]							      =    torch.linspace(1						,					0						,					num_inference_steps + 1       )[:-1]
             _UpperCAmelCase     :  Any							      =    torch.cat([steps, torch.tensor([0.0]       )]       )

             if self.config.trained_betas is not None:
                    _UpperCAmelCase     :  str							      =    torch.tensor(self.config.trained_betas						,					dtype=torch.floataa       )
             else:
                    _UpperCAmelCase     :  Dict							      =    torch.sin(steps * math.pi / 2       ) ** 2

             _UpperCAmelCase     :  List[Any]							      =    (1.0 - self.betas**2) ** 0.5

             _UpperCAmelCase     :  List[str]							      =    (torch.atana(self.betas						,					self.alphas       ) / math.pi * 2)[:-1]
             _UpperCAmelCase     :  Dict							      =    timesteps.to(A       )

             _UpperCAmelCase     :  Dict							      =    []




      def      _A	(       self						:				Optional[int]						,					A						:				torch.FloatTensor						,					A						:				int						,					A						:				torch.FloatTensor						,					A						:				bool = True						,					):
             if self.num_inference_steps is None:
                    raise ValueError(
                        "Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler"       )

             _UpperCAmelCase     :  Tuple							      =    (self.timesteps == timestep).nonzero().item()
             _UpperCAmelCase     :  Optional[Any]							      =    timestep_index + 1

             _UpperCAmelCase     :  int							      =    sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index]
             self.ets.append(A       )

             if len(self.ets       ) == 1:
                    _UpperCAmelCase     :  List[Any]							      =    self.ets[-1]
             elif len(self.ets       ) == 2:
                    _UpperCAmelCase     :  str							      =    (3 * self.ets[-1] - self.ets[-2]) / 2
             elif len(self.ets       ) == 3:
                    _UpperCAmelCase     :  Tuple							      =    (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12
             else:
                    _UpperCAmelCase     :  Union[str, Any]							      =    (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4])

             _UpperCAmelCase     :  Union[str, Any]							      =    self._get_prev_sample(A						,					A						,					A						,					A       )

             if not return_dict:
                    return (prev_sample,)

             return SchedulerOutput(prev_sample=A       )




      def      _A	(       self						:				Union[str, Any]						,					A						:				torch.FloatTensor						,					*A						:				Union[str, Any]						,					**A						:				Dict       ):
             return sample




      def      _A	(       self						:				Optional[Any]						,					A						:				Optional[int]						,					A						:				int						,					A						:				Optional[Any]						,					A						:				List[str]       ):
             _UpperCAmelCase     :  List[str]							      =    self.alphas[timestep_index]
             _UpperCAmelCase     :  List[Any]							      =    self.betas[timestep_index]

             _UpperCAmelCase     :  Optional[Any]							      =    self.alphas[prev_timestep_index]
             _UpperCAmelCase     :  Dict							      =    self.betas[prev_timestep_index]

             _UpperCAmelCase     :  Tuple							      =    (sample - sigma * ets) / max(A						,					1E-8       )
             _UpperCAmelCase     :  List[str]							      =    next_alpha * pred + ets * next_sigma

             return prev_sample





      def __len__(       self						:				Union[str, Any]       ):
             return self.config.num_train_timesteps

