

# 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 			A						(							_UpperCAmelCase			):







    """simple docstring"""





    lowerCamelCase		     =							'facebook/bart-large-mnli'
    lowerCamelCase		     =							(
        '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.'
    )
    lowerCamelCase		     =							'text_classifier'
    lowerCamelCase		     =							AutoTokenizer
    lowerCamelCase		     =							AutoModelForSequenceClassification

    lowerCamelCase		     =							['text', ['text']]
    lowerCamelCase		     =							['text']


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





        '''simple docstring'''







        super().setup()
        A__   				=       self.model.config
        A__   				=       -1
        for idx, label in config.idalabel.items():
            if label.lower().startswith('entail'		):
                A__   				=       int(lowercase_		)
        if self.entailment_id == -1:
            raise ValueError('Could not determine the entailment ID from the model config, please pass it at init.'		)


    def 						snake_case__					(				self			:	Dict,lowercase_			:	Union[str, Any],lowercase_			:	Tuple		)->   List[Any]:





        '''simple docstring'''







        A__   				=       labels
        return self.pre_processor(
            [text] * len(lowercase_		),[F'This example is {label}' for label in labels],return_tensors='pt',padding='max_length',)


    def 						snake_case__					(				self			:	Any,lowercase_			:	Dict		)->   List[Any]:





        '''simple docstring'''







        A__   				=       outputs.logits
        A__   				=       torch.argmax(logits[:, 2]		).item()
        return self._labels[label_id]




import os
import tempfile
import unittest
from pathlib import Path

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


if is_torch_available():
     from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments


