


import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import MaskaFormerConfig, 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 MaskaFormerForUniversalSegmentation, MaskaFormerModel

    if is_vision_available():
        from transformers import MaskaFormerImageProcessor

if is_vision_available():
    from PIL import Image

class 							lowercase  :







     def __init__(						self :						Optional[int]   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						Any=2   ,   _lowercase :						str=True   ,   _lowercase :						List[Any]=False   ,   _lowercase :						Optional[Any]=10   ,   _lowercase :						Tuple=3   ,   _lowercase :						int=32 * 8   ,   _lowercase :						str=32 * 8   ,   _lowercase :						Optional[Any]=4   ,   _lowercase :						Optional[int]=64   ,   ):
           SCREAMING_SNAKE_CASE__       :  int						     =		parent
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		batch_size
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		is_training
           SCREAMING_SNAKE_CASE__       :  Any						     =		use_auxiliary_loss
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		num_queries
           SCREAMING_SNAKE_CASE__       :  int						     =		num_channels
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		min_size
           SCREAMING_SNAKE_CASE__       :  Dict						     =		max_size
           SCREAMING_SNAKE_CASE__       :  Dict						     =		num_labels
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		hidden_dim
           SCREAMING_SNAKE_CASE__       :  str						     =		hidden_dim







     def 							lowercase__       (						self :						str      ):
           SCREAMING_SNAKE_CASE__       :  str						     =		floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]      ).to(
               _lowercase      )

           SCREAMING_SNAKE_CASE__       :  str						     =		torch.ones([self.batch_size, self.min_size, self.max_size]   ,   device=_lowercase      )

           SCREAMING_SNAKE_CASE__       :  int						     =		(
               torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]   ,   device=_lowercase      ) > 0.5
           ).float()
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		(torch.rand((self.batch_size, self.num_labels)   ,   device=_lowercase      ) > 0.5).long()

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







     def 							lowercase__       (						self :						Dict      ):
           SCREAMING_SNAKE_CASE__       :  str						     =		MaskaFormerConfig(
               hidden_size=self.hidden_dim   ,   )
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		self.num_queries
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		self.num_labels

           SCREAMING_SNAKE_CASE__       :  Any						     =		[1, 1, 1, 1]
           SCREAMING_SNAKE_CASE__       :  int						     =		self.num_channels

           SCREAMING_SNAKE_CASE__       :  List[str]						     =		64
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		1_28
           SCREAMING_SNAKE_CASE__       :  int						     =		self.hidden_dim
           SCREAMING_SNAKE_CASE__       :  int						     =		self.hidden_dim
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		self.hidden_dim
           return config







     def 							lowercase__       (						self :						List[str]      ):
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  List[str]						     =		self.prepare_config_and_inputs()
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		{'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
           return config, inputs_dict







     def 							lowercase__       (						self :						List[Any]   ,   _lowercase :						int   ,   _lowercase :						List[Any]      ):
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		output.encoder_hidden_states
           SCREAMING_SNAKE_CASE__       :  Any						     =		output.pixel_decoder_hidden_states
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		output.transformer_decoder_hidden_states

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







     def 							lowercase__       (						self :						str   ,   _lowercase :						Optional[int]   ,   _lowercase :						Optional[int]   ,   _lowercase :						Tuple   ,   _lowercase :						Any=False      ):
           with torch.no_grad():
                 SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		MaskaFormerModel(config=_lowercase      )
                 model.to(_lowercase      )
                 model.eval()

                 SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		model(pixel_values=_lowercase   ,   pixel_mask=_lowercase      )
                 SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		model(_lowercase   ,   output_hidden_states=_lowercase      )

           self.parent.assertEqual(
               output.transformer_decoder_last_hidden_state.shape   ,   (self.batch_size, self.num_queries, self.hidden_dim)   ,   )
           # 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(_lowercase   ,   _lowercase      )







     def 							lowercase__       (						self :						str   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						List[Any]   ,   _lowercase :						int   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						List[Any]      ):
           SCREAMING_SNAKE_CASE__       :  Any						     =		MaskaFormerForUniversalSegmentation(config=_lowercase      )
           model.to(_lowercase      )
           model.eval()

           def comm_check_on_output(_lowercase :						Optional[int]      ):
                 # 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():
                 SCREAMING_SNAKE_CASE__       :  Any						     =		model(pixel_values=_lowercase   ,   pixel_mask=_lowercase      )
                 SCREAMING_SNAKE_CASE__       :  List[str]						     =		model(_lowercase      )

                 comm_check_on_output(_lowercase      )

                 SCREAMING_SNAKE_CASE__       :  Any						     =		model(
                     pixel_values=_lowercase   ,   pixel_mask=_lowercase   ,   mask_labels=_lowercase   ,   class_labels=_lowercase      )

           comm_check_on_output(_lowercase      )

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




@require_torch
class 							lowercase  (						_UpperCAmelCase    ,       _UpperCAmelCase    ,       unittest.TestCase			):
     lowerCamelCase			:			Any      = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
     lowerCamelCase			:			Dict      = {'''feature-extraction''': MaskaFormerModel} if is_torch_available() else {}

     lowerCamelCase			:			str      = False
     lowerCamelCase			:			Dict      = False
     lowerCamelCase			:			Dict      = False
     lowerCamelCase			:			Any      = False







     def 							lowercase__       (						self :						List[Any]      ):
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		MaskaFormerModelTester(self      )
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		ConfigTester(self   ,   config_class=_lowercase   ,   has_text_modality=_lowercase      )







     def 							lowercase__       (						self :						str      ):
           self.config_tester.run_common_tests()







     def 							lowercase__       (						self :						Optional[int]      ):
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.create_and_check_maskaformer_model(_lowercase   ,   **_lowercase   ,   output_hidden_states=_lowercase      )







     def 							lowercase__       (						self :						Dict      ):
           SCREAMING_SNAKE_CASE__       :  int						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*_lowercase      )







     @unittest.skip(reason='''Mask2Former does not use inputs_embeds'''      )
     def 							lowercase__       (						self :						int      ):
           pass







     @unittest.skip(reason='''Mask2Former does not have a get_input_embeddings method'''      )
     def 							lowercase__       (						self :						List[str]      ):
           pass







     @unittest.skip(reason='''Mask2Former is not a generative model'''      )
     def 							lowercase__       (						self :						List[Any]      ):
           pass







     @unittest.skip(reason='''Mask2Former does not use token embeddings'''      )
     def 							lowercase__       (						self :						Optional[int]      ):
           pass







     @require_torch_multi_gpu
     @unittest.skip(
         reason='''Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''      )
     def 							lowercase__       (						self :						Union[str, Any]      ):
           pass







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







     def 							lowercase__       (						self :						Dict      ):
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  int						     =		self.model_tester.prepare_config_and_inputs_for_common()

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

                 SCREAMING_SNAKE_CASE__       :  Any						     =		['''pixel_values''']
                 self.assertListEqual(arg_names[:1]   ,   _lowercase      )







     @slow
     def 							lowercase__       (						self :						Dict      ):
           for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
                 SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		MaskaFormerModel.from_pretrained(_lowercase      )
                 self.assertIsNotNone(_lowercase      )







     def 							lowercase__       (						self :						Any      ):
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		(self.model_tester.min_size,) * 2
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		{
               '''pixel_values''': torch.randn((2, 3, *size)   ,   device=_lowercase      ),
               '''mask_labels''': torch.randn((2, 10, *size)   ,   device=_lowercase      ),
               '''class_labels''': torch.zeros(2   ,   10   ,   device=_lowercase      ).long(),
           }
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		self.model_tester.get_config()

           SCREAMING_SNAKE_CASE__       :  List[str]						     =		MaskaFormerForUniversalSegmentation(_lowercase      ).to(_lowercase      )
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		model(**_lowercase      )
           self.assertTrue(outputs.loss is not None      )







     def 							lowercase__       (						self :						Optional[Any]      ):
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  str						     =		self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.create_and_check_maskaformer_model(_lowercase   ,   **_lowercase   ,   output_hidden_states=_lowercase      )







     def 							lowercase__       (						self :						Optional[int]      ):
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		model_class(_lowercase      ).to(_lowercase      )
                 SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		model(**_lowercase   ,   output_attentions=_lowercase      )
                 self.assertTrue(outputs.attentions is not None      )







     def 							lowercase__       (						self :						List[Any]      ):
           if not self.model_tester.is_training:
                 return

           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		self.all_model_classes[1]
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  List[str]						     =		self.model_tester.prepare_config_and_inputs()

           SCREAMING_SNAKE_CASE__       :  str						     =		model_class(_lowercase      )
           model.to(_lowercase      )
           model.train()

           SCREAMING_SNAKE_CASE__       :  int						     =		model(_lowercase   ,   mask_labels=_lowercase   ,   class_labels=_lowercase      ).loss
           loss.backward()







     def 							lowercase__       (						self :						Optional[Any]      ):
           SCREAMING_SNAKE_CASE__       :  str						     =		self.all_model_classes[1]
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		self.model_tester.prepare_config_and_inputs()
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		True
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		True

           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		model_class(_lowercase      ).to(_lowercase      )
           model.train()

           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		model(_lowercase   ,   mask_labels=_lowercase   ,   class_labels=_lowercase      )

           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		outputs.encoder_hidden_states[0]
           encoder_hidden_states.retain_grad()

           SCREAMING_SNAKE_CASE__       :  Dict						     =		outputs.pixel_decoder_hidden_states[0]
           pixel_decoder_hidden_states.retain_grad()

           SCREAMING_SNAKE_CASE__       :  Any						     =		outputs.transformer_decoder_hidden_states[0]
           transformer_decoder_hidden_states.retain_grad()

           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		outputs.attentions[0]
           attentions.retain_grad()

           outputs.loss.backward(retain_graph=_lowercase      )

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




a_ :Optional[int]  							=			1e-4




def    a   (							)	-> int:




      '''simple docstring'''

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





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







     @cached_property
     def 							lowercase__       (						self :						Tuple      ):
           return "facebook/mask2former-swin-small-coco-instance"







     @cached_property
     def 							lowercase__       (						self :						Any      ):
           return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints      ) if is_vision_available() else None







     def 							lowercase__       (						self :						Any      ):
           SCREAMING_SNAKE_CASE__       :  int						     =		MaskaFormerModel.from_pretrained(self.model_checkpoints      ).to(_lowercase      )
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		self.default_image_processor
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		prepare_img()
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		image_processor(_lowercase   ,   return_tensors='''pt'''      ).to(_lowercase      )
           SCREAMING_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(_lowercase   ,   (1, 3, 3_84, 3_84)      )

           with torch.no_grad():
                 SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		model(**_lowercase      )

           SCREAMING_SNAKE_CASE__       :  Dict						     =		torch.tensor(
               [[-0.2790, -1.0717, -1.1668], [-0.5128, -0.3128, -0.4987], [-0.5832, 0.1971, -0.0197]]      ).to(_lowercase      )
           self.assertTrue(
               torch.allclose(
                   outputs.encoder_last_hidden_state[0, 0, :3, :3]   ,   _lowercase   ,   atol=_lowercase      )      )

           SCREAMING_SNAKE_CASE__       :  Any						     =		torch.tensor(
               [[0.8973, 1.1847, 1.1776], [1.1934, 1.5040, 1.5128], [1.1153, 1.4486, 1.4951]]      ).to(_lowercase      )
           self.assertTrue(
               torch.allclose(
                   outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]   ,   _lowercase   ,   atol=_lowercase      )      )

           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		torch.tensor(
               [[2.1152, 1.7000, -0.8603], [1.5808, 1.8004, -0.9353], [1.6043, 1.7495, -0.5999]]      ).to(_lowercase      )
           self.assertTrue(
               torch.allclose(
                   outputs.transformer_decoder_last_hidden_state[0, :3, :3]   ,   _lowercase   ,   atol=_lowercase      )      )







     def 							lowercase__       (						self :						int      ):
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints      ).to(_lowercase      ).eval()
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		self.default_image_processor
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		prepare_img()
           SCREAMING_SNAKE_CASE__       :  str						     =		image_processor(_lowercase   ,   return_tensors='''pt'''      ).to(_lowercase      )
           SCREAMING_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(_lowercase   ,   (1, 3, 3_84, 3_84)      )

           with torch.no_grad():
                 SCREAMING_SNAKE_CASE__       :  Tuple						     =		model(**_lowercase      )
           # masks_queries_logits
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		outputs.masks_queries_logits
           self.assertEqual(
               masks_queries_logits.shape   ,   (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)      )
           SCREAMING_SNAKE_CASE__       :  int						     =		[
               [-8.7839, -9.0056, -8.8121],
               [-7.4104, -7.0313, -6.5401],
               [-6.6105, -6.3427, -6.4675],
           ]
           SCREAMING_SNAKE_CASE__       :  str						     =		torch.tensor(_lowercase      ).to(_lowercase      )
           self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]   ,   _lowercase   ,   atol=_lowercase      )      )
           # class_queries_logits
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		outputs.class_queries_logits
           self.assertEqual(class_queries_logits.shape   ,   (1, model.config.num_queries, model.config.num_labels + 1)      )
           SCREAMING_SNAKE_CASE__       :  Dict						     =		torch.tensor(
               [
                   [1.8324, -8.0835, -4.1922],
                   [0.8450, -9.0050, -3.6053],
                   [0.3045, -7.7293, -3.0275],
               ]      ).to(_lowercase      )
           self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]   ,   _lowercase   ,   atol=_lowercase      )      )







     def 							lowercase__       (						self :						str      ):
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints      ).to(_lowercase      ).eval()
           SCREAMING_SNAKE_CASE__       :  Any						     =		self.default_image_processor

           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		image_processor(
               [np.zeros((3, 8_00, 13_33)      ), np.zeros((3, 8_00, 13_33)      )]   ,   segmentation_maps=[np.zeros((3_84, 3_84)      ).astype(np.floataa      ), np.zeros((3_84, 3_84)      ).astype(np.floataa      )]   ,   return_tensors='''pt'''   ,   )

           SCREAMING_SNAKE_CASE__       :  Tuple						     =		inputs['''pixel_values'''].to(_lowercase      )
           SCREAMING_SNAKE_CASE__       :  Dict						     =		[el.to(_lowercase      ) for el in inputs['''mask_labels''']]
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		[el.to(_lowercase      ) for el in inputs['''class_labels''']]

           with torch.no_grad():
                 SCREAMING_SNAKE_CASE__       :  List[str]						     =		model(**_lowercase      )

           self.assertTrue(outputs.loss is not None      )










'''simple docstring'''



import flax.linen as nn
import jax
import jax.numpy as jnp





class   UpperCAmelCase_    (nn.Module    ):





    """simple docstring"""






    lowerCamelCase    :   int
    lowerCamelCase    :   jnp.dtype					=     jnp.floataa







    def  lowercase_							(				self      )							->	Union[str, Any]:
         __lowerCamelCase  :      Optional[Any]														=						nn.Conv(
             self.out_channels     ,	kernel_size=(3, 3)     ,	strides=(1, 1)     ,	padding=((1, 1), (1, 1))     ,	dtype=self.dtype     ,	)







    def __call__(				self     ,	SCREAMING_SNAKE_CASE_      )							->	Any:
         __lowerCamelCase							,			__lowerCamelCase							,			__lowerCamelCase							,			__lowerCamelCase  :      Optional[int]														=						hidden_states.shape
         __lowerCamelCase  :      Dict														=						jax.image.resize(
             SCREAMING_SNAKE_CASE_     ,	shape=(batch, height * 2, width * 2, channels)     ,	method='nearest'     ,	)
         __lowerCamelCase  :      Optional[Any]														=						self.conv(SCREAMING_SNAKE_CASE_      )
         return hidden_states







class   UpperCAmelCase_    (nn.Module    ):





    """simple docstring"""






    lowerCamelCase    :   int
    lowerCamelCase    :   jnp.dtype					=     jnp.floataa







    def  lowercase_							(				self      )							->	List[str]:
         __lowerCamelCase  :      str														=						nn.Conv(
             self.out_channels     ,	kernel_size=(3, 3)     ,	strides=(2, 2)     ,	padding=((1, 1), (1, 1))     ,	dtype=self.dtype     ,	)







    def __call__(				self     ,	SCREAMING_SNAKE_CASE_      )							->	List[str]:
         # pad = ((0, 0), (0, 1), (0, 1), (0, 0))  # pad height and width dim
         # hidden_states = jnp.pad(hidden_states, pad_width=pad)
         __lowerCamelCase  :      str														=						self.conv(SCREAMING_SNAKE_CASE_      )
         return hidden_states







class   UpperCAmelCase_    (nn.Module    ):





    """simple docstring"""






    lowerCamelCase    :   int
    lowerCamelCase    :   int					=     None
    lowerCamelCase    :   float					=     0.0
    lowerCamelCase    :   bool					=     None
    lowerCamelCase    :   jnp.dtype					=     jnp.floataa







    def  lowercase_							(				self      )							->	Optional[int]:
         __lowerCamelCase  :      Optional[Any]														=						self.in_channels if self.out_channels is None else self.out_channels

         __lowerCamelCase  :      Optional[Any]														=						nn.GroupNorm(num_groups=32     ,	epsilon=1E-5      )
         __lowerCamelCase  :      Tuple														=						nn.Conv(
             SCREAMING_SNAKE_CASE_     ,	kernel_size=(3, 3)     ,	strides=(1, 1)     ,	padding=((1, 1), (1, 1))     ,	dtype=self.dtype     ,	)

         __lowerCamelCase  :      List[str]														=						nn.Dense(SCREAMING_SNAKE_CASE_     ,	dtype=self.dtype      )

         __lowerCamelCase  :      Dict														=						nn.GroupNorm(num_groups=32     ,	epsilon=1E-5      )
         __lowerCamelCase  :      int														=						nn.Dropout(self.dropout_prob      )
         __lowerCamelCase  :      Union[str, Any]														=						nn.Conv(
             SCREAMING_SNAKE_CASE_     ,	kernel_size=(3, 3)     ,	strides=(1, 1)     ,	padding=((1, 1), (1, 1))     ,	dtype=self.dtype     ,	)

         __lowerCamelCase  :      Optional[int]														=						self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

         __lowerCamelCase  :      List[Any]														=						None
         if use_nin_shortcut:
              __lowerCamelCase  :      Any														=						nn.Conv(
                  SCREAMING_SNAKE_CASE_     ,	kernel_size=(1, 1)     ,	strides=(1, 1)     ,	padding='VALID'     ,	dtype=self.dtype     ,	)







    def __call__(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_=True      )							->	Tuple:
         __lowerCamelCase  :      List[Any]														=						hidden_states
         __lowerCamelCase  :      str														=						self.norma(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Union[str, Any]														=						nn.swish(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Union[str, Any]														=						self.conva(SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      str														=						self.time_emb_proj(nn.swish(SCREAMING_SNAKE_CASE_      )      )
         __lowerCamelCase  :      List[str]														=						jnp.expand_dims(jnp.expand_dims(SCREAMING_SNAKE_CASE_     ,	1      )     ,	1      )
         __lowerCamelCase  :      Optional[int]														=						hidden_states + temb

         __lowerCamelCase  :      List[Any]														=						self.norma(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Any														=						nn.swish(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      List[Any]														=						self.dropout(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      List[Any]														=						self.conva(SCREAMING_SNAKE_CASE_      )

         if self.conv_shortcut is not None:
              __lowerCamelCase  :      List[str]														=						self.conv_shortcut(SCREAMING_SNAKE_CASE_      )

         return hidden_states + residual






