Search

Search papers and lessons

Zero Convolution

Level 3 · 10 min read

A zero convolution is a 1 by 1 convolution whose weights and bias start at zero, so it adds nothing at first and lets a new branch join a pretrained network without disturbing it.

On this page

  • Colour = topic mix
  • Calculus
  • Architectures
  • Generative models
  • Discrete math
  • Sign in to see your progress

TL;DR

A zero convolution is a 1×11 \times 1 convolution whose weight and bias are both initialised to zero. At the start of training it outputs exactly zero, so whatever it is attached to behaves as if it were not there. Training then moves its weights away from zero, and the new branch gradually starts to matter. It is the connector that lets a new branch join a pretrained network without disturbing the network's behaviour on the first step.

Where you'll see it: the links between the frozen Stable Diffusion U-Net and its trainable control copy in ControlNet, which let edges, poses or depth maps steer image generation.

Intuition

A valve that starts shut

Picture adding a new pipe to a working water system. If you connect it fully open, the new pipe floods the old one with whatever is in it, which here is random noise from an untrained network. If you connect it through a valve that starts shut, nothing changes at first. You then open the valve a little at a time, and only as much flow passes as the new pipe has learned to carry usefully.

A zero convolution is that valve. It sits between the frozen network and the new branch, and at step zero its output is zero. Gradient descent then opens it, because the weights are free to move away from zero when the training signal says they should.

Why not just add a fresh layer

A normal new layer starts with random weights, so its output is random noise. If that noise is added to a pretrained network, the network's good features are corrupted before training can even begin, and the pretrained knowledge is partly destroyed. The point of Fine-Tuning is to keep that knowledge, so the connection has to be neutral at the start. Zero initialisation is the simplest way to get that neutrality.

The architecture below shows where the valves go. The locked block is the original pretrained network, frozen. The trainable copy is a second block that receives the conditioning signal. Two zero convolutions join the two halves.

flowchart LR
  X["Input features x"] --> L["Locked block (frozen)"]
  C["Condition c"] --> Z1["Zero conv (weights 0)"]
  X --> A["Add: x + Z(c)"]
  Z1 --> A
  A --> T["Trainable copy"]
  T --> Z2["Zero conv (weights 0)"]
  L --> S["Sum"]
  Z2 --> S
  S --> Y["Output y_c"]

The math

The 1 by 1 convolution

A Convolution with a k×kk \times k kernel mixes neighbouring pixels. A 1×11 \times 1 convolution has a kernel of size one, so it does not look at neighbours at all. For a feature map with CinC_{\text{in}} channels at each pixel, it computes, at every pixel independently,

yj=∑i=1CinWji xi+bj,j=1,…,Cout.y_j = \sum_{i=1}^{C_{\text{in}}} W_{ji}\, x_i + b_j, \qquad j = 1, \dots, C_{\text{out}}.

Here xix_i is channel ii of the input at that pixel, yjy_j is channel jj of the output, WW is a weight matrix of size Cout×CinC_{\text{out}} \times C_{\text{in}}, and bjb_j is a bias. In words: a 1×11 \times 1 convolution is a learned linear map across the channels, applied the same way at every pixel. The notation Z(x;Θ)\mathcal{Z}(\mathbf{x}; \Theta) used below means this map with parameters Θ=(W,b)\Theta = (W, b).

The zero-initialised version

A zero convolution is the same map with

W=0,b=0at initialisation.W = 0, \qquad b = 0 \quad \text{at initialisation.}

Its output is then yj=0y_j = 0 for every channel and every pixel, whatever the input. This is the whole trick: a valid layer that contributes exactly nothing.

The ControlNet block

Let F(x;Θ)\mathcal{F}(\mathbf{x}; \Theta) be a pretrained block that maps a feature map x\mathbf{x} to y\mathbf{y}. ControlNet freezes Θ\Theta, makes a trainable copy with parameters Θc\Theta_c, and joins them with two zero convolutions, with parameters Θz1\Theta_{z1} and Θz2\Theta_{z2}. The output is

yc=F(x;Θ)+Z ⁣(F ⁣(x+Z(c;Θz1);Θc);Θz2),\mathbf{y}_c = \mathcal{F}(\mathbf{x}; \Theta) + \mathcal{Z}\!\left(\mathcal{F}\!\left(\mathbf{x} + \mathcal{Z}(\mathbf{c}; \Theta_{z1}); \Theta_c\right); \Theta_{z2}\right),

