


"""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       (     _UpperCamelCase		:      int       ,			_UpperCamelCase		:      Dict       ,			_UpperCamelCase		:      Any=1E-12							)		->       Dict:

							'''simple docstring'''



							__UpperCAmelCase   :						str      							=					jnp.divide(emb_a.T       ,			jnp.clip(jnp.linalg.norm(__lowerCamelCase       ,			axis=1							)       ,			a_min=__lowerCamelCase							)							).T
							__UpperCAmelCase   :						str      							=					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      lowerCamelCase__   (     nn.Module				):







					"""simple docstring"""


					__a	       =							42
					__a	       =							jnp.floataa






					def lowerCamelCase__							(       self    : Tuple  ):
												'''simple docstring'''






												__UpperCAmelCase   :						List[Any]      							=					FlaxCLIPVisionModule(self.config.vision_config  )
												__UpperCAmelCase   :						List[Any]      							=					nn.Dense(self.config.projection_dim ,       use_bias=snake_case__ ,       dtype=self.dtype  )

												__UpperCAmelCase   :						Tuple      							=					self.param("""concept_embeds""" ,       jax.nn.initializers.ones ,       (17, self.config.projection_dim)  )
												__UpperCAmelCase   :						Optional[int]      							=					self.param(
												    """special_care_embeds""" ,       jax.nn.initializers.ones ,       (3, self.config.projection_dim)  )

												__UpperCAmelCase   :						Any      							=					self.param("""concept_embeds_weights""" ,       jax.nn.initializers.ones ,       (17,)  )
												__UpperCAmelCase   :						Any      							=					self.param("""special_care_embeds_weights""" ,       jax.nn.initializers.ones ,       (3,)  )






					def __call__(       self    : Tuple ,       UpperCamelCase    : Tuple  ):
												'''simple docstring'''






												__UpperCAmelCase   :						str      							=					self.vision_model(snake_case__  )[1]
												__UpperCAmelCase   :						int      							=					self.visual_projection(snake_case__  )

												__UpperCAmelCase   :						str      							=					jax_cosine_distance(snake_case__ ,       self.special_care_embeds  )
												__UpperCAmelCase   :						Dict      							=					jax_cosine_distance(snake_case__ ,       self.concept_embeds  )

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

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

												__UpperCAmelCase   :						Any      							=					cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
												__UpperCAmelCase   :						Optional[Any]      							=					jnp.round(snake_case__ ,       3  )
												__UpperCAmelCase   :						int      							=					jnp.any(concept_scores > 0 ,       axis=1  )

												return has_nsfw_concepts






class      lowerCamelCase__   (     A_				):







					"""simple docstring"""


					__a	       =							CLIPConfig
					__a	       =							"clip_input"
					__a	       =							FlaxStableDiffusionSafetyCheckerModule






					def __init__(       self    : str ,       UpperCamelCase    : CLIPConfig ,       UpperCamelCase    : Optional[Tuple] = None ,       UpperCamelCase    : int = 0 ,       UpperCamelCase    : jnp.dtype = jnp.floataa ,       UpperCamelCase    : bool = True ,       **UpperCamelCase    : Dict ,       ):
												'''simple docstring'''






												if input_shape is None:
																			__UpperCAmelCase   :						int      							=					(1, 224, 224, 3)
												__UpperCAmelCase   :						Union[str, Any]      							=					self.module_class(config=snake_case__ ,       dtype=snake_case__ ,       **snake_case__  )
												super().__init__(snake_case__ ,       snake_case__ ,       input_shape=snake_case__ ,       seed=snake_case__ ,       dtype=snake_case__ ,       _do_init=_do_init  )






					def lowerCamelCase__							(       self    : List[Any] ,       UpperCamelCase    : jax.random.KeyArray ,       UpperCamelCase    : Tuple ,       UpperCamelCase    : FrozenDict = None  ):
												'''simple docstring'''






												__UpperCAmelCase   :						str      							=					jax.random.normal(snake_case__ ,       snake_case__  )

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

												__UpperCAmelCase   :						Optional[int]      							=					self.module.init(snake_case__ ,       snake_case__  )["params"]

												return random_params






					def __call__(       self    : Union[str, Any] ,       UpperCamelCase    : str ,       UpperCamelCase    : dict = None ,       ):
												'''simple docstring'''






												__UpperCAmelCase   :						int      							=					jnp.transpose(snake_case__ ,       (0, 2, 3, 1)  )

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


import logging

import numpy as np
import pytest
from scipy.linalg import eigh

logging.basicConfig(level=logging.INFO, format="""%(message)s""")


def 			UpperCamelCase					(    __lowerCamelCase     : np.ndarray    ):
       return input_array.reshape((input_array.size, 1)    )


def 			UpperCamelCase					(    __lowerCamelCase     : np.ndarray				,					__lowerCamelCase     : np.ndarray				,					__lowerCamelCase     : int    ):
       snake_case       :   Any							=      np.nan
       for i in range(__lowerCamelCase    ):
              snake_case       :   List[str]							=      features[:, labels == i]
              snake_case       :   Dict							=      data.mean(1    )
              # Centralize the data of class i
              snake_case       :   Optional[Any]							=      data - column_reshape(__lowerCamelCase    )
              if i > 0:
                     # If covariance_sum is not None
                     covariance_sum += np.dot(__lowerCamelCase				,					centered_data.T    )
              else:
                     # If covariance_sum is np.nan (i.e. first loop)
                     snake_case       :   Optional[Any]							=      np.dot(__lowerCamelCase				,					centered_data.T    )

       return covariance_sum / features.shape[1]


def 			UpperCamelCase					(    __lowerCamelCase     : np.ndarray				,					__lowerCamelCase     : np.ndarray				,					__lowerCamelCase     : int    ):
       snake_case       :   Optional[Any]							=      features.mean(1    )
       snake_case       :   Tuple							=      np.nan
       for i in range(__lowerCamelCase    ):
              snake_case       :   Tuple							=      features[:, labels == i]
              snake_case       :   Tuple							=      data.shape[1]
              snake_case       :   List[str]							=      data.mean(1    )
              if i > 0:
                     # If covariance_sum is not None
                     covariance_sum += device_data * np.dot(
                         column_reshape(__lowerCamelCase    ) - column_reshape(__lowerCamelCase    )				,					(column_reshape(__lowerCamelCase    ) - column_reshape(__lowerCamelCase    )).T				,					)
              else:
                     # If covariance_sum is np.nan (i.e. first loop)
                     snake_case       :   Optional[int]							=      device_data * np.dot(
                         column_reshape(__lowerCamelCase    ) - column_reshape(__lowerCamelCase    )				,					(column_reshape(__lowerCamelCase    ) - column_reshape(__lowerCamelCase    )).T				,					)

       return covariance_sum / features.shape[1]


def 			UpperCamelCase					(    __lowerCamelCase     : np.ndarray				,					__lowerCamelCase     : int    ):

       # Check if the features have been loaded
       if features.any():
              snake_case       :   Tuple							=      features.mean(1    )
              # Center the dataset
              snake_case       :   List[str]							=      features - np.reshape(__lowerCamelCase				,					(data_mean.size, 1)    )
              snake_case       :   Optional[Any]							=      np.dot(__lowerCamelCase				,					centered_data.T    ) / features.shape[1]
              snake_case				,					snake_case       :   Dict							=      np.linalg.eigh(__lowerCamelCase    )
              # Take all the columns in the reverse order (-1), and then takes only the first
              snake_case       :   Optional[Any]							=      eigenvectors[:, ::-1][:, 0:dimensions]
              # Project the database on the new space
              snake_case       :   Union[str, Any]							=      np.dot(filtered_eigenvectors.T				,					__lowerCamelCase    )
              logging.info("Principal Component Analysis computed"    )

              return projected_data
       else:
              logging.basicConfig(level=logging.ERROR				,					format="%(message)s"				,					force=__lowerCamelCase    )
              logging.error("Dataset empty"    )
              raise AssertionError


def 			UpperCamelCase					(    __lowerCamelCase     : np.ndarray				,					__lowerCamelCase     : np.ndarray				,					__lowerCamelCase     : int				,					__lowerCamelCase     : int    ):
       assert classes > dimensions

       # Check if features have been already loaded
       if features.any:
              snake_case				,					snake_case       :   str							=      eigh(
                  covariance_between_classes(__lowerCamelCase				,					__lowerCamelCase				,					__lowerCamelCase    )				,					covariance_within_classes(__lowerCamelCase				,					__lowerCamelCase				,					__lowerCamelCase    )				,					)
              snake_case       :   str							=      eigenvectors[:, ::-1][:, :dimensions]
              snake_case				,					snake_case				,					snake_case       :   int							=      np.linalg.svd(__lowerCamelCase    )
              snake_case       :   List[Any]							=      svd_matrix[:, 0:dimensions]
              snake_case       :   Optional[Any]							=      np.dot(filtered_svd_matrix.T				,					__lowerCamelCase    )
              logging.info("Linear Discriminant Analysis computed"    )

              return projected_data
       else:
              logging.basicConfig(level=logging.ERROR				,					format="%(message)s"				,					force=__lowerCamelCase    )
              logging.error("Dataset empty"    )
              raise AssertionError


def 			UpperCamelCase					(    ):
       # Create dummy dataset with 2 classes and 3 features
       snake_case       :   str							=      np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]    )
       snake_case       :   Union[str, Any]							=      np.array([0, 0, 0, 1, 1]    )
       snake_case       :   List[Any]							=      2
       snake_case       :   Any							=      2

       # Assert that the function raises an AssertionError if dimensions > classes
       with pytest.raises(__lowerCamelCase    ) as error_info:
              snake_case       :   str							=      linear_discriminant_analysis(
                  __lowerCamelCase				,					__lowerCamelCase				,					__lowerCamelCase				,					__lowerCamelCase    )
              if isinstance(__lowerCamelCase				,					np.ndarray    ):
                     raise AssertionError(
                         "Did not raise AssertionError for dimensions > classes"    )
              assert error_info.type is AssertionError


def 			UpperCamelCase					(    ):
       snake_case       :   List[str]							=      np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]    )
       snake_case       :   List[str]							=      2
       snake_case       :   int							=      np.array([[6.9282_0323, 8.6602_5404, 10.3923_0485], [3.0, 3.0, 3.0]]    )

       with pytest.raises(__lowerCamelCase    ) as error_info:
              snake_case       :   Union[str, Any]							=      principal_component_analysis(__lowerCamelCase				,					__lowerCamelCase    )
              if not np.allclose(__lowerCamelCase				,					__lowerCamelCase    ):
                     raise AssertionError
              assert error_info.type is AssertionError


if __name__ == "__main__":
   import doctest

   doctest.testmod()





