Where to Intervene in a Pretrained Model

CMU DLSys Lecture 22: Customizing Pretrained Models

Deep Learning Systems
Pretrained Models
ControlNet
LoRA
Constrained Decoding
How CLIP conditioning, visual token mapping, ControlNet zero-convolutions, LoRA adapters, and XGrammar masking customize frozen backbones across three forward-pass interfaces.
Author

Chao Ma

Published

November 23, 2026

Training a specialized model from scratch for every downstream domain breaks down once the base model grows to billions of parameters. You cannot afford to collect a full pretraining corpus per task, nor store an independent multi-gigabyte weight checkpoint for every user workflow or output format.

CMU’s Fall 2026 Lecture 22 slides (Customizing Pretrained models, Tianqi Chen and Tim Dettmers; note that the course schedule links slides only and no public video recording) frame customization as an architectural choice: where along a frozen forward pass should adaptation attach? Across image generation and large language models (LLMs), all techniques in the lecture attach at one of three interfaces along a frozen forward pass: input and conditioning embeddings (CLIP conditioning or multimodal visual tokens), frozen backbone weights augmented with parallel branches (ControlNet or LoRA), or output logits (XGrammar and repetition penalties).

Diagram titled Where to Customize a Frozen Pretrained Model, showing a horizontal pretrained forward pass from Inputs to Embeddings, Frozen Backbone with locked weights, Logits, and Output, with three interface groups: CLIP or Visual Tokens entering Embeddings, ControlNet zero-conv branch and LoRA W plus B A adapter attaching to Frozen Backbone, and XGrammar Mask filtering Logits.

All five lecture techniques attach across three forward-pass interfaces without retraining the frozen backbone.

Why keep the backbone frozen

The standard supervised recipe fixes a hypothesis class hθ(x)h_\theta(x), a multi-class cross-entropy loss l(hθ(x),y)=−hy(x)+log∑j=1kexp(hj(x))l(h_\theta(x), y) = -h_y(x) + \log\sum_{j=1}^k \exp(h_j(x)), and a mini-batch update (slides, p. 4):

θ:=θ−αB∑i=1B∇θℓ(hθ(x(i)),y(i)). \theta := \theta - \frac{\alpha}{B}\sum_{i=1}^B \nabla_\theta \ell\!\left(h_\theta(x^{(i)}), y^{(i)}\right).

Collecting separate training data and training an isolated model per domain loses cross-domain knowledge (slides, p. 5). Pretraining reverses that flow: fit a single large backbone on broad data once, then adapt it cleanly to domain constraints (slides, pp. 5, 14). Even before adding new layers, prompts and context shape what an LLM produces without altering weights (slides, p. 15). When prompts alone are insufficient, each customization method trades expressiveness, step-0 stability, and serving cost differently.

Conditioning diffusion through aligned embeddings

In image generation, customization begins by injecting an external signal into the denoising predictor. CLIP (Radford et al.; slides, p. 6) trains a text encoder and an image encoder jointly over batches of NN paired captions and images, producing embeddings T1,…,TNT_1,\dots,T_N and I1,…,INI_1,\dots,I_N in a shared vector space. Maximizing similarity along the matched diagonal pairs Ii⋅TiI_i \cdot T_i relative to off-diagonal mismatches aligns natural language with visual concepts so downstream generators can reuse text embeddings directly.

From Lecture 20, unconditional diffusion sampling integrates the reverse-time variance-preserving SDE (slides, p. 7):

dxt=−[12β(t)xt−β(t)ϵθ(xt,t)σt]dt+β(t)dWt, dx_t = -\left[\frac{1}{2}\beta(t)x_t - \frac{\beta(t)\epsilon_\theta(x_t, t)}{\sigma_t}\right]dt + \sqrt{\beta(t)}\,dW_t,

