Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft LoRA Hubconf

From Leeroopedia


Template:Implementation meta

Overview

The hubconf.py file provides PyTorch Hub entry points for loading HuggingFace Transformers configurations, tokenizers, and models via torch.hub.load().

Description

This file follows the PyTorch Hub protocol, defining callable entry points that torch.hub.load() can discover and invoke. It exposes six entry points, each wrapping the corresponding HuggingFace Auto class:

Entry Point Auto Class Description
config(*args, **kwargs) AutoConfig.from_pretrained() Load model configuration
tokenizer(*args, **kwargs) AutoTokenizer.from_pretrained() Load tokenizer
model(*args, **kwargs) AutoModel.from_pretrained() Load base model
modelWithLMHead(*args, **kwargs) AutoModelWithLMHead.from_pretrained() Load model with language model head
modelForSequenceClassification(*args, **kwargs) AutoModelForSequenceClassification.from_pretrained() Load model for sequence classification
modelForQuestionAnswering(*args, **kwargs) AutoModelForQuestionAnswering.from_pretrained() Load model for question answering

Each entry point is decorated with @add_start_docstrings to inherit documentation from the corresponding Auto class.

The file also declares a dependencies list specifying the packages required by PyTorch Hub for this repository:

["torch", "numpy", "tokenizers", "filelock", "requests", "tqdm", "regex", "sentencepiece", "sacremoses", "importlib_metadata"]

The file prepends the src/ directory to sys.path to ensure the local transformers module is importable.

Usage

Use this file when:

  • Loading HuggingFace Transformers models via PyTorch Hub without installing the transformers package.
  • Integrating Transformers into workflows that use the PyTorch Hub model registry pattern.
  • Loading models, tokenizers, or configurations in a single line from a GitHub repository.

Code Reference

Source Location

examples/NLU/hubconf.py (141 lines)

Signature

dependencies = ["torch", "numpy", "tokenizers", "filelock", "requests", "tqdm",
                "regex", "sentencepiece", "sacremoses", "importlib_metadata"]

def config(*args, **kwargs) -> AutoConfig: ...
def tokenizer(*args, **kwargs) -> AutoTokenizer: ...
def model(*args, **kwargs) -> AutoModel: ...
def modelWithLMHead(*args, **kwargs) -> AutoModelWithLMHead: ...
def modelForSequenceClassification(*args, **kwargs) -> AutoModelForSequenceClassification: ...
def modelForQuestionAnswering(*args, **kwargs) -> AutoModelForQuestionAnswering: ...

Import / CLI Usage

import torch

# Load via PyTorch Hub
config = torch.hub.load('huggingface/transformers', 'config', 'bert-base-uncased')
tokenizer = torch.hub.load('huggingface/transformers', 'tokenizer', 'bert-base-uncased')
model = torch.hub.load('huggingface/transformers', 'model', 'bert-base-uncased')

I/O Contract

Inputs

Input Type Description
*args positional Passed directly to the corresponding AutoXxx.from_pretrained(). First argument is typically the model name or path (e.g., 'bert-base-uncased')
**kwargs keyword Passed directly to from_pretrained(). Supports output_attentions, from_tf, return_unused_kwargs, etc.

Outputs

Output Type Description
config() AutoConfig A model configuration object
tokenizer() AutoTokenizer A tokenizer instance
model() AutoModel A base transformer model
modelWithLMHead() AutoModelWithLMHead A model with a language modeling head
modelForSequenceClassification() AutoModelForSequenceClassification A model with a classification head
modelForQuestionAnswering() AutoModelForQuestionAnswering A model with a QA head

Usage Examples

import torch

# Load configuration from HuggingFace Hub and cache
config = torch.hub.load('huggingface/transformers', 'config', 'bert-base-uncased')

# Load from a local saved model
config = torch.hub.load('huggingface/transformers', 'config', './test/bert_saved_model/')

# Load with custom kwargs
config = torch.hub.load(
    'huggingface/transformers', 'config', 'bert-base-uncased',
    output_attentions=True, foo=False
)
assert config.output_attentions == True

# Load with unused kwargs returned
config, unused_kwargs = torch.hub.load(
    'huggingface/transformers', 'config', 'bert-base-uncased',
    output_attentions=True, foo=False, return_unused_kwargs=True
)
assert unused_kwargs == {'foo': False}

# Load tokenizer
tokenizer = torch.hub.load('huggingface/transformers', 'tokenizer', 'bert-base-uncased')

# Load model for question answering
qa_model = torch.hub.load(
    'huggingface/transformers', 'modelForQuestionAnswering', 'bert-base-uncased'
)

# Load from TensorFlow checkpoint
from transformers import AutoConfig
config = AutoConfig.from_json_file('./tf_model/bert_tf_model_config.json')
model = torch.hub.load(
    'huggingface/transformers', 'model',
    './tf_model/bert_tf_checkpoint.ckpt.index', from_tf=True, config=config
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment