|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Text; |
| 8 | +using size_t = System.UIntPtr; |
| 9 | + |
| 10 | +#pragma warning disable MSML_GeneralName |
| 11 | +#pragma warning disable MSML_ParameterLocalVarName |
| 12 | + |
| 13 | +namespace Microsoft.ML.Transforms.TensorFlow |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// This attribute can be applied to callback functions that will be invoked |
| 17 | + /// from unmanaged code to managed code. |
| 18 | + /// </summary> |
| 19 | + /// <remarks> |
| 20 | + /// <code> |
| 21 | + /// [TensorFlow.MonoPInvokeCallback (typeof (BufferReleaseFunc))] |
| 22 | + /// internal static void MyFreeFunc (IntPtr data, IntPtr length){..} |
| 23 | + /// </code> |
| 24 | + /// </remarks> |
| 25 | + internal sealed class MonoPInvokeCallbackAttribute : Attribute |
| 26 | + { |
| 27 | + /// <summary> |
| 28 | + /// Use this constructor to annotate the type of the callback function that |
| 29 | + /// will be invoked from unmanaged code. |
| 30 | + /// </summary> |
| 31 | + /// <param name="t">T.</param> |
| 32 | + public MonoPInvokeCallbackAttribute(Type t) { } |
| 33 | + } |
| 34 | + |
| 35 | + [StructLayout(LayoutKind.Sequential)] |
| 36 | + internal struct LLBuffer |
| 37 | + { |
| 38 | + internal IntPtr data; |
| 39 | + internal size_t length; |
| 40 | + internal IntPtr data_deallocator; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Holds a block of data, suitable to pass, or retrieve from TensorFlow. |
| 45 | + /// </summary> |
| 46 | + /// <remarks> |
| 47 | + /// <para> |
| 48 | + /// Use the TFBuffer to blobs of data into TensorFlow, or to retrieve blocks |
| 49 | + /// of data out of TensorFlow. |
| 50 | + /// </para> |
| 51 | + /// <para> |
| 52 | + /// There are two constructors to wrap existing data, one to wrap blocks that are |
| 53 | + /// pointed to by an IntPtr and one that takes a byte array that we want to wrap. |
| 54 | + /// </para> |
| 55 | + /// <para> |
| 56 | + /// The empty constructor can be used to create a new TFBuffer that can be populated |
| 57 | + /// by the TensorFlow library and returned to user code. |
| 58 | + /// </para> |
| 59 | + /// <para> |
| 60 | + /// Typically, the data consists of a serialized protocol buffer, but other data |
| 61 | + /// may also be held in a buffer. |
| 62 | + /// </para> |
| 63 | + /// </remarks> |
| 64 | + // TODO: the string ctor |
| 65 | + // TODO: perhaps we should have an implicit byte [] conversion that just calls ToArray? |
| 66 | + internal class TFBuffer : TFDisposable |
| 67 | + { |
| 68 | + // extern TF_Buffer * TF_NewBufferFromString (const void *proto, size_t proto_len); |
| 69 | + [DllImport(NativeBinding.TensorFlowLibrary)] |
| 70 | + private static extern unsafe LLBuffer* TF_NewBufferFromString(IntPtr proto, IntPtr proto_len); |
| 71 | + |
| 72 | + // extern TF_Buffer * TF_NewBuffer (); |
| 73 | + [DllImport(NativeBinding.TensorFlowLibrary)] |
| 74 | + private static extern unsafe LLBuffer* TF_NewBuffer(); |
| 75 | + |
| 76 | + internal TFBuffer(IntPtr handle) : base(handle) { } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Initializes a new instance of the <see cref="T:TensorFlow.TFBuffer"/> class. |
| 80 | + /// </summary> |
| 81 | + public unsafe TFBuffer() : base((IntPtr)TF_NewBuffer()) |
| 82 | + { |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Signature of the method that is invoked to release the data. |
| 87 | + /// </summary> |
| 88 | + /// <remarks> |
| 89 | + /// Methods of this signature are invoked with the data pointer and the |
| 90 | + /// lenght pointer when then TFBuffer no longer needs to hold on to the |
| 91 | + /// data. If you are using this on platforms with static compilation |
| 92 | + /// like iOS, you need to annotate your callback with the MonoPInvokeCallbackAttribute, |
| 93 | + /// like this: |
| 94 | + /// |
| 95 | + /// <code> |
| 96 | + /// [TensorFlow.MonoPInvokeCallback (typeof (BufferReleaseFunc))] |
| 97 | + /// internal static void MyFreeFunc (IntPtr data, IntPtr length){..} |
| 98 | + /// </code> |
| 99 | + /// </remarks> |
| 100 | + public delegate void BufferReleaseFunc(IntPtr data, IntPtr lenght); |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Initializes a new instance of the <see cref="T:TensorFlow.TFBuffer"/> by wrapping the unmanaged resource pointed by the buffer. |
| 104 | + /// </summary> |
| 105 | + /// <param name="buffer">Pointer to the data that will be wrapped.</param> |
| 106 | + /// <param name="size">The size of the buffer to wrap.</param> |
| 107 | + /// <param name="release">Optional, if not null, this method will be invoked to release the block.</param> |
| 108 | + /// <remarks> |
| 109 | + /// This constructor wraps the buffer as a the data to be held by the <see cref="T:TensorFlow.TFBuffer"/>, |
| 110 | + /// if the release parameter is null, then you must ensure that the data is not released before the TFBuffer |
| 111 | + /// is no longer in use. If the value is not null, the provided method will be invoked to release |
| 112 | + /// the data when the TFBuffer is disposed, or the contents of the buffer replaced. |
| 113 | + /// </remarks> |
| 114 | + public unsafe TFBuffer(IntPtr buffer, long size, BufferReleaseFunc release) : base((IntPtr)TF_NewBuffer()) |
| 115 | + { |
| 116 | + LLBuffer* buf = (LLBuffer*)handle; |
| 117 | + buf->data = buffer; |
| 118 | + buf->length = (size_t)size; |
| 119 | + if (release == null) |
| 120 | + buf->data_deallocator = IntPtr.Zero; |
| 121 | + else |
| 122 | + buf->data_deallocator = Marshal.GetFunctionPointerForDelegate(release); |
| 123 | + } |
| 124 | + |
| 125 | + [MonoPInvokeCallback(typeof(BufferReleaseFunc))] |
| 126 | + internal static void FreeBlock(IntPtr data, IntPtr length) |
| 127 | + { |
| 128 | + Marshal.FreeHGlobal(data); |
| 129 | + } |
| 130 | + |
| 131 | + internal static IntPtr FreeBufferFunc; |
| 132 | + internal static BufferReleaseFunc FreeBlockDelegate; |
| 133 | + |
| 134 | + static TFBuffer() |
| 135 | + { |
| 136 | + FreeBlockDelegate = FreeBlock; |
| 137 | + FreeBufferFunc = Marshal.GetFunctionPointerForDelegate<BufferReleaseFunc>(FreeBlockDelegate); |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Initializes a new instance of the <see cref="T:TensorFlow.TFBuffer"/> by making a copy of the provided byte array. |
| 142 | + /// </summary> |
| 143 | + /// <param name="buffer">Buffer of data that will be wrapped.</param> |
| 144 | + /// <remarks> |
| 145 | + /// This constructor makes a copy of the data into an unmanaged buffer, |
| 146 | + /// so the byte array is not pinned. |
| 147 | + /// </remarks> |
| 148 | + public TFBuffer(byte[] buffer) : this(buffer, 0, buffer.Length) { } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Initializes a new instance of the <see cref="T:TensorFlow.TFBuffer"/> by making a copy of the provided byte array. |
| 152 | + /// </summary> |
| 153 | + /// <param name="buffer">Buffer of data that will be wrapped.</param> |
| 154 | + /// <param name="start">Starting offset into the buffer to wrap.</param> |
| 155 | + /// <param name="count">Number of bytes from the buffer to keep.</param> |
| 156 | + /// <remarks> |
| 157 | + /// This constructor makes a copy of the data into an unmanaged buffer, |
| 158 | + /// so the byte array is not pinned. |
| 159 | + /// </remarks> |
| 160 | + public TFBuffer(byte[] buffer, int start, int count) : this() |
| 161 | + { |
| 162 | + if (start < 0 || start >= buffer.Length) |
| 163 | + throw new ArgumentException("start"); |
| 164 | + if (count < 0 || count > buffer.Length - start) |
| 165 | + throw new ArgumentException("count"); |
| 166 | + unsafe |
| 167 | + { |
| 168 | + LLBuffer* buf = LLBuffer; |
| 169 | + buf->data = Marshal.AllocHGlobal(count); |
| 170 | + Marshal.Copy(buffer, start, buf->data, count); |
| 171 | + buf->length = (size_t)count; |
| 172 | + buf->data_deallocator = FreeBufferFunc; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + internal unsafe LLBuffer* LLBuffer => (LLBuffer*)handle; |
| 177 | + |
| 178 | + // extern void TF_DeleteBuffer (TF_Buffer *); |
| 179 | + [DllImport(NativeBinding.TensorFlowLibrary)] |
| 180 | + private static extern unsafe void TF_DeleteBuffer(LLBuffer* buffer); |
| 181 | + |
| 182 | + internal override void NativeDispose(IntPtr handle) |
| 183 | + { |
| 184 | + unsafe { TF_DeleteBuffer((LLBuffer*)handle); } |
| 185 | + } |
| 186 | + |
| 187 | + // extern TF_Buffer TF_GetBuffer (TF_Buffer *buffer); |
| 188 | + [DllImport(NativeBinding.TensorFlowLibrary)] |
| 189 | + private static extern unsafe LLBuffer TF_GetBuffer(LLBuffer* buffer); |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// Returns a byte array representing the data wrapped by this buffer. |
| 193 | + /// </summary> |
| 194 | + /// <returns>The array.</returns> |
| 195 | + public byte[] ToArray() |
| 196 | + { |
| 197 | + if (handle == IntPtr.Zero) |
| 198 | + return null; |
| 199 | + |
| 200 | + unsafe |
| 201 | + { |
| 202 | + var lb = (LLBuffer*)handle; |
| 203 | + |
| 204 | + var result = new byte[(int)lb->length]; |
| 205 | + Marshal.Copy(lb->data, result, 0, (int)lb->length); |
| 206 | + |
| 207 | + return result; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments