Enable SPM support for Ext and configure pipelines for extensions target testing (#3)

* initial ext files

* add a workable version of spm for extensions

* update

* update pipeline

* update pipelines

* fix dev/release pipelines for extensions pod

* fix dev pipelines for extensions pod

* fix release pipelines for extensions pod

* fix dev  pipelines for extensions pod

* adding empty include folder for configuring extensions target path

* test

* test

* test

* revert pipeline changes

* revert gitignore changes

* add ext pod binary target for release pipeline

* add pipeline for extensions

* update

* update package.swift

* update latest from branch

* update

* fix

* fix

* update

* try dummy empty file

* test

* update package.swift to use fatalerror

* syntax

* try gitignore revert

* update gitignore

* add -list

* onnxruntime-Package

* update using onnxruntime-Package

* update dev pipeline

* fix dev pipelines

* syntax

* pull extensions/

* update pipelines again

* syntax

* variables

* fix

* fix -r

* update Package.swift

* update Package.swift

* minor update

* address pr comments

* minor updates

* fix

* refine messages

* syntax

* syntax again

* address pr comments partial

* address pr comments

* add .h header file and notes

* minor updates

* syncing objc source files and add code to register custom ops using function pointer

* format

* move to the header for function doc

---------

Co-authored-by: rachguo <rachguo@rachguos-Mini.attlocal.net>
Co-authored-by: rachguo <rachguo@rachguos-Mac-mini.local>
This commit is contained in:
Rachel Guo
2023-08-11 10:07:43 -07:00
committed by GitHub
parent 7acc38c99c
commit c76787fd3e
33 changed files with 1424 additions and 62 deletions
+2 -1
View File
@@ -5,8 +5,9 @@
// the headers below can also be imported individually
#import "ort_coreml_execution_provider.h"
#import "ort_xnnpack_execution_provider.h"
#import "ort_custom_op_registration.h"
#import "ort_enums.h"
#import "ort_env.h"
#import "ort_session.h"
#import "ort_value.h"
#import "ort_xnnpack_execution_provider.h"
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// this header contains the entire ONNX Runtime training Objective-C API
// the headers below can also be imported individually
#import "onnxruntime.h"
#import "ort_checkpoint.h"
#import "ort_training_session.h"
+119
View File
@@ -0,0 +1,119 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#import <Foundation/Foundation.h>
#include <stdint.h>
NS_ASSUME_NONNULL_BEGIN
/**
* An ORT checkpoint is a snapshot of the state of a model at a given point in time.
*
* This class holds the entire training session state that includes model parameters,
* their gradients, optimizer parameters, and user properties. The `ORTTrainingSession` leverages the
* `ORTCheckpoint` by accessing and updating the contained training state.
*
* Available since 1.16.
*
* @note This class is only available when the training APIs are enabled.
*/
@interface ORTCheckpoint : NSObject
- (instancetype)init NS_UNAVAILABLE;
/**
* Creates a checkpoint from directory on disk.
*
* @param path The path to the checkpoint directory.
* @param error Optional error information set if an error occurs.
* @return The instance, or nil if an error occurs.
*
* @warning The construction of the checkpoint state requires instantiation of `ORTEnv`.
* The intialization will fail if the `ORTEnv` is not properly initialized.
*/
- (nullable instancetype)initWithPath:(NSString*)path
error:(NSError**)error NS_DESIGNATED_INITIALIZER;
/**
* Saves a checkpoint to directory on disk.
*
* @param path The path to the checkpoint directory.
* @param includeOptimizerState Flag to indicate whether to save the optimizer state or not.
* @param error Optional error information set if an error occurs.
* @return Whether the checkpoint was saved successfully.
*/
- (BOOL)saveCheckpointToPath:(NSString*)path
withOptimizerState:(BOOL)includeOptimizerState
error:(NSError**)error;
/**
* Adds an int property to this checkpoint.
*
* @param name The name of the property.
* @param value The value of the property.
* @param error Optional error information set if an error occurs.
* @return Whether the property was added successfully.
*/
- (BOOL)addIntPropertyWithName:(NSString*)name
value:(int64_t)value
error:(NSError**)error;
/**
* Adds a float property to this checkpoint.
*
* @param name The name of the property.
* @param value The value of the property.
* @param error Optional error information set if an error occurs.
* @return Whether the property was added successfully.
*/
- (BOOL)addFloatPropertyWithName:(NSString*)name
value:(float)value
error:(NSError**)error;
/**
* Adds a string property to this checkpoint.
*
* @param name The name of the property.
* @param value The value of the property.
* @param error Optional error information set if an error occurs.
* @return Whether the property was added successfully.
*/
- (BOOL)addStringPropertyWithName:(NSString*)name
value:(NSString*)value
error:(NSError**)error;
/**
* Gets an int property from this checkpoint.
*
* @param name The name of the property.
* @param error Optional error information set if an error occurs.
* @return The value of the property or 0 if an error occurs.
*/
- (int64_t)getIntPropertyWithName:(NSString*)name
error:(NSError**)error __attribute__((swift_error(nonnull_error)));
/**
* Gets a float property from this checkpoint.
*
* @param name The name of the property.
* @param error Optional error information set if an error occurs.
* @return The value of the property or 0.0f if an error occurs.
*/
- (float)getFloatPropertyWithName:(NSString*)name
error:(NSError**)error __attribute__((swift_error(nonnull_error)));
/**
*
* Gets a string property from this checkpoint.
*
* @param name The name of the property.
* @param error Optional error information set if an error occurs.
* @return The value of the property.
*/
- (nullable NSString*)getStringPropertyWithName:(NSString*)name
error:(NSError**)error __attribute__((swift_error(nonnull_error)));
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/** C API type forward declaration. */
struct OrtStatus;
/** C API type forward declaration. */
struct OrtApiBase;
/** C API type forward declaration. */
struct OrtSessionOptions;
/**
* Pointer to a custom op registration function that uses the ONNX Runtime C API.
*
* The signature is defined in the ONNX Runtime C API:
* https://github.com/microsoft/onnxruntime/blob/67f4cd54fab321d83e4a75a40efeee95a6a17079/include/onnxruntime/core/session/onnxruntime_c_api.h#L697
*
* This is a low-level type intended for interoperating with libraries which provide such a function for custom op
* registration, such as [ONNX Runtime Extensions](https://github.com/microsoft/onnxruntime-extensions).
*/
typedef struct OrtStatus* (*ORTCAPIRegisterCustomOpsFnPtr)(struct OrtSessionOptions* /*options*/,
const struct OrtApiBase* /*api*/);
+2 -1
View File
@@ -38,12 +38,13 @@ typedef NS_ENUM(int32_t, ORTTensorElementDataType) {
ORTTensorElementDataTypeUInt32,
ORTTensorElementDataTypeInt64,
ORTTensorElementDataTypeUInt64,
ORTTensorElementDataTypeString,
};
/**
* The ORT graph optimization levels.
* See here for more details:
* https://onnxruntime.ai/docs/performance/graph-optimizations.html
* https://onnxruntime.ai/docs/performance/model-optimizations/graph-optimizations.html
*/
typedef NS_ENUM(int32_t, ORTGraphOptimizationLevel) {
ORTGraphOptimizationLevelNone,
+1 -1
View File
@@ -16,7 +16,7 @@ extern "C" {
*
* Available since 1.15.
*/
NSString* ORTVersion(void);
NSString* _Nullable ORTVersion(void);
#ifdef __cplusplus
}
+49 -1
View File
@@ -3,6 +3,7 @@
#import <Foundation/Foundation.h>
#import "ort_custom_op_registration.h"
#import "ort_enums.h"
NS_ASSUME_NONNULL_BEGIN
@@ -196,12 +197,19 @@ NS_ASSUME_NONNULL_BEGIN
* Available since 1.14.
*
* The registration function must have the signature:
* OrtStatus* (*fn)(OrtSessionOptions* options, const OrtApiBase* api);
* `OrtStatus* (*fn)(OrtSessionOptions* options, const OrtApiBase* api);`
*
* The signature is defined in the ONNX Runtime C API:
* https://github.com/microsoft/onnxruntime/blob/67f4cd54fab321d83e4a75a40efeee95a6a17079/include/onnxruntime/core/session/onnxruntime_c_api.h#L697
*
* See https://onnxruntime.ai/docs/reference/operators/add-custom-op.html for more information on custom ops.
* See https://github.com/microsoft/onnxruntime/blob/342a5bf2b756d1a1fc6fdc582cfeac15182632fe/onnxruntime/test/testdata/custom_op_library/custom_op_library.cc#L115
* for an example of a custom op library registration function.
*
* @note The caller must ensure that `registrationFuncName` names a valid function that is visible to the native ONNX
* Runtime code and has the correct signature.
* They must ensure that the function does what they expect it to do because this method will just call it.
*
* @param registrationFuncName The name of the registration function to call.
* @param error Optional error information set if an error occurs.
* @return Whether the registration function was successfully called.
@@ -209,6 +217,46 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)registerCustomOpsUsingFunction:(NSString*)registrationFuncName
error:(NSError**)error;
/**
* Registers custom ops for use with `ORTSession`s using this SessionOptions by calling the specified function
* pointed to by `registerCustomOpsFn`.
*
* Available since 1.16.
*
* The registration function must have the signature:
* `OrtStatus* (*fn)(OrtSessionOptions* options, const OrtApiBase* api);`
*
* The signature is defined in the ONNX Runtime C API:
* https://github.com/microsoft/onnxruntime/blob/67f4cd54fab321d83e4a75a40efeee95a6a17079/include/onnxruntime/core/session/onnxruntime_c_api.h#L697
*
* See https://onnxruntime.ai/docs/reference/operators/add-custom-op.html for more information on custom ops.
* See https://github.com/microsoft/onnxruntime/blob/342a5bf2b756d1a1fc6fdc582cfeac15182632fe/onnxruntime/test/testdata/custom_op_library/custom_op_library.cc#L115
* for an example of a custom op library registration function.
*
* @note The caller must ensure that `registerCustomOpsFn` is a valid function pointer and has the correct signature.
* They must ensure that the function does what they expect it to do because this method will just call it.
*
* @param registerCustomOpsFn A pointer to the registration function to call.
* @param error Optional error information set if an error occurs.
* @return Whether the registration function was successfully called.
*/
- (BOOL)registerCustomOpsUsingFunctionPointer:(ORTCAPIRegisterCustomOpsFnPtr)registerCustomOpsFn
error:(NSError**)error;
/**
* Registers ONNX Runtime Extensions custom ops that have been built in to ONNX Runtime.
*
* Available since 1.16.
*
* @note ONNX Runtime must have been built with the `--use_extensions` flag for the ONNX Runtime Extensions custom ops
* to be able to be registered with this method. When using a separate ONNX Runtime Extensions library, use
* `registerCustomOpsUsingFunctionPointer:error:` instead.
*
* @param error Optional error information set if an error occurs.
* @return Whether the ONNX Runtime Extensions custom ops were successfully registered.
*/
- (BOOL)enableOrtExtensionsCustomOpsWithError:(NSError**)error;
@end
/**
+263
View File
@@ -0,0 +1,263 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#import <Foundation/Foundation.h>
#include <stdint.h>
NS_ASSUME_NONNULL_BEGIN
@class ORTCheckpoint;
@class ORTEnv;
@class ORTValue;
@class ORTSessionOptions;
/**
* Trainer class that provides methods to train, evaluate and optimize ONNX models.
*
* The training session requires four training artifacts:
* 1. Training onnx model
* 2. Evaluation onnx model (optional)
* 3. Optimizer onnx model
* 4. Checkpoint directory
*
* [onnxruntime-training python utility](https://github.com/microsoft/onnxruntime/blob/main/orttraining/orttraining/python/training/onnxblock/README.md)
* can be used to generate above training artifacts.
*
* Available since 1.16.
*
* @note This class is only available when the training APIs are enabled.
*/
@interface ORTTrainingSession : NSObject
- (instancetype)init NS_UNAVAILABLE;
/**
* Creates a training session from the training artifacts that can be used to begin or resume training.
*
* The initializer instantiates the training session based on provided env and session options, which can be used to
* begin or resume training from a given checkpoint state. The checkpoint state represents the parameters of training
* session which will be moved to the device specified in the session option if needed.
*
* @param env The `ORTEnv` instance to use for the training session.
* @param sessionOptions The `ORTSessionOptions` to use for the training session.
* @param checkpoint Training states that are used as a starting point for training.
* @param trainModelPath The path to the training onnx model.
* @param evalModelPath The path to the evaluation onnx model.
* @param optimizerModelPath The path to the optimizer onnx model used to perform gradient descent.
* @param error Optional error information set if an error occurs.
* @return The instance, or nil if an error occurs.
*
* @note Note that the training session created with a checkpoint state uses this state to store the entire training
* state (including model parameters, its gradients, the optimizer states and the properties). The training session
* keeps a strong (owning) pointer to the checkpoint state.
*/
- (nullable instancetype)initWithEnv:(ORTEnv*)env
sessionOptions:(ORTSessionOptions*)sessionOptions
checkpoint:(ORTCheckpoint*)checkpoint
trainModelPath:(NSString*)trainModelPath
evalModelPath:(nullable NSString*)evalModelPath
optimizerModelPath:(nullable NSString*)optimizerModelPath
error:(NSError**)error NS_DESIGNATED_INITIALIZER;
/**
* Performs a training step, which is equivalent to a forward and backward propagation in a single step.
*
* The training step computes the outputs of the training model and the gradients of the trainable parameters
* for the given input values. The train step is performed based on the training model that was provided to the training session.
* It is equivalent to running forward and backward propagation in a single step. The computed gradients are stored inside
* the training session state so they can be later consumed by `optimizerStep`. The gradients can be lazily reset by
* calling `lazyResetGrad` method.
*
* @param inputs The input values to the training model.
* @param error Optional error information set if an error occurs.
* @return The output values of the training model.
*/
- (nullable NSArray<ORTValue*>*)trainStepWithInputValues:(NSArray<ORTValue*>*)inputs
error:(NSError**)error;
/**
* Performs a evaluation step that computes the outputs of the evaluation model for the given inputs.
* The eval step is performed based on the evaluation model that was provided to the training session.
*
* @param inputs The input values to the eval model.
* @param error Optional error information set if an error occurs.
* @return The output values of the eval model.
*
*/
- (nullable NSArray<ORTValue*>*)evalStepWithInputValues:(NSArray<ORTValue*>*)inputs
error:(NSError**)error;
/**
* Reset the gradients of all trainable parameters to zero lazily.
*
* Calling this method sets the internal state of the training session such that the gradients of the trainable parameters
* in the ORTCheckpoint will be scheduled to be reset just before the new gradients are computed on the next
* invocation of the `trainStep` method.
*
* @param error Optional error information set if an error occurs.
* @return YES if the gradients are set to reset successfully, NO otherwise.
*/
- (BOOL)lazyResetGradWithError:(NSError**)error;
/**
* Performs the weight updates for the trainable parameters using the optimizer model. The optimizer step is performed
* based on the optimizer model that was provided to the training session. The updated parameters are stored inside the
* training state so that they can be used by the next `trainStep` method call.
*
* @param error Optional error information set if an error occurs.
* @return YES if the optimizer step was performed successfully, NO otherwise.
*/
- (BOOL)optimizerStepWithError:(NSError**)error;
/**
* Returns the names of the user inputs for the training model that can be associated with
* the `ORTValue` provided to the `trainStep`.
*
* @param error Optional error information set if an error occurs.
* @return The names of the user inputs for the training model.
*/
- (nullable NSArray<NSString*>*)getTrainInputNamesWithError:(NSError**)error;
/**
* Returns the names of the user inputs for the evaluation model that can be associated with
* the `ORTValue` provided to the `evalStep`.
*
* @param error Optional error information set if an error occurs.
* @return The names of the user inputs for the evaluation model.
*/
- (nullable NSArray<NSString*>*)getEvalInputNamesWithError:(NSError**)error;
/**
* Returns the names of the user outputs for the training model that can be associated with
* the `ORTValue` returned by the `trainStep`.
*
* @param error Optional error information set if an error occurs.
* @return The names of the user outputs for the training model.
*/
- (nullable NSArray<NSString*>*)getTrainOutputNamesWithError:(NSError**)error;
/**
* Returns the names of the user outputs for the evaluation model that can be associated with
* the `ORTValue` returned by the `evalStep`.
*
* @param error Optional error information set if an error occurs.
* @return The names of the user outputs for the evaluation model.
*/
- (nullable NSArray<NSString*>*)getEvalOutputNamesWithError:(NSError**)error;
/**
* Registers a linear learning rate scheduler for the training session.
*
* The scheduler gradually decreases the learning rate from the initial value to zero over the course of the training.
* The decrease is performed by multiplying the current learning rate by a linearly updated factor.
* Before the decrease, the learning rate is gradually increased from zero to the initial value during a warmup phase.
*
* @param warmupStepCount The number of steps to perform the linear warmup.
* @param totalStepCount The total number of steps to perform the linear decay.
* @param initialLr The initial learning rate.
* @param error Optional error information set if an error occurs.
* @return YES if the scheduler was registered successfully, NO otherwise.
*/
- (BOOL)registerLinearLRSchedulerWithWarmupStepCount:(int64_t)warmupStepCount
totalStepCount:(int64_t)totalStepCount
initialLr:(float)initialLr
error:(NSError**)error;
/**
* Update the learning rate based on the registered learning rate scheduler.
*
* Performs a scheduler step that updates the learning rate that is being used by the training session.
* This function should typically be called before invoking the optimizer step for each round, or as necessary
* to update the learning rate being used by the training session.
*
* @note A valid predefined learning rate scheduler must be first registered to invoke this method.
*
* @param error Optional error information set if an error occurs.
* @return YES if the scheduler step was performed successfully, NO otherwise.
*/
- (BOOL)schedulerStepWithError:(NSError**)error;
/**
* Returns the current learning rate being used by the training session.
*
* @param error Optional error information set if an error occurs.
* @return The current learning rate or 0.0f if an error occurs.
*/
- (float)getLearningRateWithError:(NSError**)error __attribute__((swift_error(nonnull_error)));
/**
* Sets the learning rate being used by the training session.
*
* The current learning rate is maintained by the training session and can be overwritten by invoking this method
* with the desired learning rate. This function should not be used when a valid learning rate scheduler is registered.
* It should be used either to set the learning rate derived from a custom learning rate scheduler or to set a constant
* learning rate to be used throughout the training session.
*
* @note It does not set the initial learning rate that may be needed by the predefined learning rate schedulers.
* To set the initial learning rate for learning rate schedulers, use the `registerLinearLRScheduler` method.
*
* @param lr The learning rate to be used by the training session.
* @param error Optional error information set if an error occurs.
* @return YES if the learning rate was set successfully, NO otherwise.
*/
- (BOOL)setLearningRate:(float)lr
error:(NSError**)error;
/**
* Loads the training session model parameters from a contiguous buffer.
*
* @param buffer Contiguous buffer to load the parameters from.
* @param error Optional error information set if an error occurs.
* @return YES if the parameters were loaded successfully, NO otherwise.
*/
- (BOOL)fromBufferWithValue:(ORTValue*)buffer
error:(NSError**)error;
/**
* Returns a contiguous buffer that holds a copy of all training state parameters.
*
* @param onlyTrainable If YES, returns a buffer that holds only the trainable parameters, otherwise returns a buffer
* that holds all the parameters.
* @param error Optional error information set if an error occurs.
* @return A contiguous buffer that holds a copy of all training state parameters.
*/
- (nullable ORTValue*)toBufferWithTrainable:(BOOL)onlyTrainable
error:(NSError**)error;
/**
* Exports the training session model that can be used for inference.
*
* If the training session was provided with an eval model, the training session can generate an inference model if it
* knows the inference graph outputs. The input inference graph outputs are used to prune the eval model so that the
* inference model's outputs align with the provided outputs. The exported model is saved at the path provided and
* can be used for inferencing with `ORTSession`.
*
* @note The method reloads the eval model from the path provided to the initializer and expects this path to be valid.
*
* @param inferenceModelPath The path to the serialized the inference model.
* @param graphOutputNames The names of the outputs that are needed in the inference model.
* @param error Optional error information set if an error occurs.
* @return YES if the inference model was exported successfully, NO otherwise.
*/
- (BOOL)exportModelForInferenceWithOutputPath:(NSString*)inferenceModelPath
graphOutputNames:(NSArray<NSString*>*)graphOutputNames
error:(NSError**)error;
@end
#ifdef __cplusplus
extern "C" {
#endif
/**
* This function sets the seed for generating random numbers.
* Use this function to generate reproducible results. It should be noted that completely reproducible results are not guaranteed.
*
* @param seed Manually set seed to use for random number generation.
*/
void ORTSetSeed(int64_t seed);
#ifdef __cplusplus
}
#endif
NS_ASSUME_NONNULL_END
+28
View File
@@ -32,6 +32,21 @@ NS_ASSUME_NONNULL_BEGIN
shape:(NSArray<NSNumber*>*)shape
error:(NSError**)error;
/**
* Creates a value that is a string tensor.
* The string data will be copied into a buffer owned by this ORTValue instance.
*
* Available since 1.16.
*
* @param tensorStringData The tensor string data.
* @param shape The tensor shape.
* @param error Optional error information set if an error occurs.
* @return The instance, or nil if an error occurs.
*/
- (nullable instancetype)initWithTensorStringData:(NSArray<NSString*>*)tensorStringData
shape:(NSArray<NSNumber*>*)shape
error:(NSError**)error;
/**
* Gets the type information.
*
@@ -63,6 +78,19 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (nullable NSMutableData*)tensorDataWithError:(NSError**)error;
/**
* Gets the tensor string data.
* This assumes that the value is a string tensor.
*
* This returns a copy of the value's underlying string data.
*
* Available since 1.16.
*
* @param error Optional error information set if an error occurs.
* @return The copy of the tensor string data, or nil if an error occurs.
*/
- (nullable NSArray<NSString*>*)tensorStringDataWithError:(NSError**)error;
@end
/**