


'''simple docstring'''






import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import YolosImageProcessor
class     __UpperCamelCase						(    unittest.TestCase				):





							def __init__(	self		:int	,_UpperCamelCase		:List[Any]	,_UpperCamelCase		:str=7	,_UpperCamelCase		:str=3	,_UpperCamelCase		:int=3_0	,_UpperCamelCase		:str=4_0_0	,_UpperCamelCase		:str=True	,_UpperCamelCase		:List[str]=None	,_UpperCamelCase		:Tuple=True	,_UpperCamelCase		:Tuple=[0.5, 0.5, 0.5]	,_UpperCamelCase		:Optional[Any]=[0.5, 0.5, 0.5]	,_UpperCamelCase		:Union[str, Any]=True	,_UpperCamelCase		:Dict=1 / 2_5_5	,_UpperCamelCase		:Optional[Any]=True	,):
									snake_case_		:				int      							=    size if size is not None else {'shortest_edge': 1_8, 'longest_edge': 1_3_3_3}
									snake_case_		:				List[Any]      							=    parent
									snake_case_		:				Optional[Any]      							=    batch_size
									snake_case_		:				Tuple      							=    num_channels
									snake_case_		:				str      							=    min_resolution
									snake_case_		:				List[Any]      							=    max_resolution
									snake_case_		:				int      							=    do_resize
									snake_case_		:				Any      							=    size
									snake_case_		:				int      							=    do_normalize
									snake_case_		:				Union[str, Any]      							=    image_mean
									snake_case_		:				List[str]      							=    image_std
									snake_case_		:				Dict      							=    do_rescale
									snake_case_		:				List[str]      							=    rescale_factor
									snake_case_		:				Union[str, Any]      							=    do_pad





							def 		a__ (	self		:Dict      ):
									return {
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_normalize": self.do_normalize,
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_rescale": self.do_rescale,
									    "rescale_factor": self.rescale_factor,
									    "do_pad": self.do_pad,
									}





							def 		a__ (	self		:Optional[Any]	,_UpperCamelCase		:str	,_UpperCamelCase		:Optional[int]=False      ):






									if not batched:
											snake_case_		:				str      							=    image_inputs[0]
											if isinstance(_UpperCAmelCase	,Image.Image      ):
													snake_case_		:				Tuple      							=    image.size
											else:
													snake_case_		:				int      							=    image.shape[1], image.shape[2]
											if w < h:
													snake_case_		:				Optional[int]      							=    int(self.size["""shortest_edge"""] * h / w      )
													snake_case_		:				int      							=    self.size['shortest_edge']
											elif w > h:
													snake_case_		:				List[Any]      							=    self.size['shortest_edge']
													snake_case_		:				Dict      							=    int(self.size["""shortest_edge"""] * w / h      )
											else:
													snake_case_		:				Tuple      							=    self.size['shortest_edge']
													snake_case_		:				List[Any]      							=    self.size['shortest_edge']

									else:
											snake_case_		:				List[Any]      							=    []
											for image in image_inputs:
													snake_case_		:				Union[str, Any]      							=    self.get_expected_values([image]      )
													expected_values.append((expected_height, expected_width)      )
											snake_case_		:				Union[str, Any]      							=    max(_UpperCAmelCase	,key=lambda _UpperCamelCase      : item[0]      )[0]
											snake_case_		:				Optional[int]      							=    max(_UpperCAmelCase	,key=lambda _UpperCamelCase      : item[1]      )[1]

									return expected_height, expected_width





