whispercpp 1.3.1 → 1.3.3
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.
- checksums.yaml +4 -4
- data/.gitignore +7 -3
- data/README.md +161 -43
- data/Rakefile +45 -13
- data/ext/.gitignore +4 -8
- data/ext/dependencies.rb +73 -0
- data/ext/extconf.rb +21 -198
- data/ext/options.rb +85 -0
- data/ext/ruby_whisper.c +177 -0
- data/ext/ruby_whisper.h +17 -2
- data/ext/ruby_whisper_context.c +672 -0
- data/ext/ruby_whisper_error.c +52 -0
- data/ext/ruby_whisper_model.c +232 -0
- data/ext/ruby_whisper_params.c +1303 -0
- data/ext/ruby_whisper_segment.c +220 -0
- data/ext/ruby_whisper_transcribe.cpp +93 -0
- data/ext/ruby_whisper_vad_params.c +288 -0
- data/ext/sources/CMakeGraphVizOptions.cmake +8 -0
- data/ext/sources/CMakeLists.txt +255 -0
- data/ext/sources/bindings/javascript/CMakeLists.txt +41 -0
- data/ext/sources/bindings/javascript/emscripten.cpp +93 -0
- data/ext/sources/bindings/javascript/libwhisper.worker.js +1 -0
- data/ext/sources/bindings/javascript/package-tmpl.json +26 -0
- data/ext/sources/bindings/javascript/package.json +26 -0
- data/ext/sources/bindings/javascript/whisper.js +19 -0
- data/ext/sources/build-xcframework.sh +547 -0
- data/ext/sources/cmake/DefaultTargetOptions.cmake +16 -0
- data/ext/sources/cmake/FindFFmpeg.cmake +163 -0
- data/ext/sources/cmake/build-info.cmake +60 -0
- data/ext/sources/cmake/git-vars.cmake +22 -0
- data/ext/sources/cmake/whisper-config.cmake.in +65 -0
- data/ext/sources/cmake/whisper.pc.in +10 -0
- data/ext/sources/examples/CMakeLists.txt +124 -0
- data/ext/sources/examples/addon.node/CMakeLists.txt +31 -0
- data/ext/sources/examples/addon.node/__test__/whisper.spec.js +133 -0
- data/ext/sources/examples/addon.node/addon.cpp +557 -0
- data/ext/sources/examples/addon.node/index.js +57 -0
- data/ext/sources/examples/addon.node/package.json +16 -0
- data/ext/sources/examples/addon.node/vad-example.js +132 -0
- data/ext/sources/examples/bench/CMakeLists.txt +8 -0
- data/ext/sources/examples/bench/bench.cpp +176 -0
- data/ext/sources/examples/bench.wasm/CMakeLists.txt +49 -0
- data/ext/sources/examples/bench.wasm/emscripten.cpp +87 -0
- data/ext/sources/examples/bench.wasm/index-tmpl.html +284 -0
- data/ext/sources/examples/cli/CMakeLists.txt +8 -0
- data/ext/sources/examples/cli/cli.cpp +1295 -0
- data/ext/sources/examples/coi-serviceworker.js +146 -0
- data/ext/sources/examples/command/CMakeLists.txt +10 -0
- data/ext/sources/examples/command/command.cpp +800 -0
- data/ext/sources/examples/command/commands.txt +9 -0
- data/ext/sources/examples/command.wasm/CMakeLists.txt +50 -0
- data/ext/sources/examples/command.wasm/emscripten.cpp +327 -0
- data/ext/sources/examples/command.wasm/index-tmpl.html +414 -0
- data/ext/sources/examples/common-ggml.cpp +238 -0
- data/ext/sources/examples/common-ggml.h +18 -0
- data/ext/sources/examples/common-sdl.cpp +227 -0
- data/ext/sources/examples/common-sdl.h +49 -0
- data/ext/sources/examples/common-whisper.cpp +175 -0
- data/ext/sources/examples/common-whisper.h +24 -0
- data/ext/sources/examples/common.cpp +675 -0
- data/ext/sources/examples/common.h +322 -0
- data/ext/sources/examples/deprecation-warning/CMakeLists.txt +6 -0
- data/ext/sources/examples/deprecation-warning/deprecation-warning.cpp +38 -0
- data/ext/sources/examples/ffmpeg-transcode.cpp +368 -0
- data/ext/sources/examples/generate-karaoke.sh +57 -0
- data/ext/sources/examples/grammar-parser.cpp +423 -0
- data/ext/sources/examples/grammar-parser.h +29 -0
- data/ext/sources/examples/helpers.js +191 -0
- data/ext/sources/examples/json.hpp +24596 -0
- data/ext/sources/examples/livestream.sh +112 -0
- data/ext/sources/examples/lsp/CMakeLists.txt +9 -0
- data/ext/sources/examples/lsp/lsp.cpp +469 -0
- data/ext/sources/examples/lsp/whisper.vim +362 -0
- data/ext/sources/examples/miniaudio.h +93468 -0
- data/ext/sources/examples/python/test_whisper_processor.py +7 -0
- data/ext/sources/examples/python/whisper_processor.py +54 -0
- data/ext/sources/examples/quantize/CMakeLists.txt +6 -0
- data/ext/sources/examples/quantize/quantize.cpp +226 -0
- data/ext/sources/examples/server/CMakeLists.txt +15 -0
- data/ext/sources/examples/server/bench.js +29 -0
- data/ext/sources/examples/server/httplib.h +10497 -0
- data/ext/sources/examples/server/server.cpp +1238 -0
- data/ext/sources/examples/server.py +115 -0
- data/ext/sources/examples/stb_vorbis.c +5584 -0
- data/ext/sources/examples/stream/CMakeLists.txt +10 -0
- data/ext/sources/examples/stream/stream.cpp +435 -0
- data/ext/sources/examples/stream.wasm/CMakeLists.txt +49 -0
- data/ext/sources/examples/stream.wasm/emscripten.cpp +216 -0
- data/ext/sources/examples/stream.wasm/index-tmpl.html +414 -0
- data/ext/sources/examples/sycl/CMakeLists.txt +9 -0
- data/ext/sources/examples/sycl/build.sh +22 -0
- data/ext/sources/examples/sycl/ls-sycl-device.cpp +11 -0
- data/ext/sources/examples/sycl/run-whisper.sh +17 -0
- data/ext/sources/examples/talk-llama/CMakeLists.txt +43 -0
- data/ext/sources/examples/talk-llama/eleven-labs.py +80 -0
- data/ext/sources/examples/talk-llama/llama-adapter.cpp +388 -0
- data/ext/sources/examples/talk-llama/llama-adapter.h +76 -0
- data/ext/sources/examples/talk-llama/llama-arch.cpp +1914 -0
- data/ext/sources/examples/talk-llama/llama-arch.h +464 -0
- data/ext/sources/examples/talk-llama/llama-batch.cpp +843 -0
- data/ext/sources/examples/talk-llama/llama-batch.h +147 -0
- data/ext/sources/examples/talk-llama/llama-chat.cpp +685 -0
- data/ext/sources/examples/talk-llama/llama-chat.h +59 -0
- data/ext/sources/examples/talk-llama/llama-context.cpp +2845 -0
- data/ext/sources/examples/talk-llama/llama-context.h +297 -0
- data/ext/sources/examples/talk-llama/llama-cparams.cpp +5 -0
- data/ext/sources/examples/talk-llama/llama-cparams.h +41 -0
- data/ext/sources/examples/talk-llama/llama-grammar.cpp +1229 -0
- data/ext/sources/examples/talk-llama/llama-grammar.h +173 -0
- data/ext/sources/examples/talk-llama/llama-graph.cpp +1693 -0
- data/ext/sources/examples/talk-llama/llama-graph.h +710 -0
- data/ext/sources/examples/talk-llama/llama-hparams.cpp +103 -0
- data/ext/sources/examples/talk-llama/llama-hparams.h +207 -0
- data/ext/sources/examples/talk-llama/llama-impl.cpp +167 -0
- data/ext/sources/examples/talk-llama/llama-impl.h +61 -0
- data/ext/sources/examples/talk-llama/llama-io.cpp +15 -0
- data/ext/sources/examples/talk-llama/llama-io.h +35 -0
- data/ext/sources/examples/talk-llama/llama-kv-cache-unified-iswa.cpp +279 -0
- data/ext/sources/examples/talk-llama/llama-kv-cache-unified-iswa.h +128 -0
- data/ext/sources/examples/talk-llama/llama-kv-cache-unified.cpp +1841 -0
- data/ext/sources/examples/talk-llama/llama-kv-cache-unified.h +303 -0
- data/ext/sources/examples/talk-llama/llama-kv-cache.h +44 -0
- data/ext/sources/examples/talk-llama/llama-kv-cells.h +439 -0
- data/ext/sources/examples/talk-llama/llama-memory-hybrid.cpp +246 -0
- data/ext/sources/examples/talk-llama/llama-memory-hybrid.h +138 -0
- data/ext/sources/examples/talk-llama/llama-memory-recurrent.cpp +1125 -0
- data/ext/sources/examples/talk-llama/llama-memory-recurrent.h +183 -0
- data/ext/sources/examples/talk-llama/llama-memory.cpp +59 -0
- data/ext/sources/examples/talk-llama/llama-memory.h +116 -0
- data/ext/sources/examples/talk-llama/llama-mmap.cpp +600 -0
- data/ext/sources/examples/talk-llama/llama-mmap.h +68 -0
- data/ext/sources/examples/talk-llama/llama-model-loader.cpp +1163 -0
- data/ext/sources/examples/talk-llama/llama-model-loader.h +169 -0
- data/ext/sources/examples/talk-llama/llama-model-saver.cpp +282 -0
- data/ext/sources/examples/talk-llama/llama-model-saver.h +37 -0
- data/ext/sources/examples/talk-llama/llama-model.cpp +15114 -0
- data/ext/sources/examples/talk-llama/llama-model.h +452 -0
- data/ext/sources/examples/talk-llama/llama-quant.cpp +1049 -0
- data/ext/sources/examples/talk-llama/llama-quant.h +1 -0
- data/ext/sources/examples/talk-llama/llama-sampling.cpp +2575 -0
- data/ext/sources/examples/talk-llama/llama-sampling.h +32 -0
- data/ext/sources/examples/talk-llama/llama-vocab.cpp +3377 -0
- data/ext/sources/examples/talk-llama/llama-vocab.h +132 -0
- data/ext/sources/examples/talk-llama/llama.cpp +358 -0
- data/ext/sources/examples/talk-llama/llama.h +1484 -0
- data/ext/sources/examples/talk-llama/prompts/talk-alpaca.txt +23 -0
- data/ext/sources/examples/talk-llama/speak +40 -0
- data/ext/sources/examples/talk-llama/speak.bat +1 -0
- data/ext/sources/examples/talk-llama/speak.ps1 +14 -0
- data/ext/sources/examples/talk-llama/talk-llama.cpp +810 -0
- data/ext/sources/examples/talk-llama/unicode-data.cpp +7034 -0
- data/ext/sources/examples/talk-llama/unicode-data.h +20 -0
- data/ext/sources/examples/talk-llama/unicode.cpp +854 -0
- data/ext/sources/examples/talk-llama/unicode.h +66 -0
- data/ext/sources/examples/vad-speech-segments/CMakeLists.txt +8 -0
- data/ext/sources/examples/vad-speech-segments/speech.cpp +149 -0
- data/ext/sources/examples/wchess/CMakeLists.txt +10 -0
- data/ext/sources/examples/wchess/libwchess/CMakeLists.txt +19 -0
- data/ext/sources/examples/wchess/libwchess/Chessboard.cpp +803 -0
- data/ext/sources/examples/wchess/libwchess/Chessboard.h +33 -0
- data/ext/sources/examples/wchess/libwchess/WChess.cpp +193 -0
- data/ext/sources/examples/wchess/libwchess/WChess.h +63 -0
- data/ext/sources/examples/wchess/libwchess/test-chessboard.cpp +117 -0
- data/ext/sources/examples/wchess/wchess.cmd/CMakeLists.txt +8 -0
- data/ext/sources/examples/wchess/wchess.cmd/wchess.cmd.cpp +251 -0
- data/ext/sources/examples/whisper.wasm/CMakeLists.txt +50 -0
- data/ext/sources/examples/whisper.wasm/emscripten.cpp +118 -0
- data/ext/sources/examples/whisper.wasm/index-tmpl.html +658 -0
- data/ext/sources/ggml/CMakeLists.txt +435 -0
- data/ext/sources/ggml/cmake/BuildTypes.cmake +54 -0
- data/ext/sources/ggml/cmake/GitVars.cmake +22 -0
- data/ext/sources/ggml/cmake/common.cmake +50 -0
- data/ext/sources/ggml/cmake/ggml-config.cmake.in +152 -0
- data/ext/{ggml → sources/ggml}/include/ggml-alloc.h +1 -1
- data/ext/{ggml → sources/ggml}/include/ggml-backend.h +10 -8
- data/ext/{ggml → sources/ggml}/include/ggml-cpp.h +2 -1
- data/ext/{ggml → sources/ggml}/include/ggml-cpu.h +11 -1
- data/ext/{ggml → sources/ggml}/include/ggml-metal.h +1 -1
- data/ext/{ggml → sources/ggml}/include/ggml-opt.h +49 -28
- data/ext/{ggml → sources/ggml}/include/ggml-rpc.h +6 -1
- data/ext/{ggml → sources/ggml}/include/ggml-vulkan.h +0 -2
- data/ext/{ggml → sources/ggml}/include/ggml.h +325 -269
- data/ext/sources/ggml/include/gguf.h +202 -0
- data/ext/sources/ggml/src/CMakeLists.txt +404 -0
- data/ext/{ggml → sources/ggml}/src/ggml-alloc.c +34 -29
- data/ext/sources/ggml/src/ggml-amx/CMakeLists.txt +107 -0
- data/ext/{ggml → sources/ggml}/src/ggml-backend-impl.h +1 -2
- data/ext/{ggml → sources/ggml}/src/ggml-backend-reg.cpp +92 -53
- data/ext/{ggml → sources/ggml}/src/ggml-backend.cpp +69 -34
- data/ext/sources/ggml/src/ggml-blas/CMakeLists.txt +87 -0
- data/ext/sources/ggml/src/ggml-cann/CMakeLists.txt +75 -0
- data/ext/sources/ggml/src/ggml-cann/Doxyfile +2579 -0
- data/ext/{ggml → sources/ggml}/src/ggml-cann/acl_tensor.cpp +10 -4
- data/ext/{ggml → sources/ggml}/src/ggml-cann/acl_tensor.h +5 -5
- data/ext/{ggml → sources/ggml}/src/ggml-cann/aclnn_ops.cpp +1272 -1506
- data/ext/sources/ggml/src/ggml-cann/aclnn_ops.h +1125 -0
- data/ext/{ggml → sources/ggml}/src/ggml-cann/common.h +140 -1
- data/ext/{ggml → sources/ggml}/src/ggml-cann/ggml-cann.cpp +588 -146
- data/ext/sources/ggml/src/ggml-cann/kernels/CMakeLists.txt +30 -0
- data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/dup.cpp +3 -5
- data/ext/{ggml → sources/ggml}/src/ggml-common.h +16 -8
- data/ext/sources/ggml/src/ggml-cpu/CMakeLists.txt +597 -0
- data/ext/{ggml → sources/ggml}/src/ggml-cpu/amx/amx.cpp +3 -2
- data/ext/{ggml → sources/ggml}/src/ggml-cpu/amx/mmq.cpp +11 -10
- data/ext/sources/ggml/src/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/arm/quants.c +4114 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/arm/repack.cpp +2163 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/loongarch/quants.c +2639 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/powerpc/cpu-feats.cpp +82 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/powerpc/quants.c +2732 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/riscv/quants.c +2069 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/riscv/repack.cpp +397 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/s390/quants.c +1300 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/wasm/quants.c +1481 -0
- data/ext/{ggml/src/ggml-cpu/cpu-feats-x86.cpp → sources/ggml/src/ggml-cpu/arch/x86/cpu-feats.cpp} +5 -1
- data/ext/sources/ggml/src/ggml-cpu/arch/x86/quants.c +4311 -0
- data/ext/sources/ggml/src/ggml-cpu/arch/x86/repack.cpp +3285 -0
- data/ext/sources/ggml/src/ggml-cpu/arch-fallback.h +184 -0
- data/ext/sources/ggml/src/ggml-cpu/binary-ops.cpp +158 -0
- data/ext/sources/ggml/src/ggml-cpu/binary-ops.h +16 -0
- data/ext/sources/ggml/src/ggml-cpu/cmake/FindSIMD.cmake +100 -0
- data/ext/sources/ggml/src/ggml-cpu/common.h +73 -0
- data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu-impl.h +172 -41
- data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.c +3551 -0
- data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu.cpp +78 -25
- data/ext/{ggml/src/ggml-cpu/ggml-cpu-hbm.cpp → sources/ggml/src/ggml-cpu/hbm.cpp} +1 -1
- data/ext/sources/ggml/src/ggml-cpu/kleidiai/kernels.cpp +337 -0
- data/ext/sources/ggml/src/ggml-cpu/kleidiai/kernels.h +95 -0
- data/ext/sources/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +482 -0
- data/ext/sources/ggml/src/ggml-cpu/kleidiai/kleidiai.h +17 -0
- data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.cpp +3594 -0
- data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.h +19 -0
- data/ext/sources/ggml/src/ggml-cpu/ops.cpp +9786 -0
- data/ext/sources/ggml/src/ggml-cpu/ops.h +118 -0
- data/ext/sources/ggml/src/ggml-cpu/quants.c +1158 -0
- data/ext/{ggml/src/ggml-cpu/ggml-cpu-quants.h → sources/ggml/src/ggml-cpu/quants.h} +26 -0
- data/ext/sources/ggml/src/ggml-cpu/repack.cpp +1571 -0
- data/ext/sources/ggml/src/ggml-cpu/repack.h +98 -0
- data/ext/sources/ggml/src/ggml-cpu/simd-mappings.h +1184 -0
- data/ext/{ggml/src/ggml-cpu/ggml-cpu-traits.cpp → sources/ggml/src/ggml-cpu/traits.cpp} +1 -1
- data/ext/sources/ggml/src/ggml-cpu/unary-ops.cpp +186 -0
- data/ext/sources/ggml/src/ggml-cpu/unary-ops.h +28 -0
- data/ext/sources/ggml/src/ggml-cpu/vec.cpp +345 -0
- data/ext/sources/ggml/src/ggml-cpu/vec.h +1027 -0
- data/ext/sources/ggml/src/ggml-cuda/CMakeLists.txt +184 -0
- data/ext/sources/ggml/src/ggml-cuda/acc.cu +61 -0
- data/ext/sources/ggml/src/ggml-cuda/acc.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/arange.cu +34 -0
- data/ext/sources/ggml/src/ggml-cuda/arange.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/argmax.cu +91 -0
- data/ext/sources/ggml/src/ggml-cuda/argmax.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/argsort.cu +104 -0
- data/ext/sources/ggml/src/ggml-cuda/argsort.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/binbcast.cu +363 -0
- data/ext/sources/ggml/src/ggml-cuda/binbcast.cuh +9 -0
- data/ext/sources/ggml/src/ggml-cuda/clamp.cu +45 -0
- data/ext/sources/ggml/src/ggml-cuda/clamp.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/common.cuh +851 -0
- data/ext/sources/ggml/src/ggml-cuda/concat.cu +221 -0
- data/ext/sources/ggml/src/ggml-cuda/concat.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/conv-transpose-1d.cu +89 -0
- data/ext/sources/ggml/src/ggml-cuda/conv-transpose-1d.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/conv2d-dw.cu +161 -0
- data/ext/sources/ggml/src/ggml-cuda/conv2d-dw.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/conv2d-transpose.cu +91 -0
- data/ext/sources/ggml/src/ggml-cuda/conv2d-transpose.cuh +4 -0
- data/ext/sources/ggml/src/ggml-cuda/convert.cu +752 -0
- data/ext/sources/ggml/src/ggml-cuda/convert.cuh +31 -0
- data/ext/sources/ggml/src/ggml-cuda/count-equal.cu +64 -0
- data/ext/sources/ggml/src/ggml-cuda/count-equal.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/cp-async.cuh +57 -0
- data/ext/sources/ggml/src/ggml-cuda/cpy.cu +705 -0
- data/ext/sources/ggml/src/ggml-cuda/cpy.cuh +11 -0
- data/ext/sources/ggml/src/ggml-cuda/cross-entropy-loss.cu +189 -0
- data/ext/sources/ggml/src/ggml-cuda/cross-entropy-loss.cuh +7 -0
- data/ext/sources/ggml/src/ggml-cuda/dequantize.cuh +103 -0
- data/ext/sources/ggml/src/ggml-cuda/diagmask.cu +40 -0
- data/ext/sources/ggml/src/ggml-cuda/diagmask.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-common.cuh +881 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-mma-f16.cuh +1474 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f16.cu +357 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f16.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f32.cu +365 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f32.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-vec-f16.cuh +482 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-vec-f32.cuh +472 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-wmma-f16.cu +638 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn-wmma-f16.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn.cu +346 -0
- data/ext/sources/ggml/src/ggml-cuda/fattn.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/getrows.cu +275 -0
- data/ext/sources/ggml/src/ggml-cuda/getrows.cuh +15 -0
- data/ext/sources/ggml/src/ggml-cuda/ggml-cuda.cu +3647 -0
- data/ext/sources/ggml/src/ggml-cuda/gla.cu +93 -0
- data/ext/sources/ggml/src/ggml-cuda/gla.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/im2col.cu +103 -0
- data/ext/sources/ggml/src/ggml-cuda/im2col.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/mean.cu +19 -0
- data/ext/sources/ggml/src/ggml-cuda/mean.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/mma.cuh +396 -0
- data/ext/sources/ggml/src/ggml-cuda/mmq.cu +324 -0
- data/ext/sources/ggml/src/ggml-cuda/mmq.cuh +3217 -0
- data/ext/sources/ggml/src/ggml-cuda/mmv.cu +506 -0
- data/ext/sources/ggml/src/ggml-cuda/mmv.cuh +11 -0
- data/ext/sources/ggml/src/ggml-cuda/mmvq.cu +595 -0
- data/ext/sources/ggml/src/ggml-cuda/mmvq.cuh +12 -0
- data/ext/sources/ggml/src/ggml-cuda/norm.cu +458 -0
- data/ext/sources/ggml/src/ggml-cuda/norm.cuh +11 -0
- data/ext/sources/ggml/src/ggml-cuda/opt-step-adamw.cu +78 -0
- data/ext/sources/ggml/src/ggml-cuda/opt-step-adamw.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/out-prod.cu +68 -0
- data/ext/sources/ggml/src/ggml-cuda/out-prod.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/pad.cu +49 -0
- data/ext/sources/ggml/src/ggml-cuda/pad.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/pool2d.cu +94 -0
- data/ext/sources/ggml/src/ggml-cuda/pool2d.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/quantize.cu +190 -0
- data/ext/sources/ggml/src/ggml-cuda/quantize.cuh +27 -0
- data/ext/sources/ggml/src/ggml-cuda/rope.cu +456 -0
- data/ext/sources/ggml/src/ggml-cuda/rope.cuh +7 -0
- data/ext/sources/ggml/src/ggml-cuda/scale.cu +31 -0
- data/ext/sources/ggml/src/ggml-cuda/scale.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/softmax.cu +283 -0
- data/ext/sources/ggml/src/ggml-cuda/softmax.cuh +7 -0
- data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cu +148 -0
- data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/ssm-scan.cu +155 -0
- data/ext/sources/ggml/src/ggml-cuda/ssm-scan.cuh +3 -0
- data/ext/sources/ggml/src/ggml-cuda/sum.cu +45 -0
- data/ext/sources/ggml/src/ggml-cuda/sum.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/sumrows.cu +26 -0
- data/ext/sources/ggml/src/ggml-cuda/sumrows.cuh +4 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_8.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_1.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_2.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_4.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_4.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_8.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_1.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_2.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_2.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_4.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_8.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_64-ncols2_1.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_1.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_2.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_4.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_8.cu +10 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs256-f16-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs256-f16-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-f16.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/generate_cu_files.py +78 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq1_s.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_s.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_xs.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_xxs.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq3_s.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq3_xxs.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq4_nl.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq4_xs.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q2_k.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q3_k.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_k.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_1.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_k.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q6_k.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q8_0.cu +5 -0
- data/ext/sources/ggml/src/ggml-cuda/tsembd.cu +47 -0
- data/ext/sources/ggml/src/ggml-cuda/tsembd.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/unary.cu +378 -0
- data/ext/sources/ggml/src/ggml-cuda/unary.cuh +66 -0
- data/ext/sources/ggml/src/ggml-cuda/upscale.cu +51 -0
- data/ext/sources/ggml/src/ggml-cuda/upscale.cuh +5 -0
- data/ext/sources/ggml/src/ggml-cuda/vecdotq.cuh +1135 -0
- data/ext/{ggml → sources/ggml}/src/ggml-cuda/vendors/cuda.h +1 -0
- data/ext/{ggml → sources/ggml}/src/ggml-cuda/vendors/hip.h +57 -0
- data/ext/{ggml → sources/ggml}/src/ggml-cuda/vendors/musa.h +7 -1
- data/ext/sources/ggml/src/ggml-cuda/wkv.cu +199 -0
- data/ext/sources/ggml/src/ggml-cuda/wkv.cuh +7 -0
- data/ext/sources/ggml/src/ggml-hip/CMakeLists.txt +135 -0
- data/ext/{ggml → sources/ggml}/src/ggml-impl.h +147 -158
- data/ext/sources/ggml/src/ggml-kompute/CMakeLists.txt +166 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/common.comp +112 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_add.comp +58 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_addrow.comp +25 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f16.comp +52 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f32.comp +52 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f16.comp +52 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f32.comp +52 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_diagmask.comp +30 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_gelu.comp +22 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows.comp +17 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f16.comp +31 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f32.comp +31 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_0.comp +38 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_1.comp +39 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q6_k.comp +44 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul.comp +52 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_f16.comp +69 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_mat_f32.comp +51 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_0.comp +33 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_1.comp +35 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_k.comp +140 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q6_k.comp +106 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q8_0.comp +73 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n.comp +52 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n_pre.comp +28 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_norm.comp +84 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_relu.comp +21 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rmsnorm.comp +53 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f16.comp +52 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f32.comp +52 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f16.comp +52 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f32.comp +52 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_scale.comp +19 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_scale_8.comp +23 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_silu.comp +22 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_softmax.comp +72 -0
- data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/rope_common.comp +71 -0
- data/ext/sources/ggml/src/ggml-metal/CMakeLists.txt +121 -0
- data/ext/sources/ggml/src/ggml-metal/ggml-metal-impl.h +649 -0
- data/ext/{ggml → sources/ggml}/src/ggml-metal/ggml-metal.m +2504 -1108
- data/ext/{ggml → sources/ggml}/src/ggml-metal/ggml-metal.metal +2102 -1463
- data/ext/sources/ggml/src/ggml-musa/CMakeLists.txt +113 -0
- data/ext/sources/ggml/src/ggml-musa/mudnn.cu +112 -0
- data/ext/sources/ggml/src/ggml-musa/mudnn.cuh +12 -0
- data/ext/sources/ggml/src/ggml-opencl/CMakeLists.txt +110 -0
- data/ext/sources/ggml/src/ggml-opencl/ggml-opencl.cpp +6494 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/add.cl +83 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/argsort.cl +86 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/clamp.cl +20 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/concat.cl +109 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/cpy.cl +184 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/cvt.cl +118 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/diag_mask_inf.cl +58 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/div.cl +72 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/embed_kernel.py +26 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/gelu.cl +62 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle.cl +268 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_general.cl +274 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/get_rows.cl +163 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/glu.cl +201 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/group_norm.cl +72 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/im2col_f16.cl +57 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/im2col_f32.cl +57 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul.cl +79 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mat_Ab_Bi_8x4.cl +139 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f16_f16.cl +118 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32.cl +118 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_1row.cl +94 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_l4.cl +84 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f32_f32.cl +118 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_id_q4_0_f32_8x_flat.cl +283 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32.cl +192 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_1d_16x_flat.cl +307 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_1d_8x_flat.cl +265 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_8x_flat.cl +272 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_v.cl +254 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q6_k.cl +190 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/norm.cl +81 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/pad.cl +30 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/relu.cl +16 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/repeat.cl +39 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/rms_norm.cl +96 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/rope.cl +721 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/scale.cl +16 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/sigmoid.cl +29 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/silu.cl +30 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_4_f16.cl +87 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_4_f32.cl +87 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_f16.cl +86 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_f32.cl +86 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/sub.cl +72 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/sum_rows.cl +39 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/tanh.cl +63 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/transpose.cl +84 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/tsembd.cl +48 -0
- data/ext/sources/ggml/src/ggml-opencl/kernels/upscale.cl +121 -0
- data/ext/{ggml → sources/ggml}/src/ggml-opt.cpp +373 -190
- data/ext/{ggml → sources/ggml}/src/ggml-quants.c +120 -128
- data/ext/sources/ggml/src/ggml-rpc/CMakeLists.txt +9 -0
- data/ext/{ggml → sources/ggml}/src/ggml-rpc/ggml-rpc.cpp +494 -84
- data/ext/sources/ggml/src/ggml-sycl/CMakeLists.txt +189 -0
- data/ext/sources/ggml/src/ggml-sycl/backend.hpp +37 -0
- data/ext/sources/ggml/src/ggml-sycl/binbcast.cpp +344 -0
- data/ext/sources/ggml/src/ggml-sycl/binbcast.hpp +39 -0
- data/ext/{ggml → sources/ggml}/src/ggml-sycl/common.cpp +20 -32
- data/ext/sources/ggml/src/ggml-sycl/common.hpp +561 -0
- data/ext/{ggml → sources/ggml}/src/ggml-sycl/concat.cpp +56 -70
- data/ext/sources/ggml/src/ggml-sycl/concat.hpp +20 -0
- data/ext/{ggml → sources/ggml}/src/ggml-sycl/conv.cpp +8 -12
- data/ext/sources/ggml/src/ggml-sycl/conv.hpp +20 -0
- data/ext/sources/ggml/src/ggml-sycl/convert.cpp +575 -0
- data/ext/sources/ggml/src/ggml-sycl/convert.hpp +34 -0
- data/ext/sources/ggml/src/ggml-sycl/cpy.cpp +839 -0
- data/ext/sources/ggml/src/ggml-sycl/cpy.hpp +11 -0
- data/ext/sources/ggml/src/ggml-sycl/dequantize.hpp +823 -0
- data/ext/{ggml → sources/ggml}/src/ggml-sycl/dmmv.cpp +188 -67
- data/ext/sources/ggml/src/ggml-sycl/dmmv.hpp +27 -0
- data/ext/sources/ggml/src/ggml-sycl/dpct/helper.hpp +2987 -0
- data/ext/sources/ggml/src/ggml-sycl/element_wise.cpp +1120 -0
- data/ext/sources/ggml/src/ggml-sycl/element_wise.hpp +84 -0
- data/ext/sources/ggml/src/ggml-sycl/gemm.hpp +102 -0
- data/ext/sources/ggml/src/ggml-sycl/getrows.cpp +212 -0
- data/ext/sources/ggml/src/ggml-sycl/getrows.hpp +20 -0
- data/ext/{ggml → sources/ggml}/src/ggml-sycl/ggml-sycl.cpp +1197 -1295
- data/ext/sources/ggml/src/ggml-sycl/gla.cpp +106 -0
- data/ext/sources/ggml/src/ggml-sycl/gla.hpp +8 -0
- data/ext/sources/ggml/src/ggml-sycl/im2col.cpp +136 -0
- data/ext/sources/ggml/src/ggml-sycl/im2col.hpp +21 -0
- data/ext/{ggml → sources/ggml}/src/ggml-sycl/mmq.cpp +60 -81
- data/ext/sources/ggml/src/ggml-sycl/mmq.hpp +33 -0
- data/ext/sources/ggml/src/ggml-sycl/mmvq.cpp +1065 -0
- data/ext/sources/ggml/src/ggml-sycl/mmvq.hpp +27 -0
- data/ext/sources/ggml/src/ggml-sycl/norm.cpp +482 -0
- data/ext/sources/ggml/src/ggml-sycl/norm.hpp +26 -0
- data/ext/{ggml → sources/ggml}/src/ggml-sycl/outprod.cpp +8 -17
- data/ext/sources/ggml/src/ggml-sycl/outprod.hpp +10 -0
- data/ext/sources/ggml/src/ggml-sycl/presets.hpp +74 -0
- data/ext/sources/ggml/src/ggml-sycl/quants.hpp +111 -0
- data/ext/sources/ggml/src/ggml-sycl/rope.cpp +472 -0
- data/ext/sources/ggml/src/ggml-sycl/rope.hpp +20 -0
- data/ext/{ggml → sources/ggml}/src/ggml-sycl/softmax.cpp +38 -28
- data/ext/sources/ggml/src/ggml-sycl/softmax.hpp +20 -0
- data/ext/sources/ggml/src/ggml-sycl/sycl_hw.cpp +15 -0
- data/ext/sources/ggml/src/ggml-sycl/sycl_hw.hpp +26 -0
- data/ext/{ggml → sources/ggml}/src/ggml-sycl/tsembd.cpp +6 -11
- data/ext/sources/ggml/src/ggml-sycl/tsembd.hpp +20 -0
- data/ext/sources/ggml/src/ggml-sycl/vecdotq.hpp +1307 -0
- data/ext/sources/ggml/src/ggml-sycl/wkv.cpp +289 -0
- data/ext/sources/ggml/src/ggml-sycl/wkv.hpp +10 -0
- data/ext/sources/ggml/src/ggml-vulkan/CMakeLists.txt +200 -0
- data/ext/sources/ggml/src/ggml-vulkan/cmake/host-toolchain.cmake.in +15 -0
- data/ext/{ggml → sources/ggml}/src/ggml-vulkan/ggml-vulkan.cpp +3822 -1335
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +31 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/acc.comp +29 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/add.comp +29 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/argmax.comp +51 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/argsort.comp +69 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/clamp.comp +17 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/concat.comp +41 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/contig_copy.comp +49 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_dw.comp +105 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/conv_transpose_1d.comp +98 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy.comp +23 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_from_quant.comp +51 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_to_quant.comp +242 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/cos.comp +17 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/count_equal.comp +31 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_f32.comp +20 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs.comp +462 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs_cm2.comp +699 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_head.comp +13 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq1_m.comp +42 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq1_s.comp +35 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_s.comp +44 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_xs.comp +43 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_xxs.comp +48 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_s.comp +39 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_xxs.comp +49 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq4_nl.comp +32 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq4_xs.comp +34 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q2_k.comp +34 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q3_k.comp +42 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_0.comp +30 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_1.comp +32 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_k.comp +68 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_0.comp +34 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_1.comp +35 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_k.comp +70 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q6_k.comp +33 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q8_0.comp +31 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/diag_mask_inf.comp +34 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/div.comp +27 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +337 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.comp +162 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp +360 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +267 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_split_k_reduce.comp +59 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/geglu.comp +13 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gelu.comp +25 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gelu_quick.comp +23 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_binary_head.comp +64 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_head.comp +9 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_unary_head.comp +76 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/get_rows.comp +33 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/get_rows_quant.comp +41 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_head.comp +15 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_main.comp +29 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/group_norm.comp +66 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col.comp +100 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/l2_norm.comp +41 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/leaky_relu.comp +22 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul.comp +27 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_split_k_reduce.comp +48 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec.comp +169 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_base.comp +118 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq1_m.comp +82 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq1_s.comp +79 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq2_s.comp +90 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq2_xs.comp +87 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq2_xxs.comp +87 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq3_s.comp +90 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq3_xxs.comp +88 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_nc.comp +118 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_p021.comp +154 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q2_k.comp +130 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q3_k.comp +132 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q4_k.comp +136 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q5_k.comp +167 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q6_k.comp +130 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +868 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_cm2.comp +441 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq.comp +442 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_funcs.comp +99 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/norm.comp +44 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/opt_step_adamw.comp +42 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/pad.comp +28 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/pool2d.comp +74 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/quantize_q8_1.comp +77 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/reglu.comp +9 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/relu.comp +21 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/repeat.comp +26 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/repeat_back.comp +37 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm.comp +61 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm_back.comp +55 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_head.comp +58 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_multi.comp +60 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_neox.comp +43 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_norm.comp +43 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_vision.comp +47 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/scale.comp +24 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sigmoid.comp +20 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/silu.comp +22 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/silu_back.comp +26 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sin.comp +17 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/soft_max.comp +173 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/soft_max_back.comp +50 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/square.comp +17 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sub.comp +29 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sum_rows.comp +37 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/swiglu.comp +9 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/tanh.comp +20 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/test_bfloat16_support.comp +7 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/test_coopmat2_support.comp +7 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/test_coopmat_support.comp +7 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/test_integer_dot_support.comp +7 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/timestep_embedding.comp +41 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/types.comp +1373 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/upscale.comp +36 -0
- data/ext/{ggml → sources/ggml}/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +203 -36
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/wkv6.comp +87 -0
- data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/wkv7.comp +91 -0
- data/ext/{ggml → sources/ggml}/src/ggml.c +918 -1782
- data/ext/sources/ggml/src/ggml.cpp +26 -0
- data/ext/sources/ggml/src/gguf.cpp +1351 -0
- data/ext/{include → sources/include}/whisper.h +70 -2
- data/ext/sources/src/CMakeLists.txt +145 -0
- data/ext/sources/src/coreml/whisper-compat.h +10 -0
- data/ext/sources/src/coreml/whisper-compat.m +35 -0
- data/ext/{src → sources/src}/coreml/whisper-decoder-impl.h +27 -15
- data/ext/{src → sources/src}/coreml/whisper-decoder-impl.m +36 -10
- data/ext/{src → sources/src}/coreml/whisper-encoder-impl.h +21 -9
- data/ext/{src → sources/src}/coreml/whisper-encoder-impl.m +29 -3
- data/ext/sources/src/coreml/whisper-encoder.mm +73 -0
- data/ext/sources/src/whisper-arch.h +197 -0
- data/ext/{src → sources/src}/whisper.cpp +1966 -386
- data/ext/sources/tests/CMakeLists.txt +105 -0
- data/ext/sources/tests/earnings21/eval.mk +58 -0
- data/ext/sources/tests/earnings21/eval.py +68 -0
- data/ext/sources/tests/earnings21/normalizers/__init__.py +2 -0
- data/ext/sources/tests/earnings21/normalizers/basic.py +80 -0
- data/ext/sources/tests/earnings21/normalizers/english.json +1741 -0
- data/ext/sources/tests/earnings21/normalizers/english.py +550 -0
- data/ext/sources/tests/earnings21/requirements.txt +6 -0
- data/ext/sources/tests/en-0-ref.txt +1 -0
- data/ext/sources/tests/en-1-ref.txt +1 -0
- data/ext/sources/tests/en-2-ref.txt +1 -0
- data/ext/sources/tests/es-0-ref.txt +1 -0
- data/ext/sources/tests/librispeech/eval.mk +39 -0
- data/ext/sources/tests/librispeech/eval.py +47 -0
- data/ext/sources/tests/librispeech/normalizers/__init__.py +2 -0
- data/ext/sources/tests/librispeech/normalizers/basic.py +80 -0
- data/ext/sources/tests/librispeech/normalizers/english.json +1741 -0
- data/ext/sources/tests/librispeech/normalizers/english.py +550 -0
- data/ext/sources/tests/librispeech/requirements.txt +6 -0
- data/ext/sources/tests/run-tests.sh +130 -0
- data/ext/sources/tests/test-c.c +3 -0
- data/ext/sources/tests/test-vad-full.cpp +54 -0
- data/ext/sources/tests/test-vad.cpp +83 -0
- data/ext/sources/tests/test-whisper.js +58 -0
- data/extsources.rb +39 -5
- data/lib/whisper/context.rb +15 -0
- data/lib/whisper/model/uri.rb +202 -126
- data/lib/whisper/segment.rb +58 -0
- data/sig/whisper.rbs +510 -0
- data/test/helper.rb +24 -0
- data/{tests → test}/test_callback.rb +45 -3
- data/{tests → test}/test_error.rb +2 -2
- data/{tests → test}/test_model.rb +47 -0
- data/test/test_package.rb +51 -0
- data/test/test_params.rb +297 -0
- data/test/test_segment.rb +146 -0
- data/test/test_vad.rb +19 -0
- data/test/test_vad_params.rb +103 -0
- data/{tests → test}/test_whisper.rb +106 -36
- data/whispercpp.gemspec +5 -5
- metadata +837 -134
- data/ext/cpu.mk +0 -9
- data/ext/examples/dr_wav.h +0 -8815
- data/ext/ggml/src/ggml-cann/aclnn_ops.h +0 -592
- data/ext/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +0 -4262
- data/ext/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +0 -8
- data/ext/ggml/src/ggml-cpu/ggml-cpu-quants.c +0 -10835
- data/ext/ggml/src/ggml-cpu/ggml-cpu.c +0 -14123
- data/ext/ggml/src/ggml-cpu/llamafile/sgemm.cpp +0 -1884
- data/ext/ggml/src/ggml-cpu/llamafile/sgemm.h +0 -14
- data/ext/ggml/src/ggml-metal/ggml-metal-impl.h +0 -288
- data/ext/ggml/src/ggml-sycl/convert.cpp +0 -547
- data/ext/ggml/src/ggml-sycl/element_wise.cpp +0 -1030
- data/ext/ggml/src/ggml-sycl/im2col.cpp +0 -126
- data/ext/ggml/src/ggml-sycl/mmvq.cpp +0 -1015
- data/ext/ggml/src/ggml-sycl/norm.cpp +0 -378
- data/ext/ggml/src/ggml-sycl/rope.cpp +0 -276
- data/ext/ggml/src/ggml-sycl/wkv6.cpp +0 -141
- data/ext/metal-embed.mk +0 -17
- data/ext/metal.mk +0 -6
- data/ext/ruby_whisper.cpp +0 -1909
- data/ext/scripts/get-flags.mk +0 -38
- data/lib/whisper.rb +0 -2
- data/tests/helper.rb +0 -7
- data/tests/test_package.rb +0 -31
- data/tests/test_params.rb +0 -160
- data/tests/test_segment.rb +0 -83
- /data/ext/{ggml → sources/ggml}/include/ggml-blas.h +0 -0
- /data/ext/{ggml → sources/ggml}/include/ggml-cann.h +0 -0
- /data/ext/{ggml → sources/ggml}/include/ggml-cuda.h +0 -0
- /data/ext/{ggml → sources/ggml}/include/ggml-kompute.h +0 -0
- /data/ext/{ggml → sources/ggml}/include/ggml-opencl.h +0 -0
- /data/ext/{ggml → sources/ggml}/include/ggml-sycl.h +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-amx/common.h +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-amx/ggml-amx.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-amx/mmq.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-amx/mmq.h +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-blas/ggml-blas.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/ascendc_kernels.h +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/get_row_f16.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/get_row_f32.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/get_row_q4_0.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/get_row_q8_0.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cpu/amx/amx.h +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cpu/amx/common.h +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-cpu/amx/mmq.h +0 -0
- /data/ext/{ggml/src/ggml-cpu/ggml-cpu-hbm.h → sources/ggml/src/ggml-cpu/hbm.h} +0 -0
- /data/ext/{ggml/src/ggml-cpu/ggml-cpu-traits.h → sources/ggml/src/ggml-cpu/traits.h} +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-kompute/ggml-kompute.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-quants.h +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-threading.cpp +0 -0
- /data/ext/{ggml → sources/ggml}/src/ggml-threading.h +0 -0
- /data/ext/{src → sources/src}/coreml/whisper-encoder.h +0 -0
- /data/ext/{src → sources/src}/openvino/whisper-openvino-encoder.cpp +0 -0
- /data/ext/{src → sources/src}/openvino/whisper-openvino-encoder.h +0 -0
- /data/{tests → test}/jfk_reader/.gitignore +0 -0
- /data/{tests → test}/jfk_reader/extconf.rb +0 -0
- /data/{tests → test}/jfk_reader/jfk_reader.c +0 -0
@@ -0,0 +1,2845 @@
|
|
1
|
+
#include "llama-context.h"
|
2
|
+
|
3
|
+
#include "llama-impl.h"
|
4
|
+
#include "llama-batch.h"
|
5
|
+
#include "llama-io.h"
|
6
|
+
#include "llama-memory.h"
|
7
|
+
#include "llama-mmap.h"
|
8
|
+
#include "llama-model.h"
|
9
|
+
|
10
|
+
#include <cinttypes>
|
11
|
+
#include <cstring>
|
12
|
+
#include <limits>
|
13
|
+
#include <stdexcept>
|
14
|
+
|
15
|
+
//
|
16
|
+
// llama_context
|
17
|
+
//
|
18
|
+
|
19
|
+
llama_context::llama_context(
|
20
|
+
const llama_model & model,
|
21
|
+
llama_context_params params) :
|
22
|
+
model(model),
|
23
|
+
balloc(std::make_unique<llama_batch_allocr>(model.hparams.n_pos_per_embd())) {
|
24
|
+
LLAMA_LOG_INFO("%s: constructing llama_context\n", __func__);
|
25
|
+
|
26
|
+
t_start_us = model.t_start_us;
|
27
|
+
t_load_us = model.t_load_us;
|
28
|
+
|
29
|
+
const auto & hparams = model.hparams;
|
30
|
+
|
31
|
+
cparams.n_seq_max = std::max(1u, params.n_seq_max);
|
32
|
+
if (cparams.n_seq_max > LLAMA_MAX_SEQ) {
|
33
|
+
throw std::runtime_error("n_seq_max must be <= " + std::to_string(LLAMA_MAX_SEQ));
|
34
|
+
}
|
35
|
+
|
36
|
+
cparams.n_threads = params.n_threads;
|
37
|
+
cparams.n_threads_batch = params.n_threads_batch;
|
38
|
+
cparams.yarn_ext_factor = params.yarn_ext_factor;
|
39
|
+
cparams.yarn_attn_factor = params.yarn_attn_factor;
|
40
|
+
cparams.yarn_beta_fast = params.yarn_beta_fast;
|
41
|
+
cparams.yarn_beta_slow = params.yarn_beta_slow;
|
42
|
+
cparams.defrag_thold = params.defrag_thold;
|
43
|
+
cparams.embeddings = params.embeddings;
|
44
|
+
cparams.offload_kqv = params.offload_kqv;
|
45
|
+
cparams.flash_attn = params.flash_attn;
|
46
|
+
cparams.no_perf = params.no_perf;
|
47
|
+
cparams.pooling_type = params.pooling_type;
|
48
|
+
cparams.warmup = false;
|
49
|
+
|
50
|
+
cparams.n_ctx = params.n_ctx == 0 ? hparams.n_ctx_train : params.n_ctx;
|
51
|
+
cparams.rope_freq_base = params.rope_freq_base == 0.0f ? hparams.rope_freq_base_train : params.rope_freq_base;
|
52
|
+
cparams.rope_freq_scale = params.rope_freq_scale == 0.0f ? hparams.rope_freq_scale_train : params.rope_freq_scale;
|
53
|
+
|
54
|
+
cparams.n_ctx_orig_yarn = params.yarn_orig_ctx != 0 ? params.yarn_orig_ctx :
|
55
|
+
hparams.n_ctx_orig_yarn != 0 ? hparams.n_ctx_orig_yarn :
|
56
|
+
hparams.n_ctx_train;
|
57
|
+
|
58
|
+
cparams.cb_eval = params.cb_eval;
|
59
|
+
cparams.cb_eval_user_data = params.cb_eval_user_data;
|
60
|
+
|
61
|
+
auto rope_scaling_type = params.rope_scaling_type;
|
62
|
+
if (rope_scaling_type == LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED) {
|
63
|
+
rope_scaling_type = hparams.rope_scaling_type_train;
|
64
|
+
}
|
65
|
+
|
66
|
+
if (rope_scaling_type == LLAMA_ROPE_SCALING_TYPE_NONE) {
|
67
|
+
cparams.rope_freq_scale = 1.0f; // never scale if scaling type is none
|
68
|
+
}
|
69
|
+
|
70
|
+
if (cparams.yarn_ext_factor < 0.0f) { // negative indicates 'not set'
|
71
|
+
cparams.yarn_ext_factor = rope_scaling_type == LLAMA_ROPE_SCALING_TYPE_YARN ? 1.0f : 0.0f;
|
72
|
+
}
|
73
|
+
|
74
|
+
cparams.yarn_attn_factor *= hparams.rope_attn_factor;
|
75
|
+
|
76
|
+
if (cparams.pooling_type == LLAMA_POOLING_TYPE_UNSPECIFIED) {
|
77
|
+
if (hparams.pooling_type == LLAMA_POOLING_TYPE_UNSPECIFIED) {
|
78
|
+
cparams.pooling_type = LLAMA_POOLING_TYPE_NONE;
|
79
|
+
} else {
|
80
|
+
cparams.pooling_type = hparams.pooling_type;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
if (params.attention_type == LLAMA_ATTENTION_TYPE_UNSPECIFIED) {
|
85
|
+
cparams.causal_attn = hparams.causal_attn;
|
86
|
+
} else {
|
87
|
+
cparams.causal_attn = params.attention_type == LLAMA_ATTENTION_TYPE_CAUSAL;
|
88
|
+
}
|
89
|
+
|
90
|
+
// with causal attention, the batch size is limited by the context size
|
91
|
+
cparams.n_batch = cparams.causal_attn ? std::min(cparams.n_ctx, params.n_batch) : params.n_batch;
|
92
|
+
|
93
|
+
// the batch has to be at least GGML_KQ_MASK_PAD because we will be padding the KQ_mask
|
94
|
+
// this is required by GPU kernels in order to avoid out-of-bounds accesses (e.g. ggml_flash_attn_ext)
|
95
|
+
// ref: https://github.com/ggerganov/llama.cpp/pull/5021
|
96
|
+
// TODO: this padding is not needed for the cache-less context so we should probably move it to llama_context_kv_self
|
97
|
+
if (cparams.n_batch < GGML_KQ_MASK_PAD) {
|
98
|
+
LLAMA_LOG_WARN("%s: n_batch is less than GGML_KQ_MASK_PAD - increasing to %d\n", __func__, GGML_KQ_MASK_PAD);
|
99
|
+
cparams.n_batch = GGML_KQ_MASK_PAD;
|
100
|
+
}
|
101
|
+
|
102
|
+
cparams.n_ubatch = std::min(cparams.n_batch, params.n_ubatch == 0 ? params.n_batch : params.n_ubatch);
|
103
|
+
|
104
|
+
cparams.op_offload = params.op_offload;
|
105
|
+
|
106
|
+
const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max;
|
107
|
+
|
108
|
+
LLAMA_LOG_INFO("%s: n_seq_max = %u\n", __func__, cparams.n_seq_max);
|
109
|
+
LLAMA_LOG_INFO("%s: n_ctx = %u\n", __func__, cparams.n_ctx);
|
110
|
+
LLAMA_LOG_INFO("%s: n_ctx_per_seq = %u\n", __func__, n_ctx_per_seq);
|
111
|
+
LLAMA_LOG_INFO("%s: n_batch = %u\n", __func__, cparams.n_batch);
|
112
|
+
LLAMA_LOG_INFO("%s: n_ubatch = %u\n", __func__, cparams.n_ubatch);
|
113
|
+
LLAMA_LOG_INFO("%s: causal_attn = %d\n", __func__, cparams.causal_attn);
|
114
|
+
LLAMA_LOG_INFO("%s: flash_attn = %d\n", __func__, cparams.flash_attn);
|
115
|
+
LLAMA_LOG_INFO("%s: freq_base = %.1f\n", __func__, cparams.rope_freq_base);
|
116
|
+
LLAMA_LOG_INFO("%s: freq_scale = %g\n", __func__, cparams.rope_freq_scale);
|
117
|
+
|
118
|
+
if (n_ctx_per_seq < hparams.n_ctx_train) {
|
119
|
+
LLAMA_LOG_WARN("%s: n_ctx_per_seq (%u) < n_ctx_train (%u) -- the full capacity of the model will not be utilized\n",
|
120
|
+
__func__, n_ctx_per_seq, hparams.n_ctx_train);
|
121
|
+
}
|
122
|
+
|
123
|
+
if (n_ctx_per_seq > hparams.n_ctx_train) {
|
124
|
+
LLAMA_LOG_WARN("%s: n_ctx_per_seq (%u) > n_ctx_train (%u) -- possible training context overflow\n",
|
125
|
+
__func__, n_ctx_per_seq, hparams.n_ctx_train);
|
126
|
+
}
|
127
|
+
|
128
|
+
if (!params.swa_full && cparams.n_seq_max > 1 && hparams.is_swa_any()) {
|
129
|
+
LLAMA_LOG_WARN("%s: requested n_seq_max (%u) > 1, but swa_full is not enabled -- performance may be degraded: %s\n",
|
130
|
+
__func__, cparams.n_seq_max, "https://github.com/ggml-org/llama.cpp/pull/13845#issuecomment-2924800573");
|
131
|
+
}
|
132
|
+
|
133
|
+
if (!hparams.vocab_only) {
|
134
|
+
// GPU backends
|
135
|
+
for (auto * dev : model.devices) {
|
136
|
+
ggml_backend_t backend = ggml_backend_dev_init(dev, nullptr);
|
137
|
+
if (backend == nullptr) {
|
138
|
+
throw std::runtime_error(format("failed to initialize %s backend", ggml_backend_dev_name(dev)));
|
139
|
+
}
|
140
|
+
backends.emplace_back(backend);
|
141
|
+
}
|
142
|
+
|
143
|
+
// add ACCEL backends (such as BLAS)
|
144
|
+
for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
|
145
|
+
ggml_backend_dev_t dev = ggml_backend_dev_get(i);
|
146
|
+
if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_ACCEL) {
|
147
|
+
ggml_backend_t backend = ggml_backend_dev_init(dev, nullptr);
|
148
|
+
if (backend == nullptr) {
|
149
|
+
throw std::runtime_error(format("failed to initialize %s backend", ggml_backend_dev_name(dev)));
|
150
|
+
}
|
151
|
+
backends.emplace_back(backend);
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
// add CPU backend
|
156
|
+
backend_cpu = ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr);
|
157
|
+
if (backend_cpu == nullptr) {
|
158
|
+
throw std::runtime_error("failed to initialize CPU backend");
|
159
|
+
}
|
160
|
+
backends.emplace_back(backend_cpu);
|
161
|
+
|
162
|
+
// create a list of the set_n_threads functions in the backends
|
163
|
+
for (auto & backend : backends) {
|
164
|
+
ggml_backend_dev_t dev = ggml_backend_get_device(backend.get());
|
165
|
+
ggml_backend_reg_t reg = dev ? ggml_backend_dev_backend_reg(dev) : nullptr;
|
166
|
+
if (reg) {
|
167
|
+
auto ggml_backend_set_n_threads_fn = (ggml_backend_set_n_threads_t) ggml_backend_reg_get_proc_address(reg, "ggml_backend_set_n_threads");
|
168
|
+
if (ggml_backend_set_n_threads_fn) {
|
169
|
+
set_n_threads_fns.emplace_back(backend.get(), ggml_backend_set_n_threads_fn);
|
170
|
+
}
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
llama_set_abort_callback(this, params.abort_callback, params.abort_callback_data);
|
175
|
+
|
176
|
+
// graph outputs buffer
|
177
|
+
{
|
178
|
+
// resized during inference when a batch uses more outputs
|
179
|
+
if ((uint32_t) output_reserve(params.n_seq_max) < params.n_seq_max) {
|
180
|
+
throw std::runtime_error("failed to reserve initial output buffer");
|
181
|
+
}
|
182
|
+
|
183
|
+
LLAMA_LOG_INFO("%s: %10s output buffer size = %8.2f MiB\n", __func__,
|
184
|
+
ggml_backend_buffer_name (buf_output.get()),
|
185
|
+
ggml_backend_buffer_get_size(buf_output.get()) / 1024.0 / 1024.0);
|
186
|
+
}
|
187
|
+
}
|
188
|
+
|
189
|
+
// init the memory module
|
190
|
+
if (!hparams.vocab_only) {
|
191
|
+
llama_memory_params params_mem = {
|
192
|
+
/*.type_k =*/ params.type_k,
|
193
|
+
/*.type_v =*/ params.type_v,
|
194
|
+
/*.swa_full =*/ params.swa_full,
|
195
|
+
};
|
196
|
+
|
197
|
+
memory.reset(model.create_memory(params_mem, cparams));
|
198
|
+
}
|
199
|
+
|
200
|
+
// init backends
|
201
|
+
if (!hparams.vocab_only) {
|
202
|
+
LLAMA_LOG_DEBUG("%s: enumerating backends\n", __func__);
|
203
|
+
|
204
|
+
backend_buft.clear();
|
205
|
+
backend_ptrs.clear();
|
206
|
+
|
207
|
+
for (auto & backend : backends) {
|
208
|
+
auto * buft = ggml_backend_get_default_buffer_type(backend.get());
|
209
|
+
auto backend_type = ggml_backend_dev_type(ggml_backend_get_device(backend.get()));
|
210
|
+
|
211
|
+
if (backend_type == GGML_BACKEND_DEVICE_TYPE_CPU && !model.devices.empty()) {
|
212
|
+
// use the host buffer of the first device CPU for faster transfer of the intermediate state
|
213
|
+
auto * dev = model.devices[0];
|
214
|
+
auto * host_buft = ggml_backend_dev_host_buffer_type(dev);
|
215
|
+
if (host_buft) {
|
216
|
+
buft = host_buft;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
backend_buft.push_back(buft);
|
221
|
+
backend_ptrs.push_back(backend.get());
|
222
|
+
}
|
223
|
+
|
224
|
+
LLAMA_LOG_DEBUG("%s: backend_ptrs.size() = %zu\n", __func__, backend_ptrs.size());
|
225
|
+
|
226
|
+
const size_t max_nodes = this->graph_max_nodes();
|
227
|
+
|
228
|
+
LLAMA_LOG_DEBUG("%s: max_nodes = %zu\n", __func__, max_nodes);
|
229
|
+
|
230
|
+
// buffer used to store the computation graph and the tensor meta data
|
231
|
+
buf_compute_meta.resize(ggml_tensor_overhead()*max_nodes + ggml_graph_overhead_custom(max_nodes, false));
|
232
|
+
|
233
|
+
// TODO: move these checks to ggml_backend_sched
|
234
|
+
// enabling pipeline parallelism in the scheduler increases memory usage, so it is only done when necessary
|
235
|
+
bool pipeline_parallel =
|
236
|
+
model.n_devices() > 1 &&
|
237
|
+
model.params.n_gpu_layers > (int) model.hparams.n_layer &&
|
238
|
+
model.params.split_mode == LLAMA_SPLIT_MODE_LAYER &&
|
239
|
+
cparams.offload_kqv &&
|
240
|
+
!model.has_tensor_overrides();
|
241
|
+
|
242
|
+
// pipeline parallelism requires support for async compute and events in all devices
|
243
|
+
if (pipeline_parallel) {
|
244
|
+
for (auto & backend : backends) {
|
245
|
+
auto dev_type = ggml_backend_dev_type(ggml_backend_get_device(backend.get()));
|
246
|
+
if (dev_type == GGML_BACKEND_DEVICE_TYPE_CPU) {
|
247
|
+
// ignore CPU backend
|
248
|
+
continue;
|
249
|
+
}
|
250
|
+
auto * dev = ggml_backend_get_device(backend.get());
|
251
|
+
ggml_backend_dev_props props;
|
252
|
+
ggml_backend_dev_get_props(dev, &props);
|
253
|
+
if (!props.caps.async || !props.caps.events) {
|
254
|
+
// device does not support async compute or events
|
255
|
+
pipeline_parallel = false;
|
256
|
+
break;
|
257
|
+
}
|
258
|
+
}
|
259
|
+
}
|
260
|
+
|
261
|
+
sched.reset(ggml_backend_sched_new(backend_ptrs.data(), backend_buft.data(), backend_ptrs.size(), max_nodes, pipeline_parallel, cparams.op_offload));
|
262
|
+
|
263
|
+
if (pipeline_parallel) {
|
264
|
+
LLAMA_LOG_INFO("%s: pipeline parallelism enabled (n_copies=%d)\n", __func__, ggml_backend_sched_get_n_copies(sched.get()));
|
265
|
+
}
|
266
|
+
}
|
267
|
+
|
268
|
+
// reserve worst-case graph
|
269
|
+
if (!hparams.vocab_only && memory) {
|
270
|
+
const uint32_t n_seqs = cparams.n_seq_max;
|
271
|
+
const uint32_t n_tokens = std::min(cparams.n_ctx, cparams.n_ubatch);
|
272
|
+
|
273
|
+
LLAMA_LOG_DEBUG("%s: worst-case: n_tokens = %d, n_seqs = %d, n_outputs = %d\n", __func__, n_tokens, n_seqs, n_outputs);
|
274
|
+
|
275
|
+
int n_splits_pp = -1;
|
276
|
+
int n_nodes_pp = -1;
|
277
|
+
|
278
|
+
int n_splits_tg = -1;
|
279
|
+
int n_nodes_tg = -1;
|
280
|
+
|
281
|
+
// simulate full KV cache
|
282
|
+
|
283
|
+
const auto mctx = memory->init_full();
|
284
|
+
if (!mctx) {
|
285
|
+
throw std::runtime_error("failed to initialize KV cache");
|
286
|
+
}
|
287
|
+
|
288
|
+
cross.v_embd.clear();
|
289
|
+
|
290
|
+
// reserve pp graph first so that buffers are only allocated once
|
291
|
+
{
|
292
|
+
auto * gf = graph_reserve(n_tokens, n_seqs, n_tokens, mctx.get());
|
293
|
+
if (!gf) {
|
294
|
+
throw std::runtime_error("failed to allocate compute pp buffers");
|
295
|
+
}
|
296
|
+
|
297
|
+
n_splits_pp = ggml_backend_sched_get_n_splits(sched.get());
|
298
|
+
n_nodes_pp = ggml_graph_n_nodes(gf);
|
299
|
+
}
|
300
|
+
|
301
|
+
// reserve with tg graph to get the number of splits and nodes
|
302
|
+
{
|
303
|
+
auto * gf = graph_reserve(1, 1, 1, mctx.get());
|
304
|
+
if (!gf) {
|
305
|
+
throw std::runtime_error("failed to allocate compute tg buffers");
|
306
|
+
}
|
307
|
+
|
308
|
+
n_splits_tg = ggml_backend_sched_get_n_splits(sched.get());
|
309
|
+
n_nodes_tg = ggml_graph_n_nodes(gf);
|
310
|
+
}
|
311
|
+
|
312
|
+
// reserve again with pp graph to avoid ggml-alloc reallocations during inference
|
313
|
+
{
|
314
|
+
auto * gf = graph_reserve(n_tokens, n_seqs, n_tokens, mctx.get());
|
315
|
+
if (!gf) {
|
316
|
+
throw std::runtime_error("failed to allocate compute pp buffers");
|
317
|
+
}
|
318
|
+
}
|
319
|
+
|
320
|
+
for (size_t i = 0; i < backend_ptrs.size(); ++i) {
|
321
|
+
ggml_backend_t backend = backend_ptrs[i];
|
322
|
+
ggml_backend_buffer_type_t buft = backend_buft[i];
|
323
|
+
size_t size = ggml_backend_sched_get_buffer_size(sched.get(), backend);
|
324
|
+
if (size > 1) {
|
325
|
+
LLAMA_LOG_INFO("%s: %10s compute buffer size = %8.2f MiB\n", __func__,
|
326
|
+
ggml_backend_buft_name(buft),
|
327
|
+
size / 1024.0 / 1024.0);
|
328
|
+
}
|
329
|
+
}
|
330
|
+
|
331
|
+
if (n_nodes_pp == n_nodes_tg) {
|
332
|
+
LLAMA_LOG_INFO("%s: graph nodes = %d\n", __func__, n_nodes_pp);
|
333
|
+
} else {
|
334
|
+
LLAMA_LOG_INFO("%s: graph nodes = %d (with bs=%d), %d (with bs=1)\n", __func__, n_nodes_pp, n_tokens, n_nodes_tg);
|
335
|
+
}
|
336
|
+
|
337
|
+
if (n_splits_pp == n_splits_tg) {
|
338
|
+
LLAMA_LOG_INFO("%s: graph splits = %d\n", __func__, n_splits_pp);
|
339
|
+
} else {
|
340
|
+
LLAMA_LOG_INFO("%s: graph splits = %d (with bs=%d), %d (with bs=1)\n", __func__, n_splits_pp, n_tokens, n_splits_tg);
|
341
|
+
}
|
342
|
+
}
|
343
|
+
}
|
344
|
+
|
345
|
+
llama_context::~llama_context() {
|
346
|
+
ggml_opt_free(opt_ctx);
|
347
|
+
}
|
348
|
+
|
349
|
+
void llama_context::synchronize() {
|
350
|
+
ggml_backend_sched_synchronize(sched.get());
|
351
|
+
|
352
|
+
// FIXME: if multiple single tokens are evaluated without a synchronization,
|
353
|
+
// the stats will be added to the prompt evaluation stats
|
354
|
+
// this should only happen when using batch size 1 to evaluate a batch
|
355
|
+
|
356
|
+
// add the evaluation to the stats
|
357
|
+
if (n_queued_tokens == 1) {
|
358
|
+
if (!cparams.no_perf) {
|
359
|
+
t_eval_us += ggml_time_us() - t_compute_start_us;
|
360
|
+
}
|
361
|
+
n_eval++;
|
362
|
+
} else if (n_queued_tokens > 1) {
|
363
|
+
if (!cparams.no_perf) {
|
364
|
+
t_p_eval_us += ggml_time_us() - t_compute_start_us;
|
365
|
+
}
|
366
|
+
n_p_eval += n_queued_tokens;
|
367
|
+
}
|
368
|
+
|
369
|
+
// get a more accurate load time, upon first eval
|
370
|
+
if (n_queued_tokens > 0 && !has_evaluated_once) {
|
371
|
+
t_load_us = ggml_time_us() - t_start_us;
|
372
|
+
has_evaluated_once = true;
|
373
|
+
}
|
374
|
+
|
375
|
+
n_queued_tokens = 0;
|
376
|
+
t_compute_start_us = 0;
|
377
|
+
}
|
378
|
+
|
379
|
+
const llama_model & llama_context::get_model() const {
|
380
|
+
return model;
|
381
|
+
}
|
382
|
+
|
383
|
+
const llama_cparams & llama_context::get_cparams() const {
|
384
|
+
return cparams;
|
385
|
+
}
|
386
|
+
|
387
|
+
ggml_backend_sched_t llama_context::get_sched() const {
|
388
|
+
return sched.get();
|
389
|
+
}
|
390
|
+
|
391
|
+
ggml_context * llama_context::get_ctx_compute() const {
|
392
|
+
return ctx_compute.get();
|
393
|
+
}
|
394
|
+
|
395
|
+
uint32_t llama_context::n_ctx() const {
|
396
|
+
return cparams.n_ctx;
|
397
|
+
}
|
398
|
+
|
399
|
+
uint32_t llama_context::n_ctx_per_seq() const {
|
400
|
+
return cparams.n_ctx / cparams.n_seq_max;
|
401
|
+
}
|
402
|
+
|
403
|
+
uint32_t llama_context::n_batch() const {
|
404
|
+
return cparams.n_batch;
|
405
|
+
}
|
406
|
+
|
407
|
+
uint32_t llama_context::n_ubatch() const {
|
408
|
+
return cparams.n_ubatch;
|
409
|
+
}
|
410
|
+
|
411
|
+
uint32_t llama_context::n_seq_max() const {
|
412
|
+
return cparams.n_seq_max;
|
413
|
+
}
|
414
|
+
|
415
|
+
uint32_t llama_context::n_threads() const {
|
416
|
+
return cparams.n_threads;
|
417
|
+
}
|
418
|
+
|
419
|
+
uint32_t llama_context::n_threads_batch() const {
|
420
|
+
return cparams.n_threads_batch;
|
421
|
+
}
|
422
|
+
|
423
|
+
llama_memory_t llama_context::get_memory() const {
|
424
|
+
return memory.get();
|
425
|
+
}
|
426
|
+
|
427
|
+
// deprecated
|
428
|
+
void llama_context::kv_self_defrag_sched() {
|
429
|
+
if (!memory) {
|
430
|
+
return;
|
431
|
+
}
|
432
|
+
|
433
|
+
memory_force_optimize = true;
|
434
|
+
}
|
435
|
+
|
436
|
+
// deprecated
|
437
|
+
bool llama_context::kv_self_update(bool optimize) {
|
438
|
+
if (!memory) {
|
439
|
+
return false;
|
440
|
+
}
|
441
|
+
|
442
|
+
{
|
443
|
+
// TODO: remove in the future
|
444
|
+
optimize |= memory_force_optimize;
|
445
|
+
memory_force_optimize = false;
|
446
|
+
|
447
|
+
const auto mctx = memory->init_update(this, optimize);
|
448
|
+
switch (mctx->get_status()) {
|
449
|
+
case LLAMA_MEMORY_STATUS_SUCCESS:
|
450
|
+
{
|
451
|
+
// noop
|
452
|
+
} break;
|
453
|
+
case LLAMA_MEMORY_STATUS_NO_UPDATE:
|
454
|
+
{
|
455
|
+
// no updates need to be performed
|
456
|
+
return false;
|
457
|
+
}
|
458
|
+
case LLAMA_MEMORY_STATUS_FAILED_PREPARE:
|
459
|
+
case LLAMA_MEMORY_STATUS_FAILED_COMPUTE:
|
460
|
+
{
|
461
|
+
LLAMA_LOG_ERROR("%s: failed to prepare memory update\n", __func__);
|
462
|
+
return false;
|
463
|
+
}
|
464
|
+
}
|
465
|
+
|
466
|
+
if (!mctx->apply()) {
|
467
|
+
LLAMA_LOG_ERROR("%s: failed to apply memory update\n", __func__);
|
468
|
+
}
|
469
|
+
}
|
470
|
+
|
471
|
+
// if the memory module did any computation, we have to reserve a new worst-case graph
|
472
|
+
{
|
473
|
+
const auto mctx = memory->init_full();
|
474
|
+
if (!mctx) {
|
475
|
+
throw std::runtime_error("failed to initialize memory context");
|
476
|
+
}
|
477
|
+
|
478
|
+
const uint32_t n_seqs = cparams.n_seq_max;
|
479
|
+
const uint32_t n_tokens = std::min(cparams.n_ctx, cparams.n_ubatch);
|
480
|
+
|
481
|
+
auto * gf = graph_reserve(n_tokens, n_seqs, n_tokens, mctx.get());
|
482
|
+
if (!gf) {
|
483
|
+
LLAMA_LOG_ERROR("%s: failed to reserve graph after the memory update\n", __func__);
|
484
|
+
}
|
485
|
+
}
|
486
|
+
|
487
|
+
return true;
|
488
|
+
}
|
489
|
+
|
490
|
+
enum llama_pooling_type llama_context::pooling_type() const {
|
491
|
+
return cparams.pooling_type;
|
492
|
+
}
|
493
|
+
|
494
|
+
float * llama_context::get_logits() {
|
495
|
+
return logits;
|
496
|
+
}
|
497
|
+
|
498
|
+
float * llama_context::get_logits_ith(int32_t i) {
|
499
|
+
int64_t j = -1;
|
500
|
+
|
501
|
+
try {
|
502
|
+
if (logits == nullptr) {
|
503
|
+
throw std::runtime_error("no logits");
|
504
|
+
}
|
505
|
+
|
506
|
+
if (i < 0) {
|
507
|
+
j = n_outputs + i;
|
508
|
+
if (j < 0) {
|
509
|
+
throw std::runtime_error(format("negative index out of range [0, %d)", n_outputs));
|
510
|
+
}
|
511
|
+
} else if ((size_t) i >= output_ids.size()) {
|
512
|
+
throw std::runtime_error(format("out of range [0, %zu)", output_ids.size()));
|
513
|
+
} else {
|
514
|
+
j = output_ids[i];
|
515
|
+
}
|
516
|
+
|
517
|
+
if (j < 0) {
|
518
|
+
throw std::runtime_error(format("batch.logits[%d] != true", i));
|
519
|
+
}
|
520
|
+
if (j >= n_outputs) {
|
521
|
+
// This should not happen
|
522
|
+
throw std::runtime_error(format("corrupt output buffer (j=%" PRId64 ", n_outputs=%d)", j, n_outputs));
|
523
|
+
}
|
524
|
+
|
525
|
+
return logits + j*model.vocab.n_tokens();
|
526
|
+
} catch (const std::exception & err) {
|
527
|
+
LLAMA_LOG_ERROR("%s: invalid logits id %d, reason: %s\n", __func__, i, err.what());
|
528
|
+
#ifndef NDEBUG
|
529
|
+
GGML_ABORT("fatal error");
|
530
|
+
#else
|
531
|
+
return nullptr;
|
532
|
+
#endif
|
533
|
+
}
|
534
|
+
}
|
535
|
+
|
536
|
+
float * llama_context::get_embeddings() {
|
537
|
+
return embd;
|
538
|
+
}
|
539
|
+
|
540
|
+
float * llama_context::get_embeddings_ith(int32_t i) {
|
541
|
+
int64_t j = -1;
|
542
|
+
|
543
|
+
try {
|
544
|
+
if (embd == nullptr) {
|
545
|
+
throw std::runtime_error("no embeddings");
|
546
|
+
}
|
547
|
+
|
548
|
+
if (i < 0) {
|
549
|
+
j = n_outputs + i;
|
550
|
+
if (j < 0) {
|
551
|
+
throw std::runtime_error(format("negative index out of range [0, %d)", n_outputs));
|
552
|
+
}
|
553
|
+
} else if ((size_t) i >= output_ids.size()) {
|
554
|
+
throw std::runtime_error(format("out of range [0, %zu)", output_ids.size()));
|
555
|
+
} else {
|
556
|
+
j = output_ids[i];
|
557
|
+
}
|
558
|
+
|
559
|
+
if (j < 0) {
|
560
|
+
throw std::runtime_error(format("batch.logits[%d] != true", i));
|
561
|
+
}
|
562
|
+
if (j >= n_outputs) {
|
563
|
+
// This should not happen
|
564
|
+
throw std::runtime_error(format("corrupt output buffer (j=%" PRId64 ", n_outputs=%d)", j, n_outputs));
|
565
|
+
}
|
566
|
+
|
567
|
+
return embd + j*model.hparams.n_embd;
|
568
|
+
} catch (const std::exception & err) {
|
569
|
+
LLAMA_LOG_ERROR("%s: invalid embeddings id %d, reason: %s\n", __func__, i, err.what());
|
570
|
+
#ifndef NDEBUG
|
571
|
+
GGML_ABORT("fatal error");
|
572
|
+
#else
|
573
|
+
return nullptr;
|
574
|
+
#endif
|
575
|
+
}
|
576
|
+
}
|
577
|
+
|
578
|
+
float * llama_context::get_embeddings_seq(llama_seq_id seq_id) {
|
579
|
+
auto it = embd_seq.find(seq_id);
|
580
|
+
if (it == embd_seq.end()) {
|
581
|
+
return nullptr;
|
582
|
+
}
|
583
|
+
|
584
|
+
return it->second.data();
|
585
|
+
}
|
586
|
+
|
587
|
+
void llama_context::attach_threadpool(
|
588
|
+
ggml_threadpool_t threadpool,
|
589
|
+
ggml_threadpool_t threadpool_batch) {
|
590
|
+
LLAMA_LOG_DEBUG("%s: call\n", __func__);
|
591
|
+
|
592
|
+
this->threadpool = threadpool;
|
593
|
+
this->threadpool_batch = threadpool_batch ? threadpool_batch : threadpool;
|
594
|
+
}
|
595
|
+
|
596
|
+
void llama_context::detach_threadpool() {
|
597
|
+
LLAMA_LOG_DEBUG("%s: call\n", __func__);
|
598
|
+
|
599
|
+
this->threadpool = nullptr;
|
600
|
+
this->threadpool_batch = nullptr;
|
601
|
+
}
|
602
|
+
|
603
|
+
void llama_context::set_n_threads(int32_t n_threads, int32_t n_threads_batch) {
|
604
|
+
LLAMA_LOG_DEBUG("%s: n_threads = %d, n_threads_batch = %d\n", __func__, n_threads, n_threads_batch);
|
605
|
+
|
606
|
+
cparams.n_threads = n_threads;
|
607
|
+
cparams.n_threads_batch = n_threads_batch;
|
608
|
+
}
|
609
|
+
|
610
|
+
void llama_context::set_abort_callback(bool (*abort_callback)(void * data), void * abort_callback_data) {
|
611
|
+
LLAMA_LOG_DEBUG("%s: call\n", __func__);
|
612
|
+
|
613
|
+
this->abort_callback = abort_callback;
|
614
|
+
this->abort_callback_data = abort_callback_data;
|
615
|
+
|
616
|
+
for (auto & backend : backends) {
|
617
|
+
auto * reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend.get()));
|
618
|
+
auto * set_abort_callback_fn = (ggml_backend_set_abort_callback_t) ggml_backend_reg_get_proc_address(reg, "ggml_backend_set_abort_callback");
|
619
|
+
if (set_abort_callback_fn) {
|
620
|
+
set_abort_callback_fn(backend.get(), this->abort_callback, this->abort_callback_data);
|
621
|
+
}
|
622
|
+
}
|
623
|
+
}
|
624
|
+
|
625
|
+
void llama_context::set_embeddings(bool value) {
|
626
|
+
LLAMA_LOG_DEBUG("%s: value = %d\n", __func__, value);
|
627
|
+
|
628
|
+
cparams.embeddings = value;
|
629
|
+
}
|
630
|
+
|
631
|
+
void llama_context::set_causal_attn(bool value) {
|
632
|
+
LLAMA_LOG_DEBUG("%s: value = %d\n", __func__, value);
|
633
|
+
|
634
|
+
cparams.causal_attn = value;
|
635
|
+
}
|
636
|
+
|
637
|
+
void llama_context::set_warmup(bool value) {
|
638
|
+
LLAMA_LOG_DEBUG("%s: value = %d\n", __func__, value);
|
639
|
+
|
640
|
+
cparams.warmup = value;
|
641
|
+
}
|
642
|
+
|
643
|
+
void llama_context::set_adapter_lora(
|
644
|
+
llama_adapter_lora * adapter,
|
645
|
+
float scale) {
|
646
|
+
LLAMA_LOG_DEBUG("%s: adapter = %p, scale = %f\n", __func__, (void *) adapter, scale);
|
647
|
+
|
648
|
+
loras[adapter] = scale;
|
649
|
+
}
|
650
|
+
|
651
|
+
bool llama_context::rm_adapter_lora(
|
652
|
+
llama_adapter_lora * adapter) {
|
653
|
+
LLAMA_LOG_DEBUG("%s: adapter = %p\n", __func__, (void *) adapter);
|
654
|
+
|
655
|
+
auto pos = loras.find(adapter);
|
656
|
+
if (pos != loras.end()) {
|
657
|
+
loras.erase(pos);
|
658
|
+
return true;
|
659
|
+
}
|
660
|
+
|
661
|
+
return false;
|
662
|
+
}
|
663
|
+
|
664
|
+
void llama_context::clear_adapter_lora() {
|
665
|
+
LLAMA_LOG_DEBUG("%s: call\n", __func__);
|
666
|
+
|
667
|
+
loras.clear();
|
668
|
+
}
|
669
|
+
|
670
|
+
bool llama_context::apply_adapter_cvec(
|
671
|
+
const float * data,
|
672
|
+
size_t len,
|
673
|
+
int32_t n_embd,
|
674
|
+
int32_t il_start,
|
675
|
+
int32_t il_end) {
|
676
|
+
LLAMA_LOG_DEBUG("%s: il_start = %d, il_end = %d\n", __func__, il_start, il_end);
|
677
|
+
|
678
|
+
return cvec.apply(model, data, len, n_embd, il_start, il_end);
|
679
|
+
}
|
680
|
+
|
681
|
+
llm_graph_result_ptr llama_context::process_ubatch(const llama_ubatch & ubatch, llm_graph_type gtype, llama_memory_context_i * mctx, ggml_status & ret) {
|
682
|
+
if (mctx && !mctx->apply()) {
|
683
|
+
LLAMA_LOG_ERROR("%s: failed to apply memory context\n", __func__);
|
684
|
+
ret = GGML_STATUS_FAILED;
|
685
|
+
return nullptr;
|
686
|
+
}
|
687
|
+
|
688
|
+
auto * gf = graph_init();
|
689
|
+
if (!gf) {
|
690
|
+
LLAMA_LOG_ERROR("%s: failed to initialize graph\n", __func__);
|
691
|
+
ret = GGML_STATUS_FAILED;
|
692
|
+
return nullptr;
|
693
|
+
}
|
694
|
+
|
695
|
+
auto res = graph_build(ctx_compute.get(), gf, ubatch, gtype, mctx);
|
696
|
+
if (!res) {
|
697
|
+
LLAMA_LOG_ERROR("%s: failed to build graph\n", __func__);
|
698
|
+
ret = GGML_STATUS_FAILED;
|
699
|
+
return nullptr;
|
700
|
+
}
|
701
|
+
|
702
|
+
// LLAMA_LOG_INFO("graph build time: %.3f ms (%d nodes, %d leafs)\n", (ggml_time_us() - t_start_us)/1000.0, gf->n_nodes, gf->n_leafs);
|
703
|
+
|
704
|
+
if (!ggml_backend_sched_alloc_graph(sched.get(), gf)) {
|
705
|
+
LLAMA_LOG_ERROR("%s: failed to allocate graph\n", __func__);
|
706
|
+
ret = GGML_STATUS_ALLOC_FAILED;
|
707
|
+
return nullptr;
|
708
|
+
}
|
709
|
+
|
710
|
+
res->set_inputs(&ubatch);
|
711
|
+
|
712
|
+
const auto status = graph_compute(gf, ubatch.n_tokens > 1);
|
713
|
+
if (status != GGML_STATUS_SUCCESS) {
|
714
|
+
LLAMA_LOG_ERROR("%s: failed to compute graph, compute status: %d\n", __func__, status);
|
715
|
+
ret = status;
|
716
|
+
return nullptr;
|
717
|
+
}
|
718
|
+
|
719
|
+
ret = GGML_STATUS_SUCCESS;
|
720
|
+
|
721
|
+
return res;
|
722
|
+
}
|
723
|
+
|
724
|
+
int llama_context::encode(const llama_batch & batch_inp) {
|
725
|
+
GGML_ASSERT((!batch_inp.token && batch_inp.embd) || (batch_inp.token && !batch_inp.embd)); // NOLINT
|
726
|
+
|
727
|
+
if (batch_inp.n_tokens == 0) {
|
728
|
+
LLAMA_LOG_ERROR("%s: n_tokens == 0\n", __func__);
|
729
|
+
return -1;
|
730
|
+
}
|
731
|
+
|
732
|
+
const auto & hparams = model.hparams;
|
733
|
+
|
734
|
+
const int64_t n_embd = hparams.n_embd;
|
735
|
+
|
736
|
+
// note: during encode, we always pass the full sequence starting from pos = 0
|
737
|
+
if (!balloc->init(batch_inp, model.vocab, nullptr, n_embd, true)) {
|
738
|
+
LLAMA_LOG_ERROR("%s: failed to initialize batch\n", __func__);
|
739
|
+
return -1;
|
740
|
+
}
|
741
|
+
|
742
|
+
const uint32_t n_tokens = balloc->get_n_tokens();
|
743
|
+
|
744
|
+
const llama_ubatch ubatch = balloc->split_simple(n_tokens);
|
745
|
+
|
746
|
+
// micro-batching is not possible for non-causal encoding, so we process the batch in a single shot
|
747
|
+
GGML_ASSERT(cparams.n_ubatch >= n_tokens && "encoder requires n_ubatch >= n_tokens");
|
748
|
+
|
749
|
+
if (t_compute_start_us == 0) {
|
750
|
+
t_compute_start_us = ggml_time_us();
|
751
|
+
}
|
752
|
+
|
753
|
+
// TODO: this clear of the buffer can easily be forgotten - need something better
|
754
|
+
embd_seq.clear();
|
755
|
+
|
756
|
+
n_queued_tokens += n_tokens;
|
757
|
+
|
758
|
+
// reserve output buffer
|
759
|
+
if (output_reserve(n_tokens) < n_tokens) {
|
760
|
+
LLAMA_LOG_ERROR("%s: could not reserve space for batch with %u outputs\n", __func__, n_tokens);
|
761
|
+
return -2;
|
762
|
+
};
|
763
|
+
|
764
|
+
for (uint32_t i = 0; i < n_tokens; ++i) {
|
765
|
+
output_ids[i] = i;
|
766
|
+
}
|
767
|
+
|
768
|
+
n_outputs = n_tokens;
|
769
|
+
|
770
|
+
ggml_backend_sched_reset(sched.get());
|
771
|
+
ggml_backend_sched_set_eval_callback(sched.get(), cparams.cb_eval, cparams.cb_eval_user_data);
|
772
|
+
|
773
|
+
const auto causal_attn_org = cparams.causal_attn;
|
774
|
+
|
775
|
+
// always use non-causal attention for encoder graphs
|
776
|
+
// TODO: this is a tmp solution until we have a proper way to support enc-dec models
|
777
|
+
// ref: https://github.com/ggml-org/llama.cpp/pull/12181#issuecomment-2730451223
|
778
|
+
cparams.causal_attn = false;
|
779
|
+
|
780
|
+
ggml_status status;
|
781
|
+
const auto res = process_ubatch(ubatch, LLM_GRAPH_TYPE_ENCODER, nullptr, status);
|
782
|
+
|
783
|
+
cparams.causal_attn = causal_attn_org;
|
784
|
+
|
785
|
+
if (!res) {
|
786
|
+
switch (status) {
|
787
|
+
case GGML_STATUS_ABORTED: return 2;
|
788
|
+
case GGML_STATUS_ALLOC_FAILED: return -2;
|
789
|
+
case GGML_STATUS_FAILED: return -3;
|
790
|
+
case GGML_STATUS_SUCCESS: GGML_ABORT("should not happen");
|
791
|
+
}
|
792
|
+
}
|
793
|
+
|
794
|
+
auto * t_embd = res->get_embd_pooled() ? res->get_embd_pooled() : res->get_embd();
|
795
|
+
|
796
|
+
// extract embeddings
|
797
|
+
if (t_embd) {
|
798
|
+
ggml_backend_t backend_embd = ggml_backend_sched_get_tensor_backend(sched.get(), t_embd);
|
799
|
+
GGML_ASSERT(backend_embd != nullptr);
|
800
|
+
|
801
|
+
switch (cparams.pooling_type) {
|
802
|
+
case LLAMA_POOLING_TYPE_NONE:
|
803
|
+
{
|
804
|
+
// extract token embeddings
|
805
|
+
GGML_ASSERT(embd != nullptr);
|
806
|
+
|
807
|
+
GGML_ASSERT(n_tokens*n_embd <= (int64_t) embd_size);
|
808
|
+
ggml_backend_tensor_get_async(backend_embd, t_embd, embd, 0, n_tokens*n_embd*sizeof(float));
|
809
|
+
} break;
|
810
|
+
case LLAMA_POOLING_TYPE_MEAN:
|
811
|
+
case LLAMA_POOLING_TYPE_CLS:
|
812
|
+
case LLAMA_POOLING_TYPE_LAST:
|
813
|
+
{
|
814
|
+
// extract sequence embeddings
|
815
|
+
auto & embd_seq_out = embd_seq;
|
816
|
+
|
817
|
+
for (uint32_t s = 0; s < ubatch.n_seqs_unq; ++s) {
|
818
|
+
const llama_seq_id seq_id = ubatch.seq_id_unq[s];
|
819
|
+
const int32_t seq_idx = ubatch.seq_idx[seq_id];
|
820
|
+
|
821
|
+
embd_seq_out[seq_id].resize(n_embd);
|
822
|
+
ggml_backend_tensor_get_async(backend_embd, t_embd, embd_seq_out[seq_id].data(), (n_embd*seq_idx)*sizeof(float), n_embd*sizeof(float));
|
823
|
+
}
|
824
|
+
} break;
|
825
|
+
case LLAMA_POOLING_TYPE_RANK:
|
826
|
+
{
|
827
|
+
// extract the rerank score - n_cls_out floats per sequence
|
828
|
+
auto & embd_seq_out = embd_seq;
|
829
|
+
|
830
|
+
const uint32_t n_cls_out = hparams.n_cls_out;
|
831
|
+
|
832
|
+
for (uint32_t s = 0; s < ubatch.n_seqs_unq; ++s) {
|
833
|
+
const llama_seq_id seq_id = ubatch.seq_id_unq[s];
|
834
|
+
const int32_t seq_idx = ubatch.seq_idx[seq_id];
|
835
|
+
|
836
|
+
embd_seq_out[seq_id].resize(n_cls_out);
|
837
|
+
ggml_backend_tensor_get_async(backend_embd, t_embd, embd_seq_out[seq_id].data(), (n_cls_out*seq_idx)*sizeof(float), n_cls_out*sizeof(float));
|
838
|
+
}
|
839
|
+
} break;
|
840
|
+
case LLAMA_POOLING_TYPE_UNSPECIFIED:
|
841
|
+
{
|
842
|
+
GGML_ABORT("unknown pooling type");
|
843
|
+
}
|
844
|
+
}
|
845
|
+
}
|
846
|
+
|
847
|
+
// Reset state for the next token before backend sync, to allow the CPU activities in the reset to
|
848
|
+
// overlap with device computation.
|
849
|
+
ggml_backend_sched_reset(sched.get());
|
850
|
+
|
851
|
+
// TODO: hacky solution
|
852
|
+
if (model.arch == LLM_ARCH_T5 && t_embd) {
|
853
|
+
//cross.t_embd = t_embd;
|
854
|
+
|
855
|
+
synchronize();
|
856
|
+
|
857
|
+
cross.n_embd = t_embd->ne[0];
|
858
|
+
cross.n_enc = t_embd->ne[1];
|
859
|
+
cross.v_embd.resize(cross.n_embd*cross.n_enc);
|
860
|
+
memcpy(cross.v_embd.data(), embd, ggml_nbytes(t_embd));
|
861
|
+
|
862
|
+
const auto & batch = balloc->get_batch();
|
863
|
+
|
864
|
+
// remember the sequence ids used during the encoding - needed for cross attention later
|
865
|
+
cross.seq_ids_enc.resize(n_tokens);
|
866
|
+
for (uint32_t i = 0; i < n_tokens; i++) {
|
867
|
+
cross.seq_ids_enc[i].clear();
|
868
|
+
|
869
|
+
for (int s = 0; s < batch.n_seq_id[i]; s++) {
|
870
|
+
const llama_seq_id seq_id = batch.seq_id[i][s];
|
871
|
+
|
872
|
+
cross.seq_ids_enc[i].insert(seq_id);
|
873
|
+
}
|
874
|
+
}
|
875
|
+
}
|
876
|
+
|
877
|
+
return 0;
|
878
|
+
}
|
879
|
+
|
880
|
+
int llama_context::decode(const llama_batch & batch_inp) {
|
881
|
+
GGML_ASSERT((!batch_inp.token && batch_inp.embd) || (batch_inp.token && !batch_inp.embd)); // NOLINT
|
882
|
+
|
883
|
+
if (!memory) {
|
884
|
+
LLAMA_LOG_DEBUG("%s: cannot decode batches with this context (calling encode() instead)\n", __func__);
|
885
|
+
return encode(batch_inp);
|
886
|
+
}
|
887
|
+
|
888
|
+
if (batch_inp.n_tokens == 0) {
|
889
|
+
LLAMA_LOG_ERROR("%s: n_tokens == 0\n", __func__);
|
890
|
+
return -1;
|
891
|
+
}
|
892
|
+
|
893
|
+
const auto & vocab = model.vocab;
|
894
|
+
const auto & hparams = model.hparams;
|
895
|
+
|
896
|
+
const int32_t n_vocab = vocab.n_tokens();
|
897
|
+
const int64_t n_embd = hparams.n_embd;
|
898
|
+
|
899
|
+
// when computing embeddings, all tokens are output
|
900
|
+
const bool output_all = cparams.embeddings;
|
901
|
+
|
902
|
+
if (!balloc->init(batch_inp, vocab, memory.get(), n_embd, output_all)) {
|
903
|
+
LLAMA_LOG_ERROR("%s: failed to initialize batch\n", __func__);
|
904
|
+
return -1;
|
905
|
+
}
|
906
|
+
|
907
|
+
const uint32_t n_tokens_all = balloc->get_n_tokens();
|
908
|
+
const uint32_t n_outputs_all = balloc->get_n_outputs();
|
909
|
+
|
910
|
+
if (output_all) {
|
911
|
+
// require that all tokens are output
|
912
|
+
if (n_outputs_all != n_tokens_all) {
|
913
|
+
LLAMA_LOG_ERROR("%s: pooled embedding requires that all tokens are output (n_outputs_all = %d, n_tokens_all = %d)\n",
|
914
|
+
__func__, n_outputs_all, n_tokens_all);
|
915
|
+
return -1;
|
916
|
+
}
|
917
|
+
}
|
918
|
+
|
919
|
+
GGML_ASSERT(n_tokens_all <= cparams.n_batch);
|
920
|
+
|
921
|
+
GGML_ASSERT((cparams.causal_attn || cparams.n_ubatch >= n_tokens_all) && "non-causal attention requires n_ubatch >= n_tokens");
|
922
|
+
|
923
|
+
if (t_compute_start_us == 0) {
|
924
|
+
t_compute_start_us = ggml_time_us();
|
925
|
+
}
|
926
|
+
n_queued_tokens += n_tokens_all;
|
927
|
+
|
928
|
+
// TODO: this clear of the buffer can easily be forgotten - need something better
|
929
|
+
embd_seq.clear();
|
930
|
+
|
931
|
+
bool did_optimize = false;
|
932
|
+
|
933
|
+
// handle any pending defrags/shifts
|
934
|
+
kv_self_update(false);
|
935
|
+
|
936
|
+
llama_memory_context_ptr mctx;
|
937
|
+
|
938
|
+
while (true) {
|
939
|
+
mctx = memory->init_batch(*balloc, cparams.n_ubatch, output_all);
|
940
|
+
if (!mctx) {
|
941
|
+
return -2;
|
942
|
+
}
|
943
|
+
|
944
|
+
switch (mctx->get_status()) {
|
945
|
+
case LLAMA_MEMORY_STATUS_SUCCESS:
|
946
|
+
{
|
947
|
+
} break;
|
948
|
+
case LLAMA_MEMORY_STATUS_NO_UPDATE:
|
949
|
+
{
|
950
|
+
LLAMA_LOG_ERROR("%s: unexpected memory context status: %d\n", __func__, mctx->get_status());
|
951
|
+
|
952
|
+
return -2;
|
953
|
+
}
|
954
|
+
case LLAMA_MEMORY_STATUS_FAILED_PREPARE:
|
955
|
+
{
|
956
|
+
if (!did_optimize) {
|
957
|
+
did_optimize = true;
|
958
|
+
|
959
|
+
if (kv_self_update(true)) {
|
960
|
+
LLAMA_LOG_DEBUG("%s: retrying batch size %d after cache optimization\n", __func__, balloc->get_n_tokens());
|
961
|
+
|
962
|
+
continue;
|
963
|
+
}
|
964
|
+
}
|
965
|
+
|
966
|
+
LLAMA_LOG_WARN("%s: failed to find a memory slot for batch of size %d\n", __func__, balloc->get_n_tokens());
|
967
|
+
|
968
|
+
return 1;
|
969
|
+
}
|
970
|
+
case LLAMA_MEMORY_STATUS_FAILED_COMPUTE:
|
971
|
+
{
|
972
|
+
LLAMA_LOG_ERROR("%s: compute failed while preparing batch of size %d\n", __func__, balloc->get_n_tokens());
|
973
|
+
|
974
|
+
return -2;
|
975
|
+
}
|
976
|
+
}
|
977
|
+
|
978
|
+
break;
|
979
|
+
}
|
980
|
+
|
981
|
+
// reserve output buffer
|
982
|
+
if (output_reserve(n_outputs_all) < n_outputs_all) {
|
983
|
+
LLAMA_LOG_ERROR("%s: could not reserve space for batch with %d outputs\n", __func__, n_outputs_all);
|
984
|
+
return -2;
|
985
|
+
};
|
986
|
+
|
987
|
+
int64_t n_outputs_prev = 0;
|
988
|
+
|
989
|
+
do {
|
990
|
+
const auto & ubatch = mctx->get_ubatch();
|
991
|
+
|
992
|
+
// count the outputs in this ubatch
|
993
|
+
{
|
994
|
+
int32_t n_outputs_new = 0;
|
995
|
+
|
996
|
+
if (n_outputs_all == n_tokens_all) {
|
997
|
+
n_outputs_new = ubatch.n_tokens;
|
998
|
+
} else {
|
999
|
+
for (uint32_t i = 0; i < ubatch.n_tokens; i++) {
|
1000
|
+
n_outputs_new += (int32_t) (ubatch.output[i] != 0);
|
1001
|
+
}
|
1002
|
+
}
|
1003
|
+
|
1004
|
+
// needs to happen before the graph is built
|
1005
|
+
n_outputs = n_outputs_new;
|
1006
|
+
}
|
1007
|
+
|
1008
|
+
ggml_backend_sched_reset(sched.get());
|
1009
|
+
ggml_backend_sched_set_eval_callback(sched.get(), cparams.cb_eval, cparams.cb_eval_user_data);
|
1010
|
+
|
1011
|
+
ggml_status status;
|
1012
|
+
const auto res = process_ubatch(ubatch, LLM_GRAPH_TYPE_DECODER, mctx.get(), status);
|
1013
|
+
|
1014
|
+
if (!res) {
|
1015
|
+
// the last ubatch failed or was aborted -> remove all positions of that ubatch from the KV cache
|
1016
|
+
llama_pos pos_min[LLAMA_MAX_SEQ];
|
1017
|
+
for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
|
1018
|
+
pos_min[s] = std::numeric_limits<llama_pos>::max();
|
1019
|
+
}
|
1020
|
+
|
1021
|
+
for (uint32_t i = 0; i < ubatch.n_tokens; ++i) {
|
1022
|
+
const auto & seq_id = ubatch.seq_id[i][0];
|
1023
|
+
|
1024
|
+
pos_min[seq_id] = std::min(pos_min[seq_id], ubatch.pos[i]);
|
1025
|
+
}
|
1026
|
+
|
1027
|
+
for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
|
1028
|
+
if (pos_min[s] == std::numeric_limits<llama_pos>::max()) {
|
1029
|
+
continue;
|
1030
|
+
}
|
1031
|
+
|
1032
|
+
LLAMA_LOG_WARN("%s: removing KV cache entries for seq_id = %d, pos = [%d, +inf)\n", __func__, s, pos_min[s]);
|
1033
|
+
|
1034
|
+
memory->seq_rm(s, pos_min[s], -1);
|
1035
|
+
}
|
1036
|
+
|
1037
|
+
switch (status) {
|
1038
|
+
case GGML_STATUS_ABORTED: return 2;
|
1039
|
+
case GGML_STATUS_ALLOC_FAILED: return -2;
|
1040
|
+
case GGML_STATUS_FAILED: return -3;
|
1041
|
+
case GGML_STATUS_SUCCESS: GGML_ABORT("should not happen");
|
1042
|
+
}
|
1043
|
+
}
|
1044
|
+
|
1045
|
+
// plot the computation graph in dot format (for debugging purposes)
|
1046
|
+
//if (n_past%100 == 0) {
|
1047
|
+
// ggml_graph_dump_dot(gf, NULL, "llama.dot");
|
1048
|
+
//}
|
1049
|
+
|
1050
|
+
auto * t_logits = res->get_logits();
|
1051
|
+
auto * t_embd = cparams.embeddings ? res->get_embd() : nullptr;
|
1052
|
+
|
1053
|
+
if (t_embd && res->get_embd_pooled()) {
|
1054
|
+
t_embd = res->get_embd_pooled();
|
1055
|
+
}
|
1056
|
+
|
1057
|
+
// extract logits
|
1058
|
+
if (t_logits && n_outputs > 0) {
|
1059
|
+
ggml_backend_t backend_res = ggml_backend_sched_get_tensor_backend(sched.get(), t_logits);
|
1060
|
+
GGML_ASSERT(backend_res != nullptr);
|
1061
|
+
GGML_ASSERT(logits != nullptr);
|
1062
|
+
|
1063
|
+
float * logits_out = logits + n_outputs_prev*n_vocab;
|
1064
|
+
|
1065
|
+
if (n_outputs) {
|
1066
|
+
GGML_ASSERT( n_outputs_prev + n_outputs <= n_outputs_all);
|
1067
|
+
GGML_ASSERT((n_outputs_prev + n_outputs)*n_vocab <= (int64_t) logits_size);
|
1068
|
+
ggml_backend_tensor_get_async(backend_res, t_logits, logits_out, 0, n_outputs*n_vocab*sizeof(float));
|
1069
|
+
}
|
1070
|
+
}
|
1071
|
+
|
1072
|
+
// extract embeddings
|
1073
|
+
if (t_embd && n_outputs > 0) {
|
1074
|
+
ggml_backend_t backend_embd = ggml_backend_sched_get_tensor_backend(sched.get(), t_embd);
|
1075
|
+
GGML_ASSERT(backend_embd != nullptr);
|
1076
|
+
|
1077
|
+
switch (cparams.pooling_type) {
|
1078
|
+
case LLAMA_POOLING_TYPE_NONE:
|
1079
|
+
{
|
1080
|
+
// extract token embeddings
|
1081
|
+
GGML_ASSERT(embd != nullptr);
|
1082
|
+
float * embd_out = embd + n_outputs_prev*n_embd;
|
1083
|
+
|
1084
|
+
if (n_outputs) {
|
1085
|
+
GGML_ASSERT( n_outputs_prev + n_outputs <= n_outputs_all);
|
1086
|
+
GGML_ASSERT((n_outputs_prev + n_outputs)*n_embd <= (int64_t) embd_size);
|
1087
|
+
ggml_backend_tensor_get_async(backend_embd, t_embd, embd_out, 0, n_outputs*n_embd*sizeof(float));
|
1088
|
+
}
|
1089
|
+
} break;
|
1090
|
+
case LLAMA_POOLING_TYPE_MEAN:
|
1091
|
+
case LLAMA_POOLING_TYPE_CLS:
|
1092
|
+
case LLAMA_POOLING_TYPE_LAST:
|
1093
|
+
{
|
1094
|
+
// extract sequence embeddings (cleared before processing each batch)
|
1095
|
+
auto & embd_seq_out = embd_seq;
|
1096
|
+
|
1097
|
+
for (uint32_t s = 0; s < ubatch.n_seqs_unq; ++s) {
|
1098
|
+
const llama_seq_id seq_id = ubatch.seq_id_unq[s];
|
1099
|
+
const int32_t seq_idx = ubatch.seq_idx[seq_id];
|
1100
|
+
|
1101
|
+
embd_seq_out[seq_id].resize(n_embd);
|
1102
|
+
ggml_backend_tensor_get_async(backend_embd, t_embd, embd_seq_out[seq_id].data(), (n_embd*seq_idx)*sizeof(float), n_embd*sizeof(float));
|
1103
|
+
}
|
1104
|
+
} break;
|
1105
|
+
case LLAMA_POOLING_TYPE_RANK:
|
1106
|
+
{
|
1107
|
+
// extract the rerank score - n_cls_out floats per sequence
|
1108
|
+
auto & embd_seq_out = embd_seq;
|
1109
|
+
|
1110
|
+
const uint32_t n_cls_out = hparams.n_cls_out;
|
1111
|
+
|
1112
|
+
for (uint32_t s = 0; s < ubatch.n_seqs_unq; ++s) {
|
1113
|
+
const llama_seq_id seq_id = ubatch.seq_id_unq[s];
|
1114
|
+
const int32_t seq_idx = ubatch.seq_idx[seq_id];
|
1115
|
+
|
1116
|
+
embd_seq_out[seq_id].resize(n_cls_out);
|
1117
|
+
ggml_backend_tensor_get_async(backend_embd, t_embd, embd_seq_out[seq_id].data(), (n_cls_out*seq_idx)*sizeof(float), n_cls_out*sizeof(float));
|
1118
|
+
}
|
1119
|
+
} break;
|
1120
|
+
case LLAMA_POOLING_TYPE_UNSPECIFIED:
|
1121
|
+
{
|
1122
|
+
GGML_ABORT("unknown pooling type");
|
1123
|
+
}
|
1124
|
+
}
|
1125
|
+
}
|
1126
|
+
|
1127
|
+
n_outputs_prev += n_outputs;
|
1128
|
+
} while (mctx->next());
|
1129
|
+
|
1130
|
+
// set to total number of outputs in the batch, for use in llama_get_logits_ith
|
1131
|
+
n_outputs = n_outputs_all;
|
1132
|
+
|
1133
|
+
// set output mappings
|
1134
|
+
if (n_outputs > 0) {
|
1135
|
+
bool sorted_output = true;
|
1136
|
+
|
1137
|
+
auto & out_ids = balloc->get_out_ids();
|
1138
|
+
|
1139
|
+
GGML_ASSERT(out_ids.size() == (size_t) n_outputs);
|
1140
|
+
|
1141
|
+
for (int64_t i = 0; i < n_outputs; ++i) {
|
1142
|
+
int64_t out_id = out_ids[i];
|
1143
|
+
output_ids[out_id] = i;
|
1144
|
+
if (out_id != i) {
|
1145
|
+
sorted_output = false;
|
1146
|
+
}
|
1147
|
+
}
|
1148
|
+
|
1149
|
+
// make the outputs have the same order they had in the user-provided batch
|
1150
|
+
// note: this is mostly relevant for recurrent models atm
|
1151
|
+
if (!sorted_output) {
|
1152
|
+
const uint32_t n_vocab = model.vocab.n_tokens();
|
1153
|
+
const uint64_t n_embd = model.hparams.n_embd;
|
1154
|
+
|
1155
|
+
GGML_ASSERT((size_t) n_outputs == out_ids.size());
|
1156
|
+
|
1157
|
+
// TODO: is there something more efficient which also minimizes swaps?
|
1158
|
+
// selection sort, to minimize swaps (from https://en.wikipedia.org/wiki/Selection_sort)
|
1159
|
+
for (uint32_t i = 0; i < n_outputs - 1; ++i) {
|
1160
|
+
uint32_t j_min = i;
|
1161
|
+
for (uint32_t j = i + 1; j < n_outputs; ++j) {
|
1162
|
+
if (out_ids[j] < out_ids[j_min]) {
|
1163
|
+
j_min = j;
|
1164
|
+
}
|
1165
|
+
}
|
1166
|
+
if (j_min == i) {
|
1167
|
+
continue;
|
1168
|
+
}
|
1169
|
+
std::swap(out_ids[i], out_ids[j_min]);
|
1170
|
+
if (logits_size > 0) {
|
1171
|
+
for (uint32_t k = 0; k < n_vocab; k++) {
|
1172
|
+
std::swap(logits[i*n_vocab + k], logits[j_min*n_vocab + k]);
|
1173
|
+
}
|
1174
|
+
}
|
1175
|
+
if (embd_size > 0) {
|
1176
|
+
for (uint32_t k = 0; k < n_embd; k++) {
|
1177
|
+
std::swap(embd[i*n_embd + k], embd[j_min*n_embd + k]);
|
1178
|
+
}
|
1179
|
+
}
|
1180
|
+
}
|
1181
|
+
|
1182
|
+
std::fill(output_ids.begin(), output_ids.end(), -1);
|
1183
|
+
|
1184
|
+
for (uint32_t i = 0; i < n_outputs; ++i) {
|
1185
|
+
output_ids[out_ids[i]] = i;
|
1186
|
+
}
|
1187
|
+
}
|
1188
|
+
}
|
1189
|
+
|
1190
|
+
// wait for the computation to finish (automatically done when obtaining the model output)
|
1191
|
+
//synchronize();
|
1192
|
+
|
1193
|
+
// Reset state for the next token before backend sync, to allow the CPU activities in the reset to
|
1194
|
+
// overlap with device computation.
|
1195
|
+
ggml_backend_sched_reset(sched.get());
|
1196
|
+
|
1197
|
+
return 0;
|
1198
|
+
}
|
1199
|
+
|
1200
|
+
//
|
1201
|
+
// output
|
1202
|
+
//
|
1203
|
+
|
1204
|
+
uint32_t llama_context::output_reserve(int32_t n_outputs) {
|
1205
|
+
const auto & hparams = model.hparams;
|
1206
|
+
const auto & vocab = model.vocab;
|
1207
|
+
|
1208
|
+
const int64_t n_outputs_max = std::max<int64_t>(n_outputs, n_seq_max());
|
1209
|
+
|
1210
|
+
const auto n_batch = cparams.n_batch;
|
1211
|
+
const auto n_vocab = vocab.n_tokens();
|
1212
|
+
const auto n_embd = hparams.n_embd;
|
1213
|
+
|
1214
|
+
bool has_logits = true;
|
1215
|
+
bool has_embd = cparams.embeddings;
|
1216
|
+
|
1217
|
+
// TODO: hacky enc-dec support
|
1218
|
+
if (model.arch == LLM_ARCH_T5) {
|
1219
|
+
has_logits = true;
|
1220
|
+
has_embd = true;
|
1221
|
+
}
|
1222
|
+
|
1223
|
+
logits_size = has_logits ? n_vocab*n_outputs_max : 0;
|
1224
|
+
embd_size = has_embd ? n_embd*n_outputs_max : 0;
|
1225
|
+
|
1226
|
+
if (output_ids.empty()) {
|
1227
|
+
// init, never resized afterwards
|
1228
|
+
output_ids.resize(n_batch);
|
1229
|
+
}
|
1230
|
+
|
1231
|
+
const size_t prev_size = buf_output ? ggml_backend_buffer_get_size(buf_output.get()) : 0;
|
1232
|
+
const size_t new_size = (logits_size + embd_size) * sizeof(float);
|
1233
|
+
|
1234
|
+
// alloc only when more than the current capacity is required
|
1235
|
+
// TODO: also consider shrinking the buffer
|
1236
|
+
if (!buf_output || prev_size < new_size) {
|
1237
|
+
if (buf_output) {
|
1238
|
+
#ifndef NDEBUG
|
1239
|
+
// This doesn't happen often, but may be annoying in some cases (like the HellaSwag benchmark)
|
1240
|
+
LLAMA_LOG_INFO("%s: reallocating output buffer from size %.02f MiB to %.02f MiB\n", __func__, prev_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
|
1241
|
+
#endif
|
1242
|
+
buf_output = nullptr;
|
1243
|
+
logits = nullptr;
|
1244
|
+
embd = nullptr;
|
1245
|
+
}
|
1246
|
+
|
1247
|
+
auto * buft = ggml_backend_cpu_buffer_type();
|
1248
|
+
// try to use the host buffer of the device where the output tensor is allocated for faster transfer to system memory
|
1249
|
+
auto * output_dev = model.dev_output();
|
1250
|
+
auto * output_dev_host_buft = output_dev ? ggml_backend_dev_host_buffer_type(output_dev) : nullptr;
|
1251
|
+
if (output_dev_host_buft) {
|
1252
|
+
buft = output_dev_host_buft;
|
1253
|
+
}
|
1254
|
+
buf_output.reset(ggml_backend_buft_alloc_buffer(buft, new_size));
|
1255
|
+
if (buf_output == nullptr) {
|
1256
|
+
LLAMA_LOG_ERROR("%s: failed to allocate output buffer of size %.2f MiB\n", __func__, new_size / (1024.0 * 1024.0));
|
1257
|
+
return 0;
|
1258
|
+
}
|
1259
|
+
}
|
1260
|
+
|
1261
|
+
float * output_base = (float *) ggml_backend_buffer_get_base(buf_output.get());
|
1262
|
+
|
1263
|
+
logits = has_logits ? output_base : nullptr;
|
1264
|
+
embd = has_embd ? output_base + logits_size : nullptr;
|
1265
|
+
|
1266
|
+
// set all ids as invalid (negative)
|
1267
|
+
std::fill(output_ids.begin(), output_ids.end(), -1);
|
1268
|
+
|
1269
|
+
this->n_outputs = 0;
|
1270
|
+
|
1271
|
+
return n_outputs_max;
|
1272
|
+
}
|
1273
|
+
|
1274
|
+
//
|
1275
|
+
// graph
|
1276
|
+
//
|
1277
|
+
|
1278
|
+
int32_t llama_context::graph_max_nodes() const {
|
1279
|
+
return std::max<int32_t>(65536, 5*model.n_tensors());
|
1280
|
+
}
|
1281
|
+
|
1282
|
+
ggml_cgraph * llama_context::graph_init() {
|
1283
|
+
ggml_init_params params = {
|
1284
|
+
/*.mem_size =*/ buf_compute_meta.size(),
|
1285
|
+
/*.mem_buffer =*/ buf_compute_meta.data(),
|
1286
|
+
/*.no_alloc =*/ true,
|
1287
|
+
};
|
1288
|
+
|
1289
|
+
ctx_compute.reset(ggml_init(params));
|
1290
|
+
|
1291
|
+
return ggml_new_graph_custom(ctx_compute.get(), graph_max_nodes(), false);
|
1292
|
+
}
|
1293
|
+
|
1294
|
+
ggml_cgraph * llama_context::graph_reserve(uint32_t n_tokens, uint32_t n_seqs, uint32_t n_outputs, const llama_memory_context_i * mctx) {
|
1295
|
+
LLAMA_LOG_DEBUG("%s: reserving a graph for ubatch with n_tokens = %4u, n_seqs = %2u, n_outputs = %4u\n", __func__, n_tokens, n_seqs, n_outputs);
|
1296
|
+
|
1297
|
+
if (n_tokens % n_seqs != 0) {
|
1298
|
+
n_tokens = ((n_tokens + (n_seqs - 1)) / n_seqs) * n_seqs; // round to next multiple of n_seqs
|
1299
|
+
n_outputs = std::min(n_outputs, n_tokens);
|
1300
|
+
|
1301
|
+
LLAMA_LOG_DEBUG("%s: making n_tokens a multiple of n_seqs - n_tokens = %u, n_seqs = %u, n_outputs = %u\n", __func__, n_tokens, n_seqs, n_outputs);
|
1302
|
+
}
|
1303
|
+
|
1304
|
+
// store the n_outputs as it is, and restore it afterwards
|
1305
|
+
// TODO: not sure if needed, might simplify in the future by removing this
|
1306
|
+
const auto save_n_outputs = this->n_outputs;
|
1307
|
+
|
1308
|
+
this->n_outputs = n_outputs;
|
1309
|
+
|
1310
|
+
llama_batch_allocr balloc(model.hparams.n_pos_per_embd());
|
1311
|
+
llama_ubatch ubatch = balloc.ubatch_reserve(n_tokens/n_seqs, n_seqs);
|
1312
|
+
|
1313
|
+
auto * gf = graph_init();
|
1314
|
+
auto res = graph_build(ctx_compute.get(), gf, ubatch, LLM_GRAPH_TYPE_DEFAULT, mctx);
|
1315
|
+
|
1316
|
+
this->n_outputs = save_n_outputs;
|
1317
|
+
|
1318
|
+
if (!res) {
|
1319
|
+
LLAMA_LOG_ERROR("%s: failed to build worst-case graph\n", __func__);
|
1320
|
+
return nullptr;
|
1321
|
+
}
|
1322
|
+
|
1323
|
+
ggml_backend_sched_reset(sched.get());
|
1324
|
+
|
1325
|
+
// initialize scheduler with the specified graph
|
1326
|
+
if (!ggml_backend_sched_reserve(sched.get(), gf)) {
|
1327
|
+
LLAMA_LOG_ERROR("%s: failed to allocate compute buffers\n", __func__);
|
1328
|
+
return nullptr;
|
1329
|
+
}
|
1330
|
+
|
1331
|
+
return gf;
|
1332
|
+
}
|
1333
|
+
|
1334
|
+
llm_graph_result_ptr llama_context::graph_build(
|
1335
|
+
ggml_context * ctx,
|
1336
|
+
ggml_cgraph * gf,
|
1337
|
+
const llama_ubatch & ubatch,
|
1338
|
+
llm_graph_type gtype,
|
1339
|
+
const llama_memory_context_i * mctx) {
|
1340
|
+
return model.build_graph(
|
1341
|
+
{
|
1342
|
+
/*.ctx =*/ ctx,
|
1343
|
+
/*.arch =*/ model.arch,
|
1344
|
+
/*.hparams =*/ model.hparams,
|
1345
|
+
/*.cparams =*/ cparams,
|
1346
|
+
/*.ubatch =*/ ubatch,
|
1347
|
+
/*.sched =*/ sched.get(),
|
1348
|
+
/*.backend_cpu =*/ backend_cpu,
|
1349
|
+
/*.cvec =*/ &cvec,
|
1350
|
+
/*.loras =*/ &loras,
|
1351
|
+
/*.mctx =*/ mctx,
|
1352
|
+
/*.cross =*/ &cross,
|
1353
|
+
/*.n_outputs =*/ n_outputs,
|
1354
|
+
/*.cb =*/ graph_get_cb(),
|
1355
|
+
}, gf, gtype);
|
1356
|
+
}
|
1357
|
+
|
1358
|
+
ggml_status llama_context::graph_compute(
|
1359
|
+
ggml_cgraph * gf,
|
1360
|
+
bool batched) {
|
1361
|
+
int n_threads = batched ? cparams.n_threads_batch : cparams.n_threads;
|
1362
|
+
ggml_threadpool_t tp = batched ? threadpool_batch : threadpool;
|
1363
|
+
|
1364
|
+
if (backend_cpu != nullptr) {
|
1365
|
+
auto * reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend_cpu));
|
1366
|
+
auto * set_threadpool_fn = (decltype(ggml_backend_cpu_set_threadpool) *) ggml_backend_reg_get_proc_address(reg, "ggml_backend_cpu_set_threadpool");
|
1367
|
+
set_threadpool_fn(backend_cpu, tp);
|
1368
|
+
}
|
1369
|
+
|
1370
|
+
// set the number of threads for all the backends
|
1371
|
+
for (const auto & set_n_threads_fn : set_n_threads_fns) {
|
1372
|
+
set_n_threads_fn.second(set_n_threads_fn.first, n_threads);
|
1373
|
+
}
|
1374
|
+
|
1375
|
+
auto status = ggml_backend_sched_graph_compute_async(sched.get(), gf);
|
1376
|
+
if (status != GGML_STATUS_SUCCESS) {
|
1377
|
+
LLAMA_LOG_ERROR("%s: ggml_backend_sched_graph_compute_async failed with error %d\n", __func__, status);
|
1378
|
+
}
|
1379
|
+
|
1380
|
+
// fprintf(stderr, "splits: %d\n", ggml_backend_sched_get_n_splits(sched));
|
1381
|
+
|
1382
|
+
return status;
|
1383
|
+
}
|
1384
|
+
|
1385
|
+
llm_graph_cb llama_context::graph_get_cb() const {
|
1386
|
+
return [&](const llama_ubatch & ubatch, ggml_tensor * cur, const char * name, int il) {
|
1387
|
+
if (il >= 0) {
|
1388
|
+
ggml_format_name(cur, "%s-%d", name, il);
|
1389
|
+
} else {
|
1390
|
+
ggml_set_name(cur, name);
|
1391
|
+
}
|
1392
|
+
|
1393
|
+
if (!cparams.offload_kqv) {
|
1394
|
+
if (strcmp(name, "kqv_merged_cont") == 0) {
|
1395
|
+
// all nodes between the KV store and the attention output are run on the CPU
|
1396
|
+
ggml_backend_sched_set_tensor_backend(sched.get(), cur, backend_cpu);
|
1397
|
+
}
|
1398
|
+
}
|
1399
|
+
|
1400
|
+
// norm may be automatically assigned to the backend of the previous layer, increasing data transfer between backends
|
1401
|
+
// FIXME: fix in ggml_backend_sched
|
1402
|
+
const bool full_offload = model.params.n_gpu_layers > (int) model.hparams.n_layer;
|
1403
|
+
if (ubatch.n_tokens < 32 || full_offload) {
|
1404
|
+
if (il != -1 && strcmp(name, "norm") == 0) {
|
1405
|
+
const auto & dev_layer = model.dev_layer(il);
|
1406
|
+
for (const auto & backend : backends) {
|
1407
|
+
if (ggml_backend_get_device(backend.get()) == dev_layer) {
|
1408
|
+
if (ggml_backend_supports_op(backend.get(), cur)) {
|
1409
|
+
ggml_backend_sched_set_tensor_backend(sched.get(), cur, backend.get());
|
1410
|
+
}
|
1411
|
+
}
|
1412
|
+
}
|
1413
|
+
}
|
1414
|
+
}
|
1415
|
+
};
|
1416
|
+
}
|
1417
|
+
|
1418
|
+
//
|
1419
|
+
// state save/load
|
1420
|
+
//
|
1421
|
+
|
1422
|
+
class llama_io_write_dummy : public llama_io_write_i {
|
1423
|
+
public:
|
1424
|
+
llama_io_write_dummy() = default;
|
1425
|
+
|
1426
|
+
void write(const void * /* src */, size_t size) override {
|
1427
|
+
size_written += size;
|
1428
|
+
}
|
1429
|
+
|
1430
|
+
void write_tensor(const ggml_tensor * /* tensor */, size_t /* offset */, size_t size) override {
|
1431
|
+
size_written += size;
|
1432
|
+
}
|
1433
|
+
|
1434
|
+
size_t n_bytes() override {
|
1435
|
+
return size_written;
|
1436
|
+
}
|
1437
|
+
|
1438
|
+
private:
|
1439
|
+
size_t size_written = 0;
|
1440
|
+
};
|
1441
|
+
|
1442
|
+
class llama_io_write_buffer : public llama_io_write_i {
|
1443
|
+
public:
|
1444
|
+
llama_io_write_buffer(
|
1445
|
+
uint8_t * p, size_t len) : ptr(p), buf_size(len) {}
|
1446
|
+
|
1447
|
+
void write(const void * src, size_t size) override {
|
1448
|
+
if (size > buf_size) {
|
1449
|
+
throw std::runtime_error("unexpectedly reached end of buffer");
|
1450
|
+
}
|
1451
|
+
memcpy(ptr, src, size);
|
1452
|
+
ptr += size;
|
1453
|
+
size_written += size;
|
1454
|
+
buf_size -= size;
|
1455
|
+
}
|
1456
|
+
|
1457
|
+
void write_tensor(const ggml_tensor * tensor, size_t offset, size_t size) override {
|
1458
|
+
if (size > buf_size) {
|
1459
|
+
throw std::runtime_error("unexpectedly reached end of buffer");
|
1460
|
+
}
|
1461
|
+
ggml_backend_tensor_get(tensor, ptr, offset, size);
|
1462
|
+
ptr += size;
|
1463
|
+
size_written += size;
|
1464
|
+
buf_size -= size;
|
1465
|
+
}
|
1466
|
+
|
1467
|
+
size_t n_bytes() override {
|
1468
|
+
return size_written;
|
1469
|
+
}
|
1470
|
+
|
1471
|
+
private:
|
1472
|
+
uint8_t * ptr;
|
1473
|
+
size_t buf_size = 0;
|
1474
|
+
size_t size_written = 0;
|
1475
|
+
};
|
1476
|
+
|
1477
|
+
class llama_io_read_buffer : public llama_io_read_i {
|
1478
|
+
public:
|
1479
|
+
llama_io_read_buffer(const uint8_t * p, size_t len) : ptr(p), buf_size(len) {}
|
1480
|
+
|
1481
|
+
const uint8_t * read(size_t size) override {
|
1482
|
+
const uint8_t * base_ptr = ptr;
|
1483
|
+
if (size > buf_size) {
|
1484
|
+
throw std::runtime_error("unexpectedly reached end of buffer");
|
1485
|
+
}
|
1486
|
+
ptr += size;
|
1487
|
+
size_read += size;
|
1488
|
+
buf_size -= size;
|
1489
|
+
return base_ptr;
|
1490
|
+
}
|
1491
|
+
|
1492
|
+
void read_to(void * dst, size_t size) override {
|
1493
|
+
memcpy(dst, read(size), size);
|
1494
|
+
}
|
1495
|
+
|
1496
|
+
size_t n_bytes() override {
|
1497
|
+
return size_read;
|
1498
|
+
}
|
1499
|
+
|
1500
|
+
private:
|
1501
|
+
const uint8_t * ptr;
|
1502
|
+
size_t buf_size = 0;
|
1503
|
+
size_t size_read = 0;
|
1504
|
+
};
|
1505
|
+
|
1506
|
+
class llama_io_write_file : public llama_io_write_i {
|
1507
|
+
public:
|
1508
|
+
llama_io_write_file(llama_file * f) : file(f) {}
|
1509
|
+
|
1510
|
+
void write(const void * src, size_t size) override {
|
1511
|
+
file->write_raw(src, size);
|
1512
|
+
size_written += size;
|
1513
|
+
}
|
1514
|
+
|
1515
|
+
void write_tensor(const ggml_tensor * tensor, size_t offset, size_t size) override {
|
1516
|
+
temp_buffer.resize(size);
|
1517
|
+
ggml_backend_tensor_get(tensor, temp_buffer.data(), offset, size);
|
1518
|
+
write(temp_buffer.data(), temp_buffer.size());
|
1519
|
+
}
|
1520
|
+
|
1521
|
+
size_t n_bytes() override {
|
1522
|
+
return size_written;
|
1523
|
+
}
|
1524
|
+
|
1525
|
+
private:
|
1526
|
+
llama_file * file;
|
1527
|
+
size_t size_written = 0;
|
1528
|
+
std::vector<uint8_t> temp_buffer;
|
1529
|
+
};
|
1530
|
+
|
1531
|
+
class llama_io_read_file : public llama_io_read_i {
|
1532
|
+
public:
|
1533
|
+
llama_io_read_file(llama_file * f) : file(f) {}
|
1534
|
+
|
1535
|
+
void read_to(void * dst, size_t size) override {
|
1536
|
+
file->read_raw(dst, size);
|
1537
|
+
size_read += size;
|
1538
|
+
}
|
1539
|
+
|
1540
|
+
const uint8_t * read(size_t size) override {
|
1541
|
+
temp_buffer.resize(size);
|
1542
|
+
read_to(temp_buffer.data(), size);
|
1543
|
+
return temp_buffer.data();
|
1544
|
+
}
|
1545
|
+
|
1546
|
+
size_t n_bytes() override {
|
1547
|
+
return size_read;
|
1548
|
+
}
|
1549
|
+
|
1550
|
+
private:
|
1551
|
+
llama_file * file;
|
1552
|
+
size_t size_read = 0;
|
1553
|
+
std::vector<uint8_t> temp_buffer;
|
1554
|
+
};
|
1555
|
+
|
1556
|
+
size_t llama_context::state_get_size() {
|
1557
|
+
llama_io_write_dummy io;
|
1558
|
+
try {
|
1559
|
+
return state_write_data(io);
|
1560
|
+
} catch (const std::exception & err) {
|
1561
|
+
LLAMA_LOG_ERROR("%s: error getting state size: %s\n", __func__, err.what());
|
1562
|
+
return 0;
|
1563
|
+
}
|
1564
|
+
}
|
1565
|
+
|
1566
|
+
size_t llama_context::state_get_data(uint8_t * dst, size_t size) {
|
1567
|
+
llama_io_write_buffer io(dst, size);
|
1568
|
+
try {
|
1569
|
+
return state_write_data(io);
|
1570
|
+
} catch (const std::exception & err) {
|
1571
|
+
LLAMA_LOG_ERROR("%s: error saving state: %s\n", __func__, err.what());
|
1572
|
+
return 0;
|
1573
|
+
}
|
1574
|
+
}
|
1575
|
+
|
1576
|
+
size_t llama_context::state_set_data(const uint8_t * src, size_t size) {
|
1577
|
+
llama_io_read_buffer io(src, size);
|
1578
|
+
try {
|
1579
|
+
return state_read_data(io);
|
1580
|
+
} catch (const std::exception & err) {
|
1581
|
+
LLAMA_LOG_ERROR("%s: error loading state: %s\n", __func__, err.what());
|
1582
|
+
return 0;
|
1583
|
+
}
|
1584
|
+
}
|
1585
|
+
|
1586
|
+
size_t llama_context::state_seq_get_size(llama_seq_id seq_id) {
|
1587
|
+
llama_io_write_dummy io;
|
1588
|
+
try {
|
1589
|
+
return state_seq_write_data(io, seq_id);
|
1590
|
+
} catch (const std::exception & err) {
|
1591
|
+
LLAMA_LOG_ERROR("%s: error getting state size: %s\n", __func__, err.what());
|
1592
|
+
return 0;
|
1593
|
+
}
|
1594
|
+
}
|
1595
|
+
|
1596
|
+
size_t llama_context::state_seq_get_data(llama_seq_id seq_id, uint8_t * dst, size_t size) {
|
1597
|
+
llama_io_write_buffer io(dst, size);
|
1598
|
+
try {
|
1599
|
+
return state_seq_write_data(io, seq_id);
|
1600
|
+
} catch (const std::exception & err) {
|
1601
|
+
LLAMA_LOG_ERROR("%s: error saving state: %s\n", __func__, err.what());
|
1602
|
+
return 0;
|
1603
|
+
}
|
1604
|
+
}
|
1605
|
+
|
1606
|
+
size_t llama_context::state_seq_set_data(llama_seq_id seq_id, const uint8_t * src, size_t size) {
|
1607
|
+
llama_io_read_buffer io(src, size);
|
1608
|
+
try {
|
1609
|
+
return state_seq_read_data(io, seq_id);
|
1610
|
+
} catch (const std::exception & err) {
|
1611
|
+
LLAMA_LOG_ERROR("%s: error loading state: %s\n", __func__, err.what());
|
1612
|
+
return 0;
|
1613
|
+
}
|
1614
|
+
}
|
1615
|
+
|
1616
|
+
bool llama_context::state_load_file(const char * filepath, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
|
1617
|
+
llama_file file(filepath, "rb");
|
1618
|
+
|
1619
|
+
// sanity checks
|
1620
|
+
{
|
1621
|
+
const uint32_t magic = file.read_u32();
|
1622
|
+
const uint32_t version = file.read_u32();
|
1623
|
+
|
1624
|
+
if (magic != LLAMA_SESSION_MAGIC || version != LLAMA_SESSION_VERSION) {
|
1625
|
+
LLAMA_LOG_ERROR("%s: unknown (magic, version) for session file: %08x, %08x\n", __func__, magic, version);
|
1626
|
+
return false;
|
1627
|
+
}
|
1628
|
+
}
|
1629
|
+
|
1630
|
+
// load the prompt
|
1631
|
+
{
|
1632
|
+
const uint32_t n_token_count = file.read_u32();
|
1633
|
+
|
1634
|
+
if (n_token_count > n_token_capacity) {
|
1635
|
+
LLAMA_LOG_ERROR("%s: token count in session file exceeded capacity! %u > %zu\n", __func__, n_token_count, n_token_capacity);
|
1636
|
+
return false;
|
1637
|
+
}
|
1638
|
+
|
1639
|
+
file.read_raw(tokens_out, sizeof(llama_token) * n_token_count);
|
1640
|
+
*n_token_count_out = n_token_count;
|
1641
|
+
}
|
1642
|
+
|
1643
|
+
// restore the context state
|
1644
|
+
{
|
1645
|
+
const size_t n_state_size_cur = file.size() - file.tell();
|
1646
|
+
|
1647
|
+
llama_io_read_file io( &file);
|
1648
|
+
const size_t n_read = state_read_data(io);
|
1649
|
+
|
1650
|
+
if (n_read != n_state_size_cur) {
|
1651
|
+
LLAMA_LOG_ERROR("%s: did not read all of the session file data! size %zu, got %zu\n", __func__, n_state_size_cur, n_read);
|
1652
|
+
return false;
|
1653
|
+
}
|
1654
|
+
}
|
1655
|
+
|
1656
|
+
return true;
|
1657
|
+
}
|
1658
|
+
|
1659
|
+
bool llama_context::state_save_file(const char * filepath, const llama_token * tokens, size_t n_token_count) {
|
1660
|
+
llama_file file(filepath, "wb");
|
1661
|
+
|
1662
|
+
file.write_u32(LLAMA_SESSION_MAGIC);
|
1663
|
+
file.write_u32(LLAMA_SESSION_VERSION);
|
1664
|
+
|
1665
|
+
// save the prompt
|
1666
|
+
file.write_u32((uint32_t) n_token_count);
|
1667
|
+
file.write_raw(tokens, sizeof(llama_token) * n_token_count);
|
1668
|
+
|
1669
|
+
// save the context state using stream saving
|
1670
|
+
llama_io_write_file io(&file);
|
1671
|
+
state_write_data(io);
|
1672
|
+
|
1673
|
+
return true;
|
1674
|
+
}
|
1675
|
+
|
1676
|
+
size_t llama_context::state_seq_load_file(llama_seq_id seq_id, const char * filepath, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
|
1677
|
+
llama_file file(filepath, "rb");
|
1678
|
+
|
1679
|
+
// version checks
|
1680
|
+
{
|
1681
|
+
const uint32_t magic = file.read_u32();
|
1682
|
+
const uint32_t version = file.read_u32();
|
1683
|
+
|
1684
|
+
if (magic != LLAMA_STATE_SEQ_MAGIC || version != LLAMA_STATE_SEQ_VERSION) {
|
1685
|
+
LLAMA_LOG_ERROR("%s: unknown (magic, version) for sequence state file: %08x, %08x\n", __func__, magic, version);
|
1686
|
+
return 0;
|
1687
|
+
}
|
1688
|
+
}
|
1689
|
+
|
1690
|
+
// load the prompt
|
1691
|
+
{
|
1692
|
+
const uint32_t n_token_count = file.read_u32();
|
1693
|
+
|
1694
|
+
if (n_token_count > n_token_capacity) {
|
1695
|
+
LLAMA_LOG_ERROR("%s: token count in sequence state file exceeded capacity! %u > %zu\n", __func__, n_token_count, n_token_capacity);
|
1696
|
+
return 0;
|
1697
|
+
}
|
1698
|
+
|
1699
|
+
file.read_raw(tokens_out, sizeof(llama_token) * n_token_count);
|
1700
|
+
*n_token_count_out = n_token_count;
|
1701
|
+
}
|
1702
|
+
|
1703
|
+
// restore the context state
|
1704
|
+
{
|
1705
|
+
const size_t state_size = file.size() - file.tell();
|
1706
|
+
llama_io_read_file io(&file);
|
1707
|
+
const size_t nread = state_seq_read_data(io, seq_id);
|
1708
|
+
if (!nread) {
|
1709
|
+
LLAMA_LOG_ERROR("%s: failed to restore sequence state\n", __func__);
|
1710
|
+
return 0;
|
1711
|
+
}
|
1712
|
+
GGML_ASSERT(nread <= state_size);
|
1713
|
+
GGML_ASSERT(nread + sizeof(uint32_t) * 3 + sizeof(llama_token) * *n_token_count_out == file.tell());
|
1714
|
+
}
|
1715
|
+
|
1716
|
+
return file.tell();
|
1717
|
+
}
|
1718
|
+
|
1719
|
+
size_t llama_context::state_seq_save_file(llama_seq_id seq_id, const char * filepath, const llama_token * tokens, size_t n_token_count) {
|
1720
|
+
llama_file file(filepath, "wb");
|
1721
|
+
|
1722
|
+
file.write_u32(LLAMA_STATE_SEQ_MAGIC);
|
1723
|
+
file.write_u32(LLAMA_STATE_SEQ_VERSION);
|
1724
|
+
|
1725
|
+
// save the prompt
|
1726
|
+
file.write_u32((uint32_t) n_token_count);
|
1727
|
+
file.write_raw(tokens, sizeof(llama_token) * n_token_count);
|
1728
|
+
|
1729
|
+
// save the context state using stream saving
|
1730
|
+
llama_io_write_file io(&file);
|
1731
|
+
state_seq_write_data(io, seq_id);
|
1732
|
+
|
1733
|
+
const size_t res = file.tell();
|
1734
|
+
GGML_ASSERT(res == sizeof(uint32_t) * 3 + sizeof(llama_token) * n_token_count + io.n_bytes());
|
1735
|
+
|
1736
|
+
return res;
|
1737
|
+
}
|
1738
|
+
|
1739
|
+
size_t llama_context::state_write_data(llama_io_write_i & io) {
|
1740
|
+
LLAMA_LOG_DEBUG("%s: writing state\n", __func__);
|
1741
|
+
|
1742
|
+
// write model info
|
1743
|
+
{
|
1744
|
+
LLAMA_LOG_DEBUG("%s: - writing model info\n", __func__);
|
1745
|
+
|
1746
|
+
const std::string arch_str = llm_arch_name(model.arch);
|
1747
|
+
io.write_string(arch_str);
|
1748
|
+
// TODO: add more model-specific info which should prevent loading the session file if not identical
|
1749
|
+
}
|
1750
|
+
|
1751
|
+
// write output ids
|
1752
|
+
{
|
1753
|
+
LLAMA_LOG_DEBUG("%s: - writing output ids\n", __func__);
|
1754
|
+
|
1755
|
+
const auto n_outputs = this->n_outputs;
|
1756
|
+
const auto & output_ids = this->output_ids;
|
1757
|
+
|
1758
|
+
std::vector<int32_t> w_output_pos;
|
1759
|
+
|
1760
|
+
w_output_pos.resize(n_outputs);
|
1761
|
+
|
1762
|
+
// build a more compact representation of the output ids
|
1763
|
+
for (size_t i = 0; i < n_batch(); ++i) {
|
1764
|
+
// map an output id to a position in the batch
|
1765
|
+
int64_t pos = output_ids[i];
|
1766
|
+
if (pos >= 0) {
|
1767
|
+
GGML_ASSERT(pos < n_outputs);
|
1768
|
+
w_output_pos[pos] = i;
|
1769
|
+
}
|
1770
|
+
}
|
1771
|
+
|
1772
|
+
io.write(&n_outputs, sizeof(n_outputs));
|
1773
|
+
|
1774
|
+
if (n_outputs) {
|
1775
|
+
io.write(w_output_pos.data(), n_outputs * sizeof(int32_t));
|
1776
|
+
}
|
1777
|
+
}
|
1778
|
+
|
1779
|
+
// write logits
|
1780
|
+
{
|
1781
|
+
LLAMA_LOG_DEBUG("%s: - writing logits\n", __func__);
|
1782
|
+
|
1783
|
+
const uint64_t logits_size = std::min((uint64_t) this->logits_size, (uint64_t) n_outputs * model.vocab.n_tokens());
|
1784
|
+
|
1785
|
+
io.write(&logits_size, sizeof(logits_size));
|
1786
|
+
|
1787
|
+
if (logits_size) {
|
1788
|
+
io.write(logits, logits_size * sizeof(float));
|
1789
|
+
}
|
1790
|
+
}
|
1791
|
+
|
1792
|
+
// write embeddings
|
1793
|
+
{
|
1794
|
+
LLAMA_LOG_DEBUG("%s: - writing embeddings\n", __func__);
|
1795
|
+
|
1796
|
+
const uint64_t embd_size = std::min((uint64_t) this->embd_size, (uint64_t) n_outputs * model.hparams.n_embd);
|
1797
|
+
|
1798
|
+
io.write(&embd_size, sizeof(embd_size));
|
1799
|
+
|
1800
|
+
if (embd_size) {
|
1801
|
+
io.write(embd, embd_size * sizeof(float));
|
1802
|
+
}
|
1803
|
+
}
|
1804
|
+
|
1805
|
+
if (memory != nullptr) {
|
1806
|
+
LLAMA_LOG_DEBUG("%s: - writing KV self\n", __func__);
|
1807
|
+
memory->state_write(io);
|
1808
|
+
}
|
1809
|
+
|
1810
|
+
return io.n_bytes();
|
1811
|
+
}
|
1812
|
+
|
1813
|
+
size_t llama_context::state_read_data(llama_io_read_i & io) {
|
1814
|
+
LLAMA_LOG_DEBUG("%s: reading state\n", __func__);
|
1815
|
+
|
1816
|
+
// read model info
|
1817
|
+
{
|
1818
|
+
LLAMA_LOG_DEBUG("%s: - reading model info\n", __func__);
|
1819
|
+
|
1820
|
+
const std::string cur_arch_str = llm_arch_name(model.arch);
|
1821
|
+
|
1822
|
+
std::string arch_str;
|
1823
|
+
io.read_string(arch_str);
|
1824
|
+
if (cur_arch_str != arch_str) {
|
1825
|
+
throw std::runtime_error(format("wrong model arch: '%s' instead of '%s'", arch_str.c_str(), cur_arch_str.c_str()));
|
1826
|
+
}
|
1827
|
+
// TODO: add more info which needs to be identical but which is not verified otherwise
|
1828
|
+
}
|
1829
|
+
|
1830
|
+
// read output ids
|
1831
|
+
{
|
1832
|
+
LLAMA_LOG_DEBUG("%s: - reading output ids\n", __func__);
|
1833
|
+
|
1834
|
+
auto n_outputs = this->n_outputs;
|
1835
|
+
io.read_to(&n_outputs, sizeof(n_outputs));
|
1836
|
+
|
1837
|
+
if (n_outputs > output_reserve(n_outputs)) {
|
1838
|
+
throw std::runtime_error("could not reserve outputs");
|
1839
|
+
}
|
1840
|
+
|
1841
|
+
std::vector<int32_t> output_pos;
|
1842
|
+
|
1843
|
+
if (n_outputs) {
|
1844
|
+
output_pos.resize(n_outputs);
|
1845
|
+
io.read_to(output_pos.data(), n_outputs * sizeof(int32_t));
|
1846
|
+
|
1847
|
+
for (int32_t i = 0; i < (int32_t) output_pos.size(); ++i) {
|
1848
|
+
int32_t id = output_pos[i];
|
1849
|
+
if ((uint32_t) id >= n_batch()) {
|
1850
|
+
throw std::runtime_error(format("invalid output id, %d does not fit in batch size of %u", id, n_batch()));
|
1851
|
+
}
|
1852
|
+
this->output_ids[id] = i;
|
1853
|
+
}
|
1854
|
+
|
1855
|
+
this->n_outputs = n_outputs;
|
1856
|
+
}
|
1857
|
+
}
|
1858
|
+
|
1859
|
+
// read logits
|
1860
|
+
{
|
1861
|
+
LLAMA_LOG_DEBUG("%s: - reading logits\n", __func__);
|
1862
|
+
|
1863
|
+
uint64_t logits_size;
|
1864
|
+
io.read_to(&logits_size, sizeof(logits_size));
|
1865
|
+
|
1866
|
+
if (this->logits_size < logits_size) {
|
1867
|
+
throw std::runtime_error("logits buffer too small");
|
1868
|
+
}
|
1869
|
+
|
1870
|
+
if (logits_size) {
|
1871
|
+
io.read_to(this->logits, logits_size * sizeof(float));
|
1872
|
+
}
|
1873
|
+
}
|
1874
|
+
|
1875
|
+
// read embeddings
|
1876
|
+
{
|
1877
|
+
LLAMA_LOG_DEBUG("%s: - reading embeddings\n", __func__);
|
1878
|
+
|
1879
|
+
uint64_t embd_size;
|
1880
|
+
io.read_to(&embd_size, sizeof(embd_size));
|
1881
|
+
|
1882
|
+
if (this->embd_size < embd_size) {
|
1883
|
+
throw std::runtime_error("embeddings buffer too small");
|
1884
|
+
}
|
1885
|
+
|
1886
|
+
if (embd_size) {
|
1887
|
+
io.read_to(this->embd, embd_size * sizeof(float));
|
1888
|
+
}
|
1889
|
+
}
|
1890
|
+
|
1891
|
+
if (memory) {
|
1892
|
+
LLAMA_LOG_DEBUG("%s: - reading KV self\n", __func__);
|
1893
|
+
|
1894
|
+
memory->state_read(io);
|
1895
|
+
}
|
1896
|
+
|
1897
|
+
return io.n_bytes();
|
1898
|
+
}
|
1899
|
+
|
1900
|
+
size_t llama_context::state_seq_write_data(llama_io_write_i & io, llama_seq_id seq_id) {
|
1901
|
+
GGML_UNUSED(seq_id);
|
1902
|
+
|
1903
|
+
if (memory) {
|
1904
|
+
memory->state_write(io, seq_id);
|
1905
|
+
}
|
1906
|
+
|
1907
|
+
return io.n_bytes();
|
1908
|
+
}
|
1909
|
+
|
1910
|
+
size_t llama_context::state_seq_read_data(llama_io_read_i & io, llama_seq_id seq_id) {
|
1911
|
+
GGML_UNUSED(seq_id);
|
1912
|
+
|
1913
|
+
if (memory) {
|
1914
|
+
memory->state_read(io, seq_id);
|
1915
|
+
}
|
1916
|
+
|
1917
|
+
return io.n_bytes();
|
1918
|
+
}
|
1919
|
+
|
1920
|
+
//
|
1921
|
+
// perf
|
1922
|
+
//
|
1923
|
+
|
1924
|
+
llama_perf_context_data llama_context::perf_get_data() const {
|
1925
|
+
llama_perf_context_data data = {};
|
1926
|
+
|
1927
|
+
data.t_start_ms = 1e-3 * t_start_us;
|
1928
|
+
data.t_load_ms = 1e-3 * t_load_us;
|
1929
|
+
data.t_p_eval_ms = 1e-3 * t_p_eval_us;
|
1930
|
+
data.t_eval_ms = 1e-3 * t_eval_us;
|
1931
|
+
data.n_p_eval = std::max(1, n_p_eval);
|
1932
|
+
data.n_eval = std::max(1, n_eval);
|
1933
|
+
|
1934
|
+
return data;
|
1935
|
+
}
|
1936
|
+
|
1937
|
+
void llama_context::perf_reset() {
|
1938
|
+
t_start_us = ggml_time_us();
|
1939
|
+
t_eval_us = n_eval = 0;
|
1940
|
+
t_p_eval_us = n_p_eval = 0;
|
1941
|
+
}
|
1942
|
+
|
1943
|
+
//
|
1944
|
+
// training
|
1945
|
+
//
|
1946
|
+
|
1947
|
+
static void llama_set_param(struct ggml_tensor * tensor, llama_opt_param_filter param_filter, void * userdata) {
|
1948
|
+
if (!tensor || tensor->type != GGML_TYPE_F32) {
|
1949
|
+
return;
|
1950
|
+
}
|
1951
|
+
if (!param_filter(tensor, userdata)) {
|
1952
|
+
return;
|
1953
|
+
}
|
1954
|
+
if (strcmp(tensor->name, "token_embd.weight") == 0) {
|
1955
|
+
return; // FIXME
|
1956
|
+
}
|
1957
|
+
if (strcmp(tensor->name, "rope_freqs.weight") == 0) {
|
1958
|
+
return; // FIXME
|
1959
|
+
}
|
1960
|
+
ggml_set_param(tensor);
|
1961
|
+
}
|
1962
|
+
|
1963
|
+
void llama_context::opt_init(struct llama_model * model, struct llama_opt_params lopt_params) {
|
1964
|
+
GGML_ASSERT(!opt_ctx);
|
1965
|
+
model->hparams.n_ctx_train = lopt_params.n_ctx_train > 0 ? lopt_params.n_ctx_train : n_ctx();
|
1966
|
+
const uint32_t n_batch = std::min(this->n_batch(), model->hparams.n_ctx_train);
|
1967
|
+
const uint32_t n_ubatch = std::min(this->n_ubatch(), n_batch);
|
1968
|
+
GGML_ASSERT(model->hparams.n_ctx_train % n_batch == 0);
|
1969
|
+
GGML_ASSERT(n_batch % n_ubatch == 0);
|
1970
|
+
|
1971
|
+
ggml_opt_params opt_params = ggml_opt_default_params(sched.get(), GGML_OPT_LOSS_TYPE_CROSS_ENTROPY);
|
1972
|
+
opt_params.opt_period = n_batch / n_ubatch;
|
1973
|
+
opt_params.get_opt_pars = lopt_params.get_opt_pars;
|
1974
|
+
opt_params.get_opt_pars_ud = lopt_params.get_opt_pars_ud;
|
1975
|
+
|
1976
|
+
opt_ctx = ggml_opt_init(opt_params);
|
1977
|
+
|
1978
|
+
llama_opt_param_filter param_filter = lopt_params.param_filter;
|
1979
|
+
void * param_filter_ud = lopt_params.param_filter_ud;
|
1980
|
+
|
1981
|
+
//llama_set_param(model->tok_embd, param_filter, param_filter_ud); // FIXME
|
1982
|
+
llama_set_param(model->type_embd, param_filter, param_filter_ud);
|
1983
|
+
llama_set_param(model->pos_embd, param_filter, param_filter_ud);
|
1984
|
+
llama_set_param(model->tok_norm, param_filter, param_filter_ud);
|
1985
|
+
llama_set_param(model->tok_norm_b, param_filter, param_filter_ud);
|
1986
|
+
llama_set_param(model->output_norm, param_filter, param_filter_ud);
|
1987
|
+
llama_set_param(model->output_norm_b, param_filter, param_filter_ud);
|
1988
|
+
llama_set_param(model->output, param_filter, param_filter_ud);
|
1989
|
+
llama_set_param(model->output_b, param_filter, param_filter_ud);
|
1990
|
+
llama_set_param(model->output_norm_enc, param_filter, param_filter_ud);
|
1991
|
+
llama_set_param(model->cls, param_filter, param_filter_ud);
|
1992
|
+
llama_set_param(model->cls_b, param_filter, param_filter_ud);
|
1993
|
+
llama_set_param(model->cls_out, param_filter, param_filter_ud);
|
1994
|
+
llama_set_param(model->cls_out_b, param_filter, param_filter_ud);
|
1995
|
+
|
1996
|
+
for (struct llama_layer & layer : model->layers) {
|
1997
|
+
for (size_t i = 0; i < sizeof(layer)/sizeof(struct ggml_tensor *); ++i) {
|
1998
|
+
llama_set_param(reinterpret_cast<struct ggml_tensor **>(&layer)[i], param_filter, param_filter_ud);
|
1999
|
+
}
|
2000
|
+
}
|
2001
|
+
}
|
2002
|
+
|
2003
|
+
void llama_context::opt_epoch_iter(
|
2004
|
+
ggml_opt_dataset_t dataset,
|
2005
|
+
ggml_opt_result_t result,
|
2006
|
+
const std::vector<llama_token> & tokens,
|
2007
|
+
const std::vector<llama_token> & labels_sparse,
|
2008
|
+
llama_batch & batch,
|
2009
|
+
ggml_opt_epoch_callback callback,
|
2010
|
+
bool train,
|
2011
|
+
int64_t idata_in_loop,
|
2012
|
+
int64_t ndata_in_loop,
|
2013
|
+
int64_t t_loop_start) {
|
2014
|
+
GGML_ASSERT(opt_ctx);
|
2015
|
+
const uint32_t n_ctx = llama_model_n_ctx_train(&model);
|
2016
|
+
const uint32_t n_batch = std::min(this->n_batch(), n_ctx);
|
2017
|
+
const uint32_t n_ubatch = std::min(this->n_ubatch(), n_batch);
|
2018
|
+
|
2019
|
+
memory->clear(true);
|
2020
|
+
|
2021
|
+
for (uint32_t pos_ctx = 0; pos_ctx < n_ctx; pos_ctx += n_batch) {
|
2022
|
+
batch.n_tokens = n_batch;
|
2023
|
+
for (uint32_t pos_batch = 0; pos_batch < n_batch; ++pos_batch) {
|
2024
|
+
batch.token [pos_batch] = tokens[pos_ctx + pos_batch];
|
2025
|
+
batch.pos [pos_batch] = pos_ctx + pos_batch;
|
2026
|
+
batch.n_seq_id[pos_batch] = 1;
|
2027
|
+
batch.seq_id [pos_batch][0] = 0;
|
2028
|
+
batch.logits [pos_batch] = true;
|
2029
|
+
}
|
2030
|
+
|
2031
|
+
if (!balloc->init(batch, model.vocab, nullptr, model.hparams.n_embd, true)) {
|
2032
|
+
LLAMA_LOG_ERROR("%s: failed to initialize batch\n", __func__);
|
2033
|
+
return;
|
2034
|
+
}
|
2035
|
+
|
2036
|
+
const uint32_t n_tokens_all = balloc->get_n_tokens();
|
2037
|
+
|
2038
|
+
n_queued_tokens += n_tokens_all;
|
2039
|
+
|
2040
|
+
embd_seq.clear();
|
2041
|
+
|
2042
|
+
uint32_t n_outputs_all = n_tokens_all;
|
2043
|
+
|
2044
|
+
auto mctx = memory->init_batch(*balloc, cparams.n_ubatch, true);
|
2045
|
+
if (!mctx || mctx->get_status() != LLAMA_MEMORY_STATUS_SUCCESS) {
|
2046
|
+
LLAMA_LOG_ERROR("%s: could not initialize batch\n", __func__);
|
2047
|
+
break;
|
2048
|
+
}
|
2049
|
+
|
2050
|
+
// reserve output buffer
|
2051
|
+
if (output_reserve(n_outputs_all) < n_outputs_all) {
|
2052
|
+
LLAMA_LOG_ERROR("%s: could not reserve space for batch with %d outputs\n", __func__, n_outputs_all);
|
2053
|
+
GGML_ABORT("TODO: handle this error");
|
2054
|
+
};
|
2055
|
+
|
2056
|
+
uint32_t pos_batch = 0;
|
2057
|
+
do {
|
2058
|
+
const auto & ubatch = mctx->get_ubatch();
|
2059
|
+
|
2060
|
+
n_outputs = ubatch.n_tokens;
|
2061
|
+
|
2062
|
+
if (!mctx->apply()) {
|
2063
|
+
LLAMA_LOG_ERROR("%s: failed to update the memory context\n", __func__);
|
2064
|
+
break;
|
2065
|
+
}
|
2066
|
+
|
2067
|
+
auto * gf = graph_init();
|
2068
|
+
auto res = graph_build(ctx_compute.get(), gf, ubatch, LLM_GRAPH_TYPE_DEFAULT, mctx.get());
|
2069
|
+
|
2070
|
+
struct ggml_context * ctx_compute_opt;
|
2071
|
+
{
|
2072
|
+
const size_t size_gf = ggml_graph_size(gf);
|
2073
|
+
const size_t size_meta = 4*size_gf*ggml_tensor_overhead() + 2*ggml_graph_overhead_custom(size_gf, /*grads = */ true);
|
2074
|
+
struct ggml_init_params params = {
|
2075
|
+
/*.mem_size =*/ size_meta,
|
2076
|
+
/*.mem_buffer =*/ nullptr,
|
2077
|
+
/*.no_alloc =*/ true,
|
2078
|
+
};
|
2079
|
+
ctx_compute_opt = ggml_init(params);
|
2080
|
+
}
|
2081
|
+
ggml_opt_prepare_alloc(opt_ctx, ctx_compute_opt, gf, res->get_tokens(), res->get_logits());
|
2082
|
+
ggml_opt_alloc(opt_ctx, train);
|
2083
|
+
|
2084
|
+
res->set_inputs(&ubatch);
|
2085
|
+
{
|
2086
|
+
struct ggml_tensor * labels = ggml_opt_labels(opt_ctx);
|
2087
|
+
GGML_ASSERT(labels->ne[1] == n_ubatch);
|
2088
|
+
ggml_set_zero(labels);
|
2089
|
+
const float onef = 1.0f;
|
2090
|
+
for (uint32_t pos_ubatch = 0; pos_ubatch < n_ubatch; ++pos_ubatch) {
|
2091
|
+
const uint32_t ilabel = pos_ctx + pos_batch + pos_ubatch;
|
2092
|
+
GGML_ASSERT(labels_sparse[ilabel] < labels->ne[0]);
|
2093
|
+
ggml_backend_tensor_set(labels, &onef, (pos_ubatch*labels->ne[0] + labels_sparse[ilabel])*sizeof(float), sizeof(float));
|
2094
|
+
}
|
2095
|
+
}
|
2096
|
+
ggml_opt_eval(opt_ctx, result);
|
2097
|
+
if (callback) {
|
2098
|
+
callback(train, opt_ctx, dataset, result, idata_in_loop + (pos_ctx + pos_batch)/n_ubatch + 1, ndata_in_loop, t_loop_start);
|
2099
|
+
}
|
2100
|
+
ggml_free(ctx_compute_opt);
|
2101
|
+
|
2102
|
+
pos_batch += ubatch.n_tokens;
|
2103
|
+
} while (mctx->next());
|
2104
|
+
}
|
2105
|
+
}
|
2106
|
+
|
2107
|
+
void llama_context::opt_epoch(
|
2108
|
+
ggml_opt_dataset_t dataset,
|
2109
|
+
ggml_opt_result_t result_train,
|
2110
|
+
ggml_opt_result_t result_eval,
|
2111
|
+
int64_t idata_split,
|
2112
|
+
ggml_opt_epoch_callback callback_train,
|
2113
|
+
ggml_opt_epoch_callback callback_eval) {
|
2114
|
+
const uint32_t n_ctx = this->n_ctx();
|
2115
|
+
const uint32_t n_batch = std::min(cparams.n_batch, n_ctx);
|
2116
|
+
const uint32_t n_ubatch = std::min(cparams.n_ubatch, n_batch);
|
2117
|
+
const int64_t ndata = ggml_opt_dataset_ndata(dataset);
|
2118
|
+
|
2119
|
+
GGML_ASSERT(idata_split >= 0);
|
2120
|
+
GGML_ASSERT(idata_split <= ndata);
|
2121
|
+
|
2122
|
+
const uint32_t ubatch_per_ctx = n_ctx / n_ubatch;
|
2123
|
+
|
2124
|
+
struct llama_batch batch = llama_batch_init(n_batch, 0, 1);
|
2125
|
+
std::vector<llama_token> tokens(n_ctx);
|
2126
|
+
std::vector<llama_token> labels_sparse(n_ctx);
|
2127
|
+
|
2128
|
+
int64_t idata = 0;
|
2129
|
+
|
2130
|
+
int64_t t_loop_start = ggml_time_us();
|
2131
|
+
int64_t ndata_in_loop = idata_split*ubatch_per_ctx;
|
2132
|
+
for (; idata < idata_split; ++idata) {
|
2133
|
+
constexpr bool train = true;
|
2134
|
+
const int64_t idata_in_loop = idata*ubatch_per_ctx;
|
2135
|
+
|
2136
|
+
ggml_opt_dataset_get_batch_host(dataset, tokens.data(), n_ctx*sizeof(llama_token), labels_sparse.data(), idata);
|
2137
|
+
opt_epoch_iter(dataset, result_train, tokens, labels_sparse, batch,
|
2138
|
+
callback_train, train, idata_in_loop, ndata_in_loop, t_loop_start);
|
2139
|
+
}
|
2140
|
+
|
2141
|
+
t_loop_start = ggml_time_us();
|
2142
|
+
ndata_in_loop = (ndata - idata_split)*ubatch_per_ctx;
|
2143
|
+
for (; idata < ndata; ++idata) {
|
2144
|
+
constexpr bool train = false;
|
2145
|
+
const int64_t idata_in_loop = (idata - idata_split)*ubatch_per_ctx;
|
2146
|
+
|
2147
|
+
ggml_opt_dataset_get_batch_host(dataset, tokens.data(), n_ctx*sizeof(llama_token), labels_sparse.data(), idata);
|
2148
|
+
opt_epoch_iter(dataset, result_eval, tokens, labels_sparse, batch,
|
2149
|
+
callback_eval, train, idata_in_loop, ndata_in_loop, t_loop_start);
|
2150
|
+
}
|
2151
|
+
|
2152
|
+
llama_batch_free(batch);
|
2153
|
+
}
|
2154
|
+
|
2155
|
+
//
|
2156
|
+
// interface implementation
|
2157
|
+
//
|
2158
|
+
|
2159
|
+
llama_context_params llama_context_default_params() {
|
2160
|
+
llama_context_params result = {
|
2161
|
+
/*.n_ctx =*/ 512,
|
2162
|
+
/*.n_batch =*/ 2048,
|
2163
|
+
/*.n_ubatch =*/ 512,
|
2164
|
+
/*.n_seq_max =*/ 1,
|
2165
|
+
/*.n_threads =*/ GGML_DEFAULT_N_THREADS, // TODO: better default
|
2166
|
+
/*.n_threads_batch =*/ GGML_DEFAULT_N_THREADS,
|
2167
|
+
/*.rope_scaling_type =*/ LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED,
|
2168
|
+
/*.pooling_type =*/ LLAMA_POOLING_TYPE_UNSPECIFIED,
|
2169
|
+
/*.attention_type =*/ LLAMA_ATTENTION_TYPE_UNSPECIFIED,
|
2170
|
+
/*.rope_freq_base =*/ 0.0f,
|
2171
|
+
/*.rope_freq_scale =*/ 0.0f,
|
2172
|
+
/*.yarn_ext_factor =*/ -1.0f,
|
2173
|
+
/*.yarn_attn_factor =*/ 1.0f,
|
2174
|
+
/*.yarn_beta_fast =*/ 32.0f,
|
2175
|
+
/*.yarn_beta_slow =*/ 1.0f,
|
2176
|
+
/*.yarn_orig_ctx =*/ 0,
|
2177
|
+
/*.defrag_thold =*/ -1.0f,
|
2178
|
+
/*.cb_eval =*/ nullptr,
|
2179
|
+
/*.cb_eval_user_data =*/ nullptr,
|
2180
|
+
/*.type_k =*/ GGML_TYPE_F16,
|
2181
|
+
/*.type_v =*/ GGML_TYPE_F16,
|
2182
|
+
/*.abort_callback =*/ nullptr,
|
2183
|
+
/*.abort_callback_data =*/ nullptr,
|
2184
|
+
/*.embeddings =*/ false,
|
2185
|
+
/*.offload_kqv =*/ true,
|
2186
|
+
/*.flash_attn =*/ false,
|
2187
|
+
/*.no_perf =*/ true,
|
2188
|
+
/*.op_offload =*/ true,
|
2189
|
+
/*.swa_full =*/ true,
|
2190
|
+
};
|
2191
|
+
|
2192
|
+
return result;
|
2193
|
+
}
|
2194
|
+
|
2195
|
+
llama_context * llama_init_from_model(
|
2196
|
+
llama_model * model,
|
2197
|
+
llama_context_params params) {
|
2198
|
+
if (!model) {
|
2199
|
+
LLAMA_LOG_ERROR("%s: model cannot be NULL\n", __func__);
|
2200
|
+
return nullptr;
|
2201
|
+
}
|
2202
|
+
|
2203
|
+
if (params.n_batch == 0 && params.n_ubatch == 0) {
|
2204
|
+
LLAMA_LOG_ERROR("%s: n_batch and n_ubatch cannot both be zero\n", __func__);
|
2205
|
+
return nullptr;
|
2206
|
+
}
|
2207
|
+
|
2208
|
+
if (params.n_ctx == 0 && model->hparams.n_ctx_train == 0) {
|
2209
|
+
LLAMA_LOG_ERROR("%s: n_ctx and model->hparams.n_ctx_train cannot both be zero\n", __func__);
|
2210
|
+
return nullptr;
|
2211
|
+
}
|
2212
|
+
|
2213
|
+
if (params.flash_attn && model->arch == LLM_ARCH_GROK) {
|
2214
|
+
LLAMA_LOG_WARN("%s: flash_attn is not compatible with Grok - forcing off\n", __func__);
|
2215
|
+
params.flash_attn = false;
|
2216
|
+
}
|
2217
|
+
|
2218
|
+
if (ggml_is_quantized(params.type_v) && !params.flash_attn) {
|
2219
|
+
LLAMA_LOG_ERROR("%s: V cache quantization requires flash_attn\n", __func__);
|
2220
|
+
return nullptr;
|
2221
|
+
}
|
2222
|
+
|
2223
|
+
try {
|
2224
|
+
auto * ctx = new llama_context(*model, params);
|
2225
|
+
return ctx;
|
2226
|
+
} catch (const std::exception & err) {
|
2227
|
+
LLAMA_LOG_ERROR("%s: failed to initialize the context: %s\n", __func__, err.what());
|
2228
|
+
}
|
2229
|
+
|
2230
|
+
return nullptr;
|
2231
|
+
}
|
2232
|
+
|
2233
|
+
// deprecated
|
2234
|
+
llama_context * llama_new_context_with_model(
|
2235
|
+
llama_model * model,
|
2236
|
+
llama_context_params params) {
|
2237
|
+
return llama_init_from_model(model, params);
|
2238
|
+
}
|
2239
|
+
|
2240
|
+
void llama_free(llama_context * ctx) {
|
2241
|
+
delete ctx;
|
2242
|
+
}
|
2243
|
+
|
2244
|
+
uint32_t llama_n_ctx(const llama_context * ctx) {
|
2245
|
+
return ctx->n_ctx();
|
2246
|
+
}
|
2247
|
+
|
2248
|
+
uint32_t llama_n_batch(const llama_context * ctx) {
|
2249
|
+
return ctx->n_batch();
|
2250
|
+
}
|
2251
|
+
|
2252
|
+
uint32_t llama_n_ubatch(const llama_context * ctx) {
|
2253
|
+
return ctx->n_ubatch();
|
2254
|
+
}
|
2255
|
+
|
2256
|
+
uint32_t llama_n_seq_max(const llama_context * ctx) {
|
2257
|
+
return ctx->n_seq_max();
|
2258
|
+
}
|
2259
|
+
|
2260
|
+
const llama_model * llama_get_model(const llama_context * ctx) {
|
2261
|
+
return &ctx->get_model();
|
2262
|
+
}
|
2263
|
+
|
2264
|
+
// deprecated
|
2265
|
+
llama_kv_cache * llama_get_kv_self(llama_context * ctx) {
|
2266
|
+
return dynamic_cast<llama_kv_cache *>(ctx->get_memory());
|
2267
|
+
}
|
2268
|
+
|
2269
|
+
// deprecated
|
2270
|
+
void llama_kv_self_update(llama_context * ctx) {
|
2271
|
+
ctx->kv_self_update(false);
|
2272
|
+
}
|
2273
|
+
|
2274
|
+
enum llama_pooling_type llama_pooling_type(const llama_context * ctx) {
|
2275
|
+
return ctx->pooling_type();
|
2276
|
+
}
|
2277
|
+
|
2278
|
+
void llama_attach_threadpool(
|
2279
|
+
llama_context * ctx,
|
2280
|
+
ggml_threadpool_t threadpool,
|
2281
|
+
ggml_threadpool_t threadpool_batch) {
|
2282
|
+
ctx->attach_threadpool(threadpool, threadpool_batch);
|
2283
|
+
}
|
2284
|
+
|
2285
|
+
void llama_detach_threadpool(llama_context * ctx) {
|
2286
|
+
ctx->detach_threadpool();
|
2287
|
+
}
|
2288
|
+
|
2289
|
+
void llama_set_n_threads(llama_context * ctx, int32_t n_threads, int32_t n_threads_batch) {
|
2290
|
+
ctx->set_n_threads(n_threads, n_threads_batch);
|
2291
|
+
}
|
2292
|
+
|
2293
|
+
int32_t llama_n_threads(llama_context * ctx) {
|
2294
|
+
return ctx->n_threads();
|
2295
|
+
}
|
2296
|
+
|
2297
|
+
int32_t llama_n_threads_batch(llama_context * ctx) {
|
2298
|
+
return ctx->n_threads_batch();
|
2299
|
+
}
|
2300
|
+
|
2301
|
+
void llama_set_abort_callback(llama_context * ctx, bool (*abort_callback)(void * data), void * abort_callback_data) {
|
2302
|
+
ctx->set_abort_callback(abort_callback, abort_callback_data);
|
2303
|
+
}
|
2304
|
+
|
2305
|
+
void llama_set_embeddings(llama_context * ctx, bool embeddings) {
|
2306
|
+
ctx->set_embeddings(embeddings);
|
2307
|
+
}
|
2308
|
+
|
2309
|
+
void llama_set_causal_attn(llama_context * ctx, bool causal_attn) {
|
2310
|
+
ctx->set_causal_attn(causal_attn);
|
2311
|
+
}
|
2312
|
+
|
2313
|
+
void llama_set_warmup(llama_context * ctx, bool warmup) {
|
2314
|
+
ctx->set_warmup(warmup);
|
2315
|
+
}
|
2316
|
+
|
2317
|
+
void llama_synchronize(llama_context * ctx) {
|
2318
|
+
ctx->synchronize();
|
2319
|
+
}
|
2320
|
+
|
2321
|
+
float * llama_get_logits(llama_context * ctx) {
|
2322
|
+
ctx->synchronize();
|
2323
|
+
|
2324
|
+
return ctx->get_logits();
|
2325
|
+
}
|
2326
|
+
|
2327
|
+
float * llama_get_logits_ith(llama_context * ctx, int32_t i) {
|
2328
|
+
ctx->synchronize();
|
2329
|
+
|
2330
|
+
return ctx->get_logits_ith(i);
|
2331
|
+
}
|
2332
|
+
|
2333
|
+
float * llama_get_embeddings(llama_context * ctx) {
|
2334
|
+
ctx->synchronize();
|
2335
|
+
|
2336
|
+
return ctx->get_embeddings();
|
2337
|
+
}
|
2338
|
+
|
2339
|
+
float * llama_get_embeddings_ith(llama_context * ctx, int32_t i) {
|
2340
|
+
ctx->synchronize();
|
2341
|
+
|
2342
|
+
return ctx->get_embeddings_ith(i);
|
2343
|
+
}
|
2344
|
+
|
2345
|
+
float * llama_get_embeddings_seq(llama_context * ctx, llama_seq_id seq_id) {
|
2346
|
+
ctx->synchronize();
|
2347
|
+
|
2348
|
+
return ctx->get_embeddings_seq(seq_id);
|
2349
|
+
}
|
2350
|
+
|
2351
|
+
// llama adapter API
|
2352
|
+
|
2353
|
+
int32_t llama_set_adapter_lora(
|
2354
|
+
llama_context * ctx,
|
2355
|
+
llama_adapter_lora * adapter,
|
2356
|
+
float scale) {
|
2357
|
+
ctx->set_adapter_lora(adapter, scale);
|
2358
|
+
|
2359
|
+
return 0;
|
2360
|
+
}
|
2361
|
+
|
2362
|
+
int32_t llama_rm_adapter_lora(
|
2363
|
+
llama_context * ctx,
|
2364
|
+
llama_adapter_lora * adapter) {
|
2365
|
+
bool res = ctx->rm_adapter_lora(adapter);
|
2366
|
+
|
2367
|
+
return res ? 0 : -1;
|
2368
|
+
}
|
2369
|
+
|
2370
|
+
void llama_clear_adapter_lora(llama_context * ctx) {
|
2371
|
+
ctx->clear_adapter_lora();
|
2372
|
+
}
|
2373
|
+
|
2374
|
+
int32_t llama_apply_adapter_cvec(
|
2375
|
+
llama_context * ctx,
|
2376
|
+
const float * data,
|
2377
|
+
size_t len,
|
2378
|
+
int32_t n_embd,
|
2379
|
+
int32_t il_start,
|
2380
|
+
int32_t il_end) {
|
2381
|
+
bool res = ctx->apply_adapter_cvec(data, len, n_embd, il_start, il_end);
|
2382
|
+
|
2383
|
+
return res ? 0 : -1;
|
2384
|
+
}
|
2385
|
+
|
2386
|
+
//
|
2387
|
+
// memory
|
2388
|
+
//
|
2389
|
+
|
2390
|
+
llama_memory_t llama_get_memory(const struct llama_context * ctx) {
|
2391
|
+
return ctx->get_memory();
|
2392
|
+
}
|
2393
|
+
|
2394
|
+
void llama_memory_clear(llama_memory_t mem, bool data) {
|
2395
|
+
if (!mem) {
|
2396
|
+
return;
|
2397
|
+
}
|
2398
|
+
|
2399
|
+
mem->clear(data);
|
2400
|
+
}
|
2401
|
+
|
2402
|
+
bool llama_memory_seq_rm(
|
2403
|
+
llama_memory_t mem,
|
2404
|
+
llama_seq_id seq_id,
|
2405
|
+
llama_pos p0,
|
2406
|
+
llama_pos p1) {
|
2407
|
+
if (!mem) {
|
2408
|
+
return true;
|
2409
|
+
}
|
2410
|
+
|
2411
|
+
return mem->seq_rm(seq_id, p0, p1);
|
2412
|
+
}
|
2413
|
+
|
2414
|
+
void llama_memory_seq_cp(
|
2415
|
+
llama_memory_t mem,
|
2416
|
+
llama_seq_id seq_id_src,
|
2417
|
+
llama_seq_id seq_id_dst,
|
2418
|
+
llama_pos p0,
|
2419
|
+
llama_pos p1) {
|
2420
|
+
if (!mem) {
|
2421
|
+
return;
|
2422
|
+
}
|
2423
|
+
|
2424
|
+
mem->seq_cp(seq_id_src, seq_id_dst, p0, p1);
|
2425
|
+
}
|
2426
|
+
|
2427
|
+
void llama_memory_seq_keep(
|
2428
|
+
llama_memory_t mem,
|
2429
|
+
llama_seq_id seq_id) {
|
2430
|
+
if (!mem) {
|
2431
|
+
return;
|
2432
|
+
}
|
2433
|
+
|
2434
|
+
mem->seq_keep(seq_id);
|
2435
|
+
}
|
2436
|
+
|
2437
|
+
void llama_memory_seq_add(
|
2438
|
+
llama_memory_t mem,
|
2439
|
+
llama_seq_id seq_id,
|
2440
|
+
llama_pos p0,
|
2441
|
+
llama_pos p1,
|
2442
|
+
llama_pos delta) {
|
2443
|
+
if (!mem) {
|
2444
|
+
return;
|
2445
|
+
}
|
2446
|
+
|
2447
|
+
mem->seq_add(seq_id, p0, p1, delta);
|
2448
|
+
}
|
2449
|
+
|
2450
|
+
void llama_memory_seq_div(
|
2451
|
+
llama_memory_t mem,
|
2452
|
+
llama_seq_id seq_id,
|
2453
|
+
llama_pos p0,
|
2454
|
+
llama_pos p1,
|
2455
|
+
int d) {
|
2456
|
+
if (!mem) {
|
2457
|
+
return;
|
2458
|
+
}
|
2459
|
+
|
2460
|
+
mem->seq_div(seq_id, p0, p1, d);
|
2461
|
+
}
|
2462
|
+
|
2463
|
+
llama_pos llama_memory_seq_pos_min(
|
2464
|
+
llama_memory_t mem,
|
2465
|
+
llama_seq_id seq_id) {
|
2466
|
+
if (!mem) {
|
2467
|
+
return -1;
|
2468
|
+
}
|
2469
|
+
|
2470
|
+
return mem->seq_pos_min(seq_id);
|
2471
|
+
}
|
2472
|
+
|
2473
|
+
llama_pos llama_memory_seq_pos_max(
|
2474
|
+
llama_memory_t mem,
|
2475
|
+
llama_seq_id seq_id) {
|
2476
|
+
if (!mem) {
|
2477
|
+
return -1;
|
2478
|
+
}
|
2479
|
+
|
2480
|
+
return mem->seq_pos_max(seq_id);
|
2481
|
+
}
|
2482
|
+
|
2483
|
+
bool llama_memory_can_shift(llama_memory_t mem) {
|
2484
|
+
if (!mem) {
|
2485
|
+
return false;
|
2486
|
+
}
|
2487
|
+
|
2488
|
+
return mem->get_can_shift();
|
2489
|
+
}
|
2490
|
+
|
2491
|
+
//
|
2492
|
+
// kv cache
|
2493
|
+
//
|
2494
|
+
|
2495
|
+
// deprecated
|
2496
|
+
int32_t llama_kv_self_n_tokens(const llama_context * ctx) {
|
2497
|
+
const auto * kv = llama_get_memory(ctx);
|
2498
|
+
if (!kv) {
|
2499
|
+
return 0;
|
2500
|
+
}
|
2501
|
+
|
2502
|
+
int32_t res = 0;
|
2503
|
+
|
2504
|
+
for (uint32_t s = 0; s < ctx->get_cparams().n_seq_max; s++) {
|
2505
|
+
const llama_pos p0 = kv->seq_pos_min(s);
|
2506
|
+
const llama_pos p1 = kv->seq_pos_max(s);
|
2507
|
+
|
2508
|
+
if (p0 >= 0) {
|
2509
|
+
res += (p1 - p0) + 1;
|
2510
|
+
}
|
2511
|
+
}
|
2512
|
+
|
2513
|
+
return res;
|
2514
|
+
}
|
2515
|
+
|
2516
|
+
// deprecated
|
2517
|
+
// note: this is the same as above - will be removed anyway, so it's ok
|
2518
|
+
int32_t llama_kv_self_used_cells(const llama_context * ctx) {
|
2519
|
+
const auto * kv = llama_get_memory(ctx);
|
2520
|
+
if (!kv) {
|
2521
|
+
return 0;
|
2522
|
+
}
|
2523
|
+
|
2524
|
+
int32_t res = 0;
|
2525
|
+
|
2526
|
+
for (uint32_t s = 0; s < ctx->get_cparams().n_seq_max; s++) {
|
2527
|
+
const llama_pos p0 = kv->seq_pos_min(s);
|
2528
|
+
const llama_pos p1 = kv->seq_pos_max(s);
|
2529
|
+
|
2530
|
+
if (p0 >= 0) {
|
2531
|
+
res += (p1 - p0) + 1;
|
2532
|
+
}
|
2533
|
+
}
|
2534
|
+
|
2535
|
+
return res;
|
2536
|
+
}
|
2537
|
+
|
2538
|
+
// deprecated
|
2539
|
+
void llama_kv_self_clear(llama_context * ctx) {
|
2540
|
+
auto * kv = llama_get_memory(ctx);
|
2541
|
+
if (!kv) {
|
2542
|
+
return;
|
2543
|
+
}
|
2544
|
+
|
2545
|
+
llama_memory_clear(kv, true);
|
2546
|
+
}
|
2547
|
+
|
2548
|
+
// deprecated
|
2549
|
+
bool llama_kv_self_seq_rm(
|
2550
|
+
llama_context * ctx,
|
2551
|
+
llama_seq_id seq_id,
|
2552
|
+
llama_pos p0,
|
2553
|
+
llama_pos p1) {
|
2554
|
+
auto * kv = llama_get_memory(ctx);
|
2555
|
+
if (!kv) {
|
2556
|
+
return true;
|
2557
|
+
}
|
2558
|
+
|
2559
|
+
return llama_memory_seq_rm(kv, seq_id, p0, p1);
|
2560
|
+
}
|
2561
|
+
|
2562
|
+
// deprecated
|
2563
|
+
void llama_kv_self_seq_cp(
|
2564
|
+
llama_context * ctx,
|
2565
|
+
llama_seq_id seq_id_src,
|
2566
|
+
llama_seq_id seq_id_dst,
|
2567
|
+
llama_pos p0,
|
2568
|
+
llama_pos p1) {
|
2569
|
+
auto * kv = llama_get_memory(ctx);
|
2570
|
+
if (!kv) {
|
2571
|
+
return;
|
2572
|
+
}
|
2573
|
+
|
2574
|
+
llama_memory_seq_cp(kv, seq_id_src, seq_id_dst, p0, p1);
|
2575
|
+
}
|
2576
|
+
|
2577
|
+
// deprecated
|
2578
|
+
void llama_kv_self_seq_keep(llama_context * ctx, llama_seq_id seq_id) {
|
2579
|
+
auto * kv = llama_get_memory(ctx);
|
2580
|
+
if (!kv) {
|
2581
|
+
return;
|
2582
|
+
}
|
2583
|
+
|
2584
|
+
llama_memory_seq_keep(kv, seq_id);
|
2585
|
+
}
|
2586
|
+
|
2587
|
+
// deprecated
|
2588
|
+
void llama_kv_self_seq_add(
|
2589
|
+
llama_context * ctx,
|
2590
|
+
llama_seq_id seq_id,
|
2591
|
+
llama_pos p0,
|
2592
|
+
llama_pos p1,
|
2593
|
+
llama_pos delta) {
|
2594
|
+
auto * kv = llama_get_memory(ctx);
|
2595
|
+
if (!kv) {
|
2596
|
+
return;
|
2597
|
+
}
|
2598
|
+
|
2599
|
+
llama_memory_seq_add(kv, seq_id, p0, p1, delta);
|
2600
|
+
}
|
2601
|
+
|
2602
|
+
// deprecated
|
2603
|
+
void llama_kv_self_seq_div(
|
2604
|
+
llama_context * ctx,
|
2605
|
+
llama_seq_id seq_id,
|
2606
|
+
llama_pos p0,
|
2607
|
+
llama_pos p1,
|
2608
|
+
int d) {
|
2609
|
+
auto * kv = llama_get_memory(ctx);
|
2610
|
+
if (!kv) {
|
2611
|
+
return;
|
2612
|
+
}
|
2613
|
+
|
2614
|
+
llama_memory_seq_div(kv, seq_id, p0, p1, d);
|
2615
|
+
}
|
2616
|
+
|
2617
|
+
// deprecated
|
2618
|
+
llama_pos llama_kv_self_seq_pos_min(llama_context * ctx, llama_seq_id seq_id) {
|
2619
|
+
auto * kv = llama_get_memory(ctx);
|
2620
|
+
if (!kv) {
|
2621
|
+
return -1;
|
2622
|
+
}
|
2623
|
+
|
2624
|
+
return llama_memory_seq_pos_min(kv, seq_id);
|
2625
|
+
}
|
2626
|
+
|
2627
|
+
// deprecated
|
2628
|
+
llama_pos llama_kv_self_seq_pos_max(llama_context * ctx, llama_seq_id seq_id) {
|
2629
|
+
auto * kv = llama_get_memory(ctx);
|
2630
|
+
if (!kv) {
|
2631
|
+
return -1;
|
2632
|
+
}
|
2633
|
+
|
2634
|
+
return llama_memory_seq_pos_max(kv, seq_id);
|
2635
|
+
}
|
2636
|
+
|
2637
|
+
// deprecated
|
2638
|
+
void llama_kv_self_defrag(llama_context * ctx) {
|
2639
|
+
// force defrag
|
2640
|
+
ctx->kv_self_defrag_sched();
|
2641
|
+
}
|
2642
|
+
|
2643
|
+
// deprecated
|
2644
|
+
bool llama_kv_self_can_shift(const llama_context * ctx) {
|
2645
|
+
auto * kv = llama_get_memory(ctx);
|
2646
|
+
if (!kv) {
|
2647
|
+
return false;
|
2648
|
+
}
|
2649
|
+
|
2650
|
+
return llama_memory_can_shift(kv);
|
2651
|
+
}
|
2652
|
+
|
2653
|
+
// llama state API
|
2654
|
+
|
2655
|
+
// deprecated
|
2656
|
+
size_t llama_get_state_size(llama_context * ctx) {
|
2657
|
+
return llama_state_get_size(ctx);
|
2658
|
+
}
|
2659
|
+
|
2660
|
+
// deprecated
|
2661
|
+
size_t llama_copy_state_data(llama_context * ctx, uint8_t * dst) {
|
2662
|
+
return llama_state_get_data(ctx, dst, -1);
|
2663
|
+
}
|
2664
|
+
|
2665
|
+
// deprecated
|
2666
|
+
size_t llama_set_state_data(llama_context * ctx, const uint8_t * src) {
|
2667
|
+
return llama_state_set_data(ctx, src, -1);
|
2668
|
+
}
|
2669
|
+
|
2670
|
+
// deprecated
|
2671
|
+
bool llama_load_session_file(llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
|
2672
|
+
return llama_state_load_file(ctx, path_session, tokens_out, n_token_capacity, n_token_count_out);
|
2673
|
+
}
|
2674
|
+
|
2675
|
+
// deprecated
|
2676
|
+
bool llama_save_session_file(llama_context * ctx, const char * path_session, const llama_token * tokens, size_t n_token_count) {
|
2677
|
+
return llama_state_save_file(ctx, path_session, tokens, n_token_count);
|
2678
|
+
}
|
2679
|
+
|
2680
|
+
// Returns the *actual* size of the state.
|
2681
|
+
// Intended to be used when saving to state to a buffer.
|
2682
|
+
size_t llama_state_get_size(llama_context * ctx) {
|
2683
|
+
return ctx->state_get_size();
|
2684
|
+
}
|
2685
|
+
|
2686
|
+
size_t llama_state_get_data(llama_context * ctx, uint8_t * dst, size_t size) {
|
2687
|
+
ctx->synchronize();
|
2688
|
+
|
2689
|
+
return ctx->state_get_data(dst, size);
|
2690
|
+
}
|
2691
|
+
|
2692
|
+
// Sets the state reading from the specified source address
|
2693
|
+
size_t llama_state_set_data(llama_context * ctx, const uint8_t * src, size_t size) {
|
2694
|
+
ctx->synchronize();
|
2695
|
+
|
2696
|
+
return ctx->state_set_data(src, size);
|
2697
|
+
}
|
2698
|
+
|
2699
|
+
bool llama_state_load_file(llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
|
2700
|
+
ctx->synchronize();
|
2701
|
+
|
2702
|
+
try {
|
2703
|
+
return ctx->state_load_file(path_session, tokens_out, n_token_capacity, n_token_count_out);
|
2704
|
+
} catch (const std::exception & err) {
|
2705
|
+
LLAMA_LOG_ERROR("%s: error loading session file: %s\n", __func__, err.what());
|
2706
|
+
return false;
|
2707
|
+
}
|
2708
|
+
}
|
2709
|
+
|
2710
|
+
bool llama_state_save_file(llama_context * ctx, const char * path_session, const llama_token * tokens, size_t n_token_count) {
|
2711
|
+
ctx->synchronize();
|
2712
|
+
|
2713
|
+
try {
|
2714
|
+
return ctx->state_save_file(path_session, tokens, n_token_count);
|
2715
|
+
} catch (const std::exception & err) {
|
2716
|
+
LLAMA_LOG_ERROR("%s: error saving session file: %s\n", __func__, err.what());
|
2717
|
+
return false;
|
2718
|
+
}
|
2719
|
+
}
|
2720
|
+
|
2721
|
+
size_t llama_state_seq_get_size(llama_context * ctx, llama_seq_id seq_id) {
|
2722
|
+
return ctx->state_seq_get_size(seq_id);
|
2723
|
+
}
|
2724
|
+
|
2725
|
+
size_t llama_state_seq_get_data(llama_context * ctx, uint8_t * dst, size_t size, llama_seq_id seq_id) {
|
2726
|
+
ctx->synchronize();
|
2727
|
+
|
2728
|
+
return ctx->state_seq_get_data(seq_id, dst, size);
|
2729
|
+
}
|
2730
|
+
|
2731
|
+
size_t llama_state_seq_set_data(llama_context * ctx, const uint8_t * src, size_t size, llama_seq_id seq_id) {
|
2732
|
+
ctx->synchronize();
|
2733
|
+
|
2734
|
+
return ctx->state_seq_set_data(seq_id, src, size);
|
2735
|
+
}
|
2736
|
+
|
2737
|
+
size_t llama_state_seq_save_file(llama_context * ctx, const char * filepath, llama_seq_id seq_id, const llama_token * tokens, size_t n_token_count) {
|
2738
|
+
ctx->synchronize();
|
2739
|
+
|
2740
|
+
try {
|
2741
|
+
return ctx->state_seq_save_file(seq_id, filepath, tokens, n_token_count);
|
2742
|
+
} catch (const std::exception & err) {
|
2743
|
+
LLAMA_LOG_ERROR("%s: error saving sequence state file: %s\n", __func__, err.what());
|
2744
|
+
return 0;
|
2745
|
+
}
|
2746
|
+
}
|
2747
|
+
|
2748
|
+
size_t llama_state_seq_load_file(llama_context * ctx, const char * filepath, llama_seq_id dest_seq_id, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
|
2749
|
+
ctx->synchronize();
|
2750
|
+
|
2751
|
+
try {
|
2752
|
+
return ctx->state_seq_load_file(dest_seq_id, filepath, tokens_out, n_token_capacity, n_token_count_out);
|
2753
|
+
} catch (const std::exception & err) {
|
2754
|
+
LLAMA_LOG_ERROR("%s: error loading sequence state file: %s\n", __func__, err.what());
|
2755
|
+
return 0;
|
2756
|
+
}
|
2757
|
+
}
|
2758
|
+
|
2759
|
+
///
|
2760
|
+
|
2761
|
+
int32_t llama_encode(
|
2762
|
+
llama_context * ctx,
|
2763
|
+
llama_batch batch) {
|
2764
|
+
const int ret = ctx->encode(batch);
|
2765
|
+
if (ret != 0) {
|
2766
|
+
LLAMA_LOG_ERROR("%s: failed to encode, ret = %d\n", __func__, ret);
|
2767
|
+
}
|
2768
|
+
|
2769
|
+
return ret;
|
2770
|
+
}
|
2771
|
+
|
2772
|
+
int32_t llama_decode(
|
2773
|
+
llama_context * ctx,
|
2774
|
+
llama_batch batch) {
|
2775
|
+
const int ret = ctx->decode(batch);
|
2776
|
+
if (ret != 0 && ret != 1) {
|
2777
|
+
LLAMA_LOG_ERROR("%s: failed to decode, ret = %d\n", __func__, ret);
|
2778
|
+
}
|
2779
|
+
|
2780
|
+
return ret;
|
2781
|
+
}
|
2782
|
+
|
2783
|
+
//
|
2784
|
+
// perf
|
2785
|
+
//
|
2786
|
+
|
2787
|
+
llama_perf_context_data llama_perf_context(const llama_context * ctx) {
|
2788
|
+
llama_perf_context_data data = {};
|
2789
|
+
|
2790
|
+
if (ctx == nullptr) {
|
2791
|
+
return data;
|
2792
|
+
}
|
2793
|
+
|
2794
|
+
data = ctx->perf_get_data();
|
2795
|
+
|
2796
|
+
return data;
|
2797
|
+
}
|
2798
|
+
|
2799
|
+
void llama_perf_context_print(const llama_context * ctx) {
|
2800
|
+
const auto data = llama_perf_context(ctx);
|
2801
|
+
|
2802
|
+
const double t_end_ms = 1e-3 * ggml_time_us();
|
2803
|
+
|
2804
|
+
LLAMA_LOG_INFO("%s: load time = %10.2f ms\n", __func__, data.t_load_ms);
|
2805
|
+
LLAMA_LOG_INFO("%s: prompt eval time = %10.2f ms / %5d tokens (%8.2f ms per token, %8.2f tokens per second)\n",
|
2806
|
+
__func__, data.t_p_eval_ms, data.n_p_eval, data.t_p_eval_ms / data.n_p_eval, 1e3 / data.t_p_eval_ms * data.n_p_eval);
|
2807
|
+
LLAMA_LOG_INFO("%s: eval time = %10.2f ms / %5d runs (%8.2f ms per token, %8.2f tokens per second)\n",
|
2808
|
+
__func__, data.t_eval_ms, data.n_eval, data.t_eval_ms / data.n_eval, 1e3 / data.t_eval_ms * data.n_eval);
|
2809
|
+
LLAMA_LOG_INFO("%s: total time = %10.2f ms / %5d tokens\n", __func__, (t_end_ms - data.t_start_ms), (data.n_p_eval + data.n_eval));
|
2810
|
+
}
|
2811
|
+
|
2812
|
+
void llama_perf_context_reset(llama_context * ctx) {
|
2813
|
+
ctx->perf_reset();
|
2814
|
+
}
|
2815
|
+
|
2816
|
+
//
|
2817
|
+
// training
|
2818
|
+
//
|
2819
|
+
|
2820
|
+
bool llama_opt_param_filter_all(const struct ggml_tensor * tensor, void * userdata) {
|
2821
|
+
GGML_UNUSED(tensor);
|
2822
|
+
GGML_UNUSED(userdata);
|
2823
|
+
return true;
|
2824
|
+
}
|
2825
|
+
|
2826
|
+
void llama_opt_init(struct llama_context * ctx, struct llama_model * model, struct llama_opt_params lopt_params) {
|
2827
|
+
ctx->opt_init(model, lopt_params);
|
2828
|
+
}
|
2829
|
+
|
2830
|
+
void llama_opt_epoch(
|
2831
|
+
struct llama_context * ctx,
|
2832
|
+
ggml_opt_dataset_t dataset,
|
2833
|
+
ggml_opt_result_t result_train,
|
2834
|
+
ggml_opt_result_t result_eval,
|
2835
|
+
int64_t idata_split,
|
2836
|
+
ggml_opt_epoch_callback callback_train,
|
2837
|
+
ggml_opt_epoch_callback callback_eval) {
|
2838
|
+
ctx->opt_epoch(
|
2839
|
+
dataset,
|
2840
|
+
result_train,
|
2841
|
+
result_eval,
|
2842
|
+
idata_split,
|
2843
|
+
callback_train,
|
2844
|
+
callback_eval);
|
2845
|
+
}
|