Skip to content

Commit f52ead4

Browse files
authored
Merge pull request pytorch#566 from jeffxtang/mobile_build_lib_from_src
updates of the lib build from source for ios and android
2 parents 45035ba + 90801ae commit f52ead4

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

_mobile/android.md

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ After that, the code processes the output, finding classes with the highest scor
203203

204204
## Building PyTorch Android from Source
205205

206-
In some cases you might want to use a local build of pytorch android, for example you may build custom libtorch binary with another set of operators or to make local changes.
206+
In some cases you might want to use a local build of PyTorch android, for example you may build custom libtorch binary with another set of operators or to make local changes, or try out the latest PyTorch code.
207207

208208
For this you can use `./scripts/build_pytorch_android.sh` script.
209209
```
@@ -218,11 +218,11 @@ The workflow contains several steps:
218218

219219
2\. Create symbolic links to the results of those builds:
220220
`android/pytorch_android/src/main/jniLibs/${abi}` to the directory with output libraries
221-
`android/pytorch_android/src/main/cpp/libtorch_include/${abi}` to the directory with headers. These directories are used to build `libpytorch.so` library that will be loaded on android device.
221+
`android/pytorch_android/src/main/cpp/libtorch_include/${abi}` to the directory with headers. These directories are used to build `libpytorch_jni.so` library, as part of the `pytorch_android-release.aar` bundle, that will be loaded on android device.
222222

223223
3\. And finally run `gradle` in `android/pytorch_android` directory with task `assembleRelease`
224224

225-
Script requires that Android SDK, Android NDK and gradle are installed.
225+
Script requires that Android SDK, Android NDK, Java SDK, and gradle are installed.
226226
They are specified as environment variables:
227227

228228
`ANDROID_HOME` - path to [Android SDK](https://developer.android.com/studio/command-line/sdkmanager.html)
@@ -231,17 +231,20 @@ They are specified as environment variables:
231231

232232
`GRADLE_HOME` - path to [gradle](https://gradle.org/releases/)
233233

234+
`JAVA_HOME` - path to [JAVA JDK](https://www.oracle.com/java/technologies/javase-downloads.html#javasejdk)
234235

235-
After successful build you should see the result as aar file:
236+
237+
After successful build, you should see the result as aar file:
236238

237239
```
238-
$ find pytorch_android/build/ -type f -name *aar
239-
pytorch_android/build/outputs/aar/pytorch_android.aar
240-
pytorch_android_torchvision/build/outputs/aar/pytorch_android.aar
241-
libs/fbjni_local/build/outputs/aar/pytorch_android_fbjni.aar
240+
$ find android -type f -name *aar
241+
android/pytorch_android/build/outputs/aar/pytorch_android-release.aar
242+
android/pytorch_android_torchvision/build/outputs/aar/pytorch_android_torchvision-release.aar
242243
```
243244

244-
It can be used directly in android projects, as a gradle dependency:
245+
## Using the PyTorch Android Libraries Built from Source or Nightly
246+
247+
First add the two aar files built above, or downloaded from the nightly built PyTorch Android repos at [here](https://oss.sonatype.org/#nexus-search;quick~pytorch_android) and [here](https://oss.sonatype.org/#nexus-search;quick~torchvision_android), to the Android project's `lib` folder, then add in the project's `build.gradle` file:
245248
```
246249
allprojects {
247250
repositories {
@@ -251,32 +254,23 @@ allprojects {
251254
}
252255
}
253256
254-
android {
255-
...
256-
packagingOptions {
257-
pickFirst "**/libfbjni.so"
258-
}
259-
...
260-
}
261-
262257
dependencies {
263-
implementation(name:'pytorch_android', ext:'aar')
264-
implementation(name:'pytorch_android_torchvision', ext:'aar')
265-
implementation(name:'pytorch_android_fbjni', ext:'aar')
258+
259+
// if using the libraries built from source
260+
implementation(name:'pytorch_android-release', ext:'aar')
261+
implementation(name:'pytorch_android_torchvision-release', ext:'aar')
262+
263+
// if using the nightly built libraries downloaded above, for example the 1.8.0-snapshot on Jan. 21, 2021
264+
// implementation(name:'pytorch_android-1.8.0-20210121.092759-172', ext:'aar')
265+
// implementation(name:'pytorch_android_torchvision-1.8.0-20210121.092817-173', ext:'aar')
266+
266267
...
267268
implementation 'com.android.support:appcompat-v7:28.0.0'
268-
implementation 'com.facebook.soloader:nativeloader:0.8.0'
269-
}
270-
```
271-
272-
At the moment for the case of using aar files directly we need additional configuration due to packaging specific (`libfbjni.so` is packaged in both `pytorch_android_fbjni.aar` and `pytorch_android.aar`).
273-
```
274-
packagingOptions {
275-
pickFirst "**/libfbjni.so"
269+
implementation 'com.facebook.fbjni:fbjni-java-only:0.0.3'
276270
}
277271
```
278272

279-
Also we have to add all transitive dependencies of our aars. As `pytorch_android` depends on `com.android.support:appcompat-v7:28.0.0` and `com.facebook.soloader:nativeloader:0.8.0`, we need to add them. (In case of using maven dependencies they are added automatically from `pom.xml`).
273+
Also we have to add all transitive dependencies of our aars. As `pytorch_android` depends on `com.android.support:appcompat-v7:28.0.0` or `androidx.appcompat:appcompat:1.2.0`, we need to one of them. (In case of using maven dependencies they are added automatically from `pom.xml`).
280274

281275

282276
## Custom Build

_mobile/ios.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ git submodule update --init --recursive
162162
163163
### Build LibTorch for iOS Simulators
164164

165-
Open terminal and navigate to the PyTorch root directory. Run the following command
165+
Open terminal and navigate to the PyTorch root directory. Run the following command (if you already build LibTorch for iOS devices (see below), run `rm -rf build_ios` first):
166166

167167
```
168168
BUILD_PYTORCH_MOBILE=1 IOS_PLATFORM=SIMULATOR ./scripts/build_ios.sh
@@ -171,7 +171,7 @@ After the build succeeds, all static libraries and header files will be generate
171171

172172
### Build LibTorch for arm64 Devices
173173

174-
Open terminal and navigate to the PyTorch root directory. Run the following command
174+
Open terminal and navigate to the PyTorch root directory. Run the following command (if you already build LibTorch for iOS simulators, run `rm -rf build_ios` first):
175175

176176
```
177177
BUILD_PYTORCH_MOBILE=1 IOS_ARCH=arm64 ./scripts/build_ios.sh
@@ -180,14 +180,25 @@ After the build succeeds, all static libraries and header files will be generate
180180

181181
### XCode Setup
182182

183-
Open your project in XCode, copy all the static libraries as well as header files to your project. Navigate to the project settings, set the value **Header Search Paths** to the path of header files you just copied.
183+
Open your project in XCode, go to your project Target's `Build Phases` - `Link Binaries With Libraries`, click the + sign and add all the library files located in `build_ios/install/lib`. Navigate to the project `Build Settings`, set the value **Header Search Paths** to `build_ios/install/include` and **Library Search Paths** to `build_ios/install/lib`.
184184

185185
In the build settings, search for **other linker flags**. Add a custom linker flag below
186186

187187
```
188188
-all_load
189189
```
190-
Finally, disable bitcode for your target by selecting the Build Settings, searching for **Enable Bitcode**, and set the value to **No**.
190+
191+
To use the custom built libraries the project, replace `#import <LibTorch/LibTorch.h>` (in `TorchModule.mm`) which is needed when using LibTorch via Cocoapods with the code below:
192+
```
193+
#include "ATen/ATen.h"
194+
#include "caffe2/core/timer.h"
195+
#include "caffe2/utils/string_utils.h"
196+
#include "torch/csrc/autograd/grad_mode.h"
197+
#include "torch/csrc/jit/serialization/import.h"
198+
#include "torch/script.h"
199+
```
200+
201+
Finally, disable bitcode for your target by selecting the Build Settings, searching for **Enable Bitcode**, and set the value to **No**.
191202

192203

193204

0 commit comments

Comments
 (0)