Commit 9d7e6d5
authored
[CPU/CUDA ep] Improve DeformConv op performance (#27824)
### Description
Improve DeformConv op performance
### Motivation and Context
This PR consolidates a series of optimizations targeting the
`DeformConv` (Deformable Convolution) operator across both CPU and CUDA
execution providers.
* **For CPU:** The previous implementation suffered from bottlenecks due
to redundant computations, lack of vectorization in bilinear sampling,
and sub-optimal thread pool utilization. This overhaul redesigns the
memory layout and execution pipeline to maximize SIMD opportunities and
harden memory safety.
* **For GPU:** The batched GEMM operation previously relied on an
intermediate buffer and a custom scatter kernel to format the output,
which consumed extra memory and kernel launch overhead. This update
introduces a zero-copy approach.
---
#### 1. CPU Optimizations & Refactoring
The CPU execution path has been heavily refactored to minimize branching
in hot paths, maximize vectorization, and safely handle edge cases.
| Feature / Optimization | Description | Key Benefit |
| :--- | :--- | :--- |
| **AoSoA Bilinear Sampling Plan** | Replaced on-the-fly interpolation
with a precomputed sampling plan using an 8-lane
Array-of-Structures-of-Arrays (AoSoA) layout (`kPlanAoSoALanes`). |
Perfectly aligns with 256-bit AVX2 vectors, enabling highly efficient
SIMD unrolling during the `im2col` gathering phase. |
| **Kernel Metadata Caching** | Introduced
`DeformConvKernelMetaCacheData` to cache static convolution geometry
(e.g., `kH`, `kW`, `padding`, `dilation`). | Eliminates the
O(kernel_size) overhead of reallocating and recomputing base offsets on
every single `Compute()` step. |
| **Fast Math & Branchless Logic** | Implemented a custom
`DeformConvFastFloor` and utilized an inverted bounds check with bitwise
operations to evaluate all four corners simultaneously. | Removes
expensive `std::floor` calls and unpredictable branches from the
operator's hottest path. |
| **Enhanced Parallelization** | Flattened the bilinear sampling plan
build tasks across spatial pixels. | Allows
`concurrency::ThreadPool::TryParallelFor` to split fine-grained work
effectively, drastically improving thread pool scaling. |
| **Hardened Bounds Checking** | Introduced compute-time bounds checks
using `CheckedMulSizeT` and `CheckedBatchSpan`. | Ensures batch indexing
and stride calculations stay within the addressable `size_t` range,
preventing integer overflow vulnerabilities. |
| **Bias Addition Refactoring** | Refactored bias addition to avoid
expensive `div`/`mod` operations, applying `ORT_CPU_RESTRICT` and
force-inlining. | Maximizes memory throughput and instruction pipelining
during the final bias addition phase. |
---
#### 2. GPU (CUDA) Optimizations
The CUDA implementation was optimized to reduce memory footprint and
eliminate unnecessary kernel launches.
* **Zero-Copy GEMM Output:** Removed the temporary `gemm_output_buffer`
allocation entirely. By carefully configuring the `stride_c` parameter
(`stride_c_y = M * output_image_size`), the
`cublasGemmStridedBatchedHelper` now writes the computed output directly
into the correct NCHW memory layout of the final `Y` tensor.
* **Kernel Elimination:** Completely removed the
`DeformConvCopyGemmOutputRowMajorToNCHW` custom kernel and its
associated dispatch logic. This reduces kernel launch overhead, lowers
GPU memory bandwidth pressure, and simplifies the overall CUDA execution
pipeline.
* **Reduced Memory Footprint:** Updated the `bytes_per_image`
calculation for workspace memory to reflect the removal of the GEMM
output buffer. This allows the operator to potentially process more
images in parallel under the same memory constraints.
---
#### 3. Changed
- **Batch chunking:** Chunk size `k` is chosen so that the number of
outer rounds is minimized under the temp-memory cap; **`k` does not have
to divide `N`**. The host loop uses `cur_parallel = min(k, N - b)`, so
the last chunk may be smaller. This is the intended default behavior for
this EP (not yet in a formal release).
- **Kernel-size templates:** Im2col is specialized for **1×1, 3×3, and
7×7**; other sizes (including **5×5**) use the **dynamic** `kH`/`kW`
path. Rationale: 5×5 is less common in current stacks (often replaced by
stacked 3×3); specializing 7×7 targets common large-kernel cases. Older
DCN/detection models that still use **5×5** deformable conv will take
the dynamic path—correctness is unchanged; only compile-time unrolling
differs.
- **Add aliasing flags:** Updated DeformConv aliasing comments to make
the stronger guarantee explicit: if output `Y` overlaps any input
buffer, results can be incorrect regardless of `restrict`, because
output writes may clobber source elements before they are fully
consumed. `restrict` further tightens this by introducing undefined
behavior when aliasing assumptions are violated.
---
### Summary
In the current implementation, CPU performance is 33x (main branch is
15x) that of TorchVision. If we were to implement AVX2/AVX512
optimizations from scratch, we could achieve a 36x performance boost.
However, I haven’t found any similar reference code in the ONNX Runtime
repository.
This PR also significantly improves parallelism:
<img width="540" height="332" alt="image"
src="https://github.com/user-attachments/assets/d4f670bd-dde3-43f1-b597-4471bfde005b"
/>
_Both ort and tv are configured with 16 threads_
### Open Question for Reviewers
**Regarding CUDA Temporary Memory Allocation:**
Currently, the effective maximum temporary memory for CUDA is calculated
using a heuristic (`total_global_mem * 0.1` or similar logic in
`GetDeformConvEffectiveMaxTempBytes`). While the removal of
`gemm_output_buffer` has reduced the memory footprint per image, I am
not entirely certain if this 10% threshold is still the most appropriate
value for balancing parallel image processing (`n_parallel_imgs`)
against overall VRAM consumption in large models.
I would appreciate any feedback or suggestions on whether we should tune
this threshold, or if there's a more robust way to dynamically determine
the optimal temporary workspace size for `DeformConv` in ORT.1 parent 9e3614b commit 9d7e6d5
6 files changed
Lines changed: 1379 additions & 490 deletions
File tree
- onnxruntime
- core/providers
- cpu/nn
- cuda/nn
- test/providers/cpu/nn
Large diffs are not rendered by default.
Lines changed: 40 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
76 | 112 | | |
77 | 113 | | |
78 | 114 | | |
| |||
159 | 195 | | |
160 | 196 | | |
161 | 197 | | |
162 | | - | |
| 198 | + | |
163 | 199 | | |
164 | | - | |
165 | | - | |
| 200 | + | |
| 201 | + | |
166 | 202 | | |
167 | 203 | | |
168 | 204 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
5 | 16 | | |
6 | 17 | | |
7 | 18 | | |
| |||
21 | 32 | | |
22 | 33 | | |
23 | 34 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
49 | 59 | | |
50 | 60 | | |
51 | 61 | | |
| |||
76 | 86 | | |
77 | 87 | | |
78 | 88 | | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
83 | 93 | | |
84 | 94 | | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
89 | 98 | | |
90 | | - | |
91 | | - | |
| 99 | + | |
| 100 | + | |
92 | 101 | | |
93 | | - | |
| 102 | + | |
94 | 103 | | |
95 | | - | |
96 | | - | |
97 | | - | |
| 104 | + | |
98 | 105 | | |
99 | 106 | | |
100 | | - | |
| 107 | + | |
101 | 108 | | |
102 | 109 | | |
103 | 110 | | |
| |||
146 | 153 | | |
147 | 154 | | |
148 | 155 | | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
155 | 163 | | |
156 | 164 | | |
157 | 165 | | |
158 | 166 | | |
159 | 167 | | |
160 | 168 | | |
161 | 169 | | |
162 | | - | |
163 | | - | |
164 | 170 | | |
165 | 171 | | |
166 | 172 | | |
| |||
180 | 186 | | |
181 | 187 | | |
182 | 188 | | |
| 189 | + | |
183 | 190 | | |
184 | 191 | | |
185 | 192 | | |
| |||
215 | 222 | | |
216 | 223 | | |
217 | 224 | | |
218 | | - | |
| 225 | + | |
| 226 | + | |
219 | 227 | | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
228 | 237 | | |
229 | 238 | | |
230 | 239 | | |
| |||
249 | 258 | | |
250 | 259 | | |
251 | 260 | | |
252 | | - | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
253 | 264 | | |
254 | 265 | | |
255 | | - | |
| 266 | + | |
256 | 267 | | |
257 | 268 | | |
258 | | - | |
| 269 | + | |
259 | 270 | | |
260 | 271 | | |
261 | 272 | | |
262 | | - | |
| 273 | + | |
263 | 274 | | |
264 | 275 | | |
265 | 276 | | |
266 | 277 | | |
267 | 278 | | |
| 279 | + | |
268 | 280 | | |
269 | 281 | | |
| 282 | + | |
270 | 283 | | |
271 | | - | |
272 | | - | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
273 | 288 | | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
| 289 | + | |
284 | 290 | | |
285 | 291 | | |
286 | 292 | | |
287 | 293 | | |
288 | 294 | | |
289 | | - | |
| 295 | + | |
| 296 | + | |
290 | 297 | | |
291 | 298 | | |
292 | 299 | | |
| |||
0 commit comments