@realtimex/node-llama-cpp 0.1.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/LICENSE +21 -0
- package/README.md +108 -0
- package/dist/ChatWrapper.d.ts +27 -0
- package/dist/ChatWrapper.js +233 -0
- package/dist/ChatWrapper.js.map +1 -0
- package/dist/apiDocsIndex.d.ts +1 -0
- package/dist/apiDocsIndex.js +7 -0
- package/dist/apiDocsIndex.js.map +1 -0
- package/dist/bindings/AddonTypes.d.ts +203 -0
- package/dist/bindings/AddonTypes.js +2 -0
- package/dist/bindings/AddonTypes.js.map +1 -0
- package/dist/bindings/Llama.d.ts +104 -0
- package/dist/bindings/Llama.js +570 -0
- package/dist/bindings/Llama.js.map +1 -0
- package/dist/bindings/consts.d.ts +2 -0
- package/dist/bindings/consts.js +13 -0
- package/dist/bindings/consts.js.map +1 -0
- package/dist/bindings/getLlama.d.ts +297 -0
- package/dist/bindings/getLlama.js +569 -0
- package/dist/bindings/getLlama.js.map +1 -0
- package/dist/bindings/types.d.ts +72 -0
- package/dist/bindings/types.js +105 -0
- package/dist/bindings/types.js.map +1 -0
- package/dist/bindings/utils/MemoryOrchestrator.d.ts +23 -0
- package/dist/bindings/utils/MemoryOrchestrator.js +50 -0
- package/dist/bindings/utils/MemoryOrchestrator.js.map +1 -0
- package/dist/bindings/utils/NoBinaryFoundError.d.ts +2 -0
- package/dist/bindings/utils/NoBinaryFoundError.js +7 -0
- package/dist/bindings/utils/NoBinaryFoundError.js.map +1 -0
- package/dist/bindings/utils/asyncEvery.d.ts +5 -0
- package/dist/bindings/utils/asyncEvery.js +15 -0
- package/dist/bindings/utils/asyncEvery.js.map +1 -0
- package/dist/bindings/utils/asyncSome.d.ts +5 -0
- package/dist/bindings/utils/asyncSome.js +29 -0
- package/dist/bindings/utils/asyncSome.js.map +1 -0
- package/dist/bindings/utils/binariesGithubRelease.d.ts +6 -0
- package/dist/bindings/utils/binariesGithubRelease.js +15 -0
- package/dist/bindings/utils/binariesGithubRelease.js.map +1 -0
- package/dist/bindings/utils/clearAllLocalBuilds.d.ts +1 -0
- package/dist/bindings/utils/clearAllLocalBuilds.js +47 -0
- package/dist/bindings/utils/clearAllLocalBuilds.js.map +1 -0
- package/dist/bindings/utils/cloneLlamaCppRepo.d.ts +11 -0
- package/dist/bindings/utils/cloneLlamaCppRepo.js +166 -0
- package/dist/bindings/utils/cloneLlamaCppRepo.js.map +1 -0
- package/dist/bindings/utils/compileLLamaCpp.d.ts +22 -0
- package/dist/bindings/utils/compileLLamaCpp.js +526 -0
- package/dist/bindings/utils/compileLLamaCpp.js.map +1 -0
- package/dist/bindings/utils/detectAvailableComputeLayers.d.ts +18 -0
- package/dist/bindings/utils/detectAvailableComputeLayers.js +311 -0
- package/dist/bindings/utils/detectAvailableComputeLayers.js.map +1 -0
- package/dist/bindings/utils/detectBuildTools.d.ts +14 -0
- package/dist/bindings/utils/detectBuildTools.js +149 -0
- package/dist/bindings/utils/detectBuildTools.js.map +1 -0
- package/dist/bindings/utils/detectGlibc.d.ts +4 -0
- package/dist/bindings/utils/detectGlibc.js +74 -0
- package/dist/bindings/utils/detectGlibc.js.map +1 -0
- package/dist/bindings/utils/disposeBeforeExit.d.ts +8 -0
- package/dist/bindings/utils/disposeBeforeExit.js +36 -0
- package/dist/bindings/utils/disposeBeforeExit.js.map +1 -0
- package/dist/bindings/utils/getBestComputeLayersAvailable.d.ts +9 -0
- package/dist/bindings/utils/getBestComputeLayersAvailable.js +29 -0
- package/dist/bindings/utils/getBestComputeLayersAvailable.js.map +1 -0
- package/dist/bindings/utils/getBuildFolderNameForBuildOptions.d.ts +6 -0
- package/dist/bindings/utils/getBuildFolderNameForBuildOptions.js +105 -0
- package/dist/bindings/utils/getBuildFolderNameForBuildOptions.js.map +1 -0
- package/dist/bindings/utils/getCanUsePrebuiltBinaries.d.ts +1 -0
- package/dist/bindings/utils/getCanUsePrebuiltBinaries.js +8 -0
- package/dist/bindings/utils/getCanUsePrebuiltBinaries.js.map +1 -0
- package/dist/bindings/utils/getExampleUsageCodeOfGetLlama.d.ts +2 -0
- package/dist/bindings/utils/getExampleUsageCodeOfGetLlama.js +21 -0
- package/dist/bindings/utils/getExampleUsageCodeOfGetLlama.js.map +1 -0
- package/dist/bindings/utils/getGpuTypesToUseForOption.d.ts +12 -0
- package/dist/bindings/utils/getGpuTypesToUseForOption.js +39 -0
- package/dist/bindings/utils/getGpuTypesToUseForOption.js.map +1 -0
- package/dist/bindings/utils/getLinuxDistroInfo.d.ts +9 -0
- package/dist/bindings/utils/getLinuxDistroInfo.js +46 -0
- package/dist/bindings/utils/getLinuxDistroInfo.js.map +1 -0
- package/dist/bindings/utils/getLlamaGpuTypes.d.ts +13 -0
- package/dist/bindings/utils/getLlamaGpuTypes.js +36 -0
- package/dist/bindings/utils/getLlamaGpuTypes.js.map +1 -0
- package/dist/bindings/utils/getLlamaWithoutBackend.d.ts +5 -0
- package/dist/bindings/utils/getLlamaWithoutBackend.js +40 -0
- package/dist/bindings/utils/getLlamaWithoutBackend.js.map +1 -0
- package/dist/bindings/utils/getPlatform.d.ts +2 -0
- package/dist/bindings/utils/getPlatform.js +15 -0
- package/dist/bindings/utils/getPlatform.js.map +1 -0
- package/dist/bindings/utils/getPlatformInfo.d.ts +5 -0
- package/dist/bindings/utils/getPlatformInfo.js +28 -0
- package/dist/bindings/utils/getPlatformInfo.js.map +1 -0
- package/dist/bindings/utils/hasBuildingFromSourceDependenciesInstalled.d.ts +3 -0
- package/dist/bindings/utils/hasBuildingFromSourceDependenciesInstalled.js +27 -0
- package/dist/bindings/utils/hasBuildingFromSourceDependenciesInstalled.js.map +1 -0
- package/dist/bindings/utils/hasFileInPath.d.ts +2 -0
- package/dist/bindings/utils/hasFileInPath.js +34 -0
- package/dist/bindings/utils/hasFileInPath.js.map +1 -0
- package/dist/bindings/utils/lastBuildInfo.d.ts +6 -0
- package/dist/bindings/utils/lastBuildInfo.js +17 -0
- package/dist/bindings/utils/lastBuildInfo.js.map +1 -0
- package/dist/bindings/utils/logBinaryUsageExampleToConsole.d.ts +2 -0
- package/dist/bindings/utils/logBinaryUsageExampleToConsole.js +22 -0
- package/dist/bindings/utils/logBinaryUsageExampleToConsole.js.map +1 -0
- package/dist/bindings/utils/logDistroInstallInstruction.d.ts +14 -0
- package/dist/bindings/utils/logDistroInstallInstruction.js +48 -0
- package/dist/bindings/utils/logDistroInstallInstruction.js.map +1 -0
- package/dist/bindings/utils/resolveActualBindingBinaryPath.d.ts +1 -0
- package/dist/bindings/utils/resolveActualBindingBinaryPath.js +18 -0
- package/dist/bindings/utils/resolveActualBindingBinaryPath.js.map +1 -0
- package/dist/bindings/utils/resolveCustomCmakeOptions.d.ts +1 -0
- package/dist/bindings/utils/resolveCustomCmakeOptions.js +43 -0
- package/dist/bindings/utils/resolveCustomCmakeOptions.js.map +1 -0
- package/dist/bindings/utils/testBindingBinary.d.ts +2 -0
- package/dist/bindings/utils/testBindingBinary.js +269 -0
- package/dist/bindings/utils/testBindingBinary.js.map +1 -0
- package/dist/bindings/utils/testCmakeBinary.d.ts +5 -0
- package/dist/bindings/utils/testCmakeBinary.js +32 -0
- package/dist/bindings/utils/testCmakeBinary.js.map +1 -0
- package/dist/chatWrappers/AlpacaChatWrapper.d.ts +17 -0
- package/dist/chatWrappers/AlpacaChatWrapper.js +33 -0
- package/dist/chatWrappers/AlpacaChatWrapper.js.map +1 -0
- package/dist/chatWrappers/ChatMLChatWrapper.d.ts +6 -0
- package/dist/chatWrappers/ChatMLChatWrapper.js +85 -0
- package/dist/chatWrappers/ChatMLChatWrapper.js.map +1 -0
- package/dist/chatWrappers/DeepSeekChatWrapper.d.ts +37 -0
- package/dist/chatWrappers/DeepSeekChatWrapper.js +294 -0
- package/dist/chatWrappers/DeepSeekChatWrapper.js.map +1 -0
- package/dist/chatWrappers/EmptyChatWrapper.d.ts +4 -0
- package/dist/chatWrappers/EmptyChatWrapper.js +5 -0
- package/dist/chatWrappers/EmptyChatWrapper.js.map +1 -0
- package/dist/chatWrappers/FalconChatWrapper.d.ts +19 -0
- package/dist/chatWrappers/FalconChatWrapper.js +126 -0
- package/dist/chatWrappers/FalconChatWrapper.js.map +1 -0
- package/dist/chatWrappers/FunctionaryChatWrapper.d.ts +17 -0
- package/dist/chatWrappers/FunctionaryChatWrapper.js +622 -0
- package/dist/chatWrappers/FunctionaryChatWrapper.js.map +1 -0
- package/dist/chatWrappers/GemmaChatWrapper.d.ts +7 -0
- package/dist/chatWrappers/GemmaChatWrapper.js +96 -0
- package/dist/chatWrappers/GemmaChatWrapper.js.map +1 -0
- package/dist/chatWrappers/GeneralChatWrapper.d.ts +19 -0
- package/dist/chatWrappers/GeneralChatWrapper.js +140 -0
- package/dist/chatWrappers/GeneralChatWrapper.js.map +1 -0
- package/dist/chatWrappers/HarmonyChatWrapper.d.ts +78 -0
- package/dist/chatWrappers/HarmonyChatWrapper.js +539 -0
- package/dist/chatWrappers/HarmonyChatWrapper.js.map +1 -0
- package/dist/chatWrappers/Llama2ChatWrapper.d.ts +12 -0
- package/dist/chatWrappers/Llama2ChatWrapper.js +95 -0
- package/dist/chatWrappers/Llama2ChatWrapper.js.map +1 -0
- package/dist/chatWrappers/Llama3ChatWrapper.d.ts +16 -0
- package/dist/chatWrappers/Llama3ChatWrapper.js +173 -0
- package/dist/chatWrappers/Llama3ChatWrapper.js.map +1 -0
- package/dist/chatWrappers/Llama3_1ChatWrapper.d.ts +32 -0
- package/dist/chatWrappers/Llama3_1ChatWrapper.js +290 -0
- package/dist/chatWrappers/Llama3_1ChatWrapper.js.map +1 -0
- package/dist/chatWrappers/Llama3_2LightweightChatWrapper.d.ts +35 -0
- package/dist/chatWrappers/Llama3_2LightweightChatWrapper.js +264 -0
- package/dist/chatWrappers/Llama3_2LightweightChatWrapper.js.map +1 -0
- package/dist/chatWrappers/MistralChatWrapper.d.ts +16 -0
- package/dist/chatWrappers/MistralChatWrapper.js +180 -0
- package/dist/chatWrappers/MistralChatWrapper.js.map +1 -0
- package/dist/chatWrappers/QwenChatWrapper.d.ts +36 -0
- package/dist/chatWrappers/QwenChatWrapper.js +344 -0
- package/dist/chatWrappers/QwenChatWrapper.js.map +1 -0
- package/dist/chatWrappers/SeedChatWrapper.d.ts +25 -0
- package/dist/chatWrappers/SeedChatWrapper.js +183 -0
- package/dist/chatWrappers/SeedChatWrapper.js.map +1 -0
- package/dist/chatWrappers/generic/JinjaTemplateChatWrapper.d.ts +138 -0
- package/dist/chatWrappers/generic/JinjaTemplateChatWrapper.js +665 -0
- package/dist/chatWrappers/generic/JinjaTemplateChatWrapper.js.map +1 -0
- package/dist/chatWrappers/generic/TemplateChatWrapper.d.ts +76 -0
- package/dist/chatWrappers/generic/TemplateChatWrapper.js +212 -0
- package/dist/chatWrappers/generic/TemplateChatWrapper.js.map +1 -0
- package/dist/chatWrappers/generic/utils/UniqueIdGenerator.d.ts +7 -0
- package/dist/chatWrappers/generic/utils/UniqueIdGenerator.js +30 -0
- package/dist/chatWrappers/generic/utils/UniqueIdGenerator.js.map +1 -0
- package/dist/chatWrappers/generic/utils/chatHistoryFunctionCallMessageTemplate.d.ts +24 -0
- package/dist/chatWrappers/generic/utils/chatHistoryFunctionCallMessageTemplate.js +45 -0
- package/dist/chatWrappers/generic/utils/chatHistoryFunctionCallMessageTemplate.js.map +1 -0
- package/dist/chatWrappers/generic/utils/extractFunctionCallSettingsFromJinjaTemplate.d.ts +25 -0
- package/dist/chatWrappers/generic/utils/extractFunctionCallSettingsFromJinjaTemplate.js +690 -0
- package/dist/chatWrappers/generic/utils/extractFunctionCallSettingsFromJinjaTemplate.js.map +1 -0
- package/dist/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.d.ts +2 -0
- package/dist/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.js +66 -0
- package/dist/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.js.map +1 -0
- package/dist/chatWrappers/generic/utils/getFirstValidResult.d.ts +6 -0
- package/dist/chatWrappers/generic/utils/getFirstValidResult.js +19 -0
- package/dist/chatWrappers/generic/utils/getFirstValidResult.js.map +1 -0
- package/dist/chatWrappers/generic/utils/squashChatHistoryItems.d.ts +2 -0
- package/dist/chatWrappers/generic/utils/squashChatHistoryItems.js +35 -0
- package/dist/chatWrappers/generic/utils/squashChatHistoryItems.js.map +1 -0
- package/dist/chatWrappers/generic/utils/templateSegmentOptionsToChatWrapperSettings.d.ts +22 -0
- package/dist/chatWrappers/generic/utils/templateSegmentOptionsToChatWrapperSettings.js +28 -0
- package/dist/chatWrappers/generic/utils/templateSegmentOptionsToChatWrapperSettings.js.map +1 -0
- package/dist/chatWrappers/utils/ChatModelFunctionsDocumentationGenerator.d.ts +76 -0
- package/dist/chatWrappers/utils/ChatModelFunctionsDocumentationGenerator.js +177 -0
- package/dist/chatWrappers/utils/ChatModelFunctionsDocumentationGenerator.js.map +1 -0
- package/dist/chatWrappers/utils/chunkChatItems.d.ts +10 -0
- package/dist/chatWrappers/utils/chunkChatItems.js +44 -0
- package/dist/chatWrappers/utils/chunkChatItems.js.map +1 -0
- package/dist/chatWrappers/utils/getModelLinageNames.d.ts +2 -0
- package/dist/chatWrappers/utils/getModelLinageNames.js +18 -0
- package/dist/chatWrappers/utils/getModelLinageNames.js.map +1 -0
- package/dist/chatWrappers/utils/isJinjaTemplateEquivalentToSpecializedChatWrapper.d.ts +4 -0
- package/dist/chatWrappers/utils/isJinjaTemplateEquivalentToSpecializedChatWrapper.js +394 -0
- package/dist/chatWrappers/utils/isJinjaTemplateEquivalentToSpecializedChatWrapper.js.map +1 -0
- package/dist/chatWrappers/utils/isLlama3_2LightweightModel.d.ts +2 -0
- package/dist/chatWrappers/utils/isLlama3_2LightweightModel.js +9 -0
- package/dist/chatWrappers/utils/isLlama3_2LightweightModel.js.map +1 -0
- package/dist/chatWrappers/utils/jsonDumps.d.ts +7 -0
- package/dist/chatWrappers/utils/jsonDumps.js +18 -0
- package/dist/chatWrappers/utils/jsonDumps.js.map +1 -0
- package/dist/chatWrappers/utils/resolveChatWrapper.d.ts +148 -0
- package/dist/chatWrappers/utils/resolveChatWrapper.js +325 -0
- package/dist/chatWrappers/utils/resolveChatWrapper.js.map +1 -0
- package/dist/cli/cli.d.ts +2 -0
- package/dist/cli/cli.js +45 -0
- package/dist/cli/cli.js.map +1 -0
- package/dist/cli/commands/ChatCommand.d.ts +59 -0
- package/dist/cli/commands/ChatCommand.js +856 -0
- package/dist/cli/commands/ChatCommand.js.map +1 -0
- package/dist/cli/commands/CompleteCommand.d.ts +47 -0
- package/dist/cli/commands/CompleteCommand.js +658 -0
- package/dist/cli/commands/CompleteCommand.js.map +1 -0
- package/dist/cli/commands/DebugCommand.d.ts +7 -0
- package/dist/cli/commands/DebugCommand.js +55 -0
- package/dist/cli/commands/DebugCommand.js.map +1 -0
- package/dist/cli/commands/InfillCommand.d.ts +49 -0
- package/dist/cli/commands/InfillCommand.js +693 -0
- package/dist/cli/commands/InfillCommand.js.map +1 -0
- package/dist/cli/commands/InitCommand.d.ts +12 -0
- package/dist/cli/commands/InitCommand.js +230 -0
- package/dist/cli/commands/InitCommand.js.map +1 -0
- package/dist/cli/commands/OnPostInstallCommand.d.ts +4 -0
- package/dist/cli/commands/OnPostInstallCommand.js +62 -0
- package/dist/cli/commands/OnPostInstallCommand.js.map +1 -0
- package/dist/cli/commands/PullCommand.d.ts +13 -0
- package/dist/cli/commands/PullCommand.js +158 -0
- package/dist/cli/commands/PullCommand.js.map +1 -0
- package/dist/cli/commands/inspect/InspectCommand.d.ts +4 -0
- package/dist/cli/commands/inspect/InspectCommand.js +21 -0
- package/dist/cli/commands/inspect/InspectCommand.js.map +1 -0
- package/dist/cli/commands/inspect/commands/InspectEstimateCommand.d.ts +17 -0
- package/dist/cli/commands/inspect/commands/InspectEstimateCommand.js +275 -0
- package/dist/cli/commands/inspect/commands/InspectEstimateCommand.js.map +1 -0
- package/dist/cli/commands/inspect/commands/InspectGgufCommand.d.ts +13 -0
- package/dist/cli/commands/inspect/commands/InspectGgufCommand.js +230 -0
- package/dist/cli/commands/inspect/commands/InspectGgufCommand.js.map +1 -0
- package/dist/cli/commands/inspect/commands/InspectGpuCommand.d.ts +4 -0
- package/dist/cli/commands/inspect/commands/InspectGpuCommand.js +296 -0
- package/dist/cli/commands/inspect/commands/InspectGpuCommand.js.map +1 -0
- package/dist/cli/commands/inspect/commands/InspectMeasureCommand.d.ts +26 -0
- package/dist/cli/commands/inspect/commands/InspectMeasureCommand.js +869 -0
- package/dist/cli/commands/inspect/commands/InspectMeasureCommand.js.map +1 -0
- package/dist/cli/commands/source/SourceCommand.d.ts +4 -0
- package/dist/cli/commands/source/SourceCommand.js +19 -0
- package/dist/cli/commands/source/SourceCommand.js.map +1 -0
- package/dist/cli/commands/source/commands/BuildCommand.d.ts +16 -0
- package/dist/cli/commands/source/commands/BuildCommand.js +148 -0
- package/dist/cli/commands/source/commands/BuildCommand.js.map +1 -0
- package/dist/cli/commands/source/commands/ClearCommand.d.ts +7 -0
- package/dist/cli/commands/source/commands/ClearCommand.js +54 -0
- package/dist/cli/commands/source/commands/ClearCommand.js.map +1 -0
- package/dist/cli/commands/source/commands/DownloadCommand.d.ts +16 -0
- package/dist/cli/commands/source/commands/DownloadCommand.js +219 -0
- package/dist/cli/commands/source/commands/DownloadCommand.js.map +1 -0
- package/dist/cli/projectTemplates.d.ts +7 -0
- package/dist/cli/projectTemplates.js +10 -0
- package/dist/cli/projectTemplates.js.map +1 -0
- package/dist/cli/recommendedModels.d.ts +2 -0
- package/dist/cli/recommendedModels.js +428 -0
- package/dist/cli/recommendedModels.js.map +1 -0
- package/dist/cli/startCreateCli.d.ts +2 -0
- package/dist/cli/startCreateCli.js +26 -0
- package/dist/cli/startCreateCli.js.map +1 -0
- package/dist/cli/utils/ConsoleInteraction.d.ts +22 -0
- package/dist/cli/utils/ConsoleInteraction.js +122 -0
- package/dist/cli/utils/ConsoleInteraction.js.map +1 -0
- package/dist/cli/utils/ConsoleTable.d.ts +24 -0
- package/dist/cli/utils/ConsoleTable.js +90 -0
- package/dist/cli/utils/ConsoleTable.js.map +1 -0
- package/dist/cli/utils/basicChooseFromListConsoleInteraction.d.ts +13 -0
- package/dist/cli/utils/basicChooseFromListConsoleInteraction.js +111 -0
- package/dist/cli/utils/basicChooseFromListConsoleInteraction.js.map +1 -0
- package/dist/cli/utils/consolePromptQuestion.d.ts +6 -0
- package/dist/cli/utils/consolePromptQuestion.js +81 -0
- package/dist/cli/utils/consolePromptQuestion.js.map +1 -0
- package/dist/cli/utils/getReadablePath.d.ts +1 -0
- package/dist/cli/utils/getReadablePath.js +14 -0
- package/dist/cli/utils/getReadablePath.js.map +1 -0
- package/dist/cli/utils/interactivelyAskForModel.d.ts +13 -0
- package/dist/cli/utils/interactivelyAskForModel.js +485 -0
- package/dist/cli/utils/interactivelyAskForModel.js.map +1 -0
- package/dist/cli/utils/isRunningUnderRosetta.d.ts +1 -0
- package/dist/cli/utils/isRunningUnderRosetta.js +20 -0
- package/dist/cli/utils/isRunningUnderRosetta.js.map +1 -0
- package/dist/cli/utils/logUsedGpuTypeOption.d.ts +2 -0
- package/dist/cli/utils/logUsedGpuTypeOption.js +9 -0
- package/dist/cli/utils/logUsedGpuTypeOption.js.map +1 -0
- package/dist/cli/utils/packageJsonConfig.d.ts +6 -0
- package/dist/cli/utils/packageJsonConfig.js +51 -0
- package/dist/cli/utils/packageJsonConfig.js.map +1 -0
- package/dist/cli/utils/packageManager.d.ts +1 -0
- package/dist/cli/utils/packageManager.js +15 -0
- package/dist/cli/utils/packageManager.js.map +1 -0
- package/dist/cli/utils/parseXtcArg.d.ts +5 -0
- package/dist/cli/utils/parseXtcArg.js +16 -0
- package/dist/cli/utils/parseXtcArg.js.map +1 -0
- package/dist/cli/utils/printCommonInfoLines.d.ts +12 -0
- package/dist/cli/utils/printCommonInfoLines.js +163 -0
- package/dist/cli/utils/printCommonInfoLines.js.map +1 -0
- package/dist/cli/utils/printInfoLine.d.ts +12 -0
- package/dist/cli/utils/printInfoLine.js +54 -0
- package/dist/cli/utils/printInfoLine.js.map +1 -0
- package/dist/cli/utils/printModelDestination.d.ts +2 -0
- package/dist/cli/utils/printModelDestination.js +11 -0
- package/dist/cli/utils/printModelDestination.js.map +1 -0
- package/dist/cli/utils/projectTemplates.d.ts +19 -0
- package/dist/cli/utils/projectTemplates.js +47 -0
- package/dist/cli/utils/projectTemplates.js.map +1 -0
- package/dist/cli/utils/renderModelCompatibilityPercentageWithColors.d.ts +6 -0
- package/dist/cli/utils/renderModelCompatibilityPercentageWithColors.js +14 -0
- package/dist/cli/utils/renderModelCompatibilityPercentageWithColors.js.map +1 -0
- package/dist/cli/utils/resolveCommandGgufPath.d.ts +19 -0
- package/dist/cli/utils/resolveCommandGgufPath.js +123 -0
- package/dist/cli/utils/resolveCommandGgufPath.js.map +1 -0
- package/dist/cli/utils/resolveHeaderFlag.d.ts +1 -0
- package/dist/cli/utils/resolveHeaderFlag.js +21 -0
- package/dist/cli/utils/resolveHeaderFlag.js.map +1 -0
- package/dist/cli/utils/resolveModelRecommendationFileOptions.d.ts +14 -0
- package/dist/cli/utils/resolveModelRecommendationFileOptions.js +12 -0
- package/dist/cli/utils/resolveModelRecommendationFileOptions.js.map +1 -0
- package/dist/cli/utils/resolveNpmrcConfig.d.ts +18 -0
- package/dist/cli/utils/resolveNpmrcConfig.js +129 -0
- package/dist/cli/utils/resolveNpmrcConfig.js.map +1 -0
- package/dist/cli/utils/splitAnsiToLines.d.ts +1 -0
- package/dist/cli/utils/splitAnsiToLines.js +32 -0
- package/dist/cli/utils/splitAnsiToLines.js.map +1 -0
- package/dist/cli/utils/toBytes.d.ts +1 -0
- package/dist/cli/utils/toBytes.js +5 -0
- package/dist/cli/utils/toBytes.js.map +1 -0
- package/dist/cli/utils/withCliCommandDescriptionDocsUrl.d.ts +2 -0
- package/dist/cli/utils/withCliCommandDescriptionDocsUrl.js +23 -0
- package/dist/cli/utils/withCliCommandDescriptionDocsUrl.js.map +1 -0
- package/dist/commands.d.ts +6 -0
- package/dist/commands.js +9 -0
- package/dist/commands.js.map +1 -0
- package/dist/config.d.ts +79 -0
- package/dist/config.js +127 -0
- package/dist/config.js.map +1 -0
- package/dist/consts.d.ts +4 -0
- package/dist/consts.js +11 -0
- package/dist/consts.js.map +1 -0
- package/dist/evaluator/LlamaChat/LlamaChat.d.ts +500 -0
- package/dist/evaluator/LlamaChat/LlamaChat.js +2696 -0
- package/dist/evaluator/LlamaChat/LlamaChat.js.map +1 -0
- package/dist/evaluator/LlamaChat/utils/FunctionCallNameGrammar.d.ts +11 -0
- package/dist/evaluator/LlamaChat/utils/FunctionCallNameGrammar.js +55 -0
- package/dist/evaluator/LlamaChat/utils/FunctionCallNameGrammar.js.map +1 -0
- package/dist/evaluator/LlamaChat/utils/FunctionCallParamsGrammar.d.ts +16 -0
- package/dist/evaluator/LlamaChat/utils/FunctionCallParamsGrammar.js +45 -0
- package/dist/evaluator/LlamaChat/utils/FunctionCallParamsGrammar.js.map +1 -0
- package/dist/evaluator/LlamaChat/utils/LlamaFunctionCallValidationError.d.ts +8 -0
- package/dist/evaluator/LlamaChat/utils/LlamaFunctionCallValidationError.js +12 -0
- package/dist/evaluator/LlamaChat/utils/LlamaFunctionCallValidationError.js.map +1 -0
- package/dist/evaluator/LlamaChat/utils/contextShiftStrategies/eraseFirstResponseAndKeepFirstSystemChatContextShiftStrategy.d.ts +16 -0
- package/dist/evaluator/LlamaChat/utils/contextShiftStrategies/eraseFirstResponseAndKeepFirstSystemChatContextShiftStrategy.js +260 -0
- package/dist/evaluator/LlamaChat/utils/contextShiftStrategies/eraseFirstResponseAndKeepFirstSystemChatContextShiftStrategy.js.map +1 -0
- package/dist/evaluator/LlamaChatSession/LlamaChatSession.d.ts +520 -0
- package/dist/evaluator/LlamaChatSession/LlamaChatSession.js +628 -0
- package/dist/evaluator/LlamaChatSession/LlamaChatSession.js.map +1 -0
- package/dist/evaluator/LlamaChatSession/utils/LlamaChatSessionPromptCompletionEngine.d.ts +45 -0
- package/dist/evaluator/LlamaChatSession/utils/LlamaChatSessionPromptCompletionEngine.js +191 -0
- package/dist/evaluator/LlamaChatSession/utils/LlamaChatSessionPromptCompletionEngine.js.map +1 -0
- package/dist/evaluator/LlamaChatSession/utils/defineChatSessionFunction.d.ts +15 -0
- package/dist/evaluator/LlamaChatSession/utils/defineChatSessionFunction.js +16 -0
- package/dist/evaluator/LlamaChatSession/utils/defineChatSessionFunction.js.map +1 -0
- package/dist/evaluator/LlamaCompletion.d.ts +219 -0
- package/dist/evaluator/LlamaCompletion.js +498 -0
- package/dist/evaluator/LlamaCompletion.js.map +1 -0
- package/dist/evaluator/LlamaContext/LlamaContext.d.ts +336 -0
- package/dist/evaluator/LlamaContext/LlamaContext.js +1919 -0
- package/dist/evaluator/LlamaContext/LlamaContext.js.map +1 -0
- package/dist/evaluator/LlamaContext/LlamaContextSequenceCheckpoints.d.ts +27 -0
- package/dist/evaluator/LlamaContext/LlamaContextSequenceCheckpoints.js +130 -0
- package/dist/evaluator/LlamaContext/LlamaContextSequenceCheckpoints.js.map +1 -0
- package/dist/evaluator/LlamaContext/LlamaSampler.d.ts +1 -0
- package/dist/evaluator/LlamaContext/LlamaSampler.js +39 -0
- package/dist/evaluator/LlamaContext/LlamaSampler.js.map +1 -0
- package/dist/evaluator/LlamaContext/TokenPredictor.d.ts +55 -0
- package/dist/evaluator/LlamaContext/TokenPredictor.js +20 -0
- package/dist/evaluator/LlamaContext/TokenPredictor.js.map +1 -0
- package/dist/evaluator/LlamaContext/tokenPredictors/DraftSequenceTokenPredictor.d.ts +56 -0
- package/dist/evaluator/LlamaContext/tokenPredictors/DraftSequenceTokenPredictor.js +266 -0
- package/dist/evaluator/LlamaContext/tokenPredictors/DraftSequenceTokenPredictor.js.map +1 -0
- package/dist/evaluator/LlamaContext/tokenPredictors/InputLookupTokenPredictor.d.ts +58 -0
- package/dist/evaluator/LlamaContext/tokenPredictors/InputLookupTokenPredictor.js +138 -0
- package/dist/evaluator/LlamaContext/tokenPredictors/InputLookupTokenPredictor.js.map +1 -0
- package/dist/evaluator/LlamaContext/types.d.ts +602 -0
- package/dist/evaluator/LlamaContext/types.js +2 -0
- package/dist/evaluator/LlamaContext/types.js.map +1 -0
- package/dist/evaluator/LlamaContext/utils/batchItemsPrioritizationStrategies/firstInFirstOutStrategy.d.ts +5 -0
- package/dist/evaluator/LlamaContext/utils/batchItemsPrioritizationStrategies/firstInFirstOutStrategy.js +16 -0
- package/dist/evaluator/LlamaContext/utils/batchItemsPrioritizationStrategies/firstInFirstOutStrategy.js.map +1 -0
- package/dist/evaluator/LlamaContext/utils/batchItemsPrioritizationStrategies/maximumParallelismStrategy.d.ts +5 -0
- package/dist/evaluator/LlamaContext/utils/batchItemsPrioritizationStrategies/maximumParallelismStrategy.js +42 -0
- package/dist/evaluator/LlamaContext/utils/batchItemsPrioritizationStrategies/maximumParallelismStrategy.js.map +1 -0
- package/dist/evaluator/LlamaContext/utils/padSafeContextSize.d.ts +1 -0
- package/dist/evaluator/LlamaContext/utils/padSafeContextSize.js +18 -0
- package/dist/evaluator/LlamaContext/utils/padSafeContextSize.js.map +1 -0
- package/dist/evaluator/LlamaContext/utils/resolveBatchItemsPrioritizationStrategy.d.ts +2 -0
- package/dist/evaluator/LlamaContext/utils/resolveBatchItemsPrioritizationStrategy.js +13 -0
- package/dist/evaluator/LlamaContext/utils/resolveBatchItemsPrioritizationStrategy.js.map +1 -0
- package/dist/evaluator/LlamaEmbedding.d.ts +21 -0
- package/dist/evaluator/LlamaEmbedding.js +53 -0
- package/dist/evaluator/LlamaEmbedding.js.map +1 -0
- package/dist/evaluator/LlamaEmbeddingContext.d.ts +52 -0
- package/dist/evaluator/LlamaEmbeddingContext.js +86 -0
- package/dist/evaluator/LlamaEmbeddingContext.js.map +1 -0
- package/dist/evaluator/LlamaGrammar.d.ts +39 -0
- package/dist/evaluator/LlamaGrammar.js +72 -0
- package/dist/evaluator/LlamaGrammar.js.map +1 -0
- package/dist/evaluator/LlamaGrammarEvaluationState.d.ts +19 -0
- package/dist/evaluator/LlamaGrammarEvaluationState.js +29 -0
- package/dist/evaluator/LlamaGrammarEvaluationState.js.map +1 -0
- package/dist/evaluator/LlamaJsonSchemaGrammar.d.ts +17 -0
- package/dist/evaluator/LlamaJsonSchemaGrammar.js +35 -0
- package/dist/evaluator/LlamaJsonSchemaGrammar.js.map +1 -0
- package/dist/evaluator/LlamaModel/LlamaModel.d.ts +344 -0
- package/dist/evaluator/LlamaModel/LlamaModel.js +853 -0
- package/dist/evaluator/LlamaModel/LlamaModel.js.map +1 -0
- package/dist/evaluator/LlamaModel/utils/TokenAttributes.d.ts +29 -0
- package/dist/evaluator/LlamaModel/utils/TokenAttributes.js +65 -0
- package/dist/evaluator/LlamaModel/utils/TokenAttributes.js.map +1 -0
- package/dist/evaluator/LlamaRankingContext.d.ts +91 -0
- package/dist/evaluator/LlamaRankingContext.js +178 -0
- package/dist/evaluator/LlamaRankingContext.js.map +1 -0
- package/dist/evaluator/TokenBias.d.ts +37 -0
- package/dist/evaluator/TokenBias.js +68 -0
- package/dist/evaluator/TokenBias.js.map +1 -0
- package/dist/evaluator/TokenMeter.d.ts +45 -0
- package/dist/evaluator/TokenMeter.js +74 -0
- package/dist/evaluator/TokenMeter.js.map +1 -0
- package/dist/evaluator/utils/chunkDocument.d.ts +86 -0
- package/dist/evaluator/utils/chunkDocument.js +212 -0
- package/dist/evaluator/utils/chunkDocument.js.map +1 -0
- package/dist/gguf/consts.d.ts +4 -0
- package/dist/gguf/consts.js +12 -0
- package/dist/gguf/consts.js.map +1 -0
- package/dist/gguf/errors/InvalidGgufMagicError.d.ts +3 -0
- package/dist/gguf/errors/InvalidGgufMagicError.js +6 -0
- package/dist/gguf/errors/InvalidGgufMagicError.js.map +1 -0
- package/dist/gguf/errors/UnsupportedGgufValueTypeError.d.ts +4 -0
- package/dist/gguf/errors/UnsupportedGgufValueTypeError.js +9 -0
- package/dist/gguf/errors/UnsupportedGgufValueTypeError.js.map +1 -0
- package/dist/gguf/fileReaders/GgufFileReader.d.ts +36 -0
- package/dist/gguf/fileReaders/GgufFileReader.js +106 -0
- package/dist/gguf/fileReaders/GgufFileReader.js.map +1 -0
- package/dist/gguf/fileReaders/GgufFsFileReader.d.ts +16 -0
- package/dist/gguf/fileReaders/GgufFsFileReader.js +62 -0
- package/dist/gguf/fileReaders/GgufFsFileReader.js.map +1 -0
- package/dist/gguf/fileReaders/GgufNetworkFetchFileReader.d.ts +28 -0
- package/dist/gguf/fileReaders/GgufNetworkFetchFileReader.js +94 -0
- package/dist/gguf/fileReaders/GgufNetworkFetchFileReader.js.map +1 -0
- package/dist/gguf/insights/GgufInsights.d.ts +78 -0
- package/dist/gguf/insights/GgufInsights.js +854 -0
- package/dist/gguf/insights/GgufInsights.js.map +1 -0
- package/dist/gguf/insights/GgufInsightsConfigurationResolver.d.ts +203 -0
- package/dist/gguf/insights/GgufInsightsConfigurationResolver.js +284 -0
- package/dist/gguf/insights/GgufInsightsConfigurationResolver.js.map +1 -0
- package/dist/gguf/insights/GgufInsightsTokens.d.ts +5 -0
- package/dist/gguf/insights/GgufInsightsTokens.js +40 -0
- package/dist/gguf/insights/GgufInsightsTokens.js.map +1 -0
- package/dist/gguf/insights/utils/getRamUsageFromUnifiedVram.d.ts +5 -0
- package/dist/gguf/insights/utils/getRamUsageFromUnifiedVram.js +7 -0
- package/dist/gguf/insights/utils/getRamUsageFromUnifiedVram.js.map +1 -0
- package/dist/gguf/insights/utils/resolveContextContextSizeOption.d.ts +33 -0
- package/dist/gguf/insights/utils/resolveContextContextSizeOption.js +117 -0
- package/dist/gguf/insights/utils/resolveContextContextSizeOption.js.map +1 -0
- package/dist/gguf/insights/utils/resolveModelGpuLayersOption.d.ts +20 -0
- package/dist/gguf/insights/utils/resolveModelGpuLayersOption.js +251 -0
- package/dist/gguf/insights/utils/resolveModelGpuLayersOption.js.map +1 -0
- package/dist/gguf/insights/utils/scoreLevels.d.ts +5 -0
- package/dist/gguf/insights/utils/scoreLevels.js +16 -0
- package/dist/gguf/insights/utils/scoreLevels.js.map +1 -0
- package/dist/gguf/parser/GgufV2Parser.d.ts +20 -0
- package/dist/gguf/parser/GgufV2Parser.js +184 -0
- package/dist/gguf/parser/GgufV2Parser.js.map +1 -0
- package/dist/gguf/parser/GgufV3Parser.d.ts +3 -0
- package/dist/gguf/parser/GgufV3Parser.js +4 -0
- package/dist/gguf/parser/GgufV3Parser.js.map +1 -0
- package/dist/gguf/parser/parseGguf.d.ts +8 -0
- package/dist/gguf/parser/parseGguf.js +61 -0
- package/dist/gguf/parser/parseGguf.js.map +1 -0
- package/dist/gguf/readGgufFileInfo.d.ts +54 -0
- package/dist/gguf/readGgufFileInfo.js +82 -0
- package/dist/gguf/readGgufFileInfo.js.map +1 -0
- package/dist/gguf/types/GgufFileInfoTypes.d.ts +85 -0
- package/dist/gguf/types/GgufFileInfoTypes.js +18 -0
- package/dist/gguf/types/GgufFileInfoTypes.js.map +1 -0
- package/dist/gguf/types/GgufMetadataTypes.d.ts +480 -0
- package/dist/gguf/types/GgufMetadataTypes.js +194 -0
- package/dist/gguf/types/GgufMetadataTypes.js.map +1 -0
- package/dist/gguf/types/GgufTensorInfoTypes.d.ts +63 -0
- package/dist/gguf/types/GgufTensorInfoTypes.js +54 -0
- package/dist/gguf/types/GgufTensorInfoTypes.js.map +1 -0
- package/dist/gguf/utils/GgufReadOffset.d.ts +6 -0
- package/dist/gguf/utils/GgufReadOffset.js +18 -0
- package/dist/gguf/utils/GgufReadOffset.js.map +1 -0
- package/dist/gguf/utils/convertMetadataKeyValueRecordToNestedObject.d.ts +6 -0
- package/dist/gguf/utils/convertMetadataKeyValueRecordToNestedObject.js +76 -0
- package/dist/gguf/utils/convertMetadataKeyValueRecordToNestedObject.js.map +1 -0
- package/dist/gguf/utils/getGgufFileTypeName.d.ts +4 -0
- package/dist/gguf/utils/getGgufFileTypeName.js +13 -0
- package/dist/gguf/utils/getGgufFileTypeName.js.map +1 -0
- package/dist/gguf/utils/getGgufMetadataArchitectureData.d.ts +3 -0
- package/dist/gguf/utils/getGgufMetadataArchitectureData.js +4 -0
- package/dist/gguf/utils/getGgufMetadataArchitectureData.js.map +1 -0
- package/dist/gguf/utils/getGgufMetadataKeyValue.d.ts +1 -0
- package/dist/gguf/utils/getGgufMetadataKeyValue.js +27 -0
- package/dist/gguf/utils/getGgufMetadataKeyValue.js.map +1 -0
- package/dist/gguf/utils/ggufQuantNames.d.ts +2 -0
- package/dist/gguf/utils/ggufQuantNames.js +42 -0
- package/dist/gguf/utils/ggufQuantNames.js.map +1 -0
- package/dist/gguf/utils/normalizeGgufDownloadUrl.d.ts +2 -0
- package/dist/gguf/utils/normalizeGgufDownloadUrl.js +18 -0
- package/dist/gguf/utils/normalizeGgufDownloadUrl.js.map +1 -0
- package/dist/gguf/utils/resolveBinarySplitGgufPartUrls.d.ts +2 -0
- package/dist/gguf/utils/resolveBinarySplitGgufPartUrls.js +38 -0
- package/dist/gguf/utils/resolveBinarySplitGgufPartUrls.js.map +1 -0
- package/dist/gguf/utils/resolveSplitGgufParts.d.ts +7 -0
- package/dist/gguf/utils/resolveSplitGgufParts.js +64 -0
- package/dist/gguf/utils/resolveSplitGgufParts.js.map +1 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.js +64 -0
- package/dist/index.js.map +1 -0
- package/dist/state.d.ts +6 -0
- package/dist/state.js +22 -0
- package/dist/state.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types.d.ts +408 -0
- package/dist/types.js +13 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/DisposeGuard.d.ts +13 -0
- package/dist/utils/DisposeGuard.js +120 -0
- package/dist/utils/DisposeGuard.js.map +1 -0
- package/dist/utils/InsufficientMemoryError.d.ts +3 -0
- package/dist/utils/InsufficientMemoryError.js +6 -0
- package/dist/utils/InsufficientMemoryError.js.map +1 -0
- package/dist/utils/LlamaText.d.ts +92 -0
- package/dist/utils/LlamaText.js +527 -0
- package/dist/utils/LlamaText.js.map +1 -0
- package/dist/utils/LruCache.d.ts +12 -0
- package/dist/utils/LruCache.js +44 -0
- package/dist/utils/LruCache.js.map +1 -0
- package/dist/utils/OpenAIFormat.d.ts +177 -0
- package/dist/utils/OpenAIFormat.js +488 -0
- package/dist/utils/OpenAIFormat.js.map +1 -0
- package/dist/utils/OverridesObject.d.ts +7 -0
- package/dist/utils/OverridesObject.js +2 -0
- package/dist/utils/OverridesObject.js.map +1 -0
- package/dist/utils/ReplHistory.d.ts +9 -0
- package/dist/utils/ReplHistory.js +72 -0
- package/dist/utils/ReplHistory.js.map +1 -0
- package/dist/utils/StopGenerationDetector.d.ts +47 -0
- package/dist/utils/StopGenerationDetector.js +291 -0
- package/dist/utils/StopGenerationDetector.js.map +1 -0
- package/dist/utils/ThreadsSplitter.d.ts +32 -0
- package/dist/utils/ThreadsSplitter.js +177 -0
- package/dist/utils/ThreadsSplitter.js.map +1 -0
- package/dist/utils/TokenStreamRegulator.d.ts +38 -0
- package/dist/utils/TokenStreamRegulator.js +200 -0
- package/dist/utils/TokenStreamRegulator.js.map +1 -0
- package/dist/utils/UnsupportedError.d.ts +2 -0
- package/dist/utils/UnsupportedError.js +7 -0
- package/dist/utils/UnsupportedError.js.map +1 -0
- package/dist/utils/appendUserMessageToChatHistory.d.ts +6 -0
- package/dist/utils/appendUserMessageToChatHistory.js +22 -0
- package/dist/utils/appendUserMessageToChatHistory.js.map +1 -0
- package/dist/utils/clearTempFolder.d.ts +1 -0
- package/dist/utils/clearTempFolder.js +16 -0
- package/dist/utils/clearTempFolder.js.map +1 -0
- package/dist/utils/cmake.d.ts +10 -0
- package/dist/utils/cmake.js +146 -0
- package/dist/utils/cmake.js.map +1 -0
- package/dist/utils/compareTokens.d.ts +2 -0
- package/dist/utils/compareTokens.js +4 -0
- package/dist/utils/compareTokens.js.map +1 -0
- package/dist/utils/createModelDownloader.d.ts +262 -0
- package/dist/utils/createModelDownloader.js +486 -0
- package/dist/utils/createModelDownloader.js.map +1 -0
- package/dist/utils/findBestOption.d.ts +4 -0
- package/dist/utils/findBestOption.js +15 -0
- package/dist/utils/findBestOption.js.map +1 -0
- package/dist/utils/findCharacterRemovalCountToFitChatHistoryInContext.d.ts +20 -0
- package/dist/utils/findCharacterRemovalCountToFitChatHistoryInContext.js +85 -0
- package/dist/utils/findCharacterRemovalCountToFitChatHistoryInContext.js.map +1 -0
- package/dist/utils/gbnfJson/GbnfGrammarGenerator.d.ts +19 -0
- package/dist/utils/gbnfJson/GbnfGrammarGenerator.js +60 -0
- package/dist/utils/gbnfJson/GbnfGrammarGenerator.js.map +1 -0
- package/dist/utils/gbnfJson/GbnfTerminal.d.ts +11 -0
- package/dist/utils/gbnfJson/GbnfTerminal.js +54 -0
- package/dist/utils/gbnfJson/GbnfTerminal.js.map +1 -0
- package/dist/utils/gbnfJson/getGbnfGrammarForGbnfJsonSchema.d.ts +5 -0
- package/dist/utils/gbnfJson/getGbnfGrammarForGbnfJsonSchema.js +11 -0
- package/dist/utils/gbnfJson/getGbnfGrammarForGbnfJsonSchema.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfAnyJson.d.ts +9 -0
- package/dist/utils/gbnfJson/terminals/GbnfAnyJson.js +53 -0
- package/dist/utils/gbnfJson/terminals/GbnfAnyJson.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfArray.d.ts +18 -0
- package/dist/utils/gbnfJson/terminals/GbnfArray.js +83 -0
- package/dist/utils/gbnfJson/terminals/GbnfArray.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfBoolean.d.ts +7 -0
- package/dist/utils/gbnfJson/terminals/GbnfBoolean.js +22 -0
- package/dist/utils/gbnfJson/terminals/GbnfBoolean.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfBooleanValue.d.ts +7 -0
- package/dist/utils/gbnfJson/terminals/GbnfBooleanValue.js +17 -0
- package/dist/utils/gbnfJson/terminals/GbnfBooleanValue.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfCommaWhitespace.d.ts +11 -0
- package/dist/utils/gbnfJson/terminals/GbnfCommaWhitespace.js +28 -0
- package/dist/utils/gbnfJson/terminals/GbnfCommaWhitespace.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfFormatString.d.ts +11 -0
- package/dist/utils/gbnfJson/terminals/GbnfFormatString.js +90 -0
- package/dist/utils/gbnfJson/terminals/GbnfFormatString.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfGrammar.d.ts +9 -0
- package/dist/utils/gbnfJson/terminals/GbnfGrammar.js +23 -0
- package/dist/utils/gbnfJson/terminals/GbnfGrammar.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfInsideStringChar.d.ts +5 -0
- package/dist/utils/gbnfJson/terminals/GbnfInsideStringChar.js +24 -0
- package/dist/utils/gbnfJson/terminals/GbnfInsideStringChar.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfNull.d.ts +5 -0
- package/dist/utils/gbnfJson/terminals/GbnfNull.js +11 -0
- package/dist/utils/gbnfJson/terminals/GbnfNull.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfNumber.d.ts +9 -0
- package/dist/utils/gbnfJson/terminals/GbnfNumber.js +22 -0
- package/dist/utils/gbnfJson/terminals/GbnfNumber.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfNumberValue.d.ts +9 -0
- package/dist/utils/gbnfJson/terminals/GbnfNumberValue.js +21 -0
- package/dist/utils/gbnfJson/terminals/GbnfNumberValue.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfObjectMap.d.ts +28 -0
- package/dist/utils/gbnfJson/terminals/GbnfObjectMap.js +88 -0
- package/dist/utils/gbnfJson/terminals/GbnfObjectMap.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfOr.d.ts +9 -0
- package/dist/utils/gbnfJson/terminals/GbnfOr.js +34 -0
- package/dist/utils/gbnfJson/terminals/GbnfOr.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfRef.d.ts +15 -0
- package/dist/utils/gbnfJson/terminals/GbnfRef.js +34 -0
- package/dist/utils/gbnfJson/terminals/GbnfRef.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfRepetition.d.ts +15 -0
- package/dist/utils/gbnfJson/terminals/GbnfRepetition.js +86 -0
- package/dist/utils/gbnfJson/terminals/GbnfRepetition.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfString.d.ts +12 -0
- package/dist/utils/gbnfJson/terminals/GbnfString.js +43 -0
- package/dist/utils/gbnfJson/terminals/GbnfString.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfStringValue.d.ts +8 -0
- package/dist/utils/gbnfJson/terminals/GbnfStringValue.js +26 -0
- package/dist/utils/gbnfJson/terminals/GbnfStringValue.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfVerbatimText.d.ts +6 -0
- package/dist/utils/gbnfJson/terminals/GbnfVerbatimText.js +21 -0
- package/dist/utils/gbnfJson/terminals/GbnfVerbatimText.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/GbnfWhitespace.d.ts +13 -0
- package/dist/utils/gbnfJson/terminals/GbnfWhitespace.js +67 -0
- package/dist/utils/gbnfJson/terminals/GbnfWhitespace.js.map +1 -0
- package/dist/utils/gbnfJson/terminals/gbnfConsts.d.ts +30 -0
- package/dist/utils/gbnfJson/terminals/gbnfConsts.js +72 -0
- package/dist/utils/gbnfJson/terminals/gbnfConsts.js.map +1 -0
- package/dist/utils/gbnfJson/types.d.ts +213 -0
- package/dist/utils/gbnfJson/types.js +30 -0
- package/dist/utils/gbnfJson/types.js.map +1 -0
- package/dist/utils/gbnfJson/utils/GbnfJsonScopeState.d.ts +10 -0
- package/dist/utils/gbnfJson/utils/GbnfJsonScopeState.js +15 -0
- package/dist/utils/gbnfJson/utils/GbnfJsonScopeState.js.map +1 -0
- package/dist/utils/gbnfJson/utils/defsScope.d.ts +7 -0
- package/dist/utils/gbnfJson/utils/defsScope.js +17 -0
- package/dist/utils/gbnfJson/utils/defsScope.js.map +1 -0
- package/dist/utils/gbnfJson/utils/getGbnfJsonTerminalForGbnfJsonSchema.d.ts +5 -0
- package/dist/utils/gbnfJson/utils/getGbnfJsonTerminalForGbnfJsonSchema.js +143 -0
- package/dist/utils/gbnfJson/utils/getGbnfJsonTerminalForGbnfJsonSchema.js.map +1 -0
- package/dist/utils/gbnfJson/utils/getGbnfJsonTerminalForLiteral.d.ts +5 -0
- package/dist/utils/gbnfJson/utils/getGbnfJsonTerminalForLiteral.js +16 -0
- package/dist/utils/gbnfJson/utils/getGbnfJsonTerminalForLiteral.js.map +1 -0
- package/dist/utils/gbnfJson/utils/validateObjectAgainstGbnfSchema.d.ts +8 -0
- package/dist/utils/gbnfJson/utils/validateObjectAgainstGbnfSchema.js +242 -0
- package/dist/utils/gbnfJson/utils/validateObjectAgainstGbnfSchema.js.map +1 -0
- package/dist/utils/getBuildDefaults.d.ts +5 -0
- package/dist/utils/getBuildDefaults.js +9 -0
- package/dist/utils/getBuildDefaults.js.map +1 -0
- package/dist/utils/getChatWrapperSegmentDefinition.d.ts +2 -0
- package/dist/utils/getChatWrapperSegmentDefinition.js +9 -0
- package/dist/utils/getChatWrapperSegmentDefinition.js.map +1 -0
- package/dist/utils/getConsoleLogPrefix.d.ts +1 -0
- package/dist/utils/getConsoleLogPrefix.js +10 -0
- package/dist/utils/getConsoleLogPrefix.js.map +1 -0
- package/dist/utils/getFirstWritableDir.d.ts +8 -0
- package/dist/utils/getFirstWritableDir.js +60 -0
- package/dist/utils/getFirstWritableDir.js.map +1 -0
- package/dist/utils/getGrammarsFolder.d.ts +2 -0
- package/dist/utils/getGrammarsFolder.js +19 -0
- package/dist/utils/getGrammarsFolder.js.map +1 -0
- package/dist/utils/getLlamaClasses.d.ts +9 -0
- package/dist/utils/getLlamaClasses.js +14 -0
- package/dist/utils/getLlamaClasses.js.map +1 -0
- package/dist/utils/getModuleVersion.d.ts +1 -0
- package/dist/utils/getModuleVersion.js +13 -0
- package/dist/utils/getModuleVersion.js.map +1 -0
- package/dist/utils/getQueuedTokensBeforeStopTrigger.d.ts +6 -0
- package/dist/utils/getQueuedTokensBeforeStopTrigger.js +22 -0
- package/dist/utils/getQueuedTokensBeforeStopTrigger.js.map +1 -0
- package/dist/utils/getReadableContextSize.d.ts +1 -0
- package/dist/utils/getReadableContextSize.js +7 -0
- package/dist/utils/getReadableContextSize.js.map +1 -0
- package/dist/utils/getTempDir.d.ts +10 -0
- package/dist/utils/getTempDir.js +121 -0
- package/dist/utils/getTempDir.js.map +1 -0
- package/dist/utils/getTypeScriptTypeStringForGbnfJsonSchema.d.ts +2 -0
- package/dist/utils/getTypeScriptTypeStringForGbnfJsonSchema.js +205 -0
- package/dist/utils/getTypeScriptTypeStringForGbnfJsonSchema.js.map +1 -0
- package/dist/utils/gitReleaseBundles.d.ts +2 -0
- package/dist/utils/gitReleaseBundles.js +132 -0
- package/dist/utils/gitReleaseBundles.js.map +1 -0
- package/dist/utils/hashString.d.ts +1 -0
- package/dist/utils/hashString.js +8 -0
- package/dist/utils/hashString.js.map +1 -0
- package/dist/utils/includesText.d.ts +1 -0
- package/dist/utils/includesText.js +12 -0
- package/dist/utils/includesText.js.map +1 -0
- package/dist/utils/isLockfileActive.d.ts +4 -0
- package/dist/utils/isLockfileActive.js +12 -0
- package/dist/utils/isLockfileActive.js.map +1 -0
- package/dist/utils/isToken.d.ts +2 -0
- package/dist/utils/isToken.js +4 -0
- package/dist/utils/isToken.js.map +1 -0
- package/dist/utils/isUrl.d.ts +1 -0
- package/dist/utils/isUrl.js +15 -0
- package/dist/utils/isUrl.js.map +1 -0
- package/dist/utils/mergeUnionTypes.d.ts +10 -0
- package/dist/utils/mergeUnionTypes.js +2 -0
- package/dist/utils/mergeUnionTypes.js.map +1 -0
- package/dist/utils/modelDownloadEndpoints.d.ts +13 -0
- package/dist/utils/modelDownloadEndpoints.js +27 -0
- package/dist/utils/modelDownloadEndpoints.js.map +1 -0
- package/dist/utils/modelFileAccessTokens.d.ts +5 -0
- package/dist/utils/modelFileAccessTokens.js +41 -0
- package/dist/utils/modelFileAccessTokens.js.map +1 -0
- package/dist/utils/optionsMatrix.d.ts +58 -0
- package/dist/utils/optionsMatrix.js +97 -0
- package/dist/utils/optionsMatrix.js.map +1 -0
- package/dist/utils/parseModelFileName.d.ts +15 -0
- package/dist/utils/parseModelFileName.js +132 -0
- package/dist/utils/parseModelFileName.js.map +1 -0
- package/dist/utils/parseModelUri.d.ts +40 -0
- package/dist/utils/parseModelUri.js +346 -0
- package/dist/utils/parseModelUri.js.map +1 -0
- package/dist/utils/parseTextTemplate.d.ts +66 -0
- package/dist/utils/parseTextTemplate.js +116 -0
- package/dist/utils/parseTextTemplate.js.map +1 -0
- package/dist/utils/prettyPrintObject.d.ts +10 -0
- package/dist/utils/prettyPrintObject.js +84 -0
- package/dist/utils/prettyPrintObject.js.map +1 -0
- package/dist/utils/pushAll.d.ts +6 -0
- package/dist/utils/pushAll.js +11 -0
- package/dist/utils/pushAll.js.map +1 -0
- package/dist/utils/removeNullFields.d.ts +2 -0
- package/dist/utils/removeNullFields.js +17 -0
- package/dist/utils/removeNullFields.js.map +1 -0
- package/dist/utils/resolveGithubRelease.d.ts +2 -0
- package/dist/utils/resolveGithubRelease.js +77 -0
- package/dist/utils/resolveGithubRelease.js.map +1 -0
- package/dist/utils/resolveLastTokens.d.ts +2 -0
- package/dist/utils/resolveLastTokens.js +12 -0
- package/dist/utils/resolveLastTokens.js.map +1 -0
- package/dist/utils/resolveModelDestination.d.ts +16 -0
- package/dist/utils/resolveModelDestination.js +54 -0
- package/dist/utils/resolveModelDestination.js.map +1 -0
- package/dist/utils/resolveModelFile.d.ts +142 -0
- package/dist/utils/resolveModelFile.js +201 -0
- package/dist/utils/resolveModelFile.js.map +1 -0
- package/dist/utils/runtime.d.ts +4 -0
- package/dist/utils/runtime.js +8 -0
- package/dist/utils/runtime.js.map +1 -0
- package/dist/utils/safeEventCallback.d.ts +6 -0
- package/dist/utils/safeEventCallback.js +29 -0
- package/dist/utils/safeEventCallback.js.map +1 -0
- package/dist/utils/signalSleep.d.ts +1 -0
- package/dist/utils/signalSleep.js +20 -0
- package/dist/utils/signalSleep.js.map +1 -0
- package/dist/utils/spawnCommand.d.ts +11 -0
- package/dist/utils/spawnCommand.js +89 -0
- package/dist/utils/spawnCommand.js.map +1 -0
- package/dist/utils/tokenizeInput.d.ts +3 -0
- package/dist/utils/tokenizeInput.js +14 -0
- package/dist/utils/tokenizeInput.js.map +1 -0
- package/dist/utils/tokenizerUtils.d.ts +12 -0
- package/dist/utils/tokenizerUtils.js +32 -0
- package/dist/utils/tokenizerUtils.js.map +1 -0
- package/dist/utils/transformPromisable.d.ts +54 -0
- package/dist/utils/transformPromisable.js +95 -0
- package/dist/utils/transformPromisable.js.map +1 -0
- package/dist/utils/truncateTextAndRoundToWords.d.ts +11 -0
- package/dist/utils/truncateTextAndRoundToWords.js +110 -0
- package/dist/utils/truncateTextAndRoundToWords.js.map +1 -0
- package/dist/utils/utilTypes.d.ts +18 -0
- package/dist/utils/utilTypes.js +2 -0
- package/dist/utils/utilTypes.js.map +1 -0
- package/dist/utils/waitForLockfileRelease.d.ts +5 -0
- package/dist/utils/waitForLockfileRelease.js +19 -0
- package/dist/utils/waitForLockfileRelease.js.map +1 -0
- package/dist/utils/withLockfile.d.ts +7 -0
- package/dist/utils/withLockfile.js +44 -0
- package/dist/utils/withLockfile.js.map +1 -0
- package/dist/utils/withOra.d.ts +7 -0
- package/dist/utils/withOra.js +37 -0
- package/dist/utils/withOra.js.map +1 -0
- package/dist/utils/withProgressLog.d.ts +22 -0
- package/dist/utils/withProgressLog.js +211 -0
- package/dist/utils/withProgressLog.js.map +1 -0
- package/dist/utils/withStatusLogs.d.ts +6 -0
- package/dist/utils/withStatusLogs.js +25 -0
- package/dist/utils/withStatusLogs.js.map +1 -0
- package/dist/utils/wrapAbortSignal.d.ts +1 -0
- package/dist/utils/wrapAbortSignal.js +14 -0
- package/dist/utils/wrapAbortSignal.js.map +1 -0
- package/llama/.clang-format +46 -0
- package/llama/CMakeLists.txt +141 -0
- package/llama/addon/AddonContext.cpp +1181 -0
- package/llama/addon/AddonContext.h +85 -0
- package/llama/addon/AddonGrammar.cpp +92 -0
- package/llama/addon/AddonGrammar.h +22 -0
- package/llama/addon/AddonGrammarEvaluationState.cpp +36 -0
- package/llama/addon/AddonGrammarEvaluationState.h +17 -0
- package/llama/addon/AddonModel.cpp +691 -0
- package/llama/addon/AddonModel.h +64 -0
- package/llama/addon/AddonModelData.cpp +25 -0
- package/llama/addon/AddonModelData.h +15 -0
- package/llama/addon/AddonModelLora.cpp +103 -0
- package/llama/addon/AddonModelLora.h +28 -0
- package/llama/addon/AddonSampler.cpp +669 -0
- package/llama/addon/AddonSampler.h +75 -0
- package/llama/addon/RingBuffer.h +109 -0
- package/llama/addon/addon.cpp +330 -0
- package/llama/addon/addonGlobals.cpp +22 -0
- package/llama/addon/addonGlobals.h +12 -0
- package/llama/addon/globals/addonLog.cpp +143 -0
- package/llama/addon/globals/addonLog.h +24 -0
- package/llama/addon/globals/addonProgress.cpp +15 -0
- package/llama/addon/globals/addonProgress.h +15 -0
- package/llama/addon/globals/getGpuInfo.cpp +146 -0
- package/llama/addon/globals/getGpuInfo.h +11 -0
- package/llama/addon/globals/getMemoryInfo.cpp +63 -0
- package/llama/addon/globals/getMemoryInfo.h +4 -0
- package/llama/addon/globals/getSwapInfo.cpp +69 -0
- package/llama/addon/globals/getSwapInfo.h +4 -0
- package/llama/binariesGithubRelease.json +3 -0
- package/llama/cmake/addVariantSuffix.cmake +21 -0
- package/llama/cmake/win32.ensureNinjaPath.cmake +68 -0
- package/llama/cmake/win32.ensureNodeLib.cmake +34 -0
- package/llama/cmake/win32.llvmApplyGnuModeAdaptations.cmake +12 -0
- package/llama/cmake/win32.llvmEnsureCmakeAr.cmake +37 -0
- package/llama/cmake/win32.llvmUseGnuModeCompilers.cmake +87 -0
- package/llama/cmake/win32.programFilesPaths.cmake +35 -0
- package/llama/gpuInfo/vulkan-gpu-info.cpp +207 -0
- package/llama/gpuInfo/vulkan-gpu-info.h +9 -0
- package/llama/package.json +5 -0
- package/llama/profiles/llvm.win32.host-arm64.target-arm64.cmake +14 -0
- package/llama/profiles/llvm.win32.host-x64.target-arm64.cmake +14 -0
- package/llama/profiles/llvm.win32.host-x64.target-x64.cmake +14 -0
- package/llama/toolchains/darwin.host-x64.target-arm64.cmake +8 -0
- package/llama/toolchains/linux.host-arm64.target-x64.cmake +5 -0
- package/llama/toolchains/linux.host-x64.target-arm64.cmake +5 -0
- package/llama/toolchains/linux.host-x64.target-arm71.cmake +5 -0
- package/llama/toolchains/llvm.win32.host-x64.target-x64.cmake +20 -0
- package/llama/toolchains/win32.host-arm64.target-arm64.cmake +21 -0
- package/llama/toolchains/win32.host-x64.target-arm64.cmake +21 -0
- package/llama/xpack/package.json +10 -0
- package/package.json +241 -0
- package/templates/README.md +6 -0
- package/templates/package.json +10 -0
- package/templates/packed/electron-typescript-react.json +1 -0
- package/templates/packed/node-typescript.json +1 -0
|
@@ -0,0 +1,1181 @@
|
|
|
1
|
+
#include <thread>
|
|
2
|
+
#include <algorithm>
|
|
3
|
+
#include <cmath>
|
|
4
|
+
#include "common/common.h"
|
|
5
|
+
#include "llama-vocab.h"
|
|
6
|
+
#include "llama.h"
|
|
7
|
+
|
|
8
|
+
#include "addonGlobals.h"
|
|
9
|
+
#include "AddonModel.h"
|
|
10
|
+
#include "AddonModelLora.h"
|
|
11
|
+
#include "AddonGrammarEvaluationState.h"
|
|
12
|
+
#include "AddonContext.h"
|
|
13
|
+
|
|
14
|
+
static uint64_t calculateBatchMemorySize(int32_t n_tokens_alloc, int32_t embd, int32_t n_seq_max) {
|
|
15
|
+
uint64_t totalSize = 0;
|
|
16
|
+
|
|
17
|
+
if (embd) {
|
|
18
|
+
totalSize += sizeof(float) * n_tokens_alloc * embd;
|
|
19
|
+
} else {
|
|
20
|
+
totalSize += sizeof(llama_token) * n_tokens_alloc;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
totalSize += sizeof(llama_pos) * n_tokens_alloc;
|
|
24
|
+
totalSize += sizeof(int32_t) * n_tokens_alloc;
|
|
25
|
+
totalSize += sizeof(llama_seq_id *) * (n_tokens_alloc + 1);
|
|
26
|
+
|
|
27
|
+
totalSize += sizeof(llama_seq_id) * n_seq_max * n_tokens_alloc;
|
|
28
|
+
|
|
29
|
+
totalSize += sizeof(int8_t) * n_tokens_alloc;
|
|
30
|
+
|
|
31
|
+
return totalSize;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
class AddonContextDecodeBatchWorker : public Napi::AsyncWorker {
|
|
35
|
+
public:
|
|
36
|
+
AddonContext* ctx;
|
|
37
|
+
|
|
38
|
+
AddonContextDecodeBatchWorker(const Napi::Env& env, AddonContext* ctx)
|
|
39
|
+
: Napi::AsyncWorker(env, "AddonContextDecodeBatchWorker"),
|
|
40
|
+
ctx(ctx),
|
|
41
|
+
deferred(Napi::Promise::Deferred::New(env)) {
|
|
42
|
+
ctx->Ref();
|
|
43
|
+
}
|
|
44
|
+
~AddonContextDecodeBatchWorker() {
|
|
45
|
+
ctx->Unref();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
Napi::Promise GetPromise() {
|
|
49
|
+
return deferred.Promise();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected:
|
|
53
|
+
Napi::Promise::Deferred deferred;
|
|
54
|
+
|
|
55
|
+
void Execute() {
|
|
56
|
+
try {
|
|
57
|
+
// Perform the evaluation using llama_decode.
|
|
58
|
+
int r = llama_decode(ctx->ctx, ctx->batch);
|
|
59
|
+
|
|
60
|
+
if (r != 0) {
|
|
61
|
+
if (r == 1) {
|
|
62
|
+
SetError("could not find a KV slot for the batch (try reducing the size of the batch or increase the context)");
|
|
63
|
+
} else {
|
|
64
|
+
SetError("Eval has failed");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
llama_synchronize(ctx->ctx);
|
|
71
|
+
} catch (const std::exception& e) {
|
|
72
|
+
SetError(e.what());
|
|
73
|
+
} catch(...) {
|
|
74
|
+
SetError("Unknown error when calling \"llama_decode\"");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
void OnOK() {
|
|
78
|
+
deferred.Resolve(Env().Undefined());
|
|
79
|
+
}
|
|
80
|
+
void OnError(const Napi::Error& err) {
|
|
81
|
+
deferred.Reject(err.Value());
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
class AddonContextLoadContextWorker : public Napi::AsyncWorker {
|
|
86
|
+
public:
|
|
87
|
+
AddonContext* context;
|
|
88
|
+
|
|
89
|
+
AddonContextLoadContextWorker(const Napi::Env& env, AddonContext* context)
|
|
90
|
+
: Napi::AsyncWorker(env, "AddonContextLoadContextWorker"),
|
|
91
|
+
context(context),
|
|
92
|
+
deferred(Napi::Promise::Deferred::New(env)) {
|
|
93
|
+
context->Ref();
|
|
94
|
+
}
|
|
95
|
+
~AddonContextLoadContextWorker() {
|
|
96
|
+
context->Unref();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
Napi::Promise GetPromise() {
|
|
100
|
+
return deferred.Promise();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
protected:
|
|
104
|
+
Napi::Promise::Deferred deferred;
|
|
105
|
+
|
|
106
|
+
void Execute() {
|
|
107
|
+
try {
|
|
108
|
+
context->ctx = llama_init_from_model(context->model->model, context->context_params);
|
|
109
|
+
|
|
110
|
+
context->contextLoaded = context->ctx != nullptr && context->ctx != NULL;
|
|
111
|
+
} catch (const std::exception& e) {
|
|
112
|
+
SetError(e.what());
|
|
113
|
+
} catch(...) {
|
|
114
|
+
SetError("Unknown error when calling \"llama_init_from_model\"");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
void OnOK() {
|
|
118
|
+
if (context->contextLoaded) {
|
|
119
|
+
uint64_t contextMemorySize = llama_state_get_size(context->ctx);
|
|
120
|
+
adjustNapiExternalMemoryAdd(Env(), contextMemorySize);
|
|
121
|
+
context->loadedContextMemorySize = contextMemorySize;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
deferred.Resolve(Napi::Boolean::New(Env(), context->contextLoaded));
|
|
125
|
+
}
|
|
126
|
+
void OnError(const Napi::Error& err) {
|
|
127
|
+
deferred.Reject(err.Value());
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
class AddonContextUnloadContextWorker : public Napi::AsyncWorker {
|
|
131
|
+
public:
|
|
132
|
+
AddonContext* context;
|
|
133
|
+
|
|
134
|
+
AddonContextUnloadContextWorker(const Napi::Env& env, AddonContext* context)
|
|
135
|
+
: Napi::AsyncWorker(env, "AddonContextUnloadContextWorker"),
|
|
136
|
+
context(context),
|
|
137
|
+
deferred(Napi::Promise::Deferred::New(env)) {
|
|
138
|
+
context->Ref();
|
|
139
|
+
}
|
|
140
|
+
~AddonContextUnloadContextWorker() {
|
|
141
|
+
context->Unref();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
Napi::Promise GetPromise() {
|
|
145
|
+
return deferred.Promise();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
protected:
|
|
149
|
+
Napi::Promise::Deferred deferred;
|
|
150
|
+
|
|
151
|
+
void Execute() {
|
|
152
|
+
try {
|
|
153
|
+
llama_free(context->ctx);
|
|
154
|
+
context->contextLoaded = false;
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
if (context->has_batch) {
|
|
158
|
+
llama_batch_free(context->batch);
|
|
159
|
+
context->has_batch = false;
|
|
160
|
+
context->batch_n_tokens = 0;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
context->dispose();
|
|
164
|
+
} catch (const std::exception& e) {
|
|
165
|
+
SetError(e.what());
|
|
166
|
+
} catch(...) {
|
|
167
|
+
SetError("Unknown error when calling \"llama_batch_free\"");
|
|
168
|
+
}
|
|
169
|
+
} catch (const std::exception& e) {
|
|
170
|
+
SetError(e.what());
|
|
171
|
+
} catch(...) {
|
|
172
|
+
SetError("Unknown error when calling \"llama_free\"");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
void OnOK() {
|
|
176
|
+
adjustNapiExternalMemorySubtract(Env(), context->loadedContextMemorySize);
|
|
177
|
+
context->loadedContextMemorySize = 0;
|
|
178
|
+
|
|
179
|
+
adjustNapiExternalMemorySubtract(Env(), context->batchMemorySize);
|
|
180
|
+
context->batchMemorySize = 0;
|
|
181
|
+
|
|
182
|
+
deferred.Resolve(Env().Undefined());
|
|
183
|
+
}
|
|
184
|
+
void OnError(const Napi::Error& err) {
|
|
185
|
+
deferred.Reject(err.Value());
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class AddonContextSampleTokenWorker : public Napi::AsyncWorker {
|
|
191
|
+
public:
|
|
192
|
+
AddonContext* ctx;
|
|
193
|
+
AddonSampler* sampler;
|
|
194
|
+
bool arrayResult = false;
|
|
195
|
+
bool returnProbabilities = false;
|
|
196
|
+
bool returnConfidence = false;
|
|
197
|
+
float tokenConfidence = -1;
|
|
198
|
+
bool has_probabilities = false;
|
|
199
|
+
size_t probabilities_size;
|
|
200
|
+
llama_token * probabilities_tokens;
|
|
201
|
+
float * probabilities_probs;
|
|
202
|
+
int32_t batchLogitIndex;
|
|
203
|
+
llama_token result;
|
|
204
|
+
bool no_output = false;
|
|
205
|
+
|
|
206
|
+
AddonContextSampleTokenWorker(const Napi::CallbackInfo& info, AddonContext* ctx)
|
|
207
|
+
: Napi::AsyncWorker(info.Env(), "AddonContextSampleTokenWorker"),
|
|
208
|
+
ctx(ctx),
|
|
209
|
+
deferred(Napi::Promise::Deferred::New(info.Env())) {
|
|
210
|
+
ctx->Ref();
|
|
211
|
+
|
|
212
|
+
batchLogitIndex = info[0].As<Napi::Number>().Int32Value();
|
|
213
|
+
sampler = Napi::ObjectWrap<AddonSampler>::Unwrap(info[1].As<Napi::Object>());
|
|
214
|
+
arrayResult = info.Length() > 2 && info[2].IsBoolean();
|
|
215
|
+
returnProbabilities = arrayResult ? info[2].As<Napi::Boolean>().Value() : false;
|
|
216
|
+
returnConfidence = arrayResult && info.Length() > 3 && info[3].IsBoolean() ? info[3].As<Napi::Boolean>().Value() : false;
|
|
217
|
+
sampler->Ref();
|
|
218
|
+
}
|
|
219
|
+
~AddonContextSampleTokenWorker() {
|
|
220
|
+
ctx->Unref();
|
|
221
|
+
sampler->Unref();
|
|
222
|
+
|
|
223
|
+
if (has_probabilities) {
|
|
224
|
+
delete[] probabilities_tokens;
|
|
225
|
+
delete[] probabilities_probs;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
Napi::Promise GetPromise() {
|
|
230
|
+
return deferred.Promise();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
protected:
|
|
234
|
+
Napi::Promise::Deferred deferred;
|
|
235
|
+
|
|
236
|
+
void Execute() {
|
|
237
|
+
try {
|
|
238
|
+
SampleToken();
|
|
239
|
+
} catch (const std::exception& e) {
|
|
240
|
+
SetError(e.what());
|
|
241
|
+
} catch(...) {
|
|
242
|
+
SetError("Unknown error when calling \"SampleToken\"");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
void SampleToken() {
|
|
247
|
+
if (llama_get_logits(ctx->ctx) == nullptr) {
|
|
248
|
+
SetError("This model does not support token generation");
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
sampler->rebuildChainIfNeeded();
|
|
253
|
+
|
|
254
|
+
const auto * logits = llama_get_logits_ith(ctx->ctx, batchLogitIndex);
|
|
255
|
+
const int n_vocab = llama_vocab_n_tokens(ctx->model->vocab);
|
|
256
|
+
|
|
257
|
+
auto & candidates = sampler->tokenCandidates;
|
|
258
|
+
for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
|
|
259
|
+
candidates[token_id] = llama_token_data{token_id, logits[token_id], 0.0f};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
llama_token_data_array cur_p = {
|
|
263
|
+
/* .data = */ candidates.data(),
|
|
264
|
+
/* .size = */ candidates.size(),
|
|
265
|
+
/* .selected = */ -1,
|
|
266
|
+
/* .sorted = */ false,
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
llama_sampler_apply(sampler->chain, &cur_p);
|
|
270
|
+
|
|
271
|
+
if (!(cur_p.selected >= 0 && cur_p.selected < (int32_t)cur_p.size)) {
|
|
272
|
+
no_output = true;
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
auto new_token_id = cur_p.data[cur_p.selected].id;
|
|
277
|
+
|
|
278
|
+
if (returnProbabilities || returnConfidence) {
|
|
279
|
+
if (!cur_p.sorted) {
|
|
280
|
+
std::sort(cur_p.data, cur_p.data + cur_p.size, [](const llama_token_data & a, const llama_token_data & b) {
|
|
281
|
+
return a.logit > b.logit;
|
|
282
|
+
});
|
|
283
|
+
cur_p.sorted = true;
|
|
284
|
+
|
|
285
|
+
for (size_t i = 0; i < cur_p.size; i++) {
|
|
286
|
+
if (cur_p.data[i].id == new_token_id) {
|
|
287
|
+
cur_p.selected = i;
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (returnProbabilities) {
|
|
295
|
+
probabilities_size = cur_p.size;
|
|
296
|
+
probabilities_tokens = new llama_token[probabilities_size];
|
|
297
|
+
probabilities_probs = new float[probabilities_size];
|
|
298
|
+
float maxLogit = cur_p.size > 0 ? cur_p.data[0].logit : -INFINITY;
|
|
299
|
+
|
|
300
|
+
for (size_t i = 0; i < cur_p.size; i++) {
|
|
301
|
+
auto logit = cur_p.data[i].logit;
|
|
302
|
+
|
|
303
|
+
probabilities_tokens[i] = cur_p.data[i].id;
|
|
304
|
+
probabilities_probs[i] = logit;
|
|
305
|
+
|
|
306
|
+
if (logit > maxLogit) {
|
|
307
|
+
maxLogit = logit;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (probabilities_size > 0 && maxLogit != -INFINITY) {
|
|
312
|
+
float sum = 0.0f;
|
|
313
|
+
for (size_t i = 0; i < probabilities_size; i++) {
|
|
314
|
+
float prob = expf(probabilities_probs[i] - maxLogit);
|
|
315
|
+
probabilities_probs[i] = prob;
|
|
316
|
+
sum += prob;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
for (size_t i = 0; i < probabilities_size; i++) {
|
|
320
|
+
probabilities_probs[i] /= sum;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
has_probabilities = true;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (returnConfidence) {
|
|
328
|
+
if (has_probabilities && cur_p.selected < probabilities_size) {
|
|
329
|
+
tokenConfidence = probabilities_probs[cur_p.selected];
|
|
330
|
+
} else {
|
|
331
|
+
float maxLogit = cur_p.data[0].logit;
|
|
332
|
+
float sum = 0.0f;
|
|
333
|
+
for (size_t i = 0; i < cur_p.size; i++) {
|
|
334
|
+
auto logit = cur_p.data[i].logit;
|
|
335
|
+
|
|
336
|
+
if (logit > maxLogit) {
|
|
337
|
+
maxLogit = logit;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
for (size_t i = 0; i < cur_p.size; i++) {
|
|
342
|
+
sum += expf(cur_p.data[i].logit - maxLogit);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
tokenConfidence = expf(cur_p.data[cur_p.selected].logit - maxLogit) / sum;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
try {
|
|
350
|
+
sampler->acceptToken(new_token_id);
|
|
351
|
+
result = new_token_id;
|
|
352
|
+
} catch (const std::exception& e) {
|
|
353
|
+
SetError(std::string("Failed to accept token in sampler: ") + e.what());
|
|
354
|
+
} catch(...) {
|
|
355
|
+
SetError("Unknown error when calling \"acceptToken\"");
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
void OnOK() {
|
|
359
|
+
Napi::Number resultToken;
|
|
360
|
+
if (no_output) {
|
|
361
|
+
resultToken = Napi::Number::New(Env(), -1);
|
|
362
|
+
} else {
|
|
363
|
+
resultToken = Napi::Number::New(Env(), static_cast<uint32_t>(result));
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (!arrayResult) {
|
|
367
|
+
deferred.Resolve(resultToken);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
Napi::Array resultArray = Napi::Array::New(Env(), 2);
|
|
372
|
+
resultArray.Set(Napi::Number::New(Env(), 0), resultToken);
|
|
373
|
+
|
|
374
|
+
if (has_probabilities) {
|
|
375
|
+
Napi::Array probabilities = Napi::Array::New(Env(), probabilities_size * 2);
|
|
376
|
+
for (size_t i = 0; i < probabilities_size; i++) {
|
|
377
|
+
probabilities.Set(i * 2, Napi::Number::New(Env(), probabilities_tokens[i]));
|
|
378
|
+
probabilities.Set(i * 2 + 1, Napi::Number::New(Env(), probabilities_probs[i]));
|
|
379
|
+
}
|
|
380
|
+
resultArray.Set(1, probabilities);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (returnConfidence && tokenConfidence != -1) {
|
|
384
|
+
resultArray.Set(2, Napi::Number::New(Env(), tokenConfidence));
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
deferred.Resolve(resultArray);
|
|
388
|
+
}
|
|
389
|
+
void OnError(const Napi::Error& err) {
|
|
390
|
+
deferred.Reject(err.Value());
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
AddonContext::AddonContext(const Napi::CallbackInfo& info) : Napi::ObjectWrap<AddonContext>(info) {
|
|
395
|
+
model = Napi::ObjectWrap<AddonModel>::Unwrap(info[0].As<Napi::Object>());
|
|
396
|
+
model->Ref();
|
|
397
|
+
|
|
398
|
+
context_params = llama_context_default_params();
|
|
399
|
+
context_params.n_ctx = 4096;
|
|
400
|
+
context_params.n_threads = std::max(cpu_get_num_math(), 1);
|
|
401
|
+
context_params.n_threads_batch = context_params.n_threads;
|
|
402
|
+
context_params.no_perf = true;
|
|
403
|
+
context_params.swa_full = false;
|
|
404
|
+
|
|
405
|
+
if (info.Length() > 1 && info[1].IsObject()) {
|
|
406
|
+
Napi::Object options = info[1].As<Napi::Object>();
|
|
407
|
+
|
|
408
|
+
if (options.Has("contextSize")) {
|
|
409
|
+
context_params.n_ctx = options.Get("contextSize").As<Napi::Number>().Uint32Value();
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (options.Has("batchSize")) {
|
|
413
|
+
context_params.n_batch = options.Get("batchSize").As<Napi::Number>().Uint32Value();
|
|
414
|
+
context_params.n_ubatch = context_params.n_batch; // the batch queue is managed in the JS side, so there's no need for managing it on the C++ side
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (options.Has("sequences")) {
|
|
418
|
+
context_params.n_seq_max = options.Get("sequences").As<Napi::Number>().Uint32Value();
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (options.Has("embeddings")) {
|
|
422
|
+
context_params.embeddings = options.Get("embeddings").As<Napi::Boolean>().Value();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
if (options.Has("ranking") && options.Get("ranking").As<Napi::Boolean>().Value()) {
|
|
426
|
+
context_params.pooling_type = LLAMA_POOLING_TYPE_RANK;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (options.Has("flashAttention")) {
|
|
430
|
+
bool flashAttention = options.Get("flashAttention").As<Napi::Boolean>().Value();
|
|
431
|
+
context_params.flash_attn_type = flashAttention ? LLAMA_FLASH_ATTN_TYPE_ENABLED : LLAMA_FLASH_ATTN_TYPE_DISABLED;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (options.Has("threads")) {
|
|
435
|
+
const auto n_threads = options.Get("threads").As<Napi::Number>().Int32Value();
|
|
436
|
+
const auto resolved_n_threads = n_threads == 0 ? std::max((int32_t)std::thread::hardware_concurrency(), context_params.n_threads) : n_threads;
|
|
437
|
+
|
|
438
|
+
context_params.n_threads = resolved_n_threads;
|
|
439
|
+
context_params.n_threads_batch = resolved_n_threads;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (options.Has("performanceTracking")) {
|
|
443
|
+
context_params.no_perf = !(options.Get("performanceTracking").As<Napi::Boolean>().Value());
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (options.Has("kvCacheKeyType") && options.Get("kvCacheKeyType").IsNumber()) {
|
|
447
|
+
auto keyType = options.Get("kvCacheKeyType").As<Napi::Number>().Int32Value();
|
|
448
|
+
if (keyType >= 0 && keyType < GGML_TYPE_COUNT) {
|
|
449
|
+
context_params.type_k = static_cast<ggml_type>(keyType);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (options.Has("kvCacheValueType") && options.Get("kvCacheValueType").IsNumber()) {
|
|
454
|
+
auto valueType = options.Get("kvCacheValueType").As<Napi::Number>().Int32Value();
|
|
455
|
+
if (valueType >= 0 && valueType < GGML_TYPE_COUNT) {
|
|
456
|
+
context_params.type_v = static_cast<ggml_type>(valueType);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
if (options.Has("swaFullCache")) {
|
|
461
|
+
context_params.swa_full = options.Get("swaFullCache").As<Napi::Boolean>().Value();
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
AddonContext::~AddonContext() {
|
|
466
|
+
dispose();
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
void AddonContext::dispose() {
|
|
470
|
+
if (disposed) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
disposed = true;
|
|
475
|
+
if (contextLoaded) {
|
|
476
|
+
contextLoaded = false;
|
|
477
|
+
llama_free(ctx);
|
|
478
|
+
|
|
479
|
+
adjustNapiExternalMemorySubtract(Env(), loadedContextMemorySize);
|
|
480
|
+
loadedContextMemorySize = 0;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
model->Unref();
|
|
484
|
+
|
|
485
|
+
disposeBatch();
|
|
486
|
+
}
|
|
487
|
+
void AddonContext::disposeBatch() {
|
|
488
|
+
if (!has_batch) {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
llama_batch_free(batch);
|
|
493
|
+
has_batch = false;
|
|
494
|
+
batch_n_tokens = 0;
|
|
495
|
+
|
|
496
|
+
adjustNapiExternalMemorySubtract(Env(), batchMemorySize);
|
|
497
|
+
batchMemorySize = 0;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
Napi::Value AddonContext::Init(const Napi::CallbackInfo& info) {
|
|
501
|
+
if (disposed) {
|
|
502
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
503
|
+
return info.Env().Undefined();
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
AddonContextLoadContextWorker* worker = new AddonContextLoadContextWorker(this->Env(), this);
|
|
507
|
+
worker->Queue();
|
|
508
|
+
return worker->GetPromise();
|
|
509
|
+
}
|
|
510
|
+
Napi::Value AddonContext::Dispose(const Napi::CallbackInfo& info) {
|
|
511
|
+
if (disposed) {
|
|
512
|
+
return info.Env().Undefined();
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (contextLoaded) {
|
|
516
|
+
contextLoaded = false;
|
|
517
|
+
|
|
518
|
+
AddonContextUnloadContextWorker* worker = new AddonContextUnloadContextWorker(this->Env(), this);
|
|
519
|
+
worker->Queue();
|
|
520
|
+
return worker->GetPromise();
|
|
521
|
+
} else {
|
|
522
|
+
dispose();
|
|
523
|
+
|
|
524
|
+
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(info.Env());
|
|
525
|
+
deferred.Resolve(info.Env().Undefined());
|
|
526
|
+
return deferred.Promise();
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
Napi::Value AddonContext::GetContextSize(const Napi::CallbackInfo& info) {
|
|
531
|
+
if (disposed) {
|
|
532
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
533
|
+
return info.Env().Undefined();
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
return Napi::Number::From(info.Env(), llama_n_ctx(ctx));
|
|
537
|
+
}
|
|
538
|
+
Napi::Value AddonContext::InitBatch(const Napi::CallbackInfo& info) {
|
|
539
|
+
if (disposed) {
|
|
540
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
541
|
+
return info.Env().Undefined();
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (has_batch) {
|
|
545
|
+
llama_batch_free(batch);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
int32_t n_tokens = info[0].As<Napi::Number>().Int32Value();
|
|
549
|
+
|
|
550
|
+
batch = llama_batch_init(n_tokens, 0, 1);
|
|
551
|
+
has_batch = true;
|
|
552
|
+
batch_n_tokens = n_tokens;
|
|
553
|
+
|
|
554
|
+
uint64_t newBatchMemorySize = calculateBatchMemorySize(n_tokens, llama_model_n_embd(model->model), context_params.n_batch);
|
|
555
|
+
if (newBatchMemorySize > batchMemorySize) {
|
|
556
|
+
adjustNapiExternalMemoryAdd(Env(), newBatchMemorySize - batchMemorySize);
|
|
557
|
+
batchMemorySize = newBatchMemorySize;
|
|
558
|
+
} else if (newBatchMemorySize < batchMemorySize) {
|
|
559
|
+
adjustNapiExternalMemorySubtract(Env(), batchMemorySize - newBatchMemorySize);
|
|
560
|
+
batchMemorySize = newBatchMemorySize;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
return info.Env().Undefined();
|
|
564
|
+
}
|
|
565
|
+
Napi::Value AddonContext::DisposeBatch(const Napi::CallbackInfo& info) {
|
|
566
|
+
if (disposed) {
|
|
567
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
568
|
+
return info.Env().Undefined();
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
disposeBatch();
|
|
572
|
+
|
|
573
|
+
return info.Env().Undefined();
|
|
574
|
+
}
|
|
575
|
+
Napi::Value AddonContext::AddToBatch(const Napi::CallbackInfo& info) {
|
|
576
|
+
if (!has_batch) {
|
|
577
|
+
Napi::Error::New(info.Env(), "No batch is initialized").ThrowAsJavaScriptException();
|
|
578
|
+
return info.Env().Undefined();
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
int32_t sequenceId = info[0].As<Napi::Number>().Int32Value();
|
|
582
|
+
int32_t firstTokenContextIndex = info[1].As<Napi::Number>().Int32Value();
|
|
583
|
+
Napi::Uint32Array tokens = info[2].As<Napi::Uint32Array>();
|
|
584
|
+
Napi::Uint32Array tokenLogitIndexes = info[3].As<Napi::Uint32Array>();
|
|
585
|
+
|
|
586
|
+
auto tokensLength = tokens.ElementLength();
|
|
587
|
+
auto tokenLogitIndexesLength = tokenLogitIndexes.ElementLength();
|
|
588
|
+
GGML_ASSERT(batch.n_tokens + tokensLength <= batch_n_tokens);
|
|
589
|
+
|
|
590
|
+
Napi::Uint32Array resLogitIndexes = Napi::Uint32Array::New(info.Env(), tokenLogitIndexesLength);
|
|
591
|
+
|
|
592
|
+
for (size_t i = 0, l = 0; i < tokensLength; i++) {
|
|
593
|
+
if (l < tokenLogitIndexesLength && l < tokenLogitIndexesLength && tokenLogitIndexes[l] == i) {
|
|
594
|
+
common_batch_add(batch, static_cast<llama_token>(tokens[i]), firstTokenContextIndex + i, { sequenceId }, true);
|
|
595
|
+
resLogitIndexes[l] = batch.n_tokens - 1;
|
|
596
|
+
l++;
|
|
597
|
+
} else {
|
|
598
|
+
common_batch_add(batch, static_cast<llama_token>(tokens[i]), firstTokenContextIndex + i, { sequenceId }, false);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
return resLogitIndexes;
|
|
603
|
+
}
|
|
604
|
+
Napi::Value AddonContext::DisposeSequence(const Napi::CallbackInfo& info) {
|
|
605
|
+
if (disposed) {
|
|
606
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
607
|
+
return info.Env().Undefined();
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
int32_t sequenceId = info[0].As<Napi::Number>().Int32Value();
|
|
611
|
+
|
|
612
|
+
bool result = llama_memory_seq_rm(llama_get_memory(ctx), sequenceId, -1, -1);
|
|
613
|
+
|
|
614
|
+
if (!result) {
|
|
615
|
+
Napi::Error::New(info.Env(), "Failed to dispose sequence").ThrowAsJavaScriptException();
|
|
616
|
+
return info.Env().Undefined();
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
return info.Env().Undefined();
|
|
620
|
+
}
|
|
621
|
+
Napi::Value AddonContext::RemoveTokenCellsFromSequence(const Napi::CallbackInfo& info) {
|
|
622
|
+
if (disposed) {
|
|
623
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
624
|
+
return info.Env().Undefined();
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
int32_t sequenceId = info[0].As<Napi::Number>().Int32Value();
|
|
628
|
+
int32_t startPos = info[1].As<Napi::Number>().Int32Value();
|
|
629
|
+
int32_t endPos = info[2].As<Napi::Number>().Int32Value();
|
|
630
|
+
|
|
631
|
+
bool result = llama_memory_seq_rm(llama_get_memory(ctx), sequenceId, startPos, endPos);
|
|
632
|
+
|
|
633
|
+
return Napi::Boolean::New(info.Env(), result);
|
|
634
|
+
}
|
|
635
|
+
Napi::Value AddonContext::ShiftSequenceTokenCells(const Napi::CallbackInfo& info) {
|
|
636
|
+
if (disposed) {
|
|
637
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
638
|
+
return info.Env().Undefined();
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
int32_t sequenceId = info[0].As<Napi::Number>().Int32Value();
|
|
642
|
+
int32_t startPos = info[1].As<Napi::Number>().Int32Value();
|
|
643
|
+
int32_t endPos = info[2].As<Napi::Number>().Int32Value();
|
|
644
|
+
int32_t shiftDelta = info[3].As<Napi::Number>().Int32Value();
|
|
645
|
+
|
|
646
|
+
llama_memory_seq_add(llama_get_memory(ctx), sequenceId, startPos, endPos, shiftDelta);
|
|
647
|
+
|
|
648
|
+
return info.Env().Undefined();
|
|
649
|
+
}
|
|
650
|
+
Napi::Value AddonContext::GetSequenceKvCacheMinPosition(const Napi::CallbackInfo& info) {
|
|
651
|
+
if (disposed) {
|
|
652
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
653
|
+
return info.Env().Undefined();
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
int32_t sequenceId = info[0].As<Napi::Number>().Int32Value();
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
const auto minPosition = llama_memory_seq_pos_min(llama_get_memory(ctx), sequenceId);
|
|
660
|
+
|
|
661
|
+
return Napi::Number::New(info.Env(), minPosition);
|
|
662
|
+
}
|
|
663
|
+
Napi::Value AddonContext::GetSequenceKvCacheMaxPosition(const Napi::CallbackInfo& info) {
|
|
664
|
+
if (disposed) {
|
|
665
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
666
|
+
return info.Env().Undefined();
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
int32_t sequenceId = info[0].As<Napi::Number>().Int32Value();
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
const auto maxPosition = llama_memory_seq_pos_max(llama_get_memory(ctx), sequenceId);
|
|
673
|
+
|
|
674
|
+
return Napi::Number::New(info.Env(), maxPosition);
|
|
675
|
+
}
|
|
676
|
+
Napi::Value AddonContext::DecodeBatch(const Napi::CallbackInfo& info) {
|
|
677
|
+
AddonContextDecodeBatchWorker* worker = new AddonContextDecodeBatchWorker(info.Env(), this);
|
|
678
|
+
worker->Queue();
|
|
679
|
+
return worker->GetPromise();
|
|
680
|
+
}
|
|
681
|
+
Napi::Value AddonContext::SampleToken(const Napi::CallbackInfo& info) {
|
|
682
|
+
AddonContextSampleTokenWorker* worker = new AddonContextSampleTokenWorker(info, this);
|
|
683
|
+
worker->Queue();
|
|
684
|
+
return worker->GetPromise();
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
Napi::Value AddonContext::GetEmbedding(const Napi::CallbackInfo& info) {
|
|
688
|
+
if (disposed) {
|
|
689
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
690
|
+
return info.Env().Undefined();
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
int32_t inputTokensLength = info[0].As<Napi::Number>().Int32Value();
|
|
694
|
+
int32_t maxVectorSize = (info.Length() > 1 && info[1].IsNumber()) ? info[1].As<Napi::Number>().Int32Value() : 0;
|
|
695
|
+
|
|
696
|
+
if (inputTokensLength <= 0) {
|
|
697
|
+
Napi::Error::New(info.Env(), "Invalid input tokens length").ThrowAsJavaScriptException();
|
|
698
|
+
return info.Env().Undefined();
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
const int n_embd = llama_model_n_embd(model->model);
|
|
702
|
+
const enum llama_pooling_type pooling_type = llama_pooling_type(ctx);
|
|
703
|
+
const auto* embeddings = pooling_type == LLAMA_POOLING_TYPE_NONE ? NULL : llama_get_embeddings_seq(ctx, 0);
|
|
704
|
+
if (embeddings == NULL) {
|
|
705
|
+
embeddings = llama_get_embeddings_ith(ctx, inputTokensLength - 1);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
if (embeddings == NULL) {
|
|
709
|
+
Napi::Error::New(info.Env(), std::string("Failed to get embeddings for token ") + std::to_string(inputTokensLength - 1)).ThrowAsJavaScriptException();
|
|
710
|
+
return info.Env().Undefined();
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
size_t resultSize = maxVectorSize == 0 ? n_embd : std::min(n_embd, maxVectorSize);
|
|
714
|
+
Napi::Float64Array result = Napi::Float64Array::New(info.Env(), resultSize);
|
|
715
|
+
for (size_t i = 0; i < resultSize; i++) {
|
|
716
|
+
result[i] = embeddings[i];
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
return result;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
Napi::Value AddonContext::GetStateSize(const Napi::CallbackInfo& info) {
|
|
723
|
+
if (disposed) {
|
|
724
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
725
|
+
return info.Env().Undefined();
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
return Napi::Number::From(info.Env(), llama_state_get_size(ctx));
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
Napi::Value AddonContext::GetThreads(const Napi::CallbackInfo& info) {
|
|
732
|
+
if (disposed) {
|
|
733
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
734
|
+
return info.Env().Undefined();
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
return Napi::Number::From(info.Env(), llama_n_threads(ctx));
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
Napi::Value AddonContext::SetThreads(const Napi::CallbackInfo& info) {
|
|
741
|
+
if (disposed) {
|
|
742
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
743
|
+
return info.Env().Undefined();
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
const auto threads = info[0].As<Napi::Number>().Int32Value();
|
|
747
|
+
const auto resolvedThreads = threads == 0
|
|
748
|
+
? std::max((int32_t)std::thread::hardware_concurrency(), std::max(cpu_get_num_math(), 1))
|
|
749
|
+
: threads;
|
|
750
|
+
|
|
751
|
+
if (llama_n_threads(ctx) != resolvedThreads) {
|
|
752
|
+
llama_set_n_threads(ctx, resolvedThreads, resolvedThreads);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
return info.Env().Undefined();
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
class AddonContextSaveSequenceStateToFileWorker : public Napi::AsyncWorker {
|
|
759
|
+
public:
|
|
760
|
+
AddonContext* context;
|
|
761
|
+
std::string filepath;
|
|
762
|
+
llama_seq_id sequenceId;
|
|
763
|
+
std::vector<llama_token> tokens;
|
|
764
|
+
size_t savedFileSize = 0;
|
|
765
|
+
|
|
766
|
+
AddonContextSaveSequenceStateToFileWorker(const Napi::CallbackInfo& info, AddonContext* context)
|
|
767
|
+
: Napi::AsyncWorker(info.Env(), "AddonContextSaveSequenceStateToFileWorker"),
|
|
768
|
+
context(context),
|
|
769
|
+
deferred(Napi::Promise::Deferred::New(info.Env())) {
|
|
770
|
+
context->Ref();
|
|
771
|
+
|
|
772
|
+
filepath = info[0].As<Napi::String>().Utf8Value();
|
|
773
|
+
sequenceId = info[1].As<Napi::Number>().Int32Value();
|
|
774
|
+
Napi::Uint32Array inputTokens = info[2].As<Napi::Uint32Array>();
|
|
775
|
+
|
|
776
|
+
tokens.resize(inputTokens.ElementLength());
|
|
777
|
+
for (size_t i = 0; i < tokens.size(); i++) {
|
|
778
|
+
tokens[i] = inputTokens[i];
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
~AddonContextSaveSequenceStateToFileWorker() {
|
|
782
|
+
context->Unref();
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
Napi::Promise GetPromise() {
|
|
786
|
+
return deferred.Promise();
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
protected:
|
|
790
|
+
Napi::Promise::Deferred deferred;
|
|
791
|
+
|
|
792
|
+
void Execute() {
|
|
793
|
+
try {
|
|
794
|
+
savedFileSize = llama_state_seq_save_file(context->ctx, filepath.c_str(), sequenceId, tokens.data(), tokens.size());
|
|
795
|
+
if (savedFileSize == 0) {
|
|
796
|
+
SetError("Failed to save state to file");
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
} catch (const std::exception& e) {
|
|
800
|
+
SetError(e.what());
|
|
801
|
+
} catch(...) {
|
|
802
|
+
SetError("Unknown error when calling \"llama_state_seq_save_file\"");
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
void OnOK() {
|
|
806
|
+
deferred.Resolve(Napi::Number::New(Env(), savedFileSize));
|
|
807
|
+
}
|
|
808
|
+
void OnError(const Napi::Error& err) {
|
|
809
|
+
deferred.Reject(err.Value());
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
Napi::Value AddonContext::SaveSequenceStateToFile(const Napi::CallbackInfo& info) {
|
|
813
|
+
if (disposed) {
|
|
814
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
815
|
+
return info.Env().Undefined();
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
AddonContextSaveSequenceStateToFileWorker* worker = new AddonContextSaveSequenceStateToFileWorker(info, this);
|
|
819
|
+
worker->Queue();
|
|
820
|
+
return worker->GetPromise();
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
class AddonContextLoadSequenceStateFromFileWorker : public Napi::AsyncWorker {
|
|
824
|
+
public:
|
|
825
|
+
AddonContext* context;
|
|
826
|
+
std::string filepath;
|
|
827
|
+
llama_seq_id sequenceId;
|
|
828
|
+
size_t maxContextSize;
|
|
829
|
+
std::vector<llama_token> tokens;
|
|
830
|
+
|
|
831
|
+
AddonContextLoadSequenceStateFromFileWorker(const Napi::CallbackInfo& info, AddonContext* context)
|
|
832
|
+
: Napi::AsyncWorker(info.Env(), "AddonContextLoadSequenceStateFromFileWorker"),
|
|
833
|
+
context(context),
|
|
834
|
+
deferred(Napi::Promise::Deferred::New(info.Env())) {
|
|
835
|
+
context->Ref();
|
|
836
|
+
|
|
837
|
+
filepath = info[0].As<Napi::String>().Utf8Value();
|
|
838
|
+
sequenceId = info[1].As<Napi::Number>().Int32Value();
|
|
839
|
+
maxContextSize = info[2].As<Napi::Number>().Uint32Value();
|
|
840
|
+
|
|
841
|
+
tokens.resize(maxContextSize);
|
|
842
|
+
}
|
|
843
|
+
~AddonContextLoadSequenceStateFromFileWorker() {
|
|
844
|
+
context->Unref();
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
Napi::Promise GetPromise() {
|
|
848
|
+
return deferred.Promise();
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
protected:
|
|
852
|
+
Napi::Promise::Deferred deferred;
|
|
853
|
+
|
|
854
|
+
void Execute() {
|
|
855
|
+
try {
|
|
856
|
+
size_t tokenCount = 0;
|
|
857
|
+
const size_t fileSize = llama_state_seq_load_file(context->ctx, filepath.c_str(), sequenceId, tokens.data(), tokens.size(), &tokenCount);
|
|
858
|
+
if (fileSize == 0) {
|
|
859
|
+
SetError("Failed to load state from file. Current context sequence size may be smaller that the state of the file");
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
tokens.resize(tokenCount);
|
|
864
|
+
} catch (const std::exception& e) {
|
|
865
|
+
SetError(e.what());
|
|
866
|
+
} catch(...) {
|
|
867
|
+
SetError("Unknown error when calling \"llama_state_seq_load_file\"");
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
void OnOK() {
|
|
871
|
+
size_t tokenCount = tokens.size();
|
|
872
|
+
Napi::Uint32Array result = Napi::Uint32Array::New(Env(), tokenCount);
|
|
873
|
+
|
|
874
|
+
for (size_t i = 0; i < tokenCount; i++) {
|
|
875
|
+
result[i] = tokens[i];
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
deferred.Resolve(result);
|
|
879
|
+
}
|
|
880
|
+
void OnError(const Napi::Error& err) {
|
|
881
|
+
deferred.Reject(err.Value());
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
Napi::Value AddonContext::LoadSequenceStateFromFile(const Napi::CallbackInfo& info) {
|
|
885
|
+
if (disposed) {
|
|
886
|
+
Napi::Error::New(info.Env(), "Context is disposed").ThrowAsJavaScriptException();
|
|
887
|
+
return info.Env().Undefined();
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
AddonContextLoadSequenceStateFromFileWorker* worker = new AddonContextLoadSequenceStateFromFileWorker(info, this);
|
|
891
|
+
worker->Queue();
|
|
892
|
+
return worker->GetPromise();
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
Napi::Value AddonContext::PrintTimings(const Napi::CallbackInfo& info) {
|
|
896
|
+
llama_perf_context_print(ctx);
|
|
897
|
+
llama_perf_context_reset(ctx);
|
|
898
|
+
return info.Env().Undefined();
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
Napi::Value AddonContext::EnsureDraftContextIsCompatibleForSpeculative(const Napi::CallbackInfo& info) {
|
|
902
|
+
constexpr auto vocabSizeMaxDifference = 128; // SPEC_VOCAB_MAX_SIZE_DIFFERENCE
|
|
903
|
+
constexpr auto vocabCheckStartTokenId = 5; // SPEC_VOCAB_CHECK_START_TOKEN_ID
|
|
904
|
+
|
|
905
|
+
const AddonContext * draftContext = Napi::ObjectWrap<AddonContext>::Unwrap(info[0].As<Napi::Object>());
|
|
906
|
+
const auto currentCtx = ctx;
|
|
907
|
+
const auto draftCtx = draftContext->ctx;
|
|
908
|
+
const auto currentModel = model->model;
|
|
909
|
+
const auto draftModel = draftContext->model->model;
|
|
910
|
+
const auto currentVocab = model->vocab;
|
|
911
|
+
const auto draftVocab = draftContext->model->vocab;
|
|
912
|
+
|
|
913
|
+
if (llama_vocab_type(currentVocab) != llama_vocab_type(draftVocab)) {
|
|
914
|
+
Napi::Error::New(info.Env(), "Speculative draft model vocabulary type must match the target model vocabulary type").ThrowAsJavaScriptException();
|
|
915
|
+
return info.Env().Undefined();
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
if (llama_vocab_get_add_bos(currentVocab) != llama_vocab_get_add_bos(draftVocab) ||
|
|
919
|
+
llama_vocab_get_add_eos(currentVocab) != llama_vocab_get_add_eos(draftVocab) ||
|
|
920
|
+
llama_vocab_bos(currentVocab) != llama_vocab_bos(draftVocab) ||
|
|
921
|
+
llama_vocab_eos(currentVocab) != llama_vocab_eos(draftVocab)
|
|
922
|
+
) {
|
|
923
|
+
Napi::Error::New(info.Env(), "Speculative draft model special tokens must match the target model special tokens").ThrowAsJavaScriptException();
|
|
924
|
+
return info.Env().Undefined();
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
const int currentModelVocabSize = llama_vocab_n_tokens(currentVocab);
|
|
928
|
+
const int draftModelVocabSize = llama_vocab_n_tokens(draftVocab);
|
|
929
|
+
|
|
930
|
+
const int vocabDiff = std::abs(currentModelVocabSize - draftModelVocabSize);
|
|
931
|
+
|
|
932
|
+
if (vocabDiff > vocabSizeMaxDifference) {
|
|
933
|
+
Napi::Error::New(
|
|
934
|
+
info.Env(),
|
|
935
|
+
std::string("Speculative draft model vocabulary must closely match the target model vocabulary size (vocabulary size difference: ") +
|
|
936
|
+
std::to_string(vocabDiff) + std::string(", max allowed: ") + std::to_string(vocabSizeMaxDifference) + std::string(")")
|
|
937
|
+
).ThrowAsJavaScriptException();
|
|
938
|
+
return info.Env().Undefined();
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
const int minVocabSize = std::min(currentModelVocabSize, draftModelVocabSize);
|
|
942
|
+
for (int i = vocabCheckStartTokenId; i < minVocabSize; ++i) {
|
|
943
|
+
const char * currentTokenText = llama_vocab_get_text(currentVocab, i);
|
|
944
|
+
const char * draftTokenText = llama_vocab_get_text(draftVocab, i);
|
|
945
|
+
if (std::strcmp(currentTokenText, draftTokenText) != 0) {
|
|
946
|
+
Napi::Error::New(
|
|
947
|
+
info.Env(),
|
|
948
|
+
std::string("Speculative draft model vocabulary must match the target model vocabulary, but token ") +
|
|
949
|
+
std::to_string(i) + std::string(" content differs. Target: \"") + std::string(currentTokenText) +
|
|
950
|
+
std::string("\", Draft: \"") + std::string(draftTokenText) + std::string("")
|
|
951
|
+
).ThrowAsJavaScriptException();
|
|
952
|
+
return info.Env().Undefined();
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
return info.Env().Undefined();
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
Napi::Value AddonContext::SetLoras(const Napi::CallbackInfo& info) {
|
|
960
|
+
Napi::Array loraArray = info[0].As<Napi::Array>();
|
|
961
|
+
Napi::Array scaleArray = info[1].As<Napi::Array>();
|
|
962
|
+
|
|
963
|
+
std::vector<llama_adapter_lora *> loras;
|
|
964
|
+
std::vector<float> scales;
|
|
965
|
+
|
|
966
|
+
loras.reserve(loraArray.Length());
|
|
967
|
+
scales.reserve(scaleArray.Length());
|
|
968
|
+
|
|
969
|
+
for (size_t i = 0; i < loraArray.Length() && i < scaleArray.Length(); i++) {
|
|
970
|
+
AddonModelLora* lora = Napi::ObjectWrap<AddonModelLora>::Unwrap(loraArray.Get(i).As<Napi::Object>());
|
|
971
|
+
float scale = scaleArray.Get(i).As<Napi::Number>().FloatValue();
|
|
972
|
+
|
|
973
|
+
loras.push_back(lora->lora_adapter);
|
|
974
|
+
scales.push_back(scale);
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
llama_set_adapters_lora(ctx, loras.data(), loras.size(), scales.data());
|
|
978
|
+
|
|
979
|
+
return info.Env().Undefined();
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
class RestoreCheckpointWorker : public Napi::AsyncWorker {
|
|
983
|
+
public:
|
|
984
|
+
AddonContext* context;
|
|
985
|
+
AddonContextSequenceCheckpoint* checkpoint;
|
|
986
|
+
std::size_t maxPosIndex;
|
|
987
|
+
bool restoreSuccess = false;
|
|
988
|
+
|
|
989
|
+
RestoreCheckpointWorker(const Napi::CallbackInfo& info, AddonContext* context, AddonContextSequenceCheckpoint* checkpoint, std::size_t maxPosIndex)
|
|
990
|
+
: Napi::AsyncWorker(info.Env(), "RestoreCheckpointWorker"),
|
|
991
|
+
context(context),
|
|
992
|
+
checkpoint(checkpoint),
|
|
993
|
+
maxPosIndex(maxPosIndex),
|
|
994
|
+
deferred(Napi::Promise::Deferred::New(info.Env())) {
|
|
995
|
+
context->Ref();
|
|
996
|
+
checkpoint->Ref();
|
|
997
|
+
}
|
|
998
|
+
~RestoreCheckpointWorker() {
|
|
999
|
+
context->Unref();
|
|
1000
|
+
checkpoint->Unref();
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
Napi::Promise GetPromise() {
|
|
1004
|
+
return deferred.Promise();
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
protected:
|
|
1008
|
+
Napi::Promise::Deferred deferred;
|
|
1009
|
+
|
|
1010
|
+
void Execute() {
|
|
1011
|
+
try {
|
|
1012
|
+
std::lock_guard<std::mutex> lock(checkpoint->dataMutex);
|
|
1013
|
+
|
|
1014
|
+
std::size_t dataSize = checkpoint->data.size();
|
|
1015
|
+
std::size_t restoreSize = llama_state_seq_set_data_ext(context->ctx, checkpoint->data.data(), dataSize, checkpoint->sequenceId, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY);
|
|
1016
|
+
if (restoreSize == dataSize) {
|
|
1017
|
+
restoreSuccess = (
|
|
1018
|
+
llama_memory_seq_rm(llama_get_memory(context->ctx), checkpoint->sequenceId, maxPosIndex + 1, -1) &&
|
|
1019
|
+
llama_memory_seq_pos_max(llama_get_memory(context->ctx), checkpoint->sequenceId) == maxPosIndex
|
|
1020
|
+
);
|
|
1021
|
+
}
|
|
1022
|
+
} catch (const std::exception& e) {
|
|
1023
|
+
SetError(e.what());
|
|
1024
|
+
} catch(...) {
|
|
1025
|
+
SetError("Unknown error when calling \"llama_state_seq_set_data_ext\"");
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
void OnOK() {
|
|
1029
|
+
deferred.Resolve(Napi::Boolean::New(Env(), restoreSuccess));
|
|
1030
|
+
}
|
|
1031
|
+
void OnError(const Napi::Error& err) {
|
|
1032
|
+
deferred.Reject(err.Value());
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
|
|
1036
|
+
Napi::Value AddonContext::RestoreCheckpoint(const Napi::CallbackInfo& info) {
|
|
1037
|
+
AddonContextSequenceCheckpoint* checkpoint = Napi::ObjectWrap<AddonContextSequenceCheckpoint>::Unwrap(info[0].As<Napi::Object>());
|
|
1038
|
+
std::size_t maxPosIndex = info[1].As<Napi::Number>().Int32Value();
|
|
1039
|
+
|
|
1040
|
+
RestoreCheckpointWorker* worker = new RestoreCheckpointWorker(info, this, checkpoint, maxPosIndex);
|
|
1041
|
+
worker->Queue();
|
|
1042
|
+
return worker->GetPromise();
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
void AddonContext::init(Napi::Object exports) {
|
|
1046
|
+
exports.Set(
|
|
1047
|
+
"AddonContext",
|
|
1048
|
+
DefineClass(
|
|
1049
|
+
exports.Env(),
|
|
1050
|
+
"AddonContext",
|
|
1051
|
+
{
|
|
1052
|
+
InstanceMethod("init", &AddonContext::Init),
|
|
1053
|
+
InstanceMethod("getContextSize", &AddonContext::GetContextSize),
|
|
1054
|
+
InstanceMethod("initBatch", &AddonContext::InitBatch),
|
|
1055
|
+
InstanceMethod("addToBatch", &AddonContext::AddToBatch),
|
|
1056
|
+
InstanceMethod("disposeSequence", &AddonContext::DisposeSequence),
|
|
1057
|
+
InstanceMethod("removeTokenCellsFromSequence", &AddonContext::RemoveTokenCellsFromSequence),
|
|
1058
|
+
InstanceMethod("shiftSequenceTokenCells", &AddonContext::ShiftSequenceTokenCells),
|
|
1059
|
+
InstanceMethod("getSequenceKvCacheMinPosition", &AddonContext::GetSequenceKvCacheMinPosition),
|
|
1060
|
+
InstanceMethod("getSequenceKvCacheMaxPosition", &AddonContext::GetSequenceKvCacheMaxPosition),
|
|
1061
|
+
InstanceMethod("decodeBatch", &AddonContext::DecodeBatch),
|
|
1062
|
+
InstanceMethod("sampleToken", &AddonContext::SampleToken),
|
|
1063
|
+
InstanceMethod("getEmbedding", &AddonContext::GetEmbedding),
|
|
1064
|
+
InstanceMethod("getStateSize", &AddonContext::GetStateSize),
|
|
1065
|
+
InstanceMethod("getThreads", &AddonContext::GetThreads),
|
|
1066
|
+
InstanceMethod("setThreads", &AddonContext::SetThreads),
|
|
1067
|
+
InstanceMethod("printTimings", &AddonContext::PrintTimings),
|
|
1068
|
+
InstanceMethod("ensureDraftContextIsCompatibleForSpeculative", &AddonContext::EnsureDraftContextIsCompatibleForSpeculative),
|
|
1069
|
+
InstanceMethod("saveSequenceStateToFile", &AddonContext::SaveSequenceStateToFile),
|
|
1070
|
+
InstanceMethod("loadSequenceStateFromFile", &AddonContext::LoadSequenceStateFromFile),
|
|
1071
|
+
InstanceMethod("setLoras", &AddonContext::SetLoras),
|
|
1072
|
+
InstanceMethod("restoreCheckpoint", &AddonContext::RestoreCheckpoint),
|
|
1073
|
+
InstanceMethod("dispose", &AddonContext::Dispose),
|
|
1074
|
+
}
|
|
1075
|
+
)
|
|
1076
|
+
);
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
AddonContextSequenceCheckpoint::AddonContextSequenceCheckpoint(const Napi::CallbackInfo& info) : Napi::ObjectWrap<AddonContextSequenceCheckpoint>(info) {
|
|
1080
|
+
|
|
1081
|
+
}
|
|
1082
|
+
AddonContextSequenceCheckpoint::~AddonContextSequenceCheckpoint() {
|
|
1083
|
+
dispose();
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
class AddonContextSequenceCheckpointInitWorker : public Napi::AsyncWorker {
|
|
1087
|
+
public:
|
|
1088
|
+
AddonContextSequenceCheckpoint* checkpoint;
|
|
1089
|
+
AddonContext* context;
|
|
1090
|
+
|
|
1091
|
+
AddonContextSequenceCheckpointInitWorker(const Napi::CallbackInfo& info, AddonContextSequenceCheckpoint* checkpoint, AddonContext* context)
|
|
1092
|
+
: Napi::AsyncWorker(info.Env(), "AddonContextSequenceCheckpointInitWorker"),
|
|
1093
|
+
checkpoint(checkpoint),
|
|
1094
|
+
context(context),
|
|
1095
|
+
deferred(Napi::Promise::Deferred::New(info.Env())) {
|
|
1096
|
+
checkpoint->Ref();
|
|
1097
|
+
context->Ref();
|
|
1098
|
+
}
|
|
1099
|
+
~AddonContextSequenceCheckpointInitWorker() {
|
|
1100
|
+
checkpoint->Unref();
|
|
1101
|
+
context->Unref();
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
Napi::Promise GetPromise() {
|
|
1105
|
+
return deferred.Promise();
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
protected:
|
|
1109
|
+
Napi::Promise::Deferred deferred;
|
|
1110
|
+
|
|
1111
|
+
void Execute() {
|
|
1112
|
+
try {
|
|
1113
|
+
checkpoint->minPos = llama_memory_seq_pos_min(llama_get_memory(context->ctx), checkpoint->sequenceId);
|
|
1114
|
+
checkpoint->maxPos = llama_memory_seq_pos_max(llama_get_memory(context->ctx), checkpoint->sequenceId);
|
|
1115
|
+
const size_t checkpointSize = llama_state_seq_get_size_ext(context->ctx, checkpoint->sequenceId, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY);
|
|
1116
|
+
|
|
1117
|
+
checkpoint->data.resize(checkpointSize, 0);
|
|
1118
|
+
llama_state_seq_get_data_ext(context->ctx, checkpoint->data.data(), checkpointSize, checkpoint->sequenceId, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY);
|
|
1119
|
+
} catch (const std::exception& e) {
|
|
1120
|
+
SetError(e.what());
|
|
1121
|
+
} catch(...) {
|
|
1122
|
+
SetError("Unknown error when calling \"llama_state_seq_get_data_ext\"");
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
void OnOK() {
|
|
1126
|
+
deferred.Resolve(Env().Undefined());
|
|
1127
|
+
}
|
|
1128
|
+
void OnError(const Napi::Error& err) {
|
|
1129
|
+
deferred.Reject(err.Value());
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
|
|
1133
|
+
Napi::Value AddonContextSequenceCheckpoint::Init(const Napi::CallbackInfo& info) {
|
|
1134
|
+
AddonContext * context = Napi::ObjectWrap<AddonContext>::Unwrap(info[0].As<Napi::Object>());
|
|
1135
|
+
sequenceId = info[1].As<Napi::Number>().Int32Value();
|
|
1136
|
+
|
|
1137
|
+
AddonContextSequenceCheckpointInitWorker* worker = new AddonContextSequenceCheckpointInitWorker(info, this, context);
|
|
1138
|
+
worker->Queue();
|
|
1139
|
+
return worker->GetPromise();
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
Napi::Value AddonContextSequenceCheckpoint::Dispose(const Napi::CallbackInfo& info) {
|
|
1143
|
+
dispose();
|
|
1144
|
+
return info.Env().Undefined();
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
void AddonContextSequenceCheckpoint::dispose() {
|
|
1148
|
+
std::lock_guard<std::mutex> lock(dataMutex);
|
|
1149
|
+
data.clear();
|
|
1150
|
+
data.resize(0);
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
Napi::Value AddonContextSequenceCheckpoint::GetSize(const Napi::CallbackInfo& info) {
|
|
1154
|
+
return Napi::Number::New(info.Env(), data.size());
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
Napi::Value AddonContextSequenceCheckpoint::GetMinPos(const Napi::CallbackInfo& info) {
|
|
1158
|
+
return Napi::Number::New(info.Env(), minPos);
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
Napi::Value AddonContextSequenceCheckpoint::GetMaxPos(const Napi::CallbackInfo& info) {
|
|
1162
|
+
return Napi::Number::New(info.Env(), maxPos);
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
void AddonContextSequenceCheckpoint::init(Napi::Object exports) {
|
|
1166
|
+
exports.Set(
|
|
1167
|
+
"AddonContextSequenceCheckpoint",
|
|
1168
|
+
DefineClass(
|
|
1169
|
+
exports.Env(),
|
|
1170
|
+
"AddonContextSequenceCheckpoint",
|
|
1171
|
+
{
|
|
1172
|
+
InstanceMethod("init", &AddonContextSequenceCheckpoint::Init),
|
|
1173
|
+
InstanceMethod("dispose", &AddonContextSequenceCheckpoint::Dispose),
|
|
1174
|
+
|
|
1175
|
+
InstanceAccessor("size", &AddonContextSequenceCheckpoint::GetSize, nullptr),
|
|
1176
|
+
InstanceAccessor("minPos", &AddonContextSequenceCheckpoint::GetMinPos, nullptr),
|
|
1177
|
+
InstanceAccessor("maxPos", &AddonContextSequenceCheckpoint::GetMaxPos, nullptr),
|
|
1178
|
+
}
|
|
1179
|
+
)
|
|
1180
|
+
);
|
|
1181
|
+
}
|