


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




							"""simple docstring"""


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

							def        __snake_case   (				self			:    List[Any]							)      ->       int:
														__snake_case						:     List[Any]									=  floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]							).to(
														    lowerCamelCase							)

														__snake_case						:     List[str]									=  torch.ones([self.batch_size, self.min_size, self.max_size]       ,      device=lowerCamelCase							)

														__snake_case						:     List[str]									=  (
														    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]       ,      device=lowerCamelCase							) > 0.5
														).float()
														__snake_case						:     Dict									=  (torch.rand((self.batch_size, self.num_labels)       ,      device=lowerCamelCase							) > 0.5).long()

														__snake_case						:     int									=  self.get_config()
														return config, pixel_values, pixel_mask, mask_labels, class_labels

							def        __snake_case   (				self			:    Any							)      ->       Tuple:
														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			:    int							)      ->       Dict:
														__snake_case					,     __snake_case					,     __snake_case					,     __snake_case					,     __snake_case						:     Dict									=  self.prepare_config_and_inputs()
														__snake_case						:     Tuple									=  {"pixel_values": pixel_values, "pixel_mask": pixel_mask}
														return config, inputs_dict

							def        __snake_case   (				self			:    Tuple       ,      lowerCamelCase			:    Tuple       ,      lowerCamelCase			:    List[str]							)      ->       List[str]:
														__snake_case						:     Any									=  output.encoder_hidden_states
														__snake_case						:     List[str]									=  output.pixel_decoder_hidden_states
														__snake_case						:     Dict									=  output.transformer_decoder_hidden_states

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

							def        __snake_case   (				self			:    Union[str, Any]       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    List[Any]       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    Dict=False							)      ->       Dict:
														with torch.no_grad():
																					__snake_case						:     int									=  MaskFormerModel(config=lowerCamelCase							)
																					model.to(lowerCamelCase							)
																					model.eval()

																					__snake_case						:     List[Any]									=  model(pixel_values=lowerCamelCase       ,      pixel_mask=lowerCamelCase							)
																					__snake_case						:     Union[str, Any]									=  model(lowerCamelCase       ,      output_hidden_states=lowerCamelCase							)
														# 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(lowerCamelCase       ,      lowerCamelCase							)






							def        __snake_case   (				self			:    Optional[int]       ,      lowerCamelCase			:    Any       ,      lowerCamelCase			:    Dict       ,      lowerCamelCase			:    Optional[int]       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    Union[str, Any]							)      ->       Optional[Any]:
														__snake_case						:     Union[str, Any]									=  MaskFormerForInstanceSegmentation(config=lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()

														def comm_check_on_output(lowerCamelCase			:    Union[str, Any]							):
																					# 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():
																					__snake_case						:     Dict									=  model(pixel_values=lowerCamelCase       ,      pixel_mask=lowerCamelCase							)
																					__snake_case						:     Optional[Any]									=  model(lowerCamelCase							)

																					comm_check_on_output(lowerCamelCase							)

																					__snake_case						:     str									=  model(
																					    pixel_values=lowerCamelCase       ,      pixel_mask=lowerCamelCase       ,      mask_labels=lowerCamelCase       ,      class_labels=lowerCamelCase							)

														comm_check_on_output(lowerCamelCase							)

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

@require_torch
class 							a						(_lowerCAmelCase	,							_lowerCAmelCase	,							unittest.TestCase ):




							"""simple docstring"""


							__UpperCAmelCase       :				Tuple	    =    (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
							__UpperCAmelCase       :				Dict	    =    (
							    {"feature-extraction": MaskFormerModel, "image-segmentation": MaskFormerForInstanceSegmentation}
							    if is_torch_available()
							    else {}
							)

							__UpperCAmelCase       :				Dict	    =    False
							__UpperCAmelCase       :				int	    =    False
							__UpperCAmelCase       :				List[str]	    =    False
							__UpperCAmelCase       :				str	    =    False

							def        __snake_case   (				self			:    Optional[int]							)      ->       List[str]:
														__snake_case						:     Union[str, Any]									=  MaskFormerModelTester(self							)
														__snake_case						:     Dict									=  ConfigTester(self       ,      config_class=lowerCamelCase       ,      has_text_modality=lowerCamelCase							)

							def        __snake_case   (				self			:    int							)      ->       Dict:
														self.config_tester.run_common_tests()

							def        __snake_case   (				self			:    Any							)      ->       Union[str, Any]:
														__snake_case					,     __snake_case						:     Dict									=  self.model_tester.prepare_config_and_inputs_for_common()
														self.model_tester.create_and_check_maskformer_model(lowerCamelCase       ,      **lowerCamelCase       ,      output_hidden_states=lowerCamelCase							)

							def        __snake_case   (				self			:    Any							)      ->       Optional[Any]:
														__snake_case						:     Optional[int]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*lowerCamelCase							)

							@unittest.skip(reason="MaskFormer does not use inputs_embeds"							)
							def        __snake_case   (				self			:    List[Any]							)      ->       Tuple:
														pass

							@unittest.skip(reason="MaskFormer does not have a get_input_embeddings method"							)
							def        __snake_case   (				self			:    Optional[int]							)      ->       List[str]:
														pass

							@unittest.skip(reason="MaskFormer is not a generative model"							)
							def        __snake_case   (				self			:    Union[str, Any]							)      ->       Tuple:
														pass

							@unittest.skip(reason="MaskFormer does not use token embeddings"							)
							def        __snake_case   (				self			:    List[Any]							)      ->       Union[str, 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			:    str							)      ->       List[Any]:
														pass

							@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."							)
							def        __snake_case   (				self			:    int							)      ->       Dict:
														pass

							def        __snake_case   (				self			:    int							)      ->       List[Any]:
														__snake_case					,     __snake_case						:     Optional[Any]									=  self.model_tester.prepare_config_and_inputs_for_common()

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

																					__snake_case						:     Dict									=  ["pixel_values"]
																					self.assertListEqual(arg_names[:1]       ,      lowerCamelCase							)

							@slow
							def        __snake_case   (				self			:    List[Any]							)      ->       int:
														for model_name in ["facebook/maskformer-swin-small-coco"]:
																					__snake_case						:     int									=  MaskFormerModel.from_pretrained(lowerCamelCase							)
																					self.assertIsNotNone(lowerCamelCase							)

							def        __snake_case   (				self			:    Union[str, Any]							)      ->       str:
														__snake_case						:     Union[str, Any]									=  (self.model_tester.min_size,) * 2
														__snake_case						:     List[Any]									=  {
														    "pixel_values": torch.randn((2, 3, *size)       ,      device=lowerCamelCase							),
														    "mask_labels": torch.randn((2, 10, *size)       ,      device=lowerCamelCase							),
														    "class_labels": torch.zeros(2       ,      10       ,      device=lowerCamelCase							).long(),
														}

														__snake_case						:     Optional[int]									=  MaskFormerForInstanceSegmentation(MaskFormerConfig()							).to(lowerCamelCase							)
														__snake_case						:     Dict									=  model(**lowerCamelCase							)
														self.assertTrue(outputs.loss is not None							)

							def        __snake_case   (				self			:    Any							)      ->       Optional[Any]:
														__snake_case					,     __snake_case						:     int									=  self.model_tester.prepare_config_and_inputs_for_common()
														self.model_tester.create_and_check_maskformer_model(lowerCamelCase       ,      **lowerCamelCase       ,      output_hidden_states=lowerCamelCase							)

							def        __snake_case   (				self			:    Any							)      ->       List[str]:
														__snake_case					,     __snake_case						:     Optional[int]									=  self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					__snake_case						:     int									=  model_class(lowerCamelCase							).to(lowerCamelCase							)
																					__snake_case						:     Dict									=  model(**lowerCamelCase       ,      output_attentions=lowerCamelCase							)
																					self.assertTrue(outputs.attentions is not None							)

							def        __snake_case   (				self			:    Optional[int]							)      ->       str:
														if not self.model_tester.is_training:
																					return
														# only MaskFormerForInstanceSegmentation has the loss
														__snake_case						:     int									=  self.all_model_classes[1]
														__snake_case					,     __snake_case					,     __snake_case					,     __snake_case					,     __snake_case						:     List[str]									=  self.model_tester.prepare_config_and_inputs()

														__snake_case						:     Optional[Any]									=  model_class(lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.train()

														__snake_case						:     str									=  model(lowerCamelCase       ,      mask_labels=lowerCamelCase       ,      class_labels=lowerCamelCase							).loss
														loss.backward()






							def        __snake_case   (				self			:    Any							)      ->       str:
														# only MaskFormerForInstanceSegmentation has the loss
														__snake_case						:     List[Any]									=  self.all_model_classes[1]
														__snake_case					,     __snake_case					,     __snake_case					,     __snake_case					,     __snake_case						:     Tuple									=  self.model_tester.prepare_config_and_inputs()
														__snake_case						:     Union[str, Any]									=  True
														__snake_case						:     Optional[int]									=  True

														__snake_case						:     Any									=  model_class(lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.train()

														__snake_case						:     str									=  model(lowerCamelCase       ,      mask_labels=lowerCamelCase       ,      class_labels=lowerCamelCase							)

														__snake_case						:     Optional[Any]									=  outputs.encoder_hidden_states[0]
														encoder_hidden_states.retain_grad()

														__snake_case						:     str									=  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
														__snake_case						:     Any									=  outputs.transformer_decoder_hidden_states[0]
														transformer_decoder_hidden_states.retain_grad()

														__snake_case						:     Tuple									=  outputs.attentions[0]
														attentions.retain_grad()

														outputs.loss.backward(retain_graph=lowerCamelCase							)

														self.assertIsNotNone(encoder_hidden_states.grad							)
														self.assertIsNotNone(pixel_decoder_hidden_states.grad							)
														self.assertIsNotNone(transformer_decoder_hidden_states.grad							)
														self.assertIsNotNone(attentions.grad							)
_snake_case	:						int         =							1E-4







def 							lowerCAmelCase_	(   ):
							__snake_case						:     List[str]									=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
							return image

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




							"""simple docstring"""


							@cached_property
							def        __snake_case   (				self			:    Optional[Any]							)      ->       List[str]:
														return (
														    MaskFormerImageProcessor.from_pretrained("facebook/maskformer-swin-small-coco"							)
														    if is_vision_available()
														    else None
														)

							def        __snake_case   (				self			:    Dict							)      ->       Optional[int]:
														__snake_case						:     Tuple									=  MaskFormerModel.from_pretrained("facebook/maskformer-swin-small-coco"							).to(lowerCamelCase							)
														__snake_case						:     Dict									=  self.default_image_processor
														__snake_case						:     Optional[int]									=  prepare_img()
														__snake_case						:     Optional[int]									=  image_processor(lowerCamelCase       ,      return_tensors="pt"							).to(lowerCamelCase							)
														__snake_case						:     Optional[Any]									=  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(lowerCamelCase       ,      (1, 3, 800, 1088)							)

														with torch.no_grad():
																					__snake_case						:     Optional[int]									=  model(**lowerCamelCase							)

														__snake_case						:     str									=  torch.tensor(
														    [[-0.04_82, 0.92_28, 0.49_51], [-0.25_47, 0.80_17, 0.85_27], [-0.00_69, 0.33_85, -0.00_89]]							).to(lowerCamelCase							)
														self.assertTrue(
														    torch.allclose(
														        outputs.encoder_last_hidden_state[0, 0, :3, :3]       ,      lowerCamelCase       ,      atol=lowerCamelCase							)							)

														__snake_case						:     Optional[int]									=  torch.tensor(
														    [[-0.84_22, -0.84_34, -0.97_18], [-1.01_44, -0.55_65, -0.41_95], [-1.00_38, -0.44_84, -0.19_61]]							).to(lowerCamelCase							)
														self.assertTrue(
														    torch.allclose(
														        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]       ,      lowerCamelCase       ,      atol=lowerCamelCase							)							)

														__snake_case						:     Tuple									=  torch.tensor(
														    [[0.28_52, -0.01_59, 0.97_35], [0.62_54, 0.18_58, 0.85_29], [-0.06_80, -0.41_16, 1.84_13]]							).to(lowerCamelCase							)
														self.assertTrue(
														    torch.allclose(
														        outputs.transformer_decoder_last_hidden_state[0, :3, :3]       ,      lowerCamelCase       ,      atol=lowerCamelCase							)							)

							def        __snake_case   (				self			:    Dict							)      ->       List[Any]:
														__snake_case						:     Any									=  (
														    MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco"							)
														    .to(lowerCamelCase							)
														    .eval()
														)
														__snake_case						:     Any									=  self.default_image_processor
														__snake_case						:     str									=  prepare_img()
														__snake_case						:     Tuple									=  image_processor(lowerCamelCase       ,      return_tensors="pt"							).to(lowerCamelCase							)
														__snake_case						:     int									=  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(lowerCamelCase       ,      (1, 3, 800, 1088)							)

														with torch.no_grad():
																					__snake_case						:     str									=  model(**lowerCamelCase							)
														# masks_queries_logits
														__snake_case						:     Optional[int]									=  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)       ,      )
														__snake_case						:     Union[str, Any]									=  [
														    [-1.3_73_71_24, -1.7_72_49_37, -1.9_36_42_33],
														    [-1.5_97_72_81, -1.9_86_79_39, -2.1_52_36_95],
														    [-1.5_79_53_98, -1.9_26_98_32, -2.09_39_42],
														]
														__snake_case						:     Optional[Any]									=  torch.tensor(lowerCamelCase							).to(lowerCamelCase							)
														self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,      lowerCamelCase       ,      atol=lowerCamelCase							)							)
														# class_queries_logits
														__snake_case						:     Tuple									=  outputs.class_queries_logits
														self.assertEqual(
														    class_queries_logits.shape       ,      (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)							)
														__snake_case						:     Optional[int]									=  torch.tensor(
														    [
														        [1.6512E00, -5.2572E00, -3.3519E00],
														        [3.6169E-02, -5.9025E00, -2.9313E00],
														        [1.0766E-04, -7.7630E00, -5.1263E00],
														    ]							).to(lowerCamelCase							)
														self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,      lowerCamelCase       ,      atol=lowerCamelCase							)							)

							def        __snake_case   (				self			:    Any							)      ->       int:
														__snake_case						:     int									=  (
														    MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-resnet101-coco-stuff"							)
														    .to(lowerCamelCase							)
														    .eval()
														)
														__snake_case						:     Tuple									=  self.default_image_processor
														__snake_case						:     List[str]									=  prepare_img()
														__snake_case						:     List[str]									=  image_processor(lowerCamelCase       ,      return_tensors="pt"							).to(lowerCamelCase							)
														__snake_case						:     List[str]									=  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(lowerCamelCase       ,      (1, 3, 800, 1088)							)

														with torch.no_grad():
																					__snake_case						:     Optional[Any]									=  model(**lowerCamelCase							)
														# masks_queries_logits
														__snake_case						:     str									=  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)       ,      )
														__snake_case						:     Optional[int]									=  [[-0.90_46, -2.63_66, -4.60_62], [-3.41_79, -5.78_90, -8.80_57], [-4.91_79, -7.65_60, -10.77_11]]
														__snake_case						:     List[Any]									=  torch.tensor(lowerCamelCase							).to(lowerCamelCase							)
														self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,      lowerCamelCase       ,      atol=lowerCamelCase							)							)
														# class_queries_logits
														__snake_case						:     Dict									=  outputs.class_queries_logits
														self.assertEqual(
														    class_queries_logits.shape       ,      (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)							)
														__snake_case						:     Any									=  torch.tensor(
														    [[4.71_88, -3.25_85, -2.88_57], [6.68_71, -2.91_81, -1.24_87], [7.24_49, -2.27_64, -2.18_74]]							).to(lowerCamelCase							)
														self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,      lowerCamelCase       ,      atol=lowerCamelCase							)							)






							def        __snake_case   (				self			:    Optional[Any]							)      ->       List[str]:
														__snake_case						:     List[Any]									=  (
														    MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco"							)
														    .to(lowerCamelCase							)
														    .eval()
														)
														__snake_case						:     Optional[int]									=  self.default_image_processor

														__snake_case						:     List[str]									=  image_processor(
														    [np.zeros((3, 800, 1333)							), np.zeros((3, 800, 1333)							)]       ,      segmentation_maps=[np.zeros((384, 384)							).astype(np.floataa							), np.zeros((384, 384)							).astype(np.floataa							)]       ,      return_tensors="pt"       ,      )

														__snake_case						:     str									=  inputs["pixel_values"].to(lowerCamelCase							)
														__snake_case						:     Tuple									=  [el.to(lowerCamelCase							) for el in inputs["mask_labels"]]
														__snake_case						:     Tuple									=  [el.to(lowerCamelCase							) for el in inputs["class_labels"]]

														with torch.no_grad():
																					__snake_case						:     Dict									=  model(**lowerCamelCase							)

														self.assertTrue(outputs.loss is not None							)







