A tourist’s guide to Hugging Face Transformer’s “models” folder

Community Article
Published November 30, 2026

A first look at the part of the library that serves as the “source of truth” for model definitions.


A few months ago I was making contributions to an open source project called TransformerLens.

This is a library which provides mechanistic interpretability researchers a standardised API and naming conventions to get access to and edit the activations of arbitrary layers in a transformer like model. It also has other tools that build upon this functionality.

Screenshot of code from TransformerLens quick start guide
From the quick start section in the repo read me. Give it a try!

In March, the library went from version 2 to version 3, and in doing so changed how support was added for new models. In the new version you added support for an entire architecture by creating an “adapter file.” This is basically a standardised python file where you “fill in the blanks” about the architecture’s quirks as declared in the Hugging Face Transformers library code for that architecture. The HF library being the source of the model code and the HF hub being the source of the specific model weights you choose.

The library introduced a new “model bridge” system which basically takes that adapter file and uses the information in it to create the TransformerLens version of the Transformers model so that you may use the standardised API and names to access any activation across any layer without worrying about how different labs name things.

At the time there was an open issue that listed models that lacked unit tests. I took that opportunity to start onboarding onto the repo and contributed unit tests for the Phi model. Then, I moved on to adding model support, unit and integration tests for Tencent’s Hunyuan Dense V1 and Liquid AI’s LFM2. I then updated LFM2 MoE from an existing MVP level of support to full support. You can find this work here.

Mapping code for Lfm2 from TransformerLens
An example of the “mapping” code for Lfm2 . The keys are the standardised names and the “name” attribute contains the actual names of the various components from the Transformers code. For the full adapter file check: GitHub

To be able to create these adapter files required me to go through the respective model files in the Transformers library.

This article serves as a guide I wish I had when I was going through those model files. I hope it will make it easier for you to look through model code in the Transformer’s library or just give you an appreciation for it.


A 10,000 feet view

The Hugging Face hub hosts millions of model checkpoints. These include official weights from AI labs but also a variety of fine tuned versions uploaded by the community.

The Hugging Face Transformers library gives users an easy way to use these models by providing easy to use APIs so that you can get started very quickly. And the key ingredient that allows you to do this is the model definitions that the Transformers library holds.

Code from Transformers "Try a model" section
From the “Try a model” section of the repo read me. Read the next section on the different types of models available before giving it a try!

You see, to be able to send an input to a model and get a response, at the very least you need two things, a model’s architecture and the specific weights that go into that architecture. Then of course the rest follows (tokenisers for eg).

While there exist millions of model weights on the hub, the Transformers library in it self only officially supports about 500+ model architectures. Why the discrepancy? A single architecture, for example LFM2 by Liquid AI, comes in many different size variants. On top of that, the community regularly uploads fine tuned versions of these models to the hub. All of them are compatible with the same underlying architecture.

To put it more simply, a single architecture code file (Llama for eg.) can work with dozens of official (model variations by Meta) and thousands of unofficial checkpoints (fine tuned versions uploaded by community) as long as key details match.

All of these architectures find their home in the “models” subfolder of the Transformers library on GitHub.

A couple of interesting things to note beyond what I mentioned above. One, the models on the hub are used not just by the Transformers library, but also by a number of other libraries in the ecosystem. TransformerLens is an obvious example, but there are many others (vLLM, llama.cpp etc.). And somewhat symmetrically, you are not necessarily limited to using the models Transformers supports out of the box. You can use other models (custom architecture + weights) from the hub by setting trust_remote_code=True. But you should only do this if you trust the model provider since this could lead to malicious code running on your machine!

The Hub is not just for Hugging Face products and Transformers does not limit itself to just officially supported models.


A 1000 feet view

The models folder itself has about 500+ folders (and growing!) each representing a distinct architecture. Let’s look at the variety on offer!

The first thing to note is that not all these models are the vanilla decoder only transformers that we all know and love. The options available are:

  1. Decoder only: Llama, Gemma etc.
  2. Encoder only: BERT, RoBERTa etc.
  3. Encoder - decoder: T5, BART etc.
  4. State space: Mamba, Mamba 2
  5. Hybrid: Zamba, Jamba etc. (These are real names btw.)
  6. Graph: Graphormer (Appears to have been deprecated)
  7. Decision models: Jev (Newest entrant!)

There are other ways of classifying models as well:

  1. Dense vs MoE: Liquid AI ships LFM2 and LFM2 MoE, both supported by Transformers as distinct architectures. Interesting thing to note is that both of these are hybrid models in that they have both attention and short convolution layers.

  2. Modalities: You have text to text (LLama, Gemma), audio to text (Whisper), image to text (Qwen2-VL) among the types on offer.

