Commit bf6dce3
Fix int32 stride overflow in jagged_to_padded_dense at B*L*D > INT_MAX (#5755)
Summary:
Pull Request resolved: #5755
X-link: https://github.com/facebookresearch/FBGEMM/pull/2686
Fixes a class of int32 stride-overflow bugs in fbgemm::jagged_to_padded_dense.forward (and the dense_to_jagged backward path that calls it).
The kernel jagged_dense_elementwise_dense_output_kernel_ in common.cuh declares its dense `y` and `output` parameters as PackedTensorAccessor32. PTA32 stores strides as int32, so `output[oidx][jidx][iidx]` lowers to `oidx * strides_[0]` in int32. When the dense tensor's numel exceeds INT_MAX (specifically when (B - 1) * max_L * D > INT_MAX), the multiply wraps negative, writes go outside the destination buffer, and the high-oidx rows of the at::empty_symint allocation stay unwritten. Because the outer-loop bound B * max_L need not itself overflow, FBGEMM_LAUNCH_KERNEL's grid sanity check does not fire - the kernel runs to completion silently, and the unwritten memory surfaces as NaN / garbage downstream.
Fix (narrow, contained to a single kernel + its launcher):
- Template walk_down_tensor_storage_tree_ on a new PosT type. The existing int callers continue to work via auto template deduction; the int64 path can now pass int64_t.
- Template jagged_dense_elementwise_dense_output_kernel_ on a new IdxT used both as the PackedTensorAccessor stride width and as the type of the loop / index variables (outer, oidx, jidx, offset, iidx). This keeps the int32 fast path register-width consistent and lets the int64 specialization avoid all int32 wrap points.
- INVOKE_KERNEL_WITH_DIM in jagged_dense_elementwise_dense_output_ now branches on whether x_values, y_reshaped, and output_reshaped all have numel < INT_MAX, dispatching either the existing PTA32+int32_t fast path (unchanged) or a new PTA64+int64_t specialization. Workloads that fit in int32 pay zero extra cost; only over-INT_MAX workloads pay the 64-bit indexing tax.
Scope intentionally limited:
- Does NOT touch the sibling jagged_dense_dense_elementwise_jagged_output_kernel_ or its INVOKE_KERNEL_WITH_DIM (in this file or in jagged_dense_dense_elementwise_add_jagged_output_forward.cu).
- Does NOT touch check_shape_and_partition_, jagged_dense_elementwise_mul_backward.cu, jagged_to_padded_dense_forward.cu, jagged_tensor_ops_autograd.cpp, or cuda_prelude.cuh.
- The kernels and call sites that ARE NOT modified retain their previous PTA32/int32 behavior unchanged. They have the same hazard at sufficiently large shapes; addressing them is out of scope for this diff and can be tracked separately if a need arises.
Context: this bug class was identified during investigation of T264042859 (NaN at pos_encoding.grad in Instagram Reels MTML training at 20k UIH). For the IG Reels prod shapes (B_RO=1280, uih_max_len=20480, pe_emb_dim=64 - numel ~ 1.68B, below INT_MAX), this fix does NOT change kernel behavior - the int32 fast path is still selected. The IG Reels prod failure was resolved separately by D99880049, which avoids the dense_to_jagged path entirely with a model-side gather refactor. This diff lands the kernel-level fix as defense-in-depth against future callers whose shapes do cross INT_MAX (e.g. pe_emb_dim scaled to 128, or any other dense_to_jagged-on-broadcast pattern at scale).
Reviewed By: q10
Differential Revision: D104247303
fbshipit-source-id: f27c91b6c17b529c8b577f3822ef85ac675cfa101 parent bd907c4 commit bf6dce3
2 files changed
Lines changed: 196 additions & 57 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
87 | 91 | | |
88 | | - | |
89 | | - | |
| 92 | + | |
| 93 | + | |
90 | 94 | | |
91 | 95 | | |
92 | 96 | | |
93 | | - | |
94 | | - | |
| 97 | + | |
| 98 | + | |
95 | 99 | | |
96 | 100 | | |
97 | | - | |
| 101 | + | |
98 | 102 | | |
99 | 103 | | |
100 | 104 | | |
| |||
103 | 107 | | |
104 | 108 | | |
105 | 109 | | |
106 | | - | |
107 | | - | |
| 110 | + | |
| 111 | + | |
108 | 112 | | |
109 | 113 | | |
110 | 114 | | |
| |||
128 | 132 | | |
129 | 133 | | |
130 | 134 | | |
131 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
132 | 148 | | |
133 | 149 | | |
134 | | - | |
| 150 | + | |
135 | 151 | | |
136 | 152 | | |
137 | | - | |
138 | | - | |
| 153 | + | |
| 154 | + | |
139 | 155 | | |
140 | 156 | | |
141 | 157 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
154 | 171 | | |
155 | 172 | | |
156 | 173 | | |
157 | 174 | | |
158 | | - | |
| 175 | + | |
159 | 176 | | |
160 | 177 | | |
161 | 178 | | |
| |||
168 | 185 | | |
169 | 186 | | |
170 | 187 | | |
171 | | - | |
| 188 | + | |
172 | 189 | | |
173 | 190 | | |
174 | 191 | | |
| |||
256 | 273 | | |
257 | 274 | | |
258 | 275 | | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
288 | 338 | | |
289 | 339 | | |
290 | 340 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
25 | 30 | | |
26 | 31 | | |
27 | 32 | | |
| |||
160 | 165 | | |
161 | 166 | | |
162 | 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 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
163 | 252 | | |
164 | 253 | | |
165 | 254 | | |
| |||
0 commit comments