@require_torch
class 			A						(							unittest.TestCase			):







    """simple docstring"""





    def 						snake_case__					(				self			:	List[Any],lowercase_			:	str		)->   List[Any]:





        '''simple docstring'''







        for model_result in results.values():
            for batch_size, sequence_length in zip(model_result['bs'],model_result['ss']		):
                A__   				=       model_result['result'][batch_size][sequence_length]
                self.assertIsNotNone(lowercase_		)


    def 						snake_case__					(				self			:	Dict		)->   List[str]:





        '''simple docstring'''







        A__   				=       'sshleifer/tiny-gpt2'
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,sequence_lengths=[8],batch_sizes=[1],multi_process=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_inference_result		)
        self.check_results_dict_not_empty(results.memory_inference_result		)


    def 						snake_case__					(				self			:	Dict		)->   List[str]:





        '''simple docstring'''







        A__   				=       'sgugger/tiny-distilbert-classification'
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,sequence_lengths=[8],batch_sizes=[1],multi_process=lowercase_,only_pretrain_model=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_inference_result		)
        self.check_results_dict_not_empty(results.memory_inference_result		)


    def 						snake_case__					(				self			:	List[Any]		)->   Any:





        '''simple docstring'''







        A__   				=       'sshleifer/tiny-gpt2'
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,torchscript=lowercase_,sequence_lengths=[8],batch_sizes=[1],multi_process=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_inference_result		)
        self.check_results_dict_not_empty(results.memory_inference_result		)


    @unittest.skipIf(torch_device == 'cpu','Cant do half precision'		)
    def 						snake_case__					(				self			:	Any		)->   Dict:





        '''simple docstring'''







        A__   				=       'sshleifer/tiny-gpt2'
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,fpaa=lowercase_,sequence_lengths=[8],batch_sizes=[1],multi_process=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_inference_result		)
        self.check_results_dict_not_empty(results.memory_inference_result		)


    def 						snake_case__					(				self			:	Any		)->   Optional[Any]:





        '''simple docstring'''







        A__   				=       'sshleifer/tiny-gpt2'
        A__   				=       AutoConfig.from_pretrained(lowercase_		)
        # set architectures equal to `None`
        A__   				=       None
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,sequence_lengths=[8],batch_sizes=[1],multi_process=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_,configs=[config]		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_inference_result		)
        self.check_results_dict_not_empty(results.memory_inference_result		)


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





        '''simple docstring'''







        A__   				=       'sshleifer/tiny-gpt2'
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,sequence_lengths=[8],batch_sizes=[1],multi_process=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_train_result		)
        self.check_results_dict_not_empty(results.memory_train_result		)


    @unittest.skipIf(torch_device == 'cpu','Can\'t do half precision'		)
    def 						snake_case__					(				self			:	List[Any]		)->   Dict:





        '''simple docstring'''







        A__   				=       'sshleifer/tiny-gpt2'
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,sequence_lengths=[8],batch_sizes=[1],fpaa=lowercase_,multi_process=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_train_result		)
        self.check_results_dict_not_empty(results.memory_train_result		)


    def 						snake_case__					(				self			:	int		)->   Optional[int]:





        '''simple docstring'''







        A__   				=       'sshleifer/tiny-gpt2'
        A__   				=       AutoConfig.from_pretrained(lowercase_		)
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,sequence_lengths=[8],batch_sizes=[1],multi_process=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_,configs=[config]		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_inference_result		)
        self.check_results_dict_not_empty(results.memory_inference_result		)


    def 						snake_case__					(				self			:	List[Any]		)->   Any:





        '''simple docstring'''







        A__   				=       'sshleifer/tinier_bart'
        A__   				=       AutoConfig.from_pretrained(lowercase_		)
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,sequence_lengths=[8],batch_sizes=[1],multi_process=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_,configs=[config]		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_inference_result		)
        self.check_results_dict_not_empty(results.memory_inference_result		)


    def 						snake_case__					(				self			:	List[str]		)->   List[str]:





        '''simple docstring'''







        A__   				=       'sshleifer/tiny-gpt2'
        A__   				=       AutoConfig.from_pretrained(lowercase_		)
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,sequence_lengths=[8],batch_sizes=[1],multi_process=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_,configs=[config]		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_train_result		)
        self.check_results_dict_not_empty(results.memory_train_result		)


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





        '''simple docstring'''







        A__   				=       'sshleifer/tinier_bart'
        A__   				=       AutoConfig.from_pretrained(lowercase_		)
        A__   				=       PyTorchBenchmarkArguments(
            models=[MODEL_ID],training=lowercase_,inference=lowercase_,sequence_lengths=[8],batch_sizes=[1],multi_process=lowercase_,)
        A__   				=       PyTorchBenchmark(lowercase_,configs=[config]		)
        A__   				=       benchmark.run()
        self.check_results_dict_not_empty(results.time_train_result		)
        self.check_results_dict_not_empty(results.memory_train_result		)


    def 						snake_case__					(				self			:	Optional[Any]		)->   Tuple:





        '''simple docstring'''







        A__   				=       'sshleifer/tiny-gpt2'
        with tempfile.TemporaryDirectory() as tmp_dir:
            A__   				=       PyTorchBenchmarkArguments(
                models=[MODEL_ID],training=lowercase_,inference=lowercase_,save_to_csv=lowercase_,sequence_lengths=[8],batch_sizes=[1],inference_time_csv_file=os.path.join(lowercase_,'inf_time.csv'		),train_memory_csv_file=os.path.join(lowercase_,'train_mem.csv'		),inference_memory_csv_file=os.path.join(lowercase_,'inf_mem.csv'		),train_time_csv_file=os.path.join(lowercase_,'train_time.csv'		),env_info_csv_file=os.path.join(lowercase_,'env.csv'		),multi_process=lowercase_,)
            A__   				=       PyTorchBenchmark(lowercase_		)
            benchmark.run()
            self.assertTrue(Path(os.path.join(lowercase_,'inf_time.csv'		)		).exists()		)
            self.assertTrue(Path(os.path.join(lowercase_,'train_time.csv'		)		).exists()		)
            self.assertTrue(Path(os.path.join(lowercase_,'inf_mem.csv'		)		).exists()		)
            self.assertTrue(Path(os.path.join(lowercase_,'train_mem.csv'		)		).exists()		)
            self.assertTrue(Path(os.path.join(lowercase_,'env.csv'		)		).exists()		)


    def 						snake_case__					(				self			:	Tuple		)->   str:





        '''simple docstring'''







        A__   				=       'sshleifer/tiny-gpt2'

        def _check_summary_is_not_empty(lowercase_			:	Optional[Any]		):
            self.assertTrue(hasattr(lowercase_,'sequential'		)		)
            self.assertTrue(hasattr(lowercase_,'cumulative'		)		)
            self.assertTrue(hasattr(lowercase_,'current'		)		)
            self.assertTrue(hasattr(lowercase_,'total'		)		)

        with tempfile.TemporaryDirectory() as tmp_dir:
            A__   				=       PyTorchBenchmarkArguments(
                models=[MODEL_ID],training=lowercase_,inference=lowercase_,sequence_lengths=[8],batch_sizes=[1],log_filename=os.path.join(lowercase_,'log.txt'		),log_print=lowercase_,trace_memory_line_by_line=lowercase_,multi_process=lowercase_,)
            A__   				=       PyTorchBenchmark(lowercase_		)
            A__   				=       benchmark.run()
            _check_summary_is_not_empty(result.inference_summary		)
            _check_summary_is_not_empty(result.train_summary		)
            self.assertTrue(Path(os.path.join(lowercase_,'log.txt'		)		).exists()		)

