


import math
from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, randn_tensor
from .scheduling_utils import SchedulerMixin, SchedulerOutput
@dataclass
class 					__magic_name__		(			__UpperCAmelCase     ):
       __A			:      torch.FloatTensor
       __A			:      torch.FloatTensor







class 					__magic_name__		(			__UpperCAmelCase			,						__UpperCAmelCase     ):
       __A			:      Any        =   1






       @register_to_config
       def __init__(   self  :    int  ,     snake_case__  :    int = 2_0_0_0  ,     snake_case__  :    float = 0.15  ,     snake_case__  :    float = 0.01  ,     snake_case__  :    float = 1_3_4_8.0  ,     snake_case__  :    float = 1e-5  ,     snake_case__  :    int = 1  ,     ):

              '''simple docstring'''






              lowercase		:List[Any]       							=				sigma_max

              # setable values
              lowercase		:List[str]       							=				None

              self.set_sigmas(snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				)






       def  __snake_case						(   self  :    Optional[Any]  ,     snake_case__  :    torch.FloatTensor  ,     snake_case__  :    Optional[int] = None				):

              '''simple docstring'''






              return sample






       def  __snake_case						(   self  :    Optional[int]  ,     snake_case__  :    int  ,     snake_case__  :    float = None  ,     snake_case__  :    Union[str, torch.device] = None				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				sampling_eps if sampling_eps is not None else self.config.sampling_eps

              lowercase		:Tuple       							=				torch.linspace(1  ,     snake_case__  ,     snake_case__  ,     device=snake_case__				)






       def  __snake_case						(   self  :    List[str]  ,     snake_case__  :    int  ,     snake_case__  :    float = None  ,     snake_case__  :    float = None  ,     snake_case__  :    float = None				):

              '''simple docstring'''






              lowercase		:int       							=				sigma_min if sigma_min is not None else self.config.sigma_min
              lowercase		:Any       							=				sigma_max if sigma_max is not None else self.config.sigma_max
              lowercase		:Tuple       							=				sampling_eps if sampling_eps is not None else self.config.sampling_eps
              if self.timesteps is None:
                     self.set_timesteps(snake_case__  ,     snake_case__				)

              lowercase		:Union[str, Any]       							=				sigma_min * (sigma_max / sigma_min) ** (self.timesteps / sampling_eps)
              lowercase		:str       							=				torch.exp(torch.linspace(math.log(snake_case__				)  ,     math.log(snake_case__				)  ,     snake_case__				)				)
              lowercase		:List[str]       							=				torch.tensor([sigma_min * (sigma_max / sigma_min) ** t for t in self.timesteps]				)






       def  __snake_case						(   self  :    Optional[Any]  ,     snake_case__  :    Tuple  ,     snake_case__  :    int				):

              '''simple docstring'''






              return torch.where(
                  timesteps == 0  ,     torch.zeros_like(t.to(timesteps.device				)				)  ,     self.discrete_sigmas[timesteps - 1].to(timesteps.device				)  ,     )






       def  __snake_case						(   self  :    List[Any]  ,     snake_case__  :    torch.FloatTensor  ,     snake_case__  :    int  ,     snake_case__  :    torch.FloatTensor  ,     snake_case__  :    Optional[torch.Generator] = None  ,     snake_case__  :    bool = True  ,     ):

              '''simple docstring'''






              if self.timesteps is None:
                     raise ValueError(
                         '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler'''				)

              lowercase		:Optional[int]       							=				timestep * torch.ones(
                  sample.shape[0]  ,     device=sample.device				)  # torch.repeat_interleave(timestep, sample.shape[0])
              lowercase		:Optional[int]       							=				(timestep * (len(self.timesteps				) - 1)).long()

              # mps requires indices to be in the same device, so we use cpu as is the default with cuda
              lowercase		:Union[str, Any]       							=				timesteps.to(self.discrete_sigmas.device				)

              lowercase		:List[Any]       							=				self.discrete_sigmas[timesteps].to(sample.device				)
              lowercase		:str       							=				self.get_adjacent_sigma(snake_case__  ,     snake_case__				).to(sample.device				)
              lowercase		:Optional[Any]       							=				torch.zeros_like(snake_case__				)
              lowercase		:List[Any]       							=				(sigma**2 - adjacent_sigma**2) ** 0.5

              # equation 6 in the paper: the model_output modeled by the network is grad_x log pt(x)
              # also equation 47 shows the analog from SDE models to ancestral sampling methods
              lowercase		:Union[str, Any]       							=				diffusion.flatten()
              while len(diffusion.shape				) < len(sample.shape				):
                     lowercase		:Dict       							=				diffusion.unsqueeze(-1				)
              lowercase		:List[Any]       							=				drift - diffusion**2 * model_output

              #  equation 6: sample noise for the diffusion term of
              lowercase		:List[Any]       							=				randn_tensor(
                  sample.shape  ,     layout=sample.layout  ,     generator=snake_case__  ,     device=sample.device  ,     dtype=sample.dtype				)
              lowercase		:int       							=				sample - drift  # subtract because `dt` is a small negative timestep
              # TODO is the variable diffusion the correct scaling term for the noise?
              lowercase		:Any       							=				prev_sample_mean + diffusion * noise  # add impact of diffusion field g

              if not return_dict:
                     return (prev_sample, prev_sample_mean)

              return SdeVeOutput(prev_sample=snake_case__  ,     prev_sample_mean=snake_case__				)






       def  __snake_case						(   self  :    Union[str, Any]  ,     snake_case__  :    torch.FloatTensor  ,     snake_case__  :    torch.FloatTensor  ,     snake_case__  :    Optional[torch.Generator] = None  ,     snake_case__  :    bool = True  ,     ):

              '''simple docstring'''






              if self.timesteps is None:
                     raise ValueError(
                         '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler'''				)

              # For small batch sizes, the paper "suggest replacing norm(z) with sqrt(d), where d is the dim. of z"
              # sample noise for correction
              lowercase		:Tuple       							=				randn_tensor(sample.shape  ,     layout=sample.layout  ,     generator=snake_case__				).to(sample.device				)

              # compute step size from the model_output, the noise, and the snr
              lowercase		:List[Any]       							=				torch.norm(model_output.reshape(model_output.shape[0]  ,     -1				)  ,     dim=-1				).mean()
              lowercase		:List[str]       							=				torch.norm(noise.reshape(noise.shape[0]  ,     -1				)  ,     dim=-1				).mean()
              lowercase		:int       							=				(self.config.snr * noise_norm / grad_norm) ** 2 * 2
              lowercase		:Optional[Any]       							=				step_size * torch.ones(sample.shape[0]				).to(sample.device				)
              # self.repeat_scalar(step_size, sample.shape[0])

              # compute corrected sample: model_output term and noise term
              lowercase		:Union[str, Any]       							=				step_size.flatten()
              while len(step_size.shape				) < len(sample.shape				):
                     lowercase		:Union[str, Any]       							=				step_size.unsqueeze(-1				)
              lowercase		:Union[str, Any]       							=				sample + step_size * model_output
              lowercase		:Any       							=				prev_sample_mean + ((step_size * 2) ** 0.5) * noise

              if not return_dict:
                     return (prev_sample,)

              return SchedulerOutput(prev_sample=snake_case__				)






       def  __snake_case						(   self  :    List[str]  ,     snake_case__  :    torch.FloatTensor  ,     snake_case__  :    torch.FloatTensor  ,     snake_case__  :    torch.FloatTensor  ,     ):

              '''simple docstring'''






              lowercase		:List[Any]       							=				timesteps.to(original_samples.device				)
              lowercase		:str       							=				self.discrete_sigmas.to(original_samples.device				)[timesteps]
              lowercase		:Optional[int]       							=				(
                  noise * sigmas[:, None, None, None]
                  if noise is not None
                  else torch.randn_like(snake_case__				) * sigmas[:, None, None, None]
              )
              lowercase		:str       							=				noise + original_samples
              return noisy_samples







       def __len__(   self  :    Optional[Any]				):

              '''simple docstring'''






              return self.config.num_train_timesteps





"""simple docstring"""



import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
UpperCAmelCase      		=			{'''LayoutLMv2Config''', '''LayoutLMv3Config'''}
@is_pipeline_test
class 					__magic_name__		(			unittest.TestCase     ):
       __A			:      int        =   MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
       __A			:      List[str]        =   TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

       if model_mapping is not None:
              __A			:      int        =   {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
       if tf_model_mapping is not None:
              __A			:      Optional[int]        =   {
                  config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
              }






       def  __snake_case						(   self  :    Tuple  ,     snake_case__  :    Any  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    int				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				ZeroShotClassificationPipeline(
                  model=snake_case__  ,     tokenizer=snake_case__  ,     candidate_labels=['''polics''', '''health''']				)
              return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]






       def  __snake_case						(   self  :    Any  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    Tuple				):

              '''simple docstring'''






              lowercase		:int       							=				classifier('''Who are you voting for in 2020?'''  ,     candidate_labels='''politics'''				)
              self.assertEqual(snake_case__  ,     {'''sequence''': ANY(snake_case__				), '''labels''': [ANY(snake_case__				)], '''scores''': [ANY(snake_case__				)]}				)

              # No kwarg
              lowercase		:Tuple       							=				classifier('''Who are you voting for in 2020?'''  ,     ['''politics''']				)
              self.assertEqual(snake_case__  ,     {'''sequence''': ANY(snake_case__				), '''labels''': [ANY(snake_case__				)], '''scores''': [ANY(snake_case__				)]}				)

              lowercase		:Tuple       							=				classifier('''Who are you voting for in 2020?'''  ,     candidate_labels=['''politics''']				)
              self.assertEqual(snake_case__  ,     {'''sequence''': ANY(snake_case__				), '''labels''': [ANY(snake_case__				)], '''scores''': [ANY(snake_case__				)]}				)

              lowercase		:Union[str, Any]       							=				classifier('''Who are you voting for in 2020?'''  ,     candidate_labels='''politics, public health'''				)
              self.assertEqual(
                  snake_case__  ,     {'''sequence''': ANY(snake_case__				), '''labels''': [ANY(snake_case__				), ANY(snake_case__				)], '''scores''': [ANY(snake_case__				), ANY(snake_case__				)]}				)
              self.assertAlmostEqual(sum(nested_simplify(outputs['''scores''']				)				)  ,     1.0				)

              lowercase		:Optional[Any]       							=				classifier('''Who are you voting for in 2020?'''  ,     candidate_labels=['''politics''', '''public health''']				)
              self.assertEqual(
                  snake_case__  ,     {'''sequence''': ANY(snake_case__				), '''labels''': [ANY(snake_case__				), ANY(snake_case__				)], '''scores''': [ANY(snake_case__				), ANY(snake_case__				)]}				)
              self.assertAlmostEqual(sum(nested_simplify(outputs['''scores''']				)				)  ,     1.0				)

              lowercase		:Optional[Any]       							=				classifier(
                  '''Who are you voting for in 2020?'''  ,     candidate_labels='''politics'''  ,     hypothesis_template='''This text is about {}'''				)
              self.assertEqual(snake_case__  ,     {'''sequence''': ANY(snake_case__				), '''labels''': [ANY(snake_case__				)], '''scores''': [ANY(snake_case__				)]}				)

              # https://github.com/huggingface/transformers/issues/13846
              lowercase		:Optional[Any]       							=				classifier(['''I am happy''']  ,     ['''positive''', '''negative''']				)
              self.assertEqual(
                  snake_case__  ,     [
                      {'''sequence''': ANY(snake_case__				), '''labels''': [ANY(snake_case__				), ANY(snake_case__				)], '''scores''': [ANY(snake_case__				), ANY(snake_case__				)]}
                      for i in range(1				)
                  ]  ,     )
              lowercase		:Tuple       							=				classifier(['''I am happy''', '''I am sad''']  ,     ['''positive''', '''negative''']				)
              self.assertEqual(
                  snake_case__  ,     [
                      {'''sequence''': ANY(snake_case__				), '''labels''': [ANY(snake_case__				), ANY(snake_case__				)], '''scores''': [ANY(snake_case__				), ANY(snake_case__				)]}
                      for i in range(2				)
                  ]  ,     )

              with self.assertRaises(snake_case__				):
                     classifier(''''''  ,     candidate_labels='''politics'''				)

              with self.assertRaises(snake_case__				):
                     classifier(snake_case__  ,     candidate_labels='''politics'''				)

              with self.assertRaises(snake_case__				):
                     classifier('''Who are you voting for in 2020?'''  ,     candidate_labels=''''''				)

              with self.assertRaises(snake_case__				):
                     classifier('''Who are you voting for in 2020?'''  ,     candidate_labels=snake_case__				)

              with self.assertRaises(snake_case__				):
                     classifier(
                         '''Who are you voting for in 2020?'''  ,     candidate_labels='''politics'''  ,     hypothesis_template='''Not formatting template'''  ,     )

              with self.assertRaises(snake_case__				):
                     classifier(
                         '''Who are you voting for in 2020?'''  ,     candidate_labels='''politics'''  ,     hypothesis_template=snake_case__  ,     )

              self.run_entailment_id(snake_case__				)






       def  __snake_case						(   self  :    Any  ,     snake_case__  :    Pipeline				):

              '''simple docstring'''






              lowercase		:List[Any]       							=				zero_shot_classifier.model.config
              lowercase		:int       							=				config.labelaid
              lowercase		:str       							=				zero_shot_classifier.entailment_id

              lowercase		:Dict       							=				{'''LABEL_0''': 0, '''LABEL_1''': 1, '''LABEL_2''': 2}
              self.assertEqual(zero_shot_classifier.entailment_id  ,     -1				)

              lowercase		:Optional[Any]       							=				{'''entailment''': 0, '''neutral''': 1, '''contradiction''': 2}
              self.assertEqual(zero_shot_classifier.entailment_id  ,     0				)

              lowercase		:Tuple       							=				{'''ENTAIL''': 0, '''NON-ENTAIL''': 1}
              self.assertEqual(zero_shot_classifier.entailment_id  ,     0				)

              lowercase		:str       							=				{'''ENTAIL''': 2, '''NEUTRAL''': 1, '''CONTR''': 0}
              self.assertEqual(zero_shot_classifier.entailment_id  ,     2				)

              lowercase		:Optional[Any]       							=				original_labelaid
              self.assertEqual(snake_case__  ,     zero_shot_classifier.entailment_id				)






       @require_torch
       def  __snake_case						(   self  :    List[str]				):

              '''simple docstring'''






              lowercase		:List[Any]       							=				pipeline(
                  '''zero-shot-classification'''  ,     model='''sshleifer/tiny-distilbert-base-cased-distilled-squad'''  ,     framework='''pt'''  ,     )
              # There was a regression in 4.10 for this
              # Adding a test so we don't make the mistake again.
              # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
              zero_shot_classifier(
                  '''Who are you voting for in 2020?''' * 1_0_0  ,     candidate_labels=['''politics''', '''public health''', '''science''']				)






       @require_torch
       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              lowercase		:str       							=				pipeline(
                  '''zero-shot-classification'''  ,     model='''sshleifer/tiny-distilbert-base-cased-distilled-squad'''  ,     framework='''pt'''  ,     )
              lowercase		:Any       							=				zero_shot_classifier(
                  '''Who are you voting for in 2020?'''  ,     candidate_labels=['''politics''', '''public health''', '''science''']				)

              self.assertEqual(
                  nested_simplify(snake_case__				)  ,     {
                      '''sequence''': '''Who are you voting for in 2020?''',
                      '''labels''': ['''science''', '''public health''', '''politics'''],
                      '''scores''': [0.3_33, 0.3_33, 0.3_33],
                  }  ,     )






       @require_tf
       def  __snake_case						(   self  :    List[str]				):

              '''simple docstring'''






              lowercase		:Tuple       							=				pipeline(
                  '''zero-shot-classification'''  ,     model='''sshleifer/tiny-distilbert-base-cased-distilled-squad'''  ,     framework='''tf'''  ,     )
              lowercase		:List[Any]       							=				zero_shot_classifier(
                  '''Who are you voting for in 2020?'''  ,     candidate_labels=['''politics''', '''public health''', '''science''']				)

              self.assertEqual(
                  nested_simplify(snake_case__				)  ,     {
                      '''sequence''': '''Who are you voting for in 2020?''',
                      '''labels''': ['''science''', '''public health''', '''politics'''],
                      '''scores''': [0.3_33, 0.3_33, 0.3_33],
                  }  ,     )






       @slow
       @require_torch
       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              lowercase		:Dict       							=				pipeline('''zero-shot-classification'''  ,     model='''roberta-large-mnli'''  ,     framework='''pt'''				)
              lowercase		:Union[str, Any]       							=				zero_shot_classifier(
                  '''Who are you voting for in 2020?'''  ,     candidate_labels=['''politics''', '''public health''', '''science''']				)

              self.assertEqual(
                  nested_simplify(snake_case__				)  ,     {
                      '''sequence''': '''Who are you voting for in 2020?''',
                      '''labels''': ['''politics''', '''public health''', '''science'''],
                      '''scores''': [0.9_76, 0.0_15, 0.0_09],
                  }  ,     )
              lowercase		:Optional[Any]       							=				zero_shot_classifier(
                  '''The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'''
                  ''' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'''
                  ''' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'''
                  ''' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'''
                  ''' machine translation tasks show these models to be superior in quality while being more parallelizable'''
                  ''' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'''
                  ''' English-to-German translation task, improving over the existing best results, including ensembles by'''
                  ''' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'''
                  ''' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'''
                  ''' fraction of the training costs of the best models from the literature. We show that the Transformer'''
                  ''' generalizes well to other tasks by applying it successfully to English constituency parsing both with'''
                  ''' large and limited training data.'''  ,     candidate_labels=['''machine learning''', '''statistics''', '''translation''', '''vision''']  ,     multi_label=snake_case__  ,     )
              self.assertEqual(
                  nested_simplify(snake_case__				)  ,     {
                      '''sequence''': (
                          '''The dominant sequence transduction models are based on complex recurrent or convolutional neural'''
                          ''' networks in an encoder-decoder configuration. The best performing models also connect the'''
                          ''' encoder and decoder through an attention mechanism. We propose a new simple network'''
                          ''' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'''
                          ''' and convolutions entirely. Experiments on two machine translation tasks show these models to be'''
                          ''' superior in quality while being more parallelizable and requiring significantly less time to'''
                          ''' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'''
                          ''' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'''
                          ''' English-to-French translation task, our model establishes a new single-model state-of-the-art'''
                          ''' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'''
                          ''' costs of the best models from the literature. We show that the Transformer generalizes well to'''
                          ''' other tasks by applying it successfully to English constituency parsing both with large and'''
                          ''' limited training data.'''
                      ),
                      '''labels''': ['''translation''', '''machine learning''', '''vision''', '''statistics'''],
                      '''scores''': [0.8_17, 0.7_13, 0.0_18, 0.0_18],
                  }  ,     )







       @slow
       @require_tf
       def  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              lowercase		:str       							=				pipeline('''zero-shot-classification'''  ,     model='''roberta-large-mnli'''  ,     framework='''tf'''				)
              lowercase		:Optional[int]       							=				zero_shot_classifier(
                  '''Who are you voting for in 2020?'''  ,     candidate_labels=['''politics''', '''public health''', '''science''']				)

              self.assertEqual(
                  nested_simplify(snake_case__				)  ,     {
                      '''sequence''': '''Who are you voting for in 2020?''',
                      '''labels''': ['''politics''', '''public health''', '''science'''],
                      '''scores''': [0.9_76, 0.0_15, 0.0_09],
                  }  ,     )
              lowercase		:str       							=				zero_shot_classifier(
                  '''The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'''
                  ''' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'''
                  ''' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'''
                  ''' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'''
                  ''' machine translation tasks show these models to be superior in quality while being more parallelizable'''
                  ''' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'''
                  ''' English-to-German translation task, improving over the existing best results, including ensembles by'''
                  ''' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'''
                  ''' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'''
                  ''' fraction of the training costs of the best models from the literature. We show that the Transformer'''
                  ''' generalizes well to other tasks by applying it successfully to English constituency parsing both with'''
                  ''' large and limited training data.'''  ,     candidate_labels=['''machine learning''', '''statistics''', '''translation''', '''vision''']  ,     multi_label=snake_case__  ,     )
              self.assertEqual(
                  nested_simplify(snake_case__				)  ,     {
                      '''sequence''': (
                          '''The dominant sequence transduction models are based on complex recurrent or convolutional neural'''
                          ''' networks in an encoder-decoder configuration. The best performing models also connect the'''
                          ''' encoder and decoder through an attention mechanism. We propose a new simple network'''
                          ''' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'''
                          ''' and convolutions entirely. Experiments on two machine translation tasks show these models to be'''
                          ''' superior in quality while being more parallelizable and requiring significantly less time to'''
                          ''' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'''
                          ''' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'''
                          ''' English-to-French translation task, our model establishes a new single-model state-of-the-art'''
                          ''' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'''
                          ''' costs of the best models from the literature. We show that the Transformer generalizes well to'''
                          ''' other tasks by applying it successfully to English constituency parsing both with large and'''
                          ''' limited training data.'''
                      ),
                      '''labels''': ['''translation''', '''machine learning''', '''vision''', '''statistics'''],
                      '''scores''': [0.8_17, 0.7_13, 0.0_18, 0.0_18],
                  }  ,     )

