-
Notifications
You must be signed in to change notification settings - Fork 613
[executorch][runtime] Introduce PteDataMap for weight sharing #8887
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fea62dd
[executorch][runtime] Introduce PteDataMap for weight sharing
lucylq e7a42e1
Update on "[executorch][runtime] Introduce PteDataMap for weight shar…
lucylq 6218803
Update on "[executorch][runtime] Introduce PteDataMap for weight shar…
lucylq afb3839
Update on "[executorch][runtime] Introduce PteDataMap for weight shar…
lucylq 43484a2
Update on "[executorch][runtime] Introduce PteDataMap for weight shar…
lucylq a25d855
Update on "[executorch][runtime] Introduce PteDataMap for weight shar…
lucylq 6c4f055
Update on "[executorch][runtime] Introduce PteDataMap for weight shar…
lucylq 44d9570
Update on "[executorch][runtime] Introduce PteDataMap for weight shar…
lucylq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <executorch/runtime/executor/pte_data_map.h> | ||
#include <executorch/schema/program_generated.h> | ||
|
||
namespace executorch { | ||
namespace runtime { | ||
namespace internal { | ||
|
||
/* static */ executorch::runtime::Result<PteDataMap> PteDataMap::create( | ||
executorch::runtime::DataLoader* loader, | ||
size_t segment_base_offset, | ||
const flatbuffers::FlatbufferNamedData* named_data, | ||
const flatbuffers::FlatbufferDataSegment* segments) { | ||
ET_CHECK_OR_RETURN_ERROR( | ||
loader != nullptr && named_data != nullptr && segments != nullptr, | ||
InvalidArgument, | ||
"PteDataMap loader, named_data or segments is null; most likely the program does not have any named_data segments"); | ||
return PteDataMap(loader, segment_base_offset, named_data, segments); | ||
} | ||
|
||
ET_NODISCARD | ||
executorch::runtime::Result<executorch::runtime::FreeableBuffer> | ||
PteDataMap::get_data(const char* key) const { | ||
for (size_t i = 0; i < named_data_->size(); i++) { | ||
ET_CHECK_OR_RETURN_ERROR( | ||
named_data_->Get(i) != nullptr && named_data_->Get(i)->key() != nullptr, | ||
InvalidArgument, | ||
"Searching for key %s: NamedData at index %zu is null", | ||
key, | ||
i); | ||
if (strcmp(named_data_->Get(i)->key()->c_str(), key) == 0) { | ||
// Get the segment index. | ||
size_t segment_index = named_data_->Get(i)->segment_index(); | ||
|
||
// Get the segment offset and size. | ||
ET_CHECK_OR_RETURN_ERROR( | ||
segment_index < segments_->size(), | ||
InvalidArgument, | ||
"Segment index %zu for key %s is out of range for segments size %u", | ||
segment_index, | ||
key, | ||
segments_->size()); | ||
size_t segment_offset = segments_->Get(segment_index)->offset(); | ||
size_t segment_size = segments_->Get(segment_index)->size(); | ||
|
||
return loader_->load( | ||
/*offset=*/segment_base_offset_ + segment_offset, | ||
segment_size, | ||
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::External)); | ||
} | ||
} | ||
return Error::NotFound; | ||
} | ||
|
||
ET_NODISCARD executorch::runtime::Result<size_t> PteDataMap::get_num_keys() | ||
const { | ||
return named_data_->size(); | ||
} | ||
|
||
ET_NODISCARD executorch::runtime::Result<const char*> PteDataMap::get_key( | ||
size_t index) const { | ||
ET_CHECK_OR_RETURN_ERROR( | ||
index < named_data_->size(), | ||
InvalidArgument, | ||
"Index out of range: named_data size is %u, received index %zu", | ||
named_data_->size(), | ||
index); | ||
|
||
ET_CHECK_OR_RETURN_ERROR( | ||
named_data_->Get(index) != nullptr && | ||
named_data_->Get(index)->key() != nullptr, | ||
InvalidArgument, | ||
"NamedData at index %zu is null", | ||
index); | ||
return named_data_->Get(index)->key()->c_str(); | ||
} | ||
|
||
} // namespace internal | ||
} // namespace runtime | ||
} // namespace executorch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <executorch/runtime/core/data_loader.h> | ||
#include <executorch/runtime/core/named_data_map.h> | ||
|
||
// Forward declare flatbuffer types. This is a public header and must not | ||
// include the generated flatbuffer header. | ||
namespace executorch_flatbuffer { | ||
struct NamedData; | ||
struct DataSegment; | ||
} // namespace executorch_flatbuffer | ||
|
||
namespace flatbuffers { | ||
template <typename T> | ||
struct Offset; | ||
} // namespace flatbuffers | ||
|
||
// @lint-ignore CLANGTIDY facebook-modularize-issue-check | ||
#if EXECUTORCH_INTERNAL_FLATBUFFERS == 1 | ||
// TODO(T216992074): update internal flatbuffers (v1.12) to match OSS (v24.3.5). | ||
namespace flatbuffers { | ||
template <typename T> | ||
class Vector; | ||
using FlatbufferNamedData = | ||
flatbuffers::Vector<flatbuffers::Offset<executorch_flatbuffer::NamedData>>; | ||
using FlatbufferDataSegment = flatbuffers::Vector< | ||
flatbuffers::Offset<executorch_flatbuffer::DataSegment>>; | ||
} // namespace flatbuffers | ||
#else | ||
namespace flatbuffers { | ||
template <typename T, typename SizeT> | ||
class Vector; | ||
using FlatbufferNamedData = flatbuffers:: | ||
Vector<flatbuffers::Offset<executorch_flatbuffer::NamedData>, uint32_t>; | ||
using FlatbufferDataSegment = flatbuffers:: | ||
Vector<flatbuffers::Offset<executorch_flatbuffer::DataSegment>, uint32_t>; | ||
} // namespace flatbuffers | ||
#endif | ||
|
||
namespace executorch { | ||
namespace runtime { | ||
namespace internal { | ||
|
||
/** | ||
* A NamedDataMap implementation for Flatbuffer-serialized named data | ||
* originating from a PTE file. | ||
*/ | ||
class PteDataMap final : public NamedDataMap { | ||
public: | ||
/** | ||
* Creates a new DataMap that wraps named_data from the PTE file. | ||
* | ||
* @param[in] loader The DataLoader that accesses the PTE file. | ||
* Note: the loader must outlive the PteDataMap instance. | ||
* @param[in] segment_base_offset The offset to the first segment in the PTE | ||
* file, in bytes. | ||
* @param[in] named_data The named_data from the PTE file. Note: the pointer | ||
* passed here must outlive the PteDataMap instance. | ||
* @param[in] segments The segments from the PTE file. Note: the pointer | ||
* passed here must outlive the PteDataMap instance. | ||
*/ | ||
static Result<PteDataMap> create( | ||
DataLoader* loader, | ||
size_t segment_base_offset, | ||
const flatbuffers::FlatbufferNamedData* named_data, | ||
const flatbuffers::FlatbufferDataSegment* segments); | ||
|
||
/** | ||
* The PteDataMap currently only handles opaque data that does not contain | ||
* tensor-specific metadata. | ||
*/ | ||
ET_NODISCARD | ||
Result<const TensorLayout> get_metadata( | ||
ET_UNUSED const char* key) const override { | ||
return Error::NotImplemented; | ||
} | ||
|
||
/** | ||
* Retrieve read-only data for the specified key. | ||
* | ||
* @param[in] key The name of the blob to get data on. | ||
* | ||
* @return error if the key is not present or data cannot be loaded. | ||
*/ | ||
ET_NODISCARD | ||
Result<FreeableBuffer> get_data(const char* key) const override; | ||
|
||
/** | ||
* The PteDataMap currently does not implement load_into. | ||
*/ | ||
ET_NODISCARD Error load_data_into( | ||
ET_UNUSED const char* key, | ||
ET_UNUSED void* buffer, | ||
ET_UNUSED size_t size) const override { | ||
return Error::NotImplemented; | ||
} | ||
|
||
/** | ||
* @returns The number of keys in the map. | ||
*/ | ||
ET_NODISCARD Result<size_t> get_num_keys() const override; | ||
|
||
/** | ||
* @returns The key at the specified index, error if index out of bounds. | ||
*/ | ||
ET_NODISCARD Result<const char*> get_key(size_t index) const override; | ||
|
||
// Moveable, to be compatible with Result. | ||
PteDataMap(PteDataMap&&) noexcept = default; | ||
~PteDataMap() override = default; | ||
|
||
private: | ||
PteDataMap( | ||
DataLoader* loader, | ||
size_t segment_base_offset, | ||
const flatbuffers::FlatbufferNamedData* named_data, | ||
const flatbuffers::FlatbufferDataSegment* segments) | ||
: loader_(loader), | ||
segment_base_offset_(segment_base_offset), | ||
named_data_(named_data), | ||
segments_(segments) {} | ||
|
||
// Not copyable or assignable. | ||
PteDataMap(const PteDataMap& rhs) = delete; | ||
PteDataMap& operator=(PteDataMap&& rhs) noexcept = delete; | ||
PteDataMap& operator=(const PteDataMap& rhs) = delete; | ||
|
||
// Data loader, used to load segment data. | ||
DataLoader* loader_; | ||
|
||
// The offset to the first segment in the PTE file, in bytes. | ||
size_t segment_base_offset_; | ||
|
||
// Named data, containing name and segment index. | ||
const flatbuffers::FlatbufferNamedData* named_data_; | ||
|
||
// Segments, to retrieve offset and size for the loader. | ||
const flatbuffers::FlatbufferDataSegment* segments_; | ||
}; | ||
|
||
} // namespace internal | ||
} // namespace runtime | ||
} // namespace executorch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.