Repository navigation
Android: compile shared platform-neutral Java sources in place (step 1 of desktop JVM support) #22338
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
Open
RanjithRagavan
wants to merge
16
commits into
pytorch:main
Choose a base branch
from
RanjithRagavan:shared-java-sources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Android: compile shared platform-neutral Java sources in place (step 1 of desktop JVM support) #22338
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8233921
Android: compile shared platform-neutral Java sources in place (step …
RanjithRagavan 542aa72
Merge branch 'main' into shared-java-sources
RanjithRagavan 035e265
Merge branch 'main' into shared-java-sources
RanjithRagavan 3411d2c
Merge branch 'main' into shared-java-sources
RanjithRagavan 9c602b7
Merge branch 'main' into shared-java-sources
RanjithRagavan ecea77c
docs: note dual-build maintenance rule for shared sources
RanjithRagavan b80087e
Merge branch 'main' into shared-java-sources
RanjithRagavan 5c52016
Merge branch 'main' into shared-java-sources
RanjithRagavan e0358ef
docs: lint-ignore the new extension/java link until it exists on main
RanjithRagavan be98682
Merge branch 'main' into shared-java-sources
psiddh d609e41
Address review: Buck filegroups, spotless scope, Log visibility, ASR …
RanjithRagavan 5bfd3be
Merge remote-tracking branch 'upstream/main' into shared-java-sources
RanjithRagavan 971cdfb
Merge branch 'main' into shared-java-sources
RanjithRagavan 320519f
Merge branch 'main' into shared-java-sources
RanjithRagavan 2cee376
Merge branch 'main' into shared-java-sources
RanjithRagavan 341db13
Merge branch 'main' into shared-java-sources
RanjithRagavan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
extension/android/executorch_android/src/main/java/org/pytorch/executorch/Log.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package org.pytorch.executorch | ||
|
|
||
| /** | ||
| * Platform logging facade used by the shared sources under extension/java. | ||
| * | ||
| * <p>The shared sources reference [Log] by simple name (same package, no import), and each platform | ||
| * artifact compiles its own implementation: | ||
| * <ul> | ||
| * <li>Android (this file): delegates to [android.util.Log], so logcat output — tags, priorities, | ||
| * and messages — is byte-for-byte identical to logging directly from the caller. | ||
| * <li>Desktop JVM (extension/jvm, added separately): a console-backed implementation. | ||
| * </ul> | ||
| * | ||
| * The shared source set itself contains no [Log] definition, so there is exactly one implementation | ||
| * per artifact and no duplicate-class collision. | ||
| * | ||
| * Note: this is public (not `internal`) because the shared sources are compiled by several Buck | ||
| * targets (executorch, executorch_llama, executorch_training), and `internal` visibility is scoped | ||
| * to a single Kotlin compilation module — a shared source in another target calling [Log] would | ||
| * fail to compile. It is nevertheless an implementation detail and not part of the public API. | ||
| */ | ||
| object Log { | ||
| @JvmStatic fun v(tag: String, msg: String): Int = android.util.Log.v(tag, msg) | ||
|
|
||
| @JvmStatic fun d(tag: String, msg: String): Int = android.util.Log.d(tag, msg) | ||
|
|
||
| @JvmStatic fun i(tag: String, msg: String): Int = android.util.Log.i(tag, msg) | ||
|
|
||
| @JvmStatic fun w(tag: String, msg: String): Int = android.util.Log.w(tag, msg) | ||
|
|
||
| @JvmStatic fun w(tag: String, msg: String, tr: Throwable): Int = android.util.Log.w(tag, msg, tr) | ||
|
|
||
| @JvmStatic fun e(tag: String, msg: String): Int = android.util.Log.e(tag, msg) | ||
|
|
||
| @JvmStatic fun e(tag: String, msg: String, tr: Throwable): Int = android.util.Log.e(tag, msg, tr) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target") | ||
| load("@fbcode_macros//build_defs:native_rules.bzl", "buck_filegroup") | ||
|
|
||
| oncall("executorch") | ||
|
|
||
| # Shared platform-neutral Java/Kotlin sources, compiled in place by each | ||
| # platform artifact (see README.md in this directory). Exported as filegroup | ||
| # labels so consuming targets never need parent-relative ("../") source paths, | ||
| # and globs so newly added shared sources are picked up automatically. | ||
| # | ||
| # NOTE: the ASR sources under src/main/java/org/pytorch/executorch/extension/asr | ||
| # are intentionally not referenced by any Buck target here; they are built by | ||
| # the OSS Gradle AAR only (same as before the shared-source move). Add a | ||
| # filegroup + consumer target when an internal Buck build needs them. | ||
|
|
||
| fbcode_target(_kind = buck_filegroup, | ||
| name = "core_sources", | ||
| srcs = glob([ | ||
| "src/main/java/org/pytorch/executorch/*.kt", | ||
| "src/main/java/org/pytorch/executorch/annotations/*.kt", | ||
| ]), | ||
| ) | ||
|
|
||
| fbcode_target(_kind = buck_filegroup, | ||
| name = "training_sources", | ||
| srcs = glob(["src/main/java/org/pytorch/executorch/training/*.kt"]), | ||
| ) | ||
|
|
||
| fbcode_target(_kind = buck_filegroup, | ||
| name = "llm_sources", | ||
| srcs = glob(["src/main/java/org/pytorch/executorch/extension/llm/*.kt"]), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # ExecuTorch shared Java sources | ||
|
|
||
| This directory holds the **platform-neutral** Java/Kotlin API sources | ||
| (`Module`, `Tensor`, `EValue`, `DType`, `LlmModule`, `AsrModule`, training, | ||
| …) that are shared by every Java-platform artifact. | ||
|
|
||
| ## How the sources are consumed | ||
|
|
||
| These sources are **not** published as their own artifact. Each platform | ||
| build compiles them directly into its own, self-contained artifact by | ||
| referencing this directory as an additional source root: | ||
|
|
||
| - **Android** (`extension/android/executorch_android`) adds | ||
| `../../java/src/main/java` via `java.srcDirs`, so the shipping AAR embeds | ||
| the same classes it always has — no new transitive dependency, unchanged | ||
| bytecode target (Java 11), unchanged manifest. | ||
| - **Desktop JVM** (`extension/jvm`, added separately) compiles the same | ||
| sources into its own jar. | ||
|
|
||
| ## Contract for sources in this directory | ||
|
|
||
| 1. **No platform APIs.** Sources here must not import `android.*`, | ||
| `java.awt.*`, or any other platform-specific API. The only permitted | ||
| dependencies are the JDK, fbjni, and soloader. | ||
| 2. **Logging goes through the `org.pytorch.executorch.Log` facade.** The | ||
| facade is referenced by simple name (same package, no import) and is | ||
| *not* defined in this directory. Each platform artifact compiles exactly | ||
| one implementation (Android: backed by `android.util.Log`; desktop: | ||
| console-backed), so there is never a duplicate-class collision and each | ||
| platform keeps its native logging behavior. | ||
| 3. **Native library loading stays per-platform.** Shared sources load the | ||
| native library through soloader's `NativeLoader`, whose delegate is | ||
| configured by the platform runtime, not by a cross-platform mutable | ||
| global. | ||
|
|
||
| ## Adding a source here | ||
|
|
||
| Both consumers pick up new files automatically — Gradle | ||
| (`extension/android/executorch_android/build.gradle`) globs this directory via | ||
| `java.srcDirs`, and the Buck side globs it via the filegroups in | ||
| `extension/java/BUCK` (`core_sources`, `training_sources`, `llm_sources`), so | ||
| no per-file lists need updating. Keep every file ktfmt-formatted (the Android | ||
| module's Spotless cannot target this directory — Spotless requires all target | ||
| files to live under the module's project dir). | ||
|
|
||
| The ASR (`extension/asr/`) sources and the Android-only `ImageProcessor` are | ||
| Gradle/AAR-only today and intentionally have no Buck targets, matching the | ||
| pre-move state; add Buck targets for them when an internal build needs them. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nits: The maintenance note says shared sources must be listed in a matching Buck target, but the shared ASR sources are not listed here. Is ASR intentionally Gradle-only, or should the comment/targets be updated to reflect the actual contract?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct — ASR is intentionally Gradle-only, matching the pre-move state (the ASR sources were not in any Buck target before this PR either). The BUCK header and
extension/java/README.mdnow state this explicitly for ASR and the Android-onlyImageProcessor, so the contract is documented rather than implied. Happy to add anexecutorch_asrtarget in a follow-up if an internal build needs it.