





import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
			import torch

if is_vision_available():
			from PIL import Image

			from transformers import YolosImageProcessor






class 					__A    (    unittest.TestCase ):

			'''simple docstring'''







			def __init__(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase=7       ,  __lowerCAmelCase=3       ,  __lowerCAmelCase=3_0       ,  __lowerCAmelCase=4_0_0       ,  __lowerCAmelCase=True       ,  __lowerCAmelCase=None       ,  __lowerCAmelCase=True       ,  __lowerCAmelCase=[0.5, 0.5, 0.5]       ,  __lowerCAmelCase=[0.5, 0.5, 0.5]       ,  __lowerCAmelCase=True       ,  __lowerCAmelCase=1 / 2_5_5       ,  __lowerCAmelCase=True       ,  ):



				'''simple docstring'''







				lowerCamelCase__								=		size if size is not None else {'''shortest_edge''': 1_8, '''longest_edge''': 1_3_3_3}
				lowerCamelCase__								=		parent
				lowerCamelCase__								=		batch_size
				lowerCamelCase__								=		num_channels
				lowerCamelCase__								=		min_resolution
				lowerCamelCase__								=		max_resolution
				lowerCamelCase__								=		do_resize
				lowerCamelCase__								=		size
				lowerCamelCase__								=		do_normalize
				lowerCamelCase__								=		image_mean
				lowerCamelCase__								=		image_std
				lowerCamelCase__								=		do_rescale
				lowerCamelCase__								=		rescale_factor
				lowerCamelCase__								=		do_pad





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				return {
				    "do_resize": self.do_resize,
				    "size": self.size,
				    "do_normalize": self.do_normalize,
				    "image_mean": self.image_mean,
				    "image_std": self.image_std,
				    "do_rescale": self.do_rescale,
				    "rescale_factor": self.rescale_factor,
				    "do_pad": self.do_pad,
				}





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase=False							):



				'''simple docstring'''







				if not batched:
					lowerCamelCase__								=		image_inputs[0]
					if isinstance(__lowerCAmelCase       ,  Image.Image							):
						lowerCamelCase__				,	lowerCamelCase__								=		image.size
					else:
						lowerCamelCase__				,	lowerCamelCase__								=		image.shape[1], image.shape[2]
					if w < h:
						lowerCamelCase__								=		int(self.size['''shortest_edge'''] * h / w							)
						lowerCamelCase__								=		self.size['''shortest_edge''']
					elif w > h:
						lowerCamelCase__								=		self.size['''shortest_edge''']
						lowerCamelCase__								=		int(self.size['''shortest_edge'''] * w / h							)
					else:
						lowerCamelCase__								=		self.size['''shortest_edge''']
						lowerCamelCase__								=		self.size['''shortest_edge''']

				else:
					lowerCamelCase__								=		[]
					for image in image_inputs:
						lowerCamelCase__				,	lowerCamelCase__								=		self.get_expected_values([image]							)
						expected_values.append((expected_height, expected_width)							)
					lowerCamelCase__								=		max(__lowerCAmelCase       ,  key=lambda __lowerCAmelCase							: item[0]							)[0]
					lowerCamelCase__								=		max(__lowerCAmelCase       ,  key=lambda __lowerCAmelCase							: item[1]							)[1]

				return expected_height, expected_width






