class_docstr: This class implements a classifier with the HuggingFace framework.
docstr: Initialization of HuggingFaceClassifierPyTorch specifically for the PyTorch-based backend.
  
  :param model: Huggingface model model which returns outputs of type
                ImageClassifierOutput from the transformers library.
                Must have the logits attribute set as output.
  :param loss: The loss function for which to compute gradients for training. The target label must be raw
          categorical, i.e. not converted to one-hot encoding.
  :param input_shape: The shape of one input instance.
  :param optimizer: The optimizer used to train the classifier.
  :param use_amp: Whether to use the automatic mixed precision tool to enable mixed precision training or
                  gradient computation, e.g. with loss gradient computation. When set to True, this option is
                  only triggered if there are GPUs available.
  :param opt_level: Specify a pure or mixed precision optimization level. Used when use_amp is True. Accepted
                      values are `O0`, `O1`, `O2`, and `O3`.
  :param loss_scale: Loss scaling. Used when use_amp is True. If passed as a string, must be a string
                      representing a number, e.g., “1.0”, or the string “dynamic”.
  :param nb_classes: The number of classes of the model.
  :param optimizer: The optimizer used to train the classifier.
  :param channels_first: Set channels first or last. Normally should be set to True for HF models based on
                         a pytorch backend.
  :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
          maximum values allowed for features. If floats are provided, these will be used as the range of all
          features. If arrays are provided, each value will be considered the bound for a feature, thus
          the shape of clip values needs to match the total number of features.
  :param preprocessing_defences: Preprocessing defence(s) to be applied by the classifier.
  :param postprocessing_defences: Postprocessing defence(s) to be applied by the classifier.
  :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
          used for data preprocessing. The first value will be subtracted from the input. The input will then
          be divided by the second one.
  :param device_type: Type of device on which the classifier is run, either `gpu` or `cpu`.
  :param processor: Optional argument. Function which takes in a batch of data and performs
                    the preprocessing relevant to a given foundation model.
                    Must be differentiable for grandient based defences and attacks.
code: def __init__(
      self,
      model: "transformers.PreTrainedModel",
      loss: "torch.nn.modules.loss._Loss",
      input_shape: Tuple[int, ...],
      nb_classes: int,
      optimizer: Optional["torch.optim.Optimizer"] = None,
      use_amp: bool = False,
      opt_level: str = "O1",
      loss_scale: Optional[Union[float, str]] = "dynamic",
      channels_first: bool = True,
      clip_values: Optional["CLIP_VALUES_TYPE"] = None,
      preprocessing_defences: Union[
          "Preprocessor", List["Preprocessor"], None
      ] = None,
      postprocessing_defences: Union[
          "Postprocessor", List["Postprocessor"], None
      ] = None,
      preprocessing: "PREPROCESSING_TYPE" = (0.0, 1.0),
      processor: Optional[Callable] = None,
      device_type: str = "gpu",
  ):
      """
      Initialization of HuggingFaceClassifierPyTorch specifically for the PyTorch-based backend.
  
      :param model: Huggingface model model which returns outputs of type
                    ImageClassifierOutput from the transformers library.
                    Must have the logits attribute set as output.
      :param loss: The loss function for which to compute gradients for training. The target label must be raw
              categorical, i.e. not converted to one-hot encoding.
      :param input_shape: The shape of one input instance.
      :param optimizer: The optimizer used to train the classifier.
      :param use_amp: Whether to use the automatic mixed precision tool to enable mixed precision training or
                      gradient computation, e.g. with loss gradient computation. When set to True, this option is
                      only triggered if there are GPUs available.
      :param opt_level: Specify a pure or mixed precision optimization level. Used when use_amp is True. Accepted
                          values are `O0`, `O1`, `O2`, and `O3`.
      :param loss_scale: Loss scaling. Used when use_amp is True. If passed as a string, must be a string
                          representing a number, e.g., “1.0”, or the string “dynamic”.
      :param nb_classes: The number of classes of the model.
      :param optimizer: The optimizer used to train the classifier.
      :param channels_first: Set channels first or last. Normally should be set to True for HF models based on
                             a pytorch backend.
      :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
              maximum values allowed for features. If floats are provided, these will be used as the range of all
              features. If arrays are provided, each value will be considered the bound for a feature, thus
              the shape of clip values needs to match the total number of features.
      :param preprocessing_defences: Preprocessing defence(s) to be applied by the classifier.
      :param postprocessing_defences: Postprocessing defence(s) to be applied by the classifier.
      :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
              used for data preprocessing. The first value will be subtracted from the input. The input will then
              be divided by the second one.
      :param device_type: Type of device on which the classifier is run, either `gpu` or `cpu`.
      :param processor: Optional argument. Function which takes in a batch of data and performs
                        the preprocessing relevant to a given foundation model.
                        Must be differentiable for grandient based defences and attacks.
      """
      import torch
  
      self.processor = processor
  
      super().__init__(
          model=model,
          loss=loss,
          input_shape=input_shape,
          nb_classes=nb_classes,
          optimizer=optimizer,
          use_amp=use_amp,
          opt_level=opt_level,
          loss_scale=loss_scale,
          channels_first=channels_first,
          clip_values=clip_values,
          preprocessing_defences=preprocessing_defences,
          postprocessing_defences=postprocessing_defences,
          preprocessing=preprocessing,
          device_type=device_type,
      )
  
      import functools
  
      def prefix_function(
          function: Callable, postfunction: Callable
      ) -> Callable[[Any, Any], torch.Tensor]:
          """
          Huggingface returns logit under outputs.logits. To make this compatible with ART we wrap the forward pass
          function of a HF model here, which automatically extracts the logits.
  
          :param function: The first function to run, in our case the forward pass of the model.
          :param postfunction: Second function to run, in this case simply extracts the logits.
          :return: model outputs.
          """
  
          @functools.wraps(function)
          def run(*args, **kwargs) -> torch.Tensor:
              outputs = function(*args, **kwargs)
              return postfunction(outputs)
  
          return run
  
      def get_logits(outputs: "ImageClassifierOutput") -> torch.Tensor:
          """
          Gets the logits attribute from ImageClassifierOutput
  
          :param outputs: outputs of type ImageClassifierOutput from a Huggingface
          :return: model's logit predictions.
          """
          if isinstance(outputs, torch.Tensor):
              return outputs
          return outputs.logits
  
      self.model.forward = prefix_function(
          self.model.forward, get_logits
      )
  
file_path: adversarial-robustness-toolbox/art/estimators/classification/hugging_face.py
imports: import logging
  from typing import List, Optional, Tuple, Union, Dict, Callable, Any, TYPE_CHECKING
  import numpy
  import six
  from art.estimators.classification.pytorch import PyTorchClassifier
label_desc: Use image node_id 1 to create a new HuggingFaceClassifierPyTorch object from inherited base classes: PyTorchClassifier with example: obj = HuggingFaceClassifierPyTorch(model, loss, input_shape, nb_classes, optimizer, use_amp, opt_level, loss_scale, channels_first, clip_values, preprocessing_defences, postprocessing_defences, preprocessing, processor, device_type)