For the most part, we will limit ourselves to text to text decoder only models. You may use the base you build here to explore other models.


Up close with a model folder

For our purposes we will start with the Lfm2 folder. Primarily because I have worked with this before but also because it avoids the complexities that multimodality adds. It is better to start simple and then add complexity in an iterative manner.

The first thing you notice when you open the folder is the following four files:

  1. An init file (A short auto generated file, we won’t go through this)
  2. configuration_lfm2.py
  3. modelling_lfm2.py
  4. modular_lfm2.py

Will the real configuration please stand up?

If you look inside configuration_lfm2.py , you will find a config object Lfm2Config with some basic values related to the model architecture defined along with default values.

Why do I say default values? Let’s head over the Hugging Face hub to the LiquidAI collection. We specifically look at the files for LiquidAI/LFM2-2.6B. You will find a config.json file here which contains details about this specific model. It includes specific details missing from the config file in the architecture folder, such as the name and order of layer type (attention or conv). It also includes different values for some variables that were defined in configuration_lfm2.py , such as hidden size being redefined to 2048 instead of the 2560 in configuration_lfm2.py.

This happens because each specific model checkpoint on the hub varies in a number of ways from each other even if they have the same architecture. This could include total number of parameters, number of decoder layers, layer type, vocabulary size, hidden size etc. But they all follow the same architecture. And so each of them comes with a config file specifying these details.

This config, along with configuration_lfm2.py is used by the Transformers library to construct the actual model skeleton which is then populated by the weights that the checkpoint on the hub provides.

Go ahead, look at the various LFM2 models and how their config.json vary!

One thing to note is that the Lfm2 collection on the hub contains both MoE and non MoE variants. You can recognise the MoE variant by its name which includes total and active (A) parameters, for example: LiquidAI/LFM2-24B-A2B. But when you use these models, be sure to use the correct architecture (MoE vs non MoE) with the checkpoint you use!

Another interesting tidbit is that Lfm2.5 models can be used with the Lfm2 architecture. This is because the Lfm2.5 models differ from Lfm2 models not in their architecture but in the pre training and post training recipes. This is also why the checkpoints exist on the hub but no separate folder for Lfm2.5 exists in the Transformers library. The more you know!

Modelling vs Modular

The actual code that runs when you do a forward pass is in the modelling file. However this file is not the one you edit. You make any changes to the modular file and then the modelling file is generated based on that.

The reason for this is to allow reuse of code across models. There are many instances where elements of the model, or significant parts of it, are the same as the closest canonical model. This could be Llama or Gemma for instance. So when adding support for a new model you only add the bits that are unique to that model and borrow the rest from an existing model. It is then that the modelling file is generated.

This is also why modular_lfm2.py is 315 lines where as modelling_lfm2.py is 604 lines. This line count behaviour is a general trend reflective of the logic I mention in the previous paragraph, but I have noticed exceptions to this.

In any case, the file you want to read to understand a model’s architecture is the modelling_xyz.py file for model architecture XYZ.

Looking at the modelling code

Looking at modelling_lfm2.py , you will find a whole bunch of classes and functions. The ones towards the beginning build the various parts of a canonical decoder only transformer. This includes RMS norm, rotary embedding and attention helpers, MLP, the attention module and the short convolution module. Lfm2 uses both attention and short convolutions (alternative to attention) so it does differ from a vanilla decoder in that way.

The same file for Lfm2MoE also has a bunch of stuff for its mixture of experts implementation.

Then you have a series of classes where all these components are assembled together till you get Lfm2ForCausalLM, the actual class that builds our LLM and does text generation when called by the API along with whatever specific checkpoint you chose.

Models may differ in modality, attention variant / alternative or may add other bells and whistles. This file will be the place to see these components implemented.

If you work with model code, it is important to go through the definitions and the way the components are assembled. Sometimes there may be subtle differences from what you expect.

Take for example Lfm2 and Hunyuan Dense V1. Both are decoder only models with Lfm2 using attention and short convolution layers in hybrid. That’s the obvious difference. The less obvious difference is that in Lfm2 the rotary position embeddings are applied after applying the RMS norm to the query and key values. In Hunyuan Dense V1 however, the norm is applied after the rotary position embedding.

Lfm2 attention code
Attention code for Lfm2, norm before rotary embedding
Lfm2 attention code
Attention code for Hunyuan Dense V1, norm after rotary embedding

It is easy to miss such details unless you read the code or the technical reports. Or maybe you luck out and the model card mentions such a technical detail.


Some interesting observations

As you go through the code, you will come across a few more things which are easy to miss. But these details hint at the fact that there is actually a lot more to the library, or even the models folder, than meets the eye. Here I briefly discuss a few of those aspects.

