// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using System; using System.Runtime.InteropServices; namespace Microsoft.ML.OnnxRuntime { /// /// Represents Lora Adapter in memory /// public class OrtLoraAdapter : SafeHandle { /// /// 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. /// /// path to the adapter file /// optional allocator, can be null, must be a device allocator /// New instance of LoraAdapter 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); } /// /// Creates an instance of OrtLoraAdapter from an array of bytes. The API /// makes a copy of the bytes internally. /// /// array of bytes containing valid LoraAdapter format /// optional device allocator or null /// new instance of LoraAdapter 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 /// /// Overrides SafeHandle.IsInvalid /// /// returns true if handle is equal to Zero public override bool IsInvalid { get { return handle == IntPtr.Zero; } } /// /// Overrides SafeHandle.ReleaseHandle() to properly dispose of /// the native instance of OrtLoraAdapter /// /// always returns true protected override bool ReleaseHandle() { NativeMethods.ReleaseLoraAdapter(handle); handle = IntPtr.Zero; return true; } #endregion } }