Back to paper library

DCNv4

Unbounded Weights

Original paper

Efficient Deformable ConvNets: Rethinking Dynamic and Sparse Operator for Vision Applications

Paper authors

Yuwen Xiong · Zhiqi Li · Yuntao Chen …

Show all 13 authors

Yuwen Xiong · Zhiqi Li · Yuntao Chen · Feng Wang · Xizhou Zhu · Jiapeng Luo · Wenhai Wang · Tong Lu · Hongsheng Li · Yu Qiao · Lewei Lu · Jie Zhou · Jifeng Dai

Publication
CVPR 2024Conference
First submitted to arXiv
arXiv:2401.06197
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{classiccnnsdcnv42024,
  title = {{Efficient Deformable ConvNets: Rethinking Dynamic and Sparse Operator for Vision Applications}},
  author = {Yuwen Xiong and Zhiqi Li and Yuntao Chen and Feng Wang and Xizhou Zhu and Jiapeng Luo and Wenhai Wang and Tong Lu and Hongsheng Li and Yu Qiao and Lewei Lu and Jie Zhou and Jifeng Dai},
  year = {2024},
  eprint = {2401.06197},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2401.06197}
}
On this page

In InternImage's ablations, replacing softmax with sigmoid for DCNv3's modulation weights drops ImageNet accuracy from 83.5% to 65.7%. Softmax looks indispensable to DCNv3.

A little over a year later, largely the same group introduces DCNv4. Its first change is to remove softmax, and it converges faster.

First author Yuwen Xiong coauthored the original deformable convolution in 2017; corresponding author Jifeng Dai was that paper's first author. This team includes the University of Toronto, Shanghai AI Laboratory, Tsinghua University, SenseTime, and the Chinese University of Hong Kong. Beyond softmax, the other half of the paper asks why deformable convolution runs slower than 7×7 depthwise convolution when it samples only 9 points.

Figure 1 from the paper: (a) a bar chart with input tensor sizes 56×56×128, 28×28×256, and 14×14×512 on the horizontal axis, and execution time relative to DCNv3 = 1 on the vertical axis. Each group has bars for FlashAttention, Window Attention, DWConv, DCNv3, and DCNv4. DCNv4 is shortest in the first two groups and close to FlashAttention in the third; the 56×56×128 group is marked 3.76× faster than DCNv3. (b) a line chart with training iterations from 0 to 15K horizontally and ImageNet top-1 accuracy vertically. DCNv4 leads early, DWConv and Dense Attention lie in the middle, and DCNv3 starts lowest.
Figure 1 from Xiong et al., taken from PDF page 1. The original caption is omitted.

The left plot shows speed, with DCNv3 normalized to 1 and shorter bars indicating faster execution. DCNv4 is fastest at the first two sizes and close to FlashAttention at 14×14×512. The right plot swaps operators in the same network architecture and compares early training accuracy. DCNv4 leads throughout, while DCNv3 starts at the bottom.

Defining the Problem

Why Is Sampling Just 9 Points Still Slow?

First, a recap of DCNv3. At location p0p_0, the output for channel group gg is

yg(p0)=∑k=1Kmgk xg ⁣(p0+pk+Δpgk),y_g(p_0)=\sum_{k=1}^{K} m_{gk}\, x_g\!\left(p_0+p_k+\Delta p_{gk}\right),

K=9K=9 is the number of sampling points on a 3×3 grid, Δpgk\Delta p_{gk} is the learned offset, and mgkm_{gk} is the modulation weight, normalized by softmax along KK in DCNv3. Both offsets and weights are computed from the input and vary by location.

A sparse operator looking at only 9 points ought to be faster than 7×7 depthwise convolution or dense attention. In practice, memory access gets in the way.

The authors analyze this with the roofline model. For input and output of size H×W×CH\times W\times C, DCNv3 takes 36HWC36HWC FLOPs: each of 9 sampling points needs bilinear interpolation involving 4 pixels. Ideally, each value is read from memory once, for roughly 3.7HWC3.7HWC memory accesses.

With parallel computation, however, many threads read simultaneously, and cache hits are not guaranteed. In the worst case, each output location rereads 36 interpolation pixels, 27 offsets and weights—2 offsets and 1 weight for each of 9 points—and writes 1 result. Memory access grows to 64HWC64HWC, 17 times the ideal case.

The ratio of computation to memory access is only 0.6 to 9.7, on the memory-bandwidth-bound side of the roofline model. More GPU arithmetic capacity cannot help much.

Softmax Was Borrowed

The other issue is softmax. DCNv3 borrows it from attention, but attention has its own reason for needing it.

