|
10 | 10 |
|
11 | 11 | #pragma once |
12 | 12 | #include <executorch/runtime/platform/compiler.h> |
| 13 | +#include <cstddef> |
13 | 14 | #include <cstdint> |
| 15 | +#include <variant> |
14 | 16 | #include <vector> |
15 | 17 |
|
| 18 | +#include <executorch/extension/tensor/tensor.h> |
| 19 | +#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> |
| 20 | + |
16 | 21 | namespace executorch { |
17 | 22 | namespace extension { |
18 | 23 | namespace llm { |
19 | 24 |
|
20 | | -struct ET_EXPERIMENTAL Image { |
| 25 | +class ET_EXPERIMENTAL Image { |
| 26 | + public: |
| 27 | + // Default constructor |
| 28 | + Image() : width_(0), height_(0), channels_(0) {} |
| 29 | + |
| 30 | + // Constructor for uint8_t data |
| 31 | + Image( |
| 32 | + std::vector<uint8_t>&& data, |
| 33 | + int32_t width, |
| 34 | + int32_t height, |
| 35 | + int32_t channels) |
| 36 | + : data_(std::move(data)), |
| 37 | + width_(width), |
| 38 | + height_(height), |
| 39 | + channels_(channels) {} |
| 40 | + |
| 41 | + // Constructor for float data |
| 42 | + Image( |
| 43 | + std::vector<float>&& data, |
| 44 | + int32_t width, |
| 45 | + int32_t height, |
| 46 | + int32_t channels) |
| 47 | + : data_(std::move(data)), |
| 48 | + width_(width), |
| 49 | + height_(height), |
| 50 | + channels_(channels) {} |
| 51 | + |
| 52 | + // Getters |
| 53 | + int32_t width() const { |
| 54 | + return width_; |
| 55 | + } |
| 56 | + int32_t height() const { |
| 57 | + return height_; |
| 58 | + } |
| 59 | + int32_t channels() const { |
| 60 | + return channels_; |
| 61 | + } |
| 62 | + |
| 63 | + // Data access |
| 64 | + bool is_uint8() const { |
| 65 | + return std::holds_alternative<std::vector<uint8_t>>(data_); |
| 66 | + } |
| 67 | + |
| 68 | + bool is_float() const { |
| 69 | + return std::holds_alternative<std::vector<float>>(data_); |
| 70 | + } |
| 71 | + |
| 72 | + const std::vector<uint8_t>& get_uint8_data() const& { |
| 73 | + return std::get<std::vector<uint8_t>>(data_); |
| 74 | + } |
| 75 | + |
| 76 | + std::vector<uint8_t>& get_uint8_data() & { |
| 77 | + return std::get<std::vector<uint8_t>>(data_); |
| 78 | + } |
| 79 | + |
| 80 | + const std::vector<float>& get_float_data() const& { |
| 81 | + return std::get<std::vector<float>>(data_); |
| 82 | + } |
| 83 | + |
| 84 | + std::vector<float>& get_float_data() & { |
| 85 | + return std::get<std::vector<float>>(data_); |
| 86 | + } |
| 87 | + |
| 88 | + executorch::runtime::Result<executorch::extension::TensorPtr> toTensor( |
| 89 | + bool with_batch = false) const { |
| 90 | + // Note: This creates a 3D tensor (CHW). The model might expect a 4D |
| 91 | + // tensor (NCHW). The caller should handle reshaping if needed. |
| 92 | + std::vector<executorch::aten::SizesType> sizes = { |
| 93 | + channels(), height(), width()}; |
| 94 | + if (with_batch) { |
| 95 | + sizes.insert(sizes.begin(), 1); |
| 96 | + } |
| 97 | + if (is_float()) { |
| 98 | + return executorch::extension::from_blob( |
| 99 | + const_cast<float*>(get_float_data().data()), |
| 100 | + sizes, |
| 101 | + ::executorch::aten::ScalarType::Float); |
| 102 | + } else if (is_uint8()) { |
| 103 | + return executorch::extension::from_blob( |
| 104 | + const_cast<uint8_t*>(get_uint8_data().data()), |
| 105 | + sizes, |
| 106 | + ::executorch::aten::ScalarType::Byte); |
| 107 | + } |
| 108 | + ET_LOG( |
| 109 | + Error, "Image data is not initialized with uint8_t or float vector."); |
| 110 | + return ::executorch::runtime::Error::NotSupported; |
| 111 | + } |
| 112 | + |
| 113 | + private: |
21 | 114 | // Assuming NCHW format |
22 | | - std::vector<uint8_t> data; |
23 | | - int32_t width; |
24 | | - int32_t height; |
25 | | - int32_t channels; |
| 115 | + std::variant<std::vector<uint8_t>, std::vector<float>> data_; |
| 116 | + int32_t width_; |
| 117 | + int32_t height_; |
| 118 | + int32_t channels_; |
26 | 119 | }; |
27 | 120 |
|
28 | 121 | } // namespace llm |
|
0 commit comments