






import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

	if is_vision_available():
		from transformers import MaskFormerImageProcessor

if is_vision_available():
	from PIL import Image
class  lowerCAmelCase_       :




							def __init__(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=2 ,							SCREAMING_SNAKE_CASE_     :   Dict=True ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=False ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=10 ,							SCREAMING_SNAKE_CASE_     :   str=3 ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=32 * 4 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=32 * 6 ,							SCREAMING_SNAKE_CASE_     :   List[Any]=4 ,							SCREAMING_SNAKE_CASE_     :   int=32 ,							):
											lowerCAmelCase__      			=						parent
											lowerCAmelCase__      			=						batch_size
											lowerCAmelCase__      			=						is_training
											lowerCAmelCase__      			=						use_auxiliary_loss
											lowerCAmelCase__      			=						num_queries
											lowerCAmelCase__      			=						num_channels
											lowerCAmelCase__      			=						min_size
											lowerCAmelCase__      			=						max_size
											lowerCAmelCase__      			=						num_labels
											lowerCAmelCase__      			=						mask_feature_size




							def 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]	).to(
											    UpperCamelCase_	)

											lowerCAmelCase__      			=						torch.ones([self.batch_size, self.min_size, self.max_size] ,							device=UpperCamelCase_	)

											lowerCAmelCase__      			=						(
											    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] ,							device=UpperCamelCase_	) > 0.5
											).float()
											lowerCAmelCase__      			=						(torch.rand((self.batch_size, self.num_labels) ,							device=UpperCamelCase_	) > 0.5).long()

											lowerCAmelCase__      			=						self.get_config()
											return config, pixel_values, pixel_mask, mask_labels, class_labels




							def 						__snake_case					(   self     :   Dict	):
											return MaskFormerConfig.from_backbone_and_decoder_configs(
											    backbone_config=SwinConfig(
											        depths=[1, 1, 1, 1] ,							) ,							decoder_config=DetrConfig(
											        decoder_ffn_dim=128 ,							num_queries=self.num_queries ,							decoder_attention_heads=2 ,							d_model=self.mask_feature_size ,							) ,							mask_feature_size=self.mask_feature_size ,							fpn_feature_size=self.mask_feature_size ,							num_channels=self.num_channels ,							num_labels=self.num_labels ,							)




							def 						__snake_case					(   self     :   Optional[Any]	):
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						self.prepare_config_and_inputs()
											lowerCAmelCase__      			=						{'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
											return config, inputs_dict




							def 						__snake_case					(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   Dict	):
											lowerCAmelCase__      			=						output.encoder_hidden_states
											lowerCAmelCase__      			=						output.pixel_decoder_hidden_states
											lowerCAmelCase__      			=						output.transformer_decoder_hidden_states

											self.parent.assertTrue(len(UpperCamelCase_	) ,							len(config.backbone_config.depths	)	)
											self.parent.assertTrue(len(UpperCamelCase_	) ,							len(config.backbone_config.depths	)	)
											self.parent.assertTrue(len(UpperCamelCase_	) ,							config.decoder_config.decoder_layers	)




							def 						__snake_case					(   self     :   str ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Tuple=False	):
											with torch.no_grad():
															lowerCAmelCase__      			=						MaskFormerModel(config=UpperCamelCase_	)
															model.to(UpperCamelCase_	)
															model.eval()

															lowerCAmelCase__      			=						model(pixel_values=UpperCamelCase_ ,							pixel_mask=UpperCamelCase_	)
															lowerCAmelCase__      			=						model(UpperCamelCase_ ,							output_hidden_states=UpperCamelCase_	)
											# the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
											# encoder and pixel decoder
											self.parent.assertEqual(
											    output.transformer_decoder_last_hidden_state.shape ,							(self.batch_size, self.num_queries, self.mask_feature_size) ,							)
											# let's ensure the other two hidden state exists
											self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None	)
											self.parent.assertTrue(output.encoder_last_hidden_state is not None	)

											if output_hidden_states:
															self.check_output_hidden_state(UpperCamelCase_ ,							UpperCamelCase_	)




							def 						__snake_case					(   self     :   int ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   List[str]	):
											lowerCAmelCase__      			=						MaskFormerForInstanceSegmentation(config=UpperCamelCase_	)
											model.to(UpperCamelCase_	)
											model.eval()

											def comm_check_on_output(SCREAMING_SNAKE_CASE_     :   Tuple	):
															# let's still check that all the required stuff is there
															self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None	)
															self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None	)
															self.parent.assertTrue(result.encoder_last_hidden_state is not None	)
															# okay, now we need to check the logits shape
															# due to the encoder compression, masks have a //4 spatial size
															self.parent.assertEqual(
															    result.masks_queries_logits.shape ,							(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) ,							)
															# + 1 for null class
															self.parent.assertEqual(
															    result.class_queries_logits.shape ,							(self.batch_size, self.num_queries, self.num_labels + 1)	)

											with torch.no_grad():
															lowerCAmelCase__      			=						model(pixel_values=UpperCamelCase_ ,							pixel_mask=UpperCamelCase_	)
															lowerCAmelCase__      			=						model(UpperCamelCase_	)

															comm_check_on_output(UpperCamelCase_	)

															lowerCAmelCase__      			=						model(
															    pixel_values=UpperCamelCase_ ,							pixel_mask=UpperCamelCase_ ,							mask_labels=UpperCamelCase_ ,							class_labels=UpperCamelCase_	)

											comm_check_on_output(UpperCamelCase_	)

											self.parent.assertTrue(result.loss is not None	)
											self.parent.assertEqual(result.loss.shape ,							torch.Size([1]	)	)