Scaled dot-product attention is softmax(QK⊤/d)V\mathrm{softmax}(QK^\top/\sqrt{d})V. Without softmax, one can compute K⊤VK^\top V first, obtaining a d×dd\times d matrix. Every query in the same window then multiplies the same matrix, reducing attention to a linear projection.

Convolution and DCNv3 avoid this issue. Each location already has its own sampling window with its own values. There is no “key” concept requiring this normalization.

To test this, the authors instead add softmax to ConvNeXt's 7×7 depthwise convolution weights:

Table: from the paper's Table 1, ImageNet-1K top-1.
ModelEpoch 5Epoch 10Epoch 20Epoch 50Epoch 300
ConvNeXt29.9%53.5%66.1%74.8%83.8%
ConvNeXt + softmax8.5%25.3%51.1%69.1%81.6%

Softmax clearly slows convergence and costs 2.2 percentage points by the end of training.

Softmax bounds each weight between 0 and 1 and makes their sum 1, restricting the output to a weighted average of sampled values. A weighted average stays between their minimum and maximum, so it cannot perform subtraction—the very operation used by basic filters such as edge detectors. Take the “right minus left” filter (−1,0,1)(-1, 0, 1): unbounded weights can learn it directly, while softmax cannot produce negative weights, so its best option puts all the weight on the right point, leaving about half the error.

Figure 2 from the paper: four panels compare spatial aggregation for query pixels at different locations in one channel. The upper-left legend uses a star for the query pixel and squares for response pixels; the color bar has two ranges, (0, 1) and (−∞, +∞). (a) Attention: Shared/Fixed windows, Dynamic weights, Bounded range. (b) DCNv3: Dedicated/Adaptive windows, Dynamic weights, Bounded range. (c) Convolution: Dedicated/Fixed windows, Static weights, Unbounded range. (d) DCNv4: Dedicated/Adaptive windows, Dynamic weights, Unbounded range.
Figure 2 from Xiong et al., taken from PDF page 3. The original caption is omitted.

This figure compares the four operators. Attention has varying but bounded weights. Convolution has unbounded but fixed weights and fixed window shapes. DCNv4 targets the lower-right panel: a deformable window dedicated to each location, with input-dependent, unbounded weights.

Solving the Problem

1. Remove Softmax

The formula stays the same as DCNv3, except that mgkm_{gk} bypasses softmax. A linear layer predicts it directly from the input, allowing any real value.

Removing softmax has a side benefit. Offsets and weights originally use two linear layers; now these can merge into one, saving a kernel launch and synchronization. The authors also simplify the offset subnetwork. It originally uses depthwise 3×3, LayerNorm, GELU, and a linear layer; removing the middle LayerNorm and GELU returns it to a simple separable convolution like Xception's.

2. Move Less Data on the GPU

Figure 3 from the paper: two diagrams compare thread assignments in DCNv3 and DCNv4. Each has a set of offsets and weights in GPU memory on the left and a cube-shaped H, W, C output tensor on the right. (a) DCNv3 uses Thread-1 through Thread-6, one per channel, each making a memory-read request. (b) DCNv4 uses only Thread-1 through Thread-3, with each handling two channels in the same group, halving read requests; the upper-right label marks a 2× reduction in memory access.
Figure 3 from Xiong et al., taken from PDF page 4. The original caption is omitted.

DCNv3's original CUDA implementation launches H×W×CH\times W\times C threads, one per channel at each location. Yet the D=C/GD=C/G channels in a group share the same offsets and weights at a given location. All DD threads read the same data separately and separately compute the bilinear interpolation coefficients.

DCNv4 lets each thread handle several channels within one group, reading offsets and weights once and computing interpolation coefficients once. The illustration uses two channels per thread for clarity; the actual implementation uses 8.

Interestingly, this change alone makes it slower. Fewer threads reduce parallelism, and each thread has 8 times the work. The real speedup comes next. Channels are contiguous in memory, in channel-last layout, so one vectorized instruction can read adjacent channels together—for example, four 32-bit floats in one 128-bit read. Writes work the same way.

The authors measure each step separately. The input is 56×56×128, batch size 64, with 4 groups of 32 channels. Times are in milliseconds:

Table: from the paper's Table 10. A100.
ImplementationWhole moduleCUDA kernel
Original DCNv33.281.45
Simplified module design2.121.45
One thread per 8 channels2.201.53
Reuse interpolation coefficients2.181.51
Vectorized reads and writes1.280.606
Half precision0.8730.404

