class_docstr: This lobe provides an interface for integrating any HuggingFace transformer model within SpeechBrain.
  
  We use AutoClasses for loading any model from the hub and its necessary components.
  For example, we build Wav2Vec2 class which inherits HFTransformersInterface for working with HuggingFace's wav2vec models.
  While Wav2Vec2 can enjoy some already built features like modeling loading, pretrained weights loading, all weights freezing,
  feature_extractor loading, etc.
  Users are expected to override the essential forward() function to fit their specific needs.
  Depending on the HuggingFace transformer model in question, one can also modify the state_dict by overwriting the _modify_state_dict() method,
  or adapting their config by modifying override_config() method, etc.
  See:
  https://huggingface.co/docs/transformers/model_doc/auto
  https://huggingface.co/docs/transformers/autoclass_tutorial
  
  Arguments
  ---------
  source : str
      HuggingFace hub name: e.g "facebook/wav2vec2-large-lv60"
  save_path : str
      save directory of the downloaded model.
  for_pretraining: bool (default: False)
      If True, build the model for pretraining
  with_lm_head : bool (default: False)
      If True, build the model with lm_head
  with_casual_lm : bool (default: False)
      If True, build casual lm  model
  seq2seqlm : bool (default: False)
      If True, build a sequence-to-sequence model with lm_head
  quantization_config : dict (default: None)
      Quantization config, extremely useful for deadling with LLM
  freeze : bool (default: True)
      If True, the model is frozen. If False, the model will be trained
      alongside with the rest of the pipeline.
  cache_dir : str or Path (default: None)
      Location of HuggingFace cache for storing pre-trained models, to which symlinks are created.
  
  Example
  -------
  >>> model_hub = "facebook/wav2vec2-base-960h"
  >>> save_path = "tmp"
  >>> model = HFTransformersInterface(model_hub, save_path=save_path)
class_docstr_tok: ["This","lobe","provides","an","interface","for","integrating","any","HuggingFace","transformer","model","within","SpeechBrain",".","We","use","AutoClasses","for","loading","any","model","from","the","hub","and","its","necessary","components",".","For","example",",","we","build","Wav2Vec2","class","which","inherits","HFTransformersInterface","for","working","with","HuggingFace","'s","wav2vec","models",".","While","Wav2Vec2","can","enjoy","some","already","built","features","like","modeling","loading",",","pretrained","weights","loading",",","all","weights","freezing",",","feature_extractor","loading",",","etc",".","Users","are","expected","to","override","the","essential","forward","(",")","function","to","fit","their","specific","needs",".","Depending","on","the","HuggingFace","transformer","model","in","question",",","one","can","also","modify","the","state_dict","by","overwriting","the","_modify_state_dict","(",")","method",",","or","adapting","their","config","by","modifying","override_config","(",")","method",",","etc",".","See",":","https",":","\/\/huggingface.co\/docs\/transformers\/model_doc\/auto","https",":","\/\/huggingface.co\/docs\/transformers\/autoclass_tutorial","Arguments","--","--","--","--","-","source",":","str","HuggingFace","hub","name",":","e.g","``","facebook\/wav2vec2-large-lv60","''","save_path",":","str","save","directory","of","the","downloaded","model",".","for_pretraining",":","bool","(","default",":","False",")","If","True",",","build","the","model","for","pretraining","with_lm_head",":","bool","(","default",":","False",")","If","True",",","build","the","model","with","lm_head","with_casual_lm",":","bool","(","default",":","False",")","If","True",",","build","casual","lm","model","seq2seqlm",":","bool","(","default",":","False",")","If","True",",","build","a","sequence-to-sequence","model","with","lm_head","quantization_config",":","dict","(","default",":","None",")","Quantization","config",",","extremely","useful","for","deadling","with","LLM","freeze",":","bool","(","default",":","True",")","If","True",",","the","model","is","frozen",".","If","False",",","the","model","will","be","trained","alongside","with","the","rest","of","the","pipeline",".","cache_dir",":","str","or","Path","(","default",":","None",")","Location","of","HuggingFace","cache","for","storing","pre-trained","models",",","to","which","symlinks","are","created",".","Example","--","--","--","-",">",">",">","model_hub","=","``","facebook\/wav2vec2-base-960h","''",">",">",">","save_path","=","``","tmp","''",">",">",">","model","=","HFTransformersInterface","(","model_hub",",","save_path=save_path",")"]
docstr: Checks if the pretrained model has been trained with SpeechBrain and
  is hosted locally or on a HuggingFace hub.
  Called as static function in HFTransformersInterface._from_pretrained.
  
  Arguments
  ---------
  path : str
      Used as "source"; local path or HuggingFace hub name: e.g "facebook/wav2vec2-large-lv60"
  save_path : str
      norm_output (dir) of the downloaded model.
  
  Returns
  -------
  is_sb : bool
      Whether/not the model is deserializable w/ SpeechBrain or not (then, model conversion is needed).
  checkpoint_filename : str
      as of HuggingFace documentation: file name relative to the repo root (guaranteed to be here).
  is_local : bool
      Whether/not the model is hosted locally or on a HuggingFace hub.
  
  Raises
  ------
  ValueError
      If file is not found
