-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C#] Expose Multi-Lora support in C# (#22281)
### Description ### Motivation and Context #22046
- Loading branch information
1 parent
4e15b22
commit 224f065
Showing
7 changed files
with
267 additions
and
5 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
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
81 changes: 81 additions & 0 deletions
81
csharp/src/Microsoft.ML.OnnxRuntime/OrtLoraAdapter.shared.cs
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,81 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Microsoft.ML.OnnxRuntime | ||
{ | ||
/// <summary> | ||
/// Represents Lora Adapter in memory | ||
/// </summary> | ||
public class OrtLoraAdapter : SafeHandle | ||
{ | ||
/// <summary> | ||
/// Creates an instance of OrtLoraAdapter from file. | ||
/// The adapter file is memory mapped. If allocator parameter | ||
/// is provided, then lora parameters are copied to the memory | ||
/// allocated by the specified allocator. | ||
/// </summary> | ||
/// <param name="adapterPath">path to the adapter file</param> | ||
/// <param name="ortAllocator">optional allocator, can be null, must be a device allocator</param> | ||
/// <returns>New instance of LoraAdapter</returns> | ||
public static OrtLoraAdapter Create(string adapterPath, OrtAllocator ortAllocator) | ||
{ | ||
var platformPath = NativeOnnxValueHelper.GetPlatformSerializedString(adapterPath); | ||
var allocatorHandle = (ortAllocator != null) ? ortAllocator.Pointer : IntPtr.Zero; | ||
NativeApiStatus.VerifySuccess(NativeMethods.CreateLoraAdapter(platformPath, allocatorHandle, | ||
out IntPtr adapterHandle)); | ||
return new OrtLoraAdapter(adapterHandle); | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of OrtLoraAdapter from an array of bytes. The API | ||
/// makes a copy of the bytes internally. | ||
/// </summary> | ||
/// <param name="bytes">array of bytes containing valid LoraAdapter format</param> | ||
/// <param name="ortAllocator">optional device allocator or null</param> | ||
/// <returns>new instance of LoraAdapter</returns> | ||
public static OrtLoraAdapter Create(byte[] bytes, OrtAllocator ortAllocator) | ||
{ | ||
var allocatorHandle = (ortAllocator != null) ? ortAllocator.Pointer : IntPtr.Zero; | ||
NativeApiStatus.VerifySuccess(NativeMethods.CreateLoraAdapterFromArray(bytes, | ||
new UIntPtr((uint)bytes.Length), allocatorHandle, out IntPtr adapterHandle)); | ||
return new OrtLoraAdapter(adapterHandle); | ||
} | ||
|
||
internal OrtLoraAdapter(IntPtr adapter) | ||
: base(adapter, true) | ||
{ | ||
} | ||
|
||
internal IntPtr Handle | ||
{ | ||
get | ||
{ | ||
return handle; | ||
} | ||
} | ||
|
||
#region SafeHandle | ||
|
||
/// <summary> | ||
/// Overrides SafeHandle.IsInvalid | ||
/// </summary> | ||
/// <value>returns true if handle is equal to Zero</value> | ||
public override bool IsInvalid { get { return handle == IntPtr.Zero; } } | ||
|
||
/// <summary> | ||
/// Overrides SafeHandle.ReleaseHandle() to properly dispose of | ||
/// the native instance of OrtLoraAdapter | ||
/// </summary> | ||
/// <returns>always returns true</returns> | ||
protected override bool ReleaseHandle() | ||
{ | ||
NativeMethods.ReleaseLoraAdapter(handle); | ||
handle = IntPtr.Zero; | ||
return true; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
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