




'''simple docstring'''






import unittest

from transformers import SpeechaTextaConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers.models.speech_to_text_a.modeling_speech_to_text_a import (
          SpeechaTextaDecoder,
          SpeechaTextaForCausalLM,
      )

@require_torch
class   SCREAMING_SNAKE_CASE_		:



 def __init__(  self   ,		A   ,		A=99   ,		A=13   ,		A=16   ,		A=7   ,		A=True   ,		A=True   ,		A=True   ,		A=False   ,		A=True   ,		A=2   ,		A=32   ,		A=4   ,		A=4   ,		A=30   ,		A=0   ,		A=1   ,		A=2   ,		A=None   ,		):






        '''simple docstring'''




        lowercase  :		List[Any]						=      parent
        lowercase  :		Any						=      batch_size
        lowercase  :		List[Any]						=      decoder_seq_length
        # For common tests
        lowercase  :		Tuple						=      self.decoder_seq_length
        lowercase  :		List[str]						=      is_training
        lowercase  :		int						=      use_attention_mask
        lowercase  :		List[str]						=      use_labels

        lowercase  :		Optional[int]						=      vocab_size
        lowercase  :		Dict						=      d_model
        lowercase  :		List[Any]						=      d_model
        lowercase  :		str						=      decoder_layers
        lowercase  :		Optional[Any]						=      decoder_layers
        lowercase  :		Dict						=      decoder_ffn_dim
        lowercase  :		List[str]						=      decoder_attention_heads
        lowercase  :		int						=      decoder_attention_heads
        lowercase  :		str						=      eos_token_id
        lowercase  :		List[Any]						=      bos_token_id
        lowercase  :		Optional[int]						=      pad_token_id
        lowercase  :		Optional[Any]						=      decoder_start_token_id
        lowercase  :		Optional[int]						=      use_cache
        lowercase  :		int						=      max_position_embeddings

        lowercase  :		Any						=      None
        lowercase  :		Dict						=      decoder_seq_length
        lowercase  :		Optional[int]						=      2
        lowercase  :		List[str]						=      1



 def    SCREAMING_SNAKE_CASE(  self ):






        '''simple docstring'''




        lowercase  :		Optional[Any]						=      ids_tensor([self.batch_size, self.decoder_seq_length]   ,		self.vocab_size )

        lowercase  :		Dict						=      None
        if self.use_attention_mask:
               lowercase  :		int						=      ids_tensor([self.batch_size, self.decoder_seq_length]   ,		vocab_size=2 )

        lowercase  :		Tuple						=      None
        if self.use_labels:
               lowercase  :		Dict						=      ids_tensor([self.batch_size, self.decoder_seq_length]   ,		self.vocab_size )

        lowercase  :		Dict						=      SpeechaTextaConfig(
            vocab_size=self.vocab_size   ,		d_model=self.d_model   ,		decoder_layers=self.decoder_layers   ,		decoder_ffn_dim=self.decoder_ffn_dim   ,		decoder_attention_heads=self.decoder_attention_heads   ,		eos_token_id=self.eos_token_id   ,		bos_token_id=self.bos_token_id   ,		use_cache=self.use_cache   ,		pad_token_id=self.pad_token_id   ,		decoder_start_token_id=self.decoder_start_token_id   ,		max_position_embeddings=self.max_position_embeddings   ,		)

        return (
            config,
            input_ids,
            attention_mask,
            lm_labels,
        )



 def    SCREAMING_SNAKE_CASE(  self   ,		A   ,		A   ,		A   ,		A   ,		):






        '''simple docstring'''




        lowercase  :		List[Any]						=      True
        lowercase  :		Tuple						=      SpeechaTextaDecoder(config=A ).to(A ).eval()
        lowercase  :		Any						=      input_ids[:2]

        input_ids[input_ids == 0] += 1
        # first forward pass
        lowercase  :		Union[str, Any]						=      model(A   ,		use_cache=A )
        lowercase  :		str						=      model(A )
        lowercase  :		Tuple						=      model(A   ,		use_cache=A )

        self.parent.assertTrue(len(A ) == len(A ) )
        self.parent.assertTrue(len(A ) == len(A ) + 1 )

        lowercase  :		Any						=      outputs['''past_key_values''']

        # create hypothetical next token and extent to next_input_ids
        lowercase  :		Optional[int]						=      ids_tensor((2, 1)   ,		config.vocab_size - 1 ) + 1

        # append to next input_ids and
        lowercase  :		Optional[Any]						=      torch.cat([input_ids, next_tokens]   ,		dim=-1 )

        print(A )

        lowercase  :		Dict						=      model(A )['''last_hidden_state''']
        lowercase  :		Tuple						=      model(A   ,		past_key_values=A )['''last_hidden_state''']

        # select random slice
        lowercase  :		List[Any]						=      ids_tensor((1,)   ,		output_from_past.shape[-1] ).item()
        lowercase  :		Dict						=      output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
        lowercase  :		Dict						=      output_from_past[:, 0, random_slice_idx].detach()

        # test that outputs are equal for slice
        assert torch.allclose(A   ,		A   ,		atol=1E-3 )





 def    SCREAMING_SNAKE_CASE(  self ):






        '''simple docstring'''




        lowercase  :		Optional[Any]						=      self.prepare_config_and_inputs()
        (
            (
            lowercase
        ),     (
            lowercase
        ),     (
            lowercase
        ),     (
            lowercase
        ),     
        )  :		int						=      config_and_inputs

        lowercase  :		List[Any]						=      {
            '''input_ids''': input_ids,
            '''attention_mask''': attention_mask,
        }
        return config, inputs_dict






