




'''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 		snake_case_   (UpperCamelCase	:  Optional[int]			,						UpperCamelCase	:  Union[str, Any]			,						UpperCamelCase	:  str=1e-12				):





							'''simple docstring'''


							_a     					=    jnp.divide(emb_a.T			,						jnp.clip(jnp.linalg.norm(UpperCamelCase			,						axis=1				)			,						a_min=UpperCamelCase				)				).T
							_a     					=    jnp.divide(emb_a.T			,						jnp.clip(jnp.linalg.norm(UpperCamelCase			,						axis=1				)			,						a_min=UpperCamelCase				)				).T
							return jnp.matmul(UpperCamelCase			,						norm_emb_a.T				)

class       A   (		nn.Module   ):
							lowercase_          =    42
							lowercase_          =    jnp.floataa







							def        __lowerCAmelCase      (  self	:  Any )      ->							Optional[int]:


														"""simple docstring"""


														_a     					=    FlaxCLIPVisionModule(self.config.vision_config )
														_a     					=    nn.Dense(self.config.projection_dim							,      use_bias=lowerCAmelCase_							,      dtype=self.dtype )

														_a     					=    self.param('''concept_embeds'''							,      jax.nn.initializers.ones							,      (17, self.config.projection_dim) )
														_a     					=    self.param(
														    '''special_care_embeds'''							,      jax.nn.initializers.ones							,      (3, self.config.projection_dim) )

														_a     					=    self.param('''concept_embeds_weights'''							,      jax.nn.initializers.ones							,      (17,) )
														_a     					=    self.param('''special_care_embeds_weights'''							,      jax.nn.initializers.ones							,      (3,) )







							def __call__(  self	:  Dict							,      lowerCAmelCase_	:  Any )      ->							Union[str, Any]:


														"""simple docstring"""


														_a     					=    self.vision_model(lowerCAmelCase_ )[1]
														_a     					=    self.visual_projection(lowerCAmelCase_ )

														_a     					=    jax_cosine_distance(lowerCAmelCase_							,      self.special_care_embeds )
														_a     					=    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
														_a     					=    0.0

														_a     					=    special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
														_a     					=    jnp.round(lowerCAmelCase_							,      3 )
														_a     					=    jnp.any(special_scores > 0							,      axis=1							,      keepdims=lowerCAmelCase_ )
														# Use a lower threshold if an image has any special care concept
														_a     					=    is_special_care * 0.0_1

														_a     					=    cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
														_a     					=    jnp.round(lowerCAmelCase_							,      3 )
														_a     					=    jnp.any(concept_scores > 0							,      axis=1 )

														return has_nsfw_concepts



class       A   (		_a   ):
							lowercase_          =    CLIPConfig
							lowercase_          =    'clip_input'
							lowercase_          =    FlaxStableDiffusionSafetyCheckerModule







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


														"""simple docstring"""


														if input_shape is None:
																					_a     					=    (1, 2_24, 2_24, 3)
														_a     					=    self.module_class(config=lowerCAmelCase_							,      dtype=lowerCAmelCase_							,      **lowerCAmelCase_ )
														super().__init__(lowerCAmelCase_							,      lowerCAmelCase_							,      input_shape=lowerCAmelCase_							,      seed=lowerCAmelCase_							,      dtype=lowerCAmelCase_							,      _do_init=_do_init )







							def        __lowerCAmelCase      (  self	:  Optional[Any]							,      lowerCAmelCase_	:  jax.random.KeyArray							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  FrozenDict = None )      ->							FrozenDict:


														"""simple docstring"""


														_a     					=    jax.random.normal(lowerCAmelCase_							,      lowerCAmelCase_ )

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

														_a     					=    self.module.init(lowerCAmelCase_							,      lowerCAmelCase_ )['''params''']

														return random_params







							def __call__(  self	:  List[Any]							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  dict = None							,      )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    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 tempfile
import unittest

import numpy as np
from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax


if is_flax_available():
				import os

				from flax.core.frozen_dict import unfreeze
				from flax.traverse_util import flatten_dict

				from transformers import FlaxBertModel

				_snake_case				:				Optional[Any]       							=		'0.12'  # assumed parallelism: 8

@require_flax
@is_staging_test
class       A   (		unittest.TestCase   ):







							@classmethod
							def        __lowerCAmelCase      (  cls	:  Tuple )      ->							int:


														"""simple docstring"""


														_a     					=    TOKEN
														HfFolder.save_token(lowerCAmelCase_ )







							@classmethod
							def        __lowerCAmelCase      (  cls	:  Tuple )      ->							Optional[int]:


														"""simple docstring"""


														try:
																					delete_repo(token=cls._token							,      repo_id='''test-model-flax''' )
														except HTTPError:
																					pass

														try:
																					delete_repo(token=cls._token							,      repo_id='''valid_org/test-model-flax-org''' )
														except HTTPError:
																					pass







							def        __lowerCAmelCase      (  self	:  Any )      ->							Optional[Any]:


														"""simple docstring"""


														_a     					=    BertConfig(
														    vocab_size=99							,      hidden_size=32							,      num_hidden_layers=5							,      num_attention_heads=4							,      intermediate_size=37 )
														_a     					=    FlaxBertModel(lowerCAmelCase_ )
														model.push_to_hub('''test-model-flax'''							,      use_auth_token=self._token )

														_a     					=    FlaxBertModel.from_pretrained(F'{USER}/test-model-flax' )

														_a     					=    flatten_dict(unfreeze(model.params ) )
														_a     					=    flatten_dict(unfreeze(new_model.params ) )

														for key in base_params.keys():
																					_a     					=    (base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCAmelCase_							,      1e-3							,      msg=F'{key} not identical' )

														# Reset repo
														delete_repo(token=self._token							,      repo_id='''test-model-flax''' )

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(lowerCAmelCase_							,      repo_id='''test-model-flax'''							,      push_to_hub=lowerCAmelCase_							,      use_auth_token=self._token )

														_a     					=    FlaxBertModel.from_pretrained(F'{USER}/test-model-flax' )

														_a     					=    flatten_dict(unfreeze(model.params ) )
														_a     					=    flatten_dict(unfreeze(new_model.params ) )

														for key in base_params.keys():
																					_a     					=    (base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCAmelCase_							,      1e-3							,      msg=F'{key} not identical' )







							def        __lowerCAmelCase      (  self	:  int )      ->							Tuple:


														"""simple docstring"""


														_a     					=    BertConfig(
														    vocab_size=99							,      hidden_size=32							,      num_hidden_layers=5							,      num_attention_heads=4							,      intermediate_size=37 )
														_a     					=    FlaxBertModel(lowerCAmelCase_ )
														model.push_to_hub('''valid_org/test-model-flax-org'''							,      use_auth_token=self._token )

														_a     					=    FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' )

														_a     					=    flatten_dict(unfreeze(model.params ) )
														_a     					=    flatten_dict(unfreeze(new_model.params ) )

														for key in base_params.keys():
																					_a     					=    (base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCAmelCase_							,      1e-3							,      msg=F'{key} not identical' )

														# Reset repo
														delete_repo(token=self._token							,      repo_id='''valid_org/test-model-flax-org''' )

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(
																					    lowerCAmelCase_							,      repo_id='''valid_org/test-model-flax-org'''							,      push_to_hub=lowerCAmelCase_							,      use_auth_token=self._token )

														_a     					=    FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' )

														_a     					=    flatten_dict(unfreeze(model.params ) )
														_a     					=    flatten_dict(unfreeze(new_model.params ) )

														for key in base_params.keys():
																					_a     					=    (base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCAmelCase_							,      1e-3							,      msg=F'{key} not identical' )



def 		snake_case_   (UpperCamelCase	:  List[str]			,						UpperCamelCase	:  Optional[Any]				):





							'''simple docstring'''


							_a     					=    True
							_a     					=    flatten_dict(modela.params				)
							_a     					=    flatten_dict(modela.params				)
							for key in flat_params_a.keys():
														if np.sum(np.abs(flat_params_a[key] - flat_params_a[key]				)				) > 1e-4:
																					_a     					=    False

							return models_are_equal

@require_flax
class       A   (		unittest.TestCase   ):







							def        __lowerCAmelCase      (  self	:  Dict )      ->							int:


														"""simple docstring"""


														_a     					=    BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' )
														_a     					=    FlaxBertModel(lowerCAmelCase_ )

														_a     					=    '''bert'''
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(os.path.join(lowerCAmelCase_							,      lowerCAmelCase_ ) )

																					with self.assertRaises(lowerCAmelCase_ ):
																												_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_ )

																					_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_							,      subfolder=lowerCAmelCase_ )

														self.assertTrue(check_models_equal(lowerCAmelCase_							,      lowerCAmelCase_ ) )







							def        __lowerCAmelCase      (  self	:  Union[str, Any] )      ->							Optional[int]:


														"""simple docstring"""


														_a     					=    BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' )
														_a     					=    FlaxBertModel(lowerCAmelCase_ )

														_a     					=    '''bert'''
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(os.path.join(lowerCAmelCase_							,      lowerCAmelCase_ )							,      max_shard_size='''10KB''' )

																					with self.assertRaises(lowerCAmelCase_ ):
																												_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_ )

																					_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_							,      subfolder=lowerCAmelCase_ )

														self.assertTrue(check_models_equal(lowerCAmelCase_							,      lowerCAmelCase_ ) )







							def        __lowerCAmelCase      (  self	:  Optional[Any] )      ->							List[str]:


														"""simple docstring"""


														_a     					=    '''bert'''
														_a     					=    '''hf-internal-testing/tiny-random-bert-subfolder'''

														with self.assertRaises(lowerCAmelCase_ ):
																					_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_ )

														_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_							,      subfolder=lowerCAmelCase_ )

														self.assertIsNotNone(lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							Tuple:


														"""simple docstring"""


														_a     					=    '''bert'''
														_a     					=    '''hf-internal-testing/tiny-random-bert-sharded-subfolder'''
														with self.assertRaises(lowerCAmelCase_ ):
																					_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_ )

														_a     					=    FlaxBertModel.from_pretrained(lowerCAmelCase_							,      subfolder=lowerCAmelCase_ )

														self.assertIsNotNone(lowerCAmelCase_ )

