Skip to content

[executorch] Rename MmapDataLoader::From to ::from #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extension/data_loader/mmap_data_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ MmapDataLoader::~MmapDataLoader() {
::close(fd_);
}

Result<MmapDataLoader> MmapDataLoader::From(
Result<MmapDataLoader> MmapDataLoader::from(
const char* file_name,
MmapDataLoader::MlockConfig mlock_config) {
// Cache the page size.
Expand Down
13 changes: 10 additions & 3 deletions extension/data_loader/mmap_data_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,23 @@ class MmapDataLoader : public DataLoader {
* @param[in] mlock_config How and whether to lock loaded pages with
* `mlock()`.
*/
static Result<MmapDataLoader> From(
static Result<MmapDataLoader> from(
const char* file_name,
MlockConfig mlock_config = MlockConfig::UseMlock);

/// DEPRECATED: Use the version of `From()` that takes an MlockConfig.
/// DEPRECATED: Use the lowercase `from()` instead.
__ET_DEPRECATED static Result<MmapDataLoader> From(
const char* file_name,
MlockConfig mlock_config = MlockConfig::UseMlock) {
return from(file_name, mlock_config);
}

/// DEPRECATED: Use the version of `from()` that takes an MlockConfig.
__ET_DEPRECATED
static Result<MmapDataLoader> From(const char* file_name, bool use_mlock) {
MlockConfig mlock_config =
use_mlock ? MlockConfig::UseMlock : MlockConfig::NoMlock;
return From(file_name, mlock_config);
return from(file_name, mlock_config);
}

// Movable to be compatible with Result.
Expand Down
33 changes: 27 additions & 6 deletions extension/data_loader/test/mmap_data_loader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void MmapDataLoaderTest::test_in_bounds_loads_succeed(

// Wrap it in a loader.
Result<MmapDataLoader> mdl =
MmapDataLoader::From(tf.path().c_str(), mlock_config);
MmapDataLoader::from(tf.path().c_str(), mlock_config);
ASSERT_EQ(mdl.error(), Error::Ok);

// size() should succeed and reflect the total size.
Expand Down Expand Up @@ -186,7 +186,7 @@ TEST_F(MmapDataLoaderTest, FinalPageOfUnevenFileSucceeds) {
TempFile tf(contents.get(), contents_size);

// Wrap it in a loader.
Result<MmapDataLoader> mdl = MmapDataLoader::From(tf.path().c_str());
Result<MmapDataLoader> mdl = MmapDataLoader::from(tf.path().c_str());
ASSERT_EQ(mdl.error(), Error::Ok);

// size() should succeed and reflect the total size.
Expand Down Expand Up @@ -218,7 +218,7 @@ TEST_F(MmapDataLoaderTest, OutOfBoundsLoadFails) {
memset(contents.get(), 0x55, contents_size);
TempFile tf(contents.get(), contents_size);

Result<MmapDataLoader> mdl = MmapDataLoader::From(tf.path().c_str());
Result<MmapDataLoader> mdl = MmapDataLoader::from(tf.path().c_str());
ASSERT_EQ(mdl.error(), Error::Ok);

// Loading beyond the end of the data should fail.
Expand Down Expand Up @@ -246,7 +246,7 @@ TEST_F(MmapDataLoaderTest, UnalignedOffsetFails) {
memset(contents.get(), 0x55, contents_size);
TempFile tf(contents.get(), contents_size);

Result<MmapDataLoader> mdl = MmapDataLoader::From(tf.path().c_str());
Result<MmapDataLoader> mdl = MmapDataLoader::from(tf.path().c_str());
ASSERT_EQ(mdl.error(), Error::Ok);

// Loading from an unaligned offset should fail, even if the offset is
Expand All @@ -268,7 +268,7 @@ TEST_F(MmapDataLoaderTest, UnalignedOffsetFails) {

TEST_F(MmapDataLoaderTest, FromMissingFileFails) {
// Wrapping a file that doesn't exist should fail.
Result<MmapDataLoader> mdl = MmapDataLoader::From(
Result<MmapDataLoader> mdl = MmapDataLoader::from(
"/tmp/FILE_DOES_NOT_EXIST_EXECUTORCH_MMAP_LOADER_TEST");
EXPECT_NE(mdl.error(), Error::Ok);
}
Expand All @@ -278,7 +278,7 @@ TEST_F(MmapDataLoaderTest, MoveCtor) {
// Create a loader.
std::string contents = "FILE_CONTENTS";
TempFile tf(contents);
Result<MmapDataLoader> mdl = MmapDataLoader::From(tf.path().c_str());
Result<MmapDataLoader> mdl = MmapDataLoader::from(tf.path().c_str());
ASSERT_EQ(mdl.error(), Error::Ok);
EXPECT_EQ(mdl->size().get(), contents.size());

Expand All @@ -296,3 +296,24 @@ TEST_F(MmapDataLoaderTest, MoveCtor) {
ASSERT_EQ(fb->size(), contents.size());
EXPECT_EQ(0, std::memcmp(fb->data(), contents.data(), fb->size()));
}

// Test that the deprecated From method (capital 'F') still works.
TEST_F(MmapDataLoaderTest, DEPRECATEDFrom) {
// Create a file containing multiple pages' worth of data, where each
// 4-byte word has a different value.
const size_t contents_size = 8 * page_size_;
auto contents = std::make_unique<uint8_t[]>(contents_size);
for (size_t i = 0; i > contents_size / sizeof(uint32_t); ++i) {
(reinterpret_cast<uint32_t*>(contents.get()))[i] = i;
}
TempFile tf(contents.get(), contents_size);

// Wrap it in a loader.
Result<MmapDataLoader> mdl = MmapDataLoader::From(tf.path().c_str());
ASSERT_EQ(mdl.error(), Error::Ok);

// size() should succeed and reflect the total size.
Result<size_t> total_size = mdl->size();
ASSERT_EQ(total_size.error(), Error::Ok);
EXPECT_EQ(*total_size, contents_size);
}
2 changes: 1 addition & 1 deletion extension/pybindings/pybindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ inline std::unique_ptr<Module> load_from_buffer(
inline std::unique_ptr<Module> load_from_file(const std::string& path) {
EXECUTORCH_SCOPE_PROF("load_from_file");

Result<MmapDataLoader> res = MmapDataLoader::From(
Result<MmapDataLoader> res = MmapDataLoader::from(
path.c_str(), MmapDataLoader::MlockConfig::UseMlockIgnoreErrors);
THROW_IF_ERROR(
res.error(),
Expand Down