Skip to content

Commit

Permalink
- Added GroupDisposable to dispose a collection of items all together
Browse files Browse the repository at this point in the history
 - Renamed `LLamaModelKvOverride` to `LLamaModelMetadataOverride`
  • Loading branch information
martindevans committed Dec 14, 2023
1 parent c21da98 commit bc095e6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 29 deletions.
65 changes: 39 additions & 26 deletions LLama/Extensions/IModelParamsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,51 @@
using LLama.Abstractions;
using LLama.Native;

namespace LLama.Extensions
namespace LLama.Extensions;

/// <summary>
/// Extention methods to the IModelParams interface
/// </summary>
public static class IModelParamsExtensions
{
/// <summary>
/// Extention methods to the IModelParams interface
/// Convert the given `IModelParams` into a `LLamaModelParams`
/// </summary>
public static class IModelParamsExtensions
/// <param name="params"></param>
/// <param name="result"></param>
/// <returns></returns>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="ArgumentException"></exception>
public static IDisposable ToLlamaModelParams(this IModelParams @params, out LLamaModelParams result)
{
/// <summary>
/// Convert the given `IModelParams` into a `LLamaModelParams`
/// </summary>
/// <param name="params"></param>
/// <param name="result"></param>
/// <returns></returns>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="ArgumentException"></exception>
public static MemoryHandle ToLlamaModelParams(this IModelParams @params, out LLamaModelParams result)
{
result = NativeApi.llama_model_default_params();
var disposer = new GroupDisposable();

result.main_gpu = @params.MainGpu;
result.n_gpu_layers = @params.GpuLayerCount;
result.use_mlock = @params.UseMemoryLock;
result.use_mmap = @params.UseMemorymap;
result.vocab_only = @params.VocabOnly;
result = NativeApi.llama_model_default_params();

var pin = @params.TensorSplits.Pin();
unsafe
{
result.tensor_split = (float*)pin.Pointer;
}
result.main_gpu = @params.MainGpu;
result.n_gpu_layers = @params.GpuLayerCount;
result.use_mlock = @params.UseMemoryLock;
result.use_mmap = @params.UseMemorymap;
result.vocab_only = @params.VocabOnly;

return pin;
unsafe
{
result.tensor_split = (float*)disposer.Add(@params.TensorSplits.Pin()).Pointer;
}

//todo: MetadataOverrides
//if (@params.MetadataOverrides.Count == 0)
//{
// unsafe
// {
// result.kv_overrides = (LLamaModelMetadataOverride*)IntPtr.Zero;
// }
//}
//else
//{
// throw new NotImplementedException("MetadataOverrides");
//}

return disposer;
}
}
}
57 changes: 57 additions & 0 deletions LLama/Native/GroupDisposable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Buffers;
using System.Collections.Generic;

namespace LLama.Native;

/// <summary>
/// Disposes all contained disposables when this class is disposed
/// </summary>
internal sealed class GroupDisposable
: IDisposable
{
private bool _disposed;

private readonly List<MemoryHandle> _handles = new();
private readonly List<IDisposable> _disposables = new();

/// <inheritdoc />
~GroupDisposable()
{
Dispose();
}

public MemoryHandle Add(MemoryHandle handle)
{
if (_disposed)
throw new ObjectDisposedException("Cannot add new handle, already disposed");
_handles.Add(handle);

return handle;
}

public T Add<T>(T disposable)
where T : class, IDisposable
{
if (_disposed)
throw new ObjectDisposedException("Cannot add new IDisposable, already disposed");
_disposables.Add(disposable);

return disposable;
}

/// <inheritdoc />
public void Dispose()
{
if (_disposed)
return;

foreach (var memoryHandle in _handles)
memoryHandle.Dispose();
foreach (var disposable in _disposables)
disposable.Dispose();

_disposed = true;
GC.SuppressFinalize(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace LLama.Native;

/// <summary>
/// Override a key/value pair in the llama model metadata
/// Override a key/value pair in the llama model metadata (llama_model_kv_override)
/// </summary>
[StructLayout(LayoutKind.Explicit)]
public unsafe struct LLamaModelKvOverride
public unsafe struct LLamaModelMetadataOverride
{
/// <summary>
/// Key to override
Expand Down
2 changes: 1 addition & 1 deletion LLama/Native/LLamaModelParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public unsafe struct LLamaModelParams
/// <summary>
/// override key-value pairs of the model meta data
/// </summary>
public LLamaModelKvOverride* kv_overrides;
public LLamaModelMetadataOverride* kv_overrides;

/// <summary>
/// only load the vocabulary, no weights
Expand Down

0 comments on commit bc095e6

Please sign in to comment.