@require_torch
@require_vision
class 					__A    (    lowerCAmelCase			,				unittest.TestCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						YolosImageProcessor if is_vision_available() else None





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		YolosImageProcessingTester(self							)





			@property
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				return self.image_processor_tester.prepare_image_processor_dict()





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.image_processing_class(**self.image_processor_dict							)
				self.assertTrue(hasattr(__lowerCAmelCase       ,  '''image_mean'''							)							)
				self.assertTrue(hasattr(__lowerCAmelCase       ,  '''image_std'''							)							)
				self.assertTrue(hasattr(__lowerCAmelCase       ,  '''do_normalize'''							)							)
				self.assertTrue(hasattr(__lowerCAmelCase       ,  '''do_resize'''							)							)
				self.assertTrue(hasattr(__lowerCAmelCase       ,  '''size'''							)							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.image_processing_class.from_dict(self.image_processor_dict							)
				self.assertEqual(image_processor.size       ,  {'''shortest_edge''': 1_8, '''longest_edge''': 1_3_3_3}							)
				self.assertEqual(image_processor.do_pad       ,  __lowerCAmelCase							)

				lowerCamelCase__								=		self.image_processing_class.from_dict(
				    self.image_processor_dict       ,  size=4_2       ,  max_size=8_4       ,  pad_and_return_pixel_mask=__lowerCAmelCase							)
				self.assertEqual(image_processor.size       ,  {'''shortest_edge''': 4_2, '''longest_edge''': 8_4}							)
				self.assertEqual(image_processor.do_pad       ,  __lowerCAmelCase							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				pass





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.image_processing_class(**self.image_processor_dict							)
				# create random PIL images
				lowerCamelCase__								=		prepare_image_inputs(self.image_processor_tester       ,  equal_resolution=__lowerCAmelCase							)
				for image in image_inputs:
					self.assertIsInstance(__lowerCAmelCase       ,  Image.Image							)

				# Test not batched input
				lowerCamelCase__								=		image_processing(image_inputs[0]       ,  return_tensors='''pt'''							).pixel_values

				lowerCamelCase__				,	lowerCamelCase__								=		self.image_processor_tester.get_expected_values(__lowerCAmelCase							)

				self.assertEqual(
				    encoded_images.shape       ,  (1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,  )

				# Test batched
				lowerCamelCase__				,	lowerCamelCase__								=		self.image_processor_tester.get_expected_values(__lowerCAmelCase       ,  batched=__lowerCAmelCase							)

				lowerCamelCase__								=		image_processing(__lowerCAmelCase       ,  return_tensors='''pt'''							).pixel_values
				self.assertEqual(
				    encoded_images.shape       ,  (
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        expected_height,
				        expected_width,
				    )       ,  )





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.image_processing_class(**self.image_processor_dict							)
				# create random numpy tensors
				lowerCamelCase__								=		prepare_image_inputs(self.image_processor_tester       ,  equal_resolution=__lowerCAmelCase       ,  numpify=__lowerCAmelCase							)
				for image in image_inputs:
					self.assertIsInstance(__lowerCAmelCase       ,  np.ndarray							)

				# Test not batched input
				lowerCamelCase__								=		image_processing(image_inputs[0]       ,  return_tensors='''pt'''							).pixel_values

				lowerCamelCase__				,	lowerCamelCase__								=		self.image_processor_tester.get_expected_values(__lowerCAmelCase							)

				self.assertEqual(
				    encoded_images.shape       ,  (1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,  )

				# Test batched
				lowerCamelCase__								=		image_processing(__lowerCAmelCase       ,  return_tensors='''pt'''							).pixel_values

				lowerCamelCase__				,	lowerCamelCase__								=		self.image_processor_tester.get_expected_values(__lowerCAmelCase       ,  batched=__lowerCAmelCase							)

				self.assertEqual(
				    encoded_images.shape       ,  (
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        expected_height,
				        expected_width,
				    )       ,  )





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.image_processing_class(**self.image_processor_dict							)
				# create random PyTorch tensors
				lowerCamelCase__								=		prepare_image_inputs(self.image_processor_tester       ,  equal_resolution=__lowerCAmelCase       ,  torchify=__lowerCAmelCase							)
				for image in image_inputs:
					self.assertIsInstance(__lowerCAmelCase       ,  torch.Tensor							)

				# Test not batched input
				lowerCamelCase__								=		image_processing(image_inputs[0]       ,  return_tensors='''pt'''							).pixel_values

				lowerCamelCase__				,	lowerCamelCase__								=		self.image_processor_tester.get_expected_values(__lowerCAmelCase							)

				self.assertEqual(
				    encoded_images.shape       ,  (1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,  )

				# Test batched
				lowerCamelCase__								=		image_processing(__lowerCAmelCase       ,  return_tensors='''pt'''							).pixel_values

				lowerCamelCase__				,	lowerCamelCase__								=		self.image_processor_tester.get_expected_values(__lowerCAmelCase       ,  batched=__lowerCAmelCase							)

				self.assertEqual(
				    encoded_images.shape       ,  (
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        expected_height,
				        expected_width,
				    )       ,  )





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.image_processing_class(**self.image_processor_dict							)
				lowerCamelCase__								=		self.image_processing_class(do_resize=__lowerCAmelCase       ,  do_normalize=__lowerCAmelCase       ,  do_rescale=__lowerCAmelCase							)
				# create random PyTorch tensors
				lowerCamelCase__								=		prepare_image_inputs(self.image_processor_tester       ,  equal_resolution=__lowerCAmelCase       ,  torchify=__lowerCAmelCase							)
				for image in image_inputs:
					self.assertIsInstance(__lowerCAmelCase       ,  torch.Tensor							)

				# Test whether the method "pad" and calling the image processor return the same tensors
				lowerCamelCase__								=		image_processing_a.pad(__lowerCAmelCase       ,  return_tensors='''pt'''							)
				lowerCamelCase__								=		image_processing_a(__lowerCAmelCase       ,  return_tensors='''pt'''							)

				self.assertTrue(
				    torch.allclose(encoded_images_with_method['''pixel_values''']       ,  encoded_images['''pixel_values''']       ,  atol=1E-4							)							)





			@slow
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
				with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt'''       ,  '''r'''							) as f:
					lowerCamelCase__								=		json.loads(f.read()							)

				lowerCamelCase__								=		{'''image_id''': 3_9_7_6_9, '''annotations''': target}

				# encode them
				lowerCamelCase__								=		YolosImageProcessor.from_pretrained('''hustvl/yolos-small'''							)
				lowerCamelCase__								=		image_processing(images=__lowerCAmelCase       ,  annotations=__lowerCAmelCase       ,  return_tensors='''pt'''							)

				# verify pixel values
				lowerCamelCase__								=		torch.Size([1, 3, 8_0_0, 1_0_6_6]							)
				self.assertEqual(encoding['''pixel_values'''].shape       ,  __lowerCAmelCase							)

				lowerCamelCase__								=		torch.tensor([0.2796, 0.3138, 0.3481]							)
				self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]       ,  __lowerCAmelCase       ,  atol=1E-4							)							)

				# verify area
				lowerCamelCase__								=		torch.tensor([5887.9600, 1_1250.2061, 48_9353.8438, 83_7122.7500, 14_7967.5156, 16_5732.3438]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']       ,  __lowerCAmelCase							)							)
				# verify boxes
				lowerCamelCase__								=		torch.Size([6, 4]							)
				self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape       ,  __lowerCAmelCase							)
				lowerCamelCase__								=		torch.tensor([0.5503, 0.2765, 0.0604, 0.2215]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]       ,  __lowerCAmelCase       ,  atol=1E-3							)							)
				# verify image_id
				lowerCamelCase__								=		torch.tensor([3_9_7_6_9]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']       ,  __lowerCAmelCase							)							)
				# verify is_crowd
				lowerCamelCase__								=		torch.tensor([0, 0, 0, 0, 0, 0]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']       ,  __lowerCAmelCase							)							)
				# verify class_labels
				lowerCamelCase__								=		torch.tensor([7_5, 7_5, 6_3, 6_5, 1_7, 1_7]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']       ,  __lowerCAmelCase							)							)
				# verify orig_size
				lowerCamelCase__								=		torch.tensor([4_8_0, 6_4_0]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']       ,  __lowerCAmelCase							)							)
				# verify size
				lowerCamelCase__								=		torch.tensor([8_0_0, 1_0_6_6]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']       ,  __lowerCAmelCase							)							)





			@slow
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
				with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt'''       ,  '''r'''							) as f:
					lowerCamelCase__								=		json.loads(f.read()							)

				lowerCamelCase__								=		{'''file_name''': '''000000039769.png''', '''image_id''': 3_9_7_6_9, '''segments_info''': target}

				lowerCamelCase__								=		pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic'''							)

				# encode them
				lowerCamelCase__								=		YolosImageProcessor(format='''coco_panoptic'''							)
				lowerCamelCase__								=		image_processing(images=__lowerCAmelCase       ,  annotations=__lowerCAmelCase       ,  masks_path=__lowerCAmelCase       ,  return_tensors='''pt'''							)

				# verify pixel values
				lowerCamelCase__								=		torch.Size([1, 3, 8_0_0, 1_0_6_6]							)
				self.assertEqual(encoding['''pixel_values'''].shape       ,  __lowerCAmelCase							)

				lowerCamelCase__								=		torch.tensor([0.2796, 0.3138, 0.3481]							)
				self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]       ,  __lowerCAmelCase       ,  atol=1E-4							)							)

				# verify area
				lowerCamelCase__								=		torch.tensor([14_7979.6875, 16_5527.0469, 48_4638.5938, 1_1292.9375, 5879.6562, 7634.1147]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']       ,  __lowerCAmelCase							)							)
				# verify boxes
				lowerCamelCase__								=		torch.Size([6, 4]							)
				self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape       ,  __lowerCAmelCase							)
				lowerCamelCase__								=		torch.tensor([0.2625, 0.5437, 0.4688, 0.8625]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]       ,  __lowerCAmelCase       ,  atol=1E-3							)							)
				# verify image_id
				lowerCamelCase__								=		torch.tensor([3_9_7_6_9]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']       ,  __lowerCAmelCase							)							)
				# verify is_crowd
				lowerCamelCase__								=		torch.tensor([0, 0, 0, 0, 0, 0]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']       ,  __lowerCAmelCase							)							)
				# verify class_labels
				lowerCamelCase__								=		torch.tensor([1_7, 1_7, 6_3, 7_5, 7_5, 9_3]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']       ,  __lowerCAmelCase							)							)
				# verify masks
				lowerCamelCase__								=		8_2_2_8_7_3
				self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item()       ,  __lowerCAmelCase							)
				# verify orig_size
				lowerCamelCase__								=		torch.tensor([4_8_0, 6_4_0]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']       ,  __lowerCAmelCase							)							)
				# verify size
				lowerCamelCase__								=		torch.tensor([8_0_0, 1_0_6_6]							)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']       ,  __lowerCAmelCase							)							)










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 					__A    (    lowerCAmelCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						42
			lowerCAmelCase_							     =						42






class 					__A    (    nn.Module ):

			'''simple docstring'''







			lowerCAmelCase_							     =						42
			lowerCAmelCase_							     =						(16, 32, 96, 256)
			lowerCAmelCase_							     =						jnp.floataa





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		nn.Conv(
				    self.block_out_channels[0]       ,  kernel_size=(3, 3)       ,  padding=((1, 1), (1, 1))       ,  dtype=self.dtype       ,  )

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

				lowerCamelCase__								=		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							):



				'''simple docstring'''







				lowerCamelCase__								=		self.conv_in(__lowerCAmelCase							)
				lowerCamelCase__								=		nn.silu(__lowerCAmelCase							)

				for block in self.blocks:
					lowerCamelCase__								=		block(__lowerCAmelCase							)
					lowerCamelCase__								=		nn.silu(__lowerCAmelCase							)

				lowerCamelCase__								=		self.conv_out(__lowerCAmelCase							)

				return embedding






@flax_register_to_config
class 					__A    (    nn.Module			,				lowerCAmelCase			,				lowerCAmelCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						32
			lowerCAmelCase_							     =						4
			lowerCAmelCase_							     =						(
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "DownBlock2D",
			)
			lowerCAmelCase_							     =						False
			lowerCAmelCase_							     =						(320, 640, 1280, 1280)
			lowerCAmelCase_							     =						2
			lowerCAmelCase_							     =						8
			lowerCAmelCase_							     =						None
			lowerCAmelCase_							     =						1280
			lowerCAmelCase_							     =						0.0
			lowerCAmelCase_							     =						False
			lowerCAmelCase_							     =						jnp.floataa
			lowerCAmelCase_							     =						True
			lowerCAmelCase_							     =						0
			lowerCAmelCase_							     =						"rgb"
			lowerCAmelCase_							     =						(16, 32, 96, 256)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		(1, self.in_channels, self.sample_size, self.sample_size)
				lowerCamelCase__								=		jnp.zeros(__lowerCAmelCase       ,  dtype=jnp.floataa							)
				lowerCamelCase__								=		jnp.ones((1,)       ,  dtype=jnp.intaa							)
				lowerCamelCase__								=		jnp.zeros((1, 1, self.cross_attention_dim)       ,  dtype=jnp.floataa							)
				lowerCamelCase__								=		(1, 3, self.sample_size * 8, self.sample_size * 8)
				lowerCamelCase__								=		jnp.zeros(__lowerCAmelCase       ,  dtype=jnp.floataa							)

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

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





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.block_out_channels
				lowerCamelCase__								=		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.
				lowerCamelCase__								=		self.num_attention_heads or self.attention_head_dim

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

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

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

				lowerCamelCase__								=		self.only_cross_attention
				if isinstance(__lowerCAmelCase       ,  __lowerCAmelCase							):
					lowerCamelCase__								=		(only_cross_attention,) * len(self.down_block_types							)

				if isinstance(__lowerCAmelCase       ,  __lowerCAmelCase							):
					lowerCamelCase__								=		(num_attention_heads,) * len(self.down_block_types							)

				# down
				lowerCamelCase__								=		[]
				lowerCamelCase__								=		[]

				lowerCamelCase__								=		block_out_channels[0]

				lowerCamelCase__								=		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							):
					lowerCamelCase__								=		output_channel
					lowerCamelCase__								=		block_out_channels[i]
					lowerCamelCase__								=		i == len(__lowerCAmelCase							) - 1

					if down_block_type == "CrossAttnDownBlock2D":
						lowerCamelCase__								=		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:
						lowerCamelCase__								=		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							):
						lowerCamelCase__								=		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:
						lowerCamelCase__								=		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							)

				lowerCamelCase__								=		down_blocks
				lowerCamelCase__								=		controlnet_down_blocks

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

				lowerCamelCase__								=		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       ,  ):



				'''simple docstring'''







				lowerCamelCase__								=		self.controlnet_conditioning_channel_order
				if channel_order == "bgr":
					lowerCamelCase__								=		jnp.flip(__lowerCAmelCase       ,  axis=1							)

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

				lowerCamelCase__								=		self.time_proj(__lowerCAmelCase							)
				lowerCamelCase__								=		self.time_embedding(__lowerCAmelCase							)

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

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

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

				# 4. mid
				lowerCamelCase__								=		self.mid_block(__lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  deterministic=not train							)

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

				lowerCamelCase__								=		controlnet_down_block_res_samples

				lowerCamelCase__								=		self.controlnet_mid_block(__lowerCAmelCase							)

				# 6. scaling
				lowerCamelCase__								=		[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							)



