Skip to content

Commit b8f4a78

Browse files
committed
Update on "[executorch][flat_tensor] DataMap implementation"
DataMap implementation that * Loads a flat_tensor file * Populates a map with {fqn: tensor} and {fqn: TensorLayout}. * Makes tensor information available via the named_data_map.h interface. For now, DataMap doesn't store the DataLoader. - If/when tensors are in their own segments, DataMap should also store a DataLoader. Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/) [ghstack-poisoned]
1 parent 87eded3 commit b8f4a78

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

extension/flat_tensor/named_data_map/data_map.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
* This source code is licensed under the BSD-style license found in the
66
* LICENSE file in the root directory of this source tree.
77
*/
8+
89
#include <executorch/extension/flat_tensor/named_data_map/data_map.h>
910
#include <executorch/extension/flat_tensor/serialize/flat_tensor_header.h>
1011
#include <executorch/extension/flat_tensor/serialize/schema_generated.h>
11-
1212
#include <executorch/runtime/core/error.h>
13+
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
1314
#include <executorch/runtime/core/freeable_buffer.h>
1415
#include <executorch/runtime/core/result.h>
1516
#include <executorch/runtime/core/span.h>
1617
#include <executorch/runtime/platform/compiler.h>
1718

18-
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
19-
2019
#include <tuple>
2120
#include <unordered_map>
2221

@@ -48,16 +47,16 @@ bool IsAligned(const void* data) {
4847

4948
ET_NODISCARD Result<const TensorLayout> DataMap::get_metadata(
5049
const char* fqn) const {
51-
auto result = _tensor_map.find(fqn);
52-
if (result == _tensor_map.end()) {
50+
auto result = _name_to_tensor.find(fqn);
51+
if (result == _name_to_tensor.end()) {
5352
return Error::NotFound;
5453
}
5554
return std::get<2>(result->second);
5655
}
5756

5857
ET_NODISCARD Result<FreeableBuffer> DataMap::get_data(const char* fqn) const {
59-
auto result = _tensor_map.find(fqn);
60-
if (result == _tensor_map.end()) {
58+
auto result = _name_to_tensor.find(fqn);
59+
if (result == _name_to_tensor.end()) {
6160
return Error::NotFound;
6261
}
6362
int offset = std::get<1>(result->second);
@@ -68,15 +67,15 @@ ET_NODISCARD Result<FreeableBuffer> DataMap::get_data(const char* fqn) const {
6867
}
6968

7069
ET_NODISCARD Result<int> DataMap::get_num_keys() const {
71-
return _tensor_map.size();
70+
return _name_to_tensor.size();
7271
}
7372

7473
ET_NODISCARD Result<const char*> DataMap::get_key(int index) const {
75-
if (index <= 0 || index >= _tensor_map.size()) {
74+
if (index <= 0 || index >= _name_to_tensor.size()) {
7675
return Error::InvalidArgument;
7776
}
7877

79-
auto iter = _tensor_map.begin();
78+
auto iter = _name_to_tensor.begin();
8079
for (int i = 0; i < index; ++i) {
8180
++iter;
8281
}

extension/flat_tensor/named_data_map/data_map.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88

99
#pragma once
1010

11-
#include <cstddef>
12-
1311
#include <executorch/runtime/core/data_loader.h>
12+
#include <executorch/runtime/core/exec_aten/exec_aten.h>
1413
#include <executorch/runtime/core/named_data_map.h>
1514
#include <executorch/runtime/core/result.h>
15+
#include <executorch/runtime/core/tensor_layout.h>
1616
#include <executorch/runtime/platform/compiler.h>
1717

18-
#include <executorch/runtime/core/exec_aten/exec_aten.h>
19-
20-
#include <executorch/runtime/core/tensor_layout.h>
2118
#include <unordered_map>
2219
#include <utility>
2320

@@ -54,10 +51,11 @@ class DataMap final : public executorch::runtime::NamedDataMap {
5451
executorch::runtime::FreeableBuffer&& flat_tensor_data,
5552
std::unordered_map<
5653
std::string,
57-
std::tuple<int, int, executorch::runtime::TensorLayout>> tensor_map,
54+
std::tuple<int, int, executorch::runtime::TensorLayout>>
55+
name_to_tensor,
5856
executorch::runtime::FreeableBuffer&& data_ro)
5957
: _flat_tensor_data(std::move(flat_tensor_data)),
60-
_tensor_map(std::move(tensor_map)),
58+
_name_to_tensor(std::move(name_to_tensor)),
6159
_data_ro(std::move(data_ro)) {}
6260

6361
// Not copyable or assignable.
@@ -66,14 +64,14 @@ class DataMap final : public executorch::runtime::NamedDataMap {
6664
DataMap& operator=(const DataMap& rhs) = delete;
6765

6866
// FlatTensor flatbuffer data. Contains the data backing up
69-
// TensorLayout information in the _tensor_map; must outlive it.
67+
// TensorLayout information in the _name_to_tensor map; must outlive it.
7068
executorch::runtime::FreeableBuffer _flat_tensor_data;
7169

72-
// Map of fqn to {segment index, offset, TensorLayout}.
70+
// Map of name to {segment index, offset, TensorLayout}.
7371
std::unordered_map<
7472
std::string,
7573
std::tuple<int, int, executorch::runtime::TensorLayout>>
76-
_tensor_map;
74+
_name_to_tensor;
7775

7876
// Raw, read-only tensor data.
7977
executorch::runtime::FreeableBuffer _data_ro;

extension/flat_tensor/test/data_map_test.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#include <executorch/extension/flat_tensor/named_data_map/data_map.h>
10-
119
#include <executorch/extension/data_loader/buffer_data_loader.h>
1210
#include <executorch/extension/data_loader/file_data_loader.h>
11+
#include <executorch/extension/flat_tensor/named_data_map/data_map.h>
12+
#include <executorch/extension/flat_tensor/serialize/flat_tensor_header.h>
13+
#include <executorch/extension/flat_tensor/serialize/schema_generated.h>
1314
#include <executorch/runtime/core/error.h>
1415
#include <executorch/runtime/core/result.h>
1516
#include <executorch/runtime/platform/runtime.h>
1617
#include <executorch/test/utils/DeathTest.h>
1718

18-
#include <executorch/extension/flat_tensor/serialize/flat_tensor_header.h>
19-
#include <executorch/extension/flat_tensor/serialize/schema_generated.h>
20-
2119
#include <gtest/gtest.h>
2220

2321
using namespace ::testing;

extension/flat_tensor/test/targets.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ def define_common_targets(is_fbcode=False):
3232
"data_map_test.cpp",
3333
],
3434
deps = [
35-
"//executorch/extension/flat_tensor/serialize:schema",
36-
"//executorch/extension/flat_tensor/serialize:generated_headers",
37-
"//executorch/extension/flat_tensor/named_data_map:data_map",
38-
"//executorch/extension/flat_tensor/serialize:flat_tensor_header",
3935
"//executorch/extension/data_loader:buffer_data_loader",
4036
"//executorch/extension/data_loader:file_data_loader",
37+
"//executorch/extension/flat_tensor/named_data_map:data_map",
38+
"//executorch/extension/flat_tensor/serialize:flat_tensor_header",
39+
"//executorch/extension/flat_tensor/serialize:generated_headers",
40+
"//executorch/extension/flat_tensor/serialize:schema",
4141
"//executorch/runtime/core/exec_aten:lib",
4242
],
4343
env = modules_env,

0 commit comments

Comments
 (0)