Skip to content

Commit 2e1f34c

Browse files
Refactor GGUF to use persistent FileChannel and enhance error handling for file opening.
1 parent 8d34f64 commit 2e1f34c

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static Model loadModel(Path ggufPath, int contextLength, boolean loadWeig
8888
// detect model type
8989
ModelType modelType = detectModelType(gguf.getMetadata());
9090
// model type-specific load
91-
return modelType.loadModel(fileChannel, gguf, contextLength, loadWeights, useTornadovm);
91+
return modelType.loadModel(gguf.getFileChannel(), gguf, contextLength, loadWeights, useTornadovm);
9292
}
9393

9494
/**

src/main/java/org/beehive/gpullama3/tensor/GGUF.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
import java.util.List;
1818
import java.util.Map;
1919

20+
import static java.nio.file.StandardOpenOption.READ;
21+
import static java.nio.file.StandardOpenOption.WRITE;
22+
2023
public final class GGUF {
24+
private static FileChannel fileChannel;
2125
private static final int GGUF_MAGIC = 0x46554747;
2226
private static final int DEFAULT_ALIGNMENT = 32; // must be a power of 2
2327
private static final List<Integer> SUPPORTED_GGUF_VERSIONS = List.of(2, 3);
@@ -41,9 +45,18 @@ public static GGUF loadGGUFMetadata(Path modelPath) throws IOException {
4145
throw new FileNotFoundException("Model file not found: " + modelPath);
4246
}
4347

44-
// second check to make sure that nothing goes wrong during model loading
45-
try (FileChannel fileChannel = FileChannel.open(modelPath);
46-
) {
48+
// Open file
49+
try {
50+
System.out.println("[GGUF] fileChannel = FileChannel.open(modelPath, READ, WRITE);");
51+
fileChannel = FileChannel.open(modelPath, READ, WRITE);
52+
// Ensure we start reading from the beginning of the file
53+
fileChannel.position(0);
54+
} catch (Exception e) {
55+
throw new RuntimeException("Failed to open file channel for " + modelPath, e);
56+
}
57+
58+
// Read and store the gguf metadata
59+
try {
4760
GGUF gguf = new GGUF();
4861
// The header of the file.
4962
gguf.readHeader(fileChannel); // gguf_header_t header;

0 commit comments

Comments
 (0)