





from typing import Optional, Tuple, Union

import flax
import flax.linen as nn
import jax
import jax.numpy as jnp
from flax.core.frozen_dict import FrozenDict

from ..configuration_utils import ConfigMixin, flax_register_to_config
from ..utils import BaseOutput
from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps
from .modeling_flax_utils import FlaxModelMixin
from .unet_ad_blocks_flax import (
    FlaxCrossAttnDownBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
)




@flax.struct.dataclass
class        SCREAMING_SNAKE_CASE__					(		UpperCamelCase__ ):
 __SCREAMING_SNAKE_CASE							=      42
 __SCREAMING_SNAKE_CASE							=      42




class        SCREAMING_SNAKE_CASE__					(		nn.Module ):
 __SCREAMING_SNAKE_CASE							=      42
 __SCREAMING_SNAKE_CASE							=      (16, 32, 96, 2_56)
 __SCREAMING_SNAKE_CASE							=      jnp.floataa



 def 							UpperCamelCase     (				self					):
        A__		   =		nn.Conv(
            self.block_out_channels[0],kernel_size=(3, 3),padding=((1, 1), (1, 1)),dtype=self.dtype,)

        A__		   =		[]
        for i in range(len(self.block_out_channels					) - 1					):
               A__		   =		self.block_out_channels[i]
               A__		   =		self.block_out_channels[i + 1]
               A__		   =		nn.Conv(
                   __lowerCamelCase,kernel_size=(3, 3),padding=((1, 1), (1, 1)),dtype=self.dtype,)
               blocks.append(__lowerCamelCase					)
               A__		   =		nn.Conv(
                   __lowerCamelCase,kernel_size=(3, 3),strides=(2, 2),padding=((1, 1), (1, 1)),dtype=self.dtype,)
               blocks.append(__lowerCamelCase					)
        A__		   =		blocks

        A__		   =		nn.Conv(
            self.conditioning_embedding_channels,kernel_size=(3, 3),padding=((1, 1), (1, 1)),kernel_init=nn.initializers.zeros_init(),bias_init=nn.initializers.zeros_init(),dtype=self.dtype,)




 def __call__(				self,__lowerCamelCase					):
        A__		   =		self.conv_in(__lowerCamelCase					)
        A__		   =		nn.silu(__lowerCamelCase					)

        for block in self.blocks:
               A__		   =		block(__lowerCamelCase					)
               A__		   =		nn.silu(__lowerCamelCase					)

        A__		   =		self.conv_out(__lowerCamelCase					)

        return embedding




