Skip to content

Commit 78ae1ae

Browse files
authored
Merge pull request #34 from dhruvarayasam/file-not-found-fix
File not found error handling in loadModel method in GGUF.java
2 parents 460be99 + f8338ca commit 78ae1ae

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

external/tornadovm

src/main/java/com/example/core/model/GGUF.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package com.example.core.model;
22

33
import com.example.auxiliary.Timer;
4+
import com.example.core.model.GGUF.GGUFTensorInfo;
45
import com.example.core.model.tensor.FloatTensor;
56
import com.example.core.model.tensor.GGMLTensorEntry;
67
import com.example.core.types.MetadataValueType;
78
import com.example.core.types.Pair;
89

10+
import java.io.FileNotFoundException;
911
import java.io.IOException;
1012
import java.lang.foreign.Arena;
1113
import java.lang.foreign.MemorySegment;
1214
import java.nio.ByteBuffer;
1315
import java.nio.ByteOrder;
1416
import java.nio.channels.FileChannel;
1517
import java.nio.charset.StandardCharsets;
18+
import java.nio.file.Files;
1619
import java.nio.file.Path;
1720
import java.util.HashMap;
1821
import java.util.List;
@@ -36,10 +39,19 @@ public final class GGUF {
3639
private long tensorDataOffset;
3740

3841
public static GGUF loadModel(Path modelPath) throws IOException {
42+
43+
// file existence check
44+
if (!Files.exists(modelPath)) {
45+
throw new FileNotFoundException("Model file not found: " + modelPath);
46+
}
47+
48+
// second check to make sure that nothing goes wrong during model loading
3949
try (FileChannel fileChannel = FileChannel.open(modelPath); var ignored = Timer.log("Parse " + modelPath)) {
4050
GGUF gguf = new GGUF();
4151
gguf.loadModelImpl(fileChannel);
4252
return gguf;
53+
} catch (Exception e) {
54+
throw new RuntimeException("Unexpected error while loading GGUF model from " + modelPath, e);
4355
}
4456
}
4557

src/main/java/com/example/loader/weights/ModelLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static ModelType detectModelType(Map<String, Object> metadata) {
6767

6868
public static Model loadModel(Path ggufPath, int contextLength, boolean loadWeights) throws IOException {
6969
// initial load of metadata from gguf file
70-
GGUF gguf = GGUF.loadModel(ggufPath);
70+
GGUF gguf = GGUF.loadModel(ggufPath);
7171
FileChannel fileChannel = FileChannel.open(ggufPath, StandardOpenOption.READ);
7272
// detect model type
7373
ModelType modelType = detectModelType(gguf.getMetadata());

0 commit comments

Comments
 (0)