





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     __snake_case       :





       '''simple docstring'''







       def __init__(  self :    Dict							,				A :    Optional[Any]							,				A :    Tuple=13							,				A :    Union[str, Any]=7							,				A :    Optional[Any]=True							,				A :    List[Any]=True							,				A :    Tuple=True							,				A :    List[Any]=True							,				A :    str=99							,				A :    Union[str, Any]=32							,				A :    List[Any]=2							,				A :    Any=4							,				A :    Tuple=37							,				A :    Any="gelu"							,				A :    List[Any]=0.1							,				A :    str=0.1							,				A :    List[str]=512							,				A :    Dict=16							,				A :    Union[str, Any]=2							,				A :    Optional[int]=0.02							,				A :    Dict=3							,				A :    int=4							,				A :    Optional[Any]=None							,				):
         __snake_case:						List[Any]   =						parent
         __snake_case:						int   =						13
         __snake_case:						Optional[Any]   =						7
         __snake_case:						Dict   =						True
         __snake_case:						Dict   =						True
         __snake_case:						str   =						True
         __snake_case:						List[str]   =						True
         __snake_case:						Tuple   =						99
         __snake_case:						Any   =						384
         __snake_case:						Tuple   =						2
         __snake_case:						int   =						4
         __snake_case:						Optional[int]   =						37
         __snake_case:						str   =						'''gelu'''
         __snake_case:						Any   =						0.1
         __snake_case:						Dict   =						0.1
         __snake_case:						Tuple   =						512
         __snake_case:						int   =						16
         __snake_case:						int   =						2
         __snake_case:						Any   =						0.02
         __snake_case:						Optional[Any]   =						3
         __snake_case:						Any   =						4
         __snake_case:						Any   =						128
         __snake_case:						List[str]   =						2
         __snake_case:						List[str]   =						9
         __snake_case:						Optional[int]   =						1
         __snake_case:						List[Any]   =						None




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

         __snake_case:						Optional[int]   =						None
         if self.use_input_mask:
           __snake_case:						Dict   =						random_attention_mask([self.batch_size, self.seq_length]		)

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

         __snake_case:						str   =						None
         __snake_case:						Union[str, Any]   =						None
         __snake_case:						Dict   =						None
         if self.use_labels:
           __snake_case:						str   =						ids_tensor([self.batch_size]							,				self.type_sequence_label_size		)
           __snake_case:						List[str]   =						ids_tensor([self.batch_size, self.seq_length]							,				self.num_labels		)
           __snake_case:						Tuple   =						ids_tensor([self.batch_size]							,				self.num_choices		)

         __snake_case:						Any   =						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=A							,				)

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




       def 							UpperCAmelCase__ (  self :    Tuple							,				A :    List[Any]							,				A :    str							,				A :    int							,				A :    List[str]							,				A :    str							,				A :    List[Any]							,				A :    Tuple		):
         __snake_case:						Tuple   =						TFConvBertModel(config=A		)
         __snake_case:						str   =						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

         __snake_case:						Any   =						[input_ids, input_mask]
         __snake_case:						int   =						model(A		)

         __snake_case:						int   =						model(A		)

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




       def 							UpperCAmelCase__ (  self :    str							,				A :    List[str]							,				A :    List[Any]							,				A :    Union[str, Any]							,				A :    Optional[Any]							,				A :    Union[str, Any]							,				A :    Optional[Any]							,				A :    int		):
         __snake_case:						Any   =						TFConvBertForMaskedLM(config=A		)
         __snake_case:						List[str]   =						{
             '''input_ids''': input_ids,
             '''attention_mask''': input_mask,
             '''token_type_ids''': token_type_ids,
         }
         __snake_case:						Tuple   =						model(A		)
         self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.seq_length, self.vocab_size)		)




       def 							UpperCAmelCase__ (  self :    Dict							,				A :    Union[str, Any]							,				A :    Any							,				A :    int							,				A :    Tuple							,				A :    Dict							,				A :    Optional[int]							,				A :    Optional[int]		):
         __snake_case:						str   =						self.num_labels
         __snake_case:						Optional[int]   =						TFConvBertForSequenceClassification(config=A		)
         __snake_case:						str   =						{
             '''input_ids''': input_ids,
             '''attention_mask''': input_mask,
             '''token_type_ids''': token_type_ids,
         }

         __snake_case:						Any   =						model(A		)
         self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.num_labels)		)




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




       def 							UpperCAmelCase__ (  self :    List[Any]							,				A :    Any							,				A :    Any							,				A :    Optional[int]							,				A :    Any							,				A :    Tuple							,				A :    str							,				A :    List[str]		):
         __snake_case:						Optional[Any]   =						self.num_labels
         __snake_case:						Any   =						TFConvBertForTokenClassification(config=A		)
         __snake_case:						str   =						{
             '''input_ids''': input_ids,
             '''attention_mask''': input_mask,
             '''token_type_ids''': token_type_ids,
         }
         __snake_case:						Dict   =						model(A		)
         self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.seq_length, self.num_labels)		)




       def 							UpperCAmelCase__ (  self :    str							,				A :    Union[str, Any]							,				A :    Dict							,				A :    int							,				A :    int							,				A :    Optional[Any]							,				A :    Optional[int]							,				A :    List[Any]		):
         __snake_case:						Union[str, Any]   =						TFConvBertForQuestionAnswering(config=A		)
         __snake_case:						Optional[Any]   =						{
             '''input_ids''': input_ids,
             '''attention_mask''': input_mask,
             '''token_type_ids''': token_type_ids,
         }

         __snake_case:						List[str]   =						model(A		)
         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 							UpperCAmelCase__ (  self :    Dict		):
         __snake_case:						Any   =						self.prepare_config_and_inputs()
         (
             __snake_case
         ):						Any   =						config_and_inputs
         __snake_case:						List[str]   =						{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
         return config, inputs_dict







@require_tf
class     __snake_case       (							__lowerCamelCase   ,			__lowerCamelCase   ,			unittest.TestCase			):





       '''simple docstring'''







       lowerCAmelCase__      		=     (
           (
               TFConvBertModel,
               TFConvBertForMaskedLM,
               TFConvBertForQuestionAnswering,
               TFConvBertForSequenceClassification,
               TFConvBertForTokenClassification,
               TFConvBertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       lowerCAmelCase__      		=     (
           {
               "feature-extraction": TFConvBertModel,
               "fill-mask": TFConvBertForMaskedLM,
               "question-answering": TFConvBertForQuestionAnswering,
               "text-classification": TFConvBertForSequenceClassification,
               "token-classification": TFConvBertForTokenClassification,
               "zero-shot": TFConvBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       lowerCAmelCase__      		=     False
       lowerCAmelCase__      		=     False
       lowerCAmelCase__      		=     False




       def 							UpperCAmelCase__ (  self :    Any		):
         __snake_case:						List[str]   =						TFConvBertModelTester(self		)
         __snake_case:						Optional[int]   =						ConfigTester(self							,				config_class=A							,				hidden_size=37		)




       def 							UpperCAmelCase__ (  self :    List[Any]		):
         self.config_tester.run_common_tests()




       def 							UpperCAmelCase__ (  self :    Any		):
         __snake_case:						Any   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A		)




       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						str   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*A		)




       def 							UpperCAmelCase__ (  self :    Union[str, Any]		):
         __snake_case:						List[Any]   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*A		)




       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						str   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*A		)




       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         __snake_case:						Optional[int]   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*A		)




       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         __snake_case:						str   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*A		)




       @slow
       def 							UpperCAmelCase__ (  self :    str		):
         __snake_case:						Tuple   =						self.model_tester.prepare_config_and_inputs_for_common()
         __snake_case:						Union[str, Any]   =						True
         __snake_case:						Optional[int]   =						True

         if hasattr(A							,				"""use_cache"""		):
           __snake_case:						Union[str, Any]   =						True

         __snake_case:						Tuple   =						getattr(self.model_tester							,				"""encoder_seq_length"""							,				self.model_tester.seq_length		)
         __snake_case:						Any   =						getattr(self.model_tester							,				"""key_length"""							,				A		)

         for model_class in self.all_model_classes:
           __snake_case:						Optional[int]   =						self._prepare_for_class(A							,				A		)
           __snake_case:						Any   =						model_class(A		)
           __snake_case:						List[str]   =						len(model(A		)		)

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

             if self.is_encoder_decoder:
               __snake_case:						Union[str, Any]   =						outputs['''encoder_hidden_states''']
               __snake_case:						Any   =						outputs['''encoder_attentions''']
             else:
               __snake_case:						Optional[Any]   =						outputs['''hidden_states''']
               __snake_case:						Dict   =						outputs['''attentions''']

             self.assertEqual(len(A		)							,				A		)

             __snake_case:						Optional[Any]   =						getattr(
                 self.model_tester							,				"""expected_num_hidden_layers"""							,				self.model_tester.num_hidden_layers + 1		)

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

             self.assertEqual(len(A		)							,				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 							UpperCAmelCase__ (  self :    List[str]		):
         __snake_case:						str   =						TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""		)
         self.assertIsNotNone(A		)






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

         def check_decoder_attentions_output(A :    int		):
           __snake_case:						List[str]   =						len(A		)
           self.assertEqual(out_len % 2							,				0		)
           __snake_case:						Optional[Any]   =						outputs.decoder_attentions
           self.assertEqual(len(A		)							,				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(A :    Optional[Any]		):
           __snake_case:						int   =						[
               t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else 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 / 2, encoder_seq_length, encoder_key_length]							,				)

         for model_class in self.all_model_classes:
           __snake_case:						Dict   =						True
           __snake_case:						str   =						False
           __snake_case:						Optional[Any]   =						model_class(A		)
           __snake_case:						Dict   =						model(self._prepare_for_class(A							,				A		)		)
           __snake_case:						int   =						len(A		)
           self.assertEqual(config.output_hidden_states							,				A		)
           check_encoder_attentions_output(A		)

           if self.is_encoder_decoder:
             __snake_case:						str   =						model_class(A		)
             __snake_case:						List[str]   =						model(self._prepare_for_class(A							,				A		)		)
             self.assertEqual(config.output_hidden_states							,				A		)
             check_decoder_attentions_output(A		)

           # Check that output attentions can also be changed via the config
           del inputs_dict["output_attentions"]
           __snake_case:						List[Any]   =						True
           __snake_case:						Dict   =						model_class(A		)
           __snake_case:						Optional[Any]   =						model(self._prepare_for_class(A							,				A		)		)
           self.assertEqual(config.output_hidden_states							,				A		)
           check_encoder_attentions_output(A		)

           # Check attention is always last and order is fine
           __snake_case:						List[Any]   =						True
           __snake_case:						List[Any]   =						True
           __snake_case:						Dict   =						model_class(A		)
           __snake_case:						List[Any]   =						model(self._prepare_for_class(A							,				A		)		)

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







