Commit 8edd904
Add device parameter to KeyedJaggedTensor.empty_like and copy_ method (#3510)
Summary:
Pull Request resolved: #3510
This diff enhances the KeyedJaggedTensor API to support device-aware operations, which is needed for efficient cross-device tensor management in TorchRec.
reference: [memory snapshot and footprint for non-blocking copy](#3485)
## Key Changes:
1. **Extended `empty_like` method**: Added an optional `device` parameter to support creating empty KJT structures on a different device. This enables two usage patterns:
- Original: Creates empty KJT on the same device, preserving stride/stride_per_key_per_rank with empty data
- Device-copy: Creates empty KJT structure on a new device, useful for pre-allocating tensors before async copy operations
2. **New `copy_` method**: Implements an in-place copy operation for KeyedJaggedTensor that:
- Copies values, weights, lengths, and offsets from source to destination KJT
- Supports non-blocking (async) copies for better performance
- Assumes host-side metadata (keys, stride, etc.) is already configured
- Handles optional tensors (weights, lengths, offsets) appropriately
3. **Refactored implementation**: Split the original `_kjt_empty_like` logic into:
- `_kjt_empty_like_stride`: Preserves original behavior for same-device empty KJT
- `_kjt_empty_like_device`: New function for cross-device empty KJT creation
These changes enable more efficient device-to-device transfer patterns in distributed training scenarios.
{F1983205769}
### Validation:
in a prototyping experiments with sparse-data-dist pipeline (TrainPipelineSparseDist), the Memcpy HtoD has similar speed (bandwidth) and the CUDA memory timeline profile, but the reserved memory is 79.7GB vs 74.0GB, showing a 5~6GB benefit. While the input KJT per rank is about 1GB.
* trace with direct copy
{F1983200620}
* trace with inplace copy
{F1983200591}
* snapshot with direct copy
{F1983200644} {F1983200655}
* snapshot with inplace copy
{F1983200664} {F1983200670}
Reviewed By: spmex
Differential Revision: D86068070
fbshipit-source-id: 0d1076fd192190b46eed4bda1d4e53b4b245d2a71 parent da8924a commit 8edd904
File tree
3 files changed
+537
-17
lines changed- torchrec
- distributed/test_utils
- sparse
- tests
3 files changed
+537
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
44 | 75 | | |
45 | 76 | | |
46 | | - | |
47 | | - | |
48 | | - | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
49 | 83 | | |
50 | 84 | | |
51 | | - | |
52 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
53 | 132 | | |
54 | 133 | | |
55 | 134 | | |
| |||
299 | 378 | | |
300 | 379 | | |
301 | 380 | | |
302 | | - | |
| 381 | + | |
303 | 382 | | |
304 | 383 | | |
305 | 384 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1466 | 1466 | | |
1467 | 1467 | | |
1468 | 1468 | | |
1469 | | - | |
| 1469 | + | |
1470 | 1470 | | |
| 1471 | + | |
1471 | 1472 | | |
1472 | 1473 | | |
1473 | 1474 | | |
| |||
1488 | 1489 | | |
1489 | 1490 | | |
1490 | 1491 | | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
| 1498 | + | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
1491 | 1537 | | |
1492 | 1538 | | |
1493 | 1539 | | |
| |||
1940 | 1986 | | |
1941 | 1987 | | |
1942 | 1988 | | |
1943 | | - | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
1944 | 1993 | | |
1945 | | - | |
| 1994 | + | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
1946 | 2000 | | |
1947 | 2001 | | |
1948 | 2002 | | |
| 2003 | + | |
1949 | 2004 | | |
1950 | 2005 | | |
1951 | 2006 | | |
1952 | 2007 | | |
1953 | | - | |
| 2008 | + | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
| 2013 | + | |
| 2014 | + | |
| 2015 | + | |
| 2016 | + | |
| 2017 | + | |
| 2018 | + | |
| 2019 | + | |
| 2020 | + | |
| 2021 | + | |
| 2022 | + | |
| 2023 | + | |
| 2024 | + | |
| 2025 | + | |
| 2026 | + | |
| 2027 | + | |
| 2028 | + | |
| 2029 | + | |
| 2030 | + | |
| 2031 | + | |
| 2032 | + | |
| 2033 | + | |
| 2034 | + | |
| 2035 | + | |
| 2036 | + | |
| 2037 | + | |
| 2038 | + | |
| 2039 | + | |
| 2040 | + | |
| 2041 | + | |
| 2042 | + | |
| 2043 | + | |
| 2044 | + | |
| 2045 | + | |
| 2046 | + | |
| 2047 | + | |
| 2048 | + | |
| 2049 | + | |
| 2050 | + | |
| 2051 | + | |
| 2052 | + | |
| 2053 | + | |
| 2054 | + | |
| 2055 | + | |
| 2056 | + | |
| 2057 | + | |
| 2058 | + | |
| 2059 | + | |
| 2060 | + | |
| 2061 | + | |
| 2062 | + | |
| 2063 | + | |
| 2064 | + | |
| 2065 | + | |
1954 | 2066 | | |
1955 | 2067 | | |
1956 | 2068 | | |
| |||
0 commit comments