
"""simple docstring"""



import argparse

import torch

from transformers import (
    UniSpeechSatConfig,
    UniSpeechSatForAudioFrameClassification,
    UniSpeechSatForSequenceClassification,
    UniSpeechSatForXVector,
    WavaVecaFeatureExtractor,
    logging,
)


logging.set_verbosity_info()
UpperCamelCase__          =	logging.get_logger(__name__)
def 					lowerCamelCase   (     _snake_case    ,_snake_case    ,_snake_case ):
    UpperCAmelCase__	:      List[Any]     							=					UniSpeechSatForSequenceClassification.from_pretrained(A__    ,config=A__ )
    UpperCAmelCase__	:      str     							=					downstream_dict['projector.weight']
    UpperCAmelCase__	:      List[str]     							=					downstream_dict['projector.bias']
    UpperCAmelCase__	:      Optional[Any]     							=					downstream_dict['model.post_net.linear.weight']
    UpperCAmelCase__	:      List[str]     							=					downstream_dict['model.post_net.linear.bias']
    return model
def 					lowerCamelCase   (     _snake_case    ,_snake_case    ,_snake_case ):
    UpperCAmelCase__	:      int     							=					UniSpeechSatForAudioFrameClassification.from_pretrained(A__    ,config=A__ )
    UpperCAmelCase__	:      Optional[int]     							=					downstream_dict['model.linear.weight']
    UpperCAmelCase__	:      List[Any]     							=					downstream_dict['model.linear.bias']
    return model
def 					lowerCamelCase   (     _snake_case    ,_snake_case    ,_snake_case ):
    UpperCAmelCase__	:      Tuple     							=					UniSpeechSatForXVector.from_pretrained(A__    ,config=A__ )
    UpperCAmelCase__	:      List[str]     							=					downstream_dict['connector.weight']
    UpperCAmelCase__	:      List[Any]     							=					downstream_dict['connector.bias']
    for i, kernel_size in enumerate(hf_config.tdnn_kernel ):
        UpperCAmelCase__	:      int     							=					downstream_dict[
            F'''model.framelevel_feature_extractor.module.{i}.kernel.weight'''
        ]
        UpperCAmelCase__	:      Any     							=					downstream_dict[F'''model.framelevel_feature_extractor.module.{i}.kernel.bias''']

    UpperCAmelCase__	:      Tuple     							=					downstream_dict['model.utterancelevel_feature_extractor.linear1.weight']
    UpperCAmelCase__	:      Optional[int]     							=					downstream_dict['model.utterancelevel_feature_extractor.linear1.bias']
    UpperCAmelCase__	:      List[Any]     							=					downstream_dict['model.utterancelevel_feature_extractor.linear2.weight']
    UpperCAmelCase__	:      Optional[Any]     							=					downstream_dict['model.utterancelevel_feature_extractor.linear2.bias']
    UpperCAmelCase__	:      Union[str, Any]     							=					downstream_dict['objective.W']
    return model
@torch.no_grad()
def 					lowerCamelCase   (     _snake_case    ,_snake_case    ,_snake_case    ,_snake_case ):
    UpperCAmelCase__	:      Union[str, Any]     							=					torch.load(A__    ,map_location='cpu' )

    UpperCAmelCase__	:      List[Any]     							=					checkpoint['Downstream']

    UpperCAmelCase__	:      Dict     							=					UniSpeechSatConfig.from_pretrained(A__ )
    UpperCAmelCase__	:      str     							=					WavaVecaFeatureExtractor.from_pretrained(
        A__    ,return_attention_mask=A__    ,do_normalize=A__ )

    UpperCAmelCase__	:      Optional[Any]     							=					hf_config.architectures[0]
    if arch.endswith('ForSequenceClassification' ):
        UpperCAmelCase__	:      Tuple     							=					convert_classification(A__    ,A__    ,A__ )
    elif arch.endswith('ForAudioFrameClassification' ):
        UpperCAmelCase__	:      Dict     							=					convert_diarization(A__    ,A__    ,A__ )
    elif arch.endswith('ForXVector' ):
        UpperCAmelCase__	:      Optional[int]     							=					convert_xvector(A__    ,A__    ,A__ )
    else:
        raise NotImplementedError(F'''S3PRL weights conversion is not supported for {arch}''' )

    if hf_config.use_weighted_layer_sum:
        UpperCAmelCase__	:      Any     							=					checkpoint['Featurizer']['weights']

    hf_feature_extractor.save_pretrained(A__ )
    hf_model.save_pretrained(A__ )


