Repository: deepjavalibrary/djl
Pull request #1928 (2022-08-18 22:12:21)

[Android] Fixes torchSetGradMode bug for android
## Description ##

Brief description of what this PR is about

[Android] Don't support torchSetGradMode

- If this change is a backward incompatible change, why must this change be made?
- Interesting edge cases to note here



Patch:
diff --git a/.github/workflows/native_s3_pytorch_android.yml b/.github/workflows/native_s3_pytorch_android.yml
index 080e6ce85..ab536a0a0 100644
--- a/.github/workflows/native_s3_pytorch_android.yml
+++ b/.github/workflows/native_s3_pytorch_android.yml
@@ -33,14 +33,12 @@ jobs:
           export ANDROID_ABI=${{ matrix.format }}
           cd android_pytorch_tmp
           BUILD_LITE_INTERPRETER=0 ./scripts/build_android.sh
+          # PyTorch 1.12.1 padding.h include ATen/native/PadNd.h,
+          # but ATen/native/*.h files are excluded for mobile build.
+          # TODO: Remove it once PyTorch fix this problem.
           cp aten/src/ATen/native/PadNd.h build_android/install/include/ATen/native
           cd build_android
           zip -r ${{ matrix.format }}_native.zip install/include lib
-      - name: Upload pytorch src
-        uses: actions/upload-artifact@v3
-        with:
-          name: pytorch-src-${{ matrix.format }}
-          path: android_pytorch_tmp
       - name: Configure AWS Credentials
         uses: aws-actions/configure-aws-credentials@v1
         with:
@@ -51,3 +49,8 @@ jobs:
         run: |
           aws s3 cp android_pytorch_tmp/build_android/${{ matrix.format }}_native.zip s3://djl-ai/publish/pytorch/${PYTORCH_VERSION}/android_native/
           aws cloudfront create-invalidation --distribution-id E371VB8JQ6NRVY --paths "/pytorch/${PYTORCH_VERSION}/android_native*"
+      - name: Upload pytorch src
+        uses: actions/upload-artifact@v3
+        with:
+          name: pytorch-src-${{ matrix.format }}
+          path: android_pytorch_tmp
diff --git a/android/pytorch-native/README.md b/android/pytorch-native/README.md
index 1896a3aaa..d12c595ff 100644
--- a/android/pytorch-native/README.md
+++ b/android/pytorch-native/README.md
@@ -145,7 +145,7 @@ cp -r ../engines/pytorch/pytorch-native/jnilib/0.19.0/android/* pytorch-native/j
 ./gradlew pTML
 ```
 
-`ai.djl.android:pytorch-native:0.18.0-SNAPSHOT`  will be published to mavenLocal (your local ~/.m2 folder) if you ran `./gradlew pTML`
+`ai.djl.android:pytorch-native:0.19.0-SNAPSHOT` will be published to mavenLocal (your local ~/.m2 folder) if you ran `./gradlew pTML`
 
 ## Build PyTorch Android demo projects
 
diff --git a/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/NamedEntity.java b/api/src/main/java/ai/djl/modality/nlp/translator/NamedEntity.java
similarity index 98%
rename from extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/NamedEntity.java
rename to api/src/main/java/ai/djl/modality/nlp/translator/NamedEntity.java
index 19325d5e5..26841c603 100644
--- a/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/NamedEntity.java
+++ b/api/src/main/java/ai/djl/modality/nlp/translator/NamedEntity.java
@@ -10,7 +10,7 @@
  * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
  * and limitations under the License.
  */