docstr_tok: ["Checks","if","the","pretrained","model","has","been","trained","with","SpeechBrain","and","is","hosted","locally","or","on","a","HuggingFace","hub",".","Called","as","static","function","in","HFTransformersInterface._from_pretrained",".","Arguments","--","--","--","--","-","path",":","str","Used","as","``","source","''",";","local","path","or","HuggingFace","hub","name",":","e.g","``","facebook\/wav2vec2-large-lv60","''","save_path",":","str","norm_output","(","dir",")","of","the","downloaded","model",".","Returns","--","--","--","-","is_sb",":","bool","Whether\/not","the","model","is","deserializable","w\/","SpeechBrain","or","not","(","then",",","model","conversion","is","needed",")",".","checkpoint_filename",":","str","as","of","HuggingFace","documentation",":","file","name","relative","to","the","repo","root","(","guaranteed","to","be","here",")",".","is_local",":","bool","Whether\/not","the","model","is","hosted","locally","or","on","a","HuggingFace","hub",".","Raises","--","--","--","ValueError","If","file","is","not","found"]
code: def _check_model_source(self, path, save_path):
      """Checks if the pretrained model has been trained with SpeechBrain and
      is hosted locally or on a HuggingFace hub.
      Called as static function in HFTransformersInterface._from_pretrained.
  
      Arguments
      ---------
      path : str
          Used as "source"; local path or HuggingFace hub name: e.g "facebook/wav2vec2-large-lv60"
      save_path : str
          norm_output (dir) of the downloaded model.
  
      Returns
      -------
      is_sb : bool
          Whether/not the model is deserializable w/ SpeechBrain or not (then, model conversion is needed).
      checkpoint_filename : str
          as of HuggingFace documentation: file name relative to the repo root (guaranteed to be here).
      is_local : bool
          Whether/not the model is hosted locally or on a HuggingFace hub.
  
      Raises
      ------
      ValueError
          If file is not found
      """
      checkpoint_filename = ""
      source = pathlib.Path(path)
      is_local = True
  
      # If path is a huggingface hub.
      if not source.exists():
          is_local = False
  
      # Check if source is downloaded already
      sink = pathlib.Path(
          save_path
          + "/models--"
          + path.replace("/", "--")
          + "/snapshots"
      )
      if sink.exists():
          sink = (
              sink / os.listdir(str(sink))[0]
          )  # there's a hash-id subfolder
          if any(
              File.endswith(".bin") or File.endswith(".ckpt")
              for File in os.listdir(str(sink))
          ):
              is_local = True
              local_path = str(sink)
          else:
              local_path = path
      else:
          local_path = path
  
      if is_local:
          # Test for HuggingFace model
          if any(
              File.endswith(".bin") for File in os.listdir(local_path)
          ):
              is_sb = False
              return is_sb, checkpoint_filename, is_local
  
          # Test for SpeechBrain model and get the filename.
          for File in os.listdir(local_path):
              if File.endswith(".ckpt"):
                  checkpoint_filename = os.path.join(path, File)
                  is_sb = True
                  return is_sb, checkpoint_filename, is_local
      else:
          files = model_info(
              path
          ).siblings  # get the list of files of the Hub
  
          # Test if it's an HuggingFace model or a SB one
          for File in files:
              if File.rfilename.endswith(".ckpt"):
                  checkpoint_filename = File.rfilename
                  is_sb = True
                  return is_sb, checkpoint_filename, is_local
  
          for File in files:
              if File.rfilename.endswith(".bin"):
                  checkpoint_filename = File.rfilename
                  is_sb = False
                  return is_sb, checkpoint_filename, is_local
  
      err_msg = f"{path} does not contain a .bin or .ckpt checkpoint !"
      raise FileNotFoundError(err_msg)
  
imports: import os
  import torch
  import logging
  import pathlib
  from torch import nn
  from huggingface_hub import model_info
  from speechbrain.utils.fetching import fetch
  from speechbrain.dataio.dataio import length_to_mask
  from transformers import AutoConfig, AutoTokenizer, AutoFeatureExtractor, AutoModelForPreTraining, AutoModel, AutoModelWithLMHead, AutoModelForSeq2SeqLM, AutoModelForCausalLM
label_desc: Use image node_id 3 for calling the HFTransformersInterface obj's underlying member method code with example usage: obj._check_model_source(path, save_path) and returns: is_sb,  checkpoint_filename,  is_local, is_sb,  checkpoint_filename,  is_local, is_sb,  checkpoint_filename,  is_local, is_sb,  checkpoint_filename,  is_local