Commit 02201b8
fix(transforms): make Crop.compute_slices torch.compile-friendly (#8960)
`CenterSpatialCrop` blows up under `torch.compile` while the other crop
transforms are fine (#8191). It fails in `Crop.compute_slices` with `The
tensor has a non-zero number of elements, but its data is not allocated
yet`.
The cause is that `compute_slices` ran its start/end math through CPU
tensors (`convert_to_tensor(..., device="cpu")`). For
`CenterSpatialCrop` the ROI values come from the input shape, so under
tracing they're fake tensors with no storage, and moving them to the CPU
asks Dynamo for data that isn't there.
Since it's just integer math, I moved it to plain Python. A small
`_to_int_list` helper handles the input forms (scalar, sequence, tensor,
ndarray), with the same clamping and broadcasting as before.
`CenterSpatialCrop` now compiles like the rest of the transforms.
Added a regression test that compiles `CenterSpatialCrop` and checks the
shape (fails before, passes after), guarded for PyTorch versions with
`torch.compile`.
Fixes #8191.
---------
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
Signed-off-by: Soumya Snigdha Kundu <soumyawork15@gmail.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>1 parent 7c23098 commit 02201b8
2 files changed
Lines changed: 50 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
342 | 342 | | |
343 | 343 | | |
344 | 344 | | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
345 | 363 | | |
346 | 364 | | |
347 | 365 | | |
| |||
379 | 397 | | |
380 | 398 | | |
381 | 399 | | |
382 | | - | |
383 | | - | |
384 | 400 | | |
385 | 401 | | |
386 | 402 | | |
387 | 403 | | |
388 | 404 | | |
389 | 405 | | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
396 | 409 | | |
397 | 410 | | |
398 | 411 | | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
407 | 416 | | |
408 | 417 | | |
409 | 418 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
19 | 21 | | |
| 22 | + | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
| |||
50 | 53 | | |
51 | 54 | | |
52 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
53 | 78 | | |
54 | 79 | | |
55 | 80 | | |
0 commit comments