@require_torch
class   SCREAMING_SNAKE_CASE_		(       lowerCamelCase_		,     lowerCamelCase_		,     lowerCamelCase_		,     unittest.TestCase				):
 __snake_case	:     str			   =      (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
 __snake_case	:     int			   =      (SpeechaTextaForCausalLM,) if is_torch_available() else ()
 __snake_case	:     int			   =      {"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
 __snake_case	:     Optional[int]			   =      True
 __snake_case	:     int			   =      False



 def    SCREAMING_SNAKE_CASE(  self   ,		):






        '''simple docstring'''




        lowercase  :		Any						=      SpeechaTextaStandaloneDecoderModelTester(self   ,		is_training=A )
        lowercase  :		Dict						=      ConfigTester(self   ,		config_class=A )



 def    SCREAMING_SNAKE_CASE(  self ):






        '''simple docstring'''




        pass



 def    SCREAMING_SNAKE_CASE(  self ):






        '''simple docstring'''




        pass



 def    SCREAMING_SNAKE_CASE(  self ):






        '''simple docstring'''




        pass



 def    SCREAMING_SNAKE_CASE(  self ):






        '''simple docstring'''




        self.config_tester.run_common_tests()



 def    SCREAMING_SNAKE_CASE(  self ):






        '''simple docstring'''




        lowercase  :		Optional[int]						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_decoder_model_past(*A )





 def    SCREAMING_SNAKE_CASE(  self ):






        '''simple docstring'''




        return










'''simple docstring'''






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

from ...utils import logging


lowercase	:      Optional[Any]				=       logging.get_logger(__name__)




def 		a_( lowerCamelCase   ,				lowerCamelCase      )				->Optional[int]:






       '''simple docstring'''




       lowercase  :		List[str]						=      nn.functional.normalize(lowerCamelCase      )
       lowercase  :		Optional[int]						=      nn.functional.normalize(lowerCamelCase      )
       return torch.mm(lowerCamelCase   ,				normalized_text_embeds.t()      )





class   SCREAMING_SNAKE_CASE_		(       lowerCamelCase_				):
 __snake_case	:     List[str]			   =      CLIPConfig

 __snake_case	:     Optional[int]			   =      ["CLIPEncoderLayer"]



 def __init__(  self   ,		A ):






        '''simple docstring'''




        super().__init__(A )

        lowercase  :		Optional[Any]						=      CLIPVisionModel(config.vision_config )
        lowercase  :		Tuple						=      nn.Linear(config.vision_config.hidden_size   ,		config.projection_dim   ,		bias=A )

        lowercase  :		Any						=      nn.Parameter(torch.ones(17   ,		config.projection_dim )   ,		requires_grad=A )
        lowercase  :		int						=      nn.Parameter(torch.ones(3   ,		config.projection_dim )   ,		requires_grad=A )

        lowercase  :		List[str]						=      nn.Parameter(torch.ones(17 )   ,		requires_grad=A )
        lowercase  :		str						=      nn.Parameter(torch.ones(3 )   ,		requires_grad=A )



 @torch.no_grad()
 def    SCREAMING_SNAKE_CASE(  self   ,		A   ,		A ):






        '''simple docstring'''




        lowercase  :		Union[str, Any]						=      self.vision_model(A )[1]  # pooled_output
        lowercase  :		List[Any]						=      self.visual_projection(A )

        # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
        lowercase  :		Optional[Any]						=      cosine_distance(A   ,		self.special_care_embeds ).cpu().float().numpy()
        lowercase  :		int						=      cosine_distance(A   ,		self.concept_embeds ).cpu().float().numpy()

        lowercase  :		Dict						=      []
        lowercase  :		str						=      image_embeds.shape[0]
        for i in range(A ):
               lowercase  :		List[str]						=      {'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []}

               # increase this value to create a stronger `nfsw` filter
               # at the cost of increasing the possibility of filtering benign images
               lowercase  :		str						=      0.0

               for concept_idx in range(len(special_cos_dist[0] ) ):
                      lowercase  :		List[str]						=      special_cos_dist[i][concept_idx]
                      lowercase  :		List[Any]						=      self.special_care_embeds_weights[concept_idx].item()
                      lowercase  :		Optional[Any]						=      round(concept_cos - concept_threshold + adjustment   ,		3 )
                      if result_img["special_scores"][concept_idx] > 0:
                             result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} )
                             lowercase  :		List[str]						=      0.0_1

               for concept_idx in range(len(cos_dist[0] ) ):
                      lowercase  :		List[Any]						=      cos_dist[i][concept_idx]
                      lowercase  :		Optional[Any]						=      self.concept_embeds_weights[concept_idx].item()
                      lowercase  :		Any						=      round(concept_cos - concept_threshold + adjustment   ,		3 )
                      if result_img["concept_scores"][concept_idx] > 0:
                             result_img["bad_concepts"].append(A )

               result.append(A )

        lowercase  :		Dict						=      [len(res['''bad_concepts'''] ) > 0 for res in result]

        for idx, has_nsfw_concept in enumerate(A ):
               if has_nsfw_concept:
                      if torch.is_tensor(A ) or torch.is_tensor(images[0] ):
                             lowercase  :		Any						=      torch.zeros_like(images[idx] )  # black image
                      else:
                             lowercase  :		Tuple						=      np.zeros(images[idx].shape )  # black image

        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.''' )

        return images, has_nsfw_concepts





 @torch.no_grad()
 def    SCREAMING_SNAKE_CASE(  self   ,		A   ,		A ):






        '''simple docstring'''




        lowercase  :		Dict						=      self.vision_model(A )[1]  # pooled_output
        lowercase  :		Optional[int]						=      self.visual_projection(A )

        lowercase  :		Tuple						=      cosine_distance(A   ,		self.special_care_embeds )
        lowercase  :		List[str]						=      cosine_distance(A   ,		self.concept_embeds )

        # increase this value to create a stronger `nsfw` filter
        # at the cost of increasing the possibility of filtering benign images
        lowercase  :		List[str]						=      0.0

        lowercase  :		Optional[Any]						=      special_cos_dist - self.special_care_embeds_weights + adjustment
        # special_scores = special_scores.round(decimals=3)
        lowercase  :		Union[str, Any]						=      torch.any(special_scores > 0   ,		dim=1 )
        lowercase  :		Union[str, Any]						=      special_care * 0.0_1
        lowercase  :		Any						=      special_adjustment.unsqueeze(1 ).expand(-1   ,		cos_dist.shape[1] )

        lowercase  :		Union[str, Any]						=      (cos_dist - self.concept_embeds_weights) + special_adjustment
        # concept_scores = concept_scores.round(decimals=3)
        lowercase  :		Tuple						=      torch.any(concept_scores > 0   ,		dim=1 )

        lowercase  :		Dict						=      0.0  # black image

        return images, has_nsfw_concepts




