





import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VideoMAEConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch
       from torch import nn

       from transformers import (
           MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
           VideoMAEForPreTraining,
           VideoMAEForVideoClassification,
           VideoMAEModel,
       )
       from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from transformers import VideoMAEImageProcessor


class        __SCREAMING_SNAKE_CASE      :







       def __init__(				self					:     Any		, A					:     Union[str, Any]		, A					:     Optional[int]=1_3		, A					:     Optional[int]=1_0		, A					:     List[str]=3		, A					:     Dict=2		, A					:     Any=2		, A					:     Dict=2		, A					:     List[Any]=True		, A					:     int=True		, A					:     int=3_2		, A					:     Optional[int]=5		, A					:     Tuple=4		, A					:     Any=3_7		, A					:     Tuple="gelu"		, A					:     Dict=0.1		, A					:     List[Any]=0.1		, A					:     int=1_0		, A					:     Union[str, Any]=0.02		, A					:     Union[str, Any]=0.9		, A					:     Tuple=None		, )       ->Any:
          lowerCamelCase__       :						Tuple        =							parent
          lowerCamelCase__       :						List[Any]        =							batch_size
          lowerCamelCase__       :						Optional[Any]        =							image_size
          lowerCamelCase__       :						str        =							num_channels
          lowerCamelCase__       :						Union[str, Any]        =							patch_size
          lowerCamelCase__       :						str        =							tubelet_size
          lowerCamelCase__       :						Any        =							num_frames
          lowerCamelCase__       :						List[Any]        =							is_training
          lowerCamelCase__       :						Any        =							use_labels
          lowerCamelCase__       :						List[Any]        =							hidden_size
          lowerCamelCase__       :						List[str]        =							num_hidden_layers
          lowerCamelCase__       :						Optional[Any]        =							num_attention_heads
          lowerCamelCase__       :						Optional[Any]        =							intermediate_size
          lowerCamelCase__       :						Optional[int]        =							hidden_act
          lowerCamelCase__       :						int        =							hidden_dropout_prob
          lowerCamelCase__       :						str        =							attention_probs_dropout_prob
          lowerCamelCase__       :						List[str]        =							type_sequence_label_size
          lowerCamelCase__       :						Dict        =							initializer_range
          lowerCamelCase__       :						Union[str, Any]        =							mask_ratio
          lowerCamelCase__       :						str        =							scope

          # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
          lowerCamelCase__       :						Optional[int]        =							(image_size // patch_size) ** 2
          lowerCamelCase__       :						Optional[int]        =							(num_frames // tubelet_size) * self.num_patches_per_frame

          # use this variable to define bool_masked_pos
          lowerCamelCase__       :						int        =							int(mask_ratio * self.seq_length      )







       def 						__lowerCamelCase   (				self					:     Optional[int]      )       ->List[Any]:
          lowerCamelCase__       :						Tuple        =							floats_tensor(
              [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]      )

          lowerCamelCase__       :						List[str]        =							None
          if self.use_labels:
             lowerCamelCase__       :						Dict        =							ids_tensor([self.batch_size]		, self.type_sequence_label_size      )

          lowerCamelCase__       :						List[str]        =							self.get_config()

          return config, pixel_values, labels







       def 						__lowerCamelCase   (				self					:     int      )       ->Any:
          return VideoMAEConfig(
              image_size=self.image_size		, patch_size=self.patch_size		, num_channels=self.num_channels		, num_frames=self.num_frames		, tubelet_size=self.tubelet_size		, hidden_size=self.hidden_size		, num_hidden_layers=self.num_hidden_layers		, num_attention_heads=self.num_attention_heads		, intermediate_size=self.intermediate_size		, hidden_act=self.hidden_act		, hidden_dropout_prob=self.hidden_dropout_prob		, attention_probs_dropout_prob=self.attention_probs_dropout_prob		, is_decoder=A		, initializer_range=self.initializer_range		, )







       def 						__lowerCamelCase   (				self					:     Dict		, A					:     Dict		, A					:     int		, A					:     List[str]      )       ->List[str]:
          lowerCamelCase__       :						Dict        =							VideoMAEModel(config=A      )
          model.to(A      )
          model.eval()
          lowerCamelCase__       :						Dict        =							model(A      )
          self.parent.assertEqual(result.last_hidden_state.shape		, (self.batch_size, self.seq_length, self.hidden_size)      )







       def 						__lowerCamelCase   (				self					:     Any		, A					:     Dict		, A					:     Tuple		, A					:     Any      )       ->List[Any]:
          lowerCamelCase__       :						Tuple        =							VideoMAEForPreTraining(A      )
          model.to(A      )
          model.eval()
          # important: each video needs to have the same number of masked patches
          # hence we define a single mask, which we then repeat for each example in the batch
          lowerCamelCase__       :						Any        =							torch.ones((self.num_masks,)      )
          lowerCamelCase__       :						List[str]        =							torch.cat([mask, torch.zeros(self.seq_length - mask.size(0      )      )]      )
          lowerCamelCase__       :						Dict        =							mask.expand(self.batch_size		, -1      ).bool()

          lowerCamelCase__       :						Any        =							model(A		, A      )
          # model only returns predictions for masked patches
          lowerCamelCase__       :						Any        =							mask.sum().item()
          lowerCamelCase__       :						List[Any]        =							3 * self.tubelet_size * self.patch_size**2
          self.parent.assertEqual(result.logits.shape		, (self.batch_size, num_masked_patches, decoder_num_labels)      )







       def 						__lowerCamelCase   (				self					:     List[str]      )       ->Any:
          lowerCamelCase__       :						List[str]        =							self.prepare_config_and_inputs()
          lowerCamelCase__       ,							lowerCamelCase__       ,							lowerCamelCase__       :						Dict        =							config_and_inputs
          lowerCamelCase__       :						Any        =							{'''pixel_values''': pixel_values}
          return config, inputs_dict


@require_torch
class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_					,lowerCAmelCase_					,unittest.TestCase ):
       _UpperCAmelCase				:  Any			   =   (
           (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
       )
       _UpperCAmelCase				:  List[str]			   =   (
           {"feature-extraction": VideoMAEModel, "video-classification": VideoMAEForVideoClassification}
           if is_torch_available()
           else {}
       )

       _UpperCAmelCase				:  List[Any]			   =   False
       _UpperCAmelCase				:  Dict			   =   False
       _UpperCAmelCase				:  List[str]			   =   False
       _UpperCAmelCase				:  Union[str, Any]			   =   False







       def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->Optional[int]:
          lowerCamelCase__       :						Optional[int]        =							VideoMAEModelTester(self      )
          lowerCamelCase__       :						Union[str, Any]        =							ConfigTester(self		, config_class=A		, has_text_modality=A		, hidden_size=3_7      )







       def 						__lowerCamelCase   (				self					:     List[Any]		, A					:     int		, A					:     Union[str, Any]		, A					:     Any=False      )       ->Any:
          lowerCamelCase__       :						Optional[int]        =							copy.deepcopy(A      )

          if model_class == VideoMAEForPreTraining:
             # important: each video needs to have the same number of masked patches
             # hence we define a single mask, which we then repeat for each example in the batch
             lowerCamelCase__       :						Union[str, Any]        =							torch.ones((self.model_tester.num_masks,)      )
             lowerCamelCase__       :						List[str]        =							torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0      )      )]      )
             lowerCamelCase__       :						List[str]        =							mask.expand(self.model_tester.batch_size		, -1      ).bool()
             lowerCamelCase__       :						List[Any]        =							bool_masked_pos.to(A      )

          if return_labels:
             if model_class in [
                 *get_values(A      ),
             ]:
                lowerCamelCase__       :						Dict        =							torch.zeros(
                    self.model_tester.batch_size		, dtype=torch.long		, device=A      )

          return inputs_dict







       def 						__lowerCamelCase   (				self					:     Optional[int]      )       ->Any:
          self.config_tester.run_common_tests()







       @unittest.skip(reason='''VideoMAE does not use inputs_embeds'''      )
       def 						__lowerCamelCase   (				self					:     List[Any]      )       ->str:
          pass







       def 						__lowerCamelCase   (				self					:     Optional[int]      )       ->int:
          lowerCamelCase__       ,							lowerCamelCase__       :						str        =							self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             lowerCamelCase__       :						str        =							model_class(A      )
             self.assertIsInstance(model.get_input_embeddings()		, (nn.Module)      )
             lowerCamelCase__       :						List[Any]        =							model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(A		, nn.Linear      )      )







       def 						__lowerCamelCase   (				self					:     int      )       ->Optional[int]:
          lowerCamelCase__       ,							lowerCamelCase__       :						Union[str, Any]        =							self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             lowerCamelCase__       :						List[str]        =							model_class(A      )
             lowerCamelCase__       :						Optional[Any]        =							inspect.signature(model.forward      )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             lowerCamelCase__       :						List[str]        =							[*signature.parameters.keys()]

             lowerCamelCase__       :						int        =							['''pixel_values''']
             self.assertListEqual(arg_names[:1]		, A      )







       def 						__lowerCamelCase   (				self					:     List[Any]      )       ->Optional[Any]:
          lowerCamelCase__       :						Tuple        =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*A      )







       def 						__lowerCamelCase   (				self					:     str      )       ->Optional[int]:
          lowerCamelCase__       :						Any        =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*A      )







       @slow
       def 						__lowerCamelCase   (				self					:     str      )       ->List[str]:
          for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowerCamelCase__       :						List[Any]        =							VideoMAEModel.from_pretrained(A      )
             self.assertIsNotNone(A      )







       def 						__lowerCamelCase   (				self					:     Tuple      )       ->Tuple:
          if not self.has_attentions:
             pass

          else:
             lowerCamelCase__       ,							lowerCamelCase__       :						Dict        =							self.model_tester.prepare_config_and_inputs_for_common()
             lowerCamelCase__       :						List[Any]        =							True

             for model_class in self.all_model_classes:
                lowerCamelCase__       :						Optional[Any]        =							self.model_tester.seq_length - self.model_tester.num_masks
                lowerCamelCase__       :						Union[str, Any]        =							(
                    num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
                )

                lowerCamelCase__       :						Tuple        =							True
                lowerCamelCase__       :						str        =							False
                lowerCamelCase__       :						str        =							True
                lowerCamelCase__       :						Tuple        =							model_class(A      )
                model.to(A      )
                model.eval()
                with torch.no_grad():
                   lowerCamelCase__       :						Optional[int]        =							model(**self._prepare_for_class(A		, A      )      )
                lowerCamelCase__       :						Tuple        =							outputs.attentions
                self.assertEqual(len(A      )		, self.model_tester.num_hidden_layers      )

                # check that output_attentions also work using config
                del inputs_dict["output_attentions"]
                lowerCamelCase__       :						List[Any]        =							True
                lowerCamelCase__       :						Optional[Any]        =							model_class(A      )
                model.to(A      )
                model.eval()
                with torch.no_grad():
                   lowerCamelCase__       :						List[Any]        =							model(**self._prepare_for_class(A		, A      )      )
                lowerCamelCase__       :						Any        =							outputs.attentions
                self.assertEqual(len(A      )		, self.model_tester.num_hidden_layers      )

                self.assertListEqual(
                    list(attentions[0].shape[-3:]      )		, [self.model_tester.num_attention_heads, seq_len, seq_len]		, )
                lowerCamelCase__       :						Any        =							len(A      )

                # Check attention is always last and order is fine
                lowerCamelCase__       :						int        =							True
                lowerCamelCase__       :						Any        =							True
                lowerCamelCase__       :						str        =							model_class(A      )
                model.to(A      )
                model.eval()
                with torch.no_grad():
                   lowerCamelCase__       :						int        =							model(**self._prepare_for_class(A		, A      )      )

                self.assertEqual(out_len + 1		, len(A      )      )

                lowerCamelCase__       :						Dict        =							outputs.attentions

                self.assertEqual(len(A      )		, self.model_tester.num_hidden_layers      )
                self.assertListEqual(
                    list(self_attentions[0].shape[-3:]      )		, [self.model_tester.num_attention_heads, seq_len, seq_len]		, )







       def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->str:
          def check_hidden_states_output(A					:     Optional[Any]		, A					:     List[Any]		, A					:     Optional[Any]      ):
             lowerCamelCase__       :						Dict        =							model_class(A      )
             model.to(A      )
             model.eval()

             with torch.no_grad():
                lowerCamelCase__       :						Any        =							model(**self._prepare_for_class(A		, A      )      )

             lowerCamelCase__       :						Any        =							outputs.hidden_states
             lowerCamelCase__       :						Dict        =							self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(A      )		, A      )

             lowerCamelCase__       :						Dict        =							self.model_tester.seq_length - self.model_tester.num_masks
             lowerCamelCase__       :						Union[str, Any]        =							num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length

             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]      )		, [seq_length, self.model_tester.hidden_size]		, )

          lowerCamelCase__       ,							lowerCamelCase__       :						List[str]        =							self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             lowerCamelCase__       :						List[str]        =							True
             check_hidden_states_output(A		, A		, A      )

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             lowerCamelCase__       :						Tuple        =							True

             check_hidden_states_output(A		, A		, A      )







       @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''      )
       def 						__lowerCamelCase   (				self					:     List[Any]      )       ->Union[str, Any]:
          pass



