o
    ߥi                  	   @   s   d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ dd	lmZ ejejejd
ejejejd
ejejejd
ejejejd
G dd deeZdS )    ) RobertaForSequenceClassification)Models)Model
TorchModel)MODELS)&AttentionTextClassificationModelOutput)Tasks)parse_labels_in_order   )
VecoConfig)module_namec                       s@   e Zd ZdZeZ fddZ fddZe fddZ	  Z
S )VecoForSequenceClassificationa	  Veco Model transformer with a sequence classification/regression head on top (a linear layer on top of the
    pooled output) e.g. for GLUE tasks.

    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic
    methods the library implements for all its model (such as downloading or saving, resizing the input embeddings,
    pruning heads etc.)

    This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)
    subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to
    general usage and behavior.

    Preprocessor:
        This is the text classification model of Veco, the preprocessor of this model
        is `modelscope.preprocessors.TextClassificationTransformersPreprocessor`.

    Trainer:
        This model should be trained by dataset which has mixed languages,
        and evaluated by datasets of languages one by one.
        For example, if the training dataset is xnli (which has sub datasets of multiple languages), then you
        should mix the sub-datasets with the languages you want to train to one training dataset, and evaluate
        the model one sub-dataset by one sub-dataset of different languages.
        This procedure can be done by custom code. If you are using trainer of ModelScope,
        the `VecoTrainer` is suggested to use to train this model. This trainer overrides the basic evaluation
        loop, and will call the evaluation dataset one by one. Besides, this trainer will use the `VecoTaskDataset`
        to mix the input datasets to one, you can check the API Doc for the details.

        To check the complete example please
        view the unittest `test_veco_xnli` in `tests.trainers.test_finetune_sequence_classification.py`

    Parameters:
        config ([`VecoConfig`]): Model configuration class with all the parameters of the
            model. Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model
            weights.

    This class overrides [`RobertaForSequenceClassification`]. Please check the superclass for the
    appropriate documentation alongside usage examples.
    c                    s*   t  j|jfi | t t| | d S )N)super__init__name_or_pathr   )selfconfigkwargs	__class__ b/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/veco/text_classification.pyr   M   s   z&VecoForSequenceClassification.__init__c                    s6   d|d< t t| j|i |}t|j|j|j|jdS )u*  
        Returns:
            Returns `modelscope.outputs.AttentionTextClassificationModelOutput`

        Examples:
            >>> from modelscope.models import Model
            >>> from modelscope.preprocessors import Preprocessor
            >>> model = Model.from_pretrained('damo/nlp_veco_fill-mask-large',
            >>>                               task='text-classification', num_labels=2)
            >>> preprocessor = Preprocessor.from_pretrained('damo/nlp_veco_fill-mask-large',
            >>>                                             label2id={'0': 0, '1': 1})
            >>> # Call the model, return some tensors
            >>> print(model(**preprocessor('这是个测试')))
            >>> # Call the pipeline, the result may be incorrect
            >>> from modelscope.pipelines import pipeline
            >>> pipeline_ins = pipeline('text-classification', pipeline_name='text-classification',
            >>>                         model=model, preprocessor=preprocessor)
            >>> print(pipeline_ins('这是个测试'))
        Treturn_dict)losslogitshidden_states
attentions)r   r   forwardr   r   r   r   r   )r   argsr   outputsr   r   r   r   Q   s   z%VecoForSequenceClassification.forwardc                    sj   | dd}| dd}t||fi |}|du r&tdi |}| |}|S tt| jdd|i|}|S )al  Instantiate the model.

        Args:
            kwargs: Input args.
                    model_dir: The model dir used to load the checkpoint and the label information.
                    num_labels: An optional arg to tell the model how many classes to initialize.
                                    Method will call utils.parse_label_mapping if num_labels is not input.
                    label2id: An optional label2id mapping, which will cover the label2id in configuration (if exists).

        Returns:
            The loaded model, which is initialized by transformers.PreTrainedModel.from_pretrained
        	model_dirNcfgpretrained_model_name_or_pathr   )popr	   r   r   r   from_pretrained)clsr   r    r!   
model_argsr   modelr   r   r   _instantiateo   s   z*VecoForSequenceClassification._instantiate)__name__
__module____qualname____doc__r   config_classr   r   classmethodr(   __classcell__r   r   r   r   r      s    'r   N)transformersr   modelscope.metainfor   modelscope.modelsr   r   modelscope.models.builderr   modelscope.outputsr   modelscope.utils.constantr   modelscope.utils.nlp.utilsr	   configurationr   register_modulenlivecosentiment_classificationsentence_similaritytext_classificationr   r   r   r   r   <module>   s"   