where generation runs backward from prior noise q(xT)q(x_T) to data q(x0)q(x_0) with dt<0dt < 0, β(t)≥0\beta(t) \ge 0 is the noise schedule, σt>0\sigma_t > 0 is the forward noise scale, and ∇xtlogqt(xt)≈−ϵθ(xt,t)/σt\nabla_{x_t}\log q_t(x_t) \approx -\epsilon_\theta(x_t,t)/\sigma_t supplies the score estimate (dW_t denotes the reverse-time Wiener increment on the slide). Adding a condition cc simply routes an embedding τθ(c)\tau_\theta(c)—such as a CLIP text embedding—into the noise predictor ϵθ(xt,τθ(c),t)\epsilon_\theta(x_t, \tau_\theta(c), t) (slides, p. 8).

Because pixel-space denoising is expensive, Latent Diffusion Models (Rombach et al., CVPR 2022; slides, p. 9; paper) compress pixels xx through a perceptual encoder z=ℰ(x)z = \mathcal{E}(x), run the denoising U-Net ϵθ\epsilon_\theta in latent space, inject conditioning tokens τθ(c)\tau_\theta(c) (text, semantic maps, representations, or images) via cross-attention (Q,K,VQ, K, V), and reconstruct pixels via x̃=𝒟(z)\tilde{x} = \mathcal{D}(z).

Zero-initialized residual branches protect step-0 behavior

Prompt embeddings steer high-level semantics, yet they struggle to enforce pixel-aligned spatial structure like edge maps or body poses, and naive full fine-tuning risks erasing pretrained visual knowledge. Both ControlNet (slides, pp. 10–12) and LoRA (slides, p. 16) attach a parallel residual branch alongside locked weights so that, when the branch is initialized to zero, the step-0 output equals the untouched pretrained model.

Side-by-side schematics of ControlNet (p. 10) and LoRA (p. 16). Left shows ControlNet: input x enters Locked Block in parallel with condition c passing through a 0-conv, summed with x, passed through a Trainable Copy and a second 0-conv, and added to produce y_c equals F(x). Right shows LoRA: input X enters Frozen W in parallel with A (r by d_in) and B (d_out by r, with B initialized to 0 per Hu et al.), added to produce Y equals W X.

Parallel branches leave step-0 forward outputs unchanged; in the author’s backward check, outer ControlNet weights and LoRA B learn first.

ControlNet: locked copy plus zero convolutions

Slide 10 sketches ControlNet’s locked block alongside a trainable copy gated by two zero convolutions and notes: “Initialized as original model (due to zero weight convolution)” (Zhang, Rao, and Agrawala; slides, p. 10). In the notation of the cited paper—not printed symbolically on slide 10—locking Θ\Theta, cloning a trainable copy Θc:=Θ\Theta_c := \Theta, and inserting two 1×11\times 1 zero-initialized convolutions 𝒵(⋅;Θz1)\mathcal{Z}(\cdot; \Theta_{z1}) and 𝒵(⋅;Θz2)\mathcal{Z}(\cdot; \Theta_{z2}) (both weights and biases initialized to 00) yields

yc=ℱ(x;Θ)+𝒵(ℱ(x+𝒵(c;Θz1);Θc);Θz2). y_c = \mathcal{F}(x; \Theta) + \mathcal{Z}\!\left(\mathcal{F}\!\left(x + \mathcal{Z}(c; \Theta_{z1}); \Theta_c\right); \Theta_{z2}\right).

At step 00, both zero convolutions output 00, so yc=ℱ(x;Θ)y_c = \mathcal{F}(x; \Theta) identically. Even with sparse or noisy spatial conditions cc such as Canny edge maps or human pose skeletons (slides, p. 12), the trainable copy starts in an uncorrupted pretrained state. In Stable Diffusion (slides, p. 11), ControlNet duplicates the locked encoder blocks (64×6464\times 64 down to 8×88\times 8) and the 8×88\times 8 middle block, then injects zero-convolved residuals back into both the locked middle block and the U-Net decoder skip paths.

