Skip to content

Commit a54716e

Browse files
xzhu1900hariharans29Thiago CrepaldiyufengleeSherlockNoMad
authored
cherry pick outstanding commits (microsoft#7871)
* Fix bug in Transpose CUDA kernel (microsoft#7329) * Fix permission error for ORTModule lock file (microsoft#7814) * fix topo sort in quant tool (microsoft#7833) * fix topo sort in quant tool * add unit test and make the topo sort stable * Relax tol for Conv1D fp16 test (microsoft#7844) * Relax tol for Conv1D fp16 test Co-authored-by: Sherlock Huang <bahuang@OrtTrainingDev3.af05slrtruoetgaxwwjv5nsq5e.px.internal.cloudapp.net> * Resolve issue with wrapped ORTModule load_state_dict (microsoft#7847) * Encapsulate children modules inside a ModuleAccessor object to prevent erroneuos iteration over children while loading the state dictionary * Add named_models, models, apply methods, change ModuleAccessor to ModuleMetadata and modify unit tests * Change ModuleMetadata module getter logic, raise NotImplementedError for add_modules * Add comment explaining why overriding _load_from_state_dict method is needed * fixed bugs in packed mode and enable pack mode tests in ci (microsoft#7848) * fixed bugs in packed mode and enable pack mode tests in ci * removed unnecessary space * pr comments * pr comments * disable an average pool test * try disabling another avg pool * disable more avg pool tests * disable maxpool tests * add environment variable to control default training package's local version (microsoft#7849) * [js] update documents (microsoft#7852) * [js] update documents * escape double quotes * update operators.md * resolve comments * Support bool type for Pad CPU (microsoft#7856) * Initial commit * update * nit * Include ORT C/C++ API headers in the ORT Mobile AAR package (microsoft#7858) * Add header files of ort c/c++ api to aar package * Move header file selection to cmake based on EP choice * fix duplicated node name (microsoft#7865) * Clean up CPU kernel definition for opset 13 Pad (microsoft#7867) Co-authored-by: Hariharan Seshadri <[email protected]> Co-authored-by: Thiago Crepaldi <[email protected]> Co-authored-by: Yufeng Li <[email protected]> Co-authored-by: Sherlock <[email protected]> Co-authored-by: Sherlock Huang <bahuang@OrtTrainingDev3.af05slrtruoetgaxwwjv5nsq5e.px.internal.cloudapp.net> Co-authored-by: baijumeswani <[email protected]> Co-authored-by: Tixxx <[email protected]> Co-authored-by: liqunfu <[email protected]> Co-authored-by: Yulong Wang <[email protected]> Co-authored-by: Guoyu Wang <[email protected]> Co-authored-by: Tianlei Wu <[email protected]>
1 parent c487824 commit a54716e

File tree

36 files changed

+1127
-312
lines changed

36 files changed

+1127
-312
lines changed

cmake/onnxruntime.cmake

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
1414
set(OUTPUT_STYLE xcode)
1515
endif()
1616

17+
# This macro is to get the path of header files for mobile packaging, for iOS and Android
18+
macro(get_mobile_api_headers _HEADERS)
19+
# include both c and cxx api
20+
set(${_HEADERS}
21+
"${REPO_ROOT}/include/onnxruntime/core/session/onnxruntime_c_api.h"
22+
"${REPO_ROOT}/include/onnxruntime/core/session/onnxruntime_cxx_api.h"
23+
"${REPO_ROOT}/include/onnxruntime/core/session/onnxruntime_cxx_inline.h"
24+
)
25+
26+
# need to add header files for enabled EPs
27+
foreach(f ${ONNXRUNTIME_PROVIDER_NAMES})
28+
file(GLOB _provider_headers CONFIGURE_DEPENDS
29+
"${REPO_ROOT}/include/onnxruntime/core/providers/${f}/*.h"
30+
)
31+
list(APPEND ${_HEADERS} "${_provider_headers}")
32+
unset(_provider_headers)
33+
endforeach()
34+
endmacro()
35+
1736
#If you want to verify if there is any extra line in symbols.txt, run
1837
# nm -C -g --defined libonnxruntime.so |grep -v '\sA\s' | cut -f 3 -d ' ' | sort
1938
# after build
@@ -39,21 +58,7 @@ if(WIN32)
3958
"${ONNXRUNTIME_ROOT}/core/dll/onnxruntime.rc"
4059
)
4160
elseif(onnxruntime_BUILD_APPLE_FRAMEWORK)
42-
# include both c and cxx api
43-
set(APPLE_FRAMEWORK_HEADERS
44-
"${REPO_ROOT}/include/onnxruntime/core/session/onnxruntime_c_api.h"
45-
"${REPO_ROOT}/include/onnxruntime/core/session/onnxruntime_cxx_api.h"
46-
"${REPO_ROOT}/include/onnxruntime/core/session/onnxruntime_cxx_inline.h"
47-
)
48-
49-
# need to add header files for enabled EPs
50-
foreach(f ${ONNXRUNTIME_PROVIDER_NAMES})
51-
file(GLOB _provider_headers CONFIGURE_DEPENDS
52-
"${REPO_ROOT}/include/onnxruntime/core/providers/${f}/*.h"
53-
)
54-
list(APPEND APPLE_FRAMEWORK_HEADERS "${_provider_headers}")
55-
unset(_provider_headers)
56-
endforeach()
61+
get_mobile_api_headers(APPLE_FRAMEWORK_HEADERS)
5762

5863
# apple framework requires the header file be part of the library
5964
onnxruntime_add_shared_library(onnxruntime
@@ -132,6 +137,18 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Android" OR (onnxruntime_MINIMAL_BUILD AND UNIX))
132137
endif()
133138
endif()
134139

140+
# we need to copy C/C++ API headers to be packed into Android AAR package
141+
if(CMAKE_SYSTEM_NAME STREQUAL "Android" AND onnxruntime_BUILD_JAVA)
142+
get_mobile_api_headers(ANDROID_AAR_HEADERS)
143+
set(ANDROID_HEADERS_DIR ${CMAKE_CURRENT_BINARY_DIR}/android/headers)
144+
file(MAKE_DIRECTORY ${ANDROID_HEADERS_DIR})
145+
# copy the header files one by one
146+
foreach(h_ ${ANDROID_AAR_HEADERS})
147+
get_filename_component(HEADER_NAME_ ${h_} NAME)
148+
configure_file(${h_} ${ANDROID_HEADERS_DIR}/${HEADER_NAME_} COPYONLY)
149+
endforeach()
150+
endif()
151+
135152
target_link_libraries(onnxruntime PRIVATE
136153
onnxruntime_session
137154
${onnxruntime_libs}

js/README.md

Lines changed: 146 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This directory contains multiple NPM projects:
77
- [onnxruntime-web](#onnxruntime-web)
88
- [onnxruntime-react-native](#onnxruntime-react-native)
99

10-
### Development
10+
## Development
1111

1212
This folder contains a `.vscode` folder for Visual Studio Code workspace configs. Using VSCode to open this folder
1313
will allow code-formatting and linting features on typescript and C/C++ source code inside this folder. Following files
@@ -19,20 +19,45 @@ are used for code-formatting and linting features for developers:
1919
- .eslintrc.js
2020
- .clang-format
2121

22-
#### Using VSCode:
22+
Please follow the steps described below to setup development environment.
2323

24-
1. in `<ORT_ROOT>/js`, run:
25-
> npm ci
26-
2. use VSCode to open folder `<ORT_ROOT>/js`
27-
3. install VSCode extension if not installed yet:
28-
- Clang-Format
29-
- ESLint
24+
### Prerequisites
3025

31-
To populate typescript type declarations, in each projects, run `npm ci`.
26+
- Node.js (14.0+): https://nodejs.org/ - (Optional) Use nvm ([Windows](https://github.com/coreybutler/nvm-windows) / [Mac/Linux](https://github.com/creationix/nvm)) to install Node.js
3227

33-
#### Run code formatter and linter manually
28+
- Python (2.7 or 3.6+): https://www.python.org/downloads/
3429

35-
in `<ORT_ROOT>/js`, use `npm run lint` to run ESLint , and use `npm run format` to run clang-format.
30+
- python should be added to the PATH environment variable
31+
32+
- Visual Studio Code: https://code.visualstudio.com/
33+
34+
- **required** extension: [ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)
35+
- **required** extension: [Clang-Format](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format)
36+
- **required** extension: [Debugger for Chrome](https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome)
37+
38+
- Chrome or Edge Browser
39+
40+
### Setup TypeScript development environment
41+
42+
In `<ORT_ROOT>/js`, run:
43+
44+
```
45+
npm ci
46+
```
47+
48+
This will install Clang-format and ESLint for code-formatting and linting features. This is a one-time setup unless a `git clean` is performed or folder `<ORT_ROOT>/js/node_modules` is removed manually.
49+
50+
### Using VSCode:
51+
52+
Use VSCode to open folder `<ORT_ROOT>/js`.
53+
54+
Make sure to open the correct folder to allow VSCode to load workspace configuration. Otherwise typescript and code formatter may not work as expected.
55+
56+
To populate typescript type declarations, in each project folder, run `npm ci`.
57+
58+
### Run code formatter and linter manually
59+
60+
In `<ORT_ROOT>/js`, use `npm run lint` to run ESLint , and use `npm run format` to run clang-format.
3661

3762
## onnxruntime-common
3863

@@ -112,10 +137,6 @@ It should be able to consumed by from projects that uses NPM packages (through a
112137
113138
This project is a library for running ONNX models on browsers. It is the successor of [ONNX.js](https://github.com/Microsoft/onnxjs).
114139

115-
### Requirements
116-
117-
Node.js v12+ (recommended v14+)
118-
119140
### Build
120141

121142
1. Install NPM packages
@@ -124,21 +145,26 @@ Node.js v12+ (recommended v14+)
124145
2. in `<ORT_ROOT>/js/common/`, run `npm ci`.
125146
3. in `<ORT_ROOT>/js/web/`, run `npm ci`.
126147

127-
2. ~~Follow [instructions](https://www.onnxruntime.ai/docs/how-to/build.html#apis-and-language-bindings) for building ONNX Runtime WebAssembly. (TODO: document is not ready. we are working on it.)~~
148+
2. ~~Follow [instructions](https://www.onnxruntime.ai/docs/how-to/build.html#apis-and-language-bindings) for building ONNX Runtime WebAssembly. (TODO: document is not ready. we are working on it. Please see steps described as below.)~~
128149

129-
in `<ORT_ROOT>/`, run either of the following commands to build WebAssembly:
150+
in `<ORT_ROOT>/`, run one of the following commands to build WebAssembly:
130151

131152
```sh
132153
# In windows, use 'build' to replace './build.sh'
133154

134155
# The following command build debug.
135156
./build.sh --build_wasm
136157

158+
# The following command build debug with debug info.
159+
./build.sh --build_wasm --skip_tests --enable_wasm_debug_info
160+
137161
# The following command build release.
138162
./build.sh --config Release --build_wasm --skip_tests --disable_wasm_exception_catching --disable_rtti
139163
```
140164

141-
To build with multi-thread support, append flag ` --enable_wasm_threads` to the command. Make sure to build both single-thread and multi-thread before next step.
165+
To build with multi-thread support, append flag `--enable_wasm_threads` to the command. Make sure to build both single-thread and multi-thread before next step.
166+
167+
NOTE: You can also find latest build artifacts on [Windows WebAssembly CI Pipeline](https://dev.azure.com/onnxruntime/onnxruntime/_build?definitionId=161&_a=summary&repositoryFilter=1&branchFilter=4%2C4%2C4%2C4%2C4%2C4). Choose any build for master branch, download artifacts "Release_ort-wasm" and "Release_ort-wasm-threaded" and unzip.
142168

143169
3. Copy following files from build output folder to `<ORT_ROOT>/js/web/dist/`:
144170

@@ -156,9 +182,91 @@ Node.js v12+ (recommended v14+)
156182
npm run build
157183
```
158184

185+
### Test
186+
187+
We use command `npm test` (test runner) and `npm run test:e2e` (E2E test) for tests in ONNXRuntime Web.
188+
189+
#### test runner
190+
191+
In folder `<ORT_ROOT>/js/web`,
192+
193+
- Run `npm test -- --help` for a full CLI instruction.
194+
- Run `npm test -- <your-args> --debug` to run one or more test cases.
195+
196+
There are multiple levels of tests for ONNXRuntime Web:
197+
198+
- unit test: tests for individual components written in TypeScript. Launch unit test by:
199+
```
200+
npm test -- unittest
201+
```
202+
- model test: run a single model. The model folder should contains one .onnx model file and one or more folders for test cases, each folder contains several input*\*.pb and output*\*.pb as test data. Launch model test by:
203+
```
204+
npm test -- model <model_folder>
205+
```
206+
- op test: test a single operator. An op test is described in a `.jsonc` file which specify the operator type, its attributes and one or more test case(s), each includes a list of expected input tensor(s) and output tensor(s). The `.jsonc` file is located at `<ORT_ROOT>/js/web/test/data/ops`. Launch op test by:
207+
208+
```
209+
npm test -- op <file_name>
210+
```
211+
212+
- suite test: suite test includes unit test, a list of model tests and op tests. Launch suite test by:
213+
```
214+
npm test
215+
```
216+
217+
#### E2E test
218+
219+
E2E test is for testing end-to-end package consuming. In this test, NPM packages for `onnxruntime-common` and `onnxruntime-web` are generated and a clean folder is used for installing packages. Then a simple mocha test is performed to make sure package can be consumed correctly.
220+
221+
To launch E2E test:
222+
223+
```
224+
npm run test:e2e
225+
```
226+
227+
### Debugging
228+
229+
#### Debugging TypeScript on Desktop/Chrome
230+
231+
To debug the code from test-runner on Chrome:
232+
233+
- Launch `npm test -- <your_args> --debug`. It opens an instance of Chrome browser.
234+
- In the open Chrome browser, click the `DEBUG` button on the top-right of the page.
235+
- In VSCode, click [side bar]->Run and Debug->select [Attach to Chrome]->click [Start Debugging] to attach.
236+
- put breakpoints in source code, and Refresh the page to reload.
237+
238+
#### Debugging TypeScript on iOS/Safari
239+
240+
To debug on an Apple iOS device, please refer to the following steps:
241+
242+
- install [
243+
RemoteDebug iOS WebKit Adapter](https://github.com/RemoteDebug/remotedebug-ios-webkit-adapter) by following its instructions.
244+
- launch the adapter in commandline: `remotedebug_ios_webkit_adapter --port=9000`.
245+
- in VSCode, select debug configuration `Remote Browser via Webkit Adaptor`.
246+
- follow the steps above to debug.
247+
248+
#### Debugging TypeScript on Android/Chrome
249+
250+
To debug on an Android device, please refer to the following steps:
251+
252+
- Install [Android SDK Platform Tools](https://developer.android.com/studio/releases/platform-tools) and make sure `adb` is ready to use.
253+
- Follow instructions in [Remote Debugging on Android](https://developer.chrome.com/devtools/docs/remote-debugging-legacy) to launch `adb`. Make sure to use port 9000 so that the existing debug configuration works.
254+
- in VSCode, select debug configuration `Remote Browser via Webkit Adaptor`.
255+
- follow the steps above to debug.
256+
257+
#### Debugging C/C++ for ONNX Runtime WebAssembly
258+
259+
To debug C/C++ code for ONNX Runtime WebAssembly, you need to build ONNX Runtime with debug info (see [Build](#Build-2)).
260+
261+
Currently debugging C/C++ code in WebAssembly is not supported in VSCode yet. Please follow [this instruction](https://developer.chrome.com/blog/wasm-debugging-2020/) to debug in browser devtool using extension [C/C++ DevTools Support (DWARF)](https://chrome.google.com/webstore/detail/cc%20%20-devtools-support-dwa/pdcpmagijalfljmkmjngeonclgbbannb).
262+
263+
### Generating Document
264+
265+
Use command `npm run build:doc` to generate the latest documents.
266+
159267
### Distribution
160268

161-
It should be able to consumed by both from projects that uses NPM packages (through a Node.js folder structure of `node_modules` folder that generated by `npm install onnxruntime-web`) and from a CDN service that serves a `.min.js` file and one or multiple `.wasm` file(s).
269+
It should be able to consumed by both from projects that uses NPM packages (through a Node.js folder structure of `node_modules` folder that generated by `npm install onnxruntime-web`) and from a CDN service that serves a `ort.min.js` file and one or multiple `.wasm` file(s).
162270

163271
## onnxruntime-react-native
164272

@@ -192,54 +300,61 @@ This project provides an ONNX Runtime React Native JavaScript library to run ONN
192300
1. Set up an Android build environment referring to [instruction](https://www.onnxruntime.ai/docs/how-to/build.html#android)
193301

194302
2. In `<ORT_ROOT>`, run this python script to build ONNX Runtime Android archive file. In windows, this requires admin account to build. If an app uses a fixed set of models, refer to [instruction](https://www.onnxruntime.ai/docs/how-to/build.html#android) and build a mobile version package
303+
195304
```python
196305
python tools/ci_build/github/android/build_aar_package.py js/react_native/scripts/aar_build_settings.json --config MinSizeRel --android_sdk_path <ANDROID_SDK_PATH> --android_ndk_path <ANDROID_NDK_PATH> --build_dir <BUILD_DIRECTORY>
197306
```
198307

199308
3. This generates `onnxruntime-mobile-<version>.aar` in `<BUILD_DIRECTORY>/aar_out/MinSizeRel/com/microsoft/onnxruntime/onnxruntime-mobile/<version>`. Copy `aar` file into `<ORT_ROOT>/js/react_native/android/libs` and rename it as `onnxruntime.aar`
200309

201310
4. To verify, open Android Emulator and run this command from `<ORT_ROOT>/js/react_native/android`
311+
202312
```sh
203313
adb shell am instrument -w ai.onnxruntime.react_native.test/androidx.test.runner.AndroidJUnitRunner
204314
```
205315

206316
3. Build iOS ONNX Runtime package
207317

208318
1. Set up iOS build environment referring to [instruction](https://www.onnxruntime.ai/docs/how-to/build.html#ios).
209-
319+
210320
2. Build ONNX Runtime library for iOS from `<ORT_ROOT>` using this command,
321+
211322
```sh
212323
./build.sh --config MinSizeRel --use_xcode --ios --ios_sysroot iphoneos --osx_arch arm64 --apple_deploy_target 11
213324
```
325+
214326
Copy `<ORT_ROOT>/build/iOS/MinSizeRel/MinSizeRel-iphoneos/libonnxruntime.<version>.dylib` file into `<ORT_ROOT>/js/react_native/ios/Libraries/onnxruntime/lib/iphoneos`
215327

216328
3. Clean up the previous build and build ONNX Runtime library for iOS Simulator from `<ORT_ROOT>`
329+
217330
```sh
218331
./build.sh --config MinSizeRel --use_xcode --ios --ios_sysroot iphonesimulator --osx_arch x86_64 --apple_deploy_target 11
219332
```
333+
220334
Copy `<ORT_ROOT>/build/iOS/MinSizeRel/MinSizeRel-iphonesimulator/libonnxruntime.<version>.dylib` file into `<ORT_ROOT>/js/react_native/ios/Libraries/onnxruntime/lib/iphonesimulator`
221-
335+
222336
4. Edit `onnxruntime-react-native.iphoneos.podspec` and `onnxruntime-react-native.iphonesimulator.podsepc` in `<ORT_ROOT>/js/react_native` to change a version of ONNX Runtime library.
223337

224338
5. Copy ONNX Runtime header files
339+
225340
```sh
226341
cp <ORT_ROOT>/include/onnxruntime/core/session/*.h <ORT_ROOT>/js/react_native/ios/Libraries/onnxruntime/include
227342
```
228343

229344
6. To verify, open iOS Simulator and run this command from `<ORT_ROOT>/js/react_native/ios`. Change a destination to specify a running iOS Simulator.
230-
```sh
231-
pod install
232-
export ONNXRUNTIME_VERSION=<version>; xcodebuild test -workspace OnnxruntimeModule.xcworkspace -scheme OnnxruntimeModuleTest -destination 'platform=iOS Simulator,name=iPhone 11,OS=14.5'
233-
```
345+
```sh
346+
pod install
347+
export ONNXRUNTIME_VERSION=<version>; xcodebuild test -workspace OnnxruntimeModule.xcworkspace -scheme OnnxruntimeModuleTest -destination 'platform=iOS Simulator,name=iPhone 11,OS=14.5'
348+
```
234349

235350
4. Update a version in `package.json` to align with ONNX Runtime version.
236351

237352
5. Test an example for Android and iOS. In Windows, open Android Emulator first. From `<ORT_ROOT>/js/react_native`
238-
```sh
239-
yarn bootstrap
240-
yarn example ios
241-
yarn example android
242-
```
353+
```sh
354+
yarn bootstrap
355+
yarn example ios
356+
yarn example android
357+
```
243358

244359
### NPM Packaging
245360

@@ -251,7 +366,7 @@ This project provides an ONNX Runtime React Native JavaScript library to run ONN
251366
252367
4. Run `npm publish <tgz> --dry-run` to see how it's going to be published
253368

254-
5. Run `npm publish <tgz>` to publish to npmjs
369+
5. Run `npm publish <tgz>` to publish to npmjs. If it's for a dev, add flag `--tag dev`.
255370
256371
### Distribution
257372

0 commit comments

Comments
 (0)