Corr2D
Cross-correlation with the loops on display, straight from the textbook
- input_tensor
- kernel
- output
CdlCorr2d is the cross-correlation operator from the Dive into Deep Learning textbook, implemented the textbook way: explicit nested loops. It slides a 2-D kernel over a 2-D input, and at every position computes the sum of the element-wise product between the window and the kernel. That's literally the whole operation, one Y[i, j] = (input[i:i+h, j:j+w] * kernel).sum() per output pixel. If you've ever wondered what a convolution layer is doing under the hood, this node shows you - because there's no hood.
How it works
It takes a 2-D input (H, W) and a 2-D kernel (h, w), allocates an output of (H − h + 1, W − w + 1), and fills it in the most explicit way possible: two loops, one per output row and column, each summing the element-wise product of the kernel with the corresponding input window. No F.conv2d, no stride, no padding, no batch or channel dimensions - the pure definition.
Two things worth knowing about the "cross-correlation" part: technically this is cross-correlation, not convolution - a true convolution flips the kernel before sliding. PyTorch's conv2d is also cross-correlation under the hood (the flip is absorbed into learned weights), so in practice "cross-correlation" and "convolution" mean the same thing in deep learning, and the textbook deliberately uses the honest name. And because this is the raw textbook version, it only handles single 2-D tensors - no batches, no channels.
Inputs and output
input_tensor- a 2-DcdlTensor,(H, W).kernel- a 2-DcdlTensor,(h, w).
The output is output, a 2-D cdlTensor of (H − h + 1, W − w + 1).
The honest comparison with CdlConv2d
ComfyDL ships both this node and CdlConv2d, and they compute the same thing with different philosophies. CdlConv2d wraps the vectorized F.conv2d - fast, supports stride/padding, auto-expands batch and channels - and it's the one you'd use for real work. CdlCorr2d is the learning node: slower (Python loops), 2-D only, no options, and that's the point. It exists so you can see the mechanism, verify a result by hand, and then fully trust the fast version. Grab a 3×3 kernel and a 5×5 input, predict the output shape, and check - that exercise is worth more than any explanation.
Installing it
It ships with ComfyDL, one install for all 106 nodes:
cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL
pip install -r ./ComfyDL/requirements.txt
Restart ComfyUI, or search "ComfyDL" in ComfyUI Manager.
Gotchas
- The kernel must be smaller than the input on both axes - a kernel that doesn't fit gives you a negative output dimension and an error.
- It runs on CPU (the output tensor is created on default CPU), and the nested loops are slow on anything bigger than toy size. Don't try to run this on a real image; that's CdlConv2d's job.
- Because it's 2-D-only, feeding it an
[N, C, H, W]tensor from a model won't work as-is - you'd need to pull out one channel, which kind of defeats the purpose. It's a classroom node, sized for classroom data.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| input_tensor | TENSOR | — | |
| kernel | TENSOR | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| output | TENSOR | — |