Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -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

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

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