


from __future__ import absolute_import, division, print_function, unicode_literals

from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers import RobertaConfig
from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.roberta.modeling_roberta import (
    ROBERTA_INPUTS_DOCSTRING,
    ROBERTA_START_DOCSTRING,
    RobertaEmbeddings,
)

from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy




@add_start_docstrings(
    """The RoBERTa Model transformer with early exiting (DeeRoBERTa). """      , __snake_case      , )
class    UpperCAmelCase					(       __snake_case    ):
			lowercase						= RobertaConfig
			lowercase						= """roberta"""




			def __init__(						self							:		Dict       ,       __magic_name__							:		List[str]						):







						"""simple docstring"""







						super().__init__(__magic_name__						)

						UpperCamelCase         =					RobertaEmbeddings(__magic_name__						)
						self.init_weights()




@add_start_docstrings(
    """RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,
    also takes care of multi-layer training. """      , __snake_case      , )
class    UpperCAmelCase					(       __snake_case    ):
			lowercase						= RobertaConfig
			lowercase						= """roberta"""




			def __init__(						self							:		Optional[Any]       ,       __magic_name__							:		Dict						):







						"""simple docstring"""







						super().__init__(__magic_name__						)
						UpperCamelCase         =					config.num_labels
						UpperCamelCase         =					config.num_hidden_layers

						UpperCamelCase         =					DeeRobertaModel(__magic_name__						)
						UpperCamelCase         =					nn.Dropout(config.hidden_dropout_prob						)
						UpperCamelCase         =					nn.Linear(config.hidden_size       ,       self.config.num_labels						)





			@add_start_docstrings_to_model_forward(__magic_name__						)
			def   lowerCamelCase_					(						self							:		Any       ,       __magic_name__							:		Tuple=None       ,       __magic_name__							:		str=None       ,       __magic_name__							:		Any=None       ,       __magic_name__							:		str=None       ,       __magic_name__							:		str=None       ,       __magic_name__							:		Optional[int]=None       ,       __magic_name__							:		List[str]=None       ,       __magic_name__							:		Tuple=-1       ,       __magic_name__							:		Tuple=False       ,       ):







						"""simple docstring"""







						UpperCamelCase         =					self.num_layers
						try:
									UpperCamelCase         =					self.roberta(
									    __magic_name__       ,       attention_mask=__magic_name__       ,       token_type_ids=__magic_name__       ,       position_ids=__magic_name__       ,       head_mask=__magic_name__       ,       inputs_embeds=__magic_name__       ,       )

									UpperCamelCase         =					outputs[1]

									UpperCamelCase         =					self.dropout(__magic_name__						)
									UpperCamelCase         =					self.classifier(__magic_name__						)
									UpperCamelCase         =					(logits,) + outputs[2:]  # add hidden states and attention if they are here
						except HighwayException as e:
									UpperCamelCase         =					e.message
									UpperCamelCase         =					e.exit_layer
									UpperCamelCase         =					outputs[0]

						if not self.training:
									UpperCamelCase         =					entropy(__magic_name__						)
									UpperCamelCase         =					[]
									UpperCamelCase         =					[]
						if labels is not None:
									if self.num_labels == 1:
												#  We are doing regression
												UpperCamelCase         =					MSELoss()
												UpperCamelCase         =					loss_fct(logits.view(-1						)       ,       labels.view(-1						)						)
									else:
												UpperCamelCase         =					CrossEntropyLoss()
												UpperCamelCase         =					loss_fct(logits.view(-1       ,       self.num_labels						)       ,       labels.view(-1						)						)

									# work with highway exits
									UpperCamelCase         =					[]
									for highway_exit in outputs[-1]:
												UpperCamelCase         =					highway_exit[0]
												if not self.training:
															highway_logits_all.append(__magic_name__						)
															highway_entropy.append(highway_exit[2]						)
												if self.num_labels == 1:
															#  We are doing regression
															UpperCamelCase         =					MSELoss()
															UpperCamelCase         =					loss_fct(highway_logits.view(-1						)       ,       labels.view(-1						)						)
												else:
															UpperCamelCase         =					CrossEntropyLoss()
															UpperCamelCase         =					loss_fct(highway_logits.view(-1       ,       self.num_labels						)       ,       labels.view(-1						)						)
												highway_losses.append(__magic_name__						)

									if train_highway:
												UpperCamelCase         =					(sum(highway_losses[:-1]						),) + outputs
												# exclude the final highway, of course
									else:
												UpperCamelCase         =					(loss,) + outputs
						if not self.training:
									UpperCamelCase         =					outputs + ((original_entropy, highway_entropy), exit_layer)
									if output_layer >= 0:
												UpperCamelCase         =					(
												    (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:]
												)  # use the highway of the last layer

						return outputs  # (loss), logits, (hidden_states), (attentions), entropy








