





# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch

from ..models.auto import AutoModelForSequenceClassification, AutoTokenizer
from .base import PipelineTool



class   _SCREAMING_SNAKE_CASE		(      __SCREAMING_SNAKE_CASE       ):







 '''simple docstring'''
 lowercase_						=     "facebook/bart-large-mnli"
 lowercase_						=     (
     "This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which "
     "should be the text to classify, and `labels`, which should be the list of labels to use for classification. "
     "It returns the most likely label in the list of provided `labels` for the input text."
 )
 lowercase_						=     "text_classifier"
 lowercase_						=     AutoTokenizer
 lowercase_						=     AutoModelForSequenceClassification

 lowercase_						=     ["text", ["text"]]
 lowercase_						=     ["text"]

 def 				SCREAMING_SNAKE_CASE_   (self							:  Any)			->Union[str, Any]:



   '''simple docstring'''



   super().setup()
   lowerCamelCase__: Optional[int]         =self.model.config
   lowerCamelCase__: Any         =-1
   for idx, label in config.idalabel.items():
     if label.lower().startswith("entail"):
       lowerCamelCase__: Union[str, Any]         =int(UpperCAmelCase_)
   if self.entailment_id == -1:
     raise ValueError("Could not determine the entailment ID from the model config, please pass it at init.")

 def 				SCREAMING_SNAKE_CASE_   (self							:  Union[str, Any]			,					UpperCAmelCase_							:  List[Any]			,					UpperCAmelCase_							:  Union[str, Any])			->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__: Dict         =labels
   return self.pre_processor(
       [text] * len(UpperCAmelCase_)			,					[F"""This example is {label}""" for label in labels]			,					return_tensors="pt"			,					padding="max_length"			,					)







 def 				SCREAMING_SNAKE_CASE_   (self							:  Dict			,					UpperCAmelCase_							:  str)			->Any:



   '''simple docstring'''



   lowerCamelCase__: List[Any]         =outputs.logits
   lowerCamelCase__: Optional[Any]         =torch.argmax(logits[:, 2]).item()
   return self._labels[label_id]



"""simple docstring"""



import argparse

import torch
from torch import nn

from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration
def 					lowerCamelCase   (     _snake_case ):
    UpperCAmelCase__	:      int     							=					[
        'encoder.version',
        'decoder.version',
        'model.encoder.version',
        'model.decoder.version',
        'decoder.output_projection.weight',
        '_float_tensor',
        'encoder.embed_positions._float_tensor',
        'decoder.embed_positions._float_tensor',
    ]
    for k in ignore_keys:
        state_dict.pop(_snake_case    ,_snake_case )
def 					lowerCamelCase   (     _snake_case ):
    UpperCAmelCase__							,							UpperCAmelCase__	:      Optional[Any]     							=					emb.weight.shape
    UpperCAmelCase__	:      List[str]     							=					nn.Linear(_snake_case    ,_snake_case    ,bias=_snake_case )
    UpperCAmelCase__	:      Any     							=					emb.weight.data
    return lin_layer
def 					lowerCamelCase   (     _snake_case ):
    UpperCAmelCase__	:      Optional[int]     							=					torch.load(_snake_case    ,map_location='cpu' )
    UpperCAmelCase__	:      Any     							=					mam_aaa['args'] or mam_aaa['cfg']['model']
    UpperCAmelCase__	:      Optional[int]     							=					mam_aaa['model']
    remove_ignore_keys_(_snake_case )
    UpperCAmelCase__	:      Optional[int]     							=					state_dict['encoder.embed_tokens.weight'].shape[0]

    UpperCAmelCase__	:      List[str]     							=					MaMaaaConfig(
        vocab_size=_snake_case    ,max_position_embeddings=1024    ,encoder_layers=args.encoder_layers    ,decoder_layers=args.decoder_layers    ,encoder_attention_heads=args.encoder_attention_heads    ,decoder_attention_heads=args.decoder_attention_heads    ,encoder_ffn_dim=args.encoder_ffn_embed_dim    ,decoder_ffn_dim=args.decoder_ffn_embed_dim    ,d_model=args.encoder_embed_dim    ,encoder_layerdrop=args.encoder_layerdrop    ,decoder_layerdrop=args.decoder_layerdrop    ,dropout=args.dropout    ,attention_dropout=args.attention_dropout    ,activation_dropout=args.activation_dropout    ,activation_function='relu'    ,)

    UpperCAmelCase__	:      Optional[int]     							=					state_dict['decoder.embed_tokens.weight']
    UpperCAmelCase__	:      Union[str, Any]     							=					MaMaaaForConditionalGeneration(_snake_case )
    model.model.load_state_dict(_snake_case    ,strict=_snake_case )
    UpperCAmelCase__	:      Optional[Any]     							=					make_linear_from_emb(model.model.shared )

    return model


if __name__ == "__main__":
    UpperCamelCase__          =	argparse.ArgumentParser()
    # Required parameters
    parser.add_argument('fairseq_path', type=str, help='path to a model.pt on local filesystem.')
    parser.add_argument('pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
    UpperCamelCase__          =	parser.parse_args()
    UpperCamelCase__          =	convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß)
    model.save_pretrained(args.pytorch_dump_folder_path)

