Skip to content

Commit bda0f7c

Browse files
jackzhxngfacebook-github-bot
authored andcommitted
Introduce new DataLoader::load() with segment info (pytorch#4158)
Summary: Pull Request resolved: pytorch#4158 Adds a new `DataLoader::load()` function with an additonal parameter, SegmentInfo. Makes the old `DataLoader::Load()` function deprecated. Diff stack: - (**x**) [[1/n][executorch] Introduce new DataLoader::load() with segment info](https://www.internalfb.com/diff/D59399538?dst_version_fbid=2979141262226992) - ( ) [[2/n][executorch] Switch to DataLoader::load in runtime](https://www.internalfb.com/diff/D59594142) - ( ) [[3/n][executorch] Pass in correct backend id into data load from runtime](https://www.internalfb.com/diff/D59606243) Reviewed By: dbort Differential Revision: D59399538 fbshipit-source-id: bee86dba1cb02196481af1bdc7673a94b54bf003
1 parent 08f4334 commit bda0f7c

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

runtime/core/data_loader.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,57 @@ namespace executor {
2424
*/
2525
class DataLoader {
2626
public:
27+
/**
28+
* Describes the content of the segment.
29+
*/
30+
struct SegmentInfo {
31+
/**
32+
* Represents the purpose of the segment.
33+
*/
34+
enum class Type {
35+
/**
36+
* Data for the actual program.
37+
*/
38+
Program,
39+
/**
40+
* Holds constant tensor data.
41+
*/
42+
Constant,
43+
/**
44+
* Data used for initializing a backend.
45+
*/
46+
Backend,
47+
};
48+
49+
/// Type of the segment.
50+
Type segment_type;
51+
52+
/// Index of the segment within the segment list. Undefined for program
53+
/// segments.
54+
size_t segment_index;
55+
56+
/// An optional, null-terminated string describing the segment. For
57+
/// `Backend` segments, this is the backend ID. Null for other segment
58+
/// types.
59+
const char* descriptor;
60+
61+
SegmentInfo() = default;
62+
63+
explicit SegmentInfo(
64+
Type segment_type,
65+
size_t segment_index = 0,
66+
const char* descriptor = nullptr)
67+
: segment_type(segment_type),
68+
segment_index(segment_index),
69+
descriptor(descriptor) {}
70+
};
71+
2772
virtual ~DataLoader() = default;
2873

2974
/**
75+
* DEPRECATED: Use `load()` going forward for access to segment info during
76+
* the load.
77+
*
3078
* Loads `size` bytes at byte offset `offset` from the underlying data source
3179
* into a `FreeableBuffer`, which owns the memory.
3280
*
@@ -37,6 +85,24 @@ class DataLoader {
3785
size_t offset,
3886
size_t size) = 0;
3987

88+
/**
89+
* Loads data from the underlying data source.
90+
*
91+
* NOTE: This must be thread-safe. If this call modifies common state, the
92+
* implementation must do its own locking.
93+
*
94+
* @param offset The byte offset in the data source to start loading from.
95+
* @param size The number of bytes to load.
96+
* @param segment_info Information about the segment being loaded.
97+
*
98+
* @returns a `FreeableBuffer` that owns the loaded data.
99+
*/
100+
__ET_NODISCARD virtual Result<FreeableBuffer>
101+
load(size_t offset, size_t size, const SegmentInfo& segment_info) {
102+
(void)segment_info;
103+
return Load(offset, size); // NOLINT(facebook-hte-Deprecated)
104+
}
105+
40106
/**
41107
* Returns the length of the underlying data source, typically the file size.
42108
*/

0 commit comments

Comments
 (0)