from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase__					:  str      							=				logging.get_logger(__name__)

lowerCamelCase__					:  Tuple      							=				{
    'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json',
    'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json',
    'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json',
    'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json',
    'funnel-transformer/intermediate': (
        'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json'
    ),
    'funnel-transformer/intermediate-base': (
        'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json'
    ),
    'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json',
    'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json',
    'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json',
    'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json',
}





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       "funnel"
 lowercase_							       =       {
     "hidden_size": "d_model",
     "num_attention_heads": "n_head",
 }



 def __init__(				self    :						int							,							_lowerCAmelCase    :						Optional[int]=30_522							,							_lowerCAmelCase    :						List[str]=[4, 4, 4]							,							_lowerCAmelCase    :						Tuple=None							,							_lowerCAmelCase    :						Optional[int]=2							,							_lowerCAmelCase    :						int=768							,							_lowerCAmelCase    :						Optional[Any]=12							,							_lowerCAmelCase    :						Optional[Any]=64							,							_lowerCAmelCase    :						Optional[Any]=3_072							,							_lowerCAmelCase    :						List[str]="gelu_new"							,							_lowerCAmelCase    :						Any=0.1							,							_lowerCAmelCase    :						int=0.1							,							_lowerCAmelCase    :						Tuple=0.0							,							_lowerCAmelCase    :						List[Any]=0.1							,							_lowerCAmelCase    :						Dict=None							,							_lowerCAmelCase    :						str=1E-9							,							_lowerCAmelCase    :						Any="mean"							,							_lowerCAmelCase    :						Union[str, Any]="relative_shift"							,							_lowerCAmelCase    :						Optional[Any]=True							,							_lowerCAmelCase    :						Dict=True							,							_lowerCAmelCase    :						Tuple=True							,							**_lowerCAmelCase    :						Optional[Any]							,							):
       SCREAMING_SNAKE_CASE_      		=     vocab_size
       SCREAMING_SNAKE_CASE_      		=     block_sizes
       SCREAMING_SNAKE_CASE_      		=     [1] * len(_lowerCAmelCase		) if block_repeats is None else block_repeats
       assert len(_lowerCAmelCase		) == len(
           self.block_repeats		), "`block_sizes` and `block_repeats` should have the same length."
       SCREAMING_SNAKE_CASE_      		=     num_decoder_layers
       SCREAMING_SNAKE_CASE_      		=     d_model
       SCREAMING_SNAKE_CASE_      		=     n_head
       SCREAMING_SNAKE_CASE_      		=     d_head
       SCREAMING_SNAKE_CASE_      		=     d_inner
       SCREAMING_SNAKE_CASE_      		=     hidden_act
       SCREAMING_SNAKE_CASE_      		=     hidden_dropout
       SCREAMING_SNAKE_CASE_      		=     attention_dropout
       SCREAMING_SNAKE_CASE_      		=     activation_dropout
       SCREAMING_SNAKE_CASE_      		=     initializer_range
       SCREAMING_SNAKE_CASE_      		=     initializer_std
       SCREAMING_SNAKE_CASE_      		=     layer_norm_eps
       assert pooling_type in [
           "mean",
           "max",
       ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported."
       SCREAMING_SNAKE_CASE_      		=     pooling_type
       assert attention_type in [
           "relative_shift",
           "factorized",
       ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported."
       SCREAMING_SNAKE_CASE_      		=     attention_type
       SCREAMING_SNAKE_CASE_      		=     separate_cls
       SCREAMING_SNAKE_CASE_      		=     truncate_seq
       SCREAMING_SNAKE_CASE_      		=     pool_q_only

       super().__init__(**_lowerCAmelCase		)



 @property
 def  lowerCAmelCase_    (				self    :						Optional[int]		):
       return sum(self.block_sizes		)



 @num_hidden_layers.setter
 def  lowerCAmelCase_    (				self    :						int							,							_lowerCAmelCase    :						List[Any]		):
       raise NotImplementedError(
           'This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.'		)



 @property
 def  lowerCAmelCase_    (				self    :						List[Any]		):
       return len(self.block_sizes		)




 @num_blocks.setter
 def  lowerCAmelCase_    (				self    :						Dict							,							_lowerCAmelCase    :						Union[str, Any]		):
       raise NotImplementedError('This model does not support the setting of `num_blocks`. Please set `block_sizes`.'		)