-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C#] Expose Adapters API and add tests (#998)
- Loading branch information
1 parent
28f77a3
commit 147a311
Showing
14 changed files
with
348 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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,62 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Microsoft.ML.OnnxRuntimeGenAI | ||
{ | ||
/// <summary> | ||
/// A container of adapters. | ||
/// </summary> | ||
public class Adapters : SafeHandle | ||
{ | ||
/// <summary> | ||
/// Creates a container for adapters | ||
/// used to load, unload and hold them. | ||
/// Throws on error. | ||
/// </summary> | ||
/// <param name="model">Reference to a loaded model</param> | ||
/// <returns>new Adapters object</returns> | ||
public Adapters(Model model) : base(IntPtr.Zero, true) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaCreateAdapters(model.Handle, out handle)); | ||
} | ||
|
||
/// <summary> | ||
/// Method that loads adapter data and assigns it a nmae that | ||
/// it can be referred to. Throws on error. | ||
/// </summary> | ||
/// <param name="adapterPath">file path to load</param> | ||
/// <param name="adapterName">adapter name</param> | ||
public void LoadAdapter(string adapterPath, string adapterName) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaLoadAdapter(handle, | ||
StringUtils.ToUtf8(adapterPath), StringUtils.ToUtf8(adapterName))); | ||
} | ||
|
||
/// <summary> | ||
/// Unload the adatper that was loaded by the LoadAdapter method. | ||
/// Throws on error. | ||
/// </summary> | ||
/// <param name="adapterName"></param> | ||
public void UnloadAdapter(string adapterName) | ||
{ | ||
Result.VerifySuccess(NativeMethods.OgaUnloadAdapter(handle, StringUtils.ToUtf8(adapterName))); | ||
} | ||
|
||
internal IntPtr Handle { get { return handle; } } | ||
|
||
/// <summary> | ||
/// Implement SafeHandle override | ||
/// </summary> | ||
public override bool IsInvalid => handle == IntPtr.Zero; | ||
|
||
protected override bool ReleaseHandle() | ||
{ | ||
NativeMethods.OgaDestroyAdapters(handle); | ||
handle = IntPtr.Zero; | ||
return true; | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 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.