



'''simple docstring'''




from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule


def        lowerCAmelCase_     (			_lowerCamelCase:     Optional[Any] ,   _lowerCamelCase:     List[str] ,   _lowerCamelCase:     int=1E-12      ):
		__SCREAMING_SNAKE_CASE :	Tuple					 =	jnp.divide(emb_a.T ,   jnp.clip(jnp.linalg.norm(_lowerCamelCase ,   axis=1      ) ,   a_min=_lowerCamelCase      )      ).T
		__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	jnp.divide(emb_a.T ,   jnp.clip(jnp.linalg.norm(_lowerCamelCase ,   axis=1      ) ,   a_min=_lowerCamelCase      )      ).T
		return jnp.matmul(_lowerCamelCase ,   norm_emb_a.T      )






class 		_UpperCamelCase						(							nn.Module					):


			'''simple docstring'''

			_A   :	CLIPConfig
			_A   :	jnp.dtype    					=	jnp.floataa

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	str					 =	FlaxCLIPVisionModule(self.config.vision_config		)
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	nn.Dense(self.config.projection_dim							,  use_bias=lowerCAmelCase__							,  dtype=self.dtype		)

					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	self.param("""concept_embeds"""							,  jax.nn.initializers.ones							,  (1_7, self.config.projection_dim)		)
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.param(
					    """special_care_embeds"""							,  jax.nn.initializers.ones							,  (3, self.config.projection_dim)		)

					__SCREAMING_SNAKE_CASE :	Dict					 =	self.param("""concept_embeds_weights"""							,  jax.nn.initializers.ones							,  (1_7,)		)
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.param("""special_care_embeds_weights"""							,  jax.nn.initializers.ones							,  (3,)		)



			def __call__(							self  :				List[str]							,  lowerCAmelCase__  :				str		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	self.vision_model(lowerCAmelCase__		)[1]
					__SCREAMING_SNAKE_CASE :	List[str]					 =	self.visual_projection(lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	jax_cosine_distance(lowerCAmelCase__							,  self.special_care_embeds		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	jax_cosine_distance(lowerCAmelCase__							,  self.concept_embeds		)

					# increase this value to create a stronger `nfsw` filter
					# at the cost of increasing the possibility of filtering benign image inputs
					__SCREAMING_SNAKE_CASE :	List[str]					 =	0.0

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
					__SCREAMING_SNAKE_CASE :	Tuple					 =	jnp.round(lowerCAmelCase__							,  3		)
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	jnp.any(special_scores > 0							,  axis=1							,  keepdims=lowerCAmelCase__		)
					# Use a lower threshold if an image has any special care concept
					__SCREAMING_SNAKE_CASE :	Tuple					 =	is_special_care * 0.01

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
					__SCREAMING_SNAKE_CASE :	List[str]					 =	jnp.round(lowerCAmelCase__							,  3		)
					__SCREAMING_SNAKE_CASE :	int					 =	jnp.any(concept_scores > 0							,  axis=1		)

					return has_nsfw_concepts






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Tuple    					=	CLIPConfig
			_A   :	Any    					=	'''clip_input'''
			_A   :	List[Any]    					=	FlaxStableDiffusionSafetyCheckerModule

			def __init__(							self  :				Optional[Any]							,  lowerCAmelCase__  :				CLIPConfig							,  lowerCAmelCase__  :				Optional[Tuple] = None							,  lowerCAmelCase__  :				int = 0							,  lowerCAmelCase__  :				jnp.dtype = jnp.floataa							,  lowerCAmelCase__  :				bool = True							,  **lowerCAmelCase__  :				List[str]							,  ):





					"""simple docstring"""






					if input_shape is None:
							__SCREAMING_SNAKE_CASE :	Dict					 =	(1, 2_2_4, 2_2_4, 3)
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.module_class(config=lowerCAmelCase__							,  dtype=lowerCAmelCase__							,  **lowerCAmelCase__		)
					super().__init__(lowerCAmelCase__							,  lowerCAmelCase__							,  input_shape=lowerCAmelCase__							,  seed=lowerCAmelCase__							,  dtype=lowerCAmelCase__							,  _do_init=_do_init		)

			def UpperCamelCase__      (							self  :				Tuple							,  lowerCAmelCase__  :				jax.random.KeyArray							,  lowerCAmelCase__  :				Tuple							,  lowerCAmelCase__  :				FrozenDict = None		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	str					 =	jax.random.normal(lowerCAmelCase__							,  lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Optional[int]					 =	jax.random.split(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Dict					 =	{"""params""": params_rng, """dropout""": dropout_rng}

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	self.module.init(lowerCAmelCase__							,  lowerCAmelCase__		)["""params"""]

					return random_params



			def __call__(							self  :				List[str]							,  lowerCAmelCase__  :				Optional[Any]							,  lowerCAmelCase__  :				dict = None							,  ):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	jnp.transpose(lowerCAmelCase__							,  (0, 2, 3, 1)		)

					return self.module.apply(
					    {"""params""": params or self.params}							,  jnp.array(lowerCAmelCase__							,  dtype=jnp.floataa		)							,  rngs={}							,  )




'''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 		_UpperCamelCase						(							unittest.TestCase					):


			'''simple docstring'''

			def UpperCamelCase__      (							self  :				Optional[Any]		):





					"""simple docstring"""






					super().tearDown()
					gc.collect()

			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	int					 =	FlaxControlNetModel.from_pretrained(
					    """lllyasviel/sd-controlnet-canny"""							,  from_pt=lowerCAmelCase__							,  dtype=jnp.bfloataa		)
					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Tuple					 =	FlaxStableDiffusionControlNetPipeline.from_pretrained(
					    """runwayml/stable-diffusion-v1-5"""							,  controlnet=lowerCAmelCase__							,  from_pt=lowerCAmelCase__							,  dtype=jnp.bfloataa		)
					__SCREAMING_SNAKE_CASE :	str					 =	controlnet_params

					__SCREAMING_SNAKE_CASE :	str					 =	"""bird"""
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	jax.device_count()
					__SCREAMING_SNAKE_CASE :	str					 =	pipe.prepare_text_inputs([prompts] * num_samples		)

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

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	jax.random.PRNGKey(0		)
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	jax.random.split(lowerCAmelCase__							,  jax.device_count()		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	replicate(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	shard(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	shard(lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	pipe(
					    prompt_ids=lowerCAmelCase__							,  image=lowerCAmelCase__							,  params=lowerCAmelCase__							,  prng_seed=lowerCAmelCase__							,  num_inference_steps=5_0							,  jit=lowerCAmelCase__							,  ).images
					assert images.shape == (jax.device_count(), 1, 7_6_8, 5_1_2, 3)

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	jnp.asarray(jax.device_get(image_slice.flatten()		)		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	jnp.array(
					    [0.16_79_69, 0.11_66_99, 0.08_15_43, 0.15_42_97, 0.13_28_12, 0.10_88_87, 0.16_99_22, 0.16_99_22, 0.20_50_78]		)
					print(F"output_slice: {output_slice}"		)
					assert jnp.abs(output_slice - expected_slice		).max() < 1E-2



			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	FlaxControlNetModel.from_pretrained(
					    """lllyasviel/sd-controlnet-openpose"""							,  from_pt=lowerCAmelCase__							,  dtype=jnp.bfloataa		)
					__SCREAMING_SNAKE_CASE ,		__SCREAMING_SNAKE_CASE :	Any					 =	FlaxStableDiffusionControlNetPipeline.from_pretrained(
					    """runwayml/stable-diffusion-v1-5"""							,  controlnet=lowerCAmelCase__							,  from_pt=lowerCAmelCase__							,  dtype=jnp.bfloataa		)
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	controlnet_params

					__SCREAMING_SNAKE_CASE :	List[str]					 =	"""Chef in the kitchen"""
					__SCREAMING_SNAKE_CASE :	int					 =	jax.device_count()
					__SCREAMING_SNAKE_CASE :	Tuple					 =	pipe.prepare_text_inputs([prompts] * num_samples		)

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

					__SCREAMING_SNAKE_CASE :	Dict					 =	jax.random.PRNGKey(0		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	jax.random.split(lowerCAmelCase__							,  jax.device_count()		)

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	replicate(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	shard(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	shard(lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	Tuple					 =	pipe(
					    prompt_ids=lowerCAmelCase__							,  image=lowerCAmelCase__							,  params=lowerCAmelCase__							,  prng_seed=lowerCAmelCase__							,  num_inference_steps=5_0							,  jit=lowerCAmelCase__							,  ).images
					assert images.shape == (jax.device_count(), 1, 7_6_8, 5_1_2, 3)

					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]		)
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

					__SCREAMING_SNAKE_CASE :	Tuple					 =	jnp.asarray(jax.device_get(image_slice.flatten()		)		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	jnp.array(
					    [[0.27_14_84, 0.26_17_19, 0.27_53_91, 0.27_73_44, 0.27_92_97, 0.29_10_16, 0.29_49_22, 0.30_27_34, 0.30_27_34]]		)
					print(F"output_slice: {output_slice}"		)
					assert jnp.abs(output_slice - expected_slice		).max() < 1E-2