Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Remove unneeded environment configurations
Browse files Browse the repository at this point in the history
Summary:
* 0.2.0 drops Python requirement and fixes RN 0.68 CMake issue
* 0.2.1 drops sonatype config and the need for manual JSI packages installation

Reviewed By: raedle

Differential Revision: D38726071

fbshipit-source-id: 5ffebc2d285c9f31e19f78207893fed703ee7560
  • Loading branch information
chrisklaiber authored and facebook-github-bot committed Aug 16, 2022
1 parent d8ed148 commit 952b050
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 133 deletions.
128 changes: 0 additions & 128 deletions website/docs/tutorials/add-package.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ If you have an existing React Native project and you want to add ML capabilities
yarn add react-native-pytorch-core
```

:::caution

If you are on React Native 0.68+, you will have to use the `nightly` version of the core package. A fix has been added in commit [fix RN0.68 can't build with react-native-pytorch-core](https://github.com/facebookresearch/playtorch/commit/2cf281fea6fca9e2615b46045a3b13fb08eb93c5), which is currently only available in the `nightly` version.

```shell
yarn add react-native-pytorch-core@nightly
```

:::

On iOS you are done, but Android requires the following additional steps for the `react-native-pytorch-core` package to work.

## Additional Assets for Metro
Expand Down Expand Up @@ -76,24 +66,6 @@ Execution failed for task ':app:packageDebug'.
> java.lang.OutOfMemoryError (no error message)
```

### Sonatype Repository

The PyTorch Mobile for Android dependencies are in the Sonatype repository. Add the repository url to the `allprojects > repositories`.

```shell title="./android/build.gradle"
allprojects {
repositories {
// ...

maven {
url("https://oss.sonatype.org/content/repositories/snapshots")
}

// ...
}
}
```

### Update app build.gradle

Both React Native and PyTorch Mobile for Android use `fbjni`. For example, the versions for PlayTorch that are used for development are:
Expand Down Expand Up @@ -184,106 +156,6 @@ tasks.whenTaskAdded { task ->
// ...
```
### Configure JavaScript Interface for React Native app
Last, add the `PyTorchCoreJSIModulePackage` to the `ReactNativeHost` in the app's `MainApplication.java`.
:::info
**JavaScript Interface (JSI)**
A lightweight and VM-independent layer that helps in communication between JavaScript and native platforms easier and faster. It supports Webkit JSC, Custom JSC, and [Hermes](https://hermesengine.dev/).
**JSI Documentation**
The [jsi.h](https://github.com/facebook/hermes/blob/main/API/jsi/jsi/jsi.h) in the Hermes GitHub repository and has a really well-documented C++ header, which is a great place to learn more about JSI.
PlayTorch uses the JavaScript Interface (JSI) to expose PyTorch Mobile C++ functions
:::
```java title="./android/app/src/main/java/<your-package-path>/MainApplication.java"
import com.facebook.react.bridge.JSIModulePackage;
import org.pytorch.rn.core.jsi.PyTorchCoreJSIModulePackage;
// ...
new ReactNativeHostWrapper(
this,
new ReactNativeHost(this) {
// ...
@Override
protected JSIModulePackage getJSIModulePackage() {
return new PyTorchCoreJSIModulePackage();
}
});
```
Great! You are done if you don't use other React Native packages that rely on JSI, otherwise continue!
#### Configure multiple React Native packages using JSI
If you use other React Native packages with JSI packages, you will have to create a `JSIModulePackage` that combines all of them. For example, if you use React Reanimated and PlayTorch Core create a `CustomJSIModulePackage` and return this in the `ReactNativeHost` implementation.
```java title="./android/app/src/main/java/<your-package-path>/CustomJSIModulePackage.java"
package <your-package-path>;
import androidx.annotation.Keep;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.bridge.JSIModulePackage;
import com.facebook.react.bridge.JSIModuleSpec;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.soloader.SoLoader;
import com.swmansion.reanimated.ReanimatedJSIModulePackage;
import java.util.List;
import org.pytorch.rn.core.jsi.PyTorchCoreJSIModulePackage;
/**
* This is a custom JSIModulePackage that combines ReanimatedJSIModulePackage and
* PyTorchCoreJSIModulePackage. It is needed because the ReactNativeHostWrapper only supports
* returning a single JSIModulePackage.
*/
@DoNotStrip
@Keep
public class CustomJSIModulePackage implements JSIModulePackage {
@DoNotStrip
@Keep
@Override
public List<JSIModuleSpec> getJSIModules(
ReactApplicationContext reactApplicationContext, JavaScriptContextHolder jsContext) {
ReanimatedJSIModulePackage reaJSIModulePackage = new ReanimatedJSIModulePackage();
PyTorchCoreJSIModulePackage ptlJSIModulePackage = new PyTorchCoreJSIModulePackage();
List<JSIModuleSpec> retList =
reaJSIModulePackage.getJSIModules(reactApplicationContext, jsContext);
ptlJSIModulePackage.getJSIModules(reactApplicationContext, jsContext);
return retList;
}
}
```
```java title="./android/app/src/main/java/<your-package-path>/MainApplication.java"
// ...
import com.facebook.react.bridge.JSIModulePackage;
new ReactNativeHostWrapper(
this,
new ReactNativeHost(this) {
// ...
@Override
protected JSIModulePackage getJSIModulePackage() {
// The CustomJSIModulePackage combines JSIModulePackage from different React Native
// packages (e.g., Reanimated2 and PlayTorch).
return new CustomJSIModulePackage();
}
});
```
That should be all necessary Gradle build configuration changes!
# Give us feedback
Expand Down
2 changes: 1 addition & 1 deletion website/docs/tutorials/get-started-manually.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ In this tutorial, you will manually set up the build environment, create your fi

## Installation

PlayTorch is powered by [PyTorch Mobile](https://pytorch.org/mobile) and [React Native](https://reactnative.dev/). Both are active and flourishing open source projects and require the following dependencies:
PlayTorch is powered by [PyTorch Mobile](https://pytorch.org/mobile) and [React Native](https://reactnative.dev/). Both are active and flourishing open source projects. The following dependencies are required:

<DevelopmentDependencies />

Expand Down
4 changes: 0 additions & 4 deletions website/docs/tutorials/snippets/_development-dependencies.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
**PyTorch Mobile**

* [Python](https://www.python.org/)

**React Native**

* Java Development Kit (any of the following)
Expand Down

1 comment on commit 952b050

@vercel
Copy link

@vercel vercel bot commented on 952b050 Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.