class_docstr: A class providing methods for saving and loading prediction head modules from the file system.
  
  Model classes supporting configurable head modules via config files should provide a prediction head dict at
  `model.heads` and a method `add_prediction_head(head_name, config)`.
class_docstr_tok: ["A","class","providing","methods","for","saving","and","loading","prediction","head","modules","from","the","file","system",".","Model","classes","supporting","configurable","head","modules","via","config","files","should","provide","a","prediction","head","dict","at","`","model.heads","`","and","a","method","`","add_prediction_head","(","head_name",",","config",")","`","."]
docstr: Saves a prediction head module into the given directory.
  
  Args:
      save_directory (str): The directory to save the weights in.
      name (str, optional): The prediction head name.
code: def save(self, save_directory: str, name: str = None):
      """
      Saves a prediction head module into the given directory.
  
      Args:
          save_directory (str): The directory to save the weights in.
          name (str, optional): The prediction head name.
      """
  
      if name:
          if hasattr(self.model, "heads"):
              if name not in self.model.heads:
                  if self.error_on_missing:
                      raise ValueError(f"Unknown head_name '{name}'.")
                  else:
                      logger.debug(
                          f"No prediction head with name '{name}' available."
                      )
                      return
          else:
              # we haven't found a prediction head configuration, so we assume there is only one (unnamed) head
              # (e.g. this is the case if we use a 'classic' Hf model with head)
              # -> ignore the name and go on
              name = None
      if not exists(save_directory):
          mkdir(save_directory)
      else:
          assert isdir(
              save_directory
          ), "Saving path should be a directory where the head can be saved."
  
      # if we use a custom head, save it
      if name and hasattr(self.model, "heads"):
          head = self.model.heads[name]
          head_config = head.config
      else:
          head_config = None
  
      # Save the adapter configuration
      config_dict = build_full_config(
          head_config,
          self.model.config,
          name=name,
          model_name=self.model.model_name,
          model_class=self.model.__class__.__name__,
          save_id2label=True,
      )
      # Add number of labels to config if present
      if head_config is None and hasattr(
          self.model.config, "num_labels"
      ):
          config_dict["num_labels"] = self.model.config.num_labels
  
      self.weights_helper.save_weights_config(
          save_directory, config_dict
      )
  
      # Save head weights
  
      filter_func = self.filter_func(name)
      self.weights_helper.save_weights(save_directory, filter_func)
  
imports: import json
  import logging
  from abc import ABC, abstractmethod
  from os import mkdir
  from os.path import exists, isdir, isfile, join
  from typing import Callable, Mapping, Sequence, Tuple
  import torch
  from .configuration import AdapterConfig, build_full_config
  from .head_utils import STATIC_TO_FLEX_HEAD_MAP, get_head_config_and_rename_list
  from .utils import ACTIVATION_RENAME, ADAPTERFUSION_CONFIG_NAME, ADAPTERFUSION_WEIGHTS_NAME, CONFIG_NAME, HEAD_CONFIG_NAME, HEAD_WEIGHTS_NAME, WEIGHTS_NAME, AdapterType, resolve_adapter_path
label_desc: Use image node_id 4 for calling the PredictionHeadLoader obj's underlying member method code with example usage: obj.save(save_directory, name) without return types