






"""simple docstring"""


import numpy as np
import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel

from ...utils import logging


_UpperCamelCase    :   str   			= logging.get_logger(__name__)






class 						snake_case  (  UpperCAmelCase	):
    __magic_name__   					=					CLIPConfig

    __magic_name__   					=					["""CLIPEncoderLayer"""]





    def __init__(							self     : List[str]    ,		A     : CLIPConfig      ):


      '''simple docstring'''




      super().__init__(_A      )

      a					:      int    =       CLIPVisionModelWithProjection(config.vision_config      )

      a					:      List[str]    =       nn.Linear(config.vision_config.projection_dim    ,		1      )
      a					:      List[str]    =       nn.Linear(config.vision_config.projection_dim    ,		1      )





    @torch.no_grad()
    def     lowerCamelCase__				(							self     : List[Any]    ,		A     : int    ,		A     : Dict    ,		A     : int=0.5    ,		A     : Dict=0.5      ):


      '''simple docstring'''




      a					:      Any    =       self.vision_model(_A      )[0]

      a					:      Any    =       self.p_head(_A      )
      a					:      Any    =       nsfw_detected.flatten()
      a					:      str    =       nsfw_detected > p_threshold
      a					:      Tuple    =       nsfw_detected.tolist()

      if any(_A      ):
        logger.warning(
            'Potential NSFW content was detected in one or more images. A black image will be returned instead.'
            ' Try again with a different prompt and/or seed.'      )

      for idx, nsfw_detected_ in enumerate(_A      ):
        if nsfw_detected_:
          a					:      Any    =       np.zeros(images[idx].shape      )

      a					:      List[str]    =       self.w_head(_A      )
      a					:      Optional[Any]    =       watermark_detected.flatten()
      a					:      int    =       watermark_detected > w_threshold
      a					:      str    =       watermark_detected.tolist()

      if any(_A      ):
        logger.warning(
            'Potential watermarked content was detected in one or more images. A black image will be returned instead.'
            ' Try again with a different prompt and/or seed.'      )

      for idx, watermark_detected_ in enumerate(_A      ):
        if watermark_detected_:
          a					:      Optional[int]    =       np.zeros(images[idx].shape      )

      return images, nsfw_detected, watermark_detected












"""simple docstring"""


import unittest

from transformers import AutoTokenizer, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow


if is_flax_available():
   import jax.numpy as jnp

   from transformers import FlaxXLMRobertaModel






@require_sentencepiece
@require_tokenizers
@require_flax
class 						snake_case  (  unittest.TestCase	):





    @slow
    def     lowerCamelCase__				(							self     : Optional[Any]      ):


      '''simple docstring'''




      a					:      Optional[int]    =       FlaxXLMRobertaModel.from_pretrained('xlm-roberta-base'      )
      a					:      int    =       AutoTokenizer.from_pretrained('xlm-roberta-base'      )
      a					:      int    =       'The dog is cute and lives in the garden house'
      a					:      List[Any]    =       jnp.array([tokenizer.encode(A      )]      )

      a					:      int    =       (1, 1_2, 7_6_8)  # batch_size, sequence_length, embedding_vector_dim
      a					:      Dict    =       jnp.array(
          [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]]      )

      a					:      Any    =       model(A      )['last_hidden_state']
      self.assertEqual(output.shape    ,		A      )
      # compare the actual values for a slice of last dim
      self.assertTrue(jnp.allclose(output[:, :, -1]    ,		A    ,		atol=1E-3      )      )




