Repository: google/mediapipe
File: mediapipe/tasks/ios/components/containers/sources/MPPEmbedding.h (C/C++)

Code before:
// limitations under the License.

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
 * Represents the embedding for a given embedder head. Typically used in embedding tasks.
 *
 * One and only one of the two 'floatEmbedding' and 'quantizedEmbedding' will contain data, based on
 * whether or not the embedder was configured to perform scala quantization.
 */
NS_SWIFT_NAME(Embedding)
@interface MPPEmbedding : NSObject

/**
 * @brief The Floating-point embedding.
 * Empty if the embedder was configured to perform scalar quantization.
 */
@property(nonatomic, readonly, nullable) float *floatEmbedding;

/**
 * @brief The Quantized embedding.
 * Empty if the embedder was not configured to perform scalar quantization.
 */
@property(nonatomic, readonly, nullable) char *quantizedEmbedding;

/** The index of the embedder head these entries refer to. This is useful for multi-head models. */
@property(nonatomic, readonly) NSInteger headIndex;

/** The optional name of the embedder head, which is the corresponding tensor metadata name. */
@property(nonatomic, readonly, nullable) NSString *headName;

/**
 * Initializes a new `MPPEmbedding` with the given float embedding, quantized embedding, head index
 * and head name.
 *
 * @param floatEmbedding The optional Floating-point embedding.
 * @param quantizedEmbedding The optional Quantized embedding.
 * @param headIndex The index of the embedder head.
 * @param headName The optional name of the embedder head.
 *
 * @return An instance of `MPPEmbedding` initialized with the given float embedding, quantized
 * embedding, head index and head name.
 *
 */
- (instancetype)initWithFloatEmbedding:(nullable float *)floatEmbedding
                    quantizedEmbedding:(nullable char *)quantizedEmbedding
                             headIndex:(NSInteger)headIndex
                              headName:(nullable NSString *)headName NS_DESIGNATED_INITIALIZER;


Review comment (suggestion):
@schmidt-sebastian @khanhlvg `quantizedEmbedding` is of type bytes in the proto. It is converted to `std::string` in C++. I have not used NSString* because if any operations are to be performed with the embedding, then the consumers may have to convert it into a char* array. Instead I am using char * directly as the type here. Moreover the embedding may be long since this is intended to use with image embedder as well. In case of streaming operations, continuous conversion to NSString may present extra over head.

Code after:
// limitations under the License.

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
 * Represents the embedding for a given embedder head. Typically used in embedding tasks.
 *
 * One and only one of the two 'floatEmbedding' and 'quantizedEmbedding' will contain data, based on
 * whether or not the embedder was configured to perform scala quantization.
 */
NS_SWIFT_NAME(Embedding)
@interface MPPEmbedding : NSObject

/**
 * @brief The Floating-point embedding.
 * Empty if the embedder was configured to perform scalar quantization.
 */
@property(nonatomic, readonly, nullable) NSArray<NSNumber *> *floatEmbedding;

/**
 * @brief The Quantized embedding.
 * Empty if the embedder was not configured to perform scalar quantization.
 */
@property(nonatomic, readonly, nullable) NSData *quantizedEmbedding;

/** The index of the embedder head these entries refer to. This is useful for multi-head models. */
@property(nonatomic, readonly) NSInteger headIndex;

/** The optional name of the embedder head, which is the corresponding tensor metadata name. */
@property(nonatomic, readonly, nullable) NSString *headName;

/**
 * Initializes a new `MPPEmbedding` with the given float embedding, quantized embedding, head index
 * and head name.
 *
 * @param floatEmbedding The optional Floating-point embedding.
 * @param quantizedEmbedding The optional Quantized embedding.
 * @param headIndex The index of the embedder head.
 * @param headName The optional name of the embedder head.
 *
 * @return An instance of `MPPEmbedding` initialized with the given float embedding, quantized
 * embedding, head index and head name.
 */
- (instancetype)initWithFloatEmbedding:(nullable NSArray<NSNumber *> *)floatEmbedding
                    quantizedEmbedding:(nullable NSData *)quantizedEmbedding
                             headIndex:(NSInteger)headIndex
                              headName:(nullable NSString *)headName NS_DESIGNATED_INITIALIZER;

- (instancetype)init NS_UNAVAILABLE;