Skip to content

Android check pte exists #10931

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
merged 4 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,9 @@ public void testModuleLoadForwardExplicit() throws IOException{
assertTrue(results[0].isTensor());
}

@Test
@Test(expected = RuntimeException.class)
public void testModuleLoadNonExistantFile() throws IOException{
Module module = Module.load(getTestFilePath(MISSING_FILE_NAME));

EValue[] results = module.forward();
assertEquals(null, results);
}

@Test
public void testModuleLoadMethodNonExistantFile() throws IOException{
Module module = Module.load(getTestFilePath(MISSING_FILE_NAME));

int loadMethod = module.loadMethod(FORWARD_METHOD);
assertEquals(loadMethod, ACCESS_FAILED);
}

@Test
Expand Down Expand Up @@ -146,11 +135,11 @@ public void testForwardOnDestroyedModule() throws IOException{
assertEquals(loadMethod, OK);

module.destroy();

EValue[] results = module.forward();
assertEquals(0, results.length);
}

@Test
public void testForwardFromMultipleThreads() throws InterruptedException, IOException {
Module module = Module.load(getTestFilePath(TEST_FILE_NAME));
Expand All @@ -169,7 +158,7 @@ public void run() {
assertTrue(results[0].isTensor());
completed.incrementAndGet();
} catch (InterruptedException e) {

}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.util.Log;
import com.facebook.soloader.nativeloader.NativeLoader;
import com.facebook.soloader.nativeloader.SystemDelegate;
import java.io.File;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.pytorch.executorch.annotations.Experimental;
Expand Down Expand Up @@ -52,6 +53,10 @@ public static Module load(final String modelPath, int loadMode) {
if (!NativeLoader.isInitialized()) {
NativeLoader.init(new SystemDelegate());
}
File modelFile = new File(modelPath);
if (!modelFile.canRead() || !modelFile.isFile()) {
Copy link
Preview

Copilot AI May 15, 2025

Choose a reason for hiding this comment

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

Duplicate file validation logic appears here and in LlmModule.java; extracting a shared helper method could improve maintainability.

Copilot uses AI. Check for mistakes.

throw new RuntimeException("Cannot load model path " + modelPath);
}
return new Module(new NativePeer(modelPath, loadMode));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.facebook.jni.annotations.DoNotStrip;
import com.facebook.soloader.nativeloader.NativeLoader;
import com.facebook.soloader.nativeloader.SystemDelegate;
import java.io.File;
import org.pytorch.executorch.annotations.Experimental;

/**
Expand Down Expand Up @@ -41,33 +42,49 @@ public class LlmModule {
private static native HybridData initHybrid(
int modelType, String modulePath, String tokenizerPath, float temperature, String dataPath);

/**
* Constructs a LLM Module for a model with given type, model path, tokenizer, temperature, and
* data path.
*/
public LlmModule(
int modelType, String modulePath, String tokenizerPath, float temperature, String dataPath) {
File modelFile = new File(modulePath);
if (!modelFile.canRead() || !modelFile.isFile()) {
throw new RuntimeException("Cannot load model path " + modulePath);
}
File tokenizerFile = new File(tokenizerPath);
if (!tokenizerFile.canRead() || !tokenizerFile.isFile()) {
throw new RuntimeException("Cannot load tokenizer path " + tokenizerPath);
}
Comment on lines +51 to +58
Copy link
Preview

Copilot AI May 15, 2025

Choose a reason for hiding this comment

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

The file existence and readability check is duplicated in Module.java; consider extracting this into a shared utility method to reduce duplication.

Suggested change
File modelFile = new File(modulePath);
if (!modelFile.canRead() || !modelFile.isFile()) {
throw new RuntimeException("Cannot load model path " + modulePath);
}
File tokenizerFile = new File(tokenizerPath);
if (!tokenizerFile.canRead() || !tokenizerFile.isFile()) {
throw new RuntimeException("Cannot load tokenizer path " + tokenizerPath);
}
validateFile(modulePath, "model path");
validateFile(tokenizerPath, "tokenizer path");

Copilot uses AI. Check for mistakes.

mHybridData = initHybrid(modelType, modulePath, tokenizerPath, temperature, dataPath);
}

/** Constructs a LLM Module for a model with given model path, tokenizer, temperature. */
public LlmModule(String modulePath, String tokenizerPath, float temperature) {
mHybridData = initHybrid(MODEL_TYPE_TEXT, modulePath, tokenizerPath, temperature, null);
this(MODEL_TYPE_TEXT, modulePath, tokenizerPath, temperature, null);
}

/**
* Constructs a LLM Module for a model with given model path, tokenizer, temperature and data
* path.
*/
public LlmModule(String modulePath, String tokenizerPath, float temperature, String dataPath) {
mHybridData = initHybrid(MODEL_TYPE_TEXT, modulePath, tokenizerPath, temperature, dataPath);
this(MODEL_TYPE_TEXT, modulePath, tokenizerPath, temperature, dataPath);
}

/** Constructs a LLM Module for a model with given path, tokenizer, and temperature. */
public LlmModule(int modelType, String modulePath, String tokenizerPath, float temperature) {
mHybridData = initHybrid(modelType, modulePath, tokenizerPath, temperature, null);
this(modelType, modulePath, tokenizerPath, temperature, null);
}

/** Constructs a LLM Module for a model with the given LlmModuleConfig */
public LlmModule(LlmModuleConfig config) {
mHybridData =
initHybrid(
config.getModelType(),
config.getModulePath(),
config.getTokenizerPath(),
config.getTemperature(),
config.getDataPath());
this(
config.getModelType(),
config.getModulePath(),
config.getTokenizerPath(),
config.getTemperature(),
config.getDataPath());
}

public void resetNative() {
Expand Down
Loading