"""simple docstring"""




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					):


   def       lowercase_				(self					:						Optional[int]	,  __UpperCAmelCase					:						Optional[int]		)						->    List[str]:
         """simple docstring"""



         for model_result in results.values():
               for batch_size, sequence_length in zip(model_result["bs"]	,  model_result["ss"]		):
                     UpperCAmelCase__								=  model_result["result"][batch_size][sequence_length]
                     self.assertIsNotNone(__UpperCAmelCase		)


   def       lowercase_				(self					:						str		)						->    Union[str, Any]:
         """simple docstring"""



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


   def       lowercase_				(self					:						Optional[Any]		)						->    Union[str, Any]:
         """simple docstring"""



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


   def       lowercase_				(self					:						Optional[int]		)						->    int:
         """simple docstring"""



         UpperCAmelCase__								=  "sshleifer/tiny-gpt2"
         UpperCAmelCase__								=  PyTorchBenchmarkArguments(
             models=[MODEL_ID]	,  training=__UpperCAmelCase	,  inference=__UpperCAmelCase	,  torchscript=__UpperCAmelCase	,  sequence_lengths=[8]	,  batch_sizes=[1]	,  multi_process=__UpperCAmelCase	,  )
         UpperCAmelCase__								=  PyTorchBenchmark(__UpperCAmelCase		)
         UpperCAmelCase__								=  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       lowercase_				(self					:						Any		)						->    Optional[Any]:
         """simple docstring"""



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


   def       lowercase_				(self					:						List[Any]		)						->    Tuple:
         """simple docstring"""



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


   def       lowercase_				(self					:						List[Any]		)						->    str:
         """simple docstring"""



         UpperCAmelCase__								=  "sshleifer/tiny-gpt2"
         UpperCAmelCase__								=  PyTorchBenchmarkArguments(
             models=[MODEL_ID]	,  training=__UpperCAmelCase	,  inference=__UpperCAmelCase	,  sequence_lengths=[8]	,  batch_sizes=[1]	,  multi_process=__UpperCAmelCase	,  )
         UpperCAmelCase__								=  PyTorchBenchmark(__UpperCAmelCase		)
         UpperCAmelCase__								=  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       lowercase_				(self					:						Tuple		)						->    List[Any]:
         """simple docstring"""



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


   def       lowercase_				(self					:						Optional[Any]		)						->    str:
         """simple docstring"""



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


   def       lowercase_				(self					:						str		)						->    List[Any]:
         """simple docstring"""



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


   def       lowercase_				(self					:						Tuple		)						->    Union[str, Any]:
         """simple docstring"""



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


   def       lowercase_				(self					:						int		)						->    List[Any]:
         """simple docstring"""



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


   def       lowercase_				(self					:						Union[str, Any]		)						->    Union[str, Any]:
         """simple docstring"""



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





   def       lowercase_				(self					:						Tuple		)						->    Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  "sshleifer/tiny-gpt2"

         def _check_summary_is_not_empty(__UpperCAmelCase					:						List[str]		):
               self.assertTrue(hasattr(__UpperCAmelCase	,  "sequential"		)		)
               self.assertTrue(hasattr(__UpperCAmelCase	,  "cumulative"		)		)
               self.assertTrue(hasattr(__UpperCAmelCase	,  "current"		)		)
               self.assertTrue(hasattr(__UpperCAmelCase	,  "total"		)		)

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




# 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_					):
   __UpperCAmelCase			:			List[Any]              =      'facebook/bart-large-mnli'
   __UpperCAmelCase			:			Optional[Any]              =      (
       '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.'
   )
   __UpperCAmelCase			:			Optional[int]              =      'text_classifier'
   __UpperCAmelCase			:			int              =      AutoTokenizer
   __UpperCAmelCase			:			Dict              =      AutoModelForSequenceClassification

   __UpperCAmelCase			:			int              =      ['text', ['text']]
   __UpperCAmelCase			:			Optional[int]              =      ['text']


   def       lowercase_				(self					:						List[Any]		)						->    List[str]:
         """simple docstring"""



         super().setup()
         UpperCAmelCase__								=  self.model.config
         UpperCAmelCase__								=  -1
         for idx, label in config.idalabel.items():
               if label.lower().startswith("entail"		):
                     UpperCAmelCase__								=  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       lowercase_				(self					:						Union[str, Any]	,  __UpperCAmelCase					:						Dict	,  __UpperCAmelCase					:						int		)						->    Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  labels
         return self.pre_processor(
             [text] * len(__UpperCAmelCase		)	,  [f"""This example is {label}""" for label in labels]	,  return_tensors="pt"	,  padding="max_length"	,  )





   def       lowercase_				(self					:						Dict	,  __UpperCAmelCase					:						Tuple		)						->    int:
         """simple docstring"""



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