where c\mathbf{c} is the conditioning input, such as an edge map. This is the form given in the paper, with the notation simplified.

Check the first step. Both zero convolutions output zero, so the second term vanishes and yc=F(x;Θ)=y\mathbf{y}_c = \mathcal{F}(\mathbf{x}; \Theta) = \mathbf{y}. The network starts out identical to the pretrained one.

What gets gradients, and when

Training is by gradient descent on a loss LL. The derivative of the output with respect to Θz2\Theta_{z2} is nonzero at step one, because it depends on the trainable copy's activations, which are not zero. So Θz2\Theta_{z2} learns immediately.

The path from the condition to Θz1\Theta_{z1} passes through Θz2\Theta_{z2}. Its weights are zero at step one, so the chain rule gives zero gradient to Θz1\Theta_{z1} at that step. Once Θz2\Theta_{z2} has moved away from zero, the gradient to Θz1\Theta_{z1} becomes nonzero. The paper says it details this calculation in its supplementary material. The consequence is a two-stage start: the output side opens first, then the input side.

Worked examples

Example
One gradient step on a single pixel

Take a 1×11 \times 1 convolution with Cin=2C_{\text{in}} = 2 and Cout=1C_{\text{out}} = 1, so one output channel. Start with W=(0,0)W = (0, 0) and b=0b = 0. The input at this pixel is x=(3,−1)\mathbf{x} = (3, -1). The target output is t=2t = 2, and the loss is L=(y−t)2L = (y - t)^2.

  1. Initial output: y=0⋅3+0⋅(−1)+0=0y = 0 \cdot 3 + 0 \cdot (-1) + 0 = 0. The layer is silent, as designed.
  2. Initial loss: L=(0−2)2=4L = (0 - 2)^2 = 4.
  3. Derivative of the loss with respect to the output: ∂L∂y=2(y−t)=−4\frac{\partial L}{\partial y} = 2(y - t) = -4.
  4. Derivatives with respect to the parameters, using ∂L∂Wi=∂L∂yxi\frac{\partial L}{\partial W_i} = \frac{\partial L}{\partial y} x_i and ∂L∂b=∂L∂y\frac{\partial L}{\partial b} = \frac{\partial L}{\partial y}:
    • ∂L∂W1=−4⋅3=−12\frac{\partial L}{\partial W_1} = -4 \cdot 3 = -12
    • ∂L∂W2=−4⋅(−1)=4\frac{\partial L}{\partial W_2} = -4 \cdot (-1) = 4
    • ∂L∂b=−4\frac{\partial L}{\partial b} = -4
  5. Gradient descent with learning rate 0.050.05 moves each parameter by minus the learning rate times its gradient: W1=0.6W_1 = 0.6, W2=−0.2W_2 = -0.2, b=0.2b = 0.2.
  6. New output: y=0.6⋅3+(−0.2)(−1)+0.2=2.2y = 0.6 \cdot 3 + (-0.2)(-1) + 0.2 = 2.2. New loss: (2.2−2)2=0.04(2.2 - 2)^2 = 0.04.

Interpretation: the layer began silent, and one step was enough to bring its output close to the target. A learning rate of 0.050.05 was chosen so the step is not too large. Had the weights been random, the output would have been off by an unknown amount before any training.

Example
What the extra branch costs

Compare a 1×11 \times 1 convolution with a 3×33 \times 3 one, both mapping 320 channels to 320 channels, as a rough scale for a U-Net layer. A 1×11 \times 1 layer has 320⋅320+320=102,720320 \cdot 320 + 320 = 102{,}720 parameters. A 3×33 \times 3 layer has 9⋅320⋅320+320=921,9209 \cdot 320 \cdot 320 + 320 = 921{,}920. The zero convolutions are cheap, about one ninth of the cost of a 3×33 \times 3 layer of the same width.

The bigger cost is the trainable copy, which duplicates a block. The paper reports that, on a single 40 GB A100, training Stable Diffusion with ControlNet needed about 23% more GPU memory and 34% more time per iteration than training without it. The frozen original needs no gradients, which is why the overhead is not a full doubling.

