

"""simple docstring"""




from typing import List, Optional, Tuple, Union

import torch
from torch import nn
from torch.nn import CrossEntropyLoss

from ... import AutoBackbone
from ...modeling_outputs import SemanticSegmenterOutput
from ...modeling_utils import PreTrainedModel
from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings
from ...utils.backbone_utils import BackboneMixin
from .configuration_upernet import UperNetConfig


A_				:      List[Any]           =			[
    "openmmlab/upernet-convnext-tiny",
    # See all UperNet models at https://huggingface.co/models?filter=upernet
]

# General docstring
A_				:      List[str]           =			"UperNetConfig"





class  lowerCamelCase    (nn.Module      ):

			def __init__(       self					:    Any     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    Union[int, Tuple[int, int]]     ,					__UpperCAmelCase					:    Union[int, Tuple[int, int], str] = 0     ,					__UpperCAmelCase					:    bool = False     ,					__UpperCAmelCase					:    Union[int, Tuple[int, int]] = 1     ,					) ->     None:
						super().__init__()
						SCREAMING_SNAKE_CASE__										=   nn.Convad(
						    in_channels=__UpperCAmelCase     ,					out_channels=__UpperCAmelCase     ,					kernel_size=__UpperCAmelCase     ,					padding=__UpperCAmelCase     ,					bias=__UpperCAmelCase     ,					dilation=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   nn.BatchNormad(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   nn.ReLU()





			def        SCREAMING_SNAKE_CASE					(       self					:    List[str]     ,					__UpperCAmelCase					:    torch.Tensor							) ->     torch.Tensor:
						SCREAMING_SNAKE_CASE__										=   self.conv(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   self.batch_norm(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   self.activation(__UpperCAmelCase							)

						return output





class  lowerCamelCase    (nn.Module      ):

			def __init__(       self					:    Dict     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    int							) ->     None:
						super().__init__()
						SCREAMING_SNAKE_CASE__										=   [
						    nn.AdaptiveAvgPoolad(__UpperCAmelCase							),
						    UperNetConvModule(__UpperCAmelCase     ,					__UpperCAmelCase     ,					kernel_size=1							),
						]
						for i, layer in enumerate(self.layers							):
									self.add_module(str(__UpperCAmelCase							)     ,					__UpperCAmelCase							)





			def        SCREAMING_SNAKE_CASE					(       self					:    Dict     ,					__UpperCAmelCase					:    torch.Tensor							) ->     torch.Tensor:
						SCREAMING_SNAKE_CASE__										=   input
						for layer in self.layers:
									SCREAMING_SNAKE_CASE__										=   layer(__UpperCAmelCase							)
						return hidden_state





class  lowerCamelCase    (nn.Module      ):

			def __init__(       self					:    Dict     ,					__UpperCAmelCase					:    Tuple[int, ...]     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    bool							) ->     None:
						super().__init__()
						SCREAMING_SNAKE_CASE__										=   pool_scales
						SCREAMING_SNAKE_CASE__										=   align_corners
						SCREAMING_SNAKE_CASE__										=   in_channels
						SCREAMING_SNAKE_CASE__										=   channels
						SCREAMING_SNAKE_CASE__										=   []
						for i, pool_scale in enumerate(__UpperCAmelCase							):
									SCREAMING_SNAKE_CASE__										=   UperNetPyramidPoolingBlock(pool_scale=__UpperCAmelCase     ,					in_channels=__UpperCAmelCase     ,					channels=__UpperCAmelCase							)
									self.blocks.append(__UpperCAmelCase							)
									self.add_module(str(__UpperCAmelCase							)     ,					__UpperCAmelCase							)





			def        SCREAMING_SNAKE_CASE					(       self					:    List[str]     ,					__UpperCAmelCase					:    torch.Tensor							) ->     List[torch.Tensor]:
						SCREAMING_SNAKE_CASE__										=   []
						for ppm in self.blocks:
									SCREAMING_SNAKE_CASE__										=   ppm(__UpperCAmelCase							)
									SCREAMING_SNAKE_CASE__										=   nn.functional.interpolate(
									    __UpperCAmelCase     ,					size=x.size()[2:]     ,					mode="""bilinear"""     ,					align_corners=self.align_corners							)
									ppm_outs.append(__UpperCAmelCase							)
						return ppm_outs





class  lowerCamelCase    (nn.Module      ):

			def __init__(       self					:    Tuple     ,					__UpperCAmelCase					:    List[str]     ,					__UpperCAmelCase					:    Optional[int]							) ->     Optional[int]:
						super().__init__()

						SCREAMING_SNAKE_CASE__										=   config
						SCREAMING_SNAKE_CASE__										=   config.pool_scales  # e.g. (1, 2, 3, 6)
						SCREAMING_SNAKE_CASE__										=   in_channels
						SCREAMING_SNAKE_CASE__										=   config.hidden_size
						SCREAMING_SNAKE_CASE__										=   False
						SCREAMING_SNAKE_CASE__										=   nn.Convad(self.channels     ,					config.num_labels     ,					kernel_size=1							)

						# PSP Module
						SCREAMING_SNAKE_CASE__										=   UperNetPyramidPoolingModule(
						    self.pool_scales     ,					self.in_channels[-1]     ,					self.channels     ,					align_corners=self.align_corners     ,					)
						SCREAMING_SNAKE_CASE__										=   UperNetConvModule(
						    self.in_channels[-1] + len(self.pool_scales							) * self.channels     ,					self.channels     ,					kernel_size=3     ,					padding=1     ,					)
						# FPN Module
						SCREAMING_SNAKE_CASE__										=   nn.ModuleList()
						SCREAMING_SNAKE_CASE__										=   nn.ModuleList()
						for in_channels in self.in_channels[:-1]:  # skip the top layer
									SCREAMING_SNAKE_CASE__										=   UperNetConvModule(__UpperCAmelCase     ,					self.channels     ,					kernel_size=1							)
									SCREAMING_SNAKE_CASE__										=   UperNetConvModule(self.channels     ,					self.channels     ,					kernel_size=3     ,					padding=1							)
									self.lateral_convs.append(__UpperCAmelCase							)
									self.fpn_convs.append(__UpperCAmelCase							)

						SCREAMING_SNAKE_CASE__										=   UperNetConvModule(
						    len(self.in_channels							) * self.channels     ,					self.channels     ,					kernel_size=3     ,					padding=1     ,					)

			def        SCREAMING_SNAKE_CASE					(       self					:    Union[str, Any]							) ->     Optional[Any]:
						self.apply(self._init_weights							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]     ,					__UpperCAmelCase					:    List[Any]							) ->     int:
						if isinstance(__UpperCAmelCase     ,					nn.Convad							):
									module.weight.data.normal_(mean=0.0     ,					std=self.config.initializer_range							)
									if module.bias is not None:
												module.bias.data.zero_()

			def        SCREAMING_SNAKE_CASE					(       self					:    List[str]     ,					__UpperCAmelCase					:    List[str]							) ->     str:
						SCREAMING_SNAKE_CASE__										=   inputs[-1]
						SCREAMING_SNAKE_CASE__										=   [x]
						psp_outs.extend(self.psp_modules(__UpperCAmelCase							)							)
						SCREAMING_SNAKE_CASE__										=   torch.cat(__UpperCAmelCase     ,					dim=1							)
						SCREAMING_SNAKE_CASE__										=   self.bottleneck(__UpperCAmelCase							)

						return output





			def        SCREAMING_SNAKE_CASE					(       self					:    Any     ,					__UpperCAmelCase					:    torch.Tensor							) ->     torch.Tensor:
						# build laterals
						SCREAMING_SNAKE_CASE__										=   [lateral_conv(encoder_hidden_states[i]							) for i, lateral_conv in enumerate(self.lateral_convs							)]

						laterals.append(self.psp_forward(__UpperCAmelCase							)							)

						# build top-down path
						SCREAMING_SNAKE_CASE__										=   len(__UpperCAmelCase							)
						for i in range(used_backbone_levels - 1     ,					0     ,					-1							):
									SCREAMING_SNAKE_CASE__										=   laterals[i - 1].shape[2:]
									SCREAMING_SNAKE_CASE__										=   laterals[i - 1] + nn.functional.interpolate(
									    laterals[i]     ,					size=__UpperCAmelCase     ,					mode="""bilinear"""     ,					align_corners=self.align_corners							)

						# build outputs
						SCREAMING_SNAKE_CASE__										=   [self.fpn_convs[i](laterals[i]							) for i in range(used_backbone_levels - 1							)]
						# append psp feature
						fpn_outs.append(laterals[-1]							)

						for i in range(used_backbone_levels - 1     ,					0     ,					-1							):
									SCREAMING_SNAKE_CASE__										=   nn.functional.interpolate(
									    fpn_outs[i]     ,					size=fpn_outs[0].shape[2:]     ,					mode="""bilinear"""     ,					align_corners=self.align_corners							)
						SCREAMING_SNAKE_CASE__										=   torch.cat(__UpperCAmelCase     ,					dim=1							)
						SCREAMING_SNAKE_CASE__										=   self.fpn_bottleneck(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   self.classifier(__UpperCAmelCase							)

						return output





class  lowerCamelCase    (nn.Module      ):

			def __init__(       self					:    Dict     ,					__UpperCAmelCase					:    Any     ,					__UpperCAmelCase					:    int = 2     ,					__UpperCAmelCase					:    int = 3     ,					__UpperCAmelCase					:    Union[int, Tuple[int, int]] = 1							) ->     None:
						super().__init__()

						SCREAMING_SNAKE_CASE__										=   config
						SCREAMING_SNAKE_CASE__										=   config.auxiliary_in_channels
						SCREAMING_SNAKE_CASE__										=   config.auxiliary_channels
						SCREAMING_SNAKE_CASE__										=   config.auxiliary_num_convs
						SCREAMING_SNAKE_CASE__										=   config.auxiliary_concat_input
						SCREAMING_SNAKE_CASE__										=   in_index

						SCREAMING_SNAKE_CASE__										=   (kernel_size // 2) * dilation
						SCREAMING_SNAKE_CASE__										=   []
						convs.append(
						    UperNetConvModule(
						        self.in_channels     ,					self.channels     ,					kernel_size=__UpperCAmelCase     ,					padding=__UpperCAmelCase     ,					dilation=__UpperCAmelCase							)							)
						for i in range(self.num_convs - 1							):
									convs.append(
									    UperNetConvModule(
									        self.channels     ,					self.channels     ,					kernel_size=__UpperCAmelCase     ,					padding=__UpperCAmelCase     ,					dilation=__UpperCAmelCase							)							)
						if self.num_convs == 0:
									SCREAMING_SNAKE_CASE__										=   nn.Identity()
						else:
									SCREAMING_SNAKE_CASE__										=   nn.Sequential(*__UpperCAmelCase							)
						if self.concat_input:
									SCREAMING_SNAKE_CASE__										=   UperNetConvModule(
									    self.in_channels + self.channels     ,					self.channels     ,					kernel_size=__UpperCAmelCase     ,					padding=kernel_size // 2							)

						SCREAMING_SNAKE_CASE__										=   nn.Convad(self.channels     ,					config.num_labels     ,					kernel_size=1							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]							) ->     Any:
						self.apply(self._init_weights							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Tuple     ,					__UpperCAmelCase					:    str							) ->     Any:
						if isinstance(__UpperCAmelCase     ,					nn.Convad							):
									module.weight.data.normal_(mean=0.0     ,					std=self.config.initializer_range							)
									if module.bias is not None:
												module.bias.data.zero_()





			def        SCREAMING_SNAKE_CASE					(       self					:    Union[str, Any]     ,					__UpperCAmelCase					:    torch.Tensor							) ->     torch.Tensor:
						# just take the relevant feature maps
						SCREAMING_SNAKE_CASE__										=   encoder_hidden_states[self.in_index]
						SCREAMING_SNAKE_CASE__										=   self.convs(__UpperCAmelCase							)
						if self.concat_input:
									SCREAMING_SNAKE_CASE__										=   self.conv_cat(torch.cat([hidden_states, output]     ,					dim=1							)							)
						SCREAMING_SNAKE_CASE__										=   self.classifier(__UpperCAmelCase							)
						return output





class  lowerCamelCase    (A__      ):
			lowerCamelCase__ :		str       		=				UperNetConfig
			lowerCamelCase__ :		Optional[Any]       		=				'pixel_values'
			lowerCamelCase__ :		List[Any]       		=				True

			def        SCREAMING_SNAKE_CASE					(       self					:    List[str]     ,					__UpperCAmelCase					:    List[str]							) ->     str:
						if isinstance(__UpperCAmelCase     ,					__UpperCAmelCase							):
									module.backbone.init_weights()
									module.decode_head.init_weights()
									module.auxiliary_head.init_weights()

			def        SCREAMING_SNAKE_CASE					(       self					:    int							) ->     Dict:
						self.backbone.init_weights()
						self.decode_head.init_weights()
						self.auxiliary_head.init_weights()





			def        SCREAMING_SNAKE_CASE					(       self					:    int     ,					__UpperCAmelCase					:    Tuple     ,					__UpperCAmelCase					:    int=False							) ->     int:
						if isinstance(__UpperCAmelCase     ,					__UpperCAmelCase							):
									SCREAMING_SNAKE_CASE__										=   value
A_				:      List[Any]           =			R"\n    Parameters:\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n        config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n"

A_				:      Any           =			R"\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n            [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n        output_attentions (`bool`, *optional*):\n            Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n            `attentions` under returned tensors for more detail.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n            returned tensors for more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"





@add_start_docstrings(
    'UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.'      ,A__      ,)
class  lowerCamelCase    (A__      ):

			def __init__(       self					:    int     ,					__UpperCAmelCase					:    Union[str, Any]							) ->     Any:
						super().__init__(__UpperCAmelCase							)

						SCREAMING_SNAKE_CASE__										=   AutoBackbone.from_config(config.backbone_config							)

						# Semantic segmentation head(s)
						SCREAMING_SNAKE_CASE__										=   UperNetHead(__UpperCAmelCase     ,					in_channels=self.backbone.channels							)
						SCREAMING_SNAKE_CASE__										=   UperNetFCNHead(__UpperCAmelCase							) if config.use_auxiliary_head else None

						# Initialize weights and apply final processing
						self.post_init()





			@add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format("""batch_size, sequence_length"""							)							)
			@replace_return_docstrings(output_type=__UpperCAmelCase     ,					config_class=_CONFIG_FOR_DOC							)
			def        SCREAMING_SNAKE_CASE					(       self					:    Any     ,					__UpperCAmelCase					:    Optional[torch.Tensor] = None     ,					__UpperCAmelCase					:    Optional[bool] = None     ,					__UpperCAmelCase					:    Optional[bool] = None     ,					__UpperCAmelCase					:    Optional[torch.Tensor] = None     ,					__UpperCAmelCase					:    Optional[bool] = None     ,					) ->     Union[tuple, SemanticSegmenterOutput]:
						SCREAMING_SNAKE_CASE__										=   return_dict if return_dict is not None else self.config.use_return_dict
						SCREAMING_SNAKE_CASE__										=   (
						    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
						)
						SCREAMING_SNAKE_CASE__										=   output_attentions if output_attentions is not None else self.config.output_attentions

						SCREAMING_SNAKE_CASE__										=   self.backbone.forward_with_filtered_kwargs(
						    __UpperCAmelCase     ,					output_hidden_states=__UpperCAmelCase     ,					output_attentions=__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   outputs.feature_maps

						SCREAMING_SNAKE_CASE__										=   self.decode_head(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   nn.functional.interpolate(__UpperCAmelCase     ,					size=pixel_values.shape[2:]     ,					mode="""bilinear"""     ,					align_corners=__UpperCAmelCase							)

						SCREAMING_SNAKE_CASE__										=   None
						if self.auxiliary_head is not None:
									SCREAMING_SNAKE_CASE__										=   self.auxiliary_head(__UpperCAmelCase							)
									SCREAMING_SNAKE_CASE__										=   nn.functional.interpolate(
									    __UpperCAmelCase     ,					size=pixel_values.shape[2:]     ,					mode="""bilinear"""     ,					align_corners=__UpperCAmelCase							)

						SCREAMING_SNAKE_CASE__										=   None
						if labels is not None:
									if self.config.num_labels == 1:
												raise ValueError("""The number of labels should be greater than one"""							)
									else:
												# compute weighted loss
												SCREAMING_SNAKE_CASE__										=   CrossEntropyLoss(ignore_index=self.config.loss_ignore_index							)
												SCREAMING_SNAKE_CASE__										=   loss_fct(__UpperCAmelCase     ,					__UpperCAmelCase							)
												SCREAMING_SNAKE_CASE__										=   loss_fct(__UpperCAmelCase     ,					__UpperCAmelCase							)
												SCREAMING_SNAKE_CASE__										=   main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

						if not return_dict:
									if output_hidden_states:
												SCREAMING_SNAKE_CASE__										=   (logits,) + outputs[1:]
									else:
												SCREAMING_SNAKE_CASE__										=   (logits,) + outputs[2:]
									return ((loss,) + output) if loss is not None else output

						return SemanticSegmenterOutput(
						    loss=__UpperCAmelCase     ,					logits=__UpperCAmelCase     ,					hidden_states=outputs.hidden_states     ,					attentions=outputs.attentions     ,					)





"""simple docstring"""




import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
						import jax

						from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel





class  lowerCamelCase    (unittest.TestCase      ):

			def __init__(       self					:    List[Any]     ,					__UpperCAmelCase					:    Dict     ,					__UpperCAmelCase					:    Optional[Any]=1_3     ,					__UpperCAmelCase					:    Tuple=3_0     ,					__UpperCAmelCase					:    str=2     ,					__UpperCAmelCase					:    List[Any]=3     ,					__UpperCAmelCase					:    Any=True     ,					__UpperCAmelCase					:    Dict=True     ,					__UpperCAmelCase					:    Optional[Any]=3_2     ,					__UpperCAmelCase					:    Any=5     ,					__UpperCAmelCase					:    Optional[Any]=4     ,					__UpperCAmelCase					:    Tuple=3_7     ,					__UpperCAmelCase					:    List[str]="gelu"     ,					__UpperCAmelCase					:    Union[str, Any]=0.1     ,					__UpperCAmelCase					:    List[str]=0.1     ,					__UpperCAmelCase					:    int=1_0     ,					__UpperCAmelCase					:    Any=0.02     ,					) ->     Union[str, Any]:
						SCREAMING_SNAKE_CASE__										=   parent
						SCREAMING_SNAKE_CASE__										=   batch_size
						SCREAMING_SNAKE_CASE__										=   image_size
						SCREAMING_SNAKE_CASE__										=   patch_size
						SCREAMING_SNAKE_CASE__										=   num_channels
						SCREAMING_SNAKE_CASE__										=   is_training
						SCREAMING_SNAKE_CASE__										=   use_labels
						SCREAMING_SNAKE_CASE__										=   hidden_size
						SCREAMING_SNAKE_CASE__										=   num_hidden_layers
						SCREAMING_SNAKE_CASE__										=   num_attention_heads
						SCREAMING_SNAKE_CASE__										=   intermediate_size
						SCREAMING_SNAKE_CASE__										=   hidden_act
						SCREAMING_SNAKE_CASE__										=   hidden_dropout_prob
						SCREAMING_SNAKE_CASE__										=   attention_probs_dropout_prob
						SCREAMING_SNAKE_CASE__										=   type_sequence_label_size
						SCREAMING_SNAKE_CASE__										=   initializer_range

						# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
						SCREAMING_SNAKE_CASE__										=   (image_size // patch_size) ** 2
						SCREAMING_SNAKE_CASE__										=   num_patches + 1

			def        SCREAMING_SNAKE_CASE					(       self					:    List[str]							) ->     Any:
						SCREAMING_SNAKE_CASE__										=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

						SCREAMING_SNAKE_CASE__										=   ViTConfig(
						    image_size=self.image_size     ,					patch_size=self.patch_size     ,					num_channels=self.num_channels     ,					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     ,					is_decoder=__UpperCAmelCase     ,					initializer_range=self.initializer_range     ,					)

						return config, pixel_values

			def        SCREAMING_SNAKE_CASE					(       self					:    Dict     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    int							) ->     Optional[Any]:
						SCREAMING_SNAKE_CASE__										=   FlaxViTModel(config=__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   model(__UpperCAmelCase							)
						# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
						SCREAMING_SNAKE_CASE__										=   (self.image_size, self.image_size)
						SCREAMING_SNAKE_CASE__										=   (self.patch_size, self.patch_size)
						SCREAMING_SNAKE_CASE__										=   (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
						self.parent.assertEqual(result.last_hidden_state.shape     ,					(self.batch_size, num_patches + 1, self.hidden_size)							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Union[str, Any]     ,					__UpperCAmelCase					:    Optional[int]     ,					__UpperCAmelCase					:    Optional[Any]							) ->     int:
						SCREAMING_SNAKE_CASE__										=   self.type_sequence_label_size
						SCREAMING_SNAKE_CASE__										=   FlaxViTForImageClassification(config=__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   model(__UpperCAmelCase							)
						self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.type_sequence_label_size)							)

						# test greyscale images
						SCREAMING_SNAKE_CASE__										=   1
						SCREAMING_SNAKE_CASE__										=   FlaxViTForImageClassification(__UpperCAmelCase							)

						SCREAMING_SNAKE_CASE__										=   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
						SCREAMING_SNAKE_CASE__										=   model(__UpperCAmelCase							)





			def        SCREAMING_SNAKE_CASE					(       self					:    Tuple							) ->     str:
						SCREAMING_SNAKE_CASE__										=   self.prepare_config_and_inputs()
						(
						    (
						    SCREAMING_SNAKE_CASE__
						)				,		(
						    SCREAMING_SNAKE_CASE__
						)				,		
						)										=   config_and_inputs
						SCREAMING_SNAKE_CASE__										=   {"""pixel_values""": pixel_values}
						return config, inputs_dict





@require_flax
class  lowerCamelCase    (A__      ,unittest.TestCase      ):
			lowerCamelCase__ :		Tuple       		=				(FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()

			def        SCREAMING_SNAKE_CASE					(       self					:    int							) ->     None:
						SCREAMING_SNAKE_CASE__										=   FlaxViTModelTester(self							)
						SCREAMING_SNAKE_CASE__										=   ConfigTester(self     ,					config_class=__UpperCAmelCase     ,					has_text_modality=__UpperCAmelCase     ,					hidden_size=3_7							)

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

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[int]							) ->     Optional[Any]:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]							) ->     List[str]:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Any							) ->     str:
						SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs_for_common()

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

									SCREAMING_SNAKE_CASE__										=   ["""pixel_values"""]
									self.assertListEqual(arg_names[:1]     ,					__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Any							) ->     Optional[Any]:
						SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									with self.subTest(model_class.__name__							):
												SCREAMING_SNAKE_CASE__										=   self._prepare_for_class(__UpperCAmelCase     ,					__UpperCAmelCase							)
												SCREAMING_SNAKE_CASE__										=   model_class(__UpperCAmelCase							)

												@jax.jit
												def model_jitted(__UpperCAmelCase					:    int     ,					**__UpperCAmelCase					:    Tuple							):
															return model(pixel_values=__UpperCAmelCase     ,					**__UpperCAmelCase							)

												with self.subTest("""JIT Enabled"""							):
															SCREAMING_SNAKE_CASE__										=   model_jitted(**__UpperCAmelCase							).to_tuple()

												with self.subTest("""JIT Disabled"""							):
															with jax.disable_jit():
																		SCREAMING_SNAKE_CASE__										=   model_jitted(**__UpperCAmelCase							).to_tuple()

												self.assertEqual(len(__UpperCAmelCase							)     ,					len(__UpperCAmelCase							)							)
												for jitted_output, output in zip(__UpperCAmelCase     ,					__UpperCAmelCase							):
															self.assertEqual(jitted_output.shape     ,					output.shape							)





			@slow
			def        SCREAMING_SNAKE_CASE					(       self					:    Any							) ->     Dict:
						for model_class_name in self.all_model_classes:
									SCREAMING_SNAKE_CASE__										=   model_class_name.from_pretrained("""google/vit-base-patch16-224"""							)
									SCREAMING_SNAKE_CASE__										=   model(np.ones((1, 3, 2_2_4, 2_2_4)							)							)
									self.assertIsNotNone(__UpperCAmelCase							)


