Skip to content

Commit

Permalink
csharp: update C# bindings to work with GGUF (#1651)
Browse files Browse the repository at this point in the history
  • Loading branch information
cebtenzzre authored Jan 16, 2024
1 parent f856439 commit 03a9f0b
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 57 deletions.
4 changes: 3 additions & 1 deletion gpt4all-bindings/csharp/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ insert_final_newline = true

# IDE0055: Fix formatting
dotnet_diagnostic.IDE0055.severity = error
dotnet_diagnostic.CS1573.severity = suggestion
dotnet_diagnostic.CS1591.severity = suggestion

# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = true
Expand Down Expand Up @@ -343,4 +345,4 @@ dotnet_diagnostic.IDE2004.severity = warning
[src/{VisualStudio}/**/*.{cs,vb}]
# CA1822: Make member static
# There is a risk of accidentally breaking an internal API that partners rely on though IVT.
dotnet_code_quality.CA1822.api_surface = private
dotnet_code_quality.CA1822.api_surface = private
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions gpt4all-bindings/csharp/Gpt4All.Tests/Gpt4All.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 0 additions & 2 deletions gpt4all-bindings/csharp/Gpt4All/Bindings/ILLModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
/// </summary>
public interface ILLModel : IDisposable
{
ModelType ModelType { get; }

ulong GetStateSizeBytes();

int GetThreadCount();
Expand Down
18 changes: 4 additions & 14 deletions gpt4all-bindings/csharp/Gpt4All/Bindings/LLModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,22 @@ public record ModelRecalculatingEventArgs(bool IsRecalculating);
public class LLModel : ILLModel
{
protected readonly IntPtr _handle;
private readonly ModelType _modelType;
private readonly ILogger _logger;
private bool _disposed;

public ModelType ModelType => _modelType;

internal LLModel(IntPtr handle, ModelType modelType, ILogger? logger = null)
internal LLModel(IntPtr handle, ILogger? logger = null)
{
_handle = handle;
_modelType = modelType;
_logger = logger ?? NullLogger.Instance;
}

/// <summary>
/// Create a new model from a pointer
/// </summary>
/// <param name="handle">Pointer to underlying model</param>
/// <param name="modelType">The model type</param>
public static LLModel Create(IntPtr handle, ModelType modelType, ILogger? logger = null)
public static LLModel Create(IntPtr handle, ILogger? logger = null)
{
return new LLModel(handle, modelType, logger: logger);
return new LLModel(handle, logger: logger);
}

/// <summary>
Expand Down Expand Up @@ -204,12 +199,7 @@ protected virtual void Dispose(bool disposing)
// dispose managed state
}

switch (_modelType)
{
default:
Destroy();
break;
}
Destroy();

_disposed = true;
}
Expand Down
1 change: 1 addition & 0 deletions gpt4all-bindings/csharp/Gpt4All/Gpt4All.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<!-- Windows -->
Expand Down
10 changes: 7 additions & 3 deletions gpt4all-bindings/csharp/Gpt4All/Model/Gpt4AllModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;
using Gpt4All.Bindings;
using Gpt4All.LibraryLoader;
using System.Runtime.InteropServices;

namespace Gpt4All;

Expand Down Expand Up @@ -33,10 +34,13 @@ public Gpt4AllModelFactory(string? libraryPath = default, bool bypassLoading = t

private IGpt4AllModel CreateModel(string modelPath)
{
var modelType_ = ModelFileUtils.GetModelTypeFromModelFileHeader(modelPath);
_logger.LogInformation("Creating model path={ModelPath} type={ModelType}", modelPath, modelType_);
_logger.LogInformation("Creating model path={ModelPath}", modelPath);
IntPtr error;
var handle = NativeMethods.llmodel_model_create2(modelPath, "auto", out error);
if (error != IntPtr.Zero)
{
throw new Exception(Marshal.PtrToStringAnsi(error));
}
_logger.LogDebug("Model created handle=0x{ModelHandle:X8}", handle);
_logger.LogInformation("Model loading started");
var loadedSuccessfully = NativeMethods.llmodel_loadModel(handle, modelPath, 2048);
Expand All @@ -47,7 +51,7 @@ private IGpt4AllModel CreateModel(string modelPath)
}

var logger = _loggerFactory.CreateLogger<LLModel>();
var underlyingModel = LLModel.Create(handle, modelType_, logger: logger);
var underlyingModel = LLModel.Create(handle, logger: logger);

Debug.Assert(underlyingModel.IsLoaded());

Expand Down
24 changes: 0 additions & 24 deletions gpt4all-bindings/csharp/Gpt4All/Model/ModelFileUtils.cs

This file was deleted.

2 changes: 0 additions & 2 deletions gpt4all-bindings/csharp/Gpt4All/Model/ModelOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
public record ModelOptions
{
public int Threads { get; init; } = 4;

public ModelType ModelType { get; init; } = ModelType.GPTJ;
}
11 changes: 0 additions & 11 deletions gpt4all-bindings/csharp/Gpt4All/Model/ModelType.cs

This file was deleted.

0 comments on commit 03a9f0b

Please sign in to comment.