import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import OwlViTImageProcessor, OwlViTProcessor




@require_vision
class    UpperCAmelCase					(       unittest.TestCase    ):




			def   lowerCamelCase_					(						self							:		Optional[Any]						):







						"""simple docstring"""







						UpperCamelCase         =					tempfile.mkdtemp()

						# fmt: off
						UpperCamelCase         =					["""""", """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
						# fmt: on
						UpperCamelCase         =					dict(zip(__magic_name__       ,       range(len(__magic_name__						)						)						)						)
						UpperCamelCase         =					["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
						UpperCamelCase         =					{"""unk_token""": """<unk>"""}

						UpperCamelCase         =					os.path.join(self.tmpdirname       ,       VOCAB_FILES_NAMES["""vocab_file"""]						)
						UpperCamelCase         =					os.path.join(self.tmpdirname       ,       VOCAB_FILES_NAMES["""merges_file"""]						)
						with open(self.vocab_file       ,       """w"""       ,       encoding="""utf-8"""						) as fp:
									fp.write(json.dumps(__magic_name__						) + """\n"""						)
						with open(self.merges_file       ,       """w"""       ,       encoding="""utf-8"""						) as fp:
									fp.write("""\n""".join(__magic_name__						)						)

						UpperCamelCase         =					{
						    """do_resize""": True,
						    """size""": 2_0,
						    """do_center_crop""": True,
						    """crop_size""": 1_8,
						    """do_normalize""": True,
						    """image_mean""": [0.48_145_466, 0.4_578_275, 0.40_821_073],
						    """image_std""": [0.26_862_954, 0.26_130_258, 0.27_577_711],
						}
						UpperCamelCase         =					os.path.join(self.tmpdirname       ,       __magic_name__						)
						with open(self.image_processor_file       ,       """w"""       ,       encoding="""utf-8"""						) as fp:
									json.dump(__magic_name__       ,       __magic_name__						)




			def   lowerCamelCase_					(						self							:		str       ,       **__magic_name__							:		Tuple						):







						"""simple docstring"""







						return CLIPTokenizer.from_pretrained(self.tmpdirname       ,       pad_token="""!"""       ,       **__magic_name__						)




			def   lowerCamelCase_					(						self							:		Optional[int]       ,       **__magic_name__							:		str						):







						"""simple docstring"""







						return CLIPTokenizerFast.from_pretrained(self.tmpdirname       ,       pad_token="""!"""       ,       **__magic_name__						)




			def   lowerCamelCase_					(						self							:		Optional[int]       ,       **__magic_name__							:		Optional[Any]						):







						"""simple docstring"""







						return OwlViTImageProcessor.from_pretrained(self.tmpdirname       ,       **__magic_name__						)




			def   lowerCamelCase_					(						self							:		int						):







						"""simple docstring"""







						shutil.rmtree(self.tmpdirname						)




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					[np.random.randint(2_5_5       ,       size=(3, 3_0, 4_0_0)       ,       dtype=np.uinta						)]

						UpperCamelCase         =					[Image.fromarray(np.moveaxis(__magic_name__       ,       0       ,       -1						)						) for x in image_inputs]

						return image_inputs




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_tokenizer()
						UpperCamelCase         =					self.get_rust_tokenizer()
						UpperCamelCase         =					self.get_image_processor()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)
						processor_slow.save_pretrained(self.tmpdirname						)
						UpperCamelCase         =					OwlViTProcessor.from_pretrained(self.tmpdirname       ,       use_fast=__magic_name__						)

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)
						processor_fast.save_pretrained(self.tmpdirname						)
						UpperCamelCase         =					OwlViTProcessor.from_pretrained(self.tmpdirname						)

						self.assertEqual(processor_slow.tokenizer.get_vocab()       ,       tokenizer_slow.get_vocab()						)
						self.assertEqual(processor_fast.tokenizer.get_vocab()       ,       tokenizer_fast.get_vocab()						)
						self.assertEqual(tokenizer_slow.get_vocab()       ,       tokenizer_fast.get_vocab()						)
						self.assertIsInstance(processor_slow.tokenizer       ,       __magic_name__						)
						self.assertIsInstance(processor_fast.tokenizer       ,       __magic_name__						)

						self.assertEqual(processor_slow.image_processor.to_json_string()       ,       image_processor.to_json_string()						)
						self.assertEqual(processor_fast.image_processor.to_json_string()       ,       image_processor.to_json_string()						)
						self.assertIsInstance(processor_slow.image_processor       ,       __magic_name__						)
						self.assertIsInstance(processor_fast.image_processor       ,       __magic_name__						)




			def   lowerCamelCase_					(						self							:		int						):







						"""simple docstring"""







						UpperCamelCase         =					OwlViTProcessor(tokenizer=self.get_tokenizer()       ,       image_processor=self.get_image_processor()						)
						processor.save_pretrained(self.tmpdirname						)

						UpperCamelCase         =					self.get_tokenizer(bos_token="""(BOS)"""       ,       eos_token="""(EOS)"""						)
						UpperCamelCase         =					self.get_image_processor(do_normalize=__magic_name__						)

						UpperCamelCase         =					OwlViTProcessor.from_pretrained(
						    self.tmpdirname       ,       bos_token="""(BOS)"""       ,       eos_token="""(EOS)"""       ,       do_normalize=__magic_name__						)

						self.assertEqual(processor.tokenizer.get_vocab()       ,       tokenizer_add_kwargs.get_vocab()						)
						self.assertIsInstance(processor.tokenizer       ,       __magic_name__						)

						self.assertEqual(processor.image_processor.to_json_string()       ,       image_processor_add_kwargs.to_json_string()						)
						self.assertIsInstance(processor.image_processor       ,       __magic_name__						)




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_image_processor()
						UpperCamelCase         =					self.get_tokenizer()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)

						UpperCamelCase         =					self.prepare_image_inputs()

						UpperCamelCase         =					image_processor(__magic_name__       ,       return_tensors="""np"""						)
						UpperCamelCase         =					processor(images=__magic_name__       ,       return_tensors="""np"""						)

						for key in input_image_proc.keys():
									self.assertAlmostEqual(input_image_proc[key].sum()       ,       input_processor[key].sum()       ,       delta=1e-2						)




			def   lowerCamelCase_					(						self							:		Any						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_image_processor()
						UpperCamelCase         =					self.get_tokenizer()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)

						UpperCamelCase         =					"""lower newer"""

						UpperCamelCase         =					processor(text=__magic_name__       ,       return_tensors="""np"""						)

						UpperCamelCase         =					tokenizer(__magic_name__       ,       return_tensors="""np"""						)

						for key in encoded_tok.keys():
									self.assertListEqual(encoded_tok[key][0].tolist()       ,       encoded_processor[key][0].tolist()						)




			def   lowerCamelCase_					(						self							:		Optional[Any]						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_image_processor()
						UpperCamelCase         =					self.get_tokenizer()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)

						UpperCamelCase         =					"""lower newer"""
						UpperCamelCase         =					self.prepare_image_inputs()

						UpperCamelCase         =					processor(text=__magic_name__       ,       images=__magic_name__						)

						self.assertListEqual(list(inputs.keys()						)       ,       ["""input_ids""", """attention_mask""", """pixel_values"""]						)

						# test if it raises when no input is passed
						with pytest.raises(__magic_name__						):
									processor()




			def   lowerCamelCase_					(						self							:		int						):







						"""simple docstring"""







						UpperCamelCase         =					"""google/owlvit-base-patch32"""
						UpperCamelCase         =					OwlViTProcessor.from_pretrained(__magic_name__						)

						UpperCamelCase         =					["""cat""", """nasa badge"""]
						UpperCamelCase         =					processor(text=__magic_name__						)

						UpperCamelCase         =					1_6
						self.assertListEqual(list(inputs.keys()						)       ,       ["""input_ids""", """attention_mask"""]						)
						self.assertEqual(inputs["""input_ids"""].shape       ,       (2, seq_length)						)

						# test if it raises when no input is passed
						with pytest.raises(__magic_name__						):
									processor()




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					"""google/owlvit-base-patch32"""
						UpperCamelCase         =					OwlViTProcessor.from_pretrained(__magic_name__						)

						UpperCamelCase         =					[["""cat""", """nasa badge"""], ["""person"""]]
						UpperCamelCase         =					processor(text=__magic_name__						)

						UpperCamelCase         =					1_6
						UpperCamelCase         =					len(__magic_name__						)
						UpperCamelCase         =					max([len(__magic_name__						) for texts in input_texts]						)

						self.assertListEqual(list(inputs.keys()						)       ,       ["""input_ids""", """attention_mask"""]						)
						self.assertEqual(inputs["""input_ids"""].shape       ,       (batch_size * num_max_text_queries, seq_length)						)

						# test if it raises when no input is passed
						with pytest.raises(__magic_name__						):
									processor()




			def   lowerCamelCase_					(						self							:		List[str]						):







						"""simple docstring"""







						UpperCamelCase         =					"""google/owlvit-base-patch32"""
						UpperCamelCase         =					OwlViTProcessor.from_pretrained(__magic_name__						)

						UpperCamelCase         =					["""cat""", """nasa badge"""]
						UpperCamelCase         =					processor(text=__magic_name__						)

						UpperCamelCase         =					1_6
						UpperCamelCase         =					inputs["""input_ids"""]
						UpperCamelCase         =					[
						    [4_9_4_0_6, 2_3_6_8, 4_9_4_0_7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
						    [4_9_4_0_6, 6_8_4_1, 1_1_3_0_1, 4_9_4_0_7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
						]

						self.assertListEqual(list(inputs.keys()						)       ,       ["""input_ids""", """attention_mask"""]						)
						self.assertEqual(inputs["""input_ids"""].shape       ,       (2, seq_length)						)
						self.assertListEqual(list(input_ids[0]						)       ,       predicted_ids[0]						)
						self.assertListEqual(list(input_ids[1]						)       ,       predicted_ids[1]						)




			def   lowerCamelCase_					(						self							:		str						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_image_processor()
						UpperCamelCase         =					self.get_tokenizer()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)

						UpperCamelCase         =					self.prepare_image_inputs()
						UpperCamelCase         =					self.prepare_image_inputs()

						UpperCamelCase         =					processor(images=__magic_name__       ,       query_images=__magic_name__						)

						self.assertListEqual(list(inputs.keys()						)       ,       ["""query_pixel_values""", """pixel_values"""]						)

						# test if it raises when no input is passed
						with pytest.raises(__magic_name__						):
									processor()





			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_image_processor()
						UpperCamelCase         =					self.get_tokenizer()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)

						UpperCamelCase         =					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

						UpperCamelCase         =					processor.batch_decode(__magic_name__						)
						UpperCamelCase         =					tokenizer.batch_decode(__magic_name__						)

						self.assertListEqual(__magic_name__       ,       __magic_name__						)




