





import json
import os
import shutil
import tempfile
from unittest import TestCase

from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast
from transformers.models.bart.configuration_bart import BartConfig
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES
from transformers.models.dpr.configuration_dpr import DPRConfig
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES
from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow
from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available


if is_torch_available() and is_datasets_available() and is_faiss_available():
     from transformers.models.rag.configuration_rag import RagConfig
     from transformers.models.rag.tokenization_rag import RagTokenizer



@require_faiss
@require_torch
class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[Any]        =tempfile.mkdtemp()
   lowerCamelCase__:				str        =8

   # DPR tok
   lowerCamelCase__:				List[str]        =[
       "[UNK]",
       "[CLS]",
       "[SEP]",
       "[PAD]",
       "[MASK]",
       "want",
       "##want",
       "##ed",
       "wa",
       "un",
       "runn",
       "##ing",
       ",",
       "low",
       "lowest",
   ]
   lowerCamelCase__:				Any        =os.path.join(self.tmpdirname	,			"dpr_tokenizer")
   os.makedirs(UpperCAmelCase_	,			exist_ok=UpperCAmelCase_)
   lowerCamelCase__:				int        =os.path.join(UpperCAmelCase_	,			DPR_VOCAB_FILES_NAMES["vocab_file"])
   with open(self.vocab_file	,			"w"	,			encoding="utf-8") as vocab_writer:
     vocab_writer.write("".join([x + "\n" for x in vocab_tokens]))

   # BART tok
   lowerCamelCase__:				List[str]        =[
       "l",
       "o",
       "w",
       "e",
       "r",
       "s",
       "t",
       "i",
       "d",
       "n",
       "\u0120",
       "\u0120l",
       "\u0120n",
       "\u0120lo",
       "\u0120low",
       "er",
       "\u0120lowest",
       "\u0120newer",
       "\u0120wider",
       "<unk>",
   ]
   lowerCamelCase__:				Tuple        =dict(zip(UpperCAmelCase_	,			range(len(UpperCAmelCase_))))
   lowerCamelCase__:				Optional[int]        =["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""]
   lowerCamelCase__:				Any        ={"unk_token": "<unk>"}

   lowerCamelCase__:				int        =os.path.join(self.tmpdirname	,			"bart_tokenizer")
   os.makedirs(UpperCAmelCase_	,			exist_ok=UpperCAmelCase_)
   lowerCamelCase__:				Any        =os.path.join(UpperCAmelCase_	,			BART_VOCAB_FILES_NAMES["vocab_file"])
   lowerCamelCase__:				Optional[Any]        =os.path.join(UpperCAmelCase_	,			BART_VOCAB_FILES_NAMES["merges_file"])
   with open(self.vocab_file	,			"w"	,			encoding="utf-8") as fp:
     fp.write(json.dumps(UpperCAmelCase_) + "\n")
   with open(self.merges_file	,			"w"	,			encoding="utf-8") as fp:
     fp.write("\n".join(UpperCAmelCase_))

 def 							SCREAMING_SNAKE_CASE_ (self    :       Any)	->DPRQuestionEncoderTokenizer:



   '''simple docstring'''



   return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname	,			"dpr_tokenizer"))

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any])	->BartTokenizer:



   '''simple docstring'''



   return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname	,			"bart_tokenizer"))

 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->Tuple:



   '''simple docstring'''



   shutil.rmtree(self.tmpdirname)

 @require_tokenizers
 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->int:



   '''simple docstring'''



   lowerCamelCase__:				int        =os.path.join(self.tmpdirname	,			"rag_tokenizer")
   lowerCamelCase__:				Union[str, Any]        =RagConfig(question_encoder=DPRConfig().to_dict()	,			generator=BartConfig().to_dict())
   lowerCamelCase__:				Union[str, Any]        =RagTokenizer(question_encoder=self.get_dpr_tokenizer()	,			generator=self.get_bart_tokenizer())
   rag_config.save_pretrained(UpperCAmelCase_)
   rag_tokenizer.save_pretrained(UpperCAmelCase_)
   lowerCamelCase__:				Optional[Any]        =RagTokenizer.from_pretrained(UpperCAmelCase_	,			config=UpperCAmelCase_)
   self.assertIsInstance(new_rag_tokenizer.question_encoder	,			UpperCAmelCase_)
   self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab()	,			rag_tokenizer.question_encoder.get_vocab())
   self.assertIsInstance(new_rag_tokenizer.generator	,			UpperCAmelCase_)
   self.assertEqual(new_rag_tokenizer.generator.get_vocab()	,			rag_tokenizer.generator.get_vocab())

 @slow
 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int])	->str:



   '''simple docstring'''



   lowerCamelCase__:				str        =RagTokenizer.from_pretrained("facebook/rag-token-nq")
   lowerCamelCase__:				Optional[Any]        =[
       "who got the first nobel prize in physics",
       "when is the next deadpool movie being released",
       "which mode is used for short wave broadcast service",
       "who is the owner of reading football club",
       "when is the next scandal episode coming out",
       "when is the last time the philadelphia won the superbowl",
       "what is the most current adobe flash player version",
       "how many episodes are there in dragon ball z",
       "what is the first step in the evolution of the eye",
       "where is gall bladder situated in human body",
       "what is the main mineral in lithium batteries",
       "who is the president of usa right now",
       "where do the greasers live in the outsiders",
       "panda is a national animal of which country",
       "what is the name of manchester united stadium",
   ]
   lowerCamelCase__:				Any        =tokenizer(UpperCAmelCase_)
   self.assertIsNotNone(UpperCAmelCase_)







 @slow
 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->str:



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        =RagTokenizer.from_pretrained("facebook/rag-sequence-nq")
   lowerCamelCase__:				Dict        =[
       "who got the first nobel prize in physics",
       "when is the next deadpool movie being released",
       "which mode is used for short wave broadcast service",
       "who is the owner of reading football club",
       "when is the next scandal episode coming out",
       "when is the last time the philadelphia won the superbowl",
       "what is the most current adobe flash player version",
       "how many episodes are there in dragon ball z",
       "what is the first step in the evolution of the eye",
       "where is gall bladder situated in human body",
       "what is the main mineral in lithium batteries",
       "who is the president of usa right now",
       "where do the greasers live in the outsiders",
       "panda is a national animal of which country",
       "what is the name of manchester united stadium",
   ]
   lowerCamelCase__:				Tuple        =tokenizer(UpperCAmelCase_)
   self.assertIsNotNone(UpperCAmelCase_)








