class_docstr: This class implements a classifier with the HuggingFace framework.
code: def _make_model_wrapper(
      self, model: "torch.nn.Module"
  ) -> "torch.nn.Module":
      # Try to import PyTorch and create an internal class that acts like a model wrapper extending torch.nn.Module
      import torch
  
      input_shape = self._input_shape
      input_for_hook = torch.rand(input_shape)
      # self.device may not match the device the raw model was passed into ART.
      # Check if the model is on cuda, if so set the hook input accordingly
      if next(model.parameters()).is_cuda:
          cuda_idx = torch.cuda.current_device()
          input_for_hook = input_for_hook.to(
              torch.device(f"cuda:{cuda_idx}")
          )
  
      input_for_hook = torch.unsqueeze(input_for_hook, dim=0)
  
      if self.processor is not None:
          input_for_hook = self.processor(input_for_hook)
  
      processor = self.processor
      try:
          # Define model wrapping class only if not defined before
          if not hasattr(self, "_model_wrapper"):
  
              class ModelWrapper(torch.nn.Module):
                  """
                  This is a wrapper for the input model.
                  """
  
                  def __init__(self, model: torch.nn.Module):
                      """
                      Initialization by storing the input model.
  
                      :param model: PyTorch model. The forward function of the model must return the logit output.
                      """
                      super().__init__()
                      self._model = model
  
                  # pylint: disable=W0221
                  # disable pylint because of API requirements for function
                  def forward(self, x):
                      """
                      This is where we get outputs from the input model.
  
                      :param x: Input data.
                      :type x: `torch.Tensor`
                      :return: a list of output layers, where the last 2 layers are logit and final outputs.
                      :rtype: `list`
                      """
                      # pylint: disable=W0212
                      # disable pylint because access to _model required
  
                      result = []
  
                      if isinstance(self._model, torch.nn.Module):
                          if processor is not None:
                              x = processor(x)
                          x = self._model.forward(x)
                          result.append(x)
  
                      else:  # pragma: no cover
                          raise TypeError(
                              "The input model must inherit from `nn.Module`."
                          )
  
                      return result
  
                  @property
                  def get_layers(self) -> List[str]:
                      """
                      Return the hidden layers in the model, if applicable.
  
                      :return: The hidden layers in the model, input and output layers excluded.
  
                      .. warning:: `get_layers` tries to infer the internal structure of the model.
                                   This feature comes with no guarantees on the correctness of the result.
                                   The intended order of the layers tries to match their order in the model, but this
                                   is not guaranteed either.
                      """
  
                      result_dict = {}
  
                      modules = []
  
                      # pylint: disable=W0613
                      def forward_hook(
                          input_module, hook_input, hook_output
                      ):
                          logger.info(
                              "input_module is %s with id %i",
                              input_module,
                              id(input_module),
                          )
                          modules.append(id(input_module))
  
                      handles = []
  
                      for name, module in self._model.named_modules():
                          logger.info(
                              "found %s with type %s and id %i and name %s with submods %i ",
                              module,
                              type(module),
                              id(module),
                              name,
                              len(list(module.named_modules())),
                          )
  
                          if (
                              name != ""
                              and len(list(module.named_modules())) == 1
                          ):
                              handles.append(
                                  module.register_forward_hook(
                                      forward_hook
                                  )
                              )
                              result_dict[id(module)] = name
  
                      logger.info(
                          "mapping from id to name is %s", result_dict
                      )
  
                      logger.info(
                          "------ Finished Registering Hooks------"
                      )
                      model(
                          input_for_hook
                      )  # hooks are fired sequentially from model input to the output
  
                      logger.info("------ Finished Fire Hooks------")
  
                      # Remove the hooks
                      for hook in handles:
                          hook.remove()
  
                      logger.info("new result is: ")
                      name_order = []
                      for module in modules:
                          name_order.append(result_dict[module])
  
                      logger.info(name_order)
  
                      return name_order
  
              # Set newly created class as private attribute
              self._model_wrapper = ModelWrapper  # type: ignore
  
          # Use model wrapping class to wrap the PyTorch model received as argument
          return self._model_wrapper(model)
  
      except ImportError:  # pragma: no cover
          raise ImportError(
              "Could not find PyTorch (`torch`) installation."
          ) from ImportError
  
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 2 for calling the HuggingFaceClassifierPyTorch obj's underlying member method code with example usage: obj._make_model_wrapper(model) and returns: self, result, name_order