@flax_register_to_config
class        SCREAMING_SNAKE_CASE__					(		nn.Module							,   UpperCamelCase__							,   UpperCamelCase__ ):
 __SCREAMING_SNAKE_CASE							=      32
 __SCREAMING_SNAKE_CASE							=      4
 __SCREAMING_SNAKE_CASE							=      (
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "DownBlock2D",
 )
 __SCREAMING_SNAKE_CASE							=      False
 __SCREAMING_SNAKE_CASE							=      (3_20, 6_40, 12_80, 12_80)
 __SCREAMING_SNAKE_CASE							=      2
 __SCREAMING_SNAKE_CASE							=      8
 __SCREAMING_SNAKE_CASE							=      None
 __SCREAMING_SNAKE_CASE							=      12_80
 __SCREAMING_SNAKE_CASE							=      0.0
 __SCREAMING_SNAKE_CASE							=      False
 __SCREAMING_SNAKE_CASE							=      jnp.floataa
 __SCREAMING_SNAKE_CASE							=      True
 __SCREAMING_SNAKE_CASE							=      0
 __SCREAMING_SNAKE_CASE							=      "rgb"
 __SCREAMING_SNAKE_CASE							=      (16, 32, 96, 2_56)



 def 							UpperCamelCase     (				self,__lowerCamelCase					):
        # init input tensors
        A__		   =		(1, self.in_channels, self.sample_size, self.sample_size)
        A__		   =		jnp.zeros(__lowerCamelCase,dtype=jnp.floataa					)
        A__		   =		jnp.ones((1,),dtype=jnp.intaa					)
        A__		   =		jnp.zeros((1, 1, self.cross_attention_dim),dtype=jnp.floataa					)
        A__		   =		(1, 3, self.sample_size * 8, self.sample_size * 8)
        A__		   =		jnp.zeros(__lowerCamelCase,dtype=jnp.floataa					)

        A__   ,      A__		   =		jax.random.split(__lowerCamelCase					)
        A__		   =		{'''params''': params_rng, '''dropout''': dropout_rng}

        return self.init(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase					)["params"]



 def 							UpperCamelCase     (				self					):
        A__		   =		self.block_out_channels
        A__		   =		block_out_channels[0] * 4

        # If `num_attention_heads` is not defined (which is the case for most models)
        # it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
        # The reason for this behavior is to correct for incorrectly named variables that were introduced
        # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
        # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
        # which is why we correct for the naming here.
        A__		   =		self.num_attention_heads or self.attention_head_dim

        # input
        A__		   =		nn.Conv(
            block_out_channels[0],kernel_size=(3, 3),strides=(1, 1),padding=((1, 1), (1, 1)),dtype=self.dtype,)

        # time
        A__		   =		FlaxTimesteps(
            block_out_channels[0],flip_sin_to_cos=self.flip_sin_to_cos,freq_shift=self.config.freq_shift					)
        A__		   =		FlaxTimestepEmbedding(__lowerCamelCase,dtype=self.dtype					)

        A__		   =		FlaxControlNetConditioningEmbedding(
            conditioning_embedding_channels=block_out_channels[0],block_out_channels=self.conditioning_embedding_out_channels,)

        A__		   =		self.only_cross_attention
        if isinstance(__lowerCamelCase,__lowerCamelCase					):
               A__		   =		(only_cross_attention,) * len(self.down_block_types					)

        if isinstance(__lowerCamelCase,__lowerCamelCase					):
               A__		   =		(num_attention_heads,) * len(self.down_block_types					)

        # down
        A__		   =		[]
        A__		   =		[]

        A__		   =		block_out_channels[0]

        A__		   =		nn.Conv(
            __lowerCamelCase,kernel_size=(1, 1),padding='''VALID''',kernel_init=nn.initializers.zeros_init(),bias_init=nn.initializers.zeros_init(),dtype=self.dtype,)
        controlnet_down_blocks.append(__lowerCamelCase					)

        for i, down_block_type in enumerate(self.down_block_types					):
               A__		   =		output_channel
               A__		   =		block_out_channels[i]
               A__		   =		i == len(__lowerCamelCase					) - 1

               if down_block_type == "CrossAttnDownBlock2D":
                      A__		   =		FlaxCrossAttnDownBlockaD(
                          in_channels=__lowerCamelCase,out_channels=__lowerCamelCase,dropout=self.dropout,num_layers=self.layers_per_block,num_attention_heads=num_attention_heads[i],add_downsample=not is_final_block,use_linear_projection=self.use_linear_projection,only_cross_attention=only_cross_attention[i],dtype=self.dtype,)
               else:
                      A__		   =		FlaxDownBlockaD(
                          in_channels=__lowerCamelCase,out_channels=__lowerCamelCase,dropout=self.dropout,num_layers=self.layers_per_block,add_downsample=not is_final_block,dtype=self.dtype,)

               down_blocks.append(__lowerCamelCase					)

               for _ in range(self.layers_per_block					):
                      A__		   =		nn.Conv(
                          __lowerCamelCase,kernel_size=(1, 1),padding='''VALID''',kernel_init=nn.initializers.zeros_init(),bias_init=nn.initializers.zeros_init(),dtype=self.dtype,)
                      controlnet_down_blocks.append(__lowerCamelCase					)

               if not is_final_block:
                      A__		   =		nn.Conv(
                          __lowerCamelCase,kernel_size=(1, 1),padding='''VALID''',kernel_init=nn.initializers.zeros_init(),bias_init=nn.initializers.zeros_init(),dtype=self.dtype,)
                      controlnet_down_blocks.append(__lowerCamelCase					)

        A__		   =		down_blocks
        A__		   =		controlnet_down_blocks

        # mid
        A__		   =		block_out_channels[-1]
        A__		   =		FlaxUNetMidBlockaDCrossAttn(
            in_channels=__lowerCamelCase,dropout=self.dropout,num_attention_heads=num_attention_heads[-1],use_linear_projection=self.use_linear_projection,dtype=self.dtype,)

        A__		   =		nn.Conv(
            __lowerCamelCase,kernel_size=(1, 1),padding='''VALID''',kernel_init=nn.initializers.zeros_init(),bias_init=nn.initializers.zeros_init(),dtype=self.dtype,)




 def __call__(				self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase = 1.0,__lowerCamelCase = True,__lowerCamelCase = False,):
        A__		   =		self.controlnet_conditioning_channel_order
        if channel_order == "bgr":
               A__		   =		jnp.flip(__lowerCamelCase,axis=1					)

        # 1. time
        if not isinstance(__lowerCamelCase,jnp.ndarray					):
               A__		   =		jnp.array([timesteps],dtype=jnp.intaa					)
        elif isinstance(__lowerCamelCase,jnp.ndarray					) and len(timesteps.shape					) == 0:
               A__		   =		timesteps.astype(dtype=jnp.floataa					)
               A__		   =		jnp.expand_dims(__lowerCamelCase,0					)

        A__		   =		self.time_proj(__lowerCamelCase					)
        A__		   =		self.time_embedding(__lowerCamelCase					)

        # 2. pre-process
        A__		   =		jnp.transpose(__lowerCamelCase,(0, 2, 3, 1)					)
        A__		   =		self.conv_in(__lowerCamelCase					)

        A__		   =		jnp.transpose(__lowerCamelCase,(0, 2, 3, 1)					)
        A__		   =		self.controlnet_cond_embedding(__lowerCamelCase					)
        sample += controlnet_cond

        # 3. down
        A__		   =		(sample,)
        for down_block in self.down_blocks:
               if isinstance(__lowerCamelCase,__lowerCamelCase					):
                      A__   ,      A__		   =		down_block(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,deterministic=not train					)
               else:
                      A__   ,      A__		   =		down_block(__lowerCamelCase,__lowerCamelCase,deterministic=not train					)
               down_block_res_samples += res_samples

        # 4. mid
        A__		   =		self.mid_block(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,deterministic=not train					)

        # 5. contronet blocks
        A__		   =		()
        for down_block_res_sample, controlnet_block in zip(__lowerCamelCase,self.controlnet_down_blocks					):
               A__		   =		controlnet_block(__lowerCamelCase					)
               controlnet_down_block_res_samples += (down_block_res_sample,)

        A__		   =		controlnet_down_block_res_samples

        A__		   =		self.controlnet_mid_block(__lowerCamelCase					)

        # 6. scaling
        A__		   =		[sample * conditioning_scale for sample in down_block_res_samples]
        mid_block_res_sample *= conditioning_scale

        if not return_dict:
               return (down_block_res_samples, mid_block_res_sample)

        return FlaxControlNetOutput(
            down_block_res_samples=__lowerCamelCase,mid_block_res_sample=__lowerCamelCase					)












