2025, Nov 29 11:00
Recover the Full Class and Forward of a Hugging Face Transformers Model via config.json and Source
Learn how to identify a Hugging Face Transformers model's architecture from config.json and open the exact PyTorch class and forward source for safe fine-tuning.
When you load a pretrained model from Hugging Face with Transformers, you get a ready-to-use nn.Module that you can inspect, fine-tune, and even swap layers in as long as shapes match. What you don’t instantly see is the full class definition with its forward and helper methods—the exact code that was used to build the module. The good news: you can identify the underlying architecture and read its complete implementation.
Minimal example that triggers the question
The typical workflow looks like this: grab a tokenizer and model, use them, maybe print the module to glance at the layers—and then wonder where the forward lives.
from transformers import AutoTokenizer as TokFactory, AutoModel as NetFactory
hub_id = "sentence-transformers/all-MiniLM-L6-v2"
enc = TokFactory.from_pretrained(hub_id)
net = NetFactory.from_pretrained(hub_id)
print(net)
This is enough to experiment, but it doesn’t reveal the full Python source of the model class.
What’s actually missing
Inspecting the printed module gives you a readable tree of sublayers, yet it doesn’t show the forward method and other internals defined in the underlying PyTorch class. To recover that, you need to know which architecture the checkpoint targets and then open the corresponding source file in the Transformers library.
How to recover the class definition
Start at the model’s page on Hugging Face. Open “Files and versions” and look at the configuration file. In the architectures field you’ll see which base class the checkpoint uses. For instance, the configuration indicates that the model “nreimers/MiniLM-L6-H384-uncased” is based on “BertModel”. A concrete example of such a configuration is available here: https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/blob/main/config.json#L4.
Once you know the architecture, you can open the exact class implementation in the Transformers repository. For BertModel, the source is here: https://github.com/huggingface/transformers/blob/v4.52.3/src/transformers/models/bert/modeling_bert.py. This gives you the full nn.Module code, including forward and all supporting logic.
If you prefer a higher-level overview, parameters, and usage notes, the official documentation page is here: https://huggingface.co/docs/transformers/model_doc/bert.
Why this matters
Understanding the exact architecture class allows you to reason about inputs and outputs, trace how tensors flow through the network, and make informed changes that preserve the expected shapes. This is especially useful when you plan to replace layers while keeping compatibility intact. With the architecture identified and the source open, you can align your modifications to the actual forward path rather than guessing from a printed module summary.
Takeaways
If you need the complete PyTorch implementation behind a Hugging Face model, don’t try to “extract a .py” from the checkpoint. Instead, read the model’s config to find its architecture, then open the corresponding source file in the Transformers repository. Use the documentation for additional context on structure and usage. This approach gives you both the code-level view you’re after and a reliable reference for how the model is meant to operate in training and inference pipelines.