|
| 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 | +// A generic multimodal input class that can hold either image or text data. |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include <executorch/extension/llm/runner/image.h> |
| 14 | +#include <executorch/runtime/platform/compiler.h> |
| 15 | +#include <string> |
| 16 | +#include <variant> |
| 17 | + |
| 18 | +namespace executorch { |
| 19 | +namespace extension { |
| 20 | +namespace llm { |
| 21 | + |
| 22 | +/** |
| 23 | + * A generic class to hold either image or text data for multimodal inputs. |
| 24 | + * This allows the generate() API to take a std::vector of these objects |
| 25 | + * instead of separate image and text parameters. |
| 26 | + */ |
| 27 | +class ET_EXPERIMENTAL MultimodalInput { |
| 28 | + public: |
| 29 | + enum class Type { TEXT, IMAGE }; |
| 30 | + |
| 31 | + // Constructors |
| 32 | + explicit MultimodalInput(const std::string& text) : data_(text) {} |
| 33 | + explicit MultimodalInput(std::string&& text) : data_(std::move(text)) {} |
| 34 | + explicit MultimodalInput(const Image& image) : data_(image) {} |
| 35 | + explicit MultimodalInput(Image&& image) : data_(std::move(image)) {} |
| 36 | + |
| 37 | + // Copy constructor and assignment |
| 38 | + MultimodalInput(const MultimodalInput& other) = default; |
| 39 | + MultimodalInput& operator=(const MultimodalInput& other) = default; |
| 40 | + |
| 41 | + // Move constructor and assignment |
| 42 | + MultimodalInput(MultimodalInput&& other) noexcept = default; |
| 43 | + MultimodalInput& operator=(MultimodalInput&& other) noexcept = default; |
| 44 | + |
| 45 | + // Destructor |
| 46 | + ~MultimodalInput() = default; |
| 47 | + |
| 48 | + /** |
| 49 | + * Check if this input contains text data. |
| 50 | + * @return true if this input contains text, false otherwise. |
| 51 | + */ |
| 52 | + bool is_text() const { |
| 53 | + return std::holds_alternative<std::string>(data_); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Check if this input contains image data. |
| 58 | + * @return true if this input contains an image, false otherwise. |
| 59 | + */ |
| 60 | + bool is_image() const { |
| 61 | + return std::holds_alternative<Image>(data_); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get the type of data stored in this input. |
| 66 | + * @return Type::TEXT if text data, Type::IMAGE if image data. |
| 67 | + */ |
| 68 | + Type get_type() const { |
| 69 | + return is_text() ? Type::TEXT : Type::IMAGE; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get the text data from this input. |
| 74 | + * @return Reference to the stored text string. |
| 75 | + * @throws std::bad_variant_access if this input doesn't contain text. |
| 76 | + */ |
| 77 | + const std::string& get_text() const { |
| 78 | + return std::get<std::string>(data_); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Get the text data from this input (mutable version). |
| 83 | + * @return Mutable reference to the stored text string. |
| 84 | + * @throws std::bad_variant_access if this input doesn't contain text. |
| 85 | + */ |
| 86 | + std::string& get_text() { |
| 87 | + return std::get<std::string>(data_); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get the image data from this input. |
| 92 | + * @return Reference to the stored Image object. |
| 93 | + * @throws std::bad_variant_access if this input doesn't contain an image. |
| 94 | + */ |
| 95 | + const Image& get_image() const { |
| 96 | + return std::get<Image>(data_); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the image data from this input (mutable version). |
| 101 | + * @return Mutable reference to the stored Image object. |
| 102 | + * @throws std::bad_variant_access if this input doesn't contain an image. |
| 103 | + */ |
| 104 | + Image& get_image() { |
| 105 | + return std::get<Image>(data_); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Try to get the text data from this input safely. |
| 110 | + * @return Pointer to the text string if this input contains text, nullptr |
| 111 | + * otherwise. |
| 112 | + */ |
| 113 | + const std::string* try_get_text() const { |
| 114 | + return std::get_if<std::string>(&data_); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Try to get the text data from this input safely (mutable version). |
| 119 | + * @return Pointer to the text string if this input contains text, nullptr |
| 120 | + * otherwise. |
| 121 | + */ |
| 122 | + std::string* try_get_text() { |
| 123 | + return std::get_if<std::string>(&data_); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Try to get the image data from this input safely. |
| 128 | + * @return Pointer to the Image object if this input contains an image, |
| 129 | + * nullptr otherwise. |
| 130 | + */ |
| 131 | + const Image* try_get_image() const { |
| 132 | + return std::get_if<Image>(&data_); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Try to get the image data from this input safely (mutable version). |
| 137 | + * @return Pointer to the Image object if this input contains an image, |
| 138 | + * nullptr otherwise. |
| 139 | + */ |
| 140 | + Image* try_get_image() { |
| 141 | + return std::get_if<Image>(&data_); |
| 142 | + } |
| 143 | + |
| 144 | + private: |
| 145 | + std::variant<std::string, Image> data_; |
| 146 | +}; |
| 147 | + |
| 148 | +// Convenience factory functions |
| 149 | +inline MultimodalInput make_text_input(const std::string& text) { |
| 150 | + return MultimodalInput(text); |
| 151 | +} |
| 152 | + |
| 153 | +inline MultimodalInput make_text_input(std::string&& text) { |
| 154 | + return MultimodalInput(std::move(text)); |
| 155 | +} |
| 156 | + |
| 157 | +inline MultimodalInput make_image_input(const Image& image) { |
| 158 | + return MultimodalInput(image); |
| 159 | +} |
| 160 | + |
| 161 | +inline MultimodalInput make_image_input(Image&& image) { |
| 162 | + return MultimodalInput(std::move(image)); |
| 163 | +} |
| 164 | + |
| 165 | +} // namespace llm |
| 166 | +} // namespace extension |
| 167 | +} // namespace executorch |
0 commit comments