Skip to content

Commit

Permalink
Initialize Objective-C bindings (#709)
Browse files Browse the repository at this point in the history
This PR contains Objective-C wrapper code, podspec generation infras and
the accompanying test code.

Internal workitem: https://task.ms/aii/35199

---------

Co-authored-by: Edward Chen <[email protected]>
  • Loading branch information
skyline75489 and edgchen1 authored Dec 12, 2024
1 parent e16da04 commit 8e171c7
Show file tree
Hide file tree
Showing 35 changed files with 2,579 additions and 2 deletions.
22 changes: 22 additions & 0 deletions .pipelines/macos-ios-cocoapods-publishing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
parameters:
- name: ort_version
displayName: 'OnnxRuntime version'
type: string
default: '1.18.0'

- name: build_config
displayName: 'Build Configuration'
type: string
default: 'Release'
values:
- 'Debug'
- 'RelWithDebInfo'
- 'Release'
- 'MinSizeRel'

trigger: none
stages:
- template: stages/macos-ios-cocoapods-packaging-stage.yml
parameters:
ort_version: ${{ parameters.ort_version }}
build_config: ${{ parameters.build_config }}
1 change: 1 addition & 0 deletions .pipelines/stages/jobs/capi-packaging-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,6 @@ jobs:
- template: steps/capi-appleframework-step.yml
parameters:
build_config: ${{ parameters.build_config }}
build_settings_file: "tools/ci_build/github/apple/default_full_ios_framework_build_settings.json"

- template: steps/compliant-and-cleanup-step.yml
40 changes: 40 additions & 0 deletions .pipelines/stages/jobs/macos-ios-cocoapods-packaging-job.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
parameters:
- name: build_config
type: string
- name: ort_version
type: string

jobs:
- job: macos_ios_cocoapods_packaging
pool:
vmImage: 'macOS-latest'
variables:
buildSettingsFile: "tools/ci_build/github/apple/default_full_apple_framework_build_settings.json"

workspace:
clean: all
steps:
- checkout: self
clean: true
submodules: none

- template: steps/utils/set-genai-version.yml
- template: steps/utils/set-cmake-build-type.yml
parameters:
build_config: ${{parameters.build_config}}


- task: CmdLine@2
displayName: Build MacOS & iOS CocoaPods Packages
inputs:
script: |
set -e -x
python tools/ci_build/github/apple/build_and_assemble_apple_pods.py \
--build-dir "$(Build.BinariesDirectory)/apple_framework" \
--staging-dir "$(Build.BinariesDirectory)/staging" \
--pod-version "$(genai_version)" \
--test \
--variant Full \
--build-settings-file "${{ variables.buildSettingsFile }}" \
--build-apple-framework-args==--config=$(cmake_build_type) \
--ort-version ${{parameters.ort_version}}
2 changes: 2 additions & 0 deletions .pipelines/stages/jobs/nuget-packaging-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ jobs:

- template: steps/utils/set-genai-version.yml

- task: NuGetAuthenticate@1

- powershell: |
dotnet --info
dotnet build Microsoft.ML.OnnxRuntimeGenAI.csproj -p:Configuration="$(buildConfig)" -p:IncludeMobileTargets=true --verbosity normal
Expand Down
9 changes: 7 additions & 2 deletions .pipelines/stages/jobs/steps/capi-appleframework-step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ parameters:
- name: build_config
type: string
default: 'release'

- name: build_settings_file
type: string
steps:

- checkout: self
Expand All @@ -11,6 +12,9 @@ steps:
submodules: recursive

- template: utils/set-genai-version.yml
- template: utils/set-cmake-build-type.yml
parameters:
build_config: ${{parameters.build_config}}

- template: utils/set-nightly-build-option-variable.yml

Expand All @@ -19,7 +23,8 @@ steps:
python3 -m pip install requests
python3 tools/ci_build/github/apple/build_apple_framework.py \
--build_dir "$(Build.BinariesDirectory)/apple_framework" \
tools/ci_build/github/apple/default_full_ios_framework_build_settings.json
--config $(cmake_build_type) \
"${{ parameters.build_settings_file }}"
mkdir $(Build.BinariesDirectory)/artifacts
mkdir -p $(Build.BinariesDirectory)/artifacts_staging/onnxruntime-genai.xcframework
Expand Down
15 changes: 15 additions & 0 deletions .pipelines/stages/macos-ios-cocoapods-packaging-stage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
parameters:
- name: ort_version
type: string

- name: build_config
type: string
default: 'Release'

stages:
- stage: Build_MacOS_iOS_CocoaPods_Archive
jobs:
- template: jobs/macos-ios-cocoapods-packaging-job.yml
parameters:
ort_version: ${{ parameters.ort_version }}
build_config: ${{ parameters.build_config }}
1 change: 1 addition & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ def update(args: argparse.Namespace, env: dict[str, str]):
"-DENABLE_PYTHON=OFF",
"-DENABLE_TESTS=OFF",
"-DENABLE_MODEL_BENCHMARK=OFF",
"-DCMAKE_OSX_DEPLOYMENT_TARGET=" + args.apple_deploy_target,
f"-DBUILD_APPLE_FRAMEWORK={'ON' if args.build_apple_framework else 'OFF'}",
"-DPLATFORM_NAME=" + platform_name,
]
Expand Down
17 changes: 17 additions & 0 deletions src/objectivec/cxx_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// wrapper for ORT GenAI C/C++ API headers

