


from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
		import tensorflow as tf

		from transformers import (
		    TFConvBertForMaskedLM,
		    TFConvBertForMultipleChoice,
		    TFConvBertForQuestionAnswering,
		    TFConvBertForSequenceClassification,
		    TFConvBertForTokenClassification,
		    TFConvBertModel,
		)



class 						_UpperCAmelCase      :






		def __init__(						self				:     Tuple   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     List[str]=13   ,  UpperCAmelCase				:     Union[str, Any]=7   ,  UpperCAmelCase				:     Dict=True   ,  UpperCAmelCase				:     Optional[int]=True   ,  UpperCAmelCase				:     Tuple=True   ,  UpperCAmelCase				:     Tuple=True   ,  UpperCAmelCase				:     List[str]=99   ,  UpperCAmelCase				:     List[str]=32   ,  UpperCAmelCase				:     str=2   ,  UpperCAmelCase				:     Tuple=4   ,  UpperCAmelCase				:     List[Any]=37   ,  UpperCAmelCase				:     Any="gelu"   ,  UpperCAmelCase				:     List[Any]=0.1   ,  UpperCAmelCase				:     List[str]=0.1   ,  UpperCAmelCase				:     Tuple=5_12   ,  UpperCAmelCase				:     Tuple=16   ,  UpperCAmelCase				:     Tuple=2   ,  UpperCAmelCase				:     Any=0.02   ,  UpperCAmelCase				:     List[Any]=3   ,  UpperCAmelCase				:     Tuple=4   ,  UpperCAmelCase				:     Any=None   ,  ):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						parent
				SCREAMING_SNAKE_CASE_				:int				     =						13
				SCREAMING_SNAKE_CASE_				:Any				     =						7
				SCREAMING_SNAKE_CASE_				:Tuple				     =						True
				SCREAMING_SNAKE_CASE_				:Tuple				     =						True
				SCREAMING_SNAKE_CASE_				:str				     =						True
				SCREAMING_SNAKE_CASE_				:str				     =						True
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						99
				SCREAMING_SNAKE_CASE_				:int				     =						3_84
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						2
				SCREAMING_SNAKE_CASE_				:Any				     =						4
				SCREAMING_SNAKE_CASE_				:Any				     =						37
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						"gelu"
				SCREAMING_SNAKE_CASE_				:Dict				     =						0.1
				SCREAMING_SNAKE_CASE_				:int				     =						0.1
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						5_12
				SCREAMING_SNAKE_CASE_				:Tuple				     =						16
				SCREAMING_SNAKE_CASE_				:List[str]				     =						2
				SCREAMING_SNAKE_CASE_				:Any				     =						0.02
				SCREAMING_SNAKE_CASE_				:str				     =						3
				SCREAMING_SNAKE_CASE_				:List[str]				     =						4
				SCREAMING_SNAKE_CASE_				:Any				     =						1_28
				SCREAMING_SNAKE_CASE_				:str				     =						2
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						9
				SCREAMING_SNAKE_CASE_				:Any				     =						1
				SCREAMING_SNAKE_CASE_				:int				     =						None






		def _snake_case		(						self				:     Any):
				SCREAMING_SNAKE_CASE_				:Tuple				     =						ids_tensor([self.batch_size, self.seq_length]   ,  self.vocab_size)

				SCREAMING_SNAKE_CASE_				:int				     =						None
				if self.use_input_mask:
						SCREAMING_SNAKE_CASE_				:str				     =						random_attention_mask([self.batch_size, self.seq_length])

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

				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						None
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						None
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						None
				if self.use_labels:
						SCREAMING_SNAKE_CASE_				:List[Any]				     =						ids_tensor([self.batch_size]   ,  self.type_sequence_label_size)
						SCREAMING_SNAKE_CASE_				:List[str]				     =						ids_tensor([self.batch_size, self.seq_length]   ,  self.num_labels)
						SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						ids_tensor([self.batch_size]   ,  self.num_choices)

				SCREAMING_SNAKE_CASE_				:str				     =						ConvBertConfig(
				    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   ,  initializer_range=self.initializer_range   ,  return_dict=UpperCAmelCase   ,  )

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






		def _snake_case		(						self				:     List[Any]   ,  UpperCAmelCase				:     str   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     Union[str, Any]   ,  UpperCAmelCase				:     List[str]   ,  UpperCAmelCase				:     str   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     Dict):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						TFConvBertModel(config=UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:int				     =						{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

				SCREAMING_SNAKE_CASE_				:Dict				     =						[input_ids, input_mask]
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						model(UpperCAmelCase)

				SCREAMING_SNAKE_CASE_				:int				     =						model(UpperCAmelCase)

				self.parent.assertEqual(result.last_hidden_state.shape   ,  (self.batch_size, self.seq_length, self.hidden_size))






		def _snake_case		(						self				:     int   ,  UpperCAmelCase				:     str   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     List[str]   ,  UpperCAmelCase				:     List[str]   ,  UpperCAmelCase				:     List[Any]   ,  UpperCAmelCase				:     Tuple):
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						TFConvBertForMaskedLM(config=UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:Tuple				     =						{
				    "input_ids": input_ids,
				    "attention_mask": input_mask,
				    "token_type_ids": token_type_ids,
				}
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						model(UpperCAmelCase)
				self.parent.assertEqual(result.logits.shape   ,  (self.batch_size, self.seq_length, self.vocab_size))






		def _snake_case		(						self				:     Optional[int]   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     Any   ,  UpperCAmelCase				:     List[str]   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     Union[str, Any]   ,  UpperCAmelCase				:     str):
				SCREAMING_SNAKE_CASE_				:Tuple				     =						self.num_labels
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						TFConvBertForSequenceClassification(config=UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:Tuple				     =						{
				    "input_ids": input_ids,
				    "attention_mask": input_mask,
				    "token_type_ids": token_type_ids,
				}

				SCREAMING_SNAKE_CASE_				:List[str]				     =						model(UpperCAmelCase)
				self.parent.assertEqual(result.logits.shape   ,  (self.batch_size, self.num_labels))






		def _snake_case		(						self				:     str   ,  UpperCAmelCase				:     Union[str, Any]   ,  UpperCAmelCase				:     str   ,  UpperCAmelCase				:     Union[str, Any]   ,  UpperCAmelCase				:     Any   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     Dict):
				SCREAMING_SNAKE_CASE_				:int				     =						self.num_choices
				SCREAMING_SNAKE_CASE_				:Tuple				     =						TFConvBertForMultipleChoice(config=UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:List[str]				     =						tf.tile(tf.expand_dims(UpperCAmelCase   ,  1)   ,  (1, self.num_choices, 1))
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						tf.tile(tf.expand_dims(UpperCAmelCase   ,  1)   ,  (1, self.num_choices, 1))
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						tf.tile(tf.expand_dims(UpperCAmelCase   ,  1)   ,  (1, self.num_choices, 1))
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						{
				    "input_ids": multiple_choice_inputs_ids,
				    "attention_mask": multiple_choice_input_mask,
				    "token_type_ids": multiple_choice_token_type_ids,
				}
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						model(UpperCAmelCase)
				self.parent.assertEqual(result.logits.shape   ,  (self.batch_size, self.num_choices))






		def _snake_case		(						self				:     Union[str, Any]   ,  UpperCAmelCase				:     str   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     Dict   ,  UpperCAmelCase				:     Any   ,  UpperCAmelCase				:     Any   ,  UpperCAmelCase				:     Any):
				SCREAMING_SNAKE_CASE_				:List[str]				     =						self.num_labels
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						TFConvBertForTokenClassification(config=UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:int				     =						{
				    "input_ids": input_ids,
				    "attention_mask": input_mask,
				    "token_type_ids": token_type_ids,
				}
				SCREAMING_SNAKE_CASE_				:Any				     =						model(UpperCAmelCase)
				self.parent.assertEqual(result.logits.shape   ,  (self.batch_size, self.seq_length, self.num_labels))






		def _snake_case		(						self				:     Tuple   ,  UpperCAmelCase				:     List[Any]   ,  UpperCAmelCase				:     Optional[int]   ,  UpperCAmelCase				:     Optional[int]   ,  UpperCAmelCase				:     Dict   ,  UpperCAmelCase				:     Union[str, Any]   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     List[str]):
				SCREAMING_SNAKE_CASE_				:int				     =						TFConvBertForQuestionAnswering(config=UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:Any				     =						{
				    "input_ids": input_ids,
				    "attention_mask": input_mask,
				    "token_type_ids": token_type_ids,
				}

				SCREAMING_SNAKE_CASE_				:List[str]				     =						model(UpperCAmelCase)
				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 _snake_case		(						self				:     Any):
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						self.prepare_config_and_inputs()
				(
				    (
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				
				)				:int				     =						config_and_inputs
				SCREAMING_SNAKE_CASE_				:str				     =						{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
				return config, inputs_dict




@require_tf
class 						_UpperCAmelCase      (     lowercase		,		lowercase		,		unittest.TestCase							):
		lowerCamelCase_	:			Dict       			=  (
		    (
		        TFConvBertModel,
		        TFConvBertForMaskedLM,
		        TFConvBertForQuestionAnswering,
		        TFConvBertForSequenceClassification,
		        TFConvBertForTokenClassification,
		        TFConvBertForMultipleChoice,
		    )
		    if is_tf_available()
		    else ()
		)
		lowerCamelCase_	:			int       			=  (
		    {
		        """feature-extraction""": TFConvBertModel,
		        """fill-mask""": TFConvBertForMaskedLM,
		        """question-answering""": TFConvBertForQuestionAnswering,
		        """text-classification""": TFConvBertForSequenceClassification,
		        """token-classification""": TFConvBertForTokenClassification,
		        """zero-shot""": TFConvBertForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		lowerCamelCase_	:			int       			=  False
		lowerCamelCase_	:			List[Any]       			=  False
		lowerCamelCase_	:			List[Any]       			=  False






		def _snake_case		(						self				:     List[str]):
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						TFConvBertModelTester(self)
				SCREAMING_SNAKE_CASE_				:Dict				     =						ConfigTester(self   ,  config_class=UpperCAmelCase   ,  hidden_size=37)






		def _snake_case		(						self				:     Union[str, Any]):
				self.config_tester.run_common_tests()






		def _snake_case		(						self				:     Tuple):
				SCREAMING_SNAKE_CASE_				:Tuple				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*UpperCAmelCase)






		def _snake_case		(						self				:     str):
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase)






		def _snake_case		(						self				:     Tuple):
				SCREAMING_SNAKE_CASE_				:List[str]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_multiple_choice(*UpperCAmelCase)






		def _snake_case		(						self				:     Any):
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase)






		def _snake_case		(						self				:     int):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase)






		def _snake_case		(						self				:     int):
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase)






		@slow
		def _snake_case		(						self				:     Dict):
				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:str				     =						self.model_tester.prepare_config_and_inputs_for_common()
				SCREAMING_SNAKE_CASE_				:int				     =						True
				SCREAMING_SNAKE_CASE_				:List[str]				     =						True

				if hasattr(UpperCAmelCase   ,  "use_cache"):
						SCREAMING_SNAKE_CASE_				:Optional[int]				     =						True

				SCREAMING_SNAKE_CASE_				:List[Any]				     =						getattr(self.model_tester   ,  "encoder_seq_length"   ,  self.model_tester.seq_length)
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						getattr(self.model_tester   ,  "key_length"   ,  UpperCAmelCase)

				for model_class in self.all_model_classes:
						SCREAMING_SNAKE_CASE_				:Tuple				     =						self._prepare_for_class(UpperCAmelCase   ,  UpperCAmelCase)
						SCREAMING_SNAKE_CASE_				:Dict				     =						model_class(UpperCAmelCase)
						SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						len(model(UpperCAmelCase))

						with tempfile.TemporaryDirectory() as tmpdirname:
								model.save_pretrained(UpperCAmelCase   ,  saved_model=UpperCAmelCase)
								SCREAMING_SNAKE_CASE_				:Tuple				     =						os.path.join(UpperCAmelCase   ,  "saved_model"   ,  "1")
								SCREAMING_SNAKE_CASE_				:Dict				     =						tf.keras.models.load_model(UpperCAmelCase)
								SCREAMING_SNAKE_CASE_				:List[Any]				     =						model(UpperCAmelCase)

								if self.is_encoder_decoder:
										SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						outputs["encoder_hidden_states"]
										SCREAMING_SNAKE_CASE_				:Tuple				     =						outputs["encoder_attentions"]
								else:
										SCREAMING_SNAKE_CASE_				:Tuple				     =						outputs["hidden_states"]
										SCREAMING_SNAKE_CASE_				:Optional[int]				     =						outputs["attentions"]

								self.assertEqual(len(UpperCAmelCase)   ,  UpperCAmelCase)

								SCREAMING_SNAKE_CASE_				:List[str]				     =						getattr(
								    self.model_tester   ,  "expected_num_hidden_layers"   ,  self.model_tester.num_hidden_layers + 1)

								self.assertEqual(len(UpperCAmelCase)   ,  UpperCAmelCase)
								self.assertListEqual(
								    list(output_hidden_states[0].shape[-2:])   ,  [self.model_tester.seq_length, self.model_tester.hidden_size]   ,  )

								self.assertEqual(len(UpperCAmelCase)   ,  self.model_tester.num_hidden_layers)
								self.assertListEqual(
								    list(output_attentions[0].shape[-3:])   ,  [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]   ,  )






		@slow
		def _snake_case		(						self				:     Optional[Any]):
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						TFConvBertModel.from_pretrained("YituTech/conv-bert-base")
				self.assertIsNotNone(UpperCAmelCase)







		def _snake_case		(						self				:     Any):
				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:List[Any]				     =						self.model_tester.prepare_config_and_inputs_for_common()
				SCREAMING_SNAKE_CASE_				:str				     =						True
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						getattr(self.model_tester   ,  "decoder_seq_length"   ,  self.model_tester.seq_length)
				SCREAMING_SNAKE_CASE_				:int				     =						getattr(self.model_tester   ,  "encoder_seq_length"   ,  self.model_tester.seq_length)
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						getattr(self.model_tester   ,  "key_length"   ,  UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						getattr(self.model_tester   ,  "key_length"   ,  UpperCAmelCase)

				def check_decoder_attentions_output(UpperCAmelCase				:     int):
						SCREAMING_SNAKE_CASE_				:Any				     =						len(UpperCAmelCase)
						self.assertEqual(out_len % 2   ,  0)
						SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						outputs.decoder_attentions
						self.assertEqual(len(UpperCAmelCase)   ,  self.model_tester.num_hidden_layers)
						self.assertListEqual(
						    list(decoder_attentions[0].shape[-3:])   ,  [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]   ,  )

				def check_encoder_attentions_output(UpperCAmelCase				:     int):
						SCREAMING_SNAKE_CASE_				:Optional[int]				     =						[
						    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
						]
						self.assertEqual(len(UpperCAmelCase)   ,  self.model_tester.num_hidden_layers)
						self.assertListEqual(
						    list(attentions[0].shape[-3:])   ,  [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]   ,  )

				for model_class in self.all_model_classes:
						SCREAMING_SNAKE_CASE_				:List[Any]				     =						True
						SCREAMING_SNAKE_CASE_				:List[str]				     =						False
						SCREAMING_SNAKE_CASE_				:str				     =						model_class(UpperCAmelCase)
						SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						model(self._prepare_for_class(UpperCAmelCase   ,  UpperCAmelCase))
						SCREAMING_SNAKE_CASE_				:Dict				     =						len(UpperCAmelCase)
						self.assertEqual(config.output_hidden_states   ,  UpperCAmelCase)
						check_encoder_attentions_output(UpperCAmelCase)

						if self.is_encoder_decoder:
								SCREAMING_SNAKE_CASE_				:str				     =						model_class(UpperCAmelCase)
								SCREAMING_SNAKE_CASE_				:Optional[int]				     =						model(self._prepare_for_class(UpperCAmelCase   ,  UpperCAmelCase))
								self.assertEqual(config.output_hidden_states   ,  UpperCAmelCase)
								check_decoder_attentions_output(UpperCAmelCase)

						# Check that output attentions can also be changed via the config
						del inputs_dict["output_attentions"]
						SCREAMING_SNAKE_CASE_				:int				     =						True
						SCREAMING_SNAKE_CASE_				:str				     =						model_class(UpperCAmelCase)
						SCREAMING_SNAKE_CASE_				:Optional[int]				     =						model(self._prepare_for_class(UpperCAmelCase   ,  UpperCAmelCase))
						self.assertEqual(config.output_hidden_states   ,  UpperCAmelCase)
						check_encoder_attentions_output(UpperCAmelCase)

						# Check attention is always last and order is fine
						SCREAMING_SNAKE_CASE_				:Any				     =						True
						SCREAMING_SNAKE_CASE_				:int				     =						True
						SCREAMING_SNAKE_CASE_				:int				     =						model_class(UpperCAmelCase)
						SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						model(self._prepare_for_class(UpperCAmelCase   ,  UpperCAmelCase))

						self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)   ,  len(UpperCAmelCase))
						self.assertEqual(model.config.output_hidden_states   ,  UpperCAmelCase)
						check_encoder_attentions_output(UpperCAmelCase)




@require_tf
class 						_UpperCAmelCase      (     unittest.TestCase							):






		@slow
		def _snake_case		(						self				:     str):
				SCREAMING_SNAKE_CASE_				:Tuple				     =						TFConvBertModel.from_pretrained("YituTech/conv-bert-base")
				SCREAMING_SNAKE_CASE_				:str				     =						tf.constant([[0, 1, 2, 3, 4, 5]])
				SCREAMING_SNAKE_CASE_				:Tuple				     =						model(UpperCAmelCase)[0]

				SCREAMING_SNAKE_CASE_				:Tuple				     =						[1, 6, 7_68]
				self.assertEqual(output.shape   ,  UpperCAmelCase)

				SCREAMING_SNAKE_CASE_				:str				     =						tf.constant(
				    [
				        [
				            [-0.03475493, -0.4686034, -0.30638832],
				            [0.22637248, -0.26988646, -0.7423424],
				            [0.10324868, -0.45013508, -0.58280784],
				        ]
				    ])
				tf.debugging.assert_near(output[:, :3, :3]   ,  UpperCAmelCase   ,  atol=1E-4)










import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)



class 						_UpperCAmelCase      (     unittest.TestCase							):






		def _snake_case		(						self				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						["a", "b", "c"]

				# Defaults to last layer if both are None
				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Dict				     =						get_aligned_output_features_output_indices(UpperCAmelCase   ,  UpperCAmelCase   ,  UpperCAmelCase)
				self.assertEqual(UpperCAmelCase   ,  ["c"])
				self.assertEqual(UpperCAmelCase   ,  [2])

				# Out indices set to match out features
				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Tuple				     =						get_aligned_output_features_output_indices(["a", "c"]   ,  UpperCAmelCase   ,  UpperCAmelCase)
				self.assertEqual(UpperCAmelCase   ,  ["a", "c"])
				self.assertEqual(UpperCAmelCase   ,  [0, 2])

				# Out features set to match out indices
				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:int				     =						get_aligned_output_features_output_indices(UpperCAmelCase   ,  [0, 2]   ,  UpperCAmelCase)
				self.assertEqual(UpperCAmelCase   ,  ["a", "c"])
				self.assertEqual(UpperCAmelCase   ,  [0, 2])

				# Out features selected from negative indices
				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Any				     =						get_aligned_output_features_output_indices(UpperCAmelCase   ,  [-3, -1]   ,  UpperCAmelCase)
				self.assertEqual(UpperCAmelCase   ,  ["a", "c"])
				self.assertEqual(UpperCAmelCase   ,  [-3, -1])






		def _snake_case		(						self				:     str):
				# Stage names must be set
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(["a", "b"]   ,  (0, 1)   ,  UpperCAmelCase)

				# Out features must be a list
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(("a", "b")   ,  (0, 1)   ,  ["a", "b"])

				# Out features must be a subset of stage names
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(["a", "b"]   ,  (0, 1)   ,  ["a"])

				# Out indices must be a list or tuple
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(UpperCAmelCase   ,  0   ,  ["a", "b"])

				# Out indices must be a subset of stage names
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(UpperCAmelCase   ,  (0, 1)   ,  ["a"])

				# Out features and out indices must be the same length
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(["a", "b"]   ,  (0,)   ,  ["a", "b", "c"])

				# Out features should match out indices
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(["a", "b"]   ,  (0, 2)   ,  ["a", "b", "c"])

				# Out features and out indices should be in order
				with self.assertRaises(UpperCAmelCase):
						verify_out_features_out_indices(["b", "a"]   ,  (0, 1)   ,  ["a", "b"])

				# Check passes with valid inputs
				verify_out_features_out_indices(["a", "b", "d"]   ,  (0, 1, -1)   ,  ["a", "b", "c", "d"])







		def _snake_case		(						self				:     Tuple):
				SCREAMING_SNAKE_CASE_				:int				     =						BackboneMixin()

				SCREAMING_SNAKE_CASE_				:List[str]				     =						["a", "b", "c"]
				SCREAMING_SNAKE_CASE_				:str				     =						["a", "c"]
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						[0, 2]

				# Check that the output features and indices are set correctly
				self.assertEqual(backbone.out_features   ,  ["a", "c"])
				self.assertEqual(backbone.out_indices   ,  [0, 2])

				# Check out features and indices are updated correctly
				SCREAMING_SNAKE_CASE_				:Tuple				     =						["a", "b"]
				self.assertEqual(backbone.out_features   ,  ["a", "b"])
				self.assertEqual(backbone.out_indices   ,  [0, 1])

				SCREAMING_SNAKE_CASE_				:List[str]				     =						[-3, -1]
				self.assertEqual(backbone.out_features   ,  ["a", "c"])
				self.assertEqual(backbone.out_indices   ,  [-3, -1])






