// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.ML.OnnxRuntime
{
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
///
/// Class to that stores information about the file location where an "external" initializer is stored.
///
///
public class OrtExternalInitializerInfo : SafeHandle, IReadOnlyExternalInitializerInfo
{
// Set to false when constructed with an externally managed constant handle owned by ORT.
private readonly bool _ownsHandle = true;
///
/// Create a new OrtExternalInitializerInfo instance.
///
/// The path to the file that stores the initializer data.
/// The byte offset in the file where the data is stored.
/// The size of the data (in bytes) within the file.
public OrtExternalInitializerInfo(string filePath, long fileOffset, long byteSize)
: base(IntPtr.Zero, ownsHandle: true)
{
var platformFilePath = NativeOnnxValueHelper.GetPlatformSerializedString(filePath);
NativeApiStatus.VerifySuccess(
NativeMethods.OrtCreateExternalInitializerInfo(platformFilePath, fileOffset, (UIntPtr)byteSize, out handle));
_ownsHandle = true;
}
///
/// Create a new OrtExternalInitializerInfo instance from an existing native OrtExternalInitializerInfo handle.
///
/// Native OrtExternalInitializerInfo handle.
/// True if the OrtExternalInitializerInfo instance owns the native handle.
/// Defaults to false.
internal OrtExternalInitializerInfo(IntPtr constHandle, bool ownsHandle = false)
: base(IntPtr.Zero, ownsHandle)
{
Debug.Assert(constHandle != IntPtr.Zero);
SetHandle(constHandle);
_ownsHandle = ownsHandle;
}
///
/// Get the file path to the file that store's the initializer's data.
///
///
/// The path is relative to the filesystem directory where the ONNX model was stored.
///
/// The file path.
public string GetFilePath()
{
IntPtr filePathPtr = NativeMethods.OrtExternalInitializerInfo_GetFilePath(handle);
if (filePathPtr == IntPtr.Zero)
{
return string.Empty;
}
return NativeOnnxValueHelper.StringFromNativePathString(filePathPtr);
}
///
/// Get the byte offset within the file where the initializer's data is stored.
///
/// The file offset location.
public long GetFileOffset()
{
return NativeMethods.OrtExternalInitializerInfo_GetFileOffset(handle);
}
///
/// Get the size in bytes of the initializer's data within the file.
///
/// The size in bytes of the initializer data.
public long GetByteSize()
{
UIntPtr byteSize = NativeMethods.OrtExternalInitializerInfo_GetByteSize(handle);
return checked((long)byteSize);
}
///
/// Indicates whether the native handle is invalid.
///
public override bool IsInvalid { get { return handle == IntPtr.Zero; } }
///
/// Release the native instance of OrtExternalInitializerInfo if we own it.
///
/// true on success and false on error.
protected override bool ReleaseHandle()
{
if (!_ownsHandle)
{
// Return false to indicate an error.
// ReleaseHandle() should not be called on a const handle that this class does not own.
return false;
}
NativeMethods.OrtReleaseExternalInitializerInfo(handle);
handle = IntPtr.Zero;
return true;
}
}
///
/// Interface for all readonly methods implemented by OrtExternalInitializerInfo.
///
public interface IReadOnlyExternalInitializerInfo
{
///
/// Get the file path to the file that store's the initializer's data.
///
///
/// The path is relative to the filesystem directory where the ONNX model was stored.
///
/// The file path.
string GetFilePath();
///
/// Get the byte offset within the file where the initializer's data is stored.
///
/// The file offset location.
long GetFileOffset();
///
/// Get the size in bytes of the initializer's data within the file.
///
/// The size in bytes of the initializer data.
long GetByteSize();
}
}