Commit f79df44
Fix CUDA AveragePool wrong results with asymmetric padding and dilation (#29631)
### Summary
Fixes wrong `AveragePool` output on the **CUDA** EP whenever cuDNN's
pooling descriptor cannot represent the requested pooling — i.e.
**asymmetric padding** or **dilation > 1**. This is the CUDA-EP
counterpart of the CPU fix in #29629 and, together with it, the JSEP
shape fix in #29627, closes out the CUDA leg of pytorch/pytorch#183528.
### Root cause
`CudnnPoolingDescriptor::Set` copies only the **begin** pads
(`pads[0..rank)`) into the cuDNN pooling descriptor, which stores a
*single symmetric pad value per axis* and applies it to both sides. The
ONNX **end** pads (`pads[rank..2*rank)`) are silently dropped. As a
result, **all** asymmetric-pad AveragePool on CUDA is wrong:
- explicit asymmetric `pads`,
- `auto_pad = SAME_UPPER` / `SAME_LOWER` (which produce naturally
asymmetric pads),
- `ceil_mode = 1` boundary windows.
Separately, the cuDNN pooling descriptor has **no dilation parameter at
all**, so any `dilations > 1` is also silently ignored — even with
symmetric pads.
Example divergence from the CPU reference (QA probe): 1D `pad(0,3)`, k7,
s3, `ceil_mode=1`, `count_include_pad=1` gave CUDA `[4, 6.5, 8]` vs.
correct `[4, 5.571, 4]`; 2D `pad(0,0,3,3)` gave `71.0` vs. correct
`17.75`.
### Fix
Add a custom CUDA average-pool kernel (`avg_pool_impl.cu` / `.h`,
modeled on `max_pool_with_index.cu`) that honors **per-side pads** and
**dilation**, and computes the `count_include_pad` divisor exactly like
the CPU v19 reference functor (`AveragePool{1,2,3}DTask`):
- include-pad divisor clamps the window end to `input + pad_tail` (drops
the ceil-mode phantom cells), dividing by `∏ (1 + (end - start -
1)/dilation)`;
- exclude-pad divisor counts only in-bounds cells.
In `Pool<T, AveragePool, Layout>::ComputeInternal`, a cheap dispatch
guard routes to the custom kernel **only** when the pooling is
non-global **and** (`asymmetric pads` **or** `!default_dilations`).
Every symmetric, non-dilated case — the overwhelmingly common path,
including **all** `GlobalAveragePool` — stays on the existing cuDNN fast
path, so there is **zero perf regression** on the common path.
`GlobalAveragePool` is excluded explicitly because `PoolAttributes`
leaves its `kernel_shape`/`strides`/`dilations` unpopulated. `MaxPool`
is unaffected (it ignores pad cells; `MaxPool<8>` already routes
dilation to its own custom kernel via the same `!default_dilations`
check we mirror here).
Covers fp32, fp64, fp16, and bf16 (fp16/bf16 accumulate in float).
### Tests
Added CUDA-**un-excluded** parity tests in `pool_op_test.cc` — the CUDA
leg actually runs and must match the CPU reference oracle:
- asymmetric tail-pad 1D/2D, include- and exclude-pad;
- `auto_pad=SAME_UPPER` and `SAME_LOWER` (naturally asymmetric);
- **symmetric-pad + dilation>1** (wrong on cuDNN, correct via the kernel
— locks in the dilation guard);
- a symmetric-pad regression case (must stay on cuDNN and remain
correct);
- fp16;
- a `MaxPool` asymmetric-pad case confirming MaxPool is unaffected.
The `ceil_mode + count_include_pad` cases use opset 19 so the CPU leg
runs the already-correct v19 reference functor and validates the CUDA
kernel independently of the separate opset-7..18 CPU MLAS fix (#29629);
CUDA routing is opset-independent. Full `PoolTest` suite passes on an
A100 (53 passed / 2 DML-skipped / 0 failures).
### Related
- CPU (a): #29629 — opset 7-18 MLAS `ceil_mode + count_include_pad`
divisor fix.
- JSEP (c): #29627 — JSEP pooling output shape honoring `ceil_mode`.
- pytorch/pytorch#183528.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent f4aa2b4 commit f79df44
4 files changed
Lines changed: 607 additions & 6 deletions
File tree
- onnxruntime
- core/providers/cuda/nn
- test/providers/cpu/nn
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 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 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 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 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
205 | 206 | | |
206 | 207 | | |
207 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
208 | 240 | | |
209 | 241 | | |
210 | 242 | | |
| |||
0 commit comments