Simplifying the module affects only work outside the CUDA kernel, leaving kernel time unchanged. The next two steps make the kernel slightly slower. Vectorized reads and writes then cut its time by 60%. Half precision uses 2 bytes per value, moving twice as many values at the same bandwidth. The authors say that half precision brings no speedup to the original DCNv3; only the new implementation benefits.

Discussion

The Operator Itself

First, compare operators at input size 56×56×128, measured in FP32 and FP16:

Table: from the first input-size column of the paper's Table 2. FlashAttention-2 has no FP32 version.
OperatorFP32 (ms)FP16 (ms)
Global attention (PyTorch)30.819.3
FlashAttention-2—2.46
7×7 window attention4.051.46
7×7 DWConv (PyTorch)2.021.98
7×7 DWConv (cuDNN)0.9810.438
DCNv31.451.52
DCNv40.6060.404

DCNv4 is faster even than cuDNN's 7×7 depthwise convolution, cutting FP32 execution time by nearly 40%. The DCNv3 row also confirms the earlier point: switching to FP16 makes it slightly slower.

FlashInternImage

Replace DCNv3 in InternImage with DCNv4 and leave everything else unchanged; the authors call the result FlashInternImage. Throughput is measured in both FP32 and FP16:

Table: selected rows from the paper's Table 4.
Modeltop-1Throughput (FP32 / FP16, images/s)
ConvNeXt-T82.1%2485 / 4305
InternImage-T83.5%1409 / 1746
FlashInternImage-T83.6%2316 / 3154
ConvNeXt-S83.1%1645 / 2538
InternImage-S84.2%1044 / 1321
FlashInternImage-S84.4%1625 / 2396
ConvNeXt-B83.8%1241 / 1888
InternImage-B84.9%779 / 1030
FlashInternImage-B84.9%1174 / 1816

Against InternImage, T is 64% to 80% faster, with slightly better accuracy.

The ConvNeXt comparison needs a closer look. In FP16, FlashInternImage-T still runs at only a little over 70% of ConvNeXt-T's speed. S and B are also slightly slower, at roughly 95%. A fair description is that DCNv4 brings InternImage close to ConvNeXt speed, with 1.1 to 1.5 percentage points higher accuracy.

Downstream tasks follow the same trend. On COCO with Mask R-CNN trained for 12 epochs, FlashInternImage-T gets 48.0 box AP against InternImage-T's 47.2, and FPS rises from 54 / 69 to 72 / 102. On ADE20K with UperNet, single-scale mIoU rises from 47.9 to 49.3.

Swapping It into Other Networks

The authors also use DCNv4 as a drop-in component in other architectures, keeping the other settings and hyperparameters unchanged:

  • ConvNeXt-B: replacing 7×7 depthwise convolution with DCNv4 raises ImageNet from 83.8% to 84.0%, with 20% more throughput in FP32 and 33% in FP16.
  • ViT-B: replacing attention with DCNv4 raises 81.8% to 81.9%, with 24% and 17% more throughput than the original using FlashAttention-2.
  • Latent diffusion U-Net: replacing attention modules and standard 3×3 convolutions lowers FID from 2.94 to 2.44 on ImageNet 256×256 class-conditional generation, with parameters dropping from 860M to 566M.

These architectures were tuned for their own operators, yet swapping DCNv4 straight in preserves or slightly improves accuracy while increasing speed. For generation, the authors also say the architecture and hyperparameters may need further tuning for DCNv4.

The Cost

DCNv4 remains a custom CUDA operator. Many issues in the official repository concern build failures, missing modules, lack of CPU support, and difficulties exporting to ONNX or TensorRT. The repository's last push was in May 2024.

Some users also report results differing from the paper. One finds training slower than DCNv3 on their segmentation task. Another uses the official speed script on a 3090 and measures slower results than reported. Several users integrating DCNv4 into their own models, such as YOLO, report loss failing to decrease or NaNs. These reports lack full configurations and have no official replies, so the causes remain unknown.

This note's hypothesis is that removing softmax's bound may require retuning initialization and learning rate when integrating the operator into another network. The paper's successful results come from architectures built for deformable convolution, such as InternImage, or settings tuned by the authors.

Conclusion

DCNv4 does two things. It removes softmax, leaving weights unbounded as in ordinary convolution and speeding convergence. It also rewrites the CUDA kernel: channels in the same group share one read, and vectorized instructions move several channels at a time, making the operator more than three times as fast.

If you already use InternImage, FlashInternImage brings speed at almost no extra cost. For your own model, enable half precision—DCNv4 is fastest in FP16—and keep an eye on loss during the first few hundred steps.

The title says “rethinking dynamic and sparse operators.” By the end, much of the speed comes from a plain question: how many times should the same group of channels read the same offsets from memory?