@require_tf
class     __snake_case       (							unittest.TestCase			):





       '''simple docstring'''







       @slow
       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         __snake_case:						Union[str, Any]   =						TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""		)
         __snake_case:						Tuple   =						tf.constant([[0, 1, 2, 3, 4, 5]]		)
         __snake_case:						int   =						model(A		)[0]

         __snake_case:						Dict   =						[1, 6, 768]
         self.assertEqual(output.shape							,				A		)

         __snake_case:						Union[str, Any]   =						tf.constant(
             [
                 [
                     [-0.0347_5493, -0.468_6034, -0.3063_8832],
                     [0.2263_7248, -0.2698_8646, -0.742_3424],
                     [0.1032_4868, -0.4501_3508, -0.5828_0784],
                 ]
             ]		)
         tf.debugging.assert_near(output[:, :3, :3]							,				A							,				atol=1E-4		)













from typing import List, Optional, Union

import numpy as np
import PIL.Image

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


UpperCAmelCase_     							= logging.get_logger(__name__)






class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   a						:							str   		=		["pixel_values"]




   def __init__(    self,		__magic_name__ = True,		__magic_name__ = 32,		__magic_name__=PILImageResampling.BILINEAR,		__magic_name__ = True,		**__magic_name__,		) ->      None:






       """simple docstring"""
       UpperCamelCase__							:				int          =      do_resize
       UpperCamelCase__							:				Tuple          =      do_rescale
       UpperCamelCase__							:				Any          =      size_divisor
       UpperCamelCase__							:				List[Any]          =      resample
       super().__init__(**__magic_name__							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__ = None,		**__magic_name__							) ->      np.ndarray:






       """simple docstring"""
       UpperCamelCase__   ,UpperCamelCase__							:				List[Any]          =      get_image_size(__magic_name__							)
       # Rounds the height and width down to the closest multiple of size_divisor
       UpperCamelCase__							:				Any          =      height // size_divisor * size_divisor
       UpperCamelCase__							:				Optional[int]          =      width // size_divisor * size_divisor
       UpperCamelCase__							:				str          =      resize(__magic_name__,		(new_h, new_w),		resample=__magic_name__,		data_format=__magic_name__,		**__magic_name__							)
       return image




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__ = None,		**__magic_name__							) ->      np.ndarray:






       """simple docstring"""
       return rescale(image=__magic_name__,		scale=__magic_name__,		data_format=__magic_name__,		**__magic_name__							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__ = None,		__magic_name__ = None,		__magic_name__=None,		__magic_name__ = None,		__magic_name__ = None,		__magic_name__ = ChannelDimension.FIRST,		**__magic_name__,		) ->      BatchFeature:






       """simple docstring"""
       UpperCamelCase__							:				str          =      do_resize if do_resize is not None else self.do_resize
       UpperCamelCase__							:				Union[str, Any]          =      do_rescale if do_rescale is not None else self.do_rescale
       UpperCamelCase__							:				Any          =      size_divisor if size_divisor is not None else self.size_divisor
       UpperCamelCase__							:				str          =      resample if resample is not None else self.resample

       if do_resize and size_divisor is None:
           raise ValueError('''size_divisor is required for resizing'''							)

       UpperCamelCase__							:				Union[str, Any]          =      make_list_of_images(__magic_name__							)

       if not valid_images(__magic_name__							):
           raise ValueError('''Invalid image(s)'''							)

       # All transformations expect numpy arrays.
       UpperCamelCase__							:				Optional[Any]          =      [to_numpy_array(__magic_name__							) for img in images]

       if do_resize:
           UpperCamelCase__							:				str          =      [self.resize(__magic_name__,		size_divisor=__magic_name__,		resample=__magic_name__							) for image in images]

       if do_rescale:
           UpperCamelCase__							:				List[Any]          =      [self.rescale(__magic_name__,		scale=1 / 255							) for image in images]

       UpperCamelCase__							:				Optional[Any]          =      [to_channel_dimension_format(__magic_name__,		__magic_name__							) for image in images]

       UpperCamelCase__							:				Tuple          =      {'''pixel_values''': images}
       return BatchFeature(data=__magic_name__,		tensor_type=__magic_name__							)

