Skip to content

Commit 4473b26

Browse files
Refactor ModelLoader: move loadModel() from LlamaApp to ModelLoader class
1 parent 96f851d commit 4473b26

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

src/main/java/org/beehive/gpullama3/LlamaApp.java

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,13 @@
1616
import java.util.random.RandomGeneratorFactory;
1717

1818
import static org.beehive.gpullama3.inference.sampler.Sampler.createSampler;
19+
import static org.beehive.gpullama3.model.loader.ModelLoader.loadModel;
20+
1921
public class LlamaApp {
2022
// Configuration flags for hardware acceleration and optimizations
2123
public static final boolean USE_VECTOR_API = Boolean.parseBoolean(System.getProperty("llama.VectorAPI", "true")); // Enable Java Vector API for CPU acceleration
22-
public static final boolean USE_AOT = Boolean.parseBoolean(System.getProperty("llama.AOT", "false")); // Use Ahead-of-Time compilation
2324
public static final boolean SHOW_PERF_INTERACTIVE = Boolean.parseBoolean(System.getProperty("llama.ShowPerfInteractive", "true")); // Show performance metrics in interactive mode
2425

25-
26-
/**
27-
* Loads the language model based on the given options.
28-
* <p>
29-
* If Ahead-of-Time (AOT) mode is enabled, attempts to use a pre-loaded compiled model. Otherwise, loads the model from the specified path using the model loader.
30-
* </p>
31-
*
32-
* @param options
33-
* the parsed CLI options containing model path and max token limit
34-
* @return the loaded {@link Model} instance
35-
* @throws IOException
36-
* if the model fails to load
37-
* @throws IllegalStateException
38-
* if AOT loading is enabled but the preloaded model is unavailable
39-
*/
40-
private static Model loadModel(Options options) throws IOException {
41-
if (USE_AOT) {
42-
Model model = AOT.tryUsePreLoaded(options.modelPath(), options.maxTokens());
43-
if (model == null) {
44-
throw new IllegalStateException("Failed to load precompiled AOT model.");
45-
}
46-
return model;
47-
}
48-
return ModelLoader.loadModel(options.modelPath(), options.maxTokens(), true);
49-
}
50-
51-
private static Sampler createSampler(Model model, Options options) {
52-
return selectSampler(model.configuration().vocabularySize(), options.temperature(), options.topp(), options.seed());
53-
}
54-
5526
private static void runSingleInstruction(Model model, Sampler sampler, Options options) {
5627
String response = model.runInstructOnce(sampler, options);
5728
System.out.println(response);

src/main/java/org/beehive/gpullama3/model/loader/ModelLoader.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.beehive.gpullama3.model.loader;
22

33
import org.beehive.gpullama3.Options;
4+
import org.beehive.gpullama3.aot.AOT;
45
import org.beehive.gpullama3.core.model.GGMLType;
56
import org.beehive.gpullama3.core.model.GGUF;
67
import org.beehive.gpullama3.core.model.tensor.ArrayFloatTensor;
@@ -35,6 +36,8 @@
3536

3637
public abstract class ModelLoader {
3738

39+
public static final boolean USE_AOT = Boolean.parseBoolean(System.getProperty("llama.AOT", "false")); // Use Ahead-of-Time compilation
40+
3841
protected FileChannel fileChannel;
3942
protected GGUF gguf;
4043
protected int contextLength;
@@ -74,6 +77,31 @@ private static ModelType detectModelType(Map<String, Object> metadata) {
7477
return ModelType.UNKNOWN;
7578
}
7679

80+
/**
81+
// * Loads the language model based on the given options.
82+
// * <p>
83+
// * If Ahead-of-Time (AOT) mode is enabled, attempts to use a pre-loaded compiled model. Otherwise, loads the model from the specified path using the model loader.
84+
// * </p>
85+
// *
86+
// * @param options
87+
// * the parsed CLI options containing model path and max token limit
88+
// * @return the loaded {@link Model} instance
89+
// * @throws IOException
90+
// * if the model fails to load
91+
// * @throws IllegalStateException
92+
// * if AOT loading is enabled but the preloaded model is unavailable
93+
// */
94+
public static Model loadModel(Options options) throws IOException {
95+
if (USE_AOT) {
96+
Model model = AOT.tryUsePreLoaded(options.modelPath(), options.maxTokens());
97+
if (model == null) {
98+
throw new IllegalStateException("Failed to load precompiled AOT model.");
99+
}
100+
return model;
101+
}
102+
return ModelLoader.loadModel(options.modelPath(), options.maxTokens(), true);
103+
}
104+
77105
public static Model loadModel(Path ggufPath, int contextLength, boolean loadWeights) throws IOException {
78106
// initial load of metadata from gguf file
79107
GGUF gguf = GGUF.loadModel(ggufPath);

0 commit comments

Comments
 (0)