from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
     from PIL import Image

     from transformers import ViTImageProcessor



class        _SCREAMING_SNAKE_CASE						:







 '''simple docstring'''

 def __init__(self    :       Optional[Any]	,			UpperCAmelCase_    :       Optional[int]	,			UpperCAmelCase_    :       List[Any]=13	,			UpperCAmelCase_    :       Dict=30	,			UpperCAmelCase_    :       str=2	,			UpperCAmelCase_    :       Union[str, Any]=3	,			UpperCAmelCase_    :       Union[str, Any]=True	,			UpperCAmelCase_    :       List[str]=True	,			UpperCAmelCase_    :       Dict=32	,			UpperCAmelCase_    :       List[Any]=2	,			UpperCAmelCase_    :       List[str]=4	,			UpperCAmelCase_    :       List[str]=37	,			UpperCAmelCase_    :       List[str]="gelu"	,			UpperCAmelCase_    :       List[Any]=0.1	,			UpperCAmelCase_    :       Optional[Any]=0.1	,			UpperCAmelCase_    :       str=10	,			UpperCAmelCase_    :       str=0.02	,			UpperCAmelCase_    :       Optional[int]=3	,			UpperCAmelCase_    :       Dict=None	,			)	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				int        =parent
   lowerCamelCase__:				Any        =batch_size
   lowerCamelCase__:				List[str]        =image_size
   lowerCamelCase__:				Tuple        =patch_size
   lowerCamelCase__:				Tuple        =num_channels
   lowerCamelCase__:				Tuple        =is_training
   lowerCamelCase__:				Optional[int]        =use_labels
   lowerCamelCase__:				Dict        =hidden_size
   lowerCamelCase__:				Dict        =num_hidden_layers
   lowerCamelCase__:				Optional[Any]        =num_attention_heads
   lowerCamelCase__:				Union[str, Any]        =intermediate_size
   lowerCamelCase__:				List[str]        =hidden_act
   lowerCamelCase__:				Optional[Any]        =hidden_dropout_prob
   lowerCamelCase__:				str        =attention_probs_dropout_prob
   lowerCamelCase__:				str        =type_sequence_label_size
   lowerCamelCase__:				Optional[Any]        =initializer_range
   lowerCamelCase__:				Optional[int]        =scope

   # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
   lowerCamelCase__:				List[str]        =(image_size // patch_size) ** 2
   lowerCamelCase__:				Optional[Any]        =num_patches + 1

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->List[str]:



   '''simple docstring'''



   lowerCamelCase__:				Tuple        =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

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

   return config, pixel_values, labels

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int])	->List[str]:



   '''simple docstring'''



   return ViTConfig(
       image_size=self.image_size	,			patch_size=self.patch_size	,			num_channels=self.num_channels	,			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=UpperCAmelCase_	,			initializer_range=self.initializer_range	,			)

 def 							SCREAMING_SNAKE_CASE_ (self    :       int	,			UpperCAmelCase_    :       int	,			UpperCAmelCase_    :       Optional[Any]	,			UpperCAmelCase_    :       Tuple)	->Tuple:



   '''simple docstring'''



   lowerCamelCase__:				List[str]        =TFViTModel(config=UpperCAmelCase_)
   lowerCamelCase__:				Tuple        =model(UpperCAmelCase_	,			training=UpperCAmelCase_)
   self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, self.seq_length, self.hidden_size))

   # Test with an image with different size than the one specified in config.
   lowerCamelCase__:				Dict        =self.image_size // 2
   lowerCamelCase__:				List[str]        =pixel_values[:, :, :image_size, :image_size]
   lowerCamelCase__:				List[str]        =model(UpperCAmelCase_	,			interpolate_pos_encoding=UpperCAmelCase_	,			training=UpperCAmelCase_)
   lowerCamelCase__:				List[str]        =(image_size // self.patch_size) ** 2 + 1
   self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, seq_length, self.hidden_size))

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any]	,			UpperCAmelCase_    :       Tuple	,			UpperCAmelCase_    :       int	,			UpperCAmelCase_    :       Any)	->str:



   '''simple docstring'''



   lowerCamelCase__:				Any        =self.type_sequence_label_size
   lowerCamelCase__:				str        =TFViTForImageClassification(UpperCAmelCase_)
   lowerCamelCase__:				List[str]        =model(UpperCAmelCase_	,			labels=UpperCAmelCase_	,			training=UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.type_sequence_label_size))

   # Test with an image with different size than the one specified in config.
   lowerCamelCase__:				Union[str, Any]        =self.image_size // 2
   lowerCamelCase__:				Dict        =pixel_values[:, :, :image_size, :image_size]
   lowerCamelCase__:				Tuple        =model(UpperCAmelCase_	,			interpolate_pos_encoding=UpperCAmelCase_	,			training=UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.type_sequence_label_size))

   # test greyscale images
   lowerCamelCase__:				int        =1
   lowerCamelCase__:				List[str]        =TFViTForImageClassification(UpperCAmelCase_)
   lowerCamelCase__:				List[str]        =floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
   lowerCamelCase__:				Any        =model(UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.type_sequence_label_size))







 def 							SCREAMING_SNAKE_CASE_ (self    :       str)	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[Any]        =self.prepare_config_and_inputs()
   lowerCamelCase__  , lowerCamelCase__  , lowerCamelCase__:				Union[str, Any]        =config_and_inputs
   lowerCamelCase__:				Optional[Any]        ={"pixel_values": pixel_values}
   return config, inputs_dict



