Skip to content

Commit

Permalink
Update ORT version to 1.9.0 (#41)
Browse files Browse the repository at this point in the history
* Update ORT version to 1.9.0

* Update the C example
  • Loading branch information
snnn authored Oct 14, 2021
1 parent 0e72601 commit e4b52d9
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 123 deletions.
68 changes: 34 additions & 34 deletions .github/workflows/linux-ci.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
name: Linux-CPU
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
Linux-CPU-CI-x64:
runs-on: ubuntu-latest
strategy:
matrix:
gcc: [9, 10]
steps:
- name: Check out repository code
uses: actions/checkout@v2
- name: Download onnxruntime binary
run: |
aria2c -q -d $RUNNER_TEMP https://github.com/microsoft/onnxruntime/releases/download/v1.8.1/onnxruntime-linux-x64-1.8.1.tgz
cd $RUNNER_TEMP
mkdir onnxruntimebin
cd onnxruntimebin
tar --strip=1 -zxvf $RUNNER_TEMP/onnxruntime-linux-x64-1.8.1.tgz
- name: Build
env:
CC: gcc-${{ matrix.gcc }}
CXX: g++-${{ matrix.gcc }}
run: |
cd $RUNNER_TEMP
mkdir build
cd build
cmake $GITHUB_WORKSPACE/c_cxx -DONNXRUNTIME_ROOTDIR=$RUNNER_TEMP/onnxruntimebin
make -j$(nproc)
name: Linux-CPU
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
Linux-CPU-CI-x64:
runs-on: ubuntu-latest
strategy:
matrix:
gcc: [9, 10]
steps:
- name: Check out repository code
uses: actions/checkout@v2
- name: Download onnxruntime binary
run: |
aria2c -q -d $RUNNER_TEMP https://github.com/microsoft/onnxruntime/releases/download/v1.9.0/onnxruntime-linux-x64-1.9.0.tgz
cd $RUNNER_TEMP
mkdir onnxruntimebin
cd onnxruntimebin
tar --strip=1 -zxvf $RUNNER_TEMP/onnxruntime-linux-x64-1.9.0.tgz
- name: Build
env:
CC: gcc-${{ matrix.gcc }}
CXX: g++-${{ matrix.gcc }}
run: |
cd $RUNNER_TEMP
mkdir build
cd build
cmake $GITHUB_WORKSPACE/c_cxx -DONNXRUNTIME_ROOTDIR=$RUNNER_TEMP/onnxruntimebin
make -j$(nproc)
118 changes: 59 additions & 59 deletions .github/workflows/win-ci.yml
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
name: Windows-CPU
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
Windows-CPU-CI-x64:
runs-on: windows-2019
steps:
- name: Check out repository code
uses: actions/checkout@v2
- run: C:\vcpkg\vcpkg.exe install libjpeg-turbo:x64-windows-static zlib:x64-windows-static libpng:x64-windows-static
- name: Download onnxruntime binary
run: |
choco install aria2 -y
aria2c -q -d $Env:RUNNER_TEMP -o onnxruntime.zip https://github.com/microsoft/onnxruntime/releases/download/v1.8.1/onnxruntime-win-x64-1.8.1.zip
cd $Env:RUNNER_TEMP
7z x $Env:RUNNER_TEMP/onnxruntime.zip
move onnxruntime-win-x64-1.8.1 onnxruntimebin
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.0.2
with:
vs-version: '[16.10,)'
- name: Run cmake
run: |
cd $Env:RUNNER_TEMP
mkdir build
cd build
cmake $Env:GITHUB_WORKSPACE\c_cxx -A x64 -T host=x64 -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static "-DONNXRUNTIME_ROOTDIR=$Env:RUNNER_TEMP\onnxruntimebin"
- name: Build
run: msbuild $Env:RUNNER_TEMP\build\onnxruntime_samples.sln /nologo /p:platform="x64" /p:configuration="Release" /p:VisualStudioVersion="16.0" /m
Windows-CPU-CI-x86:
runs-on: windows-2019
steps:
- name: Check out repository code
uses: actions/checkout@v2
- run: C:\vcpkg\vcpkg.exe install libjpeg-turbo:x86-windows-static zlib:x86-windows-static libpng:x86-windows-static
- name: Download onnxruntime binary
run: |
choco install aria2 -y
aria2c -q -d $Env:RUNNER_TEMP -o onnxruntime.zip https://github.com/microsoft/onnxruntime/releases/download/v1.8.1/onnxruntime-win-x86-1.8.1.zip
cd $Env:RUNNER_TEMP
7z x $Env:RUNNER_TEMP/onnxruntime.zip
move onnxruntime-win-x86-1.8.1 onnxruntimebin
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.0.2
with:
vs-version: '[16.10,)'
- name: Run cmake
run: |
cd $Env:RUNNER_TEMP
mkdir build
cd build
cmake $Env:GITHUB_WORKSPACE\c_cxx -A Win32 -T host=x64 -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x86-windows-static "-DONNXRUNTIME_ROOTDIR=$Env:RUNNER_TEMP\onnxruntimebin"
- name: Build
run: msbuild $Env:RUNNER_TEMP\build\onnxruntime_samples.sln /nologo /p:platform="Win32" /p:configuration="Release" /p:VisualStudioVersion="16.0" /m
name: Windows-CPU
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
Windows-CPU-CI-x64:
runs-on: windows-2019
steps:
- name: Check out repository code
uses: actions/checkout@v2
- run: C:\vcpkg\vcpkg.exe install libjpeg-turbo:x64-windows-static zlib:x64-windows-static libpng:x64-windows-static
- name: Download onnxruntime binary
run: |
choco install aria2 -y
aria2c -q -d $Env:RUNNER_TEMP -o onnxruntime.zip https://github.com/microsoft/onnxruntime/releases/download/v1.9.0/onnxruntime-win-x64-1.9.0.zip
cd $Env:RUNNER_TEMP
7z x $Env:RUNNER_TEMP/onnxruntime.zip
move onnxruntime-win-x64-1.9.0 onnxruntimebin
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.0.2
with:
vs-version: '[16.10,)'
- name: Run cmake
run: |
cd $Env:RUNNER_TEMP
mkdir build
cd build
cmake $Env:GITHUB_WORKSPACE\c_cxx -A x64 -T host=x64 -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static "-DONNXRUNTIME_ROOTDIR=$Env:RUNNER_TEMP\onnxruntimebin"
- name: Build
run: msbuild $Env:RUNNER_TEMP\build\onnxruntime_samples.sln /nologo /p:platform="x64" /p:configuration="Release" /p:VisualStudioVersion="16.0" /m
Windows-CPU-CI-x86:
runs-on: windows-2019
steps:
- name: Check out repository code
uses: actions/checkout@v2
- run: C:\vcpkg\vcpkg.exe install libjpeg-turbo:x86-windows-static zlib:x86-windows-static libpng:x86-windows-static
- name: Download onnxruntime binary
run: |
choco install aria2 -y
aria2c -q -d $Env:RUNNER_TEMP -o onnxruntime.zip https://github.com/microsoft/onnxruntime/releases/download/v1.9.0/onnxruntime-win-x86-1.9.0.zip
cd $Env:RUNNER_TEMP
7z x $Env:RUNNER_TEMP/onnxruntime.zip
move onnxruntime-win-x86-1.9.0 onnxruntimebin
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.0.2
with:
vs-version: '[16.10,)'
- name: Run cmake
run: |
cd $Env:RUNNER_TEMP
mkdir build
cd build
cmake $Env:GITHUB_WORKSPACE\c_cxx -A Win32 -T host=x64 -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x86-windows-static "-DONNXRUNTIME_ROOTDIR=$Env:RUNNER_TEMP\onnxruntimebin"
- name: Build
run: msbuild $Env:RUNNER_TEMP\build\onnxruntime_samples.sln /nologo /p:platform="Win32" /p:configuration="Release" /p:VisualStudioVersion="16.0" /m

82 changes: 53 additions & 29 deletions c_cxx/fns_candy_style_transfer/fns_candy_style_transfer.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "onnxruntime_c_api.h"
#include "providers.h"
#include <stdio.h>
#include <assert.h>
#include <png.h>
#include <stdio.h>

#include "onnxruntime_c_api.h"
#ifdef _WIN32
#ifdef USE_DML
#include "providers.h"
#endif
#include <objbase.h>
#endif

#ifdef _WIN32
#define tcscmp wcscmp
#define tcscmp wcscmp
#else
#define tcscmp strcmp
#define tcscmp strcmp
#endif

const OrtApi* g_ort = NULL;
Expand Down Expand Up @@ -163,11 +166,11 @@ int run_inference(OrtSession* session, const ORTCHAR_T* input_file, const ORTCHA
float* model_input;
size_t model_input_ele_count;
#ifdef _WIN32
char* output_file_p = convert_string(output_file);
char* input_file_p = convert_string(input_file);
const char* output_file_p = convert_string(output_file);
const char* input_file_p = convert_string(input_file);
#else
char* output_file_p = output_file;
char* input_file_p = input_file;
const char* output_file_p = output_file;
const char* input_file_p = input_file;
#endif
if (read_png_file(input_file_p, &input_height, &input_width, &model_input, &model_input_ele_count) != 0) {
return -1;
Expand Down Expand Up @@ -195,8 +198,8 @@ int run_inference(OrtSession* session, const ORTCHAR_T* input_file, const ORTCHA
const char* input_names[] = {"inputImage"};
const char* output_names[] = {"outputImage"};
OrtValue* output_tensor = NULL;
ORT_ABORT_ON_ERROR(
g_ort->Run(session, NULL, input_names, (const OrtValue* const*)&input_tensor, 1, output_names, 1, &output_tensor));
ORT_ABORT_ON_ERROR(g_ort->Run(session, NULL, input_names, (const OrtValue* const*)&input_tensor, 1, output_names, 1,
&output_tensor));
assert(output_tensor != NULL);
ORT_ABORT_ON_ERROR(g_ort->IsTensor(output_tensor, &is_tensor));
assert(is_tensor);
Expand All @@ -222,11 +225,24 @@ void verify_input_output_count(OrtSession* session) {
assert(count == 1);
}

#ifdef USE_CUDA
void enable_cuda(OrtSessionOptions* session_options) {
ORT_ABORT_ON_ERROR(OrtSessionOptionsAppendExecutionProvider_CUDA(session_options, 0));
int enable_cuda(OrtSessionOptions* session_options) {
// OrtCUDAProviderOptions is a C struct. C programming language doesn't have constructors/destructors.
OrtCUDAProviderOptions o;
// Here we use memset to initialize every field of the above data struct to zero.
memset(&o, 0, sizeof(o));
// But is zero a valid value for every variable? Not quite. It is not guaranteed. In the other words: does every enum
// type contain zero? The following line can be omitted because EXHAUSTIVE is mapped to zero in onnxruntime_c_api.h.
o.cudnn_conv_algo_search = EXHAUSTIVE;
o.gpu_mem_limit = SIZE_MAX;
OrtStatus* onnx_status = g_ort->SessionOptionsAppendExecutionProvider_CUDA(session_options, &o);
if (onnx_status != NULL) {
const char* msg = g_ort->GetErrorMessage(onnx_status);
fprintf(stderr, "%s\n", msg);
g_ort->ReleaseStatus(onnx_status);
return -1;
}
return 0;
}
#endif

#ifdef USE_DML
void enable_dml(OrtSessionOptions* session_options) {
Expand All @@ -245,49 +261,57 @@ int main(int argc, char* argv[]) {
}

g_ort = OrtGetApiBase()->GetApi(ORT_API_VERSION);
if (!g_ort) {
fprintf(stderr, "Failed to init ONNX Runtime engine.\n");
return -1;
}
#ifdef _WIN32
//CoInitializeEx is only needed if Windows Image Component will be used in this program for image loading/saving.
// CoInitializeEx is only needed if Windows Image Component will be used in this program for image loading/saving.
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (!SUCCEEDED(hr)) return -1;
#endif
ORTCHAR_T* model_path = argv[1];
ORTCHAR_T* input_file = argv[2];
ORTCHAR_T* output_file = argv[3];
// By default it will try CUDA first. If CUDA is not available, it will run all the things on CPU.
// But you can also explicitly set it to DML(directml) or CPU(which means cpu-only).
ORTCHAR_T* execution_provider = (argc >= 5) ? argv[4] : NULL;
OrtEnv* env;
ORT_ABORT_ON_ERROR(g_ort->CreateEnv(ORT_LOGGING_LEVEL_WARNING, "test", &env));
assert(env != NULL);
int ret = 0;
OrtSessionOptions* session_options;
ORT_ABORT_ON_ERROR(g_ort->CreateSessionOptions(&session_options));

if (execution_provider)
{
if (execution_provider) {
if (tcscmp(execution_provider, ORT_TSTR("cpu")) == 0) {
// Nothing; this is the default
} else if (tcscmp(execution_provider, ORT_TSTR("cuda")) == 0) {
#ifdef USE_CUDA
enable_cuda(session_options);
#else
puts("CUDA is not enabled in this build.");
return -1;
#endif
} else if (tcscmp(execution_provider, ORT_TSTR("dml")) == 0) {
#ifdef USE_DML
#ifdef USE_DML
enable_dml(session_options);
#else
#else
puts("DirectML is not enabled in this build.");
return -1;
#endif
#endif
} else {
usage();
puts("Invalid execution provider option.");
return -1;
}
} else {
printf("Try to enable CUDA first\n");
ret = enable_cuda(session_options);
if (ret) {
fprintf(stderr, "CUDA is not available\n");
} else {
printf("CUDA is enabled\n");
}
}

OrtSession* session;
ORT_ABORT_ON_ERROR(g_ort->CreateSession(env, model_path, session_options, &session));
verify_input_output_count(session);
int ret = run_inference(session, input_file, output_file);
ret = run_inference(session, input_file, output_file);
g_ort->ReleaseSessionOptions(session_options);
g_ort->ReleaseSession(session);
g_ort->ReleaseEnv(env);
Expand Down
2 changes: 1 addition & 1 deletion mobile/examples/basic_usage/model/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
onnx==1.8.0
onnxruntime==1.8.1
onnxruntime==1.9.0

0 comments on commit e4b52d9

Please sign in to comment.