-package ai.djl.huggingface.translator;
+package ai.djl.modality.nlp.translator;
 
 /** A class that represents a {@code NamedEntity} object. */
 public class NamedEntity {
diff --git a/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationServingTranslator.java b/api/src/main/java/ai/djl/modality/nlp/translator/TokenClassificationServingTranslator.java
similarity index 98%
rename from extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationServingTranslator.java
rename to api/src/main/java/ai/djl/modality/nlp/translator/TokenClassificationServingTranslator.java
index 172b836b4..9c54446a2 100644
--- a/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationServingTranslator.java
+++ b/api/src/main/java/ai/djl/modality/nlp/translator/TokenClassificationServingTranslator.java
@@ -10,7 +10,7 @@
  * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
  * and limitations under the License.
  */
-package ai.djl.huggingface.translator;
+package ai.djl.modality.nlp.translator;
 
 import ai.djl.modality.Input;
 import ai.djl.modality.Output;
diff --git a/engines/pytorch/pytorch-native/src/main/native/ai_djl_pytorch_jni_PyTorchLibrary_system.cc b/engines/pytorch/pytorch-native/src/main/native/ai_djl_pytorch_jni_PyTorchLibrary_system.cc
index 4f9b17b88..e464d579a 100644
--- a/engines/pytorch/pytorch-native/src/main/native/ai_djl_pytorch_jni_PyTorchLibrary_system.cc
+++ b/engines/pytorch/pytorch-native/src/main/native/ai_djl_pytorch_jni_PyTorchLibrary_system.cc
@@ -41,14 +41,20 @@ using namespace torch::autograd::profiler;
 
 JNIEXPORT jboolean JNICALL Java_ai_djl_pytorch_jni_PyTorchLibrary_torchIsGradMode(JNIEnv* env, jobject jthis) {
   API_BEGIN()
+#if defined(__ANDROID__)
+  return false;
+#else
   return c10::GradMode::is_enabled();
+#endif
   API_END_RETURN()
 }
 
 JNIEXPORT void JNICALL Java_ai_djl_pytorch_jni_PyTorchLibrary_torchSetGradMode(
     JNIEnv* env, jobject jthis, jboolean enable) {
   API_BEGIN()
+#if !defined(__ANDROID__)
   c10::GradMode::set_enabled(enable);
+#endif
   API_END()
 }
 
diff --git a/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationTranslator.java b/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationTranslator.java
index 5249f358b..7eceb2eff 100644
--- a/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationTranslator.java
+++ b/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationTranslator.java
@@ -15,6 +15,7 @@ package ai.djl.huggingface.translator;
 import ai.djl.huggingface.tokenizers.Encoding;
 import ai.djl.huggingface.tokenizers.HuggingFaceTokenizer;
 import ai.djl.huggingface.tokenizers.jni.CharSpan;
+import ai.djl.modality.nlp.translator.NamedEntity;
 import ai.djl.ndarray.NDArray;
 import ai.djl.ndarray.NDList;
 import ai.djl.ndarray.NDManager;
diff --git a/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationTranslatorFactory.java b/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationTranslatorFactory.java
index d5bf25518..556b7e4eb 100644
--- a/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationTranslatorFactory.java
+++ b/extensions/tokenizers/src/main/java/ai/djl/huggingface/translator/TokenClassificationTranslatorFactory.java
@@ -15,6 +15,8 @@ package ai.djl.huggingface.translator;
 import ai.djl.Model;
 import ai.djl.modality.Input;
 import ai.djl.modality.Output;
+import ai.djl.modality.nlp.translator.NamedEntity;
+import ai.djl.modality.nlp.translator.TokenClassificationServingTranslator;
 import ai.djl.translate.TranslateException;
 import ai.djl.translate.Translator;
 import ai.djl.translate.TranslatorFactory;


Test patch:
diff --git a/extensions/tokenizers/src/test/java/ai/djl/huggingface/tokenizers/TranslatorTest.java b/extensions/tokenizers/src/test/java/ai/djl/huggingface/tokenizers/TranslatorTest.java
index 72decd31b..2cad1353b 100644
--- a/extensions/tokenizers/src/test/java/ai/djl/huggingface/tokenizers/TranslatorTest.java
+++ b/extensions/tokenizers/src/test/java/ai/djl/huggingface/tokenizers/TranslatorTest.java
@@ -15,7 +15,6 @@ package ai.djl.huggingface.tokenizers;
 import ai.djl.Model;
 import ai.djl.ModelException;
 import ai.djl.huggingface.translator.FillMaskTranslatorFactory;
-import ai.djl.huggingface.translator.NamedEntity;
 import ai.djl.huggingface.translator.QuestionAnsweringTranslatorFactory;
 import ai.djl.huggingface.translator.TokenClassificationTranslatorFactory;
 import ai.djl.inference.Predictor;
@@ -23,6 +22,7 @@ import ai.djl.modality.Classifications;
 import ai.djl.modality.Input;
 import ai.djl.modality.Output;
 import ai.djl.modality.nlp.qa.QAInput;
+import ai.djl.modality.nlp.translator.NamedEntity;
 import ai.djl.ndarray.NDArray;
 import ai.djl.ndarray.NDList;
 import ai.djl.ndarray.NDManager;