Interpretation: the connector is almost free, and the copy is where the cost is. The design keeps the copy, because it is what gives the control branch a strong pretrained starting point.

Tradeoffs and failure modes

SituationProblemBetter choice
Connecting a new branch to a pretrained networkRandom weights corrupt the pretrained features on step oneZero-initialise the connector
Zero-initialising a whole deep networkEvery hidden layer has zero gradient, so nothing learnsZero only the connectors; use a standard initialisation such as He (Kaiming) Initialization for the rest
Replacing zero convolutions with ordinary Gaussian-initialised convolutionsThe paper reports the pretrained backbone is damaged, and control quality drops to about the level of its single-layer ControlNet-lite variantKeep the zero initialisation
Very small training setsThe control branch can overfitThe paper reports training did not collapse on 1k images, but more data helps
Relying on the text prompt to carry meaningThe control branch may not learn to read the condition image itselfThe paper randomly replaces 50% of the training prompts with empty strings, so the network learns to recognise the semantics of the condition directly

Failure modes to watch for:

  • Zero everywhere. Zero weights in a chain of layers make all gradients to earlier layers vanish, so the network cannot learn. The zero trick works only because the zero layers sit at the join, where the other side's activations are nonzero.
  • Mismatched architectures. The copy must match the frozen block's shape so the sum in the output is valid. The paper's copy follows the U-Net encoder structure, with 12 encoder blocks and a middle block.
  • Hidden cost of the copy. Memory and time per step rise, as the worked example showed, and the cost scales with the size of the copied blocks.

History

ControlNet, by Lvmin Zhang, Anyi Rao and Maneesh Agrawala in 2023, used the name "zero convolution" for the layer described here. The paper notes that zero-initialised layers have precedents: it points to work by Nichol et al. on scaling the initial weights of convolutions in diffusion models, whose zero_module implementation is the extreme case of zero weights, and to Stability's model cards, which also mention zero weights. It also notes that Gaussian initialisation is often considered less risky than zeros in general, which is why the zero choice needs the specific joining role described above.

Initialisation rules for deep networks, such as the variance scaling in He (Kaiming) Initialization, were developed to keep activations and gradients at a sensible size through many layers. A zero convolution breaks that rule on purpose, but only at a single join, so the rule still governs the rest of the network.

Check yourself

QuestionA 1×11 \times 1 convolution has zero weights and zero bias. What is its output for any input, and why is that useful at the start of training?Show answer

Its output is zero for every input, because each output channel is a sum of zero times the input plus zero. Attached to a pretrained network, it leaves the network's behaviour unchanged on the first step, so the pretrained features are not damaged before training begins.

QuestionCompute the output of a 1×11 \times 1 convolution with W=(2,−1)W = (2, -1), b=0.5b = 0.5, applied to input x=(1,4)\mathbf{x} = (1, 4).Show answer

y=2⋅1+(−1)⋅4+0.5=2−4+0.5=−1.5y = 2 \cdot 1 + (-1) \cdot 4 + 0.5 = 2 - 4 + 0.5 = -1.5.

QuestionIn the ControlNet formula, why does the gradient reach the output zero convolution before the input zero convolution?Show answer

The output zero convolution Θz2\Theta_{z2} multiplies the trainable copy's activations, which are nonzero, so its gradient is nonzero from the first step. The input zero convolution Θz1\Theta_{z1} only affects the output through Θz2\Theta_{z2}, and that path is multiplied by Θz2\Theta_{z2}'s weights, which are zero at the start. So Θz1\Theta_{z1} gets no gradient until Θz2\Theta_{z2} moves away from zero.

QuestionWhy would setting every weight of a deep network to zero, rather than only the connectors, stop it from learning?Show answer

When all weights are zero, every hidden unit computes the same thing and receives the same gradient, and the chain rule multiplies gradients through zero weights, so the gradient reaching early layers is zero. Only the connectors are zeroed in ControlNet, and the rest of the network keeps a standard initialisation, so the hidden layers still receive useful gradients.

QuestionA team wants to add a new control to a frozen pretrained network but finds the first few hundred training steps ruin its outputs. Name the most likely cause and a fix.Show answer

The most likely cause is a connector initialised with random weights, which injects noise into the pretrained features. The fix is to zero-initialise the connecting layers, so the branch starts silent and the network initially reproduces its pretrained output.

Sources