import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
       from PIL import Image

       from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
       import torch

if is_tf_available():
       import tensorflow as tf




@require_vision
@require_torchvision
class        SCREAMING_SNAKE_CASE__					(		unittest.TestCase ):



 def 							UpperCamelCase     (				self					):
        A__		   =		tempfile.mkdtemp()
        A__		   =		SamImageProcessor()
        A__		   =		SamProcessor(__lowerCamelCase					)
        processor.save_pretrained(self.tmpdirname					)



 def 							UpperCamelCase     (				self,**__lowerCamelCase					):
        return AutoProcessor.from_pretrained(self.tmpdirname,**__lowerCamelCase					).image_processor



 def 							UpperCamelCase     (				self					):
        shutil.rmtree(self.tmpdirname					)



 def 							UpperCamelCase     (				self					):
        A__		   =		[np.random.randint(255,size=(3, 30, 400),dtype=np.uinta					)]

        A__		   =		[Image.fromarray(np.moveaxis(__lowerCamelCase,0,-1					)					) for x in image_inputs]

        return image_inputs



 def 							UpperCamelCase     (				self					):
        A__		   =		SamProcessor(image_processor=self.get_image_processor()					)
        processor.save_pretrained(self.tmpdirname					)

        A__		   =		self.get_image_processor(do_normalize=__lowerCamelCase,padding_value=1.0					)

        A__		   =		SamProcessor.from_pretrained(self.tmpdirname,do_normalize=__lowerCamelCase,padding_value=1.0					)

        self.assertEqual(processor.image_processor.to_json_string(),image_processor_add_kwargs.to_json_string()					)
        self.assertIsInstance(processor.image_processor,__lowerCamelCase					)



 def 							UpperCamelCase     (				self					):
        A__		   =		self.get_image_processor()

        A__		   =		SamProcessor(image_processor=__lowerCamelCase					)

        A__		   =		self.prepare_image_inputs()

        A__		   =		image_processor(__lowerCamelCase,return_tensors='''np'''					)
        A__		   =		processor(images=__lowerCamelCase,return_tensors='''np'''					)

        input_feat_extract.pop('''original_sizes'''					)  # pop original_sizes as it is popped in the processor
        input_feat_extract.pop('''reshaped_input_sizes'''					)  # pop original_sizes as it is popped in the processor

        for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum(),input_processor[key].sum(),delta=1E-2					)




 @require_torch
 def 							UpperCamelCase     (				self					):
        A__		   =		self.get_image_processor()

        A__		   =		SamProcessor(image_processor=__lowerCamelCase					)
        A__		   =		[torch.ones((1, 3, 5, 5)					)]

        A__		   =		[[1764, 2646]]

        A__		   =		[[683, 1024]]
        A__		   =		processor.post_process_masks(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase					)
        self.assertEqual(masks[0].shape,(1, 3, 1764, 2646)					)

        A__		   =		processor.post_process_masks(
            __lowerCamelCase,torch.tensor(__lowerCamelCase					),torch.tensor(__lowerCamelCase					)					)
        self.assertEqual(masks[0].shape,(1, 3, 1764, 2646)					)

        # should also work with np
        A__		   =		[np.ones((1, 3, 5, 5)					)]
        A__		   =		processor.post_process_masks(__lowerCamelCase,np.array(__lowerCamelCase					),np.array(__lowerCamelCase					)					)

        self.assertEqual(masks[0].shape,(1, 3, 1764, 2646)					)

        A__		   =		[[1, 0], [0, 1]]
        with self.assertRaises(__lowerCamelCase					):
               A__		   =		processor.post_process_masks(__lowerCamelCase,np.array(__lowerCamelCase					),np.array(__lowerCamelCase					)					)




