Akarshan Biswas 11b3a60675
fix: refactor, fix and move gguf support utilities to backend (#6584)
* feat: move estimateKVCacheSize to BE

* feat: Migrate model planning to backend

This commit migrates the model load planning logic from the frontend to the Tauri backend. This refactors the `planModelLoad` and `isModelSupported` methods into the `tauri-plugin-llamacpp` plugin, making them directly callable from the Rust core.

The model planning now incorporates a more robust and accurate memory estimation, considering both VRAM and system RAM, and introduces a `batch_size` parameter to the model plan.

**Key changes:**

- **Moved `planModelLoad` to `tauri-plugin-llamacpp`:** The core logic for determining GPU layers, context length, and memory offloading is now in Rust for better performance and accuracy.
- **Moved `isModelSupported` to `tauri-plugin-llamacpp`:** The model support check is also now handled by the backend.
- **Removed `getChatClient` from `AIEngine`:** This optional method was not implemented and has been removed from the abstract class.
- **Improved KV Cache estimation:** The `estimate_kv_cache_internal` function in Rust now accounts for `attention.key_length` and `attention.value_length` if available, and considers sliding window attention for more precise estimates.
- **Introduced `batch_size` in ModelPlan:** The model plan now includes a `batch_size` property, which will be automatically adjusted based on the determined `ModelMode` (e.g., lower for CPU/Hybrid modes).
- **Updated `llamacpp-extension`:** The frontend extension now calls the new Tauri commands for model planning and support checks.
- **Removed `batch_size` from `llamacpp-extension/settings.json`:** The batch size is now dynamically determined by the planning logic and will be set as a model setting directly.
- **Updated `ModelSetting` and `useModelProvider` hooks:** These now handle the new `batch_size` property in model settings.
- **Added new Tauri commands and permissions:** `get_model_size`, `is_model_supported`, and `plan_model_load` are new commands with corresponding permissions.
- **Consolidated `ModelSupportStatus` and `KVCacheEstimate`:** These types are now defined in `src/tauri/plugins/tauri-plugin-llamacpp/src/gguf/types.rs`.

This refactoring centralizes critical model resource management logic, improving consistency and maintainability, and lays the groundwork for more sophisticated model loading strategies.

* feat: refine model planner to handle more memory scenarios

This commit introduces several improvements to the `plan_model_load` function, enhancing its ability to determine a suitable model loading strategy based on system memory constraints. Specifically, it includes:

-   **VRAM calculation improvements:**  Corrects the calculation of total VRAM by iterating over GPUs and multiplying by 1024*1024, improving accuracy.
-   **Hybrid plan optimization:**  Implements a more robust hybrid plan strategy, iterating through GPU layer configurations to find the highest possible GPU usage while remaining within VRAM limits.
-   **Minimum context length enforcement:** Enforces a minimum context length for the model, ensuring that the model can be loaded and used effectively.
-   **Fallback to CPU mode:** If a hybrid plan isn't feasible, it now correctly falls back to a CPU-only mode.
-   **Improved logging:** Enhanced logging to provide more detailed information about the memory planning process, including VRAM, RAM, and GPU layers.
-   **Batch size adjustment:** Updated batch size based on the selected mode, ensuring efficient utilization of available resources.
-   **Error handling and edge cases:**  Improved error handling and edge case management to prevent unexpected failures.
-   **Constants:** Added constants for easier maintenance and understanding.
-   **Power-of-2 adjustment:** Added power of 2 adjustment for max context length to ensure correct sizing for the LLM.

These changes improve the reliability and robustness of the model planning process, allowing it to handle a wider range of hardware configurations and model sizes.

* Add log for raw GPU info from tauri-plugin-hardware

* chore: update linux runner for tauri build

* feat: Improve GPU memory calculation for unified memory

This commit improves the logic for calculating usable VRAM, particularly for systems with **unified memory** like Apple Silicon. Previously, the application would report 0 total VRAM if no dedicated GPUs were found, leading to incorrect calculations and failed model loads.

This change modifies the VRAM calculation to fall back to the total system RAM if no discrete GPUs are detected. This is a common and correct approach for unified memory architectures, where the CPU and GPU share the same memory pool.

Additionally, this commit refactors the logic for calculating usable VRAM and RAM to prevent potential underflow by checking if the total memory is greater than the reserved bytes before subtracting. This ensures the calculation remains safe and correct.

* chore: fix update migration version

* fix: enable unified memory support on model support indicator

* Use total_system_memory in bytes

---------

Co-authored-by: Minh141120 <minh.itptit@gmail.com>
Co-authored-by: Faisal Amir <urmauur@gmail.com>
2025-09-25 12:17:57 +05:30

356 lines
9.5 KiB
JSON

[
{
"key": "version_backend",
"title": "Version & Backend",
"description": "Version and Backend for llama.cpp",
"controllerType": "dropdown",
"controllerProps": {
"value": "none",
"options": [],
"recommended": ""
}
},
{
"key": "llamacpp_env",
"title": "Environmental variables",
"description": "Environmental variables for llama.cpp(KEY=VALUE), separated by ';'",
"controllerType": "input",
"controllerProps": {
"value": "",
"placeholder": "Eg. GGML_VK_VISIBLE_DEVICES=0,1",
"type": "text",
"textAlign": "right"
}
},
{
"key": "auto_update_engine",
"title": "Auto update engine",
"description": "Automatically update llamacpp engine to latest version",
"controllerType": "checkbox",
"controllerProps": { "value": true }
},
{
"key": "auto_unload",
"title": "Auto-Unload Old Models",
"description": "Automatically unloads models that are not in use to free up memory. Ensure only one model is loaded at a time.",
"controllerType": "checkbox",
"controllerProps": { "value": true }
},
{
"key": "memory_util",
"title": "Smart Memory utilization",
"description": "Smart memory utilization mode for running local GGUF models",
"controllerType": "dropdown",
"controllerProps": {
"value": "high",
"options": [
{ "value": "high", "name": "High" },
{ "value": "medium", "name": "Medium" },
{ "value": "low", "name": "Low" }
],
"recommended": "high"
}
},
{
"key": "threads",
"title": "Threads",
"description": "Number of threads to use during generation (-1 for logical cores).",
"controllerType": "input",
"controllerProps": {
"value": -1,
"placeholder": "-1",
"type": "number",
"textAlign": "right"
}
},
{
"key": "threads_batch",
"title": "Threads (Batch)",
"description": "Number of threads for batch and prompt processing (default: same as Threads).",
"controllerType": "input",
"controllerProps": {
"value": -1,
"placeholder": "-1 (same as Threads)",
"type": "number",
"textAlign": "right"
}
},
{
"key": "ctx_shift",
"title": "Context Shift",
"description": "Allow model to cut text in the beginning to accommodate new text in its memory",
"controllerType": "checkbox",
"controllerProps": {
"value": false
}
},
{
"key": "n_predict",
"title": "Max Tokens to Predict",
"description": "Maximum number of tokens to generate (-1 = infinity).",
"controllerType": "input",
"controllerProps": {
"value": -1,
"placeholder": "-1",
"type": "number",
"textAlign": "right"
}
},
{
"key": "ubatch_size",
"title": "uBatch Size",
"description": "Physical maximum batch size for processing prompts.",
"controllerType": "input",
"controllerProps": {
"value": 512,
"placeholder": "512",
"type": "number",
"textAlign": "right"
}
},
{
"key": "device",
"title": "Devices for Offload",
"description": "Comma-separated list of devices to use for offloading (e.g., 'CUDA0', 'CUDA0,CUDA1'). Leave empty to use default/CPU only.",
"controllerType": "input",
"controllerProps": {
"value": "",
"placeholder": "CUDA0",
"type": "text"
}
},
{
"key": "split_mode",
"title": "GPU Split Mode",
"description": "How to split the model across multiple GPUs.",
"controllerType": "dropdown",
"controllerProps": {
"value": "layer",
"options": [
{ "value": "none", "name": "None" },
{ "value": "layer", "name": "Layer" },
{ "value": "row", "name": "Row" }
]
}
},
{
"key": "main_gpu",
"title": "Main GPU Index",
"description": "The GPU to use for the model (split-mode=none) or intermediate results (split-mode=row).",
"controllerType": "input",
"controllerProps": {
"value": 0,
"placeholder": "0",
"type": "number",
"textAlign": "right"
}
},
{
"key": "flash_attn",
"title": "Flash Attention",
"description": "Enable Flash Attention for optimized performance.",
"controllerType": "checkbox",
"controllerProps": {
"value": false
}
},
{
"key": "cont_batching",
"title": "Continuous Batching",
"description": "Enable continuous batching (a.k.a dynamic batching) for concurrent requests.",
"controllerType": "checkbox",
"controllerProps": {
"value": false
}
},
{
"key": "no_mmap",
"title": "Disable mmap",
"description": "Do not memory-map model (slower load but may reduce pageouts if not using mlock).",
"controllerType": "checkbox",
"controllerProps": {
"value": false
}
},
{
"key": "mlock",
"title": "MLock",
"description": "Force system to keep model in RAM, preventing swapping/compression.",
"controllerType": "checkbox",
"controllerProps": {
"value": false
}
},
{
"key": "cache_type_k",
"title": "KV Cache K Type",
"description": "KV cache data type for Keys (default: f16).",
"controllerType": "dropdown",
"controllerProps": {
"value": "f16",
"options": [
{ "value": "f32", "name": "f32" },
{ "value": "f16", "name": "f16" },
{ "value": "bf16", "name": "bf16" },
{ "value": "q8_0", "name": "q8_0" },
{ "value": "q4_0", "name": "q4_0" },
{ "value": "q4_1", "name": "q4_1" },
{ "value": "iq4_nl", "name": "iq4_nl" },
{ "value": "q5_0", "name": "q5_0" },
{ "value": "q5_1", "name": "q5_1" }
]
}
},
{
"key": "cache_type_v",
"title": "KV Cache V Type",
"description": "KV cache data type for Values (default: f16).",
"controllerType": "dropdown",
"controllerProps": {
"value": "f16",
"options": [
{ "value": "f32", "name": "f32" },
{ "value": "f16", "name": "f16" },
{ "value": "bf16", "name": "bf16" },
{ "value": "q8_0", "name": "q8_0" },
{ "value": "q4_0", "name": "q4_0" },
{ "value": "q4_1", "name": "q4_1" },
{ "value": "iq4_nl", "name": "iq4_nl" },
{ "value": "q5_0", "name": "q5_0" },
{ "value": "q5_1", "name": "q5_1" }
]
}
},
{
"key": "defrag_thold",
"title": "KV Cache Defragmentation Threshold",
"description": "Threshold for KV cache defragmentation (< 0 to disable).",
"controllerType": "input",
"controllerProps": {
"value": 0.1,
"placeholder": "0.1",
"type": "number",
"textAlign": "right",
"step": 0.01
}
},
{
"key": "rope_scaling",
"title": "RoPE Scaling Method",
"description": "RoPE frequency scaling method.",
"controllerType": "dropdown",
"controllerProps": {
"value": "none",
"options": [
{ "value": "none", "name": "None" },
{ "value": "linear", "name": "Linear" },
{ "value": "yarn", "name": "YaRN" }
]
}
},
{
"key": "rope_scale",
"title": "RoPE Scale Factor",
"description": "RoPE context scaling factor.",
"controllerType": "input",
"controllerProps": {
"value": 1.0,
"placeholder": "1.0",
"type": "number",
"textAlign": "right",
"min": 0,
"step": 0.01
}
},
{
"key": "rope_freq_base",
"title": "RoPE Frequency Base",
"description": "RoPE base frequency (0 = loaded from model).",
"controllerType": "input",
"controllerProps": {
"value": 0,
"placeholder": "0 (model default)",
"type": "number",
"textAlign": "right"
}
},
{
"key": "rope_freq_scale",
"title": "RoPE Frequency Scale Factor",
"description": "RoPE frequency scaling factor.",
"controllerType": "input",
"controllerProps": {
"value": 1.0,
"placeholder": "1.0",
"type": "number",
"textAlign": "right",
"min": 0,
"step": 0.01
}
},
{
"key": "mirostat",
"title": "Mirostat Mode",
"description": "Use Mirostat sampling (0: disabled, 1: Mirostat V1, 2: Mirostat V2).",
"controllerType": "dropdown",
"controllerProps": {
"value": 0,
"options": [
{ "value": 0, "name": "Disabled" },
{ "value": 1, "name": "Mirostat V1" },
{ "value": 2, "name": "Mirostat V2" }
]
}
},
{
"key": "mirostat_lr",
"title": "Mirostat Learning Rate",
"description": "Mirostat learning rate (eta).",
"controllerType": "input",
"controllerProps": {
"value": 0.1,
"placeholder": "0.1",
"type": "number",
"textAlign": "right",
"min": 0,
"step": 0.01
}
},
{
"key": "mirostat_ent",
"title": "Mirostat Target Entropy",
"description": "Mirostat target entropy (tau).",
"controllerType": "input",
"controllerProps": {
"value": 5.0,
"placeholder": "5.0",
"type": "number",
"textAlign": "right",
"min": 0,
"step": 0.01
}
},
{
"key": "grammar_file",
"title": "Grammar File",
"description": "Path to a BNF-like grammar file to constrain generations.",
"controllerType": "input",
"controllerProps": {
"value": "",
"placeholder": "path/to/grammar.gbnf",
"type": "text"
}
},
{
"key": "json_schema_file",
"title": "JSON Schema File",
"description": "Path to a JSON schema file to constrain generations.",
"controllerType": "input",
"controllerProps": {
"value": "",
"placeholder": "path/to/schema.json",
"type": "text"
}
}
]