




'''simple docstring'''




import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
     import torch

     from transformers import (
         YosoForMaskedLM,
         YosoForMultipleChoice,
         YosoForQuestionAnswering,
         YosoForSequenceClassification,
         YosoForTokenClassification,
         YosoModel,
     )
     from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST


class 						__UpperCamelCase :






       def __init__(							self,	lowerCAmelCase,	lowerCAmelCase=13,	lowerCAmelCase=7,	lowerCAmelCase=True,	lowerCAmelCase=True,	lowerCAmelCase=True,	lowerCAmelCase=True,	lowerCAmelCase=99,	lowerCAmelCase=32,	lowerCAmelCase=5,	lowerCAmelCase=4,	lowerCAmelCase=37,	lowerCAmelCase="gelu",	lowerCAmelCase=0.1,	lowerCAmelCase=0.1,	lowerCAmelCase=512,	lowerCAmelCase=16,	lowerCAmelCase=2,	lowerCAmelCase=0.0_2,	lowerCAmelCase=3,	lowerCAmelCase=4,	lowerCAmelCase=None,	):



              """simple docstring"""







              lowerCamelCase_							=parent
              lowerCamelCase_							=batch_size
              lowerCamelCase_							=seq_length
              lowerCamelCase_							=is_training
              lowerCamelCase_							=use_input_mask
              lowerCamelCase_							=use_token_type_ids
              lowerCamelCase_							=use_labels
              lowerCamelCase_							=vocab_size
              lowerCamelCase_							=hidden_size
              lowerCamelCase_							=num_hidden_layers
              lowerCamelCase_							=num_attention_heads
              lowerCamelCase_							=intermediate_size
              lowerCamelCase_							=hidden_act
              lowerCamelCase_							=hidden_dropout_prob
              lowerCamelCase_							=attention_probs_dropout_prob
              lowerCamelCase_							=max_position_embeddings
              lowerCamelCase_							=type_vocab_size
              lowerCamelCase_							=type_sequence_label_size
              lowerCamelCase_							=initializer_range
              lowerCamelCase_							=num_labels
              lowerCamelCase_							=num_choices
              lowerCamelCase_							=scope






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=ids_tensor([self.batch_size, self.seq_length],	self.vocab_size						)

              lowerCamelCase_							=None
              if self.use_input_mask:
                     lowerCamelCase_							=random_attention_mask([self.batch_size, self.seq_length]						)

              lowerCamelCase_							=None
              if self.use_token_type_ids:
                     lowerCamelCase_							=ids_tensor([self.batch_size, self.seq_length],	self.type_vocab_size						)

              lowerCamelCase_							=None
              lowerCamelCase_							=None
              lowerCamelCase_							=None
              if self.use_labels:
                     lowerCamelCase_							=ids_tensor([self.batch_size],	self.type_sequence_label_size						)
                     lowerCamelCase_							=ids_tensor([self.batch_size, self.seq_length],	self.num_labels						)
                     lowerCamelCase_							=ids_tensor([self.batch_size],	self.num_choices						)

              lowerCamelCase_							=self.get_config()

              return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






       def   lowercase__  (							self						):



              """simple docstring"""







              return YosoConfig(
                  vocab_size=self.vocab_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,	max_position_embeddings=self.max_position_embeddings,	type_vocab_size=self.type_vocab_size,	is_decoder=lowerCAmelCase,	initializer_range=self.initializer_range,	)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.get_config()
              lowerCamelCase_							=300
              return config






       def   lowercase__  (							self						):



              """simple docstring"""







              (
                  (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   
              )							=self.prepare_config_and_inputs()

              lowerCamelCase_							=True
              lowerCamelCase_							=floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
              lowerCamelCase_							=ids_tensor([self.batch_size, self.seq_length],	vocab_size=2						)

              return (
                  config,
                  input_ids,
                  token_type_ids,
                  input_mask,
                  sequence_labels,
                  token_labels,
                  choice_labels,
                  encoder_hidden_states,
                  encoder_attention_mask,
              )






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=YosoModel(config=lowerCAmelCase						)
              model.to(lowerCAmelCase						)
              model.eval()
              lowerCamelCase_							=model(lowerCAmelCase,	attention_mask=lowerCAmelCase,	token_type_ids=lowerCAmelCase						)
              lowerCamelCase_							=model(lowerCAmelCase,	token_type_ids=lowerCAmelCase						)
              lowerCamelCase_							=model(lowerCAmelCase						)
              self.parent.assertEqual(result.last_hidden_state.shape,	(self.batch_size, self.seq_length, self.hidden_size)						)






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	):



              """simple docstring"""







              lowerCamelCase_							=True
              lowerCamelCase_							=YosoModel(lowerCAmelCase						)
              model.to(lowerCAmelCase						)
              model.eval()
              lowerCamelCase_							=model(
                  lowerCAmelCase,	attention_mask=lowerCAmelCase,	token_type_ids=lowerCAmelCase,	encoder_hidden_states=lowerCAmelCase,	encoder_attention_mask=lowerCAmelCase,	)
              lowerCamelCase_							=model(
                  lowerCAmelCase,	attention_mask=lowerCAmelCase,	token_type_ids=lowerCAmelCase,	encoder_hidden_states=lowerCAmelCase,	)
              lowerCamelCase_							=model(lowerCAmelCase,	attention_mask=lowerCAmelCase,	token_type_ids=lowerCAmelCase						)
              self.parent.assertEqual(result.last_hidden_state.shape,	(self.batch_size, self.seq_length, self.hidden_size)						)






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=YosoForMaskedLM(config=lowerCAmelCase						)
              model.to(lowerCAmelCase						)
              model.eval()
              lowerCamelCase_							=model(lowerCAmelCase,	attention_mask=lowerCAmelCase,	token_type_ids=lowerCAmelCase,	labels=lowerCAmelCase						)
              self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.seq_length, self.vocab_size)						)






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=YosoForQuestionAnswering(config=lowerCAmelCase						)
              model.to(lowerCAmelCase						)
              model.eval()
              lowerCamelCase_							=model(
                  lowerCAmelCase,	attention_mask=lowerCAmelCase,	token_type_ids=lowerCAmelCase,	start_positions=lowerCAmelCase,	end_positions=lowerCAmelCase,	)
              self.parent.assertEqual(result.start_logits.shape,	(self.batch_size, self.seq_length)						)
              self.parent.assertEqual(result.end_logits.shape,	(self.batch_size, self.seq_length)						)






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=self.num_labels
              lowerCamelCase_							=YosoForSequenceClassification(lowerCAmelCase						)
              model.to(lowerCAmelCase						)
              model.eval()
              lowerCamelCase_							=model(lowerCAmelCase,	attention_mask=lowerCAmelCase,	token_type_ids=lowerCAmelCase,	labels=lowerCAmelCase						)
              self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.num_labels)						)






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=self.num_labels
              lowerCamelCase_							=YosoForTokenClassification(config=lowerCAmelCase						)
              model.to(lowerCAmelCase						)
              model.eval()
              lowerCamelCase_							=model(lowerCAmelCase,	attention_mask=lowerCAmelCase,	token_type_ids=lowerCAmelCase,	labels=lowerCAmelCase						)
              self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.seq_length, self.num_labels)						)






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=self.num_choices
              lowerCamelCase_							=YosoForMultipleChoice(config=lowerCAmelCase						)
              model.to(lowerCAmelCase						)
              model.eval()
              lowerCamelCase_							=input_ids.unsqueeze(1						).expand(-1,	self.num_choices,	-1						).contiguous()
              lowerCamelCase_							=token_type_ids.unsqueeze(1						).expand(-1,	self.num_choices,	-1						).contiguous()
              lowerCamelCase_							=input_mask.unsqueeze(1						).expand(-1,	self.num_choices,	-1						).contiguous()
              lowerCamelCase_							=model(
                  lowerCAmelCase,	attention_mask=lowerCAmelCase,	token_type_ids=lowerCAmelCase,	labels=lowerCAmelCase,	)
              self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.num_choices)						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.prepare_config_and_inputs()
              (
                  (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   (
                  lowerCamelCase_
              ),   
              )							=config_and_inputs
              lowerCamelCase_							={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
              return config, inputs_dict





@require_torch
class 						__UpperCamelCase (				lowerCamelCase__    ,      lowerCamelCase__    ,      unittest.TestCase       ):
       lowercase			:					Dict   		=(
           (
               YosoModel,
               YosoForMaskedLM,
               YosoForMultipleChoice,
               YosoForQuestionAnswering,
               YosoForSequenceClassification,
               YosoForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       lowercase			:					Optional[Any]   		=False
       lowercase			:					Tuple   		=False
       lowercase			:					List[str]   		=False

       lowercase			:					Union[str, Any]   		=()
       lowercase			:					str   		=(
           {
               'feature-extraction': YosoModel,
               'fill-mask': YosoForMaskedLM,
               'question-answering': YosoForQuestionAnswering,
               'text-classification': YosoForSequenceClassification,
               'token-classification': YosoForTokenClassification,
               'zero-shot': YosoForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=YosoModelTester(self						)
              lowerCamelCase_							=ConfigTester(self,	config_class=lowerCAmelCase,	hidden_size=37						)






       def   lowercase__  (							self						):



              """simple docstring"""







              self.config_tester.run_common_tests()






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              for type in ["absolute", "relative_key", "relative_key_query"]:
                     lowerCamelCase_							=type
                     self.model_tester.create_and_check_model(*lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase						)






       @slow
       def   lowercase__  (							self						):



              """simple docstring"""







              for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     lowerCamelCase_							=YosoModel.from_pretrained(lowerCAmelCase						)
                     self.assertIsNotNone(lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              return





@require_torch
class 						__UpperCamelCase (				unittest.TestCase       ):






       @slow
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=YosoModel.from_pretrained('''uw-madison/yoso-4096'''						)
              lowerCamelCase_							=torch.tensor([[0, 1, 2, 3, 4, 5]]						)

              with torch.no_grad():
                     lowerCamelCase_							=model(lowerCAmelCase						)[0]

              lowerCamelCase_							=torch.Size((1, 6, 768)						)
              self.assertEqual(output.shape,	lowerCAmelCase						)

              lowerCamelCase_							=torch.tensor(
                  [[[-0.0_6_1_1, 0.1_2_4_2, 0.0_8_4_0], [0.0_2_8_0, -0.0_0_4_8, 0.1_1_2_5], [0.0_1_0_6, 0.0_2_2_6, 0.0_7_5_1]]]						)

              self.assertTrue(torch.allclose(output[:, :3, :3],	lowerCAmelCase,	atol=1e-4						)						)






       @slow
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''						)
              lowerCamelCase_							=torch.tensor([[0, 1, 2, 3, 4, 5]]						)

              with torch.no_grad():
                     lowerCamelCase_							=model(lowerCAmelCase						)[0]

              lowerCamelCase_							=50_265

              lowerCamelCase_							=torch.Size((1, 6, vocab_size)						)
              self.assertEqual(output.shape,	lowerCAmelCase						)

              lowerCamelCase_							=torch.tensor(
                  [[[-2.1_3_1_3, -3.7_2_8_5, -2.2_4_0_7], [-2.7_0_4_7, -3.3_3_1_4, -2.6_4_0_8], [0.0_6_2_9, -2.5_1_6_6, -0.3_3_5_6]]]						)

              self.assertTrue(torch.allclose(output[:, :3, :3],	lowerCAmelCase,	atol=1e-4						)						)






       @slow
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''						)
              lowerCamelCase_							=torch.arange(4_096						).unsqueeze(0						)

              with torch.no_grad():
                     lowerCamelCase_							=model(lowerCAmelCase						)[0]

              lowerCamelCase_							=50_265

              lowerCamelCase_							=torch.Size((1, 4_096, vocab_size)						)
              self.assertEqual(output.shape,	lowerCAmelCase						)

              lowerCamelCase_							=torch.tensor(
                  [[[-2.3_9_1_4, -4.3_7_4_2, -5.0_9_5_6], [-4.0_9_8_8, -4.2_3_8_4, -7.0_4_0_6], [-3.1_4_2_7, -3.7_1_9_2, -6.6_8_0_0]]]						)

              self.assertTrue(torch.allclose(output[:, :3, :3],	lowerCAmelCase,	atol=1e-4						)						)










'''simple docstring'''




from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_clip import CLIPTokenizer


a_   :	Tuple							   = logging.get_logger(__name__)

a_   :	str							   = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

a_   :	Tuple							   = {
    """vocab_file""": {
        """openai/clip-vit-base-patch32""": """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """openai/clip-vit-base-patch32""": """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """openai/clip-vit-base-patch32""": (
            """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/tokenizer.json"""
        ),
    },
}

a_   :	str							   = {
    """openai/clip-vit-base-patch32""": 77,
}


class 						__UpperCamelCase (				lowerCamelCase__       ):
       lowercase			:					Union[str, Any]   		=VOCAB_FILES_NAMES
       lowercase			:					Optional[int]   		=PRETRAINED_VOCAB_FILES_MAP
       lowercase			:					Any   		=PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       lowercase			:					str   		=['input_ids', 'attention_mask']
       lowercase			:					List[Any]   		=CLIPTokenizer






       def __init__(							self,	lowerCAmelCase=None,	lowerCAmelCase=None,	lowerCAmelCase=None,	lowerCAmelCase="<|endoftext|>",	lowerCAmelCase="<|startoftext|>",	lowerCAmelCase="<|endoftext|>",	lowerCAmelCase="<|endoftext|>",	**lowerCAmelCase,	):



              """simple docstring"""







              super().__init__(
                  lowerCAmelCase,	lowerCAmelCase,	tokenizer_file=lowerCAmelCase,	unk_token=lowerCAmelCase,	bos_token=lowerCAmelCase,	eos_token=lowerCAmelCase,	pad_token=lowerCAmelCase,	**lowerCAmelCase,	)

              if not isinstance(self.backend_tokenizer.pre_tokenizer,	pre_tokenizers.Sequence						):
                     raise ValueError(
                         '''The `backend_tokenizer` provided does not match the expected format. The CLIP tokenizer has been'''
                         ''' heavily modified from transformers version 4.17.0. You need to convert the tokenizer you are using'''
                         ''' to be compatible with this version.The easiest way to do so is'''
                         ''' `CLIPTokenizerFast.from_pretrained("path_to_local_folder_or_hub_repo, from_slow=True)`. If you want'''
                         ''' to use your existing tokenizer, you will have to revert to a version prior to 4.17.0 of'''
                         ''' transformers.'''						)

              self._wrap_decode_method_backend_tokenizer()






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.backend_tokenizer.decode

              def new_decode_method(*lowerCAmelCase,	**lowerCAmelCase						):
                     lowerCamelCase_							=orig_decode_method(*lowerCAmelCase,	**lowerCAmelCase						)
                     lowerCamelCase_							=text.replace(self.backend_tokenizer.model.end_of_word_suffix,	''' '''						).strip()
                     return text

              lowerCamelCase_							=new_decode_method






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase = None						):



              """simple docstring"""







              lowerCamelCase_							=[self.bos_token_id]
              lowerCamelCase_							=[self.eos_token_id]

              if token_ids_a is None:
                     return bos_token + token_ids_a + eos_token
              return bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase = None						):



              """simple docstring"""







              lowerCamelCase_							=[self.bos_token_id]
              lowerCamelCase_							=[self.eos_token_id]

              if token_ids_a is None:
                     return len(bos_token + token_ids_a + eos_token						) * [0]
              return len(bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token						) * [0]






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase = None						):



              """simple docstring"""







              lowerCamelCase_							=self._tokenizer.model.save(lowerCAmelCase,	name=lowerCAmelCase						)
              return tuple(lowerCAmelCase						)




