Repository navigation
Commit fbf91c2
authored
Accept AOTI's compact clones of view buffers in the CUDA weight collector (pytorch#23355)
## What is wrong today
When the CUDA backend compiles a model, AOTInductor hands back every
constant as a pair: the tensor to save, and a `TensorProperties` record
that describes it (storage size, offset, shape, strides). The CUDA
weight collector writes the tensor's storage to the `.ptd` file and
records that metadata so the runtime can rebuild the tensor.
AOTInductor clones every buffer into compact storage first. The clone
covers only the bytes the buffer needs and starts at offset 0. But
`TensorProperties` is built from the original buffer.
That breaks when a buffer is a view into a larger tensor:
```python
big = torch.arange(32.0, device="cuda").reshape(8, 4)
self.register_buffer("w", big[2:5]) # 48 bytes, at offset 8 of a 128-byte storage
```
The clone is 48 bytes at offset 0, while `TensorProperties` says 128
bytes at offset 8. The collector compares the two, and lowering with
`CudaPartitioner` fails:
```
RuntimeError: AOTI cloned storage is smaller than its TensorProperties (48 < 128 bytes)
```
A strided view fails in the bounds check instead, because its
`TensorProperties` has no storage size. A column slice `big[:, 1:3]` of
an 8x8 tensor fails with:
```
RuntimeError: AOTI view 'w' requires 236 bytes from a 232-byte cloned storage
```
In both cases the clone holds every byte the buffer needs. Only the
metadata describes a different storage.
## What this change does
When the tensor is AOTInductor's compact clone of the view (a different
storage, with the same shape and strides as `TensorProperties`), the
collector skips the comparison with the original storage size and takes
the offset from the clone it writes. Any other tensor goes through the
existing path unchanged.
The check that the view fits inside the bytes actually written still
runs for every weight, so a truncated clone is still rejected. A compact
clone is also checked to span its storage exactly, so a clone with bytes
past its view is rejected too.
## What was tested
- New unit tests in `test_cuda_partitioner.py`: a clone of `big[2:5]`
paired with the view's `TensorProperties` is written as 48 bytes at
offset 0; a clone of the strided `big[:, 1:3]` is written as its
232-byte storage, byte for byte; a clone with too little storage or with
bytes past its view is rejected; a parameter that slices a fused tensor
and keeps its storage still records its own offset and storage size; a
value with the same shape but other strides is not taken for a clone;
and a tensor with another layout keeps the offset its `TensorProperties`
records. The two clone tests fail before this change with the errors
above.
- On a CUDA GPU (Linux aarch64), the module above lowered with
`CudaPartitioner`: before, the error above; after, it exports, and
running the `.pte` with the ExecuTorch runtime gives exactly the eager
output.
- `test_cuda_partitioner.py` and `test_cuda_weight_metadata.py` pass
with this change.1 parent 223455b commit fbf91c2
2 files changed
Lines changed: 124 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
399 | 399 | | |
400 | 400 | | |
401 | 401 | | |
| 402 | + | |
402 | 403 | | |
403 | 404 | | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
404 | 415 | | |
405 | 416 | | |
406 | | - | |
407 | 417 | | |
408 | 418 | | |
409 | 419 | | |
410 | | - | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
411 | 425 | | |
412 | 426 | | |
413 | 427 | | |
| |||
427 | 441 | | |
428 | 442 | | |
429 | 443 | | |
430 | | - | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
431 | 449 | | |
432 | 450 | | |
433 | 451 | | |
| |||
436 | 454 | | |
437 | 455 | | |
438 | 456 | | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
439 | 462 | | |
440 | 463 | | |
441 | 464 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
232 | 233 | | |
233 | 234 | | |
234 | 235 | | |
235 | | - | |
236 | | - | |
| 236 | + | |
| 237 | + | |
237 | 238 | | |
238 | 239 | | |
239 | 240 | | |
| |||
251 | 252 | | |
252 | 253 | | |
253 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 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 | + | |
| 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 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
254 | 350 | | |
255 | 351 | | |
256 | 352 | | |
| |||
0 commit comments