@fugood/llama.node 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +5 -2
- package/bin/darwin/arm64/llama-node.node +0 -0
- package/bin/darwin/x64/llama-node.node +0 -0
- package/bin/linux/arm64/llama-node.node +0 -0
- package/bin/linux/x64/llama-node.node +0 -0
- package/bin/linux-vulkan/arm64/llama-node.node +0 -0
- package/bin/linux-vulkan/x64/llama-node.node +0 -0
- package/bin/win32/arm64/llama-node.node +0 -0
- package/bin/win32/arm64/node.lib +0 -0
- package/bin/win32/x64/llama-node.node +0 -0
- package/bin/win32/x64/node.lib +0 -0
- package/bin/win32-vulkan/arm64/llama-node.node +0 -0
- package/bin/win32-vulkan/arm64/node.lib +0 -0
- package/bin/win32-vulkan/x64/llama-node.node +0 -0
- package/bin/win32-vulkan/x64/node.lib +0 -0
- package/lib/binding.ts +8 -1
- package/package.json +1 -1
- package/patches/llama.patch +12 -12
- package/src/DetokenizeWorker.cpp +1 -1
- package/src/LlamaContext.cpp +33 -1
- package/src/LlamaContext.h +1 -0
- package/src/LoadSessionWorker.cpp +1 -0
- package/src/llama.cpp/.github/workflows/bench.yml +310 -0
- package/src/llama.cpp/.github/workflows/build.yml +1315 -0
- package/src/llama.cpp/.github/workflows/close-issue.yml +23 -0
- package/src/llama.cpp/.github/workflows/docker.yml +116 -0
- package/src/llama.cpp/.github/workflows/editorconfig.yml +27 -0
- package/src/llama.cpp/.github/workflows/gguf-publish.yml +44 -0
- package/src/llama.cpp/.github/workflows/labeler.yml +17 -0
- package/src/llama.cpp/.github/workflows/nix-ci-aarch64.yml +65 -0
- package/src/llama.cpp/.github/workflows/nix-ci.yml +72 -0
- package/src/llama.cpp/.github/workflows/nix-flake-update.yml +22 -0
- package/src/llama.cpp/.github/workflows/nix-publish-flake.yml +36 -0
- package/src/llama.cpp/.github/workflows/python-check-requirements.yml +35 -0
- package/src/llama.cpp/.github/workflows/python-lint.yml +23 -0
- package/src/llama.cpp/.github/workflows/python-type-check.yml +38 -0
- package/src/llama.cpp/.github/workflows/server.yml +183 -0
- package/src/llama.cpp/CMakeLists.txt +91 -1245
- package/src/llama.cpp/cmake/arm64-windows-llvm.cmake +1 -1
- package/src/llama.cpp/cmake/build-info.cmake +58 -0
- package/src/llama.cpp/cmake/git-vars.cmake +22 -0
- package/src/llama.cpp/common/CMakeLists.txt +4 -3
- package/src/llama.cpp/common/build-info.cpp.in +4 -0
- package/src/llama.cpp/common/common.cpp +1116 -877
- package/src/llama.cpp/common/common.h +191 -77
- package/src/llama.cpp/common/grammar-parser.cpp +118 -31
- package/src/llama.cpp/common/json-schema-to-grammar.cpp +346 -65
- package/src/llama.cpp/common/log.h +1 -1
- package/src/llama.cpp/common/ngram-cache.h +10 -3
- package/src/llama.cpp/common/sampling.cpp +19 -10
- package/src/llama.cpp/docs/build.md +353 -0
- package/src/llama.cpp/examples/CMakeLists.txt +22 -22
- package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +6 -6
- package/src/llama.cpp/examples/batched/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/batched/batched.cpp +52 -55
- package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +20 -72
- package/src/llama.cpp/examples/benchmark/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/chat-13B.bat +57 -0
- package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/{finetune → cvector-generator}/CMakeLists.txt +2 -2
- package/src/llama.cpp/examples/cvector-generator/completions.txt +582 -0
- package/src/llama.cpp/examples/cvector-generator/cvector-generator.cpp +503 -0
- package/src/llama.cpp/examples/cvector-generator/mean.hpp +48 -0
- package/src/llama.cpp/examples/cvector-generator/negative.txt +4 -0
- package/src/llama.cpp/examples/cvector-generator/pca.hpp +325 -0
- package/src/llama.cpp/examples/cvector-generator/positive.txt +4 -0
- package/src/llama.cpp/examples/deprecation-warning/deprecation-warning.cpp +35 -0
- package/src/llama.cpp/examples/embedding/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/embedding/embedding.cpp +94 -46
- package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +2 -2
- package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +4 -6
- package/src/llama.cpp/examples/export-lora/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/export-lora/export-lora.cpp +344 -386
- package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +2 -2
- package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +30 -25
- package/src/llama.cpp/examples/gguf/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/gguf/gguf.cpp +5 -0
- package/src/llama.cpp/examples/gguf-hash/CMakeLists.txt +15 -0
- package/src/llama.cpp/examples/gguf-hash/deps/rotate-bits/rotate-bits.h +46 -0
- package/src/llama.cpp/examples/gguf-hash/deps/sha1/sha1.c +295 -0
- package/src/llama.cpp/examples/gguf-hash/deps/sha1/sha1.h +52 -0
- package/src/llama.cpp/examples/gguf-hash/deps/sha256/sha256.c +221 -0
- package/src/llama.cpp/examples/gguf-hash/deps/sha256/sha256.h +24 -0
- package/src/llama.cpp/examples/gguf-hash/deps/xxhash/xxhash.c +42 -0
- package/src/llama.cpp/examples/gguf-hash/deps/xxhash/xxhash.h +7093 -0
- package/src/llama.cpp/examples/gguf-hash/gguf-hash.cpp +693 -0
- package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +3 -3
- package/src/llama.cpp/examples/gritlm/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/gritlm/gritlm.cpp +6 -2
- package/src/llama.cpp/examples/imatrix/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/imatrix/imatrix.cpp +137 -176
- package/src/llama.cpp/examples/infill/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/infill/infill.cpp +38 -153
- package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +175 -94
- package/src/llama.cpp/examples/llama.android/app/build.gradle.kts +65 -0
- package/src/llama.cpp/examples/llama.android/build.gradle.kts +6 -0
- package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +68 -0
- package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/CMakeLists.txt +11 -7
- package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +2 -2
- package/src/llama.cpp/examples/llama.android/settings.gradle.kts +18 -0
- package/src/llama.cpp/examples/llava/CMakeLists.txt +6 -5
- package/src/llama.cpp/examples/llava/android/build_64.sh +8 -0
- package/src/llama.cpp/examples/llava/clip.cpp +23 -14
- package/src/llama.cpp/examples/llava/llava-cli.cpp +8 -6
- package/src/llama.cpp/examples/llava/requirements.txt +3 -2
- package/src/llama.cpp/examples/lookahead/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/lookahead/lookahead.cpp +2 -1
- package/src/llama.cpp/examples/lookup/CMakeLists.txt +4 -4
- package/src/llama.cpp/examples/lookup/lookup-create.cpp +2 -0
- package/src/llama.cpp/examples/lookup/lookup-merge.cpp +4 -4
- package/src/llama.cpp/examples/lookup/lookup-stats.cpp +2 -2
- package/src/llama.cpp/examples/lookup/lookup.cpp +1 -1
- package/src/llama.cpp/examples/main/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/main/main.cpp +98 -75
- package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +4 -5
- package/src/llama.cpp/examples/parallel/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/parallel/parallel.cpp +2 -1
- package/src/llama.cpp/examples/passkey/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/passkey/passkey.cpp +23 -43
- package/src/llama.cpp/examples/perplexity/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/perplexity/perplexity.cpp +13 -10
- package/src/llama.cpp/examples/quantize/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/quantize/quantize.cpp +37 -34
- package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +1 -1
- package/src/llama.cpp/examples/retrieval/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/retrieval/retrieval.cpp +26 -77
- package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +14 -7
- package/src/llama.cpp/examples/server/CMakeLists.txt +26 -2
- package/src/llama.cpp/examples/server/server.cpp +274 -671
- package/src/llama.cpp/examples/server/tests/requirements.txt +2 -2
- package/src/llama.cpp/examples/server/utils.hpp +28 -29
- package/src/llama.cpp/examples/simple/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/simple/simple.cpp +21 -29
- package/src/llama.cpp/examples/speculative/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/speculative/speculative.cpp +2 -1
- package/src/llama.cpp/examples/sycl/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/sycl/build.sh +23 -0
- package/src/llama.cpp/examples/sycl/run-llama2.sh +36 -0
- package/src/llama.cpp/examples/sycl/win-build-sycl.bat +33 -0
- package/src/llama.cpp/examples/sycl/win-run-llama2.bat +9 -0
- package/src/llama.cpp/examples/tokenize/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/tokenize/tokenize.cpp +16 -2
- package/src/llama.cpp/ggml/CMakeLists.txt +253 -0
- package/src/llama.cpp/{cmake → ggml/cmake}/FindSIMD.cmake +6 -6
- package/src/llama.cpp/{ggml-backend.h → ggml/include/ggml-backend.h} +22 -17
- package/src/llama.cpp/ggml/include/ggml-blas.h +23 -0
- package/src/llama.cpp/ggml/include/ggml-cann.h +125 -0
- package/src/llama.cpp/{ggml-cuda.h → ggml/include/ggml-cuda.h} +3 -0
- package/src/llama.cpp/{ggml-metal.h → ggml/include/ggml-metal.h} +1 -2
- package/src/llama.cpp/{ggml-sycl.h → ggml/include/ggml-sycl.h} +3 -10
- package/src/llama.cpp/{ggml.h → ggml/include/ggml.h} +80 -85
- package/src/llama.cpp/ggml/src/CMakeLists.txt +1329 -0
- package/src/llama.cpp/ggml/src/ggml-aarch64.c +2193 -0
- package/src/llama.cpp/ggml/src/ggml-aarch64.h +39 -0
- package/src/llama.cpp/{ggml-alloc.c → ggml/src/ggml-alloc.c} +100 -49
- package/src/llama.cpp/{ggml-backend-impl.h → ggml/src/ggml-backend-impl.h} +20 -8
- package/src/llama.cpp/{ggml-backend.c → ggml/src/ggml-backend.c} +307 -167
- package/src/llama.cpp/ggml/src/ggml-blas.cpp +367 -0
- package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +198 -0
- package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +230 -0
- package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +2944 -0
- package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +592 -0
- package/src/llama.cpp/ggml/src/ggml-cann/common.h +282 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +32 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +17 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/dup.cpp +223 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f16.cpp +186 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f32.cpp +180 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +193 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +191 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +208 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +206 -0
- package/src/llama.cpp/ggml/src/ggml-cann.cpp +2023 -0
- package/src/llama.cpp/{ggml-common.h → ggml/src/ggml-common.h} +41 -7
- package/src/llama.cpp/{ggml-impl.h → ggml/src/ggml-impl.h} +113 -9
- package/src/llama.cpp/{ggml-kompute.cpp → ggml/src/ggml-kompute.cpp} +33 -18
- package/src/llama.cpp/{ggml-quants.c → ggml/src/ggml-quants.c} +1460 -940
- package/src/llama.cpp/{ggml-quants.h → ggml/src/ggml-quants.h} +19 -20
- package/src/llama.cpp/{ggml-rpc.cpp → ggml/src/ggml-rpc.cpp} +95 -72
- package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +27 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +53 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +355 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +195 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/concat.hpp +21 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +547 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/convert.hpp +27 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +698 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +1023 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.hpp +27 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +3011 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/mmq.cpp +3031 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/mmq.hpp +33 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +1027 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.hpp +27 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +374 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/norm.hpp +35 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +66 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +275 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/rope.hpp +22 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/softmax.cpp +251 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/softmax.hpp +24 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +1140 -0
- package/src/llama.cpp/ggml/src/ggml-sycl.cpp +5314 -0
- package/src/llama.cpp/{ggml-vulkan.cpp → ggml/src/ggml-vulkan.cpp} +1781 -1868
- package/src/llama.cpp/{ggml.c → ggml/src/ggml.c} +1245 -2087
- package/src/llama.cpp/{sgemm.cpp → ggml/src/llamafile/sgemm.cpp} +21 -24
- package/src/llama.cpp/{sgemm.h → ggml/src/llamafile/sgemm.h} +1 -1
- package/src/llama.cpp/ggml/src/vulkan-shaders/CMakeLists.txt +5 -0
- package/src/llama.cpp/ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp +552 -0
- package/src/llama.cpp/{llama.h → include/llama.h} +175 -100
- package/src/llama.cpp/models/.editorconfig +1 -0
- package/src/llama.cpp/models/ggml-vocab-aquila.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-baichuan.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-command-r.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-command-r.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-command-r.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-falcon.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-falcon.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-falcon.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-gpt-neox.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-mpt.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-mpt.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-mpt.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-phi-3.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-phi-3.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-phi-3.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-qwen2.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-qwen2.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-qwen2.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-refact.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-refact.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-refact.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-starcoder.gguf +0 -0
- package/src/llama.cpp/models/ggml-vocab-starcoder.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-starcoder.gguf.out +46 -0
- package/src/llama.cpp/pocs/vdot/CMakeLists.txt +2 -2
- package/src/llama.cpp/requirements/requirements-all.txt +12 -0
- package/src/llama.cpp/requirements/requirements-compare-llama-bench.txt +2 -0
- package/src/llama.cpp/requirements/requirements-convert_hf_to_gguf.txt +3 -0
- package/src/llama.cpp/requirements/requirements-convert_hf_to_gguf_update.txt +3 -0
- package/src/llama.cpp/requirements/{requirements-convert.txt → requirements-convert_legacy_llama.txt} +1 -1
- package/src/llama.cpp/requirements/requirements-convert_llama_ggml_to_gguf.txt +1 -0
- package/src/llama.cpp/requirements/requirements-convert_lora_to_gguf.txt +2 -0
- package/src/llama.cpp/requirements/requirements-pydantic.txt +3 -0
- package/src/llama.cpp/requirements/requirements-test-tokenizer-random.txt +1 -0
- package/src/llama.cpp/requirements.txt +5 -4
- package/src/llama.cpp/scripts/build-info.sh +30 -0
- package/src/llama.cpp/scripts/install-oneapi.bat +19 -0
- package/src/llama.cpp/src/CMakeLists.txt +33 -0
- package/src/llama.cpp/src/llama-grammar.cpp +539 -0
- package/src/llama.cpp/src/llama-grammar.h +39 -0
- package/src/llama.cpp/src/llama-impl.h +26 -0
- package/src/llama.cpp/src/llama-sampling.cpp +635 -0
- package/src/llama.cpp/src/llama-sampling.h +56 -0
- package/src/llama.cpp/src/llama-vocab.cpp +1721 -0
- package/src/llama.cpp/src/llama-vocab.h +130 -0
- package/src/llama.cpp/{llama.cpp → src/llama.cpp} +5979 -5260
- package/src/llama.cpp/{unicode-data.cpp → src/unicode-data.cpp} +851 -802
- package/src/llama.cpp/{unicode.cpp → src/unicode.cpp} +52 -30
- package/src/llama.cpp/{unicode.h → src/unicode.h} +5 -1
- package/src/llama.cpp/tests/CMakeLists.txt +19 -20
- package/src/llama.cpp/tests/test-backend-ops.cpp +245 -67
- package/src/llama.cpp/tests/test-chat-template.cpp +57 -3
- package/src/llama.cpp/tests/test-double-float.cpp +2 -2
- package/src/llama.cpp/tests/test-grad0.cpp +2 -2
- package/src/llama.cpp/tests/test-grammar-integration.cpp +978 -31
- package/src/llama.cpp/tests/test-grammar-parser.cpp +423 -158
- package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +508 -135
- package/src/llama.cpp/tests/test-llama-grammar.cpp +15 -9
- package/src/llama.cpp/tests/test-quantize-fns.cpp +1 -1
- package/src/llama.cpp/tests/test-quantize-perf.cpp +1 -1
- package/src/llama.cpp/tests/test-rope.cpp +3 -4
- package/src/llama.cpp/tests/test-sampling.cpp +5 -5
- package/src/llama.cpp/tests/test-tokenizer-0.cpp +6 -6
- package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +20 -15
- package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +22 -11
- package/bin/darwin/arm64/default.metallib +0 -0
- package/bin/darwin/x64/default.metallib +0 -0
- package/src/llama.cpp/examples/beam-search/CMakeLists.txt +0 -5
- package/src/llama.cpp/examples/beam-search/beam-search.cpp +0 -188
- package/src/llama.cpp/examples/finetune/finetune.cpp +0 -1862
- package/src/llama.cpp/examples/llama.android/llama/CMakeLists.txt +0 -55
- package/src/llama.cpp/examples/train-text-from-scratch/CMakeLists.txt +0 -5
- package/src/llama.cpp/examples/train-text-from-scratch/train-text-from-scratch.cpp +0 -1253
- package/src/llama.cpp/ggml-opencl.cpp +0 -2305
- package/src/llama.cpp/ggml-opencl.h +0 -36
- package/src/llama.cpp/ggml-sycl.cpp +0 -17340
- package/src/llama.cpp/ggml-vulkan-shaders.hpp +0 -81211
- package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf-update.txt +0 -2
- package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt +0 -2
- package/src/llama.cpp/requirements/requirements-convert-llama-ggml-to-gguf.txt +0 -1
- package/src/llama.cpp/scripts/gen-build-info-cpp.cmake +0 -24
- /package/src/llama.cpp/{ggml-alloc.h → ggml/include/ggml-alloc.h} +0 -0
- /package/src/llama.cpp/{ggml-kompute.h → ggml/include/ggml-kompute.h} +0 -0
- /package/src/llama.cpp/{ggml-rpc.h → ggml/include/ggml-rpc.h} +0 -0
- /package/src/llama.cpp/{ggml-vulkan.h → ggml/include/ggml-vulkan.h} +0 -0
- /package/src/llama.cpp/{unicode-data.h → src/unicode-data.h} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
set(TARGET gbnf-validator)
|
|
1
|
+
set(TARGET llama-gbnf-validator)
|
|
2
2
|
add_executable(${TARGET} gbnf-validator.cpp)
|
|
3
3
|
install(TARGETS ${TARGET} RUNTIME)
|
|
4
|
-
target_link_libraries(${TARGET} PRIVATE common
|
|
4
|
+
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
|
|
5
5
|
target_compile_features(${TARGET} PRIVATE cxx_std_11)
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
#include <cstdio>
|
|
9
9
|
#include <cstdlib>
|
|
10
|
+
#include <sstream>
|
|
11
|
+
#include <fstream>
|
|
10
12
|
#include <string>
|
|
11
13
|
#include <vector>
|
|
12
14
|
|
|
@@ -14,20 +16,25 @@ static bool llama_sample_grammar_string(struct llama_grammar * grammar, const st
|
|
|
14
16
|
auto decoded = decode_utf8(input_str, {});
|
|
15
17
|
const auto & code_points = decoded.first;
|
|
16
18
|
|
|
19
|
+
const llama_grammar_rules & rules = llama_grammar_get_rules (grammar);
|
|
20
|
+
llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar);
|
|
21
|
+
|
|
17
22
|
size_t pos = 0;
|
|
18
23
|
for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy
|
|
25
|
+
|
|
26
|
+
llama_grammar_accept(rules, prev_stacks, *it, cur_stacks);
|
|
27
|
+
|
|
28
|
+
if (cur_stacks.empty()) {
|
|
22
29
|
error_pos = pos;
|
|
23
30
|
error_msg = "Unexpected character '" + unicode_cpt_to_utf8(*it) + "'";
|
|
24
|
-
|
|
31
|
+
cur_stacks = prev_stacks;
|
|
25
32
|
return false;
|
|
26
33
|
}
|
|
27
34
|
++pos;
|
|
28
35
|
}
|
|
29
36
|
|
|
30
|
-
for (const auto & stack :
|
|
37
|
+
for (const auto & stack : cur_stacks) {
|
|
31
38
|
if (stack.empty()) {
|
|
32
39
|
return true;
|
|
33
40
|
}
|
|
@@ -69,13 +76,14 @@ int main(int argc, char** argv) {
|
|
|
69
76
|
return 1;
|
|
70
77
|
}
|
|
71
78
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
+
std::string grammar_str;
|
|
80
|
+
{
|
|
81
|
+
std::ifstream grammar_file(grammar_filename);
|
|
82
|
+
GGML_ASSERT(grammar_file.is_open() && "Failed to open grammar file");
|
|
83
|
+
std::stringstream buffer;
|
|
84
|
+
buffer << grammar_file.rdbuf();
|
|
85
|
+
grammar_str = buffer.str();
|
|
86
|
+
}
|
|
79
87
|
|
|
80
88
|
// Parse the GBNF grammar
|
|
81
89
|
auto parsed_grammar = grammar_parser::parse(grammar_str.c_str());
|
|
@@ -98,22 +106,19 @@ int main(int argc, char** argv) {
|
|
|
98
106
|
auto grammar = llama_grammar_init(
|
|
99
107
|
grammar_rules.data(),
|
|
100
108
|
grammar_rules.size(), parsed_grammar.symbol_ids.at("root"));
|
|
101
|
-
|
|
109
|
+
if (grammar == nullptr) {
|
|
110
|
+
throw std::runtime_error("Failed to initialize llama_grammar");
|
|
111
|
+
}
|
|
102
112
|
// Read the input file
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
113
|
+
std::string input_str;
|
|
114
|
+
{
|
|
115
|
+
std::ifstream input_file(input_filename);
|
|
116
|
+
GGML_ASSERT(input_file.is_open() && "Failed to open input file");
|
|
117
|
+
std::stringstream buffer;
|
|
118
|
+
buffer << input_file.rdbuf();
|
|
119
|
+
input_str = buffer.str();
|
|
107
120
|
}
|
|
108
121
|
|
|
109
|
-
fseek(input_file, 0, SEEK_END);
|
|
110
|
-
size_t input_size = ftell(input_file);
|
|
111
|
-
fseek(input_file, 0, SEEK_SET);
|
|
112
|
-
|
|
113
|
-
std::string input_str(input_size, ' ');
|
|
114
|
-
fread(&input_str[0], 1, input_size, input_file);
|
|
115
|
-
fclose(input_file);
|
|
116
|
-
|
|
117
122
|
// Validate the input string against the grammar
|
|
118
123
|
size_t error_pos;
|
|
119
124
|
std::string error_msg;
|
|
@@ -92,6 +92,11 @@ static bool gguf_ex_read_0(const std::string & fname) {
|
|
|
92
92
|
|
|
93
93
|
struct gguf_context * ctx = gguf_init_from_file(fname.c_str(), params);
|
|
94
94
|
|
|
95
|
+
if (!ctx) {
|
|
96
|
+
fprintf(stderr, "%s: failed to load '%s'\n", __func__, fname.c_str());
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
|
|
95
100
|
printf("%s: version: %d\n", __func__, gguf_get_version(ctx));
|
|
96
101
|
printf("%s: alignment: %zu\n", __func__, gguf_get_alignment(ctx));
|
|
97
102
|
printf("%s: data offset: %zu\n", __func__, gguf_get_data_offset(ctx));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
set(TARGET llama-gguf-hash)
|
|
2
|
+
add_executable(${TARGET} gguf-hash.cpp)
|
|
3
|
+
install(TARGETS ${TARGET} RUNTIME)
|
|
4
|
+
|
|
5
|
+
# clibs dependencies
|
|
6
|
+
include_directories(deps/)
|
|
7
|
+
add_library(xxhash OBJECT deps/xxhash/xxhash.c deps/xxhash/xxhash.h)
|
|
8
|
+
target_link_libraries(${TARGET} PRIVATE xxhash)
|
|
9
|
+
add_library(sha1 OBJECT deps/sha1/sha1.c deps/sha1/sha1.h)
|
|
10
|
+
target_link_libraries(${TARGET} PRIVATE sha1)
|
|
11
|
+
add_library(sha256 OBJECT deps/sha256/sha256.c deps/sha256/sha256.h)
|
|
12
|
+
target_link_libraries(${TARGET} PRIVATE sha256)
|
|
13
|
+
|
|
14
|
+
target_link_libraries(${TARGET} PRIVATE ggml ${CMAKE_THREAD_LIBS_INIT})
|
|
15
|
+
target_compile_features(${TARGET} PRIVATE cxx_std_11)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
#ifndef __ROTATE_DEFS_H
|
|
4
|
+
#define __ROTATE_DEFS_H
|
|
5
|
+
|
|
6
|
+
#ifdef _MSC_VER
|
|
7
|
+
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
|
|
10
|
+
#define ROTL32(v, n) _rotl((v), (n))
|
|
11
|
+
#define ROTL64(v, n) _rotl64((v), (n))
|
|
12
|
+
|
|
13
|
+
#define ROTR32(v, n) _rotr((v), (n))
|
|
14
|
+
#define ROTR64(v, n) _rotr64((v), (n))
|
|
15
|
+
|
|
16
|
+
#else
|
|
17
|
+
|
|
18
|
+
#include <stdint.h>
|
|
19
|
+
|
|
20
|
+
#define U8V(v) ((uint8_t)(v) & 0xFFU)
|
|
21
|
+
#define U16V(v) ((uint16_t)(v) & 0xFFFFU)
|
|
22
|
+
#define U32V(v) ((uint32_t)(v) & 0xFFFFFFFFU)
|
|
23
|
+
#define U64V(v) ((uint64_t)(v) & 0xFFFFFFFFFFFFFFFFU)
|
|
24
|
+
|
|
25
|
+
#define ROTL32(v, n) \
|
|
26
|
+
(U32V((uint32_t)(v) << (n)) | ((uint32_t)(v) >> (32 - (n))))
|
|
27
|
+
|
|
28
|
+
// tests fail if we don't have this cast...
|
|
29
|
+
#define ROTL64(v, n) \
|
|
30
|
+
(U64V((uint64_t)(v) << (n)) | ((uint64_t)(v) >> (64 - (n))))
|
|
31
|
+
|
|
32
|
+
#define ROTR32(v, n) ROTL32(v, 32 - (n))
|
|
33
|
+
#define ROTR64(v, n) ROTL64(v, 64 - (n))
|
|
34
|
+
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
#define ROTL8(v, n) \
|
|
38
|
+
(U8V((uint8_t)(v) << (n)) | ((uint8_t)(v) >> (8 - (n))))
|
|
39
|
+
|
|
40
|
+
#define ROTL16(v, n) \
|
|
41
|
+
(U16V((uint16_t)(v) << (n)) | ((uint16_t)(v) >> (16 - (n))))
|
|
42
|
+
|
|
43
|
+
#define ROTR8(v, n) ROTL8(v, 8 - (n))
|
|
44
|
+
#define ROTR16(v, n) ROTL16(v, 16 - (n))
|
|
45
|
+
|
|
46
|
+
#endif
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SHA-1 in C
|
|
3
|
+
By Steve Reid <steve@edmweb.com>
|
|
4
|
+
100% Public Domain
|
|
5
|
+
|
|
6
|
+
Test Vectors (from FIPS PUB 180-1)
|
|
7
|
+
"abc"
|
|
8
|
+
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
|
|
9
|
+
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
|
|
10
|
+
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
|
|
11
|
+
A million repetitions of "a"
|
|
12
|
+
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
|
|
16
|
+
/* #define SHA1HANDSOFF * Copies data before messing with it. */
|
|
17
|
+
|
|
18
|
+
#define SHA1HANDSOFF
|
|
19
|
+
|
|
20
|
+
#include <stdio.h>
|
|
21
|
+
#include <string.h>
|
|
22
|
+
|
|
23
|
+
/* for uint32_t */
|
|
24
|
+
#include <stdint.h>
|
|
25
|
+
|
|
26
|
+
#include "sha1.h"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
|
|
30
|
+
|
|
31
|
+
/* blk0() and blk() perform the initial expand. */
|
|
32
|
+
/* I got the idea of expanding during the round function from SSLeay */
|
|
33
|
+
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
34
|
+
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|
|
35
|
+
|(rol(block->l[i],8)&0x00FF00FF))
|
|
36
|
+
#elif BYTE_ORDER == BIG_ENDIAN
|
|
37
|
+
#define blk0(i) block->l[i]
|
|
38
|
+
#else
|
|
39
|
+
#error "Endianness not defined!"
|
|
40
|
+
#endif
|
|
41
|
+
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
|
|
42
|
+
^block->l[(i+2)&15]^block->l[i&15],1))
|
|
43
|
+
|
|
44
|
+
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
|
|
45
|
+
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
|
|
46
|
+
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
|
|
47
|
+
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
|
|
48
|
+
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
|
|
49
|
+
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
/* Hash a single 512-bit block. This is the core of the algorithm. */
|
|
53
|
+
|
|
54
|
+
void SHA1Transform(
|
|
55
|
+
uint32_t state[5],
|
|
56
|
+
const unsigned char buffer[64]
|
|
57
|
+
)
|
|
58
|
+
{
|
|
59
|
+
uint32_t a, b, c, d, e;
|
|
60
|
+
|
|
61
|
+
typedef union
|
|
62
|
+
{
|
|
63
|
+
unsigned char c[64];
|
|
64
|
+
uint32_t l[16];
|
|
65
|
+
} CHAR64LONG16;
|
|
66
|
+
|
|
67
|
+
#ifdef SHA1HANDSOFF
|
|
68
|
+
CHAR64LONG16 block[1]; /* use array to appear as a pointer */
|
|
69
|
+
|
|
70
|
+
memcpy(block, buffer, 64);
|
|
71
|
+
#else
|
|
72
|
+
/* The following had better never be used because it causes the
|
|
73
|
+
* pointer-to-const buffer to be cast into a pointer to non-const.
|
|
74
|
+
* And the result is written through. I threw a "const" in, hoping
|
|
75
|
+
* this will cause a diagnostic.
|
|
76
|
+
*/
|
|
77
|
+
CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer;
|
|
78
|
+
#endif
|
|
79
|
+
/* Copy context->state[] to working vars */
|
|
80
|
+
a = state[0];
|
|
81
|
+
b = state[1];
|
|
82
|
+
c = state[2];
|
|
83
|
+
d = state[3];
|
|
84
|
+
e = state[4];
|
|
85
|
+
/* 4 rounds of 20 operations each. Loop unrolled. */
|
|
86
|
+
R0(a, b, c, d, e, 0);
|
|
87
|
+
R0(e, a, b, c, d, 1);
|
|
88
|
+
R0(d, e, a, b, c, 2);
|
|
89
|
+
R0(c, d, e, a, b, 3);
|
|
90
|
+
R0(b, c, d, e, a, 4);
|
|
91
|
+
R0(a, b, c, d, e, 5);
|
|
92
|
+
R0(e, a, b, c, d, 6);
|
|
93
|
+
R0(d, e, a, b, c, 7);
|
|
94
|
+
R0(c, d, e, a, b, 8);
|
|
95
|
+
R0(b, c, d, e, a, 9);
|
|
96
|
+
R0(a, b, c, d, e, 10);
|
|
97
|
+
R0(e, a, b, c, d, 11);
|
|
98
|
+
R0(d, e, a, b, c, 12);
|
|
99
|
+
R0(c, d, e, a, b, 13);
|
|
100
|
+
R0(b, c, d, e, a, 14);
|
|
101
|
+
R0(a, b, c, d, e, 15);
|
|
102
|
+
R1(e, a, b, c, d, 16);
|
|
103
|
+
R1(d, e, a, b, c, 17);
|
|
104
|
+
R1(c, d, e, a, b, 18);
|
|
105
|
+
R1(b, c, d, e, a, 19);
|
|
106
|
+
R2(a, b, c, d, e, 20);
|
|
107
|
+
R2(e, a, b, c, d, 21);
|
|
108
|
+
R2(d, e, a, b, c, 22);
|
|
109
|
+
R2(c, d, e, a, b, 23);
|
|
110
|
+
R2(b, c, d, e, a, 24);
|
|
111
|
+
R2(a, b, c, d, e, 25);
|
|
112
|
+
R2(e, a, b, c, d, 26);
|
|
113
|
+
R2(d, e, a, b, c, 27);
|
|
114
|
+
R2(c, d, e, a, b, 28);
|
|
115
|
+
R2(b, c, d, e, a, 29);
|
|
116
|
+
R2(a, b, c, d, e, 30);
|
|
117
|
+
R2(e, a, b, c, d, 31);
|
|
118
|
+
R2(d, e, a, b, c, 32);
|
|
119
|
+
R2(c, d, e, a, b, 33);
|
|
120
|
+
R2(b, c, d, e, a, 34);
|
|
121
|
+
R2(a, b, c, d, e, 35);
|
|
122
|
+
R2(e, a, b, c, d, 36);
|
|
123
|
+
R2(d, e, a, b, c, 37);
|
|
124
|
+
R2(c, d, e, a, b, 38);
|
|
125
|
+
R2(b, c, d, e, a, 39);
|
|
126
|
+
R3(a, b, c, d, e, 40);
|
|
127
|
+
R3(e, a, b, c, d, 41);
|
|
128
|
+
R3(d, e, a, b, c, 42);
|
|
129
|
+
R3(c, d, e, a, b, 43);
|
|
130
|
+
R3(b, c, d, e, a, 44);
|
|
131
|
+
R3(a, b, c, d, e, 45);
|
|
132
|
+
R3(e, a, b, c, d, 46);
|
|
133
|
+
R3(d, e, a, b, c, 47);
|
|
134
|
+
R3(c, d, e, a, b, 48);
|
|
135
|
+
R3(b, c, d, e, a, 49);
|
|
136
|
+
R3(a, b, c, d, e, 50);
|
|
137
|
+
R3(e, a, b, c, d, 51);
|
|
138
|
+
R3(d, e, a, b, c, 52);
|
|
139
|
+
R3(c, d, e, a, b, 53);
|
|
140
|
+
R3(b, c, d, e, a, 54);
|
|
141
|
+
R3(a, b, c, d, e, 55);
|
|
142
|
+
R3(e, a, b, c, d, 56);
|
|
143
|
+
R3(d, e, a, b, c, 57);
|
|
144
|
+
R3(c, d, e, a, b, 58);
|
|
145
|
+
R3(b, c, d, e, a, 59);
|
|
146
|
+
R4(a, b, c, d, e, 60);
|
|
147
|
+
R4(e, a, b, c, d, 61);
|
|
148
|
+
R4(d, e, a, b, c, 62);
|
|
149
|
+
R4(c, d, e, a, b, 63);
|
|
150
|
+
R4(b, c, d, e, a, 64);
|
|
151
|
+
R4(a, b, c, d, e, 65);
|
|
152
|
+
R4(e, a, b, c, d, 66);
|
|
153
|
+
R4(d, e, a, b, c, 67);
|
|
154
|
+
R4(c, d, e, a, b, 68);
|
|
155
|
+
R4(b, c, d, e, a, 69);
|
|
156
|
+
R4(a, b, c, d, e, 70);
|
|
157
|
+
R4(e, a, b, c, d, 71);
|
|
158
|
+
R4(d, e, a, b, c, 72);
|
|
159
|
+
R4(c, d, e, a, b, 73);
|
|
160
|
+
R4(b, c, d, e, a, 74);
|
|
161
|
+
R4(a, b, c, d, e, 75);
|
|
162
|
+
R4(e, a, b, c, d, 76);
|
|
163
|
+
R4(d, e, a, b, c, 77);
|
|
164
|
+
R4(c, d, e, a, b, 78);
|
|
165
|
+
R4(b, c, d, e, a, 79);
|
|
166
|
+
/* Add the working vars back into context.state[] */
|
|
167
|
+
state[0] += a;
|
|
168
|
+
state[1] += b;
|
|
169
|
+
state[2] += c;
|
|
170
|
+
state[3] += d;
|
|
171
|
+
state[4] += e;
|
|
172
|
+
/* Wipe variables */
|
|
173
|
+
a = b = c = d = e = 0;
|
|
174
|
+
#ifdef SHA1HANDSOFF
|
|
175
|
+
memset(block, '\0', sizeof(block));
|
|
176
|
+
#endif
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
/* SHA1Init - Initialize new context */
|
|
181
|
+
|
|
182
|
+
void SHA1Init(
|
|
183
|
+
SHA1_CTX * context
|
|
184
|
+
)
|
|
185
|
+
{
|
|
186
|
+
/* SHA1 initialization constants */
|
|
187
|
+
context->state[0] = 0x67452301;
|
|
188
|
+
context->state[1] = 0xEFCDAB89;
|
|
189
|
+
context->state[2] = 0x98BADCFE;
|
|
190
|
+
context->state[3] = 0x10325476;
|
|
191
|
+
context->state[4] = 0xC3D2E1F0;
|
|
192
|
+
context->count[0] = context->count[1] = 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
/* Run your data through this. */
|
|
197
|
+
|
|
198
|
+
void SHA1Update(
|
|
199
|
+
SHA1_CTX * context,
|
|
200
|
+
const unsigned char *data,
|
|
201
|
+
uint32_t len
|
|
202
|
+
)
|
|
203
|
+
{
|
|
204
|
+
uint32_t i;
|
|
205
|
+
|
|
206
|
+
uint32_t j;
|
|
207
|
+
|
|
208
|
+
j = context->count[0];
|
|
209
|
+
if ((context->count[0] += len << 3) < j)
|
|
210
|
+
context->count[1]++;
|
|
211
|
+
context->count[1] += (len >> 29);
|
|
212
|
+
j = (j >> 3) & 63;
|
|
213
|
+
if ((j + len) > 63)
|
|
214
|
+
{
|
|
215
|
+
memcpy(&context->buffer[j], data, (i = 64 - j));
|
|
216
|
+
SHA1Transform(context->state, context->buffer);
|
|
217
|
+
for (; i + 63 < len; i += 64)
|
|
218
|
+
{
|
|
219
|
+
SHA1Transform(context->state, &data[i]);
|
|
220
|
+
}
|
|
221
|
+
j = 0;
|
|
222
|
+
}
|
|
223
|
+
else
|
|
224
|
+
i = 0;
|
|
225
|
+
memcpy(&context->buffer[j], &data[i], len - i);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
/* Add padding and return the message digest. */
|
|
230
|
+
|
|
231
|
+
void SHA1Final(
|
|
232
|
+
unsigned char digest[20],
|
|
233
|
+
SHA1_CTX * context
|
|
234
|
+
)
|
|
235
|
+
{
|
|
236
|
+
unsigned i;
|
|
237
|
+
|
|
238
|
+
unsigned char finalcount[8];
|
|
239
|
+
|
|
240
|
+
unsigned char c;
|
|
241
|
+
|
|
242
|
+
#if 0 /* untested "improvement" by DHR */
|
|
243
|
+
/* Convert context->count to a sequence of bytes
|
|
244
|
+
* in finalcount. Second element first, but
|
|
245
|
+
* big-endian order within element.
|
|
246
|
+
* But we do it all backwards.
|
|
247
|
+
*/
|
|
248
|
+
unsigned char *fcp = &finalcount[8];
|
|
249
|
+
|
|
250
|
+
for (i = 0; i < 2; i++)
|
|
251
|
+
{
|
|
252
|
+
uint32_t t = context->count[i];
|
|
253
|
+
|
|
254
|
+
int j;
|
|
255
|
+
|
|
256
|
+
for (j = 0; j < 4; t >>= 8, j++)
|
|
257
|
+
*--fcp = (unsigned char) t}
|
|
258
|
+
#else
|
|
259
|
+
for (i = 0; i < 8; i++)
|
|
260
|
+
{
|
|
261
|
+
finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
|
|
262
|
+
}
|
|
263
|
+
#endif
|
|
264
|
+
c = 0200;
|
|
265
|
+
SHA1Update(context, &c, 1);
|
|
266
|
+
while ((context->count[0] & 504) != 448)
|
|
267
|
+
{
|
|
268
|
+
c = 0000;
|
|
269
|
+
SHA1Update(context, &c, 1);
|
|
270
|
+
}
|
|
271
|
+
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
|
|
272
|
+
for (i = 0; i < 20; i++)
|
|
273
|
+
{
|
|
274
|
+
digest[i] = (unsigned char)
|
|
275
|
+
((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
|
|
276
|
+
}
|
|
277
|
+
/* Wipe variables */
|
|
278
|
+
memset(context, '\0', sizeof(*context));
|
|
279
|
+
memset(&finalcount, '\0', sizeof(finalcount));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
void SHA1(
|
|
283
|
+
char *hash_out,
|
|
284
|
+
const char *str,
|
|
285
|
+
uint32_t len)
|
|
286
|
+
{
|
|
287
|
+
SHA1_CTX ctx;
|
|
288
|
+
unsigned int ii;
|
|
289
|
+
|
|
290
|
+
SHA1Init(&ctx);
|
|
291
|
+
for (ii=0; ii<len; ii+=1)
|
|
292
|
+
SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
|
|
293
|
+
SHA1Final((unsigned char *)hash_out, &ctx);
|
|
294
|
+
}
|
|
295
|
+
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
#ifndef SHA1_H
|
|
2
|
+
#define SHA1_H
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
SHA-1 in C
|
|
6
|
+
By Steve Reid <steve@edmweb.com>
|
|
7
|
+
100% Public Domain
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#include "stdint.h"
|
|
11
|
+
|
|
12
|
+
#if defined(__cplusplus)
|
|
13
|
+
extern "C" {
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
typedef struct
|
|
17
|
+
{
|
|
18
|
+
uint32_t state[5];
|
|
19
|
+
uint32_t count[2];
|
|
20
|
+
unsigned char buffer[64];
|
|
21
|
+
} SHA1_CTX;
|
|
22
|
+
|
|
23
|
+
void SHA1Transform(
|
|
24
|
+
uint32_t state[5],
|
|
25
|
+
const unsigned char buffer[64]
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
void SHA1Init(
|
|
29
|
+
SHA1_CTX * context
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
void SHA1Update(
|
|
33
|
+
SHA1_CTX * context,
|
|
34
|
+
const unsigned char *data,
|
|
35
|
+
uint32_t len
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
void SHA1Final(
|
|
39
|
+
unsigned char digest[20],
|
|
40
|
+
SHA1_CTX * context
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
void SHA1(
|
|
44
|
+
char *hash_out,
|
|
45
|
+
const char *str,
|
|
46
|
+
uint32_t len);
|
|
47
|
+
|
|
48
|
+
#if defined(__cplusplus)
|
|
49
|
+
}
|
|
50
|
+
#endif
|
|
51
|
+
|
|
52
|
+
#endif /* SHA1_H */
|