whisper.rn 0.4.0-rc.8 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/android/build.gradle +12 -3
- package/android/src/main/CMakeLists.txt +44 -13
- package/android/src/main/java/com/rnwhisper/AudioUtils.java +27 -12
- package/android/src/main/java/com/rnwhisper/RNWhisper.java +75 -34
- package/android/src/main/java/com/rnwhisper/WhisperContext.java +53 -38
- package/android/src/main/jni.cpp +38 -1
- package/android/src/main/jniLibs/arm64-v8a/librnwhisper.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnwhisper_v8fp16_va_2.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/librnwhisper.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/librnwhisper_vfpv4.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnwhisper.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnwhisper_x86_64.so +0 -0
- package/android/src/newarch/java/com/rnwhisper/RNWhisperModule.java +10 -0
- package/android/src/oldarch/java/com/rnwhisper/RNWhisperModule.java +10 -0
- package/cpp/coreml/whisper-compat.h +10 -0
- package/cpp/coreml/whisper-compat.m +35 -0
- package/cpp/coreml/whisper-decoder-impl.h +27 -15
- package/cpp/coreml/whisper-decoder-impl.m +36 -10
- package/cpp/coreml/whisper-encoder-impl.h +21 -9
- package/cpp/coreml/whisper-encoder-impl.m +29 -3
- package/cpp/ggml-alloc.c +727 -517
- package/cpp/ggml-alloc.h +47 -65
- package/cpp/ggml-backend-impl.h +196 -57
- package/cpp/ggml-backend-reg.cpp +591 -0
- package/cpp/ggml-backend.cpp +2016 -0
- package/cpp/ggml-backend.h +234 -89
- package/cpp/ggml-common.h +1861 -0
- package/cpp/ggml-cpp.h +39 -0
- package/cpp/ggml-cpu/amx/amx.cpp +221 -0
- package/cpp/ggml-cpu/amx/amx.h +8 -0
- package/cpp/ggml-cpu/amx/common.h +91 -0
- package/cpp/ggml-cpu/amx/mmq.cpp +2511 -0
- package/cpp/ggml-cpu/amx/mmq.h +10 -0
- package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
- package/cpp/ggml-cpu/arch/arm/quants.c +4113 -0
- package/cpp/ggml-cpu/arch/arm/repack.cpp +2162 -0
- package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
- package/cpp/ggml-cpu/arch/x86/quants.c +4310 -0
- package/cpp/ggml-cpu/arch/x86/repack.cpp +3284 -0
- package/cpp/ggml-cpu/arch-fallback.h +184 -0
- package/cpp/ggml-cpu/binary-ops.cpp +158 -0
- package/cpp/ggml-cpu/binary-ops.h +16 -0
- package/cpp/ggml-cpu/common.h +72 -0
- package/cpp/ggml-cpu/ggml-cpu-impl.h +511 -0
- package/cpp/ggml-cpu/ggml-cpu.c +3473 -0
- package/cpp/ggml-cpu/ggml-cpu.cpp +671 -0
- package/cpp/ggml-cpu/ops.cpp +9085 -0
- package/cpp/ggml-cpu/ops.h +111 -0
- package/cpp/ggml-cpu/quants.c +1157 -0
- package/cpp/ggml-cpu/quants.h +89 -0
- package/cpp/ggml-cpu/repack.cpp +1570 -0
- package/cpp/ggml-cpu/repack.h +98 -0
- package/cpp/ggml-cpu/simd-mappings.h +1006 -0
- package/cpp/ggml-cpu/traits.cpp +36 -0
- package/cpp/ggml-cpu/traits.h +38 -0
- package/cpp/ggml-cpu/unary-ops.cpp +186 -0
- package/cpp/ggml-cpu/unary-ops.h +28 -0
- package/cpp/ggml-cpu/vec.cpp +321 -0
- package/cpp/ggml-cpu/vec.h +973 -0
- package/cpp/ggml-cpu.h +143 -0
- package/cpp/ggml-impl.h +525 -168
- package/cpp/ggml-metal-impl.h +622 -0
- package/cpp/ggml-metal.h +16 -14
- package/cpp/ggml-metal.m +5289 -1859
- package/cpp/ggml-opt.cpp +1037 -0
- package/cpp/ggml-opt.h +237 -0
- package/cpp/ggml-quants.c +2916 -6877
- package/cpp/ggml-quants.h +87 -249
- package/cpp/ggml-threading.cpp +12 -0
- package/cpp/ggml-threading.h +14 -0
- package/cpp/ggml-whisper-sim.metallib +0 -0
- package/cpp/ggml-whisper.metallib +0 -0
- package/cpp/ggml.c +3293 -16770
- package/cpp/ggml.h +778 -835
- package/cpp/gguf.cpp +1347 -0
- package/cpp/gguf.h +202 -0
- package/cpp/rn-whisper.cpp +84 -0
- package/cpp/rn-whisper.h +2 -0
- package/cpp/whisper-arch.h +197 -0
- package/cpp/whisper.cpp +3240 -944
- package/cpp/whisper.h +144 -31
- package/ios/CMakeLists.txt +95 -0
- package/ios/RNWhisper.h +5 -0
- package/ios/RNWhisper.mm +124 -37
- package/ios/RNWhisperAudioUtils.h +1 -0
- package/ios/RNWhisperAudioUtils.m +24 -13
- package/ios/RNWhisperContext.h +8 -2
- package/ios/RNWhisperContext.mm +42 -8
- package/ios/rnwhisper.xcframework/Info.plist +74 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-backend.h +354 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-common.h +1861 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-impl.h +603 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-metal.h +66 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-opt.h +237 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-quants.h +100 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-threading.h +14 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml.h +2221 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/gguf.h +202 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-whisper.h +52 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/whisper-arch.h +197 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/whisper.h +739 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Info.plist +0 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/ggml-whisper.metallib +0 -0
- package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/rnwhisper +0 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend.h +354 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-common.h +1861 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-impl.h +603 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal.h +66 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-opt.h +237 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-quants.h +100 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-threading.h +14 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml.h +2221 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/gguf.h +202 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper.h +52 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper-arch.h +197 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper.h +739 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Info.plist +0 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/_CodeSignature/CodeResources +101 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/ggml-whisper-sim.metallib +0 -0
- package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/rnwhisper +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-backend.h +354 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-common.h +1861 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-impl.h +603 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-metal.h +66 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-opt.h +237 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-quants.h +100 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-threading.h +14 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml.h +2221 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/gguf.h +202 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-whisper.h +52 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/whisper-arch.h +197 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/whisper.h +739 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Info.plist +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/ggml-whisper.metallib +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/rnwhisper +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend.h +354 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-common.h +1861 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-impl.h +603 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal.h +66 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-opt.h +237 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-quants.h +100 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-threading.h +14 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml.h +2221 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/gguf.h +202 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper.h +52 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper-arch.h +197 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper.h +739 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Info.plist +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/_CodeSignature/CodeResources +101 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/ggml-whisper-sim.metallib +0 -0
- package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/rnwhisper +0 -0
- package/jest/mock.js +14 -1
- package/lib/commonjs/NativeRNWhisper.js.map +1 -1
- package/lib/commonjs/index.js +48 -19
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/version.json +1 -1
- package/lib/module/NativeRNWhisper.js.map +1 -1
- package/lib/module/index.js +48 -19
- package/lib/module/index.js.map +1 -1
- package/lib/module/version.json +1 -1
- package/lib/typescript/NativeRNWhisper.d.ts +6 -3
- package/lib/typescript/NativeRNWhisper.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +25 -3
- package/lib/typescript/index.d.ts.map +1 -1
- package/package.json +15 -10
- package/src/NativeRNWhisper.ts +12 -3
- package/src/index.ts +63 -24
- package/src/version.json +1 -1
- package/whisper-rn.podspec +18 -18
- package/cpp/README.md +0 -4
- package/cpp/ggml-backend.c +0 -1718
- package/cpp/ggml-metal-whisper.metal +0 -5820
package/cpp/gguf.cpp
ADDED
|
@@ -0,0 +1,1347 @@
|
|
|
1
|
+
#include "ggml.h"
|
|
2
|
+
#include "ggml-backend.h"
|
|
3
|
+
#include "ggml-impl.h"
|
|
4
|
+
#include "gguf.h"
|
|
5
|
+
|
|
6
|
+
#include <cinttypes>
|
|
7
|
+
#include <cstddef>
|
|
8
|
+
#include <cstdint>
|
|
9
|
+
#include <cstdio>
|
|
10
|
+
#include <cstdlib>
|
|
11
|
+
#include <cstring>
|
|
12
|
+
#include <map>
|
|
13
|
+
#include <new>
|
|
14
|
+
#include <stdexcept>
|
|
15
|
+
#include <string>
|
|
16
|
+
#include <vector>
|
|
17
|
+
|
|
18
|
+
template <typename T>
|
|
19
|
+
struct type_to_wsp_gguf_type;
|
|
20
|
+
|
|
21
|
+
template <>
|
|
22
|
+
struct type_to_wsp_gguf_type<uint8_t> {
|
|
23
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_UINT8;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
template <>
|
|
27
|
+
struct type_to_wsp_gguf_type<int8_t> {
|
|
28
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_INT8;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
template <>
|
|
32
|
+
struct type_to_wsp_gguf_type<uint16_t> {
|
|
33
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_UINT16;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
template <>
|
|
37
|
+
struct type_to_wsp_gguf_type<int16_t> {
|
|
38
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_INT16;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
template <>
|
|
42
|
+
struct type_to_wsp_gguf_type<uint32_t> {
|
|
43
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_UINT32;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
template <>
|
|
47
|
+
struct type_to_wsp_gguf_type<int32_t> {
|
|
48
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_INT32;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
template <>
|
|
52
|
+
struct type_to_wsp_gguf_type<float> {
|
|
53
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_FLOAT32;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
template <>
|
|
57
|
+
struct type_to_wsp_gguf_type<bool> {
|
|
58
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_BOOL;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
template <>
|
|
62
|
+
struct type_to_wsp_gguf_type<std::string> {
|
|
63
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_STRING;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
template <>
|
|
67
|
+
struct type_to_wsp_gguf_type<uint64_t> {
|
|
68
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_UINT64;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
template <>
|
|
72
|
+
struct type_to_wsp_gguf_type<int64_t> {
|
|
73
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_INT64;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
template <>
|
|
77
|
+
struct type_to_wsp_gguf_type<double> {
|
|
78
|
+
static constexpr enum wsp_gguf_type value = WSP_GGUF_TYPE_FLOAT64;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
static const std::map<wsp_gguf_type, size_t> WSP_GGUF_TYPE_SIZE = {
|
|
82
|
+
{WSP_GGUF_TYPE_UINT8, sizeof(uint8_t)},
|
|
83
|
+
{WSP_GGUF_TYPE_INT8, sizeof(int8_t)},
|
|
84
|
+
{WSP_GGUF_TYPE_UINT16, sizeof(uint16_t)},
|
|
85
|
+
{WSP_GGUF_TYPE_INT16, sizeof(int16_t)},
|
|
86
|
+
{WSP_GGUF_TYPE_UINT32, sizeof(uint32_t)},
|
|
87
|
+
{WSP_GGUF_TYPE_INT32, sizeof(int32_t)},
|
|
88
|
+
{WSP_GGUF_TYPE_FLOAT32, sizeof(float)},
|
|
89
|
+
{WSP_GGUF_TYPE_BOOL, sizeof(int8_t)},
|
|
90
|
+
{WSP_GGUF_TYPE_STRING, 0}, // undefined
|
|
91
|
+
{WSP_GGUF_TYPE_ARRAY, 0}, // undefined
|
|
92
|
+
{WSP_GGUF_TYPE_UINT64, sizeof(uint64_t)},
|
|
93
|
+
{WSP_GGUF_TYPE_INT64, sizeof(int64_t)},
|
|
94
|
+
{WSP_GGUF_TYPE_FLOAT64, sizeof(double)},
|
|
95
|
+
};
|
|
96
|
+
static_assert(WSP_GGUF_TYPE_COUNT == 13, "WSP_GGUF_TYPE_COUNT != 13");
|
|
97
|
+
|
|
98
|
+
static const std::map<wsp_gguf_type, const char *> WSP_GGUF_TYPE_NAME = {
|
|
99
|
+
{WSP_GGUF_TYPE_UINT8, "u8"},
|
|
100
|
+
{WSP_GGUF_TYPE_INT8, "i8"},
|
|
101
|
+
{WSP_GGUF_TYPE_UINT16, "u16"},
|
|
102
|
+
{WSP_GGUF_TYPE_INT16, "i16"},
|
|
103
|
+
{WSP_GGUF_TYPE_UINT32, "u32"},
|
|
104
|
+
{WSP_GGUF_TYPE_INT32, "i32"},
|
|
105
|
+
{WSP_GGUF_TYPE_FLOAT32, "f32"},
|
|
106
|
+
{WSP_GGUF_TYPE_BOOL, "bool"},
|
|
107
|
+
{WSP_GGUF_TYPE_STRING, "str"},
|
|
108
|
+
{WSP_GGUF_TYPE_ARRAY, "arr"},
|
|
109
|
+
{WSP_GGUF_TYPE_UINT64, "u64"},
|
|
110
|
+
{WSP_GGUF_TYPE_INT64, "i64"},
|
|
111
|
+
{WSP_GGUF_TYPE_FLOAT64, "f64"},
|
|
112
|
+
};
|
|
113
|
+
static_assert(WSP_GGUF_TYPE_COUNT == 13, "WSP_GGUF_TYPE_COUNT != 13");
|
|
114
|
+
|
|
115
|
+
size_t wsp_gguf_type_size(enum wsp_gguf_type type) {
|
|
116
|
+
auto it = WSP_GGUF_TYPE_SIZE.find(type);
|
|
117
|
+
return it == WSP_GGUF_TYPE_SIZE.end() ? 0 : it->second;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
struct wsp_gguf_kv {
|
|
121
|
+
std::string key;
|
|
122
|
+
|
|
123
|
+
bool is_array;
|
|
124
|
+
enum wsp_gguf_type type;
|
|
125
|
+
|
|
126
|
+
std::vector<int8_t> data;
|
|
127
|
+
std::vector<std::string> data_string;
|
|
128
|
+
|
|
129
|
+
template <typename T>
|
|
130
|
+
wsp_gguf_kv(const std::string & key, const T value)
|
|
131
|
+
: key(key), is_array(false), type(type_to_wsp_gguf_type<T>::value) {
|
|
132
|
+
WSP_GGML_ASSERT(!key.empty());
|
|
133
|
+
data.resize(sizeof(T));
|
|
134
|
+
memcpy(data.data(), &value, sizeof(T));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
template <typename T>
|
|
138
|
+
wsp_gguf_kv(const std::string & key, const std::vector<T> & value)
|
|
139
|
+
: key(key), is_array(true), type(type_to_wsp_gguf_type<T>::value) {
|
|
140
|
+
WSP_GGML_ASSERT(!key.empty());
|
|
141
|
+
data.resize(value.size()*sizeof(T));
|
|
142
|
+
for (size_t i = 0; i < value.size(); ++i) {
|
|
143
|
+
const T tmp = value[i];
|
|
144
|
+
memcpy(data.data() + i*sizeof(T), &tmp, sizeof(T));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
wsp_gguf_kv(const std::string & key, const std::string & value)
|
|
149
|
+
: key(key), is_array(false), type(WSP_GGUF_TYPE_STRING) {
|
|
150
|
+
WSP_GGML_ASSERT(!key.empty());
|
|
151
|
+
data_string.push_back(value);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
wsp_gguf_kv(const std::string & key, const std::vector<std::string> & value)
|
|
155
|
+
: key(key), is_array(true), type(WSP_GGUF_TYPE_STRING) {
|
|
156
|
+
WSP_GGML_ASSERT(!key.empty());
|
|
157
|
+
data_string = value;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const std::string & get_key() const {
|
|
161
|
+
return key;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const enum wsp_gguf_type & get_type() const {
|
|
165
|
+
return type;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
size_t get_ne() const {
|
|
169
|
+
if (type == WSP_GGUF_TYPE_STRING) {
|
|
170
|
+
const size_t ne = data_string.size();
|
|
171
|
+
WSP_GGML_ASSERT(is_array || ne == 1);
|
|
172
|
+
return ne;
|
|
173
|
+
}
|
|
174
|
+
const size_t type_size = wsp_gguf_type_size(type);
|
|
175
|
+
WSP_GGML_ASSERT(data.size() % type_size == 0);
|
|
176
|
+
const size_t ne = data.size() / type_size;
|
|
177
|
+
WSP_GGML_ASSERT(is_array || ne == 1);
|
|
178
|
+
return ne;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
template <typename T>
|
|
182
|
+
const T & get_val(const size_t i = 0) const {
|
|
183
|
+
WSP_GGML_ASSERT(type_to_wsp_gguf_type<T>::value == type);
|
|
184
|
+
if constexpr (std::is_same<T, std::string>::value) {
|
|
185
|
+
WSP_GGML_ASSERT(data_string.size() >= i+1);
|
|
186
|
+
return data_string[i];
|
|
187
|
+
}
|
|
188
|
+
const size_t type_size = wsp_gguf_type_size(type);
|
|
189
|
+
WSP_GGML_ASSERT(data.size() % type_size == 0);
|
|
190
|
+
WSP_GGML_ASSERT(data.size() >= (i+1)*type_size);
|
|
191
|
+
return reinterpret_cast<const T *>(data.data())[i];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
void cast(const enum wsp_gguf_type new_type) {
|
|
195
|
+
const size_t new_type_size = wsp_gguf_type_size(new_type);
|
|
196
|
+
WSP_GGML_ASSERT(data.size() % new_type_size == 0);
|
|
197
|
+
type = new_type;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
struct wsp_gguf_tensor_info {
|
|
202
|
+
struct wsp_ggml_tensor t; // for holding the equivalent info
|
|
203
|
+
uint64_t offset; // offset from start of `data`, must be a multiple of `ALIGNMENT`
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
struct wsp_gguf_context {
|
|
207
|
+
uint32_t version = WSP_GGUF_VERSION;
|
|
208
|
+
|
|
209
|
+
std::vector<struct wsp_gguf_kv> kv;
|
|
210
|
+
std::vector<struct wsp_gguf_tensor_info> info;
|
|
211
|
+
|
|
212
|
+
size_t alignment = WSP_GGUF_DEFAULT_ALIGNMENT;
|
|
213
|
+
size_t offset = 0; // offset of `data` from beginning of file
|
|
214
|
+
size_t size = 0; // size of `data` in bytes
|
|
215
|
+
|
|
216
|
+
void * data = nullptr;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
struct wsp_gguf_reader {
|
|
220
|
+
FILE * file;
|
|
221
|
+
|
|
222
|
+
wsp_gguf_reader(FILE * file) : file(file) {}
|
|
223
|
+
|
|
224
|
+
template <typename T>
|
|
225
|
+
bool read(T & dst) const {
|
|
226
|
+
return fread(&dst, 1, sizeof(dst), file) == sizeof(dst);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
template <typename T>
|
|
230
|
+
bool read(std::vector<T> & dst, const size_t n) const {
|
|
231
|
+
dst.resize(n);
|
|
232
|
+
for (size_t i = 0; i < dst.size(); ++i) {
|
|
233
|
+
if constexpr (std::is_same<T, bool>::value) {
|
|
234
|
+
bool tmp;
|
|
235
|
+
if (!read(tmp)) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
dst[i] = tmp;
|
|
239
|
+
} else {
|
|
240
|
+
if (!read(dst[i])) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
bool read(bool & dst) const {
|
|
249
|
+
int8_t tmp = -1;
|
|
250
|
+
if (!read(tmp)) {
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
dst = tmp != 0;
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
bool read(enum wsp_ggml_type & dst) const {
|
|
258
|
+
int32_t tmp = -1;
|
|
259
|
+
if (!read(tmp)) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
dst = wsp_ggml_type(tmp);
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
bool read(enum wsp_gguf_type & dst) const {
|
|
267
|
+
int32_t tmp = -1;
|
|
268
|
+
if (!read(tmp)) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
dst = wsp_gguf_type(tmp);
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
bool read(std::string & dst) const {
|
|
276
|
+
uint64_t size = -1;
|
|
277
|
+
if (!read(size)) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
dst.resize(size);
|
|
281
|
+
return fread(dst.data(), 1, dst.length(), file) == dst.length();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
bool read(void * dst, const size_t size) const {
|
|
285
|
+
return fread(dst, 1, size, file) == size;
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
struct wsp_gguf_context * wsp_gguf_init_empty(void) {
|
|
290
|
+
return new wsp_gguf_context;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
template<typename T>
|
|
294
|
+
bool wsp_gguf_read_emplace_helper(const struct wsp_gguf_reader & gr, std::vector<struct wsp_gguf_kv> & kv, const std::string & key, const bool is_array, const size_t n) {
|
|
295
|
+
if (is_array) {
|
|
296
|
+
std::vector<T> value;
|
|
297
|
+
try {
|
|
298
|
+
if (!gr.read(value, n)) {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
} catch (std::length_error &) {
|
|
302
|
+
WSP_GGML_LOG_ERROR("%s: encountered length_error while reading value for key '%s'\n", __func__, key.c_str());
|
|
303
|
+
return false;
|
|
304
|
+
} catch (std::bad_alloc &) {
|
|
305
|
+
WSP_GGML_LOG_ERROR("%s: encountered bad_alloc error while reading value for key '%s'\n", __func__, key.c_str());
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
kv.emplace_back(key, value);
|
|
309
|
+
} else {
|
|
310
|
+
T value;
|
|
311
|
+
if (!gr.read(value)) {
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
kv.emplace_back(key, value);
|
|
315
|
+
}
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
struct wsp_gguf_context * wsp_gguf_init_from_file_impl(FILE * file, struct wsp_gguf_init_params params) {
|
|
320
|
+
const struct wsp_gguf_reader gr(file);
|
|
321
|
+
struct wsp_gguf_context * ctx = new wsp_gguf_context;
|
|
322
|
+
|
|
323
|
+
bool ok = true;
|
|
324
|
+
|
|
325
|
+
// file magic
|
|
326
|
+
{
|
|
327
|
+
std::vector<char> magic;
|
|
328
|
+
ok = ok && gr.read(magic, 4);
|
|
329
|
+
|
|
330
|
+
if (!ok) {
|
|
331
|
+
WSP_GGML_LOG_ERROR("%s: failed to read magic\n", __func__);
|
|
332
|
+
wsp_gguf_free(ctx);
|
|
333
|
+
return nullptr;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
for (uint32_t i = 0; i < magic.size(); i++) {
|
|
337
|
+
if (magic[i] != WSP_GGUF_MAGIC[i]) {
|
|
338
|
+
WSP_GGML_LOG_ERROR("%s: invalid magic characters: '%c%c%c%c', expected 'GGUF'\n", __func__, magic[0], magic[1], magic[2], magic[3]);
|
|
339
|
+
wsp_gguf_free(ctx);
|
|
340
|
+
return nullptr;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// header
|
|
346
|
+
int64_t n_kv = 0;
|
|
347
|
+
int64_t n_tensors = 0;
|
|
348
|
+
|
|
349
|
+
if (ok && gr.read(ctx->version)) {
|
|
350
|
+
if (ok && ctx->version == 0) {
|
|
351
|
+
WSP_GGML_LOG_ERROR("%s: bad GGUF version: %" PRIu32 "\n", __func__, ctx->version);
|
|
352
|
+
ok = false;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/*
|
|
356
|
+
* bit layout is different when reading non-native endian models.
|
|
357
|
+
* assuming that the GGUF version is 3, the non-native endian model
|
|
358
|
+
* would read it as 0x30000000. we can use the AND operation against
|
|
359
|
+
* the last 4 hexadecimal digits to check if the model is the same
|
|
360
|
+
* endianness as the host system.
|
|
361
|
+
*/
|
|
362
|
+
if (ok && (ctx->version & 0x0000FFFF) == 0x00000000) {
|
|
363
|
+
WSP_GGML_LOG_ERROR("%s: failed to load model: this GGUF file version %" PRIu32 " is extremely large, is there a mismatch between the host and model endianness?\n", __func__, ctx->version);
|
|
364
|
+
ok = false;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (ok && ctx->version == 1) {
|
|
368
|
+
WSP_GGML_LOG_ERROR("%s: GGUFv1 is no longer supported, please use a more up-to-date version\n", __func__);
|
|
369
|
+
ok = false;
|
|
370
|
+
}
|
|
371
|
+
if (ok && ctx->version > WSP_GGUF_VERSION) {
|
|
372
|
+
WSP_GGML_LOG_ERROR("%s: this GGUF file is version %" PRIu32 " but this software only supports up to version %d\n",
|
|
373
|
+
__func__, ctx->version, WSP_GGUF_VERSION);
|
|
374
|
+
ok = false;
|
|
375
|
+
}
|
|
376
|
+
} else {
|
|
377
|
+
ok = false;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (ok && gr.read(n_tensors)) {
|
|
381
|
+
static_assert(sizeof(size_t) <= 8 && sizeof(wsp_gguf_tensor_info) >= 2, "int64_t insufficient for indexing");
|
|
382
|
+
if (n_tensors < 0 || n_tensors > int64_t(SIZE_MAX/sizeof(wsp_gguf_tensor_info))) {
|
|
383
|
+
WSP_GGML_LOG_ERROR("%s: number of tensors is %" PRIi64 " but must be in [0, %zu]\n",
|
|
384
|
+
__func__, n_tensors, SIZE_MAX/sizeof(wsp_gguf_tensor_info));
|
|
385
|
+
ok = false;
|
|
386
|
+
}
|
|
387
|
+
} else {
|
|
388
|
+
ok = false;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (ok && gr.read(n_kv)) {
|
|
392
|
+
static_assert(sizeof(size_t) <= 8 && sizeof(wsp_gguf_tensor_info) >= 2, "int64_t insufficient for indexing");
|
|
393
|
+
if (n_kv < 0 || n_kv > int64_t(SIZE_MAX/sizeof(wsp_gguf_kv))) {
|
|
394
|
+
WSP_GGML_LOG_ERROR("%s: number of key value pairs is %" PRIi64 " but must be in [0, %zu]\n",
|
|
395
|
+
__func__, n_kv, SIZE_MAX/sizeof(wsp_gguf_kv));
|
|
396
|
+
ok = false;
|
|
397
|
+
}
|
|
398
|
+
} else {
|
|
399
|
+
ok = false;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (!ok) {
|
|
403
|
+
WSP_GGML_LOG_ERROR("%s: failed to read header\n", __func__);
|
|
404
|
+
wsp_gguf_free(ctx);
|
|
405
|
+
return nullptr;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// KV pairs
|
|
409
|
+
{
|
|
410
|
+
for (int64_t i = 0; ok && i < n_kv; ++i) {
|
|
411
|
+
std::string key;
|
|
412
|
+
wsp_gguf_type type = wsp_gguf_type(-1);
|
|
413
|
+
bool is_array = false;
|
|
414
|
+
uint64_t n = 1;
|
|
415
|
+
|
|
416
|
+
try {
|
|
417
|
+
ok = ok && gr.read(key);
|
|
418
|
+
} catch (std::length_error &) {
|
|
419
|
+
WSP_GGML_LOG_ERROR("%s: encountered length_error while reading key %" PRIi64 "\n", __func__, i);
|
|
420
|
+
ok = false;
|
|
421
|
+
} catch (std::bad_alloc &) {
|
|
422
|
+
WSP_GGML_LOG_ERROR("%s: encountered bad_alloc error while reading key %" PRIi64 "\n", __func__, i);
|
|
423
|
+
ok = false;
|
|
424
|
+
}
|
|
425
|
+
for (size_t j = 0; ok && j < ctx->kv.size(); ++j) {
|
|
426
|
+
if (key == ctx->kv[j].key) {
|
|
427
|
+
WSP_GGML_LOG_ERROR("%s: duplicate key '%s' for tensors %zu and %" PRIi64 " \n", __func__, key.c_str(), j, i);
|
|
428
|
+
ok = false;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
if (!ok) {
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
ok = ok && gr.read(type);
|
|
436
|
+
if (type == WSP_GGUF_TYPE_ARRAY) {
|
|
437
|
+
is_array = true;
|
|
438
|
+
ok = ok && gr.read(type);
|
|
439
|
+
ok = ok && gr.read(n);
|
|
440
|
+
}
|
|
441
|
+
if (!ok) {
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
switch (type) {
|
|
446
|
+
case WSP_GGUF_TYPE_UINT8: ok = ok && wsp_gguf_read_emplace_helper<uint8_t> (gr, ctx->kv, key, is_array, n); break;
|
|
447
|
+
case WSP_GGUF_TYPE_INT8: ok = ok && wsp_gguf_read_emplace_helper<int8_t> (gr, ctx->kv, key, is_array, n); break;
|
|
448
|
+
case WSP_GGUF_TYPE_UINT16: ok = ok && wsp_gguf_read_emplace_helper<uint16_t> (gr, ctx->kv, key, is_array, n); break;
|
|
449
|
+
case WSP_GGUF_TYPE_INT16: ok = ok && wsp_gguf_read_emplace_helper<int16_t> (gr, ctx->kv, key, is_array, n); break;
|
|
450
|
+
case WSP_GGUF_TYPE_UINT32: ok = ok && wsp_gguf_read_emplace_helper<uint32_t> (gr, ctx->kv, key, is_array, n); break;
|
|
451
|
+
case WSP_GGUF_TYPE_INT32: ok = ok && wsp_gguf_read_emplace_helper<int32_t> (gr, ctx->kv, key, is_array, n); break;
|
|
452
|
+
case WSP_GGUF_TYPE_FLOAT32: ok = ok && wsp_gguf_read_emplace_helper<float> (gr, ctx->kv, key, is_array, n); break;
|
|
453
|
+
case WSP_GGUF_TYPE_BOOL: ok = ok && wsp_gguf_read_emplace_helper<bool> (gr, ctx->kv, key, is_array, n); break;
|
|
454
|
+
case WSP_GGUF_TYPE_STRING: ok = ok && wsp_gguf_read_emplace_helper<std::string>(gr, ctx->kv, key, is_array, n); break;
|
|
455
|
+
case WSP_GGUF_TYPE_UINT64: ok = ok && wsp_gguf_read_emplace_helper<uint64_t> (gr, ctx->kv, key, is_array, n); break;
|
|
456
|
+
case WSP_GGUF_TYPE_INT64: ok = ok && wsp_gguf_read_emplace_helper<int64_t> (gr, ctx->kv, key, is_array, n); break;
|
|
457
|
+
case WSP_GGUF_TYPE_FLOAT64: ok = ok && wsp_gguf_read_emplace_helper<double> (gr, ctx->kv, key, is_array, n); break;
|
|
458
|
+
case WSP_GGUF_TYPE_ARRAY:
|
|
459
|
+
default:
|
|
460
|
+
{
|
|
461
|
+
WSP_GGML_LOG_ERROR("%s: key '%s' has invalid GGUF type %d\n", __func__, key.c_str(), type);
|
|
462
|
+
ok = false;
|
|
463
|
+
} break;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (!ok) {
|
|
468
|
+
WSP_GGML_LOG_ERROR("%s: failed to read key-value pairs\n", __func__);
|
|
469
|
+
wsp_gguf_free(ctx);
|
|
470
|
+
return nullptr;
|
|
471
|
+
}
|
|
472
|
+
WSP_GGML_ASSERT(int64_t(ctx->kv.size()) == n_kv);
|
|
473
|
+
|
|
474
|
+
const int alignment_idx = wsp_gguf_find_key(ctx, WSP_GGUF_KEY_GENERAL_ALIGNMENT);
|
|
475
|
+
ctx->alignment = alignment_idx == -1 ? WSP_GGUF_DEFAULT_ALIGNMENT : wsp_gguf_get_val_u32(ctx, alignment_idx);
|
|
476
|
+
|
|
477
|
+
if (ctx->alignment == 0 || (ctx->alignment & (ctx->alignment - 1)) != 0) {
|
|
478
|
+
WSP_GGML_LOG_ERROR("%s: alignment %zu is not a power of 2\n", __func__, ctx->alignment);
|
|
479
|
+
wsp_gguf_free(ctx);
|
|
480
|
+
return nullptr;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// read the tensor info
|
|
485
|
+
for (int64_t i = 0; ok && i < n_tensors; ++i) {
|
|
486
|
+
struct wsp_gguf_tensor_info info;
|
|
487
|
+
|
|
488
|
+
// tensor name
|
|
489
|
+
{
|
|
490
|
+
std::string name;
|
|
491
|
+
try {
|
|
492
|
+
ok = ok && gr.read(name);
|
|
493
|
+
} catch (std::length_error &) {
|
|
494
|
+
WSP_GGML_LOG_ERROR("%s: encountered length_error while reading tensor name %" PRIi64 "\n", __func__, i);
|
|
495
|
+
ok = false;
|
|
496
|
+
} catch (std::bad_alloc &) {
|
|
497
|
+
WSP_GGML_LOG_ERROR("%s: encountered bad_alloc error while reading tensor name %" PRIi64 "\n", __func__, i);
|
|
498
|
+
ok = false;
|
|
499
|
+
}
|
|
500
|
+
if (name.length() >= WSP_GGML_MAX_NAME) {
|
|
501
|
+
WSP_GGML_LOG_ERROR("%s: tensor name %" PRIi64 " is too long: %zu >= %d\n", __func__, i, name.length(), WSP_GGML_MAX_NAME);
|
|
502
|
+
ok = false;
|
|
503
|
+
break;
|
|
504
|
+
}
|
|
505
|
+
wsp_ggml_set_name(&info.t, name.c_str());
|
|
506
|
+
|
|
507
|
+
// make sure there are no duplicate tensor names
|
|
508
|
+
for (int64_t j = 0; ok && j < i; ++j) {
|
|
509
|
+
if (strcmp(info.t.name, ctx->info[j].t.name) == 0) {
|
|
510
|
+
WSP_GGML_LOG_ERROR("%s: duplicate tensor name '%s' for tensors %" PRIi64 " and %" PRIi64 "\n", __func__, info.t.name, j, i);
|
|
511
|
+
ok = false;
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (!ok) {
|
|
517
|
+
break;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// tensor shape
|
|
521
|
+
{
|
|
522
|
+
uint32_t n_dims = -1;
|
|
523
|
+
ok = ok && gr.read(n_dims);
|
|
524
|
+
if (n_dims > WSP_GGML_MAX_DIMS) {
|
|
525
|
+
WSP_GGML_LOG_ERROR("%s: tensor '%s' has invalid number of dimensions: %" PRIu32 " > %" PRIu32 "\n",
|
|
526
|
+
__func__, info.t.name, n_dims, WSP_GGML_MAX_DIMS);
|
|
527
|
+
ok = false;
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
for (uint32_t j = 0; ok && j < WSP_GGML_MAX_DIMS; ++j) {
|
|
531
|
+
info.t.ne[j] = 1;
|
|
532
|
+
if (j < n_dims) {
|
|
533
|
+
ok = ok && gr.read(info.t.ne[j]);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// check that all ne are non-negative
|
|
537
|
+
if (info.t.ne[j] < 0) {
|
|
538
|
+
WSP_GGML_LOG_ERROR("%s: tensor '%s' dimension %" PRIu32 " has invalid number of elements: %" PRIi64 " < 0\n",
|
|
539
|
+
__func__, info.t.name, j, info.t.ne[j]);
|
|
540
|
+
ok = false;
|
|
541
|
+
break;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// check that the total number of elements is representable
|
|
546
|
+
if (ok && ((INT64_MAX/info.t.ne[1] <= info.t.ne[0]) ||
|
|
547
|
+
(INT64_MAX/info.t.ne[2] <= info.t.ne[0]*info.t.ne[1]) ||
|
|
548
|
+
(INT64_MAX/info.t.ne[3] <= info.t.ne[0]*info.t.ne[1]*info.t.ne[2]))) {
|
|
549
|
+
|
|
550
|
+
WSP_GGML_LOG_ERROR("%s: total number of elements in tensor '%s' with shape "
|
|
551
|
+
"(%" PRIi64 ", %" PRIi64 ", %" PRIi64 ", %" PRIi64 ") is >= %" PRIi64 "\n",
|
|
552
|
+
__func__, info.t.name, info.t.ne[0], info.t.ne[1], info.t.ne[2], info.t.ne[3], INT64_MAX);
|
|
553
|
+
ok = false;
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (!ok) {
|
|
558
|
+
break;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// tensor type
|
|
562
|
+
{
|
|
563
|
+
ok = ok && gr.read(info.t.type);
|
|
564
|
+
|
|
565
|
+
// check that tensor type is within defined range
|
|
566
|
+
if (info.t.type < 0 || info.t.type >= WSP_GGML_TYPE_COUNT) {
|
|
567
|
+
WSP_GGML_LOG_ERROR("%s: tensor '%s' has invalid ggml type %d (%s)\n",
|
|
568
|
+
__func__, info.t.name, info.t.type, wsp_ggml_type_name(info.t.type));
|
|
569
|
+
ok = false;
|
|
570
|
+
break;
|
|
571
|
+
}
|
|
572
|
+
const size_t type_size = wsp_ggml_type_size(info.t.type);
|
|
573
|
+
const int64_t blck_size = wsp_ggml_blck_size(info.t.type);
|
|
574
|
+
|
|
575
|
+
// check that row size is divisible by block size
|
|
576
|
+
if (blck_size == 0 || info.t.ne[0] % blck_size != 0) {
|
|
577
|
+
WSP_GGML_LOG_ERROR("%s: tensor '%s' of type %d (%s) has %" PRId64 " elements per row, "
|
|
578
|
+
"not a multiple of block size (%" PRId64 ")\n",
|
|
579
|
+
__func__, info.t.name, (int) info.t.type, wsp_ggml_type_name(info.t.type), info.t.ne[0], blck_size);
|
|
580
|
+
ok = false;
|
|
581
|
+
break;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// calculate byte offsets given the tensor shape and type
|
|
585
|
+
info.t.nb[0] = type_size;
|
|
586
|
+
info.t.nb[1] = info.t.nb[0]*(info.t.ne[0]/blck_size);
|
|
587
|
+
for (int j = 2; j < WSP_GGML_MAX_DIMS; ++j) {
|
|
588
|
+
info.t.nb[j] = info.t.nb[j - 1]*info.t.ne[j - 1];
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
if (!ok) {
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// tensor data offset within buffer
|
|
596
|
+
ok = ok && gr.read(info.offset);
|
|
597
|
+
|
|
598
|
+
ctx->info.push_back(info);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
if (!ok) {
|
|
602
|
+
WSP_GGML_LOG_ERROR("%s: failed to read tensor info\n", __func__);
|
|
603
|
+
wsp_gguf_free(ctx);
|
|
604
|
+
return nullptr;
|
|
605
|
+
}
|
|
606
|
+
WSP_GGML_ASSERT(int64_t(ctx->info.size()) == n_tensors);
|
|
607
|
+
|
|
608
|
+
// we require the data section to be aligned, so take into account any padding
|
|
609
|
+
if (fseek(file, WSP_GGML_PAD(ftell(file), ctx->alignment), SEEK_SET) != 0) {
|
|
610
|
+
WSP_GGML_LOG_ERROR("%s: failed to seek to beginning of data section\n", __func__);
|
|
611
|
+
wsp_gguf_free(ctx);
|
|
612
|
+
return nullptr;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// store the current file offset - this is where the data section starts
|
|
616
|
+
ctx->offset = ftell(file);
|
|
617
|
+
|
|
618
|
+
// compute the total size of the data section, taking into account the alignment
|
|
619
|
+
{
|
|
620
|
+
ctx->size = 0;
|
|
621
|
+
for (size_t i = 0; i < ctx->info.size(); ++i) {
|
|
622
|
+
const wsp_gguf_tensor_info & ti = ctx->info[i];
|
|
623
|
+
if (ti.offset != ctx->size) {
|
|
624
|
+
WSP_GGML_LOG_ERROR("%s: tensor '%s' has offset %" PRIu64 ", expected %zu\n",
|
|
625
|
+
__func__, ti.t.name, ti.offset, ctx->size);
|
|
626
|
+
WSP_GGML_LOG_ERROR("%s: failed to read tensor data\n", __func__);
|
|
627
|
+
wsp_gguf_free(ctx);
|
|
628
|
+
return nullptr;
|
|
629
|
+
}
|
|
630
|
+
ctx->size += WSP_GGML_PAD(wsp_ggml_nbytes(&ti.t), ctx->alignment);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// load the tensor data only if requested
|
|
635
|
+
if (params.ctx != nullptr) {
|
|
636
|
+
// if the provided wsp_gguf_context is no_alloc, then we create "empty" tensors and do not read the binary blob
|
|
637
|
+
// otherwise, we load the binary blob into the created wsp_ggml_context as well, and point the "data" members of
|
|
638
|
+
// the wsp_ggml_tensor structs to the appropriate locations in the binary blob
|
|
639
|
+
|
|
640
|
+
// compute the exact size needed for the new wsp_ggml_context
|
|
641
|
+
const size_t mem_size =
|
|
642
|
+
params.no_alloc ?
|
|
643
|
+
(n_tensors )*wsp_ggml_tensor_overhead() :
|
|
644
|
+
(n_tensors + 1)*wsp_ggml_tensor_overhead() + ctx->size;
|
|
645
|
+
|
|
646
|
+
struct wsp_ggml_init_params pdata = {
|
|
647
|
+
/*mem_size =*/ mem_size,
|
|
648
|
+
/*mem_buffer =*/ nullptr,
|
|
649
|
+
/*no_alloc =*/ params.no_alloc,
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
*params.ctx = wsp_ggml_init(pdata);
|
|
653
|
+
if (*params.ctx == nullptr) {
|
|
654
|
+
WSP_GGML_LOG_ERROR("%s: failed to initialize ggml context for storing tensors\n", __func__);
|
|
655
|
+
wsp_gguf_free(ctx);
|
|
656
|
+
return nullptr;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
struct wsp_ggml_context * ctx_data = *params.ctx;
|
|
660
|
+
|
|
661
|
+
struct wsp_ggml_tensor * data = nullptr;
|
|
662
|
+
|
|
663
|
+
if (!params.no_alloc) {
|
|
664
|
+
data = wsp_ggml_new_tensor_1d(ctx_data, WSP_GGML_TYPE_I8, ctx->size);
|
|
665
|
+
|
|
666
|
+
ok = ok && data != nullptr;
|
|
667
|
+
|
|
668
|
+
if (ok) {
|
|
669
|
+
wsp_ggml_set_name(data, "GGUF tensor data binary blob");
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
// read the binary blob with the tensor data
|
|
673
|
+
ok = ok && gr.read(data->data, ctx->size);
|
|
674
|
+
|
|
675
|
+
if (!ok) {
|
|
676
|
+
WSP_GGML_LOG_ERROR("%s: failed to read tensor data binary blob\n", __func__);
|
|
677
|
+
wsp_ggml_free(ctx_data);
|
|
678
|
+
*params.ctx = nullptr;
|
|
679
|
+
wsp_gguf_free(ctx);
|
|
680
|
+
return nullptr;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
ctx->data = data->data;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
wsp_ggml_set_no_alloc(ctx_data, true);
|
|
687
|
+
|
|
688
|
+
// create the tensors
|
|
689
|
+
for (size_t i = 0; i < ctx->info.size(); ++i) {
|
|
690
|
+
const struct wsp_gguf_tensor_info & info = ctx->info[i];
|
|
691
|
+
|
|
692
|
+
struct wsp_ggml_tensor * cur = wsp_ggml_new_tensor(ctx_data, info.t.type, WSP_GGML_MAX_DIMS, info.t.ne);
|
|
693
|
+
|
|
694
|
+
ok = ok && cur != nullptr;
|
|
695
|
+
|
|
696
|
+
if (!ok) {
|
|
697
|
+
break;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
wsp_ggml_set_name(cur, info.t.name);
|
|
701
|
+
|
|
702
|
+
// point the data member to the appropriate location in the binary blob using the tensor info
|
|
703
|
+
if (!params.no_alloc) {
|
|
704
|
+
cur->data = (char *) data->data + info.offset;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
if (!ok) {
|
|
709
|
+
WSP_GGML_LOG_ERROR("%s: failed to create tensors\n", __func__);
|
|
710
|
+
wsp_ggml_free(ctx_data);
|
|
711
|
+
*params.ctx = nullptr;
|
|
712
|
+
wsp_gguf_free(ctx);
|
|
713
|
+
return nullptr;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
wsp_ggml_set_no_alloc(ctx_data, params.no_alloc);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
return ctx;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
struct wsp_gguf_context * wsp_gguf_init_from_file(const char * fname, struct wsp_gguf_init_params params) {
|
|
723
|
+
FILE * file = wsp_ggml_fopen(fname, "rb");
|
|
724
|
+
|
|
725
|
+
if (!file) {
|
|
726
|
+
WSP_GGML_LOG_ERROR("%s: failed to open GGUF file '%s'\n", __func__, fname);
|
|
727
|
+
return nullptr;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
struct wsp_gguf_context * result = wsp_gguf_init_from_file_impl(file, params);
|
|
731
|
+
fclose(file);
|
|
732
|
+
return result;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
void wsp_gguf_free(struct wsp_gguf_context * ctx) {
|
|
736
|
+
if (ctx == nullptr) {
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
delete ctx;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
const char * wsp_gguf_type_name(enum wsp_gguf_type type) {
|
|
743
|
+
auto it = WSP_GGUF_TYPE_NAME.find(type);
|
|
744
|
+
return it == WSP_GGUF_TYPE_NAME.end() ? nullptr : it->second;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
uint32_t wsp_gguf_get_version(const struct wsp_gguf_context * ctx) {
|
|
748
|
+
return ctx->version;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
size_t wsp_gguf_get_alignment(const struct wsp_gguf_context * ctx) {
|
|
752
|
+
return ctx->alignment;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
size_t wsp_gguf_get_data_offset(const struct wsp_gguf_context * ctx) {
|
|
756
|
+
return ctx->offset;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
int64_t wsp_gguf_get_n_kv(const struct wsp_gguf_context * ctx) {
|
|
760
|
+
return ctx->kv.size();
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
int64_t wsp_gguf_find_key(const struct wsp_gguf_context * ctx, const char * key) {
|
|
764
|
+
// return -1 if key not found
|
|
765
|
+
int64_t keyfound = -1;
|
|
766
|
+
|
|
767
|
+
const int64_t n_kv = wsp_gguf_get_n_kv(ctx);
|
|
768
|
+
|
|
769
|
+
for (int64_t i = 0; i < n_kv; ++i) {
|
|
770
|
+
if (strcmp(key, wsp_gguf_get_key(ctx, i)) == 0) {
|
|
771
|
+
keyfound = i;
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
return keyfound;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
const char * wsp_gguf_get_key(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
780
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
781
|
+
return ctx->kv[key_id].get_key().c_str();
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
enum wsp_gguf_type wsp_gguf_get_kv_type(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
785
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
786
|
+
return ctx->kv[key_id].is_array ? WSP_GGUF_TYPE_ARRAY : ctx->kv[key_id].get_type();
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
enum wsp_gguf_type wsp_gguf_get_arr_type(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
790
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
791
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].is_array);
|
|
792
|
+
return ctx->kv[key_id].get_type();
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
const void * wsp_gguf_get_arr_data(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
796
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
797
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_type() != WSP_GGUF_TYPE_STRING);
|
|
798
|
+
return ctx->kv[key_id].data.data();
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
const char * wsp_gguf_get_arr_str(const struct wsp_gguf_context * ctx, int64_t key_id, size_t i) {
|
|
802
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
803
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_type() == WSP_GGUF_TYPE_STRING);
|
|
804
|
+
return ctx->kv[key_id].data_string[i].c_str();
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
size_t wsp_gguf_get_arr_n(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
808
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
809
|
+
|
|
810
|
+
if (ctx->kv[key_id].type == WSP_GGUF_TYPE_STRING) {
|
|
811
|
+
return ctx->kv[key_id].data_string.size();
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
const size_t type_size = wsp_gguf_type_size(ctx->kv[key_id].type);
|
|
815
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].data.size() % type_size == 0);
|
|
816
|
+
return ctx->kv[key_id].data.size() / type_size;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
uint8_t wsp_gguf_get_val_u8(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
820
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
821
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
822
|
+
return ctx->kv[key_id].get_val<uint8_t>();
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
int8_t wsp_gguf_get_val_i8(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
826
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
827
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
828
|
+
return ctx->kv[key_id].get_val<int8_t>();
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
uint16_t wsp_gguf_get_val_u16(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
832
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
833
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
834
|
+
return ctx->kv[key_id].get_val<uint16_t>();
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
int16_t wsp_gguf_get_val_i16(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
838
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
839
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
840
|
+
return ctx->kv[key_id].get_val<int16_t>();
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
uint32_t wsp_gguf_get_val_u32(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
844
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
845
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
846
|
+
return ctx->kv[key_id].get_val<uint32_t>();
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
int32_t wsp_gguf_get_val_i32(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
850
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
851
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
852
|
+
return ctx->kv[key_id].get_val<int32_t>();
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
float wsp_gguf_get_val_f32(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
856
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
857
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
858
|
+
return ctx->kv[key_id].get_val<float>();
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
uint64_t wsp_gguf_get_val_u64(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
862
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
863
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
864
|
+
return ctx->kv[key_id].get_val<uint64_t>();
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
int64_t wsp_gguf_get_val_i64(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
868
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
869
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
870
|
+
return ctx->kv[key_id].get_val<int64_t>();
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
double wsp_gguf_get_val_f64(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
874
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
875
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
876
|
+
return ctx->kv[key_id].get_val<double>();
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
bool wsp_gguf_get_val_bool(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
880
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
881
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
882
|
+
return ctx->kv[key_id].get_val<bool>();
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
const char * wsp_gguf_get_val_str(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
886
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
887
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
888
|
+
return ctx->kv[key_id].get_val<std::string>().c_str();
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
const void * wsp_gguf_get_val_data(const struct wsp_gguf_context * ctx, int64_t key_id) {
|
|
892
|
+
WSP_GGML_ASSERT(key_id >= 0 && key_id < wsp_gguf_get_n_kv(ctx));
|
|
893
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_ne() == 1);
|
|
894
|
+
WSP_GGML_ASSERT(ctx->kv[key_id].get_type() != WSP_GGUF_TYPE_STRING);
|
|
895
|
+
return ctx->kv[key_id].data.data();
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
int64_t wsp_gguf_get_n_tensors(const struct wsp_gguf_context * ctx) {
|
|
899
|
+
return ctx->info.size();
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
int64_t wsp_gguf_find_tensor(const struct wsp_gguf_context * ctx, const char * name) {
|
|
903
|
+
// return -1 if tensor not found
|
|
904
|
+
int64_t tensor_id = -1;
|
|
905
|
+
|
|
906
|
+
const int64_t n_tensors = wsp_gguf_get_n_tensors(ctx);
|
|
907
|
+
|
|
908
|
+
for (int64_t i = 0; i < n_tensors; ++i) {
|
|
909
|
+
if (strcmp(name, wsp_gguf_get_tensor_name(ctx, i)) == 0) {
|
|
910
|
+
tensor_id = i;
|
|
911
|
+
break;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
return tensor_id;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
size_t wsp_gguf_get_tensor_offset(const struct wsp_gguf_context * ctx, int64_t tensor_id) {
|
|
919
|
+
WSP_GGML_ASSERT(tensor_id >= 0 && tensor_id < wsp_gguf_get_n_tensors(ctx));
|
|
920
|
+
return ctx->info[tensor_id].offset;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
const char * wsp_gguf_get_tensor_name(const struct wsp_gguf_context * ctx, int64_t tensor_id) {
|
|
924
|
+
WSP_GGML_ASSERT(tensor_id >= 0 && tensor_id < wsp_gguf_get_n_tensors(ctx));
|
|
925
|
+
return ctx->info[tensor_id].t.name;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
enum wsp_ggml_type wsp_gguf_get_tensor_type(const struct wsp_gguf_context * ctx, int64_t tensor_id) {
|
|
929
|
+
WSP_GGML_ASSERT(tensor_id >= 0 && tensor_id < wsp_gguf_get_n_tensors(ctx));
|
|
930
|
+
return ctx->info[tensor_id].t.type;
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
size_t wsp_gguf_get_tensor_size(const struct wsp_gguf_context * ctx, int64_t tensor_id) {
|
|
934
|
+
WSP_GGML_ASSERT(tensor_id >= 0 && tensor_id < wsp_gguf_get_n_tensors(ctx));
|
|
935
|
+
return wsp_ggml_nbytes(&ctx->info[tensor_id].t);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
int64_t wsp_gguf_remove_key(struct wsp_gguf_context * ctx, const char * key) {
|
|
939
|
+
const int64_t key_id = wsp_gguf_find_key(ctx, key);
|
|
940
|
+
if (key_id >= 0) {
|
|
941
|
+
ctx->kv.erase(ctx->kv.begin() + key_id);
|
|
942
|
+
}
|
|
943
|
+
return key_id;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
template<typename T>
|
|
947
|
+
static void wsp_gguf_check_reserved_keys(const std::string & key, const T val) {
|
|
948
|
+
if (key == WSP_GGUF_KEY_GENERAL_ALIGNMENT) {
|
|
949
|
+
if constexpr (std::is_same<T, uint32_t>::value) {
|
|
950
|
+
WSP_GGML_ASSERT(val > 0 && (val & (val - 1)) == 0 && WSP_GGUF_KEY_GENERAL_ALIGNMENT " must be power of 2");
|
|
951
|
+
} else {
|
|
952
|
+
WSP_GGML_UNUSED(val);
|
|
953
|
+
WSP_GGML_ABORT(WSP_GGUF_KEY_GENERAL_ALIGNMENT " must be type u32");
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
void wsp_gguf_set_val_u8(struct wsp_gguf_context * ctx, const char * key, uint8_t val) {
|
|
959
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
960
|
+
wsp_gguf_remove_key(ctx, key);
|
|
961
|
+
ctx->kv.emplace_back(key, val);
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
void wsp_gguf_set_val_i8(struct wsp_gguf_context * ctx, const char * key, int8_t val) {
|
|
965
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
966
|
+
wsp_gguf_remove_key(ctx, key);
|
|
967
|
+
ctx->kv.emplace_back(key, val);
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
void wsp_gguf_set_val_u16(struct wsp_gguf_context * ctx, const char * key, uint16_t val) {
|
|
971
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
972
|
+
wsp_gguf_remove_key(ctx, key);
|
|
973
|
+
ctx->kv.emplace_back(key, val);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
void wsp_gguf_set_val_i16(struct wsp_gguf_context * ctx, const char * key, int16_t val) {
|
|
977
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
978
|
+
wsp_gguf_remove_key(ctx, key);
|
|
979
|
+
ctx->kv.emplace_back(key, val);
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
void wsp_gguf_set_val_u32(struct wsp_gguf_context * ctx, const char * key, uint32_t val) {
|
|
983
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
984
|
+
wsp_gguf_remove_key(ctx, key);
|
|
985
|
+
ctx->kv.emplace_back(key, val);
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
void wsp_gguf_set_val_i32(struct wsp_gguf_context * ctx, const char * key, int32_t val) {
|
|
989
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
990
|
+
wsp_gguf_remove_key(ctx, key);
|
|
991
|
+
ctx->kv.emplace_back(key, val);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
void wsp_gguf_set_val_f32(struct wsp_gguf_context * ctx, const char * key, float val) {
|
|
995
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
996
|
+
wsp_gguf_remove_key(ctx, key);
|
|
997
|
+
ctx->kv.emplace_back(key, val);
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
void wsp_gguf_set_val_u64(struct wsp_gguf_context * ctx, const char * key, uint64_t val) {
|
|
1001
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
1002
|
+
wsp_gguf_remove_key(ctx, key);
|
|
1003
|
+
ctx->kv.emplace_back(key, val);
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
void wsp_gguf_set_val_i64(struct wsp_gguf_context * ctx, const char * key, int64_t val) {
|
|
1007
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
1008
|
+
wsp_gguf_remove_key(ctx, key);
|
|
1009
|
+
ctx->kv.emplace_back(key, val);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
void wsp_gguf_set_val_f64(struct wsp_gguf_context * ctx, const char * key, double val) {
|
|
1013
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
1014
|
+
wsp_gguf_remove_key(ctx, key);
|
|
1015
|
+
ctx->kv.emplace_back(key, val);
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
void wsp_gguf_set_val_bool(struct wsp_gguf_context * ctx, const char * key, bool val) {
|
|
1019
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
1020
|
+
wsp_gguf_remove_key(ctx, key);
|
|
1021
|
+
ctx->kv.emplace_back(key, val);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
void wsp_gguf_set_val_str(struct wsp_gguf_context * ctx, const char * key, const char * val) {
|
|
1025
|
+
wsp_gguf_check_reserved_keys(key, val);
|
|
1026
|
+
wsp_gguf_remove_key(ctx, key);
|
|
1027
|
+
ctx->kv.emplace_back(key, std::string(val));
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
void wsp_gguf_set_arr_data(struct wsp_gguf_context * ctx, const char * key, enum wsp_gguf_type type, const void * data, size_t n) {
|
|
1031
|
+
wsp_gguf_check_reserved_keys(key, data);
|
|
1032
|
+
wsp_gguf_remove_key(ctx, key);
|
|
1033
|
+
|
|
1034
|
+
const size_t nbytes = n*wsp_gguf_type_size(type);
|
|
1035
|
+
std::vector<int8_t> tmp(nbytes);
|
|
1036
|
+
if (!tmp.empty()) {
|
|
1037
|
+
memcpy(tmp.data(), data, nbytes);
|
|
1038
|
+
}
|
|
1039
|
+
ctx->kv.emplace_back(key, tmp);
|
|
1040
|
+
ctx->kv.back().cast(type);
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
void wsp_gguf_set_arr_str(struct wsp_gguf_context * ctx, const char * key, const char ** data, size_t n) {
|
|
1044
|
+
wsp_gguf_check_reserved_keys(key, data);
|
|
1045
|
+
wsp_gguf_remove_key(ctx, key);
|
|
1046
|
+
|
|
1047
|
+
std::vector<std::string> tmp(n);
|
|
1048
|
+
for (size_t i = 0; i < n; ++i) {
|
|
1049
|
+
tmp[i] = data[i];
|
|
1050
|
+
}
|
|
1051
|
+
ctx->kv.emplace_back(key, tmp);
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
// set or add KV pairs from another context
|
|
1055
|
+
void wsp_gguf_set_kv(struct wsp_gguf_context * ctx, const struct wsp_gguf_context * src) {
|
|
1056
|
+
const int64_t n_kv = wsp_gguf_get_n_kv(src);
|
|
1057
|
+
for (int64_t i = 0; i < n_kv; ++i) {
|
|
1058
|
+
const struct wsp_gguf_kv & kv = src->kv[i];
|
|
1059
|
+
|
|
1060
|
+
if (!kv.is_array) {
|
|
1061
|
+
switch (kv.get_type()) {
|
|
1062
|
+
case WSP_GGUF_TYPE_UINT8: wsp_gguf_set_val_u8 (ctx, kv.get_key().c_str(), kv.get_val<uint8_t>()); break;
|
|
1063
|
+
case WSP_GGUF_TYPE_INT8: wsp_gguf_set_val_i8 (ctx, kv.get_key().c_str(), kv.get_val<int8_t>()); break;
|
|
1064
|
+
case WSP_GGUF_TYPE_UINT16: wsp_gguf_set_val_u16 (ctx, kv.get_key().c_str(), kv.get_val<uint16_t>()); break;
|
|
1065
|
+
case WSP_GGUF_TYPE_INT16: wsp_gguf_set_val_i16 (ctx, kv.get_key().c_str(), kv.get_val<int16_t>()); break;
|
|
1066
|
+
case WSP_GGUF_TYPE_UINT32: wsp_gguf_set_val_u32 (ctx, kv.get_key().c_str(), kv.get_val<uint32_t>()); break;
|
|
1067
|
+
case WSP_GGUF_TYPE_INT32: wsp_gguf_set_val_i32 (ctx, kv.get_key().c_str(), kv.get_val<int32_t>()); break;
|
|
1068
|
+
case WSP_GGUF_TYPE_FLOAT32: wsp_gguf_set_val_f32 (ctx, kv.get_key().c_str(), kv.get_val<float>()); break;
|
|
1069
|
+
case WSP_GGUF_TYPE_UINT64: wsp_gguf_set_val_u64 (ctx, kv.get_key().c_str(), kv.get_val<uint64_t>()); break;
|
|
1070
|
+
case WSP_GGUF_TYPE_INT64: wsp_gguf_set_val_i64 (ctx, kv.get_key().c_str(), kv.get_val<int64_t>()); break;
|
|
1071
|
+
case WSP_GGUF_TYPE_FLOAT64: wsp_gguf_set_val_f64 (ctx, kv.get_key().c_str(), kv.get_val<double>()); break;
|
|
1072
|
+
case WSP_GGUF_TYPE_BOOL: wsp_gguf_set_val_bool(ctx, kv.get_key().c_str(), kv.get_val<bool>()); break;
|
|
1073
|
+
case WSP_GGUF_TYPE_STRING: wsp_gguf_set_val_str (ctx, kv.get_key().c_str(), kv.get_val<std::string>().c_str()); break;
|
|
1074
|
+
case WSP_GGUF_TYPE_ARRAY:
|
|
1075
|
+
default: WSP_GGML_ABORT("invalid type");
|
|
1076
|
+
}
|
|
1077
|
+
continue;
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
const size_t ne = kv.get_ne();
|
|
1081
|
+
|
|
1082
|
+
switch (kv.get_type()) {
|
|
1083
|
+
case WSP_GGUF_TYPE_UINT8:
|
|
1084
|
+
case WSP_GGUF_TYPE_INT8:
|
|
1085
|
+
case WSP_GGUF_TYPE_UINT16:
|
|
1086
|
+
case WSP_GGUF_TYPE_INT16:
|
|
1087
|
+
case WSP_GGUF_TYPE_UINT32:
|
|
1088
|
+
case WSP_GGUF_TYPE_INT32:
|
|
1089
|
+
case WSP_GGUF_TYPE_FLOAT32:
|
|
1090
|
+
case WSP_GGUF_TYPE_UINT64:
|
|
1091
|
+
case WSP_GGUF_TYPE_INT64:
|
|
1092
|
+
case WSP_GGUF_TYPE_FLOAT64:
|
|
1093
|
+
case WSP_GGUF_TYPE_BOOL: {
|
|
1094
|
+
wsp_gguf_set_arr_data(ctx, kv.get_key().c_str(), kv.get_type(), kv.data.data(), ne);
|
|
1095
|
+
} break;
|
|
1096
|
+
case WSP_GGUF_TYPE_STRING: {
|
|
1097
|
+
std::vector<const char *> tmp(ne);
|
|
1098
|
+
for (size_t j = 0; j < ne; ++j) {
|
|
1099
|
+
tmp[j] = kv.data_string[j].c_str();
|
|
1100
|
+
}
|
|
1101
|
+
wsp_gguf_set_arr_str(ctx, kv.get_key().c_str(), tmp.data(), ne);
|
|
1102
|
+
} break;
|
|
1103
|
+
case WSP_GGUF_TYPE_ARRAY:
|
|
1104
|
+
default: WSP_GGML_ABORT("invalid type");
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
void wsp_gguf_add_tensor(
|
|
1110
|
+
struct wsp_gguf_context * ctx,
|
|
1111
|
+
const struct wsp_ggml_tensor * tensor) {
|
|
1112
|
+
WSP_GGML_ASSERT(tensor);
|
|
1113
|
+
if (wsp_gguf_find_tensor(ctx, tensor->name) != -1) {
|
|
1114
|
+
WSP_GGML_ABORT("duplicate tensor name: %s", tensor->name);
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
struct wsp_gguf_tensor_info ti;
|
|
1118
|
+
ti.t = *tensor;
|
|
1119
|
+
ti.offset = ctx->info.empty() ? 0 :
|
|
1120
|
+
ctx->info.back().offset + WSP_GGML_PAD(wsp_ggml_nbytes(&ctx->info.back().t), ctx->alignment);
|
|
1121
|
+
ctx->info.push_back(ti);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
void wsp_gguf_set_tensor_type(struct wsp_gguf_context * ctx, const char * name, enum wsp_ggml_type type) {
|
|
1125
|
+
const int64_t tensor_id = wsp_gguf_find_tensor(ctx, name);
|
|
1126
|
+
if (tensor_id < 0) {
|
|
1127
|
+
WSP_GGML_ABORT("tensor not found: %s", name);
|
|
1128
|
+
}
|
|
1129
|
+
struct wsp_ggml_tensor * tensor = &ctx->info[tensor_id].t;
|
|
1130
|
+
const size_t type_size = wsp_ggml_type_size(type);
|
|
1131
|
+
const int64_t blck_size = wsp_ggml_blck_size(type);
|
|
1132
|
+
|
|
1133
|
+
tensor->type = type;
|
|
1134
|
+
WSP_GGML_ASSERT(tensor->ne[0] % blck_size == 0 && "tensor row size not divisible by block size of new type");
|
|
1135
|
+
|
|
1136
|
+
tensor->nb[0] = type_size;
|
|
1137
|
+
tensor->nb[1] = tensor->nb[0]*(tensor->ne[0]/blck_size);
|
|
1138
|
+
for (int i = 2; i < WSP_GGML_MAX_DIMS; i++) {
|
|
1139
|
+
tensor->nb[i] = tensor->nb[i - 1]*tensor->ne[i - 1];
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
// update offsets
|
|
1143
|
+
const int64_t n_tensors = wsp_gguf_get_n_tensors(ctx);
|
|
1144
|
+
for (int64_t i = tensor_id + 1; i < n_tensors; ++i) {
|
|
1145
|
+
ctx->info[i].offset = ctx->info[i - 1].offset + WSP_GGML_PAD(wsp_ggml_nbytes(&ctx->info[i - 1].t), ctx->alignment);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
void wsp_gguf_set_tensor_data(struct wsp_gguf_context * ctx, const char * name, const void * data) {
|
|
1150
|
+
const int64_t tensor_id = wsp_gguf_find_tensor(ctx, name);
|
|
1151
|
+
if (tensor_id < 0) {
|
|
1152
|
+
WSP_GGML_ABORT("tensor not found: %s", name);
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
ctx->info[tensor_id].t.data = (void *)(uintptr_t)data; // double cast suppresses warning about casting away const
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
struct wsp_gguf_writer {
|
|
1159
|
+
std::vector<int8_t> & buf;
|
|
1160
|
+
|
|
1161
|
+
wsp_gguf_writer(std::vector<int8_t> & buf) : buf(buf) {}
|
|
1162
|
+
|
|
1163
|
+
template <typename T>
|
|
1164
|
+
void write(const T & val) const {
|
|
1165
|
+
for (size_t i = 0; i < sizeof(val); ++i) {
|
|
1166
|
+
buf.push_back(reinterpret_cast<const int8_t *>(&val)[i]);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
void write(const std::vector<int8_t> & val) const {
|
|
1171
|
+
buf.insert(buf.end(), val.begin(), val.end());
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
void write(const bool & val) const {
|
|
1175
|
+
const int8_t val8 = val ? 1 : 0;
|
|
1176
|
+
write(val8);
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
void write(const std::string & val) const {
|
|
1180
|
+
{
|
|
1181
|
+
const uint64_t n = val.length();
|
|
1182
|
+
write(n);
|
|
1183
|
+
}
|
|
1184
|
+
for (size_t i = 0; i < val.length(); ++i) {
|
|
1185
|
+
buf.push_back(reinterpret_cast<const int8_t *>(val.data())[i]);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
void write(const char * val) const {
|
|
1190
|
+
write(std::string(val));
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
void write(const enum wsp_ggml_type & val) const {
|
|
1194
|
+
write(int32_t(val));
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
void write(const enum wsp_gguf_type & val) const {
|
|
1198
|
+
write(int32_t(val));
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
void write(const struct wsp_gguf_kv & kv) const {
|
|
1202
|
+
const uint64_t ne = kv.get_ne();
|
|
1203
|
+
|
|
1204
|
+
write(kv.get_key());
|
|
1205
|
+
|
|
1206
|
+
if (kv.is_array) {
|
|
1207
|
+
write(WSP_GGUF_TYPE_ARRAY);
|
|
1208
|
+
write(kv.get_type());
|
|
1209
|
+
write(ne);
|
|
1210
|
+
} else {
|
|
1211
|
+
write(kv.get_type());
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
switch (kv.get_type()) {
|
|
1215
|
+
case WSP_GGUF_TYPE_UINT8:
|
|
1216
|
+
case WSP_GGUF_TYPE_INT8:
|
|
1217
|
+
case WSP_GGUF_TYPE_UINT16:
|
|
1218
|
+
case WSP_GGUF_TYPE_INT16:
|
|
1219
|
+
case WSP_GGUF_TYPE_UINT32:
|
|
1220
|
+
case WSP_GGUF_TYPE_INT32:
|
|
1221
|
+
case WSP_GGUF_TYPE_FLOAT32:
|
|
1222
|
+
case WSP_GGUF_TYPE_UINT64:
|
|
1223
|
+
case WSP_GGUF_TYPE_INT64:
|
|
1224
|
+
case WSP_GGUF_TYPE_FLOAT64: {
|
|
1225
|
+
write(kv.data);
|
|
1226
|
+
} break;
|
|
1227
|
+
case WSP_GGUF_TYPE_BOOL: {
|
|
1228
|
+
for (size_t i = 0; i < ne; ++i) {
|
|
1229
|
+
write(kv.get_val<bool>(i));
|
|
1230
|
+
}
|
|
1231
|
+
} break;
|
|
1232
|
+
case WSP_GGUF_TYPE_STRING: {
|
|
1233
|
+
for (size_t i = 0; i < ne; ++i) {
|
|
1234
|
+
write(kv.get_val<std::string>(i));
|
|
1235
|
+
}
|
|
1236
|
+
} break;
|
|
1237
|
+
case WSP_GGUF_TYPE_ARRAY:
|
|
1238
|
+
default: WSP_GGML_ABORT("invalid type");
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
void write_tensor_meta(const struct wsp_gguf_tensor_info & info) const {
|
|
1243
|
+
write(info.t.name);
|
|
1244
|
+
|
|
1245
|
+
const uint32_t n_dims = wsp_ggml_n_dims(&info.t);
|
|
1246
|
+
write(n_dims);
|
|
1247
|
+
|
|
1248
|
+
for (uint32_t j = 0; j < n_dims; ++j) {
|
|
1249
|
+
write(info.t.ne[j]);
|
|
1250
|
+
}
|
|
1251
|
+
write(info.t.type);
|
|
1252
|
+
write(info.offset);
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
void pad(const size_t alignment) const {
|
|
1256
|
+
while (buf.size() % alignment != 0) {
|
|
1257
|
+
const int8_t zero = 0;
|
|
1258
|
+
write(zero);
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
void write_tensor_data(const struct wsp_gguf_tensor_info & info, const size_t offset_data, const size_t alignment) const {
|
|
1263
|
+
WSP_GGML_ASSERT(buf.size() - offset_data == info.offset);
|
|
1264
|
+
|
|
1265
|
+
WSP_GGML_ASSERT(wsp_ggml_is_contiguous(&info.t));
|
|
1266
|
+
const size_t offset = buf.size();
|
|
1267
|
+
const size_t nbytes = wsp_ggml_nbytes(&info.t);
|
|
1268
|
+
|
|
1269
|
+
buf.resize(offset + nbytes);
|
|
1270
|
+
if (info.t.buffer) {
|
|
1271
|
+
wsp_ggml_backend_tensor_get(&info.t, buf.data() + offset, 0, nbytes);
|
|
1272
|
+
} else {
|
|
1273
|
+
WSP_GGML_ASSERT(info.t.data);
|
|
1274
|
+
memcpy(buf.data() + offset, info.t.data, nbytes);
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
pad(alignment);
|
|
1278
|
+
}
|
|
1279
|
+
};
|
|
1280
|
+
|
|
1281
|
+
void wsp_gguf_write_to_buf(const struct wsp_gguf_context * ctx, std::vector<int8_t> & buf, bool only_meta) {
|
|
1282
|
+
const struct wsp_gguf_writer gw(buf);
|
|
1283
|
+
|
|
1284
|
+
const int64_t n_kv = wsp_gguf_get_n_kv(ctx);
|
|
1285
|
+
const int64_t n_tensors = wsp_gguf_get_n_tensors(ctx);
|
|
1286
|
+
|
|
1287
|
+
// write header
|
|
1288
|
+
gw.write(WSP_GGUF_MAGIC[0]);
|
|
1289
|
+
gw.write(WSP_GGUF_MAGIC[1]);
|
|
1290
|
+
gw.write(WSP_GGUF_MAGIC[2]);
|
|
1291
|
+
gw.write(WSP_GGUF_MAGIC[3]);
|
|
1292
|
+
gw.write(ctx->version);
|
|
1293
|
+
gw.write(n_tensors);
|
|
1294
|
+
gw.write(n_kv);
|
|
1295
|
+
|
|
1296
|
+
// write key-value pairs
|
|
1297
|
+
for (int64_t i = 0; i < n_kv; ++i) {
|
|
1298
|
+
gw.write(ctx->kv[i]);
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
// write tensor info
|
|
1302
|
+
for (int64_t i = 0; i < n_tensors; ++i) {
|
|
1303
|
+
gw.write_tensor_meta(ctx->info[i]);
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
// we require the data section to be aligned
|
|
1307
|
+
gw.pad(ctx->alignment);
|
|
1308
|
+
|
|
1309
|
+
if (only_meta) {
|
|
1310
|
+
return;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
const size_t offset_data = gw.buf.size();
|
|
1314
|
+
|
|
1315
|
+
// write tensor data
|
|
1316
|
+
for (int64_t i = 0; i < n_tensors; ++i) {
|
|
1317
|
+
gw.write_tensor_data(ctx->info[i], offset_data, ctx->alignment);
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
bool wsp_gguf_write_to_file(const struct wsp_gguf_context * ctx, const char * fname, bool only_meta) {
|
|
1322
|
+
FILE * file = wsp_ggml_fopen(fname, "wb");
|
|
1323
|
+
|
|
1324
|
+
if (!file) {
|
|
1325
|
+
WSP_GGML_LOG_ERROR("%s: failed to open file '%s' for writing GGUF data\n", __func__, fname);
|
|
1326
|
+
return false;
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
std::vector<int8_t> buf;
|
|
1330
|
+
wsp_gguf_write_to_buf(ctx, buf, only_meta);
|
|
1331
|
+
const bool ok = fwrite(buf.data(), 1, buf.size(), file) == buf.size();
|
|
1332
|
+
fclose(file);
|
|
1333
|
+
return ok;
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
size_t wsp_gguf_get_meta_size(const struct wsp_gguf_context * ctx) {
|
|
1337
|
+
// only return size
|
|
1338
|
+
std::vector<int8_t> buf;
|
|
1339
|
+
wsp_gguf_write_to_buf(ctx, buf, /*only_meta =*/ true);
|
|
1340
|
+
return buf.size();
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
void wsp_gguf_get_meta_data(const struct wsp_gguf_context * ctx, void * data) {
|
|
1344
|
+
std::vector<int8_t> buf;
|
|
1345
|
+
wsp_gguf_write_to_buf(ctx, buf, /*only_meta =*/ true);
|
|
1346
|
+
memcpy(data, buf.data(), buf.size());
|
|
1347
|
+
}
|