|
| 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/data_loader.h> |
| 12 | +#include <executorch/runtime/core/named_data_map.h> |
| 13 | + |
| 14 | +// Forward declare flatbuffer types. This is a public header and must not |
| 15 | +// include the generated flatbuffer header. |
| 16 | +namespace flatbuffers { |
| 17 | +template <typename T> |
| 18 | +class Vector; |
| 19 | +template <typename T> |
| 20 | +struct Offset; |
| 21 | +} // namespace flatbuffers |
| 22 | + |
| 23 | +namespace executorch_flatbuffer { |
| 24 | +struct NamedData; |
| 25 | +struct DataSegment; |
| 26 | +} // namespace executorch_flatbuffer |
| 27 | + |
| 28 | +namespace executorch { |
| 29 | +namespace runtime { |
| 30 | +namespace internal { |
| 31 | + |
| 32 | +/** |
| 33 | + * A NamedDataMap implementation for Flatbuffer-serialized named data |
| 34 | + * originating from a PTE file. |
| 35 | + */ |
| 36 | +class PteDataMap final : public NamedDataMap { |
| 37 | + public: |
| 38 | + /** |
| 39 | + * Creates a new DataMap that wraps named_data from the PTE file. |
| 40 | + * |
| 41 | + * @param[in] loader The DataLoader that accesses the PTE file. |
| 42 | + * Note: the loader must outlive the PteDataMap instance. |
| 43 | + * @param[in] segment_base_offset The offset to the first segment in the PTE |
| 44 | + * file, in bytes. |
| 45 | + * @param[in] named_data The named_data from the PTE file. Note: the pointer |
| 46 | + * passed here must outlive the PteDataMap instance. |
| 47 | + * @param[in] segments The segments from the PTE file. Note: the pointer |
| 48 | + * passed here must outlive the PteDataMap instance. |
| 49 | + */ |
| 50 | + static Result<PteDataMap> create( |
| 51 | + DataLoader* loader, |
| 52 | + size_t segment_base_offset, |
| 53 | + const flatbuffers::Vector< |
| 54 | + flatbuffers::Offset<executorch_flatbuffer::NamedData>>* named_data, |
| 55 | + const flatbuffers::Vector< |
| 56 | + flatbuffers::Offset<executorch_flatbuffer::DataSegment>>* segments); |
| 57 | + |
| 58 | + /** |
| 59 | + * The PteDataMap currently only handles opaque data that does not contain |
| 60 | + * tensor-specific metadata. |
| 61 | + */ |
| 62 | + ET_NODISCARD |
| 63 | + Result<const TensorLayout> get_metadata( |
| 64 | + ET_UNUSED const char* key) const override { |
| 65 | + return Error::NotImplemented; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Retrieve read-only data for the specified key. |
| 70 | + * |
| 71 | + * @param[in] key The name of the blob to get data on. |
| 72 | + * |
| 73 | + * @return error if the key is not present or data cannot be loaded. |
| 74 | + */ |
| 75 | + ET_NODISCARD |
| 76 | + Result<FreeableBuffer> get_data(const char* key) const override; |
| 77 | + |
| 78 | + /** |
| 79 | + * The PteDataMap currently does not implement load_into. |
| 80 | + */ |
| 81 | + ET_NODISCARD Error load_data_into( |
| 82 | + ET_UNUSED const char* key, |
| 83 | + ET_UNUSED void* buffer, |
| 84 | + ET_UNUSED size_t size) const override { |
| 85 | + return Error::NotImplemented; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @returns The number of keys in the map. |
| 90 | + */ |
| 91 | + ET_NODISCARD Result<size_t> get_num_keys() const override; |
| 92 | + |
| 93 | + /** |
| 94 | + * @returns The key at the specified index, error if index out of bounds. |
| 95 | + */ |
| 96 | + ET_NODISCARD Result<const char*> get_key(size_t index) const override; |
| 97 | + |
| 98 | + // Moveable, to be compatible with Result. |
| 99 | + PteDataMap(PteDataMap&&) noexcept = default; |
| 100 | + ~PteDataMap() override = default; |
| 101 | + |
| 102 | + private: |
| 103 | + PteDataMap( |
| 104 | + DataLoader* loader, |
| 105 | + size_t segment_base_offset, |
| 106 | + const flatbuffers::Vector< |
| 107 | + flatbuffers::Offset<executorch_flatbuffer::NamedData>>* named_data, |
| 108 | + const flatbuffers::Vector< |
| 109 | + flatbuffers::Offset<executorch_flatbuffer::DataSegment>>* segments) |
| 110 | + : loader_(loader), |
| 111 | + segment_base_offset_(segment_base_offset), |
| 112 | + named_data_(named_data), |
| 113 | + segments_(segments) {} |
| 114 | + |
| 115 | + // Not copyable or assignable. |
| 116 | + PteDataMap(const PteDataMap& rhs) = delete; |
| 117 | + PteDataMap& operator=(PteDataMap&& rhs) noexcept = delete; |
| 118 | + PteDataMap& operator=(const PteDataMap& rhs) = delete; |
| 119 | + |
| 120 | + // Data loader, used to load segment data. |
| 121 | + DataLoader* loader_; |
| 122 | + |
| 123 | + // The offset to the first segment in the PTE file, in bytes. |
| 124 | + size_t segment_base_offset_; |
| 125 | + |
| 126 | + // Named data, containing name and segment index. |
| 127 | + const flatbuffers::Vector< |
| 128 | + flatbuffers::Offset<executorch_flatbuffer::NamedData>>* named_data_; |
| 129 | + |
| 130 | + // Segments, to retrieve offset and size for the loader. |
| 131 | + const flatbuffers::Vector< |
| 132 | + flatbuffers::Offset<executorch_flatbuffer::DataSegment>>* segments_; |
| 133 | +}; |
| 134 | + |
| 135 | +} // namespace internal |
| 136 | +} // namespace runtime |
| 137 | +} // namespace executorch |
0 commit comments