Commit 73ed4c9
authored
[perf] Improve performance for putting jagged tensor (Ascend#36)
## Background
When users input a TensorDict containing jagged tensors (nested
tensors), the `put_data` process becomes extremely slow.
Specifically, the `_filter_storage_data` function uses
`itemgetter(*batch_indexes)(data[fname])` to extract individual items
from each tensor in the TensorDict. This indexing approach works
efficiently for strided tensors but is extremely inefficient for jagged
tensors.
## Root Cause
For jagged tensors, itemgetter with multiple batch indexes requires
repeated indexing operations, which is $\mathcal{O}(n)$ for each access.
When extracting multiple samples, this becomes $\mathcal{O}(n²)$
complexity.
## Solution
We unbind nested tensor before accessing each sample from it.
```python3
# unbind nested tensor
results: dict = {}
for field in sorted(data.keys()):
field_data = data[field]
if isinstance(field_data, Tensor) and field_data.is_nested:
results[field] = field_data.unbind()
else:
results[field] = field_data
```
## Simple Reproduction Script
```python3
import torch
import time
from operator import itemgetter
# Create a jagged tensor with 1000 samples
offsets = torch.tensor([0] + list(torch.randint(10, 50, (1001,)).cumsum(0)))
values = torch.randn(offsets[-1].item(), 128)
jagged = torch.nested.as_nested_tensor(
[values[offsets[i]:offsets[i+1]] for i in range(1000)],
layout=torch.jagged
)
batch_indexes = list(range(0, 1000, 10)) # 100 indexes
# Method 1: Direct itemgetter on jagged tensor (SLOW)
start = time.perf_counter()
result = itemgetter(*batch_indexes)(jagged)
print(f"Direct itemgetter: {(time.perf_counter() - start)*1000:.2f} ms")
# Method 2: Unbind first, then itemgetter (FAST)
start = time.perf_counter()
field_list = jagged.unbind()
result = itemgetter(*batch_indexes)(field_list)
print(f"Unbind + itemgetter: {(time.perf_counter() - start)*1000:.2f} ms")
```
Output:
```bash
Direct itemgetter: 150.94 ms
Unbind + itemgetter: 1.80 ms
```
---------
Signed-off-by: 0oshowero0 <o0shower0o@outlook.com>1 parent b7a3b01 commit 73ed4c9
4 files changed
Lines changed: 53 additions & 23 deletions
File tree
- transfer_queue
- storage/managers
- tutorial
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
815 | 816 | | |
816 | 817 | | |
817 | 818 | | |
818 | | - | |
819 | | - | |
820 | | - | |
821 | | - | |
822 | | - | |
823 | | - | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
824 | 836 | | |
825 | 837 | | |
826 | | - | |
827 | | - | |
828 | | - | |
829 | | - | |
| 838 | + | |
830 | 839 | | |
831 | 840 | | |
832 | 841 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
394 | 394 | | |
395 | 395 | | |
396 | 396 | | |
397 | | - | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
398 | 405 | | |
399 | 406 | | |
400 | 407 | | |
| |||
Lines changed: 24 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
201 | 202 | | |
202 | 203 | | |
203 | 204 | | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
204 | 214 | | |
205 | 215 | | |
206 | 216 | | |
207 | | - | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
208 | 220 | | |
209 | 221 | | |
210 | 222 | | |
| |||
221 | 233 | | |
222 | 234 | | |
223 | 235 | | |
224 | | - | |
225 | | - | |
| 236 | + | |
| 237 | + | |
226 | 238 | | |
227 | 239 | | |
228 | 240 | | |
| |||
234 | 246 | | |
235 | 247 | | |
236 | 248 | | |
237 | | - | |
| 249 | + | |
238 | 250 | | |
239 | 251 | | |
240 | 252 | | |
| |||
432 | 444 | | |
433 | 445 | | |
434 | 446 | | |
435 | | - | |
436 | | - | |
| 447 | + | |
| 448 | + | |
437 | 449 | | |
438 | 450 | | |
439 | | - | |
| 451 | + | |
440 | 452 | | |
441 | 453 | | |
442 | 454 | | |
443 | 455 | | |
444 | 456 | | |
445 | 457 | | |
446 | 458 | | |
447 | | - | |
448 | | - | |
| 459 | + | |
| 460 | + | |
449 | 461 | | |
450 | 462 | | |
451 | 463 | | |
| |||
461 | 473 | | |
462 | 474 | | |
463 | 475 | | |
464 | | - | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
465 | 479 | | |
466 | 480 | | |
467 | 481 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| |||
0 commit comments