Skip to content

[Android] Add Java runtime API for registered ops and backends #11042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

keyprocedure
Copy link
Contributor

@keyprocedure keyprocedure commented May 21, 2025

Summary

Adds a Java runtime API that exposes native methods to query registered operators and backends for debugging.

  • Adds getRegisteredOps() and getRegisteredBackends() native methods
  • Includes instrumentation tests for the runtime API

Fixes #10438

Test plan

  • Unit tests:
./gradlew :executorch_android:test
  • Instrumentation test:
./gradlew :executorch_android:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=org.pytorch.executorch.RuntimeInstrumentationTest
  • Manual testing:
    Verified Runtime API functionality on an emulator using a sample app. getRegisteredOps() and getRegisteredBackends() successfully returned and displayed all registered ops and backends.

Copy link

pytorch-bot bot commented May 21, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/11042

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit ea88c6f with merge base 33d4790 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 21, 2025
@keyprocedure
Copy link
Contributor Author

@pytorchbot label "release notes: none"

@pytorch-bot pytorch-bot bot added the release notes: none Do not include this in the release notes label May 21, 2025
@kirklandsign
Copy link
Contributor

Thank you @keyprocedure sorry I made some updates in the codebase to fix some internal code breakage. Would you mind rebasing this PR against main?

@kirklandsign
Copy link
Contributor

Otherwise the PR looks good to me. Have you tested locally?

@keyprocedure keyprocedure force-pushed the add-android-runtime-api branch from 6f0a515 to dceb3e2 Compare May 22, 2025 03:30
@facebook-github-bot
Copy link
Contributor

@kirklandsign has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@kirklandsign
Copy link
Contributor

A few more places:

srcs = ["jni_layer.cpp", "log.cpp"],

srcs = ["jni_layer.cpp", "log.cpp"],

"jni_layer_llama.cpp",

Could you please add the new JNI file to it?

And here:

runtime.export_file(
name = "jni_layer.cpp",
)

export the new file, and add it to

"//xplat/executorch/extension/android/jni:jni_layer.cpp",

Thank you

@keyprocedure
Copy link
Contributor Author

Otherwise the PR looks good to me. Have you tested locally?

I tested the API functionality locally with the instrumentation test I created and manually verified through an emulator. I wasn't able to run the existing instrumentation tests because I didn't have the setup files

@kirklandsign
Copy link
Contributor

GH emulator CI passed 😄

And could use help with linter

@kirklandsign
Copy link
Contributor

google-java-format_linux-x86-64 -i extension/android/executorch_android/src/main/java/org/pytorch/executorch/ExecuTorchRuntime.java

@kirklandsign
Copy link
Contributor

and


     6   6 |  * LICENSE file in the root directory of this source tree.
     7   7 |  */
     8   8 | 
         9 |+#include <fbjni/fbjni.h>
     9  10 | #include <jni.h>
     9     |-#include <fbjni/fbjni.h>
    11  11 | 
        12 |+#include <executorch/runtime/backend/interface.h>
    12  13 | #include <executorch/runtime/kernel/operator_registry.h>
    12     |-#include <executorch/runtime/backend/interface.h>
    14  14 | 
    15  15 | namespace executorch_jni {
    16  16 | namespace runtime = ::executorch::ET_RUNTIME_NAMESPACE;
    17  17 | 
    18  18 | class AndroidRuntimeJni : public facebook::jni::JavaClass<AndroidRuntimeJni> {
    19  19 |  public:
    19     |-  constexpr static const char* kJavaDescriptor ="Lorg/pytorch/executorch/ExecuTorchRuntime;";
        20 |+  constexpr static const char* kJavaDescriptor =
        21 |+"Lorg/pytorch/executorch/ExecuTorchRuntime;";
    21  22 | 
    22  23 |   static void registerNatives() {
    23  24 |     javaClassStatic()->registerNatives({
    23     |-        makeNativeMethod("getRegisteredOps", AndroidRuntimeJni::getRegisteredOps),
    24     |-        makeNativeMethod("getRegisteredBackends", AndroidRuntimeJni::getRegisteredBackends),
        25 |+        makeNativeMethod(
        26 |+            "getRegisteredOps", AndroidRuntimeJni::getRegisteredOps),
        27 |+        makeNativeMethod(
        28 |+            "getRegisteredBackends", AndroidRuntimeJni::getRegisteredBackends),
    26  29 |     });
    27  30 |   }
    28  31 | 
Traceback (most recent call last):
  File "/home/ec2-user/actions-runner/_work/executorch/executorch/test-infra/.github/scripts/run_with_env_secrets.py", line 102, in <module>
--------------------------------------------------------------------------------
    33  36 |     auto result = facebook::jni::JArrayClass<jstring>::newArray(kernels.size());
    main()
  File "/home/ec2-user/actions-runner/_work/executorch/executorch/test-infra/.github/scripts/run_with_env_secrets.py", line 98, in main
    run_cmd_or_die(f"docker exec -t {container_name} /exec")
  File "/home/ec2-user/actions-runner/_work/executorch/executorch/test-infra/.github/scripts/run_with_env_secrets.py", line 39, in run_cmd_or_die
    raise RuntimeError(f"Command {cmd} failed with exit code {exit_code}")
RuntimeError: Command docker exec -t e81015920d83afd6e56e3dd9ca81cfdee041d74ffde9e4a7ced71ff12ffc333c /exec failed with exit code 1
    34  37 | 
    35  38 |     for (size_t i = 0; i < kernels.size(); ++i) {
    35     |-auto op = facebook::jni::make_jstring(kernels[i].name_);
    36     |-result->setElement(i, op.get());
        39 |+auto op = facebook::jni::make_jstring(kernels[i].name_);
        40 |+result->setElement(i, op.get());
    38  41 |     }
    39  42 | 
    40  43 |     return result;
--------------------------------------------------------------------------------
    47  50 |     auto result = facebook::jni::JArrayClass<jstring>::newArray(num_backends);
    48  51 | 
    49  52 |     for (int i = 0; i < num_backends; ++i) {
    49     |-auto name_result = runtime::get_backend_name(i);
    50     |-const char* name = "";
        53 |+auto name_result = runtime::get_backend_name(i);
        54 |+const char* name = "";
    52  55 | 
    52     |-if (name_result.ok()) {
    53     |-name = *name_result;
    54     |-}
        56 |+if (name_result.ok()) {
        57 |+name = *name_result;
        58 |+}
    56  59 | 
    56     |-auto backend_str = facebook::jni::make_jstring(name);
    57     |-result->setElement(i, backend_str.get());
        60 |+auto backend_str = facebook::jni::make_jstring(name);
        61 |+result->setElement(i, backend_str.get());
    59  62 |     }
    60  63 | 
    61  64 |     return result;

@keyprocedure
Copy link
Contributor Author

in your last comment with the CI output, is the failure just a formatting issue, or is there a runtime crash?

@kirklandsign
Copy link
Contributor

Just formatting issue

@facebook-github-bot
Copy link
Contributor

@kirklandsign has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot merged commit 52d7778 into pytorch:main May 22, 2025
89 checks passed
@kirklandsign
Copy link
Contributor

Thank you @keyprocedure code merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: none Do not include this in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android] Java API for runtime info
3 participants