



"""simple docstring"""






import collections
import inspect
import unittest

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

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    FocalNetBackbone,
		    FocalNetForImageClassification,
		    FocalNetForMaskedImageModeling,
		    FocalNetModel,
		)
		from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor

class 	__SCREAMING_SNAKE_CASE     :


					'''simple docstring'''





					def __init__(	self							:  str,  lowerCamelCase							:  Union[str, Any],  lowerCamelCase							:  Dict=13,  lowerCamelCase							:  Optional[Any]=32,  lowerCamelCase							:  Optional[Any]=2,  lowerCamelCase							:  List[Any]=3,  lowerCamelCase							:  List[Any]=16,  lowerCamelCase							:  str=[32, 64, 128],  lowerCamelCase							:  Union[str, Any]=[1, 2, 1],  lowerCamelCase							:  Union[str, Any]=[2, 2, 4],  lowerCamelCase							:  Tuple=2,  lowerCamelCase							:  Union[str, Any]=2.0,  lowerCamelCase							:  str=True,  lowerCamelCase							:  Dict=0.0,  lowerCamelCase							:  List[Any]=0.0,  lowerCamelCase							:  int=0.1,  lowerCamelCase							:  Dict="gelu",  lowerCamelCase							:  Optional[Any]=False,  lowerCamelCase							:  Dict=True,  lowerCamelCase							:  Optional[Any]=0.02,  lowerCamelCase							:  int=1E-5,  lowerCamelCase							:  Dict=True,  lowerCamelCase							:  Optional[int]=None,  lowerCamelCase							:  Tuple=True,  lowerCamelCase							:  Optional[Any]=10,  lowerCamelCase							:  Tuple=8,  lowerCamelCase							:  Tuple=["stage1", "stage2"],  lowerCamelCase							:  Optional[int]=[1, 2],  )-> str:
								lowerCamelCase__		:		Union[str, Any]		     =parent
								lowerCamelCase__		:		List[Any]		     =batch_size
								lowerCamelCase__		:		List[Any]		     =image_size
								lowerCamelCase__		:		Tuple		     =patch_size
								lowerCamelCase__		:		Dict		     =num_channels
								lowerCamelCase__		:		List[str]		     =embed_dim
								lowerCamelCase__		:		Union[str, Any]		     =hidden_sizes
								lowerCamelCase__		:		Dict		     =depths
								lowerCamelCase__		:		Optional[Any]		     =num_heads
								lowerCamelCase__		:		int		     =window_size
								lowerCamelCase__		:		int		     =mlp_ratio
								lowerCamelCase__		:		List[str]		     =qkv_bias
								lowerCamelCase__		:		Any		     =hidden_dropout_prob
								lowerCamelCase__		:		Optional[int]		     =attention_probs_dropout_prob
								lowerCamelCase__		:		Union[str, Any]		     =drop_path_rate
								lowerCamelCase__		:		Any		     =hidden_act
								lowerCamelCase__		:		Union[str, Any]		     =use_absolute_embeddings
								lowerCamelCase__		:		List[str]		     =patch_norm
								lowerCamelCase__		:		Optional[Any]		     =layer_norm_eps
								lowerCamelCase__		:		Optional[Any]		     =initializer_range
								lowerCamelCase__		:		Dict		     =is_training
								lowerCamelCase__		:		int		     =scope
								lowerCamelCase__		:		Optional[Any]		     =use_labels
								lowerCamelCase__		:		Optional[Any]		     =type_sequence_label_size
								lowerCamelCase__		:		Dict		     =encoder_stride
								lowerCamelCase__		:		List[str]		     =out_features
								lowerCamelCase__		:		Tuple		     =out_indices



					def 		snake_case	(	self							:  Dict					)-> Optional[int]:
								lowerCamelCase__		:		Tuple		     =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

								lowerCamelCase__		:		Any		     =None
								if self.use_labels:
											lowerCamelCase__		:		List[Any]		     =ids_tensor([self.batch_size],  self.type_sequence_label_size					)

								lowerCamelCase__		:		Optional[int]		     =self.get_config()

								return config, pixel_values, labels



					def 		snake_case	(	self							:  Optional[Any]					)-> Tuple:
								return FocalNetConfig(
								    image_size=self.image_size,  patch_size=self.patch_size,  num_channels=self.num_channels,  embed_dim=self.embed_dim,  hidden_sizes=self.hidden_sizes,  depths=self.depths,  num_heads=self.num_heads,  window_size=self.window_size,  mlp_ratio=self.mlp_ratio,  qkv_bias=self.qkv_bias,  hidden_dropout_prob=self.hidden_dropout_prob,  attention_probs_dropout_prob=self.attention_probs_dropout_prob,  drop_path_rate=self.drop_path_rate,  hidden_act=self.hidden_act,  use_absolute_embeddings=self.use_absolute_embeddings,  path_norm=self.patch_norm,  layer_norm_eps=self.layer_norm_eps,  initializer_range=self.initializer_range,  encoder_stride=self.encoder_stride,  out_features=self.out_features,  out_indices=self.out_indices,  )



					def 		snake_case	(	self							:  str,  lowerCamelCase							:  List[Any],  lowerCamelCase							:  Any,  lowerCamelCase							:  Dict					)-> int:
								lowerCamelCase__		:		Dict		     =FocalNetModel(config=lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()
								lowerCamelCase__		:		Union[str, Any]		     =model(lowerCamelCase					)

								lowerCamelCase__		:		List[str]		     =((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths					) - 1))
								lowerCamelCase__		:		Tuple		     =int(config.embed_dim * 2 ** (len(config.depths					) - 1)					)

								self.parent.assertEqual(result.last_hidden_state.shape,  (self.batch_size, expected_seq_len, expected_dim)					)



					def 		snake_case	(	self							:  Dict,  lowerCamelCase							:  List[str],  lowerCamelCase							:  Union[str, Any],  lowerCamelCase							:  Any					)-> List[Any]:
								lowerCamelCase__		:		Any		     =FocalNetBackbone(config=lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()
								lowerCamelCase__		:		Tuple		     =model(lowerCamelCase					)

								# verify feature maps
								self.parent.assertEqual(len(result.feature_maps					),  len(config.out_features					)					)
								self.parent.assertListEqual(list(result.feature_maps[0].shape					),  [self.batch_size, self.image_size, 8, 8]					)

								# verify channels
								self.parent.assertEqual(len(model.channels					),  len(config.out_features					)					)
								self.parent.assertListEqual(model.channels,  config.hidden_sizes[:-1]					)

								# verify backbone works with out_features=None
								lowerCamelCase__		:		Optional[int]		     =None
								lowerCamelCase__		:		List[str]		     =FocalNetBackbone(config=lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()
								lowerCamelCase__		:		Union[str, Any]		     =model(lowerCamelCase					)

								# verify feature maps
								self.parent.assertEqual(len(result.feature_maps					),  1					)
								self.parent.assertListEqual(list(result.feature_maps[0].shape					),  [self.batch_size, self.image_size * 2, 4, 4]					)

								# verify channels
								self.parent.assertEqual(len(model.channels					),  1					)
								self.parent.assertListEqual(model.channels,  [config.hidden_sizes[-1]]					)



					def 		snake_case	(	self							:  str,  lowerCamelCase							:  int,  lowerCamelCase							:  Dict,  lowerCamelCase							:  Union[str, Any]					)-> str:
								lowerCamelCase__		:		Tuple		     =FocalNetForMaskedImageModeling(config=lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()
								lowerCamelCase__		:		Tuple		     =model(lowerCamelCase					)
								self.parent.assertEqual(
								    result.reconstruction.shape,  (self.batch_size, self.num_channels, self.image_size, self.image_size)					)

								# test greyscale images
								lowerCamelCase__		:		Tuple		     =1
								lowerCamelCase__		:		Union[str, Any]		     =FocalNetForMaskedImageModeling(lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()

								lowerCamelCase__		:		List[Any]		     =floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
								lowerCamelCase__		:		Optional[Any]		     =model(lowerCamelCase					)
								self.parent.assertEqual(result.reconstruction.shape,  (self.batch_size, 1, self.image_size, self.image_size)					)



					def 		snake_case	(	self							:  Tuple,  lowerCamelCase							:  int,  lowerCamelCase							:  str,  lowerCamelCase							:  List[str]					)-> Tuple:
								lowerCamelCase__		:		Optional[Any]		     =self.type_sequence_label_size
								lowerCamelCase__		:		Tuple		     =FocalNetForImageClassification(lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()
								lowerCamelCase__		:		Tuple		     =model(lowerCamelCase,  labels=lowerCamelCase					)
								self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.type_sequence_label_size)					)

								# test greyscale images
								lowerCamelCase__		:		Optional[int]		     =1
								lowerCamelCase__		:		Optional[int]		     =FocalNetForImageClassification(lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()

								lowerCamelCase__		:		int		     =floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
								lowerCamelCase__		:		str		     =model(lowerCamelCase					)
								self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.type_sequence_label_size)					)




					def 		snake_case	(	self							:  Optional[int]					)-> Tuple:
								lowerCamelCase__		:		Tuple		     =self.prepare_config_and_inputs()

								lowerCamelCase__     ,       lowerCamelCase__     ,       lowerCamelCase__		:		Union[str, Any]		     =config_and_inputs
								lowerCamelCase__		:		Dict		     ={'''pixel_values''': pixel_values}
								return config, inputs_dict





@require_torch
class 	__SCREAMING_SNAKE_CASE     (				lowerCAmelCase_		,       lowerCAmelCase_		,       unittest.TestCase       ):


					'''simple docstring'''





					_a             =							(
					    (
					        FocalNetModel,
					        FocalNetForImageClassification,
					        FocalNetForMaskedImageModeling,
					        FocalNetBackbone,
					    )
					    if is_torch_available()
					    else ()
					)
					_a             =							(
					    {'feature-extraction': FocalNetModel, 'image-classification': FocalNetForImageClassification}
					    if is_torch_available()
					    else {}
					)
					_a             =							False

					_a             =							False
					_a             =							False
					_a             =							False
					_a             =							False



					def 		snake_case	(	self							:  Tuple					)-> str:
								lowerCamelCase__		:		str		     =FocalNetModelTester(self					)
								lowerCamelCase__		:		Tuple		     =ConfigTester(self,  config_class=lowerCamelCase,  embed_dim=37,  has_text_modality=lowerCamelCase					)



					def 		snake_case	(	self							:  Dict					)-> Optional[Any]:
								self.create_and_test_config_common_properties()
								self.config_tester.create_and_test_config_to_json_string()
								self.config_tester.create_and_test_config_to_json_file()
								self.config_tester.create_and_test_config_from_and_save_pretrained()
								self.config_tester.create_and_test_config_with_num_labels()
								self.config_tester.check_config_can_be_init_without_params()
								self.config_tester.check_config_arguments_init()



					def 		snake_case	(	self							:  Optional[int]					)-> Optional[int]:
								return



					def 		snake_case	(	self							:  Optional[int]					)-> str:
								lowerCamelCase__		:		Any		     =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCamelCase					)



					def 		snake_case	(	self							:  int					)-> Tuple:
								lowerCamelCase__		:		int		     =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_backbone(*lowerCamelCase					)



					def 		snake_case	(	self							:  int					)-> str:
								lowerCamelCase__		:		List[str]		     =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase					)



					def 		snake_case	(	self							:  List[Any]					)-> str:
								lowerCamelCase__		:		List[Any]		     =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*lowerCamelCase					)



					@unittest.skip(reason='''FocalNet does not use inputs_embeds'''					)
					def 		snake_case	(	self							:  Optional[int]					)-> Optional[Any]:
								pass



					@unittest.skip(reason='''FocalNet does not use feedforward chunking'''					)
					def 		snake_case	(	self							:  Tuple					)-> str:
								pass



					def 		snake_case	(	self							:  List[Any]					)-> List[str]:
								lowerCamelCase__     ,       lowerCamelCase__		:		str		     =self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes[:-1]:
											lowerCamelCase__		:		int		     =model_class(lowerCamelCase					)
											self.assertIsInstance(model.get_input_embeddings(),  (nn.Module)					)
											lowerCamelCase__		:		Union[str, Any]		     =model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(lowerCamelCase,  nn.Linear					)					)



					def 		snake_case	(	self							:  Optional[int]					)-> Dict:
								lowerCamelCase__     ,       lowerCamelCase__		:		Tuple		     =self.model_tester.prepare_config_and_inputs_for_common()

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

											lowerCamelCase__		:		Optional[Any]		     =['''pixel_values''']
											self.assertListEqual(arg_names[:1],  lowerCamelCase					)



					def 		snake_case	(	self							:  List[str],  lowerCamelCase							:  Dict,  lowerCamelCase							:  Dict,  lowerCamelCase							:  List[str],  lowerCamelCase							:  Dict					)-> int:
								lowerCamelCase__		:		Union[str, Any]		     =model_class(lowerCamelCase					)
								model.to(lowerCamelCase					)
								model.eval()

								with torch.no_grad():
											lowerCamelCase__		:		Union[str, Any]		     =model(**self._prepare_for_class(lowerCamelCase,  lowerCamelCase					)					)

								lowerCamelCase__		:		List[Any]		     =outputs.hidden_states

								lowerCamelCase__		:		str		     =getattr(
								    self.model_tester,  '''expected_num_hidden_layers''',  len(self.model_tester.depths					) + 1					)
								self.assertEqual(len(lowerCamelCase					),  lowerCamelCase					)

								# FocalNet has a different seq_length
								lowerCamelCase__		:		int		     =(
								    config.patch_size
								    if isinstance(config.patch_size,  collections.abc.Iterable					)
								    else (config.patch_size, config.patch_size)
								)

								lowerCamelCase__		:		Tuple		     =(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

								self.assertListEqual(
								    list(hidden_states[0].shape[-2:]					),  [num_patches, self.model_tester.embed_dim],  )

								lowerCamelCase__		:		Union[str, Any]		     =outputs.reshaped_hidden_states
								self.assertEqual(len(lowerCamelCase					),  lowerCamelCase					)

								lowerCamelCase__     ,       lowerCamelCase__     ,       lowerCamelCase__     ,       lowerCamelCase__		:		str		     =reshaped_hidden_states[0].shape
								lowerCamelCase__		:		int		     =(
								    reshaped_hidden_states[0].view(lowerCamelCase,  lowerCamelCase,  height * width					).permute(0,  2,  1					)
								)
								self.assertListEqual(
								    list(reshaped_hidden_states.shape[-2:]					),  [num_patches, self.model_tester.embed_dim],  )



					def 		snake_case	(	self							:  List[Any]					)-> str:
								lowerCamelCase__     ,       lowerCamelCase__		:		List[Any]		     =self.model_tester.prepare_config_and_inputs_for_common()

								lowerCamelCase__		:		List[Any]		     =(
								    self.model_tester.image_size
								    if isinstance(self.model_tester.image_size,  collections.abc.Iterable					)
								    else (self.model_tester.image_size, self.model_tester.image_size)
								)

								for model_class in self.all_model_classes[:-1]:
											lowerCamelCase__		:		Union[str, Any]		     =True
											self.check_hidden_states_output(lowerCamelCase,  lowerCamelCase,  lowerCamelCase,  lowerCamelCase					)

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											lowerCamelCase__		:		Optional[Any]		     =True

											self.check_hidden_states_output(lowerCamelCase,  lowerCamelCase,  lowerCamelCase,  lowerCamelCase					)



					def 		snake_case	(	self							:  int					)-> Any:
								lowerCamelCase__     ,       lowerCamelCase__		:		Optional[int]		     =self.model_tester.prepare_config_and_inputs_for_common()
								lowerCamelCase__		:		int		     =3

								lowerCamelCase__		:		Optional[int]		     =(
								    self.model_tester.image_size
								    if isinstance(self.model_tester.image_size,  collections.abc.Iterable					)
								    else (self.model_tester.image_size, self.model_tester.image_size)
								)
								lowerCamelCase__		:		int		     =(
								    config.patch_size
								    if isinstance(config.patch_size,  collections.abc.Iterable					)
								    else (config.patch_size, config.patch_size)
								)

								lowerCamelCase__		:		List[Any]		     =image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
								lowerCamelCase__		:		List[str]		     =image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

								for model_class in self.all_model_classes[:-1]:
											lowerCamelCase__		:		Union[str, Any]		     =True
											self.check_hidden_states_output(lowerCamelCase,  lowerCamelCase,  lowerCamelCase,  (padded_height, padded_width)					)

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											lowerCamelCase__		:		Dict		     =True
											self.check_hidden_states_output(lowerCamelCase,  lowerCamelCase,  lowerCamelCase,  (padded_height, padded_width)					)



					@slow
					def 		snake_case	(	self							:  Tuple					)-> Optional[Any]:
								for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCamelCase__		:		Union[str, Any]		     =FocalNetModel.from_pretrained(lowerCamelCase					)
											self.assertIsNotNone(lowerCamelCase					)




					def 		snake_case	(	self							:  Any					)-> Any:
								lowerCamelCase__     ,       lowerCamelCase__		:		Any		     =self.model_tester.prepare_config_and_inputs_for_common()

								lowerCamelCase__		:		Union[str, Any]		     =_config_zero_init(lowerCamelCase					)
								for model_class in self.all_model_classes:
											lowerCamelCase__		:		Dict		     =model_class(config=lowerCamelCase					)
											for name, param in model.named_parameters():
														if "embeddings" not in name and param.requires_grad:
																	self.assertIn(
																	    ((param.data.mean() * 1E9).round() / 1E9).item(),  [0.0, 1.0],  msg=F'''Parameter {name} of model {model_class} seems not properly initialized''',  )





@require_vision
@require_torch
class 	__SCREAMING_SNAKE_CASE     (				unittest.TestCase       ):


					'''simple docstring'''





					@cached_property
					def 		snake_case	(	self							:  Optional[Any]					)-> List[Any]:
								# TODO update organization
								return AutoImageProcessor.from_pretrained('''microsoft/focalnet-tiny'''					) if is_vision_available() else None




					@slow
					def 		snake_case	(	self							:  List[str]					)-> int:
								lowerCamelCase__		:		Optional[Any]		     =FocalNetForImageClassification.from_pretrained('''microsoft/focalnet-tiny'''					).to(lowerCamelCase					)
								lowerCamelCase__		:		str		     =self.default_image_processor

								lowerCamelCase__		:		int		     =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
								lowerCamelCase__		:		List[str]		     =image_processor(images=lowerCamelCase,  return_tensors='''pt'''					).to(lowerCamelCase					)

								# forward pass
								with torch.no_grad():
											lowerCamelCase__		:		str		     =model(**lowerCamelCase					)

								# verify the logits
								lowerCamelCase__		:		List[Any]		     =torch.Size((1, 1000)					)
								self.assertEqual(outputs.logits.shape,  lowerCamelCase					)
								lowerCamelCase__		:		Dict		     =torch.tensor([0.2_166, -0.4_368, 0.2_191]					).to(lowerCamelCase					)
								self.assertTrue(torch.allclose(outputs.logits[0, :3],  lowerCamelCase,  atol=1E-4					)					)
								self.assertTrue(outputs.logits.argmax(dim=-1					).item(),  281					)





@require_torch
class 	__SCREAMING_SNAKE_CASE     (				lowerCAmelCase_		,       unittest.TestCase       ):


					'''simple docstring'''





					_a             =							(FocalNetBackbone,) if is_torch_available() else ()
					_a             =							FocalNetConfig

					_a             =							False



					def 		snake_case	(	self							:  Dict					)-> Any:
								lowerCamelCase__		:		str		     =FocalNetModelTester(self					)









"""simple docstring"""






import gc
import unittest

from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline
from diffusers.utils import is_flax_available, load_image, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
		import jax
		import jax.numpy as jnp
		from flax.jax_utils import replicate
		from flax.training.common_utils import shard

@slow
@require_flax
class 	__SCREAMING_SNAKE_CASE     (				unittest.TestCase       ):


					'''simple docstring'''





					def 		snake_case	(	self							:  Union[str, Any]					)-> Dict:
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()



					def 		snake_case	(	self							:  str					)-> Any:
								lowerCamelCase__     ,       lowerCamelCase__		:		Optional[Any]		     =FlaxControlNetModel.from_pretrained(
								    '''lllyasviel/sd-controlnet-canny''',  from_pt=lowerCamelCase,  dtype=jnp.bfloataa					)
								lowerCamelCase__     ,       lowerCamelCase__		:		Optional[int]		     =FlaxStableDiffusionControlNetPipeline.from_pretrained(
								    '''runwayml/stable-diffusion-v1-5''',  controlnet=lowerCamelCase,  from_pt=lowerCamelCase,  dtype=jnp.bfloataa					)
								lowerCamelCase__		:		Optional[int]		     =controlnet_params

								lowerCamelCase__		:		Dict		     ='''bird'''
								lowerCamelCase__		:		List[str]		     =jax.device_count()
								lowerCamelCase__		:		Optional[Any]		     =pipe.prepare_text_inputs([prompts] * num_samples					)

								lowerCamelCase__		:		Dict		     =load_image(
								    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png'''					)
								lowerCamelCase__		:		List[Any]		     =pipe.prepare_image_inputs([canny_image] * num_samples					)

								lowerCamelCase__		:		Optional[int]		     =jax.random.PRNGKey(0					)
								lowerCamelCase__		:		Dict		     =jax.random.split(lowerCamelCase,  jax.device_count()					)

								lowerCamelCase__		:		Tuple		     =replicate(lowerCamelCase					)
								lowerCamelCase__		:		Tuple		     =shard(lowerCamelCase					)
								lowerCamelCase__		:		Optional[int]		     =shard(lowerCamelCase					)

								lowerCamelCase__		:		Tuple		     =pipe(
								    prompt_ids=lowerCamelCase,  image=lowerCamelCase,  params=lowerCamelCase,  prng_seed=lowerCamelCase,  num_inference_steps=50,  jit=lowerCamelCase,  ).images
								assert images.shape == (jax.device_count(), 1, 768, 512, 3)

								lowerCamelCase__		:		Dict		     =images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]					)
								lowerCamelCase__		:		Any		     =images[0, 253:256, 253:256, -1]

								lowerCamelCase__		:		Optional[Any]		     =jnp.asarray(jax.device_get(image_slice.flatten()					)					)
								lowerCamelCase__		:		Dict		     =jnp.array(
								    [0.167_969, 0.116_699, 0.081_543, 0.154_297, 0.132_812, 0.108_887, 0.169_922, 0.169_922, 0.205_078]					)
								print(F'''output_slice: {output_slice}'''					)
								assert jnp.abs(output_slice - expected_slice					).max() < 1E-2




					def 		snake_case	(	self							:  Optional[int]					)-> Optional[int]:
								lowerCamelCase__     ,       lowerCamelCase__		:		Dict		     =FlaxControlNetModel.from_pretrained(
								    '''lllyasviel/sd-controlnet-openpose''',  from_pt=lowerCamelCase,  dtype=jnp.bfloataa					)
								lowerCamelCase__     ,       lowerCamelCase__		:		List[Any]		     =FlaxStableDiffusionControlNetPipeline.from_pretrained(
								    '''runwayml/stable-diffusion-v1-5''',  controlnet=lowerCamelCase,  from_pt=lowerCamelCase,  dtype=jnp.bfloataa					)
								lowerCamelCase__		:		Optional[Any]		     =controlnet_params

								lowerCamelCase__		:		int		     ='''Chef in the kitchen'''
								lowerCamelCase__		:		Optional[Any]		     =jax.device_count()
								lowerCamelCase__		:		Any		     =pipe.prepare_text_inputs([prompts] * num_samples					)

								lowerCamelCase__		:		Any		     =load_image(
								    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png'''					)
								lowerCamelCase__		:		List[Any]		     =pipe.prepare_image_inputs([pose_image] * num_samples					)

								lowerCamelCase__		:		Tuple		     =jax.random.PRNGKey(0					)
								lowerCamelCase__		:		Optional[Any]		     =jax.random.split(lowerCamelCase,  jax.device_count()					)

								lowerCamelCase__		:		int		     =replicate(lowerCamelCase					)
								lowerCamelCase__		:		List[Any]		     =shard(lowerCamelCase					)
								lowerCamelCase__		:		int		     =shard(lowerCamelCase					)

								lowerCamelCase__		:		Tuple		     =pipe(
								    prompt_ids=lowerCamelCase,  image=lowerCamelCase,  params=lowerCamelCase,  prng_seed=lowerCamelCase,  num_inference_steps=50,  jit=lowerCamelCase,  ).images
								assert images.shape == (jax.device_count(), 1, 768, 512, 3)

								lowerCamelCase__		:		Dict		     =images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]					)
								lowerCamelCase__		:		List[str]		     =images[0, 253:256, 253:256, -1]

								lowerCamelCase__		:		int		     =jnp.asarray(jax.device_get(image_slice.flatten()					)					)
								lowerCamelCase__		:		Any		     =jnp.array(
								    [[0.271_484, 0.261_719, 0.275_391, 0.277_344, 0.279_297, 0.291_016, 0.294_922, 0.302_734, 0.302_734]]					)
								print(F'''output_slice: {output_slice}'''					)
								assert jnp.abs(output_slice - expected_slice					).max() < 1E-2