Why doesn’t zero initialization freeze learning forever? Tracing the author’s step-by-step backward derivation (not printed on slide 10), write the copy’s output as h=ℱ(x+𝒵(c;Θz1);Θc)h = \mathcal{F}(x + \mathcal{Z}(c; \Theta_{z1}); \Theta_c) and the outer 1×11\times 1 convolution at batch element nn and spatial position pp as 𝒵(hn,p;Wz2,bz2)=Wz2hn,p+bz2\mathcal{Z}(h_{n,p}; W_{z2}, b_{z2}) = W_{z2} h_{n,p} + b_{z2} with Wz2=0W_{z2}=0 and upstream loss gradient gn,p=∇yc,n,pℒg_{n,p} = \nabla_{y_{c,n,p}}\mathcal{L}. On the very first backward pass:

  • Because Wz2=0W_{z2} = 0, backpropagating through the outer convolution multiplies by Wz2⊤gn,p=0W_{z2}^\top g_{n,p} = 0, blocking gradients from reaching Θc\Theta_c and Θz1\Theta_{z1} at step 00.
  • Meanwhile, the outer zero convolution itself receives a kernel gradient summed over batch and spatial positions:

∂ℒ∂Wz2|Wz2=0=∑n,pgn,phn,p⊤. \left.\frac{\partial \mathcal{L}}{\partial W_{z2}}\right|_{W_{z2}=0} = \sum_{n,p} g_{n,p} h_{n,p}^\top.

Because xx enters the pretrained-initialized copy unchanged (x+0=xx + 0 = x), hn,ph_{n,p} is typically nonzero at step 00; whenever that sum across batch and space is nonzero, step 00 updates Wz2W_{z2} away from 00, unlocking nonzero gradients for Θc\Theta_c and Θz1\Theta_{z1} on step 11. As a constructed scalar check (not a diffusion measurement), set ℱ(u)=u+1\mathcal{F}(u) = u + 1 with x=2.0x = 2.0 (ℱ(2.0)=3.0\mathcal{F}(2.0) = 3.0), c=5.0c = 5.0, wz1=wz2=0w_{z1} = w_{z2} = 0, g=1.0g = 1.0, and SGD step η=0.1\eta = 0.1: at step 00, yc=3.0+0⋅3.0=3.0y_c = 3.0 + 0\cdot 3.0 = 3.0 and ∂ℒ/∂wz2=1.0×3.0=3.0\partial\mathcal{L}/\partial w_{z2} = 1.0 \times 3.0 = 3.0, moving wz2←−0.3w_{z2} \leftarrow -0.3; holding x,c,gx, c, g fixed on step 11, the inner zero convolution immediately receives ∂ℒ/∂wz1=(−0.3)(1)(5.0)=−1.5≠0\partial\mathcal{L}/\partial w_{z1} = (-0.3)(1)(5.0) = -1.5 \neq 0.

LoRA: low-rank weight adaptation

For linear projections in LLMs and attention or diffusion blocks, slide 16 freezes the pretrained weight matrix W∈ℝdout×dinW \in \mathbb{R}^{d_{\text{out}} \times d_{\text{in}}} and attaches two low-rank matrices A∈ℝr×dinA \in \mathbb{R}^{r \times d_{\text{in}}} and B∈ℝdout×rB \in \mathbb{R}^{d_{\text{out}} \times r} with r≪min(din,dout)r \ll \min(d_{\text{in}}, d_{\text{out}}) (LoRA, Hu et al.):

y=Wx+BAx, y = W x + B A x,

