Skip to content

Commit 540fa5d

Browse files
pytorchbotlucylq
andauthored
[BE] Clean pte_data_map (#12331)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: #12255 by @lucylq ^ Please use this as the source of truth for the PR details, comments, and reviews ghstack PR base: https://github.com/pytorch/executorch/tree/gh/lucylq/89/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/lucylq/89/head Merge bot PR base: https://github.com/pytorch/executorch/tree/main Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/lucylq/89/orig @diff-train-skip-merge Co-authored-by: lucylq <[email protected]>
1 parent aaf0a4c commit 540fa5d

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

runtime/executor/pte_data_map.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ namespace executorch {
1313
namespace ET_RUNTIME_NAMESPACE {
1414
namespace internal {
1515

16-
/* static */ executorch::runtime::Result<PteDataMap> PteDataMap::create(
17-
executorch::runtime::DataLoader* loader,
16+
/* static */ Result<PteDataMap> PteDataMap::create(
17+
DataLoader* loader,
1818
size_t segment_base_offset,
1919
const flatbuffers::FlatbufferNamedData* named_data,
2020
const flatbuffers::FlatbufferDataSegment* segments) {
@@ -26,22 +26,22 @@ namespace internal {
2626
}
2727

2828
ET_NODISCARD
29-
executorch::runtime::Result<executorch::runtime::FreeableBuffer>
30-
PteDataMap::get_data(executorch::aten::string_view key) const {
29+
Result<FreeableBuffer> PteDataMap::get_data(
30+
executorch::aten::string_view key) const {
3131
for (uint32_t i = 0; i < named_data_->size(); i++) {
32+
const auto* named_data_item = named_data_->Get(i);
3233
ET_CHECK_OR_RETURN_ERROR(
33-
named_data_->Get(i) != nullptr && named_data_->Get(i)->key() != nullptr,
34+
named_data_item != nullptr && named_data_item->key() != nullptr,
3435
InvalidArgument,
3536
"Searching for key %.*s: NamedData at index %d is null",
3637
static_cast<int>(key.size()),
3738
key.data(),
3839
i);
39-
if (strncmp(
40-
named_data_->Get(i)->key()->c_str(),
41-
key.data(),
42-
named_data_->Get(i)->key()->size()) == 0) {
40+
const auto* named_data_key = named_data_item->key();
41+
if (named_data_key->size() == key.size() &&
42+
memcmp(named_data_key->data(), key.data(), key.size()) == 0) {
4343
// Get the segment index.
44-
size_t segment_index = named_data_->Get(i)->segment_index();
44+
size_t segment_index = named_data_item->segment_index();
4545

4646
// Get the segment offset and size.
4747
ET_CHECK_OR_RETURN_ERROR(
@@ -54,7 +54,6 @@ PteDataMap::get_data(executorch::aten::string_view key) const {
5454
segments_->size());
5555
size_t segment_offset = segments_->Get(segment_index)->offset();
5656
size_t segment_size = segments_->Get(segment_index)->size();
57-
5857
return loader_->load(
5958
/*offset=*/segment_base_offset_ + segment_offset,
6059
segment_size,
@@ -64,27 +63,25 @@ PteDataMap::get_data(executorch::aten::string_view key) const {
6463
return Error::NotFound;
6564
}
6665

67-
ET_NODISCARD executorch::runtime::Result<uint32_t> PteDataMap::get_num_keys()
68-
const {
66+
ET_NODISCARD Result<uint32_t> PteDataMap::get_num_keys() const {
6967
return named_data_->size();
7068
}
7169

72-
ET_NODISCARD executorch::runtime::Result<const char*> PteDataMap::get_key(
73-
uint32_t index) const {
70+
ET_NODISCARD Result<const char*> PteDataMap::get_key(uint32_t index) const {
7471
ET_CHECK_OR_RETURN_ERROR(
7572
index < named_data_->size(),
7673
InvalidArgument,
7774
"Index out of range: named_data size is %u, received index %u",
7875
named_data_->size(),
7976
index);
8077

78+
const auto* item = named_data_->Get(index);
8179
ET_CHECK_OR_RETURN_ERROR(
82-
named_data_->Get(index) != nullptr &&
83-
named_data_->Get(index)->key() != nullptr,
80+
item != nullptr && item->key() != nullptr,
8481
InvalidArgument,
8582
"NamedData at index %u is null",
8683
index);
87-
return named_data_->Get(index)->key()->c_str();
84+
return item->key()->c_str();
8885
}
8986

9087
} // namespace internal

test/size_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ int main(int argc, char** argv) {
9595
// It assumes the outputs are all tensors.
9696
for (const auto i : c10::irange(method->outputs_size())) {
9797
auto output_tensor = output_list[i].toTensor();
98-
[[maybe_unused]] auto data_output = output_tensor.const_data_ptr<float>();
99-
for (const auto j : c10::irange(output_tensor.numel())) {
98+
ET_UNUSED auto data_output = output_tensor.const_data_ptr<float>();
99+
for (ET_UNUSED const auto j : c10::irange(output_tensor.numel())) {
100100
ET_LOG(Info, "%f", data_output[j]);
101101
}
102102
}

0 commit comments

Comments
 (0)