Skip to content

Commit 5dd16b1

Browse files
authored
[executorch][core] NamedDataMap interface
Differential Revision: D66834552 Pull Request resolved: #7763
1 parent b4af0df commit 5dd16b1

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

runtime/core/named_data_map.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
#include <executorch/runtime/core/exec_aten/exec_aten.h>
12+
#include <executorch/runtime/core/freeable_buffer.h>
13+
#include <executorch/runtime/core/result.h>
14+
#include <executorch/runtime/core/span.h>
15+
#include <executorch/runtime/core/tensor_layout.h>
16+
#include <executorch/runtime/platform/compiler.h>
17+
18+
namespace executorch {
19+
namespace runtime {
20+
21+
/**
22+
* Interface to access and retrieve data via name.
23+
* See executorch/extension/flat_tensor/ for an example.
24+
*/
25+
class ET_EXPERIMENTAL NamedDataMap {
26+
public:
27+
virtual ~NamedDataMap() = default;
28+
/**
29+
* Get metadata by key.
30+
*
31+
* @param key The name of the tensor.
32+
* @return Result containing TensorLayout with tensor metadata.
33+
*/
34+
ET_NODISCARD virtual Result<const executorch::runtime::TensorLayout>
35+
get_metadata(const char* key) const = 0;
36+
/**
37+
* Get data by key.
38+
*
39+
* @param key Name of the data.
40+
* @return Result containing a FreeableBuffer with the tensor data.
41+
*/
42+
ET_NODISCARD virtual Result<FreeableBuffer> get_data(
43+
const char* key) const = 0;
44+
45+
/**
46+
* Loads data corresponding to the key into the provided buffer.
47+
*
48+
* @param key The name of the data.
49+
* @param size The number of bytes to load. Use `get_metadata` to retrieve the
50+
* size of the data for a given key.
51+
* @param buffer The buffer to load the data into. Must point to at least
52+
* `size` bytes of memory.
53+
* @return Result containing the number of bytes written on success. This will
54+
* fail if the buffer is too small.
55+
*/
56+
ET_NODISCARD virtual Result<size_t>
57+
load_data_into(const char* key, void* buffer, size_t size) const = 0;
58+
59+
/**
60+
* Get the number of keys in the NamedDataMap.
61+
*
62+
* @return Result containing the number of keys.
63+
*/
64+
ET_NODISCARD virtual Result<size_t> get_num_keys() const = 0;
65+
66+
/**
67+
* Get the key at the given index.
68+
*
69+
* @param index The index of the key to retrieve.
70+
* @return Result containing the key at the given index. Note: the returned
71+
* pointer is only valid for the lifetime of the DataMap.
72+
*/
73+
ET_NODISCARD virtual Result<const char*> get_key(size_t index) const = 0;
74+
};
75+
76+
} // namespace runtime
77+
} // namespace executorch

runtime/core/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def define_common_targets():
4141
"defines.h",
4242
"error.h",
4343
"freeable_buffer.h",
44+
"named_data_map.h",
4445
"result.h",
4546
"span.h",
4647
"tensor_layout.h",

0 commit comments

Comments
 (0)