Commit 1eb5cbb
committed
[Relax][Frontend][Torch] Clamp diagonal length to zero for out-of-range offsets
For an out-of-range offset (|offset| >= max(extent1, extent2)), PyTorch's
torch.diagonal returns an empty diagonal of shape (0,). The lowering
computed diag_len = min(extent1, extent2 - offset), which could go negative:
e.g. a (3, 4) input with offset=5 gave diag_len=-1 and incompatible slice
extents (the einsum then failed to broadcast extents 2 and 0), and offset=6
even produced an incorrect non-empty shape.
Clamp diag_len with tirx.max(0, ...) in both the positive- and
negative-offset branches so an out-of-range offset lowers to an empty
diagonal, matching PyTorch. Add in-tree regression coverage for out-of-range
positive and negative offsets.1 parent d519b97 commit 1eb5cbb
2 files changed
Lines changed: 17 additions & 2 deletions
File tree
- python/tvm/relax/frontend/torch
- tests/python/relax
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1302 | 1302 | | |
1303 | 1303 | | |
1304 | 1304 | | |
1305 | | - | |
| 1305 | + | |
1306 | 1306 | | |
1307 | 1307 | | |
1308 | 1308 | | |
1309 | | - | |
| 1309 | + | |
1310 | 1310 | | |
1311 | 1311 | | |
1312 | 1312 | | |
| |||
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3399 | 3399 | | |
3400 | 3400 | | |
3401 | 3401 | | |
| 3402 | + | |
| 3403 | + | |
| 3404 | + | |
| 3405 | + | |
| 3406 | + | |
| 3407 | + | |
| 3408 | + | |
| 3409 | + | |
| 3410 | + | |
| 3411 | + | |
| 3412 | + | |
| 3413 | + | |
| 3414 | + | |
| 3415 | + | |
| 3416 | + | |
3402 | 3417 | | |
3403 | 3418 | | |
3404 | 3419 | | |
| |||
0 commit comments