forked from kherud/java-llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLlamaLoader.java
More file actions
263 lines (235 loc) · 7.92 KB
/
Copy pathLlamaLoader.java
File metadata and controls
263 lines (235 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// SPDX-FileCopyrightText: 2026 Bernard Ladenthin <bernard.ladenthin@gmail.com>
// SPDX-FileCopyrightText: 2023-2025 Konstantin Herud
//
// SPDX-License-Identifier: MIT
package net.ladenthin.llama;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;
import org.jetbrains.annotations.Nullable;
/**
* Set the system properties {@code net.ladenthin.llama.lib.path} /
* {@code net.ladenthin.llama.lib.name} appropriately so that the library can
* find *.dll, *.dylib and *.so files, according to the current OS (win, linux, mac).
*
* <p>The library files are automatically extracted from this project's package (JAR).
*
* <p>usage: call {@link #initialize()} before using the library.
*
* @author leo
*/
@SuppressWarnings("UseOfSystemOutOrSystemErr")
class LlamaLoader {
private static boolean extracted = false;
private static final LlamaSystemProperties systemProperties = new LlamaSystemProperties();
/**
* Loads the llama and jllama shared libraries
*/
static synchronized void initialize() throws UnsatisfiedLinkError {
// only cleanup before the first extract
if (!extracted) {
cleanup();
}
if ("Mac".equals(OSInfo.getOSName())) {
String nativeDirName = getNativeResourcePath();
String tempFolder = getTempDir().getAbsolutePath();
System.out.println(nativeDirName);
Path metalFilePath = extractFile(nativeDirName, "ggml-metal.metal", tempFolder, false);
if (metalFilePath == null) {
System.err.println("'ggml-metal.metal' not found");
}
}
loadNativeLibrary("jllama");
extracted = true;
}
/**
* Deleted old native libraries e.g. on Windows the DLL file is not removed on VM-Exit (bug #80)
*/
private static void cleanup() {
try (Stream<Path> dirList = Files.list(getTempDir().toPath())) {
dirList.filter(LlamaLoader::shouldCleanPath).forEach(LlamaLoader::cleanPath);
}
catch (IOException e) {
System.err.println("Failed to open directory: " + e.getMessage());
}
}
static boolean shouldCleanPath(Path path) {
String fileName = path.getFileName().toString();
return fileName.startsWith("jllama") || fileName.startsWith("llama");
}
private static void cleanPath(Path path) {
try {
Files.delete(path);
}
catch (Exception e) {
System.err.println("Failed to delete old native lib: " + e.getMessage());
}
}
private static void loadNativeLibrary(String name) {
List<String> triedPaths = new LinkedList<>();
String nativeLibName = System.mapLibraryName(name);
String nativeLibPath = systemProperties.getLibPath();
if (nativeLibPath != null) {
Path path = Paths.get(nativeLibPath, nativeLibName);
if (loadNativeLibrary(path)) {
return;
}
else {
triedPaths.add(nativeLibPath);
}
}
if (OSInfo.isAndroid()) {
try {
// loadLibrary can load directly from packed apk file automatically
// if java-llama.cpp is added as code source
System.loadLibrary(name);
return;
}
catch (UnsatisfiedLinkError e) {
triedPaths.add("Directly from .apk/lib");
}
}
// Try to load the library from java.library.path
String javaLibraryPath = System.getProperty("java.library.path", "");
for (String ldPath : javaLibraryPath.split(File.pathSeparator)) {
if (ldPath.isEmpty()) {
continue;
}
Path path = Paths.get(ldPath, nativeLibName);
if (loadNativeLibrary(path)) {
return;
}
else {
triedPaths.add(ldPath);
}
}
// As a last resort try load the os-dependent library from the jar file
nativeLibPath = getNativeResourcePath();
if (hasNativeLib(nativeLibPath, nativeLibName)) {
// temporary library folder
String tempFolder = getTempDir().getAbsolutePath();
// Try extracting the library from jar
if (extractAndLoadLibraryFile(nativeLibPath, nativeLibName, tempFolder)) {
return;
}
else {
triedPaths.add(nativeLibPath);
}
}
throw new UnsatisfiedLinkError(
String.format(
"No native library found for os.name=%s, os.arch=%s, paths=[%s]",
OSInfo.getOSName(),
OSInfo.getArchName(),
String.join(File.pathSeparator, triedPaths)
)
);
}
/**
* Loads native library using the given path and name of the library
*
* @param path path of the native library
* @return true for successfully loading, otherwise false
*/
public static boolean loadNativeLibrary(Path path) {
if (!Files.exists(path)) {
return false;
}
String absolutePath = path.toAbsolutePath().toString();
try {
System.load(absolutePath);
return true;
}
catch (UnsatisfiedLinkError e) {
System.err.println(e.getMessage());
System.err.println("Failed to load native library: " + absolutePath + ". osinfo: " + OSInfo.getNativeLibFolderPathForCurrentOS());
return false;
}
}
@Nullable
private static Path extractFile(String sourceDirectory, String fileName, String targetDirectory, boolean addUuid) {
String nativeLibraryFilePath = sourceDirectory + "/" + fileName;
Path extractedFilePath = Paths.get(targetDirectory, fileName);
try {
// Extract a native library file into the target directory
try (InputStream reader = LlamaLoader.class.getResourceAsStream(nativeLibraryFilePath)) {
if (reader == null) {
return null;
}
Files.copy(reader, extractedFilePath, StandardCopyOption.REPLACE_EXISTING);
}
finally {
// Delete the extracted lib file on JVM exit.
extractedFilePath.toFile().deleteOnExit();
}
// Set executable (x) flag to enable Java to load the native library
extractedFilePath.toFile().setReadable(true);
extractedFilePath.toFile().setWritable(true, true);
extractedFilePath.toFile().setExecutable(true);
// Check whether the contents are properly copied from the resource folder
try (InputStream nativeIn = LlamaLoader.class.getResourceAsStream(nativeLibraryFilePath);
InputStream extractedLibIn = Files.newInputStream(extractedFilePath)) {
if (!contentsEquals(nativeIn, extractedLibIn)) {
throw new RuntimeException(String.format("Failed to write a native library file at %s", extractedFilePath));
}
}
System.out.println("Extracted '" + fileName + "' to '" + extractedFilePath + "'");
return extractedFilePath;
}
catch (IOException e) {
System.err.println(e.getMessage());
return null;
}
}
/**
* Extracts and loads the specified library file to the target folder
*
* @param libFolderForCurrentOS Library path.
* @param libraryFileName Library name.
* @param targetFolder Target folder.
* @return whether the library was successfully loaded
*/
private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, String libraryFileName, String targetFolder) {
Path path = extractFile(libFolderForCurrentOS, libraryFileName, targetFolder, true);
if (path == null) {
return false;
}
return loadNativeLibrary(path);
}
static boolean contentsEquals(InputStream in1, InputStream in2) throws IOException {
if (!(in1 instanceof BufferedInputStream)) {
in1 = new BufferedInputStream(in1);
}
if (!(in2 instanceof BufferedInputStream)) {
in2 = new BufferedInputStream(in2);
}
int ch = in1.read();
while (ch != -1) {
int ch2 = in2.read();
if (ch != ch2) {
return false;
}
ch = in1.read();
}
int ch2 = in2.read();
return ch2 == -1;
}
static File getTempDir() {
String _override = systemProperties.getTmpDir(); return new File(_override != null ? _override : System.getProperty("java.io.tmpdir"));
}
static String getNativeResourcePath() {
String packagePath = LlamaLoader.class.getPackage().getName().replace(".", "/");
return String.format("/%s/%s", packagePath, OSInfo.getNativeLibFolderPathForCurrentOS());
}
private static boolean hasNativeLib(String path, String libraryName) {
return LlamaLoader.class.getResource(path + "/" + libraryName) != null;
}
}