@novastera-oss/llamarn 0.2.5 → 0.2.7
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/RNLlamaCpp.podspec +3 -2
- package/android/CMakeLists.txt +6 -3
- package/android/src/main/cpp/include/llama.h +140 -38
- package/android/src/main/jniLibs/arm64-v8a/libggml-base.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libggml-cpu.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libggml.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libllama.so +0 -0
- package/android/src/main/jniLibs/x86_64/libggml-base.so +0 -0
- package/android/src/main/jniLibs/x86_64/libggml-cpu.so +0 -0
- package/android/src/main/jniLibs/x86_64/libggml.so +0 -0
- package/android/src/main/jniLibs/x86_64/libllama.so +0 -0
- package/cpp/LlamaCppModel.cpp +48 -67
- package/cpp/LlamaCppModel.h +8 -3
- package/cpp/PureCppImpl.cpp +1 -1
- package/cpp/PureCppImpl.h +2 -2
- package/cpp/build-info.cpp +2 -2
- package/cpp/llama.cpp/CMakeLists.txt +15 -4
- package/cpp/llama.cpp/Makefile +2 -2
- package/cpp/llama.cpp/README.md +33 -13
- package/cpp/llama.cpp/common/CMakeLists.txt +15 -28
- package/cpp/llama.cpp/common/arg.cpp +38 -12
- package/cpp/llama.cpp/common/build-info.cpp.in +2 -2
- package/cpp/llama.cpp/common/chat-parser.cpp +9 -3
- package/cpp/llama.cpp/common/chat-parser.h +4 -1
- package/cpp/llama.cpp/common/chat.cpp +16 -13
- package/cpp/llama.cpp/common/chat.h +1 -1
- package/cpp/llama.cpp/common/common.cpp +52 -40
- package/cpp/llama.cpp/common/common.h +5 -2
- package/cpp/llama.cpp/common/json-partial.cpp +5 -4
- package/cpp/llama.cpp/common/json-partial.h +2 -1
- package/cpp/llama.cpp/common/json-schema-to-grammar.cpp +2 -1
- package/cpp/llama.cpp/common/json-schema-to-grammar.h +4 -4
- package/cpp/llama.cpp/common/speculative.cpp +6 -4
- package/cpp/llama.cpp/convert_hf_to_gguf.py +128 -84
- package/cpp/llama.cpp/ggml/CMakeLists.txt +47 -2
- package/cpp/llama.cpp/ggml/cmake/common.cmake +1 -2
- package/cpp/llama.cpp/ggml/include/ggml.h +1 -3
- package/cpp/llama.cpp/ggml/src/CMakeLists.txt +49 -13
- package/cpp/llama.cpp/ggml/src/ggml-backend-reg.cpp +5 -0
- package/cpp/llama.cpp/ggml/src/ggml-backend.cpp +10 -5
- package/cpp/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +3 -3
- package/cpp/llama.cpp/ggml/src/ggml-cann/common.h +6 -1
- package/cpp/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +33 -9
- package/cpp/llama.cpp/ggml/src/ggml-common.h +4 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +93 -24
- package/cpp/llama.cpp/ggml/src/ggml-cpu/amx/amx.cpp +1 -1
- package/cpp/llama.cpp/ggml/src/ggml-cpu/amx/mmq.cpp +1 -1
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/arm/quants.c +4113 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/arm/repack.cpp +2174 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +2638 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/powerpc/quants.c +2731 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +2068 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/riscv/repack.cpp +396 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/s390/quants.c +1299 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/wasm/quants.c +1480 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/x86/quants.c +4310 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-aarch64.cpp → arch/x86/repack.cpp} +59 -3206
- package/cpp/llama.cpp/ggml/src/ggml-cpu/arch-fallback.h +184 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/common.h +1 -1
- package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +7 -4
- package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +33 -2
- package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +8 -8
- package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-hbm.cpp → hbm.cpp} +1 -1
- package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +1 -1
- package/cpp/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +56 -7
- package/cpp/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.h +5 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/ops.cpp +2 -2
- package/cpp/llama.cpp/ggml/src/ggml-cpu/quants.c +1157 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-quants.h → quants.h} +26 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/repack.cpp +1555 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/repack.h +98 -0
- package/cpp/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +2 -4
- package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-traits.cpp → traits.cpp} +1 -1
- package/cpp/llama.cpp/ggml/src/ggml-cuda/common.cuh +6 -8
- package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-mma-f16.cuh +5 -2
- package/cpp/llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu +25 -16
- package/cpp/llama.cpp/ggml/src/ggml-cuda/ssm-scan.cu +6 -4
- package/cpp/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +4 -0
- package/cpp/llama.cpp/ggml/src/ggml-impl.h +2 -0
- package/cpp/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +11 -10
- package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.m +33 -8
- package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.metal +135 -100
- package/cpp/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +7 -0
- package/cpp/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +908 -3
- package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/concat.cl +109 -0
- package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_q4_0_f32_8x_flat.cl +283 -0
- package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/pad.cl +30 -0
- package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/repeat.cl +39 -0
- package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/tanh.cl +63 -0
- package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/tsembd.cl +48 -0
- package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/upscale.cl +121 -0
- package/cpp/llama.cpp/ggml/src/ggml-quants.c +0 -2
- package/cpp/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +18 -15
- package/cpp/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +3 -3
- package/cpp/llama.cpp/ggml/src/ggml-sycl/common.hpp +19 -24
- package/cpp/llama.cpp/ggml/src/ggml-sycl/convert.cpp +21 -2
- package/cpp/llama.cpp/ggml/src/ggml-sycl/cpy.cpp +121 -4
- package/cpp/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +32 -0
- package/cpp/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +3 -0
- package/cpp/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +2 -96
- package/cpp/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +164 -46
- package/cpp/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +32 -8
- package/cpp/llama.cpp/ggml/src/ggml-sycl/quants.hpp +38 -10
- package/cpp/llama.cpp/ggml/src/ggml-sycl/rope.cpp +118 -11
- package/cpp/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +108 -16
- package/cpp/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +26 -29
- package/cpp/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +432 -248
- package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +0 -12
- package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/conv_transpose_1d.comp +98 -0
- package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +2 -0
- package/cpp/llama.cpp/ggml/src/ggml.c +9 -8
- package/cpp/llama.cpp/ggml/src/ggml.cpp +26 -0
- package/cpp/llama.cpp/ggml/src/gguf.cpp +19 -2
- package/cpp/llama.cpp/gguf-py/gguf/constants.py +57 -0
- package/cpp/llama.cpp/gguf-py/gguf/gguf_writer.py +4 -1
- package/cpp/llama.cpp/gguf-py/gguf/tensor_mapping.py +14 -3
- package/cpp/llama.cpp/include/llama.h +140 -38
- package/cpp/llama.cpp/requirements/requirements-compare-llama-bench.txt +1 -0
- package/cpp/llama.cpp/src/CMakeLists.txt +4 -1
- package/cpp/llama.cpp/src/llama-arch.cpp +95 -3
- package/cpp/llama.cpp/src/llama-arch.h +7 -1
- package/cpp/llama.cpp/src/llama-batch.cpp +289 -31
- package/cpp/llama.cpp/src/llama-batch.h +47 -17
- package/cpp/llama.cpp/src/llama-chat.cpp +19 -2
- package/cpp/llama.cpp/src/llama-chat.h +1 -0
- package/cpp/llama.cpp/src/llama-context.cpp +488 -313
- package/cpp/llama.cpp/src/llama-context.h +38 -17
- package/cpp/llama.cpp/src/llama-cparams.cpp +1 -1
- package/cpp/llama.cpp/src/llama-cparams.h +1 -1
- package/cpp/llama.cpp/src/llama-graph.cpp +275 -152
- package/cpp/llama.cpp/src/llama-graph.h +109 -52
- package/cpp/llama.cpp/src/llama-hparams.cpp +6 -2
- package/cpp/llama.cpp/src/llama-hparams.h +8 -2
- package/cpp/llama.cpp/src/llama-kv-cache-unified-iswa.cpp +281 -0
- package/cpp/llama.cpp/src/llama-kv-cache-unified-iswa.h +133 -0
- package/cpp/llama.cpp/src/llama-kv-cache-unified.cpp +1835 -0
- package/cpp/llama.cpp/src/llama-kv-cache-unified.h +308 -0
- package/cpp/llama.cpp/src/llama-kv-cells.h +53 -17
- package/cpp/llama.cpp/src/llama-memory-hybrid.cpp +247 -0
- package/cpp/llama.cpp/src/llama-memory-hybrid.h +143 -0
- package/cpp/llama.cpp/src/llama-memory-recurrent.cpp +1116 -0
- package/cpp/llama.cpp/src/llama-memory-recurrent.h +188 -0
- package/cpp/llama.cpp/src/llama-memory.cpp +41 -0
- package/cpp/llama.cpp/src/llama-memory.h +89 -4
- package/cpp/llama.cpp/src/llama-mmap.cpp +1 -1
- package/cpp/llama.cpp/src/llama-model-loader.cpp +42 -17
- package/cpp/llama.cpp/src/llama-model.cpp +735 -143
- package/cpp/llama.cpp/src/llama-model.h +4 -0
- package/cpp/llama.cpp/src/llama-quant.cpp +2 -1
- package/cpp/llama.cpp/src/llama-vocab.cpp +39 -25
- package/cpp/llama.cpp/src/llama.cpp +11 -7
- package/cpp/llama.cpp/src/unicode.cpp +5 -0
- package/cpp/llama.cpp/vendor/cpp-httplib/httplib.h +10518 -0
- package/cpp/llama.cpp/vendor/miniaudio/miniaudio.h +93468 -0
- package/cpp/llama.cpp/{common → vendor}/minja/chat-template.hpp +1 -1
- package/cpp/llama.cpp/{common → vendor}/minja/minja.hpp +1 -1
- package/cpp/llama.cpp/{common → vendor/nlohmann}/json.hpp +3027 -2267
- package/cpp/llama.cpp/vendor/nlohmann/json_fwd.hpp +187 -0
- package/cpp/llama.cpp/vendor/stb/stb_image.h +7988 -0
- package/cpp/rn-completion.cpp +65 -10
- package/cpp/{rn-llama.hpp → rn-llama.h} +1 -1
- package/cpp/{rn-utils.hpp → rn-utils.h} +8 -1
- package/ios/include/chat.h +1 -1
- package/ios/include/common/minja/chat-template.hpp +1 -1
- package/ios/include/common/minja/minja.hpp +1 -1
- package/ios/include/common.h +5 -2
- package/ios/include/json-schema-to-grammar.h +4 -4
- package/ios/include/llama.h +140 -38
- package/ios/include/{common → nlohmann}/json.hpp +3027 -2267
- package/ios/libs/llama.xcframework/Info.plist +20 -20
- package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
- package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4863 -4617
- package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml.h +1 -3
- package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/llama.h +140 -38
- package/ios/libs/llama.xcframework/ios-arm64/llama.framework/llama +0 -0
- package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
- package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4834 -4638
- package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3742 -3557
- package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +1 -3
- package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/llama.h +140 -38
- package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/llama +0 -0
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4834 -4638
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3744 -3559
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml.h +1 -3
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/llama.h +140 -38
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml.h +1 -3
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/llama.h +140 -38
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/llama +0 -0
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml.h +1 -3
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/llama.h +140 -38
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/llama +0 -0
- package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/llama +0 -0
- package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
- package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4863 -4616
- package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml.h +1 -3
- package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/llama.h +140 -38
- package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/llama +0 -0
- package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
- package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4834 -4637
- package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3742 -3556
- package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +1 -3
- package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/llama.h +140 -38
- package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/llama +0 -0
- package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
- package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4900 -4653
- package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml.h +1 -3
- package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/llama.h +140 -38
- package/ios/libs/llama.xcframework/xros-arm64/llama.framework/llama +0 -0
- package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
- package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4871 -4674
- package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3773 -3587
- package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +1 -3
- package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/llama.h +140 -38
- package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/llama +0 -0
- package/package.json +1 -2
- package/cpp/llama.cpp/common/cmake/build-info-gen-cpp.cmake +0 -24
- package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +0 -8
- package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +0 -13891
- package/cpp/llama.cpp/src/llama-kv-cache.cpp +0 -2747
- package/cpp/llama.cpp/src/llama-kv-cache.h +0 -502
- /package/cpp/llama.cpp/ggml/src/ggml-cpu/{cpu-feats-x86.cpp → arch/x86/cpu-feats.cpp} +0 -0
- /package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-hbm.h → hbm.h} +0 -0
- /package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-traits.h → traits.h} +0 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "llama-batch.h"
|
|
4
|
+
#include "llama-graph.h"
|
|
5
|
+
#include "llama-kv-cells.h"
|
|
6
|
+
#include "llama-memory.h"
|
|
7
|
+
|
|
8
|
+
#include <unordered_map>
|
|
9
|
+
#include <vector>
|
|
10
|
+
|
|
11
|
+
struct llama_cparams;
|
|
12
|
+
struct llama_hparams;
|
|
13
|
+
struct llama_model;
|
|
14
|
+
struct llama_context;
|
|
15
|
+
|
|
16
|
+
//
|
|
17
|
+
// llama_kv_cache_unified
|
|
18
|
+
//
|
|
19
|
+
|
|
20
|
+
class llama_kv_cache_unified : public llama_memory_i {
|
|
21
|
+
public:
|
|
22
|
+
static uint32_t get_padding(const llama_cparams & cparams);
|
|
23
|
+
|
|
24
|
+
// this callback is used to filter out layers that should not be included in the cache
|
|
25
|
+
using layer_filter_cb = std::function<bool(int32_t il)>;
|
|
26
|
+
|
|
27
|
+
using ubatch_heads = std::vector<uint32_t>;
|
|
28
|
+
|
|
29
|
+
struct defrag_info {
|
|
30
|
+
bool empty() const {
|
|
31
|
+
return ids.empty();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// contains information about which cell moves where:
|
|
35
|
+
// - cell i moves to ids[i]
|
|
36
|
+
// - if ids[i] == i || ids[i] == ids.size(), then cell i is not moved
|
|
37
|
+
std::vector<uint32_t> ids;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
llama_kv_cache_unified(
|
|
41
|
+
const llama_model & model,
|
|
42
|
+
layer_filter_cb && filter,
|
|
43
|
+
ggml_type type_k,
|
|
44
|
+
ggml_type type_v,
|
|
45
|
+
bool v_trans,
|
|
46
|
+
bool offload,
|
|
47
|
+
uint32_t kv_size,
|
|
48
|
+
uint32_t n_seq_max,
|
|
49
|
+
uint32_t n_pad,
|
|
50
|
+
uint32_t n_swa,
|
|
51
|
+
llama_swa_type swa_type);
|
|
52
|
+
|
|
53
|
+
~llama_kv_cache_unified() = default;
|
|
54
|
+
|
|
55
|
+
//
|
|
56
|
+
// llama_memory_i
|
|
57
|
+
//
|
|
58
|
+
|
|
59
|
+
llama_memory_state_ptr init_batch(
|
|
60
|
+
const llama_batch & batch,
|
|
61
|
+
uint32_t n_ubatch,
|
|
62
|
+
bool embd_all) override;
|
|
63
|
+
|
|
64
|
+
llama_memory_state_ptr init_full() override;
|
|
65
|
+
|
|
66
|
+
llama_memory_state_ptr init_update(llama_context * lctx, bool optimize) override;
|
|
67
|
+
|
|
68
|
+
bool get_can_shift() const override;
|
|
69
|
+
|
|
70
|
+
void clear(bool data) override;
|
|
71
|
+
|
|
72
|
+
bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
|
|
73
|
+
void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
|
|
74
|
+
void seq_keep(llama_seq_id seq_id) override;
|
|
75
|
+
void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) override;
|
|
76
|
+
void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override;
|
|
77
|
+
|
|
78
|
+
llama_pos seq_pos_min(llama_seq_id seq_id) const override;
|
|
79
|
+
llama_pos seq_pos_max(llama_seq_id seq_id) const override;
|
|
80
|
+
|
|
81
|
+
// state write/load
|
|
82
|
+
|
|
83
|
+
void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1) const override;
|
|
84
|
+
void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1) override;
|
|
85
|
+
|
|
86
|
+
//
|
|
87
|
+
// llama_kv_cache_unified specific API
|
|
88
|
+
//
|
|
89
|
+
|
|
90
|
+
uint32_t get_size() const;
|
|
91
|
+
|
|
92
|
+
bool get_has_shift() const;
|
|
93
|
+
|
|
94
|
+
//
|
|
95
|
+
// graph_build API
|
|
96
|
+
//
|
|
97
|
+
|
|
98
|
+
uint32_t get_n_kv() const;
|
|
99
|
+
|
|
100
|
+
// get views of the current state of the cache
|
|
101
|
+
ggml_tensor * get_k(ggml_context * ctx, int32_t il, uint32_t n_kv) const;
|
|
102
|
+
ggml_tensor * get_v(ggml_context * ctx, int32_t il, uint32_t n_kv) const;
|
|
103
|
+
|
|
104
|
+
// store k_cur and v_cur in the cache based on the provided head location
|
|
105
|
+
ggml_tensor * cpy_k(ggml_context * ctx, ggml_tensor * k_cur, int32_t il, uint32_t head_cur) const;
|
|
106
|
+
ggml_tensor * cpy_v(ggml_context * ctx, ggml_tensor * v_cur, int32_t il, uint32_t head_cur) const;
|
|
107
|
+
|
|
108
|
+
//
|
|
109
|
+
// preparation API
|
|
110
|
+
//
|
|
111
|
+
|
|
112
|
+
// find places for the provided ubatches in the cache, returns the head locations
|
|
113
|
+
// return empty vector on failure
|
|
114
|
+
ubatch_heads prepare(const std::vector<llama_ubatch> & ubatches);
|
|
115
|
+
|
|
116
|
+
bool update(llama_context * lctx, bool do_shift, const defrag_info & dinfo);
|
|
117
|
+
|
|
118
|
+
// return the cell position where we can insert the ubatch
|
|
119
|
+
// return -1 on failure to find a contiguous slot of kv cells
|
|
120
|
+
int32_t find_slot(const llama_ubatch & ubatch) const;
|
|
121
|
+
|
|
122
|
+
// emplace the ubatch context into slot: [head_cur, head_cur + ubatch.n_tokens)
|
|
123
|
+
void apply_ubatch(uint32_t head_cur, const llama_ubatch & ubatch);
|
|
124
|
+
|
|
125
|
+
//
|
|
126
|
+
// set_input API
|
|
127
|
+
//
|
|
128
|
+
|
|
129
|
+
void set_input_kq_mask (ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const;
|
|
130
|
+
void set_input_k_shift (ggml_tensor * dst) const;
|
|
131
|
+
void set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const;
|
|
132
|
+
|
|
133
|
+
private:
|
|
134
|
+
const llama_model & model;
|
|
135
|
+
const llama_hparams & hparams;
|
|
136
|
+
|
|
137
|
+
struct kv_layer {
|
|
138
|
+
// layer index in the model
|
|
139
|
+
// note: can be different from the layer index in the KV cache
|
|
140
|
+
uint32_t il;
|
|
141
|
+
|
|
142
|
+
ggml_tensor * k;
|
|
143
|
+
ggml_tensor * v;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
bool v_trans = true; // the value tensor is transposed
|
|
147
|
+
|
|
148
|
+
// the current index from where we start searching for a free slot in the ring buffer of KV cells (see find_slot())
|
|
149
|
+
// note: this is not part of the KV state and it's only used to speed-up the find_slot() method
|
|
150
|
+
uint32_t head = 0;
|
|
151
|
+
|
|
152
|
+
const uint32_t n_seq_max = 1;
|
|
153
|
+
|
|
154
|
+
// required padding
|
|
155
|
+
const uint32_t n_pad = 1;
|
|
156
|
+
|
|
157
|
+
// SWA
|
|
158
|
+
const uint32_t n_swa = 0;
|
|
159
|
+
|
|
160
|
+
int debug = 0;
|
|
161
|
+
|
|
162
|
+
const llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE;
|
|
163
|
+
|
|
164
|
+
std::vector<ggml_context_ptr> ctxs;
|
|
165
|
+
std::vector<ggml_backend_buffer_ptr> bufs;
|
|
166
|
+
|
|
167
|
+
llama_kv_cells_unified cells;
|
|
168
|
+
|
|
169
|
+
std::vector<kv_layer> layers;
|
|
170
|
+
|
|
171
|
+
// model layer id -> KV cache layer id
|
|
172
|
+
std::unordered_map<int32_t, int32_t> map_layer_ids;
|
|
173
|
+
|
|
174
|
+
// return non-empty vector if cells have been moved
|
|
175
|
+
defrag_info defrag_prepare(int32_t n_max_nodes) const;
|
|
176
|
+
|
|
177
|
+
size_t total_size() const;
|
|
178
|
+
|
|
179
|
+
size_t size_k_bytes() const;
|
|
180
|
+
size_t size_v_bytes() const;
|
|
181
|
+
|
|
182
|
+
bool is_masked_swa(llama_pos p0, llama_pos p1) const;
|
|
183
|
+
|
|
184
|
+
ggml_tensor * build_rope_shift(
|
|
185
|
+
const llama_cparams & cparams,
|
|
186
|
+
ggml_context * ctx,
|
|
187
|
+
ggml_tensor * cur,
|
|
188
|
+
ggml_tensor * shift,
|
|
189
|
+
ggml_tensor * factors,
|
|
190
|
+
float freq_base,
|
|
191
|
+
float freq_scale) const;
|
|
192
|
+
|
|
193
|
+
llm_graph_result_ptr build_graph_shift(
|
|
194
|
+
const llama_cparams & cparams,
|
|
195
|
+
ggml_context * ctx,
|
|
196
|
+
ggml_cgraph * gf) const;
|
|
197
|
+
|
|
198
|
+
llm_graph_result_ptr build_graph_defrag(
|
|
199
|
+
const llama_cparams & cparams,
|
|
200
|
+
ggml_context * ctx,
|
|
201
|
+
ggml_cgraph * gf,
|
|
202
|
+
const defrag_info & dinfo) const;
|
|
203
|
+
|
|
204
|
+
void state_write_meta(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id = -1) const;
|
|
205
|
+
void state_write_data(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) const;
|
|
206
|
+
|
|
207
|
+
bool state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id = -1);
|
|
208
|
+
bool state_read_data(llama_io_read_i & io, uint32_t cell_count);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
class llama_kv_cache_unified_state : public llama_memory_state_i {
|
|
212
|
+
public:
|
|
213
|
+
// some shorthands
|
|
214
|
+
using ubatch_heads = llama_kv_cache_unified::ubatch_heads;
|
|
215
|
+
using defrag_info = llama_kv_cache_unified::defrag_info;
|
|
216
|
+
|
|
217
|
+
// used for errors
|
|
218
|
+
llama_kv_cache_unified_state(llama_memory_status status);
|
|
219
|
+
|
|
220
|
+
// used to create a full-cache state
|
|
221
|
+
llama_kv_cache_unified_state(
|
|
222
|
+
llama_kv_cache_unified * kv);
|
|
223
|
+
|
|
224
|
+
// used to create an update state
|
|
225
|
+
llama_kv_cache_unified_state(
|
|
226
|
+
llama_kv_cache_unified * kv,
|
|
227
|
+
llama_context * lctx,
|
|
228
|
+
bool do_shift,
|
|
229
|
+
defrag_info dinfo);
|
|
230
|
+
|
|
231
|
+
// used to create a decode state from a batch
|
|
232
|
+
llama_kv_cache_unified_state(
|
|
233
|
+
llama_kv_cache_unified * kv,
|
|
234
|
+
llama_sbatch sbatch,
|
|
235
|
+
ubatch_heads heads,
|
|
236
|
+
std::vector<llama_ubatch> ubatches);
|
|
237
|
+
|
|
238
|
+
virtual ~llama_kv_cache_unified_state();
|
|
239
|
+
|
|
240
|
+
//
|
|
241
|
+
// llama_memory_state_i
|
|
242
|
+
//
|
|
243
|
+
|
|
244
|
+
bool next() override;
|
|
245
|
+
bool apply() override;
|
|
246
|
+
|
|
247
|
+
std::vector<int64_t> & out_ids() override;
|
|
248
|
+
|
|
249
|
+
llama_memory_status get_status() const override;
|
|
250
|
+
const llama_ubatch & get_ubatch() const override;
|
|
251
|
+
|
|
252
|
+
//
|
|
253
|
+
// llama_kv_cache_unified_state specific API
|
|
254
|
+
//
|
|
255
|
+
|
|
256
|
+
uint32_t get_n_kv() const;
|
|
257
|
+
|
|
258
|
+
// get views of the current state of the cache
|
|
259
|
+
ggml_tensor * get_k(ggml_context * ctx, int32_t il) const;
|
|
260
|
+
ggml_tensor * get_v(ggml_context * ctx, int32_t il) const;
|
|
261
|
+
|
|
262
|
+
// store k_cur and v_cur in the cache based on the provided head location
|
|
263
|
+
ggml_tensor * cpy_k(ggml_context * ctx, ggml_tensor * k_cur, int32_t il) const;
|
|
264
|
+
ggml_tensor * cpy_v(ggml_context * ctx, ggml_tensor * v_cur, int32_t il) const;
|
|
265
|
+
|
|
266
|
+
void set_input_k_shift(ggml_tensor * dst) const;
|
|
267
|
+
|
|
268
|
+
void set_input_kq_mask (ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const;
|
|
269
|
+
void set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const;
|
|
270
|
+
|
|
271
|
+
private:
|
|
272
|
+
llama_memory_status status;
|
|
273
|
+
|
|
274
|
+
llama_kv_cache_unified * kv;
|
|
275
|
+
llama_context * lctx;
|
|
276
|
+
|
|
277
|
+
//
|
|
278
|
+
// update state
|
|
279
|
+
//
|
|
280
|
+
|
|
281
|
+
bool do_shift = false;
|
|
282
|
+
|
|
283
|
+
defrag_info dinfo;
|
|
284
|
+
|
|
285
|
+
//
|
|
286
|
+
// batch processing state
|
|
287
|
+
//
|
|
288
|
+
|
|
289
|
+
llama_sbatch sbatch;
|
|
290
|
+
|
|
291
|
+
// the index of the next ubatch to process
|
|
292
|
+
size_t i_next = 0;
|
|
293
|
+
|
|
294
|
+
ubatch_heads heads;
|
|
295
|
+
|
|
296
|
+
std::vector<llama_ubatch> ubatches;
|
|
297
|
+
|
|
298
|
+
//
|
|
299
|
+
// data needed for building the compute graph for the current ubatch:
|
|
300
|
+
//
|
|
301
|
+
|
|
302
|
+
// a heuristic, to avoid attending the full cache if it is not yet utilized
|
|
303
|
+
// as the cache gets filled, the benefit from this heuristic disappears
|
|
304
|
+
int32_t n_kv;
|
|
305
|
+
|
|
306
|
+
// the beginning of the current slot in which the ubatch will be inserted
|
|
307
|
+
int32_t head;
|
|
308
|
+
};
|
|
@@ -23,7 +23,7 @@ public:
|
|
|
23
23
|
|
|
24
24
|
used.clear();
|
|
25
25
|
|
|
26
|
-
for (uint32_t s = 0; s <
|
|
26
|
+
for (uint32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
|
|
27
27
|
seq_pos[s].clear();
|
|
28
28
|
}
|
|
29
29
|
}
|
|
@@ -68,12 +68,6 @@ public:
|
|
|
68
68
|
// the index of the last cell that is used + 1
|
|
69
69
|
// return 0 if no cells are used
|
|
70
70
|
uint32_t used_max_p1() const {
|
|
71
|
-
#if 0
|
|
72
|
-
if (!seq_pos[0].empty()) printf("kv_cells: min[0] = %5d, max[0] = %5d\n", *seq_pos[0].begin(), *seq_pos[0].rbegin());
|
|
73
|
-
if (!seq_pos[1].empty()) printf("kv_cells: min[1] = %5d, max[1] = %5d\n", *seq_pos[1].begin(), *seq_pos[1].rbegin());
|
|
74
|
-
if (!seq_pos[2].empty()) printf("kv_cells: min[2] = %5d, max[2] = %5d\n", *seq_pos[2].begin(), *seq_pos[2].rbegin());
|
|
75
|
-
#endif
|
|
76
|
-
|
|
77
71
|
return used.empty() ? 0 : *used.rbegin() + 1;
|
|
78
72
|
}
|
|
79
73
|
|
|
@@ -86,6 +80,9 @@ public:
|
|
|
86
80
|
assert(isrc < pos.size());
|
|
87
81
|
assert(idst < pos.size());
|
|
88
82
|
|
|
83
|
+
assert(pos[idst] == -1);
|
|
84
|
+
assert(pos[isrc] != -1);
|
|
85
|
+
|
|
89
86
|
pos [idst] = pos [isrc];
|
|
90
87
|
shift[idst] = shift[isrc];
|
|
91
88
|
seq [idst] = seq [isrc];
|
|
@@ -144,6 +141,20 @@ public:
|
|
|
144
141
|
}
|
|
145
142
|
}
|
|
146
143
|
|
|
144
|
+
// clear a non-empty cell
|
|
145
|
+
void rm(uint32_t i) {
|
|
146
|
+
assert(i < pos.size());
|
|
147
|
+
assert(pos[i] != -1);
|
|
148
|
+
|
|
149
|
+
seq_pos_rm(i);
|
|
150
|
+
seq[i].reset();
|
|
151
|
+
|
|
152
|
+
pos[i] = -1;
|
|
153
|
+
shift[i] = 0;
|
|
154
|
+
|
|
155
|
+
used.erase(i);
|
|
156
|
+
}
|
|
157
|
+
|
|
147
158
|
// note: call only if the cell has seq_id
|
|
148
159
|
// return true if the cell becomes empty
|
|
149
160
|
bool seq_rm(uint32_t i, llama_seq_id seq_id) {
|
|
@@ -157,6 +168,7 @@ public:
|
|
|
157
168
|
|
|
158
169
|
if (seq[i].none()) {
|
|
159
170
|
pos[i] = -1;
|
|
171
|
+
shift[i] = 0;
|
|
160
172
|
|
|
161
173
|
used.erase(i);
|
|
162
174
|
|
|
@@ -185,6 +197,7 @@ public:
|
|
|
185
197
|
seq[i].reset();
|
|
186
198
|
|
|
187
199
|
pos[i] = -1;
|
|
200
|
+
shift[i] = 0;
|
|
188
201
|
|
|
189
202
|
used.erase(i);
|
|
190
203
|
|
|
@@ -196,6 +209,15 @@ public:
|
|
|
196
209
|
return false;
|
|
197
210
|
}
|
|
198
211
|
|
|
212
|
+
// number of different sequences in the cell
|
|
213
|
+
int seq_count(uint32_t i) const {
|
|
214
|
+
assert(i < pos.size());
|
|
215
|
+
assert(pos[i] != -1);
|
|
216
|
+
|
|
217
|
+
return seq[i].count();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// check if the cell contains seq_id
|
|
199
221
|
bool seq_has(uint32_t i, llama_seq_id seq_id) const {
|
|
200
222
|
assert(i < pos.size());
|
|
201
223
|
assert(seq_id >= 0);
|
|
@@ -213,11 +235,25 @@ public:
|
|
|
213
235
|
seq_pos[seq_id].insert(pos[i]);
|
|
214
236
|
}
|
|
215
237
|
|
|
238
|
+
// return the sequence id of this cell
|
|
239
|
+
// note: call only for cells with exactly one sequence
|
|
240
|
+
llama_seq_id seq_get(uint32_t i) const {
|
|
241
|
+
assert(seq[i].count() == 1);
|
|
242
|
+
|
|
243
|
+
for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
|
|
244
|
+
if (seq[i].test(s)) {
|
|
245
|
+
return s;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return -1;
|
|
250
|
+
}
|
|
251
|
+
|
|
216
252
|
// the minimum position of sequence seq_id currently present in any of the cells
|
|
217
253
|
// return -1 if the sequence is not present
|
|
218
254
|
llama_pos seq_pos_min(llama_seq_id seq_id) const {
|
|
219
255
|
assert(seq_id >= 0);
|
|
220
|
-
assert(seq_id <
|
|
256
|
+
assert(seq_id < LLAMA_MAX_SEQ);
|
|
221
257
|
|
|
222
258
|
if (seq_pos[seq_id].empty()) {
|
|
223
259
|
return -1;
|
|
@@ -230,7 +266,7 @@ public:
|
|
|
230
266
|
// return -1 if the sequence is not present
|
|
231
267
|
llama_pos seq_pos_max(llama_seq_id seq_id) const {
|
|
232
268
|
assert(seq_id >= 0);
|
|
233
|
-
assert(seq_id <
|
|
269
|
+
assert(seq_id < LLAMA_MAX_SEQ);
|
|
234
270
|
|
|
235
271
|
if (seq_pos[seq_id].empty()) {
|
|
236
272
|
return -1;
|
|
@@ -268,6 +304,7 @@ public:
|
|
|
268
304
|
void pos_set(uint32_t i, llama_pos p) {
|
|
269
305
|
assert(i < pos.size());
|
|
270
306
|
assert(pos[i] == -1);
|
|
307
|
+
assert(seq[i].none());
|
|
271
308
|
|
|
272
309
|
pos[i] = p;
|
|
273
310
|
|
|
@@ -286,21 +323,20 @@ public:
|
|
|
286
323
|
pos[i] += d;
|
|
287
324
|
shift[i] += d;
|
|
288
325
|
|
|
289
|
-
seq_pos_add(i);
|
|
290
|
-
|
|
291
326
|
has_shift = true;
|
|
292
327
|
|
|
293
328
|
if (pos[i] < 0) {
|
|
294
|
-
seq_pos_rm(i);
|
|
295
|
-
|
|
296
329
|
seq[i].reset();
|
|
297
330
|
pos[i] = -1;
|
|
331
|
+
shift[i] = 0;
|
|
298
332
|
|
|
299
333
|
used.erase(i);
|
|
300
334
|
|
|
301
335
|
return true;
|
|
302
336
|
}
|
|
303
337
|
|
|
338
|
+
seq_pos_add(i);
|
|
339
|
+
|
|
304
340
|
return false;
|
|
305
341
|
}
|
|
306
342
|
|
|
@@ -348,20 +384,20 @@ private:
|
|
|
348
384
|
//
|
|
349
385
|
std::vector<llama_pos> shift;
|
|
350
386
|
|
|
351
|
-
using bits_t = std::bitset<
|
|
387
|
+
using bits_t = std::bitset<LLAMA_MAX_SEQ>;
|
|
352
388
|
|
|
353
389
|
// the bitset seq[i] tells us which sequences are currently occupying the i-th cell
|
|
354
390
|
std::vector<bits_t> seq;
|
|
355
391
|
|
|
356
392
|
// the set seq_pos[s] tells us which positions are currently present for sequence s
|
|
357
393
|
// this way seq_pos[s].begin() and seq_pos[s].rbegin() give us the min/max positions currently in the cache
|
|
358
|
-
std::set<llama_pos> seq_pos[
|
|
394
|
+
std::set<llama_pos> seq_pos[LLAMA_MAX_SEQ];
|
|
359
395
|
|
|
360
396
|
// helper functions for updating `seq_pos`, once cell at a time:
|
|
361
397
|
|
|
362
398
|
// remove cell i
|
|
363
399
|
void seq_pos_rm(uint32_t i) {
|
|
364
|
-
for (int s = 0; s <
|
|
400
|
+
for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
|
|
365
401
|
if (seq[i].test(s)) {
|
|
366
402
|
seq_pos[s].erase(pos[i]);
|
|
367
403
|
}
|
|
@@ -370,7 +406,7 @@ private:
|
|
|
370
406
|
|
|
371
407
|
// add cell i
|
|
372
408
|
void seq_pos_add(uint32_t i) {
|
|
373
|
-
for (int s = 0; s <
|
|
409
|
+
for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
|
|
374
410
|
if (seq[i].test(s)) {
|
|
375
411
|
seq_pos[s].insert(pos[i]);
|
|
376
412
|
}
|