-
Notifications
You must be signed in to change notification settings - Fork 497
Feat/tensor override #1180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feat/tensor override #1180
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8dc45de
Support override-tensors parameter
dpmm99 1ac27e2
Suppress 'ot' in _typos.toml
dpmm99 d905f9d
Merge branch 'master' into feat/tensor-override
martindevans 2defa8b
Make helper for -ot parameters internal
dpmm99 3c8d239
Fix ModelParams serialization test
dpmm99 f4213a2
Switch the tensor override helper to a local variable
dpmm99 d2607ac
Use unsafe type directly for -ot field in LLamaModelParams
dpmm99 eeb8c8f
Use unsafe type directly for -ot field in LLamaModelParams, part 2
dpmm99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| using System; | ||
|
|
||
| namespace LLama.Abstractions | ||
| { | ||
| /// <summary> | ||
| /// Represents a mapping between a tensor name pattern and a specific buffer type | ||
| /// </summary> | ||
| public class TensorBufferOverride | ||
| { | ||
| /// <summary> | ||
| /// Pattern to match tensor names. This is a regular expression. You can check the tensor names via the model.Metadata. | ||
| /// </summary> | ||
| public string Pattern { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Buffer type to use for matching tensors. Examples: CPU, GPU0, GPU1 | ||
| /// </summary> | ||
| public string BufferType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new tensor buffer override | ||
| /// </summary> | ||
| /// <param name="pattern">Pattern to match tensor names</param> | ||
| /// <param name="bufferType">Buffer type to use for matching tensors</param> | ||
| public TensorBufferOverride(string pattern, string bufferType) | ||
| { | ||
| if (string.IsNullOrEmpty(pattern)) | ||
| throw new ArgumentException("Pattern cannot be null or empty", nameof(pattern)); | ||
| if (string.IsNullOrEmpty(bufferType)) | ||
| throw new ArgumentException("Buffer type cannot be null or empty", nameof(bufferType)); | ||
|
|
||
| Pattern = pattern; | ||
| BufferType = bufferType; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using System; | ||
|
|
||
| namespace LLama.Native | ||
| { | ||
| /// <summary> | ||
| /// Represents a mapping between a tensor name pattern and a backend buffer type<br/> | ||
| /// Original type: llama_model_tensor_buft_override | ||
| /// </summary> | ||
| [StructLayout(LayoutKind.Sequential)] | ||
| public struct LLamaModelTensorBufferOverride | ||
| { | ||
| /// <summary> | ||
| /// Tensor name pattern to match | ||
| /// </summary> | ||
| public IntPtr Pattern; | ||
|
|
||
| /// <summary> | ||
| /// Backend buffer type to use for matching tensors, as obtained via ggml_backend_dev_buffer_type | ||
| /// </summary> | ||
| public IntPtr BufferType; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace LLama.Native | ||
| { | ||
| /// <summary> | ||
| /// Helper for creating and managing tensor buffer overrides | ||
| /// </summary> | ||
| internal class LLamaTensorBufferOverrideHelper : IDisposable | ||
| { | ||
| private readonly List<IntPtr> _allocatedMemory = new(); | ||
| private readonly List<LLamaModelTensorBufferOverride> _overrides = new(); | ||
| private IntPtr _overrideArray = IntPtr.Zero; | ||
| private readonly Dictionary<string, IntPtr> _bufferTypeCache = new(); | ||
|
|
||
| /// <summary> | ||
| /// Get all available buffer types | ||
| /// </summary> | ||
| /// <returns>Dictionary mapping buffer type names to their handles</returns> | ||
| public Dictionary<string, IntPtr> GetAvailableBufferTypes() | ||
| { | ||
| var result = new Dictionary<string, IntPtr>(); | ||
|
|
||
| nuint count = NativeApi.ggml_backend_dev_count(); | ||
| for (nuint i = 0; i < count; i++) | ||
| { | ||
| IntPtr dev = NativeApi.ggml_backend_dev_get(i); | ||
| IntPtr buft = NativeApi.ggml_backend_dev_buffer_type(dev); | ||
|
|
||
| if (buft != IntPtr.Zero) | ||
| { | ||
| IntPtr namePtr = NativeApi.ggml_backend_buft_name(buft); | ||
| string name = Marshal.PtrToStringAnsi(namePtr) ?? string.Empty; | ||
|
|
||
| if (!string.IsNullOrEmpty(name)) | ||
| { | ||
| result[name] = buft; | ||
| _bufferTypeCache[name] = buft; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Add a tensor buffer override | ||
| /// </summary> | ||
| /// <param name="pattern">Tensor name pattern to match</param> | ||
| /// <param name="bufferTypeName">Name of the buffer type to use</param> | ||
| /// <returns>True if the override was added successfully</returns> | ||
| public bool AddOverride(string pattern, string bufferTypeName) | ||
| { | ||
| if (string.IsNullOrEmpty(pattern) || string.IsNullOrEmpty(bufferTypeName)) | ||
| return false; | ||
|
|
||
| // Get all buffer types if cache is empty | ||
| if (_bufferTypeCache.Count == 0) | ||
| { | ||
| GetAvailableBufferTypes(); | ||
| } | ||
|
|
||
| // Check if we have this buffer type | ||
| if (!_bufferTypeCache.TryGetValue(bufferTypeName, out IntPtr bufferType)) | ||
| return false; | ||
|
|
||
| // Allocate memory for the pattern string and keep track of it | ||
| byte[] patternBytes = Encoding.UTF8.GetBytes(pattern + "\0"); | ||
| IntPtr patternPtr = Marshal.AllocHGlobal(patternBytes.Length); | ||
| Marshal.Copy(patternBytes, 0, patternPtr, patternBytes.Length); | ||
| _allocatedMemory.Add(patternPtr); | ||
|
|
||
| // Create the override | ||
| var @override = new LLamaModelTensorBufferOverride | ||
| { | ||
| Pattern = patternPtr, | ||
| BufferType = bufferType | ||
| }; | ||
|
|
||
| _overrides.Add(@override); | ||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Apply the overrides to model parameters | ||
| /// </summary> | ||
| /// <param name="modelParams">Model parameters to update</param> | ||
| public unsafe void ApplyToModelParams(ref LLamaModelParams modelParams) | ||
| { | ||
| if (_overrides.Count == 0) | ||
| { | ||
| modelParams.tensor_buft_overrides = IntPtr.Zero; | ||
| return; | ||
| } | ||
|
|
||
| // Free previous array if it exists | ||
| if (_overrideArray != IntPtr.Zero) | ||
| { | ||
| Marshal.FreeHGlobal(_overrideArray); | ||
| } | ||
|
|
||
| // Allocate memory for the array + null terminator | ||
| int size = Marshal.SizeOf<LLamaModelTensorBufferOverride>() * (_overrides.Count + 1); | ||
| _overrideArray = Marshal.AllocHGlobal(size); | ||
| _allocatedMemory.Add(_overrideArray); | ||
|
|
||
| // Copy overrides to array | ||
| for (int i = 0; i < _overrides.Count; i++) | ||
| { | ||
| IntPtr elemPtr = IntPtr.Add(_overrideArray, i * Marshal.SizeOf<LLamaModelTensorBufferOverride>()); | ||
| Marshal.StructureToPtr(_overrides[i], elemPtr, false); | ||
| } | ||
|
|
||
| // Add null terminator | ||
| IntPtr nullTermPtr = IntPtr.Add(_overrideArray, _overrides.Count * Marshal.SizeOf<LLamaModelTensorBufferOverride>()); | ||
| Marshal.StructureToPtr(new LLamaModelTensorBufferOverride { Pattern = IntPtr.Zero, BufferType = IntPtr.Zero }, nullTermPtr, false); | ||
|
|
||
| // Update model params | ||
| modelParams.tensor_buft_overrides = _overrideArray; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Dispose() | ||
| { | ||
| foreach (IntPtr ptr in _allocatedMemory) | ||
| { | ||
| Marshal.FreeHGlobal(ptr); | ||
| } | ||
| _allocatedMemory.Clear(); | ||
| _overrides.Clear(); | ||
| _overrideArray = IntPtr.Zero; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.