-
Notifications
You must be signed in to change notification settings - Fork 681
Introduce public MergedDataMap #14766
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
Open
lucylq
wants to merge
24
commits into
gh/lucylq/118/base
Choose a base branch
from
gh/lucylq/118/head
base: gh/lucylq/118/base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d5a5295
Introduce public MergedDataMap
lucylq a00bab7
Update on "Introduce public MergedDataMap"
lucylq 3aad6a9
Update on "Introduce public MergedDataMap"
lucylq a9c4c55
Update on "Introduce public MergedDataMap"
lucylq 3843756
Update on "Introduce public MergedDataMap"
lucylq f19b922
Update on "Introduce public MergedDataMap"
lucylq cb5f30d
Update on "Introduce public MergedDataMap"
lucylq e2592ae
Update on "Introduce public MergedDataMap"
lucylq 041a65a
Update on "Introduce public MergedDataMap"
lucylq a79a486
Update on "Introduce public MergedDataMap"
lucylq 4896344
Update on "Introduce public MergedDataMap"
lucylq 75ebd26
Update on "Introduce public MergedDataMap"
lucylq bae6c44
Update on "Introduce public MergedDataMap"
lucylq 8f475c1
Update on "Introduce public MergedDataMap"
lucylq 4fe31ba
Update on "Introduce public MergedDataMap"
lucylq 47383bf
Update on "Introduce public MergedDataMap"
lucylq 9f1378c
Update on "Introduce public MergedDataMap"
lucylq 53faa4a
Update on "Introduce public MergedDataMap"
lucylq 836dc33
Update on "Introduce public MergedDataMap"
lucylq 29778bd
Update on "Introduce public MergedDataMap"
lucylq d93fbb9
Update on "Introduce public MergedDataMap"
lucylq ef0d885
Update on "Introduce public MergedDataMap"
lucylq 076102f
Update on "Introduce public MergedDataMap"
lucylq 2499d6c
Update on "Introduce public MergedDataMap"
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Any targets that should be shared between fbcode and xplat must be defined in | ||
# targets.bzl. This file can contain fbcode-only targets. | ||
|
||
load(":targets.bzl", "define_common_targets") | ||
|
||
oncall("executorch") | ||
|
||
define_common_targets() |
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,117 @@ | ||
/* | ||
* 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/extension/named_data_map/merged_data_map.h> | ||
|
||
#include <executorch/runtime/core/data_loader.h> | ||
|
||
#include <vector> | ||
|
||
using executorch::aten::string_view; | ||
using executorch::ET_RUNTIME_NAMESPACE::NamedDataMap; | ||
using executorch::ET_RUNTIME_NAMESPACE::TensorLayout; | ||
using executorch::runtime::Error; | ||
using executorch::runtime::FreeableBuffer; | ||
using executorch::runtime::Result; | ||
|
||
namespace executorch { | ||
namespace extension { | ||
|
||
/*static*/ Result<MergedDataMap> MergedDataMap::load( | ||
std::vector<const NamedDataMap*> named_data_maps) { | ||
std::vector<const NamedDataMap*> valid_data_maps; | ||
for (size_t i = 0; i < named_data_maps.size(); i++) { | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (named_data_maps[i] != nullptr && | ||
named_data_maps[i]->get_num_keys().get() > 0) { | ||
valid_data_maps.push_back(named_data_maps[i]); | ||
} | ||
} | ||
ET_CHECK_OR_RETURN_ERROR( | ||
valid_data_maps.size() > 0, | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
InvalidArgument, | ||
"No non-empty named data maps provided to merge"); | ||
|
||
// Check for duplicate keys. | ||
std::unordered_map<std::string, uint32_t> key_to_map_index; | ||
for (uint32_t i = 0; i < valid_data_maps.size(); i++) { | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const auto cur_map = valid_data_maps[i]; | ||
uint32_t num_keys = cur_map->get_num_keys().get(); | ||
for (uint32_t j = 0; j < num_keys; ++j) { | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const auto cur_key = cur_map->get_key(j).get(); | ||
ET_CHECK_OR_RETURN_ERROR( | ||
key_to_map_index.find(cur_key) == key_to_map_index.end(), | ||
InvalidArgument, | ||
"Duplicate key %s in named data maps at index %u and %u", | ||
cur_key, | ||
key_to_map_index.at(cur_key), | ||
i); | ||
key_to_map_index[cur_key] = i; | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
return MergedDataMap(std::move(valid_data_maps), std::move(key_to_map_index)); | ||
} | ||
|
||
ET_NODISCARD Result<const TensorLayout> MergedDataMap::get_tensor_layout( | ||
string_view key) const { | ||
ET_CHECK_OR_RETURN_ERROR( | ||
key_to_map_index_.find(key.data()) != key_to_map_index_.end(), | ||
NotFound, | ||
"Key %s not found in named data maps", | ||
key.data()); | ||
|
||
return named_data_maps_.at(key_to_map_index_.at(key.data())) | ||
->get_tensor_layout(key); | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
ET_NODISCARD | ||
Result<FreeableBuffer> MergedDataMap::get_data(string_view key) const { | ||
ET_CHECK_OR_RETURN_ERROR( | ||
key_to_map_index_.find(key.data()) != key_to_map_index_.end(), | ||
NotFound, | ||
"Key %s not found in named data maps", | ||
key.data()); | ||
return named_data_maps_.at(key_to_map_index_.at(key.data()))->get_data(key); | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
ET_NODISCARD Error MergedDataMap::load_data_into( | ||
string_view key, | ||
void* buffer, | ||
size_t size) const { | ||
ET_CHECK_OR_RETURN_ERROR( | ||
key_to_map_index_.find(key.data()) != key_to_map_index_.end(), | ||
NotFound, | ||
"Key %s not found in named data maps", | ||
key.data()); | ||
return named_data_maps_.at(key_to_map_index_.at(key.data())) | ||
->load_data_into(key, buffer, size); | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
ET_NODISCARD Result<uint32_t> MergedDataMap::get_num_keys() const { | ||
return key_to_map_index_.size(); | ||
} | ||
|
||
ET_NODISCARD Result<const char*> MergedDataMap::get_key(uint32_t index) const { | ||
uint32_t total_num_keys = get_num_keys().get(); | ||
ET_CHECK_OR_RETURN_ERROR( | ||
index >= 0 && index < total_num_keys, | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
InvalidArgument, | ||
"Index %u out of range of size %u", | ||
index, | ||
total_num_keys); | ||
for (size_t i = 0; i < named_data_maps_.size(); i++) { | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
auto num_keys = named_data_maps_[i]->get_num_keys().get(); | ||
if (index < num_keys) { | ||
return named_data_maps_[i]->get_key(index); | ||
} | ||
index -= num_keys; | ||
} | ||
// Shouldn't reach here. | ||
return Error::Internal; | ||
} | ||
} // namespace extension | ||
} // 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,107 @@ | ||
/* | ||
* 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/named_data_map.h> | ||
|
||
#include <vector> | ||
|
||
namespace executorch { | ||
namespace extension { | ||
/** | ||
* A NamedDataMap implementation that wraps other NamedDataMaps. | ||
*/ | ||
class MergedDataMap final | ||
: public executorch::ET_RUNTIME_NAMESPACE::NamedDataMap { | ||
public: | ||
/** | ||
* Creates a new NamedDataMap that takes in other data maps. | ||
* | ||
* @param[in] data_maps vector of NamedDataMap pointers to merge. | ||
* Note: the data maps must outlive the MergedDataMap instance. | ||
*/ | ||
static executorch::runtime::Result<MergedDataMap> load( | ||
std::vector<const executorch::ET_RUNTIME_NAMESPACE::NamedDataMap*> | ||
lucylq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
named_data_maps); | ||
|
||
/** | ||
* Retrieve the tensor_layout for the specified key. | ||
* | ||
* @param[in] key The name of the tensor to get metadata on. | ||
* | ||
* @return Error::NotFound if the key is not present. | ||
*/ | ||
ET_NODISCARD | ||
executorch::runtime::Result< | ||
const executorch::ET_RUNTIME_NAMESPACE::TensorLayout> | ||
get_tensor_layout(executorch::aten::string_view key) const override; | ||
|
||
/** | ||
* Retrieve read-only data for the specified key. | ||
* | ||
* @param[in] key The name of the tensor to get data on. | ||
* | ||
* @return error if the key is not present or data cannot be loaded. | ||
*/ | ||
ET_NODISCARD | ||
executorch::runtime::Result<executorch::runtime::FreeableBuffer> get_data( | ||
executorch::aten::string_view key) const override; | ||
|
||
/** | ||
* Loads the data of the specified tensor into the provided buffer. | ||
* | ||
* @param[in] key The name of the tensor to get the data of. | ||
* @param[in] buffer The buffer to load data into. Must point to at least | ||
* `size` bytes of memory. | ||
* @param[in] size The number of bytes to load. | ||
* | ||
* @returns an Error indicating if the load was successful. | ||
*/ | ||
ET_NODISCARD executorch::runtime::Error load_data_into( | ||
executorch::aten::string_view key, | ||
void* buffer, | ||
size_t size) const override; | ||
|
||
/** | ||
* @returns The number of keys in the map. | ||
*/ | ||
ET_NODISCARD executorch::runtime::Result<uint32_t> get_num_keys() | ||
const override; | ||
/** | ||
* @returns The key at the specified index, error if index out of bounds. | ||
*/ | ||
ET_NODISCARD executorch::runtime::Result<const char*> get_key( | ||
uint32_t index) const override; | ||
|
||
MergedDataMap(MergedDataMap&&) noexcept = default; | ||
|
||
~MergedDataMap() override = default; | ||
|
||
private: | ||
MergedDataMap( | ||
std::vector<const executorch::ET_RUNTIME_NAMESPACE::NamedDataMap*> | ||
named_data_maps, | ||
std::unordered_map<std::string, uint32_t> key_to_map_index) | ||
: named_data_maps_(std::move(named_data_maps)), | ||
key_to_map_index_(std::move(key_to_map_index)) {} | ||
|
||
// Not copyable or assignable. | ||
MergedDataMap(const MergedDataMap& rhs) = delete; | ||
MergedDataMap& operator=(MergedDataMap&& rhs) noexcept = delete; | ||
MergedDataMap& operator=(const MergedDataMap& rhs) = delete; | ||
|
||
std::vector<const executorch::ET_RUNTIME_NAMESPACE::NamedDataMap*> | ||
named_data_maps_; | ||
|
||
// Map from key to index in the named_data_maps_ vector. | ||
std::unordered_map<std::string, uint32_t> key_to_map_index_; | ||
}; | ||
|
||
} // namespace extension | ||
} // 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,21 @@ | ||
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "get_aten_mode_options", "runtime") | ||
|
||
def define_common_targets(): | ||
for aten_mode in get_aten_mode_options(): | ||
aten_suffix = "_aten" if aten_mode else "" | ||
runtime.cxx_library( | ||
name = "merged_data_map" + aten_suffix, | ||
srcs = [ | ||
"merged_data_map.cpp", | ||
], | ||
exported_headers = [ | ||
"merged_data_map.h", | ||
], | ||
visibility = [ | ||
"@EXECUTORCH_CLIENTS", | ||
], | ||
deps = [ | ||
"//executorch/runtime/core:named_data_map", | ||
"//executorch/runtime/core:core", | ||
], | ||
) |
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,8 @@ | ||
# Any targets that should be shared between fbcode and xplat must be defined in | ||
# targets.bzl. This file can contain fbcode-only targets. | ||
|
||
load(":targets.bzl", "define_common_targets") | ||
|
||
oncall("executorch") | ||
|
||
define_common_targets(is_fbcode=True) |
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.