if __name__ == "__main__":
    UpperCamelCase__          =	argparse.ArgumentParser()
    parser.add_argument(
        '--base_model_name', default=None, type=str, help='Name of the huggingface pretrained base model.'
    )
    parser.add_argument('--config_path', default=None, type=str, help='Path to the huggingface classifier config.')
    parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to the s3prl checkpoint.')
    parser.add_argument('--model_dump_path', default=None, type=str, help='Path to the final converted model.')
    UpperCamelCase__          =	parser.parse_args()
    convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)




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

from ...utils import logging


lowercase_   							=  logging.get_logger(__name__)
def 	a (     A__      :  Optional[int] ,  A__      :  Union[str, Any]	)       -> int:





      """simple docstring"""



      _lowercase      			=nn.functional.normalize(A__	)
      _lowercase      			=nn.functional.normalize(A__	)
      return torch.mm(A__ ,  normalized_text_embeds.t()	)







class 					__lowerCAmelCase	(   SCREAMING_SNAKE_CASE				):
  _a		     =    CLIPConfig

  _a		     =    ["""CLIPEncoderLayer"""]




  def __init__(							self				,  lowerCAmelCase   )    ->					Union[str, Any]:
        '''simple docstring'''






        super().__init__(lowerCAmelCase   )

        _lowercase      			=CLIPVisionModel(config.vision_config   )
        _lowercase      			=nn.Linear(config.vision_config.hidden_size				,  config.projection_dim				,  bias=lowerCAmelCase   )

        _lowercase      			=nn.Parameter(torch.ones(17				,  config.projection_dim   )				,  requires_grad=lowerCAmelCase   )
        _lowercase      			=nn.Parameter(torch.ones(3				,  config.projection_dim   )				,  requires_grad=lowerCAmelCase   )

        _lowercase      			=nn.Parameter(torch.ones(17   )				,  requires_grad=lowerCAmelCase   )
        _lowercase      			=nn.Parameter(torch.ones(3   )				,  requires_grad=lowerCAmelCase   )




  @torch.no_grad()
  def     A__	(							self				,  lowerCAmelCase				,  lowerCAmelCase   )    ->					Optional[int]:
        '''simple docstring'''






        _lowercase      			=self.vision_model(lowerCAmelCase   )[1]  # pooled_output
        _lowercase      			=self.visual_projection(lowerCAmelCase   )

        # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
        _lowercase      			=cosine_distance(lowerCAmelCase				,  self.special_care_embeds   ).cpu().float().numpy()
        _lowercase      			=cosine_distance(lowerCAmelCase				,  self.concept_embeds   ).cpu().float().numpy()

        _lowercase      			=[]
        _lowercase      			=image_embeds.shape[0]
        for i in range(lowerCAmelCase   ):
              _lowercase      			={'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      			=0.0

              for concept_idx in range(len(special_cos_dist[0]   )   ):
                    _lowercase      			=special_cos_dist[i][concept_idx]
                    _lowercase      			=self.special_care_embeds_weights[concept_idx].item()
                    _lowercase      			=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      			=0.01

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

              result.append(lowerCAmelCase   )

        _lowercase      			=[len(res['bad_concepts']   ) > 0 for res in result]

        return images, has_nsfw_concepts




  @torch.no_grad()
  def     A__	(							self				,  lowerCAmelCase				,  lowerCAmelCase   )    ->					Tuple:
        '''simple docstring'''






        _lowercase      			=self.vision_model(lowerCAmelCase   )[1]  # pooled_output
        _lowercase      			=self.visual_projection(lowerCAmelCase   )

        _lowercase      			=cosine_distance(lowerCAmelCase				,  self.special_care_embeds   )
        _lowercase      			=cosine_distance(lowerCAmelCase				,  self.concept_embeds   )

        # increase this value to create a stronger `nsfw` filter
        # at the cost of increasing the possibility of filtering benign images
        _lowercase      			=0.0

        _lowercase      			=special_cos_dist - self.special_care_embeds_weights + adjustment
        # special_scores = special_scores.round(decimals=3)
        _lowercase      			=torch.any(special_scores > 0				,  dim=1   )
        _lowercase      			=special_care * 0.01
        _lowercase      			=special_adjustment.unsqueeze(1   ).expand(-1				,  cos_dist.shape[1]   )

        _lowercase      			=(cos_dist - self.concept_embeds_weights) + special_adjustment
        # concept_scores = concept_scores.round(decimals=3)
        _lowercase      			=torch.any(concept_scores > 0				,  dim=1   )

        return images, has_nsfw_concepts