using column-vector convention x∈ℝdinx \in \mathbb{R}^{d_{\text{in}}} matching the forward diagram on slide 16 (noting that writing W+BAW + BA sets unit scaling α/r=1\alpha/r = 1 or absorbs α/r\alpha/r into a factor; in Hu et al.’s full convention, not printed on slide 16, Weff=W+(α/r)BAW_{\text{eff}} = W + (\alpha/r)BA with B=0B=0 and Gaussian AA at initialization so BA=0BA=0 at step 00). Just like ControlNet in the author’s step-0 gradient check, B=0B=0 zeroes ∇Aℒ=0\nabla_A\mathcal{L} = 0 at step 00 while ∇Bℒ=(∇yℒ)(Ax)⊤\nabla_B\mathcal{L} = (\nabla_y\mathcal{L})(Ax)^\top can update BB immediately and unlock AA on step 11. At deployment, a single customized model can merge weights offline into WeffW_{\text{eff}} for zero added inference latency, whereas slide 14’s challenge of shipping domain variants motivates a multi-adapter serving design where frozen WW stays shared in GPU memory and small (A,B)(A, B) adapters swap per request.

For a constructed dimension and parameter check on a square projection W∈ℝ4096×4096W \in \mathbb{R}^{4096 \times 4096} with rank r=16r = 16:

  • Frozen base weights: 4096×4096=16,777,2164096 \times 4096 = 16{,}777{,}216 parameters.
  • Trainable LoRA weights: 4096×16+4096×16=131,0724096 \times 16 + 4096 \times 16 = 131{,}072 parameters—0.781%0.781\% (1/1281/128) of WW.

This reduction shrinks per-task checkpoint storage and trainable optimizer states directly, though activation memory during backpropagation still scales with sequence length and batch size; activation checkpointing can reduce the amount retained.

Multi-modality via embedding mapping

What if the model needs a new input modality—such as images inside an autoregressive text LLM—rather than a spatial condition inside diffusion? MiniGPT-4 (Zhu, Chen et al.) and LLaVA (Liu, Li et al.) project encoded image features into "visual tokens" that share the LLM’s embedding dimension and enter alongside text embeddings in the LLM input sequence (slides, p. 17). Once projected into that common token space, visual and text embeddings flow through the same transformer layers without changing the LLM’s text vocabulary.

Enforcing output syntax at logit time with XGrammar

Prompting and LoRA steer probabilities, not hard syntax guarantees. When an application requires strictly valid JSON or a typed schema (class Task(BaseModel): done: bool; name: str; steps: List[int]; slides, p. 19), disallowed tokens must be forced to probability zero after the LLM emits raw logits z∈ℝ|V|z \in \mathbb{R}^{|V|} (slides, p. 18):

