Skip to content

Commit 7c4cae6

Browse files
authored
Implement load_into for SharedPtrDataLoader (pytorch#22210)
### Summary `SharedPtrDataLoader` never overrode `load_into`, so it fell back to the stub in `runtime/core/data_loader.h` that logs an error and returns `NotImplemented`. Anything loading through it fails as a result — `Program::load_mutable_subsegment_into` and the training `state_dict_util` being the ones that matter, so a mutable buffer with a serialized initial value can't be read out of a shared buffer at all. The other loaders all implement this; this one looks like it was just missed. Mirrors `BufferDataLoader::load_into`. Fixes pytorch#11562 ### Test plan Added `InBoundsLoadIntoSucceeds` and `LoadIntoNullDstFails` to `extension/data_loader/test/shared_ptr_data_loader_test.cpp`, matching the `BufferDataLoader` equivalents. Ran the `SharedPtrDataLoaderTest` suite locally, all 4 pass. Authored with AI assistance (Claude Code).
1 parent c6d0043 commit 7c4cae6

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎extension/data_loader/shared_ptr_data_loader.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <executorch/runtime/core/error.h>
1414
#include <executorch/runtime/core/result.h>
1515
#include <executorch/runtime/platform/log.h>
16+
#include <cstring>
1617
#include <memory>
1718

1819
namespace executorch {
@@ -47,6 +48,24 @@ class SharedPtrDataLoader final : public executorch::runtime::DataLoader {
4748
static_cast<uint8_t*>(data_.get()) + offset, size, /*free_fn=*/nullptr);
4849
}
4950

51+
ET_NODISCARD executorch::runtime::Error load_into(
52+
size_t offset,
53+
size_t size,
54+
ET_UNUSED const SegmentInfo& segment_info,
55+
void* buffer) const override {
56+
ET_CHECK_OR_RETURN_ERROR(
57+
buffer != nullptr,
58+
InvalidArgument,
59+
"Destination buffer cannot be null");
60+
61+
auto result = load(offset, size, segment_info);
62+
if (!result.ok()) {
63+
return result.error();
64+
}
65+
std::memcpy(buffer, result->data(), size);
66+
return executorch::runtime::Error::Ok;
67+
}
68+
5069
ET_NODISCARD executorch::runtime::Result<size_t> size() const override {
5170
return size_;
5271
}

‎extension/data_loader/test/shared_ptr_data_loader_test.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,61 @@ TEST_F(SharedPtrDataLoaderTest, OutOfBoundsLoadFails) {
140140
EXPECT_NE(fb.error(), Error::Ok);
141141
}
142142
}
143+
144+
TEST_F(SharedPtrDataLoaderTest, InBoundsLoadIntoSucceeds) {
145+
// Create some heterogeneous data.
146+
const size_t SIZE = 256;
147+
std::shared_ptr<uint8_t[]> data(
148+
new uint8_t[SIZE], std::default_delete<uint8_t[]>());
149+
for (int i = 0; i < SIZE; ++i) {
150+
data[i] = i;
151+
}
152+
153+
// Wrap it in a loader.
154+
SharedPtrDataLoader sbdl(data, SIZE);
155+
156+
uint8_t dst[3] = {};
157+
Error err = sbdl.load_into(
158+
/*offset=*/2,
159+
/*size=*/sizeof(dst),
160+
/*segment_info=*/
161+
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program),
162+
dst);
163+
EXPECT_EQ(err, Error::Ok);
164+
EXPECT_EQ(0, std::memcmp(dst, "\x02\x03\x04", sizeof(dst)));
165+
166+
// Data is unaltered.
167+
EXPECT_EQ(data[2], 2);
168+
}
169+
170+
TEST_F(SharedPtrDataLoaderTest, LoadIntoNullDstFails) {
171+
// Wrap some data in a loader.
172+
const size_t SIZE = 256;
173+
std::shared_ptr<uint8_t[]> data(
174+
new uint8_t[SIZE], std::default_delete<uint8_t[]>());
175+
176+
// Wrap it in a loader.
177+
SharedPtrDataLoader sbdl(data, SIZE);
178+
179+
// Loading into a null destination should fail.
180+
{
181+
Error err = sbdl.load_into(
182+
/*offset=*/0,
183+
/*size=*/1,
184+
/*segment_info=*/
185+
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program),
186+
nullptr);
187+
EXPECT_NE(err, Error::Ok);
188+
}
189+
190+
// Loading zero bytes still fails if dst is null.
191+
{
192+
Error err = sbdl.load_into(
193+
/*offset=*/0,
194+
/*size=*/0,
195+
/*segment_info=*/
196+
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program),
197+
nullptr);
198+
EXPECT_NE(err, Error::Ok);
199+
}
200+
}

0 commit comments

Comments
 (0)