@require_vision
@require_tf
class        SCREAMING_SNAKE_CASE__					(		unittest.TestCase ):



 def 							UpperCamelCase     (				self					):
        A__		   =		tempfile.mkdtemp()
        A__		   =		SamImageProcessor()
        A__		   =		SamProcessor(__lowerCamelCase					)
        processor.save_pretrained(self.tmpdirname					)



 def 							UpperCamelCase     (				self,**__lowerCamelCase					):
        return AutoProcessor.from_pretrained(self.tmpdirname,**__lowerCamelCase					).image_processor



 def 							UpperCamelCase     (				self					):
        shutil.rmtree(self.tmpdirname					)



 def 							UpperCamelCase     (				self					):
        A__		   =		[np.random.randint(255,size=(3, 30, 400),dtype=np.uinta					)]

        A__		   =		[Image.fromarray(np.moveaxis(__lowerCamelCase,0,-1					)					) for x in image_inputs]

        return image_inputs



 def 							UpperCamelCase     (				self					):
        A__		   =		SamProcessor(image_processor=self.get_image_processor()					)
        processor.save_pretrained(self.tmpdirname					)

        A__		   =		self.get_image_processor(do_normalize=__lowerCamelCase,padding_value=1.0					)

        A__		   =		SamProcessor.from_pretrained(self.tmpdirname,do_normalize=__lowerCamelCase,padding_value=1.0					)

        self.assertEqual(processor.image_processor.to_json_string(),image_processor_add_kwargs.to_json_string()					)
        self.assertIsInstance(processor.image_processor,__lowerCamelCase					)



 def 							UpperCamelCase     (				self					):
        A__		   =		self.get_image_processor()

        A__		   =		SamProcessor(image_processor=__lowerCamelCase					)

        A__		   =		self.prepare_image_inputs()

        A__		   =		image_processor(__lowerCamelCase,return_tensors='''np'''					)
        A__		   =		processor(images=__lowerCamelCase,return_tensors='''np'''					)

        input_feat_extract.pop('''original_sizes'''					)  # pop original_sizes as it is popped in the processor
        input_feat_extract.pop('''reshaped_input_sizes'''					)  # pop reshaped_input_sizes as it is popped in the processor

        for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum(),input_processor[key].sum(),delta=1E-2					)




 @require_tf
 def 							UpperCamelCase     (				self					):
        A__		   =		self.get_image_processor()

        A__		   =		SamProcessor(image_processor=__lowerCamelCase					)
        A__		   =		[tf.ones((1, 3, 5, 5)					)]

        A__		   =		[[1764, 2646]]

        A__		   =		[[683, 1024]]
        A__		   =		processor.post_process_masks(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,return_tensors='''tf'''					)
        self.assertEqual(masks[0].shape,(1, 3, 1764, 2646)					)

        A__		   =		processor.post_process_masks(
            __lowerCamelCase,tf.convert_to_tensor(__lowerCamelCase					),tf.convert_to_tensor(__lowerCamelCase					),return_tensors='''tf''',)
        self.assertEqual(masks[0].shape,(1, 3, 1764, 2646)					)

        # should also work with np
        A__		   =		[np.ones((1, 3, 5, 5)					)]
        A__		   =		processor.post_process_masks(
            __lowerCamelCase,np.array(__lowerCamelCase					),np.array(__lowerCamelCase					),return_tensors='''tf'''					)

        self.assertEqual(masks[0].shape,(1, 3, 1764, 2646)					)

        A__		   =		[[1, 0], [0, 1]]
        with self.assertRaises(tf.errors.InvalidArgumentError					):
               A__		   =		processor.post_process_masks(
                   __lowerCamelCase,np.array(__lowerCamelCase					),np.array(__lowerCamelCase					),return_tensors='''tf'''					)