z′v={zvif token v matches mask,−∞otherwise. z'_v = \begin{cases} z_v & \text{if token } v \text{ matches mask}, \\ -\infty & \text{otherwise.} \end{cases}

Applying softmax to z′z' (alongside optional repetition penalties, slides, p. 18) guarantees every sampled step respects the constraint without touching a single weight. Yet constrained decoding introduces three systems bottlenecks named on slide 19:

  1. Large vocabulary size: every decoding step scores tens or hundreds of thousands of candidate subword tokens.
  2. Unbounded grammar states: recursive context-free grammars (CFGs)—modeled by pushdown automata (PDA) with a stack, such as slide 20’s nested-array grammar (written literally on the slide without outer grouping around <str> | <array>, which under unambiguous EBNF grouping for its nonempty-array automaton is item ::= <str> | <array> and array ::= '[' <item> (',' <item>)* ']')—create infinitely many stack configurations, making static tables over full stack histories impossible.
  3. CPU–GPU speed mismatch: GPUs generate logits quickly, so computing masks on the CPU cannot keep up with GPU decoding (slides, p. 19).

Flowchart of XGrammar token masking. PDA Stack Top State splits into two branches: over 99 percent Context-Independent tokens resolved via a Precomputed Mask Cache (accepted or rejected), and under 1 percent Context-Dependent tokens checked at runtime against deeper stack transitions. Both paths merge into Mask Applied to Logits for valid grammar tokens.

XGrammar separates >99% stack-top precomputed masks from <1% context-dependent runtime checks before masking logits.

XGrammar (slides, p. 21) mitigates this bottleneck using one structural property of subword vocabularies: most tokens (>99%, as reported on slide 21) are context-independent given only the top state of the PDA stack. At the active string node (Node (1) in rule <str> for partial input ["a; slides, pp. 20–21), ordinary substrings like a or ab" stay accepted regardless of what sits lower on the stack, while tokens like \ or \ab are rejected in the slide’s simplified string grammar (str ::= '"' [^"\]* '"') regardless of deeper history (note that full JSON syntax permits \ to begin escape sequences). Only a tiny set of context-dependent outlier tokens (such as "ab or a"b, which close the string and pop back to deeper stack rules; slides, p. 21) depends on deeper stack entries.

XGrammar therefore splits masking into two stages (slides, p. 21):

  • Compile time (stack-top adaptive cache): Precompute bitmasks for the context-independent tokens per PDA stack-top node.
  • Decode time (tiny outlier check): Retrieve the cached bitmask by indexing the current parsing state, then test only the remaining context-dependent outlier tokens efficiently.

For a constructed arithmetic check (not a universal slide bound or benchmark), suppose an illustrative vocabulary has |V|=128,000|V| = 128{,}000 tokens and >99% are context-independent at a given stack-top node: strictly < 1% leaves fewer than 1,280 outlier tokens (at most 1,279 tokens) to check at runtime, while a dense 11-bit-per-token precomputed mask takes 128,000/8=16,000 bytes=15.625 KiB128{,}000 / 8 = 16{,}000\text{ bytes} = 15.625\text{ KiB} per cached stack-top node before metadata.

Choosing the customization interface

Method Attachment Interface Parameters Trained for Customization Step-0 Behavior Extra Runtime Work Slide Example
Prompting / CLIP condition Conditioning / embeddings None (context or τθ(c)\tau_\theta(c) encoder) Zero backbone changes Condition encoding + cross-attention Text-to-image SDE (pp. 6–8)
Visual token projection Input embedding sequence Vision encoder and/or projector (setup-dependent) Extends input token sequence Visual encoder pass + extra tokens MiniGPT-4, LLaVA (p. 17)
ControlNet Parallel zero-conv blocks Encoder copy + zero-convs Exact original output (yc=ℱy_c = \mathcal{F}) Parallel encoder + middle/decoder skip paths Canny / pose Stable Diffusion (pp. 10–12)
LoRA Parallel low-rank weights Matrices A,BA, B (rank rr) Exact original output (B=0B=0, Hu et al.) Zero if merged; tiny BAxBAx if unmerged LLMs & diffusion (p. 16)
XGrammar masking Post-LLM raw logits None Weights untouched; disallowed tokens receive −∞-\infty Cached bitmask lookup + outlier stack check Typed JSON / CFG decoding (pp. 18–21)

Across all three interfaces, the systems pattern is consistent: touch only the narrow interface needed for the missing capability. Keep the pretrained forward pass intact at step 00 when adapting weights, project new modalities cleanly into shared token space when inputs change, and enforce deterministic syntax at the logits rather than hoping gradient descent never emits an invalid comma.

Sources

  • CMU 10-414/714 Fall 2026 Lecture Schedule and Lecture 22 slides: Customizing Pretrained models (22 pp.) (Tianqi Chen and Tim Dettmers; no public video recording is listed for Lecture 22).
  • Diffusion SDE figures (pp. 7–8): CVPR 2022 Tutorial on Diffusion Models.
  • Cited works from the slides: Radford et al., Learning Transferable Visual Models From Natural Language Supervision (p. 6); Rombach et al., High-Resolution Image Synthesis with Latent Diffusion Models, CVPR 2022 (arXiv:2112.10752, p. 9); Zhang, Rao, and Agrawala, Adding Conditional Control to Text-to-Image Diffusion Models (pp. 10–12); Hu et al., LoRA: Low-Rank Adaptation of Large Language Models (p. 16); Zhu, Chen et al., MiniGPT-4 & Liu, Li et al., Visual Instruction Tuning (LLaVA) (p. 17); and XGrammar (p. 21).