@require_torch
class  lowerCAmelCase_       (				__lowerCamelCase     ,    __lowerCamelCase     ,    unittest.TestCase					):
							UpperCamelCase_		:Optional[int]		      =    (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
							UpperCamelCase_		:Dict		      =    (
							    {'feature-extraction': MaskFormerModel, 'image-segmentation': MaskFormerForInstanceSegmentation}
							    if is_torch_available()
							    else {}
							)

							UpperCamelCase_		:Union[str, Any]		      =    False
							UpperCamelCase_		:Dict		      =    False
							UpperCamelCase_		:Union[str, Any]		      =    False
							UpperCamelCase_		:Optional[int]		      =    False




							def 						__snake_case					(   self     :   Tuple	):
											lowerCAmelCase__      			=						MaskFormerModelTester(self	)
											lowerCAmelCase__      			=						ConfigTester(self ,							config_class=UpperCamelCase_ ,							has_text_modality=UpperCamelCase_	)




							def 						__snake_case					(   self     :   Optional[int]	):
											self.config_tester.run_common_tests()




							def 						__snake_case					(   self     :   Optional[int]	):
											lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.create_and_check_maskformer_model(UpperCamelCase_ ,							**UpperCamelCase_ ,							output_hidden_states=UpperCamelCase_	)




							def 						__snake_case					(   self     :   Any	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*UpperCamelCase_	)




							@unittest.skip(reason='''MaskFormer does not use inputs_embeds'''	)
							def 						__snake_case					(   self     :   str	):
											pass




							@unittest.skip(reason='''MaskFormer does not have a get_input_embeddings method'''	)
							def 						__snake_case					(   self     :   int	):
											pass




							@unittest.skip(reason='''MaskFormer is not a generative model'''	)
							def 						__snake_case					(   self     :   List[str]	):
											pass




							@unittest.skip(reason='''MaskFormer does not use token embeddings'''	)
							def 						__snake_case					(   self     :   List[Any]	):
											pass




							@require_torch_multi_gpu
							@unittest.skip(
							    reason='''MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''	)
							def 						__snake_case					(   self     :   Optional[int]	):
											pass




							@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''	)
							def 						__snake_case					(   self     :   Any	):
											pass




							def 						__snake_case					(   self     :   Optional[int]	):
											lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															lowerCAmelCase__      			=						model_class(UpperCamelCase_	)
															lowerCAmelCase__      			=						inspect.signature(model.forward	)
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															lowerCAmelCase__      			=						[*signature.parameters.keys()]

															lowerCAmelCase__      			=						['''pixel_values''']
															self.assertListEqual(arg_names[:1] ,							UpperCamelCase_	)




							@slow
							def 						__snake_case					(   self     :   Optional[int]	):
											for model_name in ["facebook/maskformer-swin-small-coco"]:
															lowerCAmelCase__      			=						MaskFormerModel.from_pretrained(UpperCamelCase_	)
															self.assertIsNotNone(UpperCamelCase_	)




							def 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						(self.model_tester.min_size,) * 2
											lowerCAmelCase__      			=						{
											    '''pixel_values''': torch.randn((2, 3, *size) ,							device=UpperCamelCase_	),
											    '''mask_labels''': torch.randn((2, 10, *size) ,							device=UpperCamelCase_	),
											    '''class_labels''': torch.zeros(2 ,							10 ,							device=UpperCamelCase_	).long(),
											}

											lowerCAmelCase__      			=						MaskFormerForInstanceSegmentation(MaskFormerConfig()	).to(UpperCamelCase_	)
											lowerCAmelCase__      			=						model(**UpperCamelCase_	)
											self.assertTrue(outputs.loss is not None	)




							def 						__snake_case					(   self     :   List[str]	):
											lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.create_and_check_maskformer_model(UpperCamelCase_ ,							**UpperCamelCase_ ,							output_hidden_states=UpperCamelCase_	)




							def 						__snake_case					(   self     :   List[Any]	):
											lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															lowerCAmelCase__      			=						model_class(UpperCamelCase_	).to(UpperCamelCase_	)
															lowerCAmelCase__      			=						model(**UpperCamelCase_ ,							output_attentions=UpperCamelCase_	)
															self.assertTrue(outputs.attentions is not None	)




							def 						__snake_case					(   self     :   Union[str, Any]	):
											if not self.model_tester.is_training:
															return
											# only MaskFormerForInstanceSegmentation has the loss
											lowerCAmelCase__      			=						self.all_model_classes[1]
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()

											lowerCAmelCase__      			=						model_class(UpperCamelCase_	)
											model.to(UpperCamelCase_	)
											model.train()

											lowerCAmelCase__      			=						model(UpperCamelCase_ ,							mask_labels=UpperCamelCase_ ,							class_labels=UpperCamelCase_	).loss
											loss.backward()




							def 						__snake_case					(   self     :   List[str]	):
											# only MaskFormerForInstanceSegmentation has the loss
											lowerCAmelCase__      			=						self.all_model_classes[1]
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											lowerCAmelCase__      			=						True
											lowerCAmelCase__      			=						True

											lowerCAmelCase__      			=						model_class(UpperCamelCase_	)
											model.to(UpperCamelCase_	)
											model.train()

											lowerCAmelCase__      			=						model(UpperCamelCase_ ,							mask_labels=UpperCamelCase_ ,							class_labels=UpperCamelCase_	)

											lowerCAmelCase__      			=						outputs.encoder_hidden_states[0]
											encoder_hidden_states.retain_grad()

											lowerCAmelCase__      			=						outputs.pixel_decoder_hidden_states[0]
											pixel_decoder_hidden_states.retain_grad()
											# we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
											lowerCAmelCase__      			=						outputs.transformer_decoder_hidden_states[0]
											transformer_decoder_hidden_states.retain_grad()

											lowerCAmelCase__      			=						outputs.attentions[0]
											attentions.retain_grad()

											outputs.loss.backward(retain_graph=UpperCamelCase_	)

											self.assertIsNotNone(encoder_hidden_states.grad	)
											self.assertIsNotNone(pixel_decoder_hidden_states.grad	)
											self.assertIsNotNone(transformer_decoder_hidden_states.grad	)
											self.assertIsNotNone(attentions.grad	)



_UpperCAmelCase			:		Optional[int]   				=    1E-4





def 	lowerCAmelCase_		()		->    Tuple:


				'''simple docstring'''




				lowerCAmelCase__      			=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
				return image




@require_vision
@slow
class  lowerCAmelCase_       (				unittest.TestCase					):




							@cached_property
							def 						__snake_case					(   self     :   str	):
											return (
											    MaskFormerImageProcessor.from_pretrained('''facebook/maskformer-swin-small-coco'''	)
											    if is_vision_available()
											    else None
											)




							def 						__snake_case					(   self     :   str	):
											lowerCAmelCase__      			=						MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco'''	).to(UpperCamelCase_	)
											lowerCAmelCase__      			=						self.default_image_processor
											lowerCAmelCase__      			=						prepare_img()
											lowerCAmelCase__      			=						image_processor(UpperCamelCase_ ,							return_tensors='''pt'''	).to(UpperCamelCase_	)
											lowerCAmelCase__      			=						inputs['''pixel_values'''].shape
											# check size is divisible by 32
											self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0	)
											# check size
											self.assertEqual(UpperCamelCase_ ,							(1, 3, 800, 1_088)	)

											with torch.no_grad():
															lowerCAmelCase__      			=						model(**UpperCamelCase_	)

											lowerCAmelCase__      			=						torch.tensor(
											    [[-0.0_482, 0.9_228, 0.4_951], [-0.2_547, 0.8_017, 0.8_527], [-0.0_069, 0.3_385, -0.0_089]]	).to(UpperCamelCase_	)
											self.assertTrue(
											    torch.allclose(
											        outputs.encoder_last_hidden_state[0, 0, :3, :3] ,							UpperCamelCase_ ,							atol=UpperCamelCase_	)	)

											lowerCAmelCase__      			=						torch.tensor(
											    [[-0.8_422, -0.8_434, -0.9_718], [-1.0_144, -0.5_565, -0.4_195], [-1.0_038, -0.4_484, -0.1_961]]	).to(UpperCamelCase_	)
											self.assertTrue(
											    torch.allclose(
											        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] ,							UpperCamelCase_ ,							atol=UpperCamelCase_	)	)

											lowerCAmelCase__      			=						torch.tensor(
											    [[0.2_852, -0.0_159, 0.9_735], [0.6_254, 0.1_858, 0.8_529], [-0.0_680, -0.4_116, 1.8_413]]	).to(UpperCamelCase_	)
											self.assertTrue(
											    torch.allclose(
											        outputs.transformer_decoder_last_hidden_state[0, :3, :3] ,							UpperCamelCase_ ,							atol=UpperCamelCase_	)	)




							def 						__snake_case					(   self     :   Any	):
											lowerCAmelCase__      			=						(
											    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''	)
											    .to(UpperCamelCase_	)
											    .eval()
											)
											lowerCAmelCase__      			=						self.default_image_processor
											lowerCAmelCase__      			=						prepare_img()
											lowerCAmelCase__      			=						image_processor(UpperCamelCase_ ,							return_tensors='''pt'''	).to(UpperCamelCase_	)
											lowerCAmelCase__      			=						inputs['''pixel_values'''].shape
											# check size is divisible by 32
											self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0	)
											# check size
											self.assertEqual(UpperCamelCase_ ,							(1, 3, 800, 1_088)	)

											with torch.no_grad():
															lowerCAmelCase__      			=						model(**UpperCamelCase_	)
											# masks_queries_logits
											lowerCAmelCase__      			=						outputs.masks_queries_logits
											self.assertEqual(
											    masks_queries_logits.shape ,							(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ,							)
											lowerCAmelCase__      			=						[
											    [-1.3_737_124, -1.7_724_937, -1.9_364_233],
											    [-1.5_977_281, -1.9_867_939, -2.1_523_695],
											    [-1.5_795_398, -1.9_269_832, -2.093_942],
											]
											lowerCAmelCase__      			=						torch.tensor(UpperCamelCase_	).to(UpperCamelCase_	)
											self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] ,							UpperCamelCase_ ,							atol=UpperCamelCase_	)	)
											# class_queries_logits
											lowerCAmelCase__      			=						outputs.class_queries_logits
											self.assertEqual(
											    class_queries_logits.shape ,							(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)	)
											lowerCAmelCase__      			=						torch.tensor(
											    [
											        [1.65_12e00, -5.25_72e00, -3.35_19e00],
											        [3.61_69e-02, -5.90_25e00, -2.93_13e00],
											        [1.07_66e-04, -7.76_30e00, -5.12_63e00],
											    ]	).to(UpperCamelCase_	)
											self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] ,							UpperCamelCase_ ,							atol=UpperCamelCase_	)	)




							def 						__snake_case					(   self     :   Any	):
											lowerCAmelCase__      			=						(
											    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff'''	)
											    .to(UpperCamelCase_	)
											    .eval()
											)
											lowerCAmelCase__      			=						self.default_image_processor
											lowerCAmelCase__      			=						prepare_img()
											lowerCAmelCase__      			=						image_processor(UpperCamelCase_ ,							return_tensors='''pt'''	).to(UpperCamelCase_	)
											lowerCAmelCase__      			=						inputs['''pixel_values'''].shape
											# check size is divisible by 32
											self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0	)
											# check size
											self.assertEqual(UpperCamelCase_ ,							(1, 3, 800, 1_088)	)

											with torch.no_grad():
															lowerCAmelCase__      			=						model(**UpperCamelCase_	)
											# masks_queries_logits
											lowerCAmelCase__      			=						outputs.masks_queries_logits
											self.assertEqual(
											    masks_queries_logits.shape ,							(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ,							)
											lowerCAmelCase__      			=						[[-0.9_046, -2.6_366, -4.6_062], [-3.4_179, -5.7_890, -8.8_057], [-4.9_179, -7.6_560, -10.7_711]]
											lowerCAmelCase__      			=						torch.tensor(UpperCamelCase_	).to(UpperCamelCase_	)
											self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] ,							UpperCamelCase_ ,							atol=UpperCamelCase_	)	)
											# class_queries_logits
											lowerCAmelCase__      			=						outputs.class_queries_logits
											self.assertEqual(
											    class_queries_logits.shape ,							(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)	)
											lowerCAmelCase__      			=						torch.tensor(
											    [[4.7_188, -3.2_585, -2.8_857], [6.6_871, -2.9_181, -1.2_487], [7.2_449, -2.2_764, -2.1_874]]	).to(UpperCamelCase_	)
											self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] ,							UpperCamelCase_ ,							atol=UpperCamelCase_	)	)




							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__      			=						(
											    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''	)
											    .to(UpperCamelCase_	)
											    .eval()
											)
											lowerCAmelCase__      			=						self.default_image_processor

											lowerCAmelCase__      			=						image_processor(
											    [np.zeros((3, 800, 1_333)	), np.zeros((3, 800, 1_333)	)] ,							segmentation_maps=[np.zeros((384, 384)	).astype(np.floataa	), np.zeros((384, 384)	).astype(np.floataa	)] ,							return_tensors='''pt''' ,							)

											lowerCAmelCase__      			=						inputs['''pixel_values'''].to(UpperCamelCase_	)
											lowerCAmelCase__      			=						[el.to(UpperCamelCase_	) for el in inputs['''mask_labels''']]
											lowerCAmelCase__      			=						[el.to(UpperCamelCase_	) for el in inputs['''class_labels''']]

											with torch.no_grad():
															lowerCAmelCase__      			=						model(**UpperCamelCase_	)

											self.assertTrue(outputs.loss is not None	)













from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

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 (
			    TFFunnelBaseModel,
			    TFFunnelForMaskedLM,
			    TFFunnelForMultipleChoice,
			    TFFunnelForPreTraining,
			    TFFunnelForQuestionAnswering,
			    TFFunnelForSequenceClassification,
			    TFFunnelForTokenClassification,
			    TFFunnelModel,
			)
class  lowerCAmelCase_       :




							def __init__(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=13 ,							SCREAMING_SNAKE_CASE_     :   str=7 ,							SCREAMING_SNAKE_CASE_     :   Tuple=True ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=True ,							SCREAMING_SNAKE_CASE_     :   List[Any]=True ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=True ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=99 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=[1, 1, 2] ,							SCREAMING_SNAKE_CASE_     :   List[str]=1 ,							SCREAMING_SNAKE_CASE_     :   str=32 ,							SCREAMING_SNAKE_CASE_     :   Dict=4 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=8 ,							SCREAMING_SNAKE_CASE_     :   Dict=37 ,							SCREAMING_SNAKE_CASE_     :   Tuple="gelu_new" ,							SCREAMING_SNAKE_CASE_     :   Dict=0.1 ,							SCREAMING_SNAKE_CASE_     :   Any=0.1 ,							SCREAMING_SNAKE_CASE_     :   Any=0.0 ,							SCREAMING_SNAKE_CASE_     :   Tuple=512 ,							SCREAMING_SNAKE_CASE_     :   str=3 ,							SCREAMING_SNAKE_CASE_     :   int=0.02 ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=3 ,							SCREAMING_SNAKE_CASE_     :   Any=4 ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=None ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=False ,							):
											lowerCAmelCase__      			=						parent
											lowerCAmelCase__      			=						batch_size
											lowerCAmelCase__      			=						seq_length
											lowerCAmelCase__      			=						is_training
											lowerCAmelCase__      			=						use_input_mask
											lowerCAmelCase__      			=						use_token_type_ids
											lowerCAmelCase__      			=						use_labels
											lowerCAmelCase__      			=						vocab_size
											lowerCAmelCase__      			=						block_sizes
											lowerCAmelCase__      			=						num_decoder_layers
											lowerCAmelCase__      			=						d_model
											lowerCAmelCase__      			=						n_head
											lowerCAmelCase__      			=						d_head
											lowerCAmelCase__      			=						d_inner
											lowerCAmelCase__      			=						hidden_act
											lowerCAmelCase__      			=						hidden_dropout
											lowerCAmelCase__      			=						attention_dropout
											lowerCAmelCase__      			=						activation_dropout
											lowerCAmelCase__      			=						max_position_embeddings
											lowerCAmelCase__      			=						type_vocab_size
											lowerCAmelCase__      			=						2
											lowerCAmelCase__      			=						num_labels
											lowerCAmelCase__      			=						num_choices
											lowerCAmelCase__      			=						scope
											lowerCAmelCase__      			=						initializer_std

											# Used in the tests to check the size of the first attention layer
											lowerCAmelCase__      			=						n_head
											# Used in the tests to check the size of the first hidden state
											lowerCAmelCase__      			=						self.d_model
											# Used in the tests to check the number of output hidden states/attentions
											lowerCAmelCase__      			=						sum(self.block_sizes	) + (0 if base else self.num_decoder_layers)
											# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
											# the last hidden state of the first block (which is the first hidden state of the decoder).
											if not base:
															lowerCAmelCase__      			=						self.num_hidden_layers + 2




							def 						__snake_case					(   self     :   Optional[int]	):
											lowerCAmelCase__      			=						ids_tensor([self.batch_size, self.seq_length] ,							self.vocab_size	)

											lowerCAmelCase__      			=						None
											if self.use_input_mask:
															lowerCAmelCase__      			=						random_attention_mask([self.batch_size, self.seq_length]	)

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

											lowerCAmelCase__      			=						None
											lowerCAmelCase__      			=						None
											lowerCAmelCase__      			=						None
											if self.use_labels:
															lowerCAmelCase__      			=						ids_tensor([self.batch_size] ,							self.type_sequence_label_size	)
															lowerCAmelCase__      			=						ids_tensor([self.batch_size, self.seq_length] ,							self.num_labels	)
															lowerCAmelCase__      			=						ids_tensor([self.batch_size] ,							self.num_choices	)

											lowerCAmelCase__      			=						FunnelConfig(
											    vocab_size=self.vocab_size ,							block_sizes=self.block_sizes ,							num_decoder_layers=self.num_decoder_layers ,							d_model=self.d_model ,							n_head=self.n_head ,							d_head=self.d_head ,							d_inner=self.d_inner ,							hidden_act=self.hidden_act ,							hidden_dropout=self.hidden_dropout ,							attention_dropout=self.attention_dropout ,							activation_dropout=self.activation_dropout ,							max_position_embeddings=self.max_position_embeddings ,							type_vocab_size=self.type_vocab_size ,							initializer_std=self.initializer_std ,							)

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




							def 						__snake_case					(   self     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Dict ,							):
											lowerCAmelCase__      			=						TFFunnelModel(config=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)

											lowerCAmelCase__      			=						[input_ids, input_mask]
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)

											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, self.seq_length, self.d_model)	)

											lowerCAmelCase__      			=						False
											lowerCAmelCase__      			=						TFFunnelModel(config=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, self.seq_length, self.d_model)	)

											lowerCAmelCase__      			=						False
											lowerCAmelCase__      			=						TFFunnelModel(config=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, self.seq_length, self.d_model)	)




							def 						__snake_case					(   self     :   Any ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							):
											lowerCAmelCase__      			=						TFFunnelBaseModel(config=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)

											lowerCAmelCase__      			=						[input_ids, input_mask]
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)

											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, 2, self.d_model)	)

											lowerCAmelCase__      			=						False
											lowerCAmelCase__      			=						TFFunnelBaseModel(config=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, 3, self.d_model)	)

											lowerCAmelCase__      			=						False
											lowerCAmelCase__      			=						TFFunnelBaseModel(config=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, 2, self.d_model)	)




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




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




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




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




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




							def 						__snake_case					(   self     :   str ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Any ,							):
											lowerCAmelCase__      			=						TFFunnelForQuestionAnswering(config=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)
											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     :   List[str]	):
											lowerCAmelCase__      			=						self.prepare_config_and_inputs()
											(
											    (
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		
											)      			=						config_and_inputs
											lowerCAmelCase__      			=						{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
											return config, inputs_dict



@require_tf
class  lowerCAmelCase_       (				snake_case__     ,    snake_case__     ,    unittest.TestCase					):
							UpperCamelCase_		:Dict		      =    (
							    (
							        TFFunnelModel,
							        TFFunnelForMaskedLM,
							        TFFunnelForPreTraining,
							        TFFunnelForQuestionAnswering,
							        TFFunnelForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							UpperCamelCase_		:Dict		      =    (
							    {
							        'feature-extraction': (TFFunnelBaseModel, TFFunnelModel),
							        'fill-mask': TFFunnelForMaskedLM,
							        'question-answering': TFFunnelForQuestionAnswering,
							        'text-classification': TFFunnelForSequenceClassification,
							        'token-classification': TFFunnelForTokenClassification,
							        'zero-shot': TFFunnelForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							UpperCamelCase_		:Tuple		      =    False
							UpperCamelCase_		:Tuple		      =    False




							def 						__snake_case					(   self     :   str	):
											lowerCAmelCase__      			=						TFFunnelModelTester(self	)
											lowerCAmelCase__      			=						ConfigTester(self ,							config_class=SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Dict	):
											self.config_tester.run_common_tests()




							def 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_pretraining(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   str	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE_	)



@require_tf
class  lowerCAmelCase_       (				snake_case__     ,    unittest.TestCase					):
							UpperCamelCase_		:Optional[Any]		      =    (
							    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
							)
							UpperCamelCase_		:int		      =    False
							UpperCamelCase_		:Any		      =    False




							def 						__snake_case					(   self     :   List[str]	):
											lowerCAmelCase__      			=						TFFunnelModelTester(self ,							base=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						ConfigTester(self ,							config_class=SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Tuple	):
											self.config_tester.run_common_tests()




							def 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_base_model(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   List[str]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE_	)





