๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #320

Adapters

Adapters take a structurally different approach to PEFT than LoRA: instead of modifying existing weight matrices with a low-rank update, insert small, new bottleneck modules directly between a pretrained model's frozen layers, and train only those.

The Adapter Module

\[ \text{Adapter}(\mathbf{x}) = \mathbf{x} + \mathbf{W}_{\text{up}}\,\phi(\mathbf{W}_{\text{down}}\,\mathbf{x}) \]

\(\mathbf{W}_{\text{down}}\) projects the input down to a small bottleneck dimension, \(\phi\) applies a non-linearity, and \(\mathbf{W}_{\text{up}}\) projects back up to the original dimension โ€” structurally a tiny autoencoder-like module (recall Autoencoders), inserted with a residual connection (see Residual Connections) so it starts as a near-identity function.

Where Adapters Get Inserted

Typically, one or two small adapter modules are inserted into each Transformer block โ€” after the attention sublayer, and after the feed-forward sublayer โ€” with every other original weight in the model kept completely frozen. Only these newly added adapter parameters (and often the layer normalization parameters) are trained.

Adapters vs LoRA โ€” Direct Comparison

AdaptersLoRA
MechanismNew modules inserted between layersLow-rank update added alongside existing weight matrices
Adds inference latency?Yes โ€” extra sequential computation at inference timeNo (if merged) โ€” \(\mathbf{B}\mathbf{A}\) can be added directly into \(\mathbf{W}\) after training, adding zero extra inference cost
Structural changeNew layers in the computation graphNo new layers โ€” modifies existing matrix multiplications

This latency difference is a genuinely important practical distinction โ€” it's a large part of why LoRA became more popular than classic adapters for latency-sensitive LLM deployment specifically.

Code

import torch
import torch.nn as nn

class Adapter(nn.Module):
    def __init__(self, d_model, bottleneck_dim=64):
        super().__init__()
        self.down_proj = nn.Linear(d_model, bottleneck_dim)
        self.up_proj = nn.Linear(bottleneck_dim, d_model)
        self.activation = nn.GELU()
        nn.init.zeros_(self.up_proj.weight)   # start as a near-identity function

    def forward(self, x):
        return x + self.up_proj(self.activation(self.down_proj(x)))   # residual: near-zero change initially

adapter = Adapter(d_model=768, bottleneck_dim=64)
trainable_params = sum(p.numel() for p in adapter.parameters())
print(trainable_params)   # a tiny fraction of a full Transformer layer's parameter count

Common Mistakes

  • Assuming adapters add zero inference cost the way merged LoRA weights can โ€” adapters remain genuinely separate layers at inference time, adding real (if typically small) sequential computation.
  • Choosing an adapter bottleneck dimension without considering the same low-rank/capacity tradeoff that applies to LoRA's rank \(r\) โ€” too small a bottleneck limits what the adapter can learn to adjust.

Interview Relevance

Q: "Why might LoRA be preferred over classic adapters for latency-sensitive LLM deployment?" LoRA's low-rank update \(\mathbf{B}\mathbf{A}\) can be merged directly into the original frozen weight matrix after training (\(\mathbf{W}'=\mathbf{W}+\mathbf{B}\mathbf{A}\)), adding zero extra computation at inference time. Adapters remain genuinely separate, sequentially-executed modules inserted into the model's computation graph, adding real (if small) additional inference latency that can't be eliminated the same way.

Practice Question

Why is the adapter module's up-projection weight initialized to zero, similar to LoRA's \(\mathbf{B}\) matrix?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Adapters โ€“ FAQs

Quick answers about learning Adapters in Deep Learning.

This free note from CodingNow 2.0 explains Adapters in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Adapters, is 100% free with no signup required.
With focused practice, most students grasp Adapters in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now