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