@require_tf
class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ,							__SCREAMING_SNAKE_CASE   ,							unittest.TestCase   ):







 '''simple docstring'''
 lowercase_		=      (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
 lowercase_		=      (
     {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
     if is_tf_available()
     else {}
 )

 lowercase_		=      False
 lowercase_		=      False
 lowercase_		=      False

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->Dict:



   '''simple docstring'''



   lowerCamelCase__:				int        =TFViTModelTester(self)
   lowerCamelCase__:				List[str]        =ConfigTester(self	,			config_class=UpperCAmelCase_	,			has_text_modality=UpperCAmelCase_	,			hidden_size=37)

 def 							SCREAMING_SNAKE_CASE_ (self    :       str)	->Tuple:



   '''simple docstring'''



   self.config_tester.run_common_tests()

 @unittest.skip(reason="ViT does not use inputs_embeds")
 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->Any:



   '''simple docstring'''



   pass

 @unittest.skip(reason="ViT does not use inputs_embeds")
 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->str:



   '''simple docstring'''



   pass

 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple)	->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__  , lowerCamelCase__:				int        =self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     lowerCamelCase__:				int        =model_class(UpperCAmelCase_)
     self.assertIsInstance(model.get_input_embeddings()	,			(tf.keras.layers.Layer))
     lowerCamelCase__:				Tuple        =model.get_output_embeddings()
     self.assertTrue(x is None or isinstance(UpperCAmelCase_	,			tf.keras.layers.Layer))

 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any])	->Dict:



   '''simple docstring'''



   lowerCamelCase__  , lowerCamelCase__:				Optional[Any]        =self.model_tester.prepare_config_and_inputs_for_common()

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

     lowerCamelCase__:				str        =["pixel_values"]
     self.assertListEqual(arg_names[:1]	,			UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->Any:



   '''simple docstring'''



   lowerCamelCase__:				List[str]        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_)







 @slow
 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:				Any        =TFViTModel.from_pretrained("google/vit-base-patch16-224")
   self.assertIsNotNone(UpperCAmelCase_)