@require_torch
@require_vision
class     __UpperCamelCase						(    a__    ,	unittest.TestCase				):
							lowercase      :		str 		=	YolosImageProcessor if is_vision_available() else None





							def 		a__ (	self		:Any      ):
									snake_case_		:				List[Any]      							=    YolosImageProcessingTester(self      )





							@property
							def 		a__ (	self		:Dict      ):
									return self.image_processor_tester.prepare_image_processor_dict()





							def 		a__ (	self		:Optional[Any]      ):
									snake_case_		:				List[Any]      							=    self.image_processing_class(**self.image_processor_dict      )
									self.assertTrue(hasattr(_UpperCAmelCase	,"""image_mean"""      )      )
									self.assertTrue(hasattr(_UpperCAmelCase	,"""image_std"""      )      )
									self.assertTrue(hasattr(_UpperCAmelCase	,"""do_normalize"""      )      )
									self.assertTrue(hasattr(_UpperCAmelCase	,"""do_resize"""      )      )
									self.assertTrue(hasattr(_UpperCAmelCase	,"""size"""      )      )





							def 		a__ (	self		:Tuple      ):
									snake_case_		:				List[Any]      							=    self.image_processing_class.from_dict(self.image_processor_dict      )
									self.assertEqual(image_processor.size	,{"""shortest_edge""": 1_8, """longest_edge""": 1_3_3_3}      )
									self.assertEqual(image_processor.do_pad	,_UpperCAmelCase      )

									snake_case_		:				Any      							=    self.image_processing_class.from_dict(
									    self.image_processor_dict	,size=4_2	,max_size=8_4	,pad_and_return_pixel_mask=_UpperCAmelCase      )
									self.assertEqual(image_processor.size	,{"""shortest_edge""": 4_2, """longest_edge""": 8_4}      )
									self.assertEqual(image_processor.do_pad	,_UpperCAmelCase      )





							def 		a__ (	self		:int      ):
									pass





							def 		a__ (	self		:Optional[int]      ):
									snake_case_		:				Optional[int]      							=    self.image_processing_class(**self.image_processor_dict      )
									# create random PIL images
									snake_case_		:				List[Any]      							=    prepare_image_inputs(self.image_processor_tester	,equal_resolution=_UpperCAmelCase      )
									for image in image_inputs:
											self.assertIsInstance(_UpperCAmelCase	,Image.Image      )

									# Test not batched input
									snake_case_		:				Dict      							=    image_processing(image_inputs[0]	,return_tensors="""pt"""      ).pixel_values

									snake_case_		:				List[Any]      							=    self.image_processor_tester.get_expected_values(_UpperCAmelCase      )

									self.assertEqual(
									    encoded_images.shape	,(1, self.image_processor_tester.num_channels, expected_height, expected_width)	,)

									# Test batched
									snake_case_		:				str      							=    self.image_processor_tester.get_expected_values(_UpperCAmelCase	,batched=_UpperCAmelCase      )

									snake_case_		:				List[Any]      							=    image_processing(_UpperCAmelCase	,return_tensors="""pt"""      ).pixel_values
									self.assertEqual(
									    encoded_images.shape	,(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        expected_height,
									        expected_width,
									    )	,)





							def 		a__ (	self		:Union[str, Any]      ):
									snake_case_		:				Optional[int]      							=    self.image_processing_class(**self.image_processor_dict      )
									# create random numpy tensors
									snake_case_		:				List[str]      							=    prepare_image_inputs(self.image_processor_tester	,equal_resolution=_UpperCAmelCase	,numpify=_UpperCAmelCase      )
									for image in image_inputs:
											self.assertIsInstance(_UpperCAmelCase	,np.ndarray      )

									# Test not batched input
									snake_case_		:				Union[str, Any]      							=    image_processing(image_inputs[0]	,return_tensors="""pt"""      ).pixel_values

									snake_case_		:				Tuple      							=    self.image_processor_tester.get_expected_values(_UpperCAmelCase      )

									self.assertEqual(
									    encoded_images.shape	,(1, self.image_processor_tester.num_channels, expected_height, expected_width)	,)

									# Test batched
									snake_case_		:				Optional[Any]      							=    image_processing(_UpperCAmelCase	,return_tensors="""pt"""      ).pixel_values

									snake_case_		:				List[Any]      							=    self.image_processor_tester.get_expected_values(_UpperCAmelCase	,batched=_UpperCAmelCase      )

									self.assertEqual(
									    encoded_images.shape	,(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        expected_height,
									        expected_width,
									    )	,)





							def 		a__ (	self		:List[str]      ):
									snake_case_		:				Any      							=    self.image_processing_class(**self.image_processor_dict      )
									# create random PyTorch tensors
									snake_case_		:				str      							=    prepare_image_inputs(self.image_processor_tester	,equal_resolution=_UpperCAmelCase	,torchify=_UpperCAmelCase      )
									for image in image_inputs:
											self.assertIsInstance(_UpperCAmelCase	,torch.Tensor      )

									# Test not batched input
									snake_case_		:				Any      							=    image_processing(image_inputs[0]	,return_tensors="""pt"""      ).pixel_values

									snake_case_		:				int      							=    self.image_processor_tester.get_expected_values(_UpperCAmelCase      )

									self.assertEqual(
									    encoded_images.shape	,(1, self.image_processor_tester.num_channels, expected_height, expected_width)	,)

									# Test batched
									snake_case_		:				Any      							=    image_processing(_UpperCAmelCase	,return_tensors="""pt"""      ).pixel_values

									snake_case_		:				Optional[Any]      							=    self.image_processor_tester.get_expected_values(_UpperCAmelCase	,batched=_UpperCAmelCase      )

									self.assertEqual(
									    encoded_images.shape	,(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        expected_height,
									        expected_width,
									    )	,)





							def 		a__ (	self		:str      ):
									snake_case_		:				Any      							=    self.image_processing_class(**self.image_processor_dict      )
									snake_case_		:				Dict      							=    self.image_processing_class(do_resize=_UpperCAmelCase	,do_normalize=_UpperCAmelCase	,do_rescale=_UpperCAmelCase      )
									# create random PyTorch tensors
									snake_case_		:				str      							=    prepare_image_inputs(self.image_processor_tester	,equal_resolution=_UpperCAmelCase	,torchify=_UpperCAmelCase      )
									for image in image_inputs:
											self.assertIsInstance(_UpperCAmelCase	,torch.Tensor      )

									# Test whether the method "pad" and calling the image processor return the same tensors
									snake_case_		:				Any      							=    image_processing_a.pad(_UpperCAmelCase	,return_tensors="""pt"""      )
									snake_case_		:				Optional[int]      							=    image_processing_a(_UpperCAmelCase	,return_tensors="""pt"""      )

									self.assertTrue(
									    torch.allclose(encoded_images_with_method["""pixel_values"""]	,encoded_images["""pixel_values"""]	,atol=1E-4      )      )





							@slow
							def 		a__ (	self		:int      ):
									snake_case_		:				str      							=    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
									with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt"""	,"""r"""      ) as f:
											snake_case_		:				int      							=    json.loads(f.read()      )

									snake_case_		:				Any      							=    {'image_id': 3_9_7_6_9, 'annotations': target}

									# encode them
									snake_case_		:				int      							=    YolosImageProcessor.from_pretrained("""hustvl/yolos-small"""      )
									snake_case_		:				str      							=    image_processing(images=_UpperCAmelCase	,annotations=_UpperCAmelCase	,return_tensors="""pt"""      )

									# verify pixel values
									snake_case_		:				List[Any]      							=    torch.Size([1, 3, 8_0_0, 1_0_6_6]      )
									self.assertEqual(encoding["""pixel_values"""].shape	,_UpperCAmelCase      )

									snake_case_		:				List[str]      							=    torch.tensor([0.27_96, 0.31_38, 0.34_81]      )
									self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]	,_UpperCAmelCase	,atol=1E-4      )      )

									# verify area
									snake_case_		:				Union[str, Any]      							=    torch.tensor([58_87.96_00, 1_12_50.20_61, 48_93_53.84_38, 83_71_22.75_00, 14_79_67.51_56, 16_57_32.34_38]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]	,_UpperCAmelCase      )      )
									# verify boxes
									snake_case_		:				Tuple      							=    torch.Size([6, 4]      )
									self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape	,_UpperCAmelCase      )
									snake_case_		:				Optional[int]      							=    torch.tensor([0.55_03, 0.27_65, 0.06_04, 0.22_15]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]	,_UpperCAmelCase	,atol=1E-3      )      )
									# verify image_id
									snake_case_		:				Union[str, Any]      							=    torch.tensor([3_9_7_6_9]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]	,_UpperCAmelCase      )      )
									# verify is_crowd
									snake_case_		:				List[str]      							=    torch.tensor([0, 0, 0, 0, 0, 0]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]	,_UpperCAmelCase      )      )
									# verify class_labels
									snake_case_		:				int      							=    torch.tensor([7_5, 7_5, 6_3, 6_5, 1_7, 1_7]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]	,_UpperCAmelCase      )      )
									# verify orig_size
									snake_case_		:				Union[str, Any]      							=    torch.tensor([4_8_0, 6_4_0]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]	,_UpperCAmelCase      )      )
									# verify size
									snake_case_		:				List[str]      							=    torch.tensor([8_0_0, 1_0_6_6]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]	,_UpperCAmelCase      )      )





							@slow
							def 		a__ (	self		:Optional[Any]      ):
									snake_case_		:				Optional[int]      							=    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
									with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"""	,"""r"""      ) as f:
											snake_case_		:				Any      							=    json.loads(f.read()      )

									snake_case_		:				Optional[int]      							=    {'file_name': '000000039769.png', 'image_id': 3_9_7_6_9, 'segments_info': target}

									snake_case_		:				Dict      							=    pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic"""      )

									# encode them
									snake_case_		:				List[Any]      							=    YolosImageProcessor(format="""coco_panoptic"""      )
									snake_case_		:				Optional[int]      							=    image_processing(images=_UpperCAmelCase	,annotations=_UpperCAmelCase	,masks_path=_UpperCAmelCase	,return_tensors="""pt"""      )

									# verify pixel values
									snake_case_		:				str      							=    torch.Size([1, 3, 8_0_0, 1_0_6_6]      )
									self.assertEqual(encoding["""pixel_values"""].shape	,_UpperCAmelCase      )

									snake_case_		:				int      							=    torch.tensor([0.27_96, 0.31_38, 0.34_81]      )
									self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]	,_UpperCAmelCase	,atol=1E-4      )      )

									# verify area
									snake_case_		:				Union[str, Any]      							=    torch.tensor([14_79_79.68_75, 16_55_27.04_69, 48_46_38.59_38, 1_12_92.93_75, 58_79.65_62, 76_34.11_47]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]	,_UpperCAmelCase      )      )
									# verify boxes
									snake_case_		:				Optional[Any]      							=    torch.Size([6, 4]      )
									self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape	,_UpperCAmelCase      )
									snake_case_		:				Union[str, Any]      							=    torch.tensor([0.26_25, 0.54_37, 0.46_88, 0.86_25]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]	,_UpperCAmelCase	,atol=1E-3      )      )
									# verify image_id
									snake_case_		:				Union[str, Any]      							=    torch.tensor([3_9_7_6_9]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]	,_UpperCAmelCase      )      )
									# verify is_crowd
									snake_case_		:				Optional[int]      							=    torch.tensor([0, 0, 0, 0, 0, 0]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]	,_UpperCAmelCase      )      )
									# verify class_labels
									snake_case_		:				Optional[int]      							=    torch.tensor([1_7, 1_7, 6_3, 7_5, 7_5, 9_3]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]	,_UpperCAmelCase      )      )
									# verify masks
									snake_case_		:				Tuple      							=    8_2_2_8_7_3
									self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item()	,_UpperCAmelCase      )
									# verify orig_size
									snake_case_		:				str      							=    torch.tensor([4_8_0, 6_4_0]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]	,_UpperCAmelCase      )      )
									# verify size
									snake_case_		:				int      							=    torch.tensor([8_0_0, 1_0_6_6]      )
									self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]	,_UpperCAmelCase      )      )






'''simple docstring'''




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


     def __init__(		self		,      _UpperCAmelCase		,      _UpperCAmelCase=13		,      _UpperCAmelCase=7		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=99		,      _UpperCAmelCase=32		,      _UpperCAmelCase=2		,      _UpperCAmelCase=4		,      _UpperCAmelCase=37		,      _UpperCAmelCase="gelu"		,      _UpperCAmelCase=0.1		,      _UpperCAmelCase=0.1		,      _UpperCAmelCase=512		,      _UpperCAmelCase=16		,      _UpperCAmelCase=2		,      _UpperCAmelCase=0.02		,      _UpperCAmelCase=3		,      _UpperCAmelCase=4		,      _UpperCAmelCase=None		,      ):

          '''simple docstring'''






          __A		:					Optional[int]						=			parent
          __A		:					str						=			13
          __A		:					List[Any]						=			7
          __A		:					List[str]						=			True
          __A		:					str						=			True
          __A		:					Optional[Any]						=			True
          __A		:					int						=			True
          __A		:					Dict						=			99
          __A		:					Dict						=			384
          __A		:					Any						=			2
          __A		:					int						=			4
          __A		:					Optional[Any]						=			37
          __A		:					Optional[int]						=			'gelu'
          __A		:					Dict						=			0.1
          __A		:					Optional[int]						=			0.1
          __A		:					Any						=			512
          __A		:					int						=			16
          __A		:					List[str]						=			2
          __A		:					str						=			0.02
          __A		:					Any						=			3
          __A		:					str						=			4
          __A		:					Union[str, Any]						=			128
          __A		:					int						=			2
          __A		:					List[Any]						=			9
          __A		:					List[Any]						=			1
          __A		:					List[Any]						=			None


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Union[str, Any]						=			ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size)

          __A		:					str						=			None
          if self.use_input_mask:
               __A		:					List[Any]						=			random_attention_mask([self.batch_size, self.seq_length])

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

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

          __A		:					List[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=_UpperCAmelCase		,      )

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


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					int						=			TFConvBertModel(config=_UpperCAmelCase)
          __A		:					Optional[Any]						=			{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

          __A		:					Tuple						=			[input_ids, input_mask]
          __A		:					Any						=			model(_UpperCAmelCase)

          __A		:					Dict						=			model(_UpperCAmelCase)

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


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					str						=			TFConvBertForMaskedLM(config=_UpperCAmelCase)
          __A		:					str						=			{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          __A		:					str						=			model(_UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.vocab_size))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Optional[int]						=			self.num_labels
          __A		:					Any						=			TFConvBertForSequenceClassification(config=_UpperCAmelCase)
          __A		:					Optional[Any]						=			{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          __A		:					Dict						=			model(_UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






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


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					List[Any]						=			self.num_labels
          __A		:					List[Any]						=			TFConvBertForTokenClassification(config=_UpperCAmelCase)
          __A		:					str						=			{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          __A		:					int						=			model(_UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.num_labels))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Optional[Any]						=			TFConvBertForQuestionAnswering(config=_UpperCAmelCase)
          __A		:					Any						=			{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          __A		:					Union[str, Any]						=			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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Optional[int]						=			self.prepare_config_and_inputs()
          (
              (
              __A
          )					,(
              __A
          )					,(
              __A
          )					,(
              __A
          )					,(
              __A
          )					,(
              __A
          )					,(
              __A
          )					,
          )		:					Union[str, Any]						=			config_and_inputs
          __A		:					List[str]						=			{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
          return config, inputs_dict




@require_tf
class 					SCREAMING_SNAKE_CASE       (a__   ,						a__   ,						unittest.TestCase				):
     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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			TFConvBertModelTester(self)
          __A		:					str						=			ConfigTester(self		,      config_class=_UpperCAmelCase		,      hidden_size=37)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          self.config_tester.run_common_tests()


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Tuple						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					str						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Optional[Any]						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					List[Any]						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase)


     @slow
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A					,__A		:					Union[str, Any]						=			self.model_tester.prepare_config_and_inputs_for_common()
          __A		:					List[str]						=			True
          __A		:					List[str]						=			True

          if hasattr(_UpperCAmelCase		,      'use_cache'):
               __A		:					List[Any]						=			True

          __A		:					str						=			getattr(self.model_tester		,      'encoder_seq_length'		,      self.model_tester.seq_length)
          __A		:					Union[str, Any]						=			getattr(self.model_tester		,      'key_length'		,      _UpperCAmelCase)

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

               with tempfile.TemporaryDirectory() as tmpdirname:
                    model.save_pretrained(_UpperCAmelCase		,      saved_model=_UpperCAmelCase)
                    __A		:					Union[str, Any]						=			os.path.join(_UpperCAmelCase		,      'saved_model'		,      '1')
                    __A		:					Tuple						=			tf.keras.models.load_model(_UpperCAmelCase)
                    __A		:					str						=			model(_UpperCAmelCase)

                    if self.is_encoder_decoder:
                         __A		:					Optional[int]						=			outputs['encoder_hidden_states']
                         __A		:					str						=			outputs['encoder_attentions']
                    else:
                         __A		:					List[Any]						=			outputs['hidden_states']
                         __A		:					Optional[Any]						=			outputs['attentions']

                    self.assertEqual(len(_UpperCAmelCase)		,      _UpperCAmelCase)

                    __A		:					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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Dict						=			TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
          self.assertIsNotNone(_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A					,__A		:					Dict						=			self.model_tester.prepare_config_and_inputs_for_common()
          __A		:					Any						=			True
          __A		:					str						=			getattr(self.model_tester		,      'decoder_seq_length'		,      self.model_tester.seq_length)
          __A		:					Any						=			getattr(self.model_tester		,      'encoder_seq_length'		,      self.model_tester.seq_length)
          __A		:					int						=			getattr(self.model_tester		,      'key_length'		,      _UpperCAmelCase)
          __A		:					Tuple						=			getattr(self.model_tester		,      'key_length'		,      _UpperCAmelCase)

          def check_decoder_attentions_output(_UpperCAmelCase):
               __A		:					List[str]						=			len(_UpperCAmelCase)
               self.assertEqual(out_len % 2		,      0)
               __A		:					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):
               __A		:					str						=			[
                   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:
               __A		:					Dict						=			True
               __A		:					Any						=			False
               __A		:					str						=			model_class(_UpperCAmelCase)
               __A		:					List[str]						=			model(self._prepare_for_class(_UpperCAmelCase		,      _UpperCAmelCase))
               __A		:					List[str]						=			len(_UpperCAmelCase)
               self.assertEqual(config.output_hidden_states		,      _UpperCAmelCase)
               check_encoder_attentions_output(_UpperCAmelCase)

               if self.is_encoder_decoder:
                    __A		:					Union[str, Any]						=			model_class(_UpperCAmelCase)
                    __A		:					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"]
               __A		:					int						=			True
               __A		:					Tuple						=			model_class(_UpperCAmelCase)
               __A		:					Dict						=			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
               __A		:					Any						=			True
               __A		:					str						=			True
               __A		:					Union[str, Any]						=			model_class(_UpperCAmelCase)
               __A		:					Union[str, 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 					SCREAMING_SNAKE_CASE       (unittest.TestCase				):


     @slow
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Tuple						=			TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
          __A		:					str						=			tf.constant([[0, 1, 2, 3, 4, 5]])
          __A		:					Optional[int]						=			model(_UpperCAmelCase)[0]

          __A		:					List[Any]						=			[1, 6, 768]
          self.assertEqual(output.shape		,      _UpperCAmelCase)

          __A		:					Tuple						=			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)