#if defined(__clang__)
#pragma clang diagnostic push
// ignore clang documentation-related warnings
// instead, we will rely on Doxygen warnings for the C/C++ API headers
#pragma clang diagnostic ignored "-Wdocumentation"
#endif // defined(__clang__)

#include "ort_genai.h"

#if defined(__clang__)
#pragma clang diagnostic pop
#endif // defined(__clang__)
29 changes: 29 additions & 0 deletions src/objectivec/error_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#import <Foundation/Foundation.h>

#include <exception>

#import "cxx_api.h"

NS_ASSUME_NONNULL_BEGIN

extern NSString* const kOgaErrorDomain;

void OGASaveCodeAndDescriptionToError(int code, const char* description, NSError** error);
void OGASaveCodeAndDescriptionToError(int code, NSString* description, NSError** error);
void OGASaveExceptionToError(const std::exception& e, NSError** error);

// helper macros to catch and handle C++ exceptions
#define OGA_OBJC_API_IMPL_CATCH(error, failure_return_value) \
catch (const std::exception& e) { \
OGASaveExceptionToError(e, (error)); \
return (failure_return_value); \
}

#define OGA_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error) OGA_OBJC_API_IMPL_CATCH(error, NO)

#define OGA_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error) OGA_OBJC_API_IMPL_CATCH(error, nil)

NS_ASSUME_NONNULL_END
34 changes: 34 additions & 0 deletions src/objectivec/error_utils.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#import "error_utils.h"

NS_ASSUME_NONNULL_BEGIN

NSString* const kOgaErrorDomain = @"onnxruntime-genai";
const int kOgaErrorCode = 0x0A;

void OGASaveCodeAndDescriptionToError(int code, const char* descriptionCstr, NSError** error) {
if (!error) return;

NSString* description = [NSString stringWithCString:descriptionCstr
encoding:NSUTF8StringEncoding];

*error = [NSError errorWithDomain:kOgaErrorDomain
code:code
userInfo:@{NSLocalizedDescriptionKey : description}];
}

void OGASaveCodeAndDescriptionToError(int code, NSString* description, NSError** error) {
if (!error) return;

*error = [NSError errorWithDomain:kOgaErrorDomain
code:code
userInfo:@{NSLocalizedDescriptionKey : description}];
}

void OGASaveExceptionToError(const std::exception& e, NSError** error) {
OGASaveCodeAndDescriptionToError(kOgaErrorCode, e.what(), error);
}

NS_ASSUME_NONNULL_END
Loading

0 comments on commit 8e171c7

Please sign in to comment.