def 					lowerCAmelCase_			(	)					->		Tuple:







  """simple docstring"""





  lowerCamelCase__:				Any        =Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
  return image






@require_tf
@require_vision
class        _SCREAMING_SNAKE_CASE						(     unittest.TestCase   ):







 '''simple docstring'''

 @cached_property
 def 							SCREAMING_SNAKE_CASE_ (self    :       str)	->List[Any]:



   '''simple docstring'''



   return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224") if is_vision_available() else None







 @slow
 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int])	->Tuple:



   '''simple docstring'''



   lowerCamelCase__:				List[str]        =TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224")

   lowerCamelCase__:				List[str]        =self.default_image_processor
   lowerCamelCase__:				Union[str, Any]        =prepare_img()
   lowerCamelCase__:				List[Any]        =image_processor(images=UpperCAmelCase_	,			return_tensors="tf")

   # forward pass
   lowerCamelCase__:				Dict        =model(**UpperCAmelCase_)

   # verify the logits
   lowerCamelCase__:				List[Any]        =tf.TensorShape((1, 1_000))
   self.assertEqual(outputs.logits.shape	,			UpperCAmelCase_)

   lowerCamelCase__:				List[Any]        =tf.constant([-0.2744, 0.8215, -0.0836])

   tf.debugging.assert_near(outputs.logits[0, :3]	,			UpperCAmelCase_	,			atol=1E-4)

