pull swift/ and objc/ folder contents
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
// this header contains the entire ONNX Runtime Objective-C API
|
||||
// the headers below can also be imported individually
|
||||
|
||||
#import "ort_coreml_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"
|
||||
@@ -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,62 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "ort_session.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Gets whether the CoreML execution provider is available.
|
||||
*/
|
||||
BOOL ORTIsCoreMLExecutionProviderAvailable(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* Options for configuring the CoreML execution provider.
|
||||
*/
|
||||
@interface ORTCoreMLExecutionProviderOptions : NSObject
|
||||
|
||||
/**
|
||||
* Whether the CoreML execution provider should run on CPU only.
|
||||
*/
|
||||
@property BOOL useCPUOnly;
|
||||
|
||||
/**
|
||||
* Whether the CoreML execution provider is enabled on subgraphs.
|
||||
*/
|
||||
@property BOOL enableOnSubgraphs;
|
||||
|
||||
/**
|
||||
* Whether the CoreML execution provider is only enabled for devices with Apple
|
||||
* Neural Engine (ANE).
|
||||
*/
|
||||
@property BOOL onlyEnableForDevicesWithANE;
|
||||
|
||||
@end
|
||||
|
||||
@interface ORTSessionOptions (ORTSessionOptionsCoreMLEP)
|
||||
|
||||
/**
|
||||
* Enables the CoreML execution provider in the session configuration options.
|
||||
* It is appended to the execution provider list which is ordered by
|
||||
* decreasing priority.
|
||||
*
|
||||
* @param options The CoreML execution provider configuration options.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the provider was enabled successfully.
|
||||
*/
|
||||
- (BOOL)appendCoreMLExecutionProviderWithOptions:(ORTCoreMLExecutionProviderOptions*)options
|
||||
error:(NSError**)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*/);
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* The ORT logging verbosity levels.
|
||||
*/
|
||||
typedef NS_ENUM(int32_t, ORTLoggingLevel) {
|
||||
ORTLoggingLevelVerbose,
|
||||
ORTLoggingLevelInfo,
|
||||
ORTLoggingLevelWarning,
|
||||
ORTLoggingLevelError,
|
||||
ORTLoggingLevelFatal,
|
||||
};
|
||||
|
||||
/**
|
||||
* The ORT value types.
|
||||
* Currently, a subset of all types is supported.
|
||||
*/
|
||||
typedef NS_ENUM(int32_t, ORTValueType) {
|
||||
ORTValueTypeUnknown,
|
||||
ORTValueTypeTensor,
|
||||
};
|
||||
|
||||
/**
|
||||
* The ORT tensor element data types.
|
||||
* Currently, a subset of all types is supported.
|
||||
*/
|
||||
typedef NS_ENUM(int32_t, ORTTensorElementDataType) {
|
||||
ORTTensorElementDataTypeUndefined,
|
||||
ORTTensorElementDataTypeFloat,
|
||||
ORTTensorElementDataTypeInt8,
|
||||
ORTTensorElementDataTypeUInt8,
|
||||
ORTTensorElementDataTypeInt32,
|
||||
ORTTensorElementDataTypeUInt32,
|
||||
ORTTensorElementDataTypeInt64,
|
||||
ORTTensorElementDataTypeUInt64,
|
||||
};
|
||||
|
||||
/**
|
||||
* The ORT graph optimization levels.
|
||||
* See here for more details:
|
||||
* https://onnxruntime.ai/docs/performance/graph-optimizations.html
|
||||
*/
|
||||
typedef NS_ENUM(int32_t, ORTGraphOptimizationLevel) {
|
||||
ORTGraphOptimizationLevelNone,
|
||||
ORTGraphOptimizationLevelBasic,
|
||||
ORTGraphOptimizationLevelExtended,
|
||||
ORTGraphOptimizationLevelAll,
|
||||
};
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "ort_enums.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Gets the ORT version string in format major.minor.patch.
|
||||
*
|
||||
* Available since 1.15.
|
||||
*/
|
||||
NSString* _Nullable ORTVersion(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* The ORT environment.
|
||||
*/
|
||||
@interface ORTEnv : NSObject
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
/**
|
||||
* Creates an ORT Environment.
|
||||
*
|
||||
* @param loggingLevel The environment logging level.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return The instance, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable instancetype)initWithLoggingLevel:(ORTLoggingLevel)loggingLevel
|
||||
error:(NSError**)error NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,300 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "ort_custom_op_registration.h"
|
||||
#import "ort_enums.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class ORTEnv;
|
||||
@class ORTRunOptions;
|
||||
@class ORTSessionOptions;
|
||||
@class ORTValue;
|
||||
|
||||
/**
|
||||
* An ORT session loads and runs a model.
|
||||
*/
|
||||
@interface ORTSession : NSObject
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
/**
|
||||
* Creates a session.
|
||||
*
|
||||
* @param env The ORT Environment instance.
|
||||
* @param path The path to the ONNX model.
|
||||
* @param sessionOptions Optional session configuration options.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return The instance, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable instancetype)initWithEnv:(ORTEnv*)env
|
||||
modelPath:(NSString*)path
|
||||
sessionOptions:(nullable ORTSessionOptions*)sessionOptions
|
||||
error:(NSError**)error NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
/**
|
||||
* Runs the model.
|
||||
* The inputs and outputs are pre-allocated.
|
||||
*
|
||||
* @param inputs Dictionary of input names to input ORT values.
|
||||
* @param outputs Dictionary of output names to output ORT values.
|
||||
* @param runOptions Optional run configuration options.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the model was run successfully.
|
||||
*/
|
||||
- (BOOL)runWithInputs:(NSDictionary<NSString*, ORTValue*>*)inputs
|
||||
outputs:(NSDictionary<NSString*, ORTValue*>*)outputs
|
||||
runOptions:(nullable ORTRunOptions*)runOptions
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Runs the model.
|
||||
* The inputs are pre-allocated and the outputs are allocated by ORT.
|
||||
*
|
||||
* @param inputs Dictionary of input names to input ORT values.
|
||||
* @param outputNames Set of output names.
|
||||
* @param runOptions Optional run configuration options.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return A dictionary of output names to output ORT values with the outputs
|
||||
* requested in `outputNames`, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable NSDictionary<NSString*, ORTValue*>*)runWithInputs:(NSDictionary<NSString*, ORTValue*>*)inputs
|
||||
outputNames:(NSSet<NSString*>*)outputNames
|
||||
runOptions:(nullable ORTRunOptions*)runOptions
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Gets the model's input names.
|
||||
*
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return An array of input names, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable NSArray<NSString*>*)inputNamesWithError:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Gets the model's overridable initializer names.
|
||||
*
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return An array of overridable initializer names, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable NSArray<NSString*>*)overridableInitializerNamesWithError:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Gets the model's output names.
|
||||
*
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return An array of output names, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable NSArray<NSString*>*)outputNamesWithError:(NSError**)error;
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* Options for configuring a session.
|
||||
*/
|
||||
@interface ORTSessionOptions : NSObject
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
/**
|
||||
* Creates session configuration options.
|
||||
*
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return The instance, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable instancetype)initWithError:(NSError**)error NS_SWIFT_NAME(init());
|
||||
|
||||
/**
|
||||
* Appends an execution provider to the session options to enable the execution provider to be used when running
|
||||
* the model.
|
||||
*
|
||||
* Available since 1.14.
|
||||
*
|
||||
* The execution provider list is ordered by decreasing priority.
|
||||
* i.e. the first provider registered has the highest priority.
|
||||
*
|
||||
* @param providerName Provider name. For example, "xnnpack".
|
||||
* @param providerOptions Provider-specific options. For example, for provider "xnnpack", {"intra_op_num_threads": "2"}.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the execution provider was appended successfully
|
||||
*/
|
||||
- (BOOL)appendExecutionProvider:(NSString*)providerName
|
||||
providerOptions:(NSDictionary<NSString*, NSString*>*)providerOptions
|
||||
error:(NSError**)error;
|
||||
/**
|
||||
* Sets the number of threads used to parallelize the execution within nodes.
|
||||
* A value of 0 means ORT will pick a default value.
|
||||
*
|
||||
* @param intraOpNumThreads The number of threads.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the option was set successfully.
|
||||
*/
|
||||
- (BOOL)setIntraOpNumThreads:(int)intraOpNumThreads
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Sets the graph optimization level.
|
||||
*
|
||||
* @param graphOptimizationLevel The graph optimization level.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the option was set successfully.
|
||||
*/
|
||||
- (BOOL)setGraphOptimizationLevel:(ORTGraphOptimizationLevel)graphOptimizationLevel
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Sets the path to which the optimized model file will be saved.
|
||||
*
|
||||
* @param optimizedModelFilePath The optimized model file path.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the option was set successfully.
|
||||
*/
|
||||
- (BOOL)setOptimizedModelFilePath:(NSString*)optimizedModelFilePath
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Sets the session log ID.
|
||||
*
|
||||
* @param logID The log ID.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the option was set successfully.
|
||||
*/
|
||||
- (BOOL)setLogID:(NSString*)logID
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Sets the session log severity level.
|
||||
*
|
||||
* @param loggingLevel The log severity level.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the option was set successfully.
|
||||
*/
|
||||
- (BOOL)setLogSeverityLevel:(ORTLoggingLevel)loggingLevel
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Sets a session configuration key-value pair.
|
||||
* Any value for a previously set key will be overwritten.
|
||||
* The session configuration keys and values are documented here:
|
||||
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/onnxruntime_session_options_config_keys.h
|
||||
*
|
||||
* @param key The key.
|
||||
* @param value The value.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the option was set successfully.
|
||||
*/
|
||||
- (BOOL)addConfigEntryWithKey:(NSString*)key
|
||||
value:(NSString*)value
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Registers custom ops for use with `ORTSession`s using this SessionOptions by calling the specified
|
||||
* native function name. The custom ops library must either be linked against, or have previously been loaded
|
||||
* by the user.
|
||||
*
|
||||
* Available since 1.14.
|
||||
*
|
||||
* 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 `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.
|
||||
*/
|
||||
- (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;
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* Options for configuring a run.
|
||||
*/
|
||||
@interface ORTRunOptions : NSObject
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
/**
|
||||
* Creates run configuration options.
|
||||
*
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return The instance, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable instancetype)initWithError:(NSError**)error NS_SWIFT_NAME(init());
|
||||
|
||||
/**
|
||||
* Sets the run log tag.
|
||||
*
|
||||
* @param logTag The log tag.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the option was set successfully.
|
||||
*/
|
||||
- (BOOL)setLogTag:(NSString*)logTag
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Sets the run log severity level.
|
||||
*
|
||||
* @param loggingLevel The log severity level.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the option was set successfully.
|
||||
*/
|
||||
- (BOOL)setLogSeverityLevel:(ORTLoggingLevel)loggingLevel
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Sets a run configuration key-value pair.
|
||||
* Any value for a previously set key will be overwritten.
|
||||
* The run configuration keys and values are documented here:
|
||||
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/onnxruntime_run_options_config_keys.h
|
||||
*
|
||||
* @param key The key.
|
||||
* @param value The value.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the option was set successfully.
|
||||
*/
|
||||
- (BOOL)addConfigEntryWithKey:(NSString*)key
|
||||
value:(NSString*)value
|
||||
error:(NSError**)error;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -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
|
||||
@@ -0,0 +1,94 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "ort_enums.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class ORTValueTypeInfo;
|
||||
@class ORTTensorTypeAndShapeInfo;
|
||||
|
||||
/**
|
||||
* An ORT value encapsulates data used as an input or output to a model at runtime.
|
||||
*/
|
||||
@interface ORTValue : NSObject
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
/**
|
||||
* Creates a value that is a tensor.
|
||||
* The tensor data is allocated by the caller.
|
||||
*
|
||||
* @param tensorData The tensor data.
|
||||
* @param elementType The tensor element data type.
|
||||
* @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)initWithTensorData:(NSMutableData*)tensorData
|
||||
elementType:(ORTTensorElementDataType)elementType
|
||||
shape:(NSArray<NSNumber*>*)shape
|
||||
error:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Gets the type information.
|
||||
*
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return The type information, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable ORTValueTypeInfo*)typeInfoWithError:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Gets the tensor type and shape information.
|
||||
* This assumes that the value is a tensor.
|
||||
*
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return The tensor type and shape information, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable ORTTensorTypeAndShapeInfo*)tensorTypeAndShapeInfoWithError:(NSError**)error;
|
||||
|
||||
/**
|
||||
* Gets the tensor data.
|
||||
* This assumes that the value is a tensor.
|
||||
*
|
||||
* This returns the value's underlying data directly, not a copy of it.
|
||||
* The memory's lifetime may be tied to this value, i.e., if it was allocated
|
||||
* by ORT. On the other hand, the memory's lifetime is independent of the value
|
||||
* if the value was created with user-provided data.
|
||||
*
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return The tensor data, or nil if an error occurs.
|
||||
*/
|
||||
- (nullable NSMutableData*)tensorDataWithError:(NSError**)error;
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* A value's type information.
|
||||
*/
|
||||
@interface ORTValueTypeInfo : NSObject
|
||||
|
||||
/** The value type. */
|
||||
@property(nonatomic) ORTValueType type;
|
||||
|
||||
/** The tensor type and shape information, if the value is a tensor. */
|
||||
@property(nonatomic, nullable) ORTTensorTypeAndShapeInfo* tensorTypeAndShapeInfo;
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* A tensor's type and shape information.
|
||||
*/
|
||||
@interface ORTTensorTypeAndShapeInfo : NSObject
|
||||
|
||||
/** The tensor element data type. */
|
||||
@property(nonatomic) ORTTensorElementDataType elementType;
|
||||
|
||||
/** The tensor shape. */
|
||||
@property(nonatomic) NSArray<NSNumber*>* shape;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "ort_session.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* Options for configuring the Xnnpack execution provider.
|
||||
*/
|
||||
@interface ORTXnnpackExecutionProviderOptions : NSObject
|
||||
|
||||
/**
|
||||
* How many threads used for the Xnnpack execution provider.
|
||||
*/
|
||||
@property int intra_op_num_threads;
|
||||
|
||||
@end
|
||||
|
||||
@interface ORTSessionOptions (ORTSessionOptionsXnnpackEP)
|
||||
|
||||
/**
|
||||
* Available since 1.14.
|
||||
* Enables the Xnnpack execution provider in the session configuration options.
|
||||
* It is appended to the execution provider list which is ordered by
|
||||
* decreasing priority.
|
||||
*
|
||||
* @param options The Xnnpack execution provider configuration options.
|
||||
* @param error Optional error information set if an error occurs.
|
||||
* @return Whether the provider was enabled successfully.
|
||||
*/
|
||||
- (BOOL)appendXnnpackExecutionProviderWithOptions:(ORTXnnpackExecutionProviderOptions*)options
|
||||
error:(NSError**)error;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
Reference in New Issue
Block a user