def 	_a						(   )    ->    int:







   """simple docstring"""
   lowerCamelCase__       :						str        =							hf_hub_download(
       repo_id='''hf-internal-testing/spaghetti-video'''      ,  filename='''eating_spaghetti.npy'''      ,  repo_type='''dataset'''					)
   lowerCamelCase__       :						List[Any]        =							np.load(UpperCAmelCase					)
   return list(UpperCAmelCase					)




@require_torch
@require_vision
class        __SCREAMING_SNAKE_CASE      (			unittest.TestCase ):







       @cached_property
       def 						__lowerCamelCase   (				self					:     str      )       ->str:
          # logits were tested with a different mean and std, so we use the same here
          return (
              VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]		, image_std=[0.5, 0.5, 0.5]      )
              if is_vision_available()
              else None
          )







       @slow
       def 						__lowerCamelCase   (				self					:     Any      )       ->int:
          lowerCamelCase__       :						Union[str, Any]        =							VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''      ).to(
              A      )

          lowerCamelCase__       :						Dict        =							self.default_image_processor
          lowerCamelCase__       :						List[str]        =							prepare_video()
          lowerCamelCase__       :						Dict        =							image_processor(A		, return_tensors='''pt'''      ).to(A      )

          # forward pass
          with torch.no_grad():
             lowerCamelCase__       :						str        =							model(**A      )

          # verify the logits
          lowerCamelCase__       :						List[str]        =							torch.Size((1, 4_0_0)      )
          self.assertEqual(outputs.logits.shape		, A      )

          lowerCamelCase__       :						int        =							torch.tensor([0.36_69, -0.06_88, -0.24_21]      ).to(A      )

          self.assertTrue(torch.allclose(outputs.logits[0, :3]		, A		, atol=1e-4      )      )







       @slow
       def 						__lowerCamelCase   (				self					:     Dict      )       ->Union[str, Any]:
          lowerCamelCase__       :						int        =							VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''      ).to(A      )

          lowerCamelCase__       :						str        =							self.default_image_processor
          lowerCamelCase__       :						Optional[int]        =							prepare_video()
          lowerCamelCase__       :						Optional[Any]        =							image_processor(A		, return_tensors='''pt'''      ).to(A      )

          # add boolean mask, indicating which patches to mask
          lowerCamelCase__       :						List[Any]        =							hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos'''		, filename='''bool_masked_pos.pt'''      )
          lowerCamelCase__       :						List[Any]        =							torch.load(A      )

          # forward pass
          with torch.no_grad():
             lowerCamelCase__       :						Optional[int]        =							model(**A      )

          # verify the logits
          lowerCamelCase__       :						Optional[int]        =							torch.Size([1, 1_4_0_8, 1_5_3_6]      )
          lowerCamelCase__       :						List[Any]        =							torch.tensor(
              [[0.79_94, 0.96_12, 0.85_08], [0.74_01, 0.89_58, 0.83_02], [0.58_62, 0.74_68, 0.73_25]]		, device=A      )
          self.assertEqual(outputs.logits.shape		, A      )
          self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		, A		, atol=1e-4      )      )

          # verify the loss (`config.norm_pix_loss` = `True`)
          lowerCamelCase__       :						Dict        =							torch.tensor([0.51_42]		, device=A      )
          self.assertTrue(torch.allclose(outputs.loss		, A		, atol=1e-4      )      )

          # verify the loss (`config.norm_pix_loss` = `False`)
          lowerCamelCase__       :						Any        =							VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''		, norm_pix_loss=A      ).to(
              A      )

          with torch.no_grad():
             lowerCamelCase__       :						Any        =							model(**A      )

          lowerCamelCase__       :						Union[str, Any]        =							torch.tensor(torch.tensor([0.64_69]      )		, device=A      )
          self.assertTrue(torch.allclose(outputs.loss		, A		, atol=1e-4      )      )










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.
_A :   List[Any]  			=  {'LayoutLMv2Config', 'LayoutLMv3Config'}


@is_pipeline_test
class        __SCREAMING_SNAKE_CASE      (			unittest.TestCase ):
       _UpperCAmelCase				:  Optional[int]			   =   MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
       _UpperCAmelCase				:  Optional[Any]			   =   TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

       if model_mapping is not None:
              _UpperCAmelCase				:  str			   =   {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
       if tf_model_mapping is not None:
              _UpperCAmelCase				:  Tuple			   =   {
                  config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
              }







       def 						__lowerCamelCase   (				self					:     str		, A					:     List[str]		, A					:     Tuple		, A					:     str      )       ->List[str]:
          lowerCamelCase__       :						List[Any]        =							ZeroShotClassificationPipeline(
              model=A		, tokenizer=A		, candidate_labels=['''polics''', '''health''']      )
          return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]







       def 						__lowerCamelCase   (				self					:     List[str]		, A					:     Union[str, Any]		, A					:     Optional[Any]      )       ->Union[str, Any]:
          lowerCamelCase__       :						Any        =							classifier('''Who are you voting for in 2020?'''		, candidate_labels='''politics'''      )
          self.assertEqual(A		, {'''sequence''': ANY(A      ), '''labels''': [ANY(A      )], '''scores''': [ANY(A      )]}      )

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

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

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

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

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

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

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

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

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

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

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

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

          self.run_entailment_id(A      )







       def 						__lowerCamelCase   (				self					:     Optional[int]		, A					:     Pipeline      )       ->Dict:
          lowerCamelCase__       :						str        =							zero_shot_classifier.model.config
          lowerCamelCase__       :						Optional[int]        =							config.labelaid
          lowerCamelCase__       :						Tuple        =							zero_shot_classifier.entailment_id

          lowerCamelCase__       :						Optional[Any]        =							{'''LABEL_0''': 0, '''LABEL_1''': 1, '''LABEL_2''': 2}
          self.assertEqual(zero_shot_classifier.entailment_id		, -1      )

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

          lowerCamelCase__       :						List[str]        =							{'''ENTAIL''': 0, '''NON-ENTAIL''': 1}
          self.assertEqual(zero_shot_classifier.entailment_id		, 0      )

          lowerCamelCase__       :						Optional[Any]        =							{'''ENTAIL''': 2, '''NEUTRAL''': 1, '''CONTR''': 0}
          self.assertEqual(zero_shot_classifier.entailment_id		, 2      )

          lowerCamelCase__       :						Tuple        =							original_labelaid
          self.assertEqual(A		, zero_shot_classifier.entailment_id      )







       @require_torch
       def 						__lowerCamelCase   (				self					:     Any      )       ->str:
          lowerCamelCase__       :						Optional[int]        =							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 						__lowerCamelCase   (				self					:     List[Any]      )       ->Optional[Any]:
          lowerCamelCase__       :						List[Any]        =							pipeline(
              '''zero-shot-classification'''		, model='''sshleifer/tiny-distilbert-base-cased-distilled-squad'''		, framework='''pt'''		, )
          lowerCamelCase__       :						Dict        =							zero_shot_classifier(
              '''Who are you voting for in 2020?'''		, candidate_labels=['''politics''', '''public health''', '''science''']      )

          self.assertEqual(
              nested_simplify(A      )		, {
                  '''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 						__lowerCamelCase   (				self					:     str      )       ->List[str]:
          lowerCamelCase__       :						Any        =							pipeline(
              '''zero-shot-classification'''		, model='''sshleifer/tiny-distilbert-base-cased-distilled-squad'''		, framework='''tf'''		, )
          lowerCamelCase__       :						Optional[Any]        =							zero_shot_classifier(
              '''Who are you voting for in 2020?'''		, candidate_labels=['''politics''', '''public health''', '''science''']      )

          self.assertEqual(
              nested_simplify(A      )		, {
                  '''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 						__lowerCamelCase   (				self					:     List[Any]      )       ->Optional[Any]:
          lowerCamelCase__       :						int        =							pipeline('''zero-shot-classification'''		, model='''roberta-large-mnli'''		, framework='''pt'''      )
          lowerCamelCase__       :						int        =							zero_shot_classifier(
              '''Who are you voting for in 2020?'''		, candidate_labels=['''politics''', '''public health''', '''science''']      )

          self.assertEqual(
              nested_simplify(A      )		, {
                  '''sequence''': '''Who are you voting for in 2020?''',
                  '''labels''': ['''politics''', '''public health''', '''science'''],
                  '''scores''': [0.9_76, 0.0_15, 0.0_09],
              }		, )
          lowerCamelCase__       :						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=A		, )
          self.assertEqual(
              nested_simplify(A      )		, {
                  '''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 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->Optional[int]:
          lowerCamelCase__       :						List[Any]        =							pipeline('''zero-shot-classification'''		, model='''roberta-large-mnli'''		, framework='''tf'''      )
          lowerCamelCase__       :						Dict        =							zero_shot_classifier(
              '''Who are you voting for in 2020?'''		, candidate_labels=['''politics''', '''public health''', '''science''']      )

          self.assertEqual(
              nested_simplify(A      )		, {
                  '''sequence''': '''Who are you voting for in 2020?''',
                  '''labels''': ['''politics''', '''public health''', '''science'''],
                  '''scores''': [0.9_76, 0.0_15, 0.0_09],
              }		, )
          lowerCamelCase__       :						Union[str, 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=A		, )
          self.assertEqual(
              nested_simplify(A      )		, {
                  '''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],
              }		, )



