|
| 1 | +#include <android/log.h> |
| 2 | +#include <cassert> |
| 3 | +#include <cmath> |
| 4 | +#include <pthread.h> |
| 5 | +#include <unistd.h> |
| 6 | +#include <vector> |
| 7 | +#define ALOGI(...) \ |
| 8 | + __android_log_print(ANDROID_LOG_INFO, "PyTorchNativeApp", __VA_ARGS__) |
| 9 | +#define ALOGE(...) \ |
| 10 | + __android_log_print(ANDROID_LOG_ERROR, "PyTorchNativeApp", __VA_ARGS__) |
| 11 | + |
| 12 | +#include "jni.h" |
| 13 | + |
| 14 | +#include <opencv2/opencv.hpp> |
| 15 | +#include <torch/script.h> |
| 16 | + |
| 17 | +namespace pytorch_nativeapp { |
| 18 | +namespace { |
| 19 | +torch::Tensor warp_perspective(torch::Tensor image, torch::Tensor warp) { |
| 20 | + cv::Mat image_mat(/*rows=*/image.size(0), |
| 21 | + /*cols=*/image.size(1), |
| 22 | + /*type=*/CV_32FC1, |
| 23 | + /*data=*/image.data_ptr<float>()); |
| 24 | + cv::Mat warp_mat(/*rows=*/warp.size(0), |
| 25 | + /*cols=*/warp.size(1), |
| 26 | + /*type=*/CV_32FC1, |
| 27 | + /*data=*/warp.data_ptr<float>()); |
| 28 | + |
| 29 | + cv::Mat output_mat; |
| 30 | + cv::warpPerspective(image_mat, output_mat, warp_mat, /*dsize=*/{8, 8}); |
| 31 | + |
| 32 | + torch::Tensor output = |
| 33 | + torch::from_blob(output_mat.ptr<float>(), /*sizes=*/{8, 8}); |
| 34 | + return output.clone(); |
| 35 | +} |
| 36 | + |
| 37 | +static auto registry = |
| 38 | + torch::RegisterOperators("my_ops::warp_perspective", &warp_perspective); |
| 39 | + |
| 40 | +template <typename T> void log(const char *m, T t) { |
| 41 | + std::ostringstream os; |
| 42 | + os << t << std::endl; |
| 43 | + ALOGI("%s %s", m, os.str().c_str()); |
| 44 | +} |
| 45 | + |
| 46 | +struct JITCallGuard { |
| 47 | + torch::autograd::AutoGradMode no_autograd_guard{false}; |
| 48 | + torch::AutoNonVariableTypeMode non_var_guard{true}; |
| 49 | + torch::jit::GraphOptimizerEnabledGuard no_optimizer_guard{false}; |
| 50 | +}; |
| 51 | +} // namespace |
| 52 | + |
| 53 | +static void loadAndForwardModel(JNIEnv *env, jclass, jstring jModelPath) { |
| 54 | + const char *modelPath = env->GetStringUTFChars(jModelPath, 0); |
| 55 | + assert(modelPath); |
| 56 | + |
| 57 | + // To load torchscript model for mobile we need set these guards, |
| 58 | + // because mobile build doesn't support features like autograd for smaller |
| 59 | + // build size which is placed in `struct JITCallGuard` in this example. It may |
| 60 | + // change in future, you can track the latest changes keeping an eye in |
| 61 | + // android/pytorch_android/src/main/cpp/pytorch_jni_jit.cpp |
| 62 | + JITCallGuard guard; |
| 63 | + torch::jit::Module module = torch::jit::load(modelPath); |
| 64 | + module.eval(); |
| 65 | + torch::Tensor x = torch::randn({4, 8}); |
| 66 | + torch::Tensor y = torch::randn({8, 5}); |
| 67 | + log("x:", x); |
| 68 | + log("y:", y); |
| 69 | + c10::IValue t_out = module.forward({x, y}); |
| 70 | + log("result:", t_out); |
| 71 | + env->ReleaseStringUTFChars(jModelPath, modelPath); |
| 72 | +} |
| 73 | +} // namespace pytorch_nativeapp |
| 74 | + |
| 75 | +JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) { |
| 76 | + JNIEnv *env; |
| 77 | + if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 78 | + return JNI_ERR; |
| 79 | + } |
| 80 | + |
| 81 | + jclass c = env->FindClass("org/pytorch/nativeapp/NativeClient$NativePeer"); |
| 82 | + if (c == nullptr) { |
| 83 | + return JNI_ERR; |
| 84 | + } |
| 85 | + |
| 86 | + static const JNINativeMethod methods[] = { |
| 87 | + {"loadAndForwardModel", "(Ljava/lang/String;)V", |
| 88 | + (void *)pytorch_nativeapp::loadAndForwardModel}, |
| 89 | + }; |
| 90 | + int rc = env->RegisterNatives(c, methods, |
| 91 | + sizeof(methods) / sizeof(JNINativeMethod)); |
| 92 | + |
| 93 | + if (rc != JNI_OK) { |
| 94 | + return rc; |
| 95 | + } |
| 96 | + |
| 97 | + return JNI_VERSION_1_6; |
| 98 | +} |
0 commit comments