HF Kernels

Look at the list of imports for modelling_lfm2.py:

Kernel imports
Kernel related imports

You will find these imports used as decorators for classes and functions like the RMS norm, attention and its helpers and for the short convolution code. They allow you to use optimised kernels (functions) from Hugging Face’s Kernels Hub. With just a few lines of code, you get easy optimisation and speed gains!

This shows that even after the basic model code has been implemented, there is much work to be done by the maintainers.

A changing codebase

When I was working with the unit test for the Phi model in TransformerLens, I found a strange bit of code that was looking for rotary embedding in two places in the model code. One was at the top level as a property of the model (model.rotary_embed) which is where you would expect it to be based on the modelling code.

But it also had a fallback check, looking to access it in the first layer’s attention module. Strange.

Some research revealed that in previous versions of the Transformers library, the rotary embedding was defined as a property of the attention class, where as in newer versions this was moved to the XyzModel class (Lfm2Model class in our case).

Lfm2Model classs and rotary embedding code
Rotary embedding is defined as a property of the Lfm2Model class.

Ultimately it was decided it was okay to remove this redundant check since TransformerLens pins a much more recent version of Transformers where this transition has already taken place.

This was one of my first bug reports and PRs ever. It’s relatively simple work but I’m proud of it! And I was only able to detect this because I spent a lot of time going over each line in the file trying to understand what it did. An LLM could have caught it, or perhaps not. But delegating that task to an LLM would not have built my understanding of this code and its relation to the changes upstream in the Transformers library! Neither would it have built the confidence and understanding of how OSS works. Code generation might have become easy with LLMs, but there is much more to this craft. It’s one thing to use LLMs as aids and another to delegate the entire thinking process to them.

It is important to not miss the forest for the trees!

The residents of the Transformers library

As a fun exercise, let us look at the commit history of the Inkling model. I remember going through this a few months ago and thought it was a good insight into the people maintaining the library.

I find it the equivalent of talking to the locals when you visit a new city. You can go to the tourist sites, cafes and malls. But you only get a real sense for the city by meeting the people who call it home.

We focus our attention on the following PR: Add Inkling model (#47347). Specifically look at the commit history (commit c375c5e).

If you look at the commit messages, a few things stand out.

Adding support for a new model is an iterative, collaborative process. You see multiple people contributing, going from rough support to increasingly polished versions of the file. That's why you begin with “processor very rough draft” and end with “small fix” and “style”.

What particularly stood out to me is the language. Look at the following commit messages (linked to individual commits):

This suggests to me that the people working on this file were having fun as they went along. And so my recommendation as you go through the Transformers library is to also interact with the maintainers and their previous discussions. They seem like a bunch who is competent and also knows how to have fun!


What I hope you takeaway from this article

Apart from being a way to solidify my own knowledge, my goal with this article is to give you a guided tour of a very important component of the Transformers library. By no means is this an exhaustive coverage. Think of it as someone who visited a town six months before you showing you the main sites over one afternoon. My hope is that this will give you enough of an understanding that you will then explore further with more confidence.

It is my experience that once I gained an understanding of how things were laid out and what was connected to what, it made it much simpler to navigate the modelling code and related files for a given architecture. I hope you benefit from my insights!

I would also like to thank Aritra for reviewing this article and giving feedback that made it more well rounded. Go follow him on X for all things Hugging Face!


Recommended reading / watching

If you liked this article I think you might also enjoy reading / watching the following:

  1. How Hugging Face Transformers is quietly standardising the model ecosystem by Steven Liu (Link): This article uses the example of how railroad gauges were standardised as motivation for how Transformers is doing the same with model definitions for the entire ecosystem.

  2. Modular Transformers (Link): If you want to understand the modular vs modelling part in more detail, the Transformers documentation is where you want to go. In general, the documentation for Transformers and other Hugging Face libraries is pretty good.

  3. Decision Models in Hugging Face (Link) by Aritra, Lysandre, Pedro and Tom: As I hinted earlier, decision type models are the latest entrant to the Transformers library. Read this article to understand more about them!

  4. Beyond the Brrr: Building a Unified Ecosystem for Optimized Kernels by Aritra and Sayak (Link): Watch this video from PyTorch Day India 2026 if you would like to learn about the HF Kernels and Kernel Hub in more detail.

  5. Profiling in PyTorch (Part 1): A Beginner's Guide to torch.profiler (Link) by Aritra, Sayak, Sergio, Rémi and Pedro: So you now know about kernels. While kernels provide the speedup, profiling is how you know your optimisation is actually working! Read this three part series to familiarise yourself with how the PyTorch profiler works.

Community

Sign up or log in to comment