@require_vision
@require_torchvision
class        SCREAMING_SNAKE_CASE__					(		unittest.TestCase ):



 def 							UpperCamelCase     (				self					):
        A__		   =		tempfile.mkdtemp()
        A__		   =		SamImageProcessor()
        A__		   =		SamProcessor(__lowerCamelCase					)
        processor.save_pretrained(self.tmpdirname					)



 def 							UpperCamelCase     (				self,**__lowerCamelCase					):
        return AutoProcessor.from_pretrained(self.tmpdirname,**__lowerCamelCase					).image_processor



 def 							UpperCamelCase     (				self					):
        shutil.rmtree(self.tmpdirname					)



 def 							UpperCamelCase     (				self					):
        A__		   =		[np.random.randint(255,size=(3, 30, 400),dtype=np.uinta					)]

        A__		   =		[Image.fromarray(np.moveaxis(__lowerCamelCase,0,-1					)					) for x in image_inputs]

        return image_inputs



 @is_pt_tf_cross_test
 def 							UpperCamelCase     (				self					):
        A__		   =		self.get_image_processor()

        A__		   =		SamProcessor(image_processor=__lowerCamelCase					)
        A__		   =		np.random.randint(0,2,size=(1, 3, 5, 5)					).astype(np.floataa					)
        A__		   =		[tf.convert_to_tensor(__lowerCamelCase					)]
        A__		   =		[torch.tensor(__lowerCamelCase					)]

        A__		   =		[[1764, 2646]]

        A__		   =		[[683, 1024]]
        A__		   =		processor.post_process_masks(
            __lowerCamelCase,__lowerCamelCase,__lowerCamelCase,return_tensors='''tf'''					)
        A__		   =		processor.post_process_masks(
            __lowerCamelCase,__lowerCamelCase,__lowerCamelCase,return_tensors='''pt'''					)

        self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()					)					)




 @is_pt_tf_cross_test
 def 							UpperCamelCase     (				self					):
        A__		   =		self.get_image_processor()

        A__		   =		SamProcessor(image_processor=__lowerCamelCase					)

        A__		   =		self.prepare_image_inputs()

        A__		   =		image_processor(__lowerCamelCase,return_tensors='''pt'''					)['''pixel_values'''].numpy()
        A__		   =		processor(images=__lowerCamelCase,return_tensors='''pt'''					)['''pixel_values'''].numpy()

        A__		   =		image_processor(__lowerCamelCase,return_tensors='''tf'''					)['''pixel_values'''].numpy()
        A__		   =		processor(images=__lowerCamelCase,return_tensors='''tf'''					)['''pixel_values'''].numpy()

        self.assertTrue(np.allclose(__lowerCamelCase,__lowerCamelCase					)					)
        self.assertTrue(np.allclose(__lowerCamelCase,__lowerCamelCase					)					)
        self.assertTrue(np.allclose(__lowerCamelCase,__lowerCamelCase					)					)





