|  | 
|  | 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 | 
0 commit comments