Skip to content

CoreML iOS xcode13.2 b1

Alex Soto edited this page Dec 6, 2021 · 3 revisions

#CoreML.framework https://github.com/xamarin/xamarin-macios/pull/13497

diff -ruN /Applications/Xcode_13.1.0-rc.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/CoreML.framework/Headers/MLMultiArray.h /Applications/Xcode_13.2.0-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/CoreML.framework/Headers/MLMultiArray.h
--- /Applications/Xcode_13.1.0-rc.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/CoreML.framework/Headers/MLMultiArray.h	2021-09-30 13:42:02.000000000 -0400
+++ /Applications/Xcode_13.2.0-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/CoreML.framework/Headers/MLMultiArray.h	2021-10-22 20:30:32.000000000 -0400
@@ -7,6 +7,7 @@
 
 #import <Foundation/Foundation.h>
 #import <CoreML/MLExport.h>
+#import <CoreVideo/CVPixelBuffer.h>
 
 NS_ASSUME_NONNULL_BEGIN
 
@@ -17,6 +18,7 @@
     MLMultiArrayDataTypeDouble  = 0x10000 | 64,
     MLMultiArrayDataTypeFloat64 API_AVAILABLE(macos(11.0), ios(14.0), watchos(7.0), tvos(14.0)) = 0x10000 | 64,
     MLMultiArrayDataTypeFloat32 = 0x10000 | 32,
+    MLMultiArrayDataTypeFloat16 API_AVAILABLE(macos(12.0)) API_UNAVAILABLE(ios, watchos, tvos) = 0x10000 | 16,
     MLMultiArrayDataTypeFloat   API_AVAILABLE(macos(11.0), ios(14.0), watchos(7.0), tvos(14.0)) = 0x10000 | 32,
     MLMultiArrayDataTypeInt32   = 0x20000 | 32,
 } API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
@@ -48,6 +50,11 @@
 /// Count of total number of elements
 @property (readonly, nonatomic) NSInteger count;
 
+/**
+   Returns the backing pixel buffer if exists, otherwise nil.
+ */
+@property (readonly, nullable, nonatomic) CVPixelBufferRef pixelBuffer API_AVAILABLE(macos(12.0)) API_UNAVAILABLE(ios, watchos, tvos);
+
 @end
 
 @interface MLMultiArray (Creation)
@@ -66,6 +73,33 @@
                                  deallocator:(void (^_Nullable)(void *bytes))deallocator
                                        error:(NSError **)error;
 
+/*!
+ * Create by wrapping a pixel buffer.
+ *
+ * Use this initializer to create IOSurface backed MLMultiArray, which can reduce the inference latency by avoiding the buffer copy.
+ *
+ * The instance will own the pixel buffer and release it on the deallocation.
+ *
+ * The pixel buffer's pixel format type must be OneComponent16Half. As such, MLMultiArray's data type will be MLMultiArrayDataTypeFloat16.
+ *
+ * \code
+ * CVPixelBufferRef pixelBuffer = NULL;
+ * NSDictionary* pixelBufferAttributes = @{
+ *     (id)kCVPixelBufferIOSurfacePropertiesKey: @{}
+ * };
+ *
+ * // Since shape == [2, 3, 4], width is 4 (= shape[2]) and height is 6 (= shape[0] * shape[1]).
+ * CVPixelBufferCreate(kCFAllocatorDefault, 4, 6, kCVPixelFormatType_OneComponent16Half, (__bridge CFDictionaryRef)pixelBufferAttributes, &pixelBuffer);
+ * MLMultiArray *multiArray = [[MLMultiArray alloc] initWithPixelBuffer:pixelBuffer shape:@[@2, @3, @4]];
+ * \endcode
+ *
+ * @param pixelBuffer The pixel buffer to be owned by the instance.
+ *
+ * @param shape The shape of the MLMultiArray. The last dimension of `shape` must match the pixel buffer's width. The product of the rest of the dimensions must match the height.
+ */
+- (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer
+                              shape:(NSArray<NSNumber *> *)shape API_AVAILABLE(macos(12.0)) API_UNAVAILABLE(ios, watchos, tvos);
+
 @end
 
 @interface MLMultiArray (Concatenating)
diff -ruN /Applications/Xcode_13.1.0-rc.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/CoreML.framework/Headers/MLPredictionOptions.h /Applications/Xcode_13.2.0-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/CoreML.framework/Headers/MLPredictionOptions.h
--- /Applications/Xcode_13.1.0-rc.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/CoreML.framework/Headers/MLPredictionOptions.h	2021-09-09 16:49:38.000000000 -0400
+++ /Applications/Xcode_13.2.0-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/CoreML.framework/Headers/MLPredictionOptions.h	2021-10-22 20:33:36.000000000 -0400
@@ -22,6 +22,72 @@
 /// Set to YES to force computation to be on the CPU only
 @property (readwrite, nonatomic) BOOL usesCPUOnly API_DEPRECATED_WITH_REPLACEMENT("Use -[MLModelConfiguration computeUnits] instead.", macos(10.13, 12.0), ios(11.0, 15.0), tvos(11.0, 15.0), watchos(4.0, 8.0));
 
+/*!
+ * @abstract Propose the model to use the specified backing objects for the
+ * output feature values.
+ *
+ * @discussion Use the property to get the inference result directly into the
+ * client allocated buffer when possible for efficient memory management.
+ *
+ * The property is a dictionary of the feature name and the output backing
+ * object.
+ *
+ * The framework may not use the specified backing object and instead allocates
+ * one by itself if the outputBacking dictionary doesn't contain the entry for
+ * the feature name, the model doesn't support the user allocated buffers, or in
+ * the batch prediction mode. To check if the backing object was used, compare
+ * the output prediction and the backing object by object identity.
+ *
+ * \code
+ *  CVPixelBufferRef outputBacking = ...;
+ *  [options setOutputBackings:@{@"outputImage" : (__bridge id)outputBacking}];
+ *  id<MLFeatureProvider> prediction = [model predictionFromFeatures:inputFeatures options:options error:&error];
+ *  if ([prediction featureValueForName:@"outputImage"].imageBufferValue == outputBacking) {
+ *    // backing was used.
+ *  }
+ *  else {
+ *    // backing was NOT used.
+ *  }
+ * \endcode
+ *
+ * The backing object must be either CVPixelBuffer or MLMultiArray depending on
+ * the feature value type.
+ *
+ * Do not lock the base address of the CVPixelBuffer. In the case of a MLMultiArray
+ * backed by a pixel buffer, make sure not to lock the underlying pixel buffer by not
+ * calling any data methods such as `.dataPointer` and subscript methods before the
+ * prediction.
+ *
+ * The framework ignores a backing object with an unknown feature name.
+ *
+ * For the best performance, use page-aligned address in MLMultiArray.
+ *
+ * \code
+ *  #import <mach/vm_page_size.h>
+ *  :
+ *  void *backingBuffer = aligned_alloc(vm_page_size, round_page(backingBufferSize));
+ *  if (backingBuffer == NULL) { ... error handling ... }
+ *  MLMultiArray *outputBacking = [[MLMultiArray alloc] initWithDataPointer:(char *)backingBuffer
+ *                                                                        ...
+ *                                                              deallocator:^(void *) { free(backingBuffer); }
+ *                                                                        ... ];
+ * \endcode
+ *
+ * For CVPixelBuffer backing, consider to use IOSurface-backed CVPixelBuffer
+ * created by CVPixelBufferPool because it is often the most efficient choice for
+ * memory footprint and performance, especially when the pixel buffers are
+ * subsequently used for playback or export. (See also AVSampleBufferDisplayLayer
+ * and AVAssetWriter.)
+ *
+ * The output backing object must satisfy the output feature description's
+ * `-isAllowedValue:` test, or the framework reporets an error at the prediction
+ * time. The exception is FP16 MLMultiArray backed by CVPixelBuffer, which may be
+ * accepted in Double or Float32 multi array output feature depending on the
+ * underlying inference engine.
+ *
+ */
+@property (readwrite, copy, nonatomic) NSDictionary<NSString *, id> *outputBackings API_AVAILABLE(macos(11.0)) API_UNAVAILABLE(ios, watchos, tvos);
+
 @end
 
 NS_ASSUME_NONNULL_END
Clone this wiki locally