DownBlock<T>
Models & Types · AiDotNet.Diffusion.VAE
Downsampling block for VAE encoder with multiple ResBlocks and strided convolution.
Inheritance: LayerBase<T> → DownBlock<T>
Implements: IShapeContract
For Beginners
A DownBlock is like a compression stage in an encoder.
What it does:
- Processes the input through multiple residual blocks (learning features)
- Reduces spatial size by half using strided convolution (compression)
Example: 64x64 input -> 32x32 output (spatial dimensions halved)
Why use strided convolution instead of pooling?
- Strided conv is learnable (the network decides how to downsample)
- Max/Avg pooling has fixed behavior that may discard useful information
- Strided conv is the standard in modern generative models like VAEs and diffusion
Structure:
input [B, C_in, H, W]
│
├─→ ResBlock → ResBlock → ... (numLayers blocks)
│
↓
[B, C_out, H, W]
│
├─→ Conv3x3 (stride=2) ─→ downsample
│
↓
output [B, C_out, H/2, W/2]
How It Works
This implements a downsampling block following the Stable Diffusion VAE architecture:
- Multiple VAEResBlocks to process features at the current resolution
- Strided convolution (stride=2) to reduce spatial dimensions by half
Constructors
| Constructor | Summary |
|---|---|
DownBlock(int inChannels, int outChannels, int numLayers, int numGroups, int inputSpatialSize, bool hasDownsample) |
Initializes a new instance of the DownBlock class. |
Properties
| Property | Summary |
|---|---|
bool HasDownsample { get; } |
Gets whether this block performs downsampling. |
bool SupportsTraining { get; } |
|
int InputChannels { get; } |
Gets the number of input channels. |
int NumLayers { get; } |
Gets the number of residual blocks. |
int OutputChannels { get; } |
Gets the number of output channels. |
Methods
| Method | Summary |
|---|---|
IReadOnlyList<ILayer<T>> GetSubLayers() |
|
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank) |
|
IReadOnlyList<VAEResBlock<T>> GetResBlocks() |
Gets the residual blocks for external access (e.g., for skip connections in UNet). |
void ResetState() |
Resets the internal state of the block. |
void UpdateParameters(T learningRate) |
Updates all learnable parameters using gradient descent. |