| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 33e17df commit caae2db
18 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,7 +55,7 @@ public partial class c_api | |||
| 55 | 55 | /// <param name="oper"></param> | |
| 56 | 56 | /// <returns></returns> | |
| 57 | 57 | [DllImport(TensorFlowLibName)] | |
| 58 | - public static extern int TF_OperationGetAttrValueProto(IntPtr oper, string attr_name, IntPtr output_attr_value, SafeStatusHandle status); | ||
| 58 | + public static extern int TF_OperationGetAttrValueProto(IntPtr oper, string attr_name, SafeBufferHandle output_attr_value, SafeStatusHandle status); | ||
| 59 | 59 | ||
| 60 | 60 | [DllImport(TensorFlowLibName)] | |
| 61 | 61 | public static extern void TF_SetAttrBool(IntPtr desc, string attr_name, bool value); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,45 +14,42 @@ You may obtain a copy of the License at | |||
| 14 | 14 | limitations under the License. | |
| 15 | 15 | ******************************************************************************/ | |
| 16 | 16 | ||
| 17 | + using NumSharp.Backends.Unmanaged; | ||
| 17 | 18 | using System; | |
| 18 | 19 | using System.Runtime.CompilerServices; | |
| 19 | 20 | using System.Runtime.InteropServices; | |
| 20 | - using NumSharp.Backends.Unmanaged; | ||
| 21 | + using Tensorflow.Util; | ||
| 21 | 22 | using static Tensorflow.c_api; | |
| 22 | 23 | ||
| 23 | 24 | namespace Tensorflow | |
| 24 | 25 | { | |
| 25 | 26 | /// <summary> | |
| 26 | 27 | /// Represents a TF_Buffer that can be passed to Tensorflow. | |
| 27 | 28 | /// </summary> | |
| 28 | - public class Buffer : DisposableObject | ||
| 29 | + public sealed class Buffer : IDisposable | ||
| 29 | 30 | { | |
| 30 | - private unsafe TF_Buffer buffer | ||
| 31 | - { | ||
| 32 | - [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| 33 | - get => *bufferptr; | ||
| 34 | - } | ||
| 31 | + public SafeBufferHandle Handle { get; } | ||
| 35 | 32 | ||
| 36 | - private unsafe TF_Buffer* bufferptr | ||
| 37 | - { | ||
| 38 | - [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| 39 | - get => (TF_Buffer*) _handle; | ||
| 40 | - } | ||
| 33 | + /// <remarks> | ||
| 34 | + /// <inheritdoc cref="SafeHandleLease" path="/devdoc/usage"/> | ||
| 35 | + /// </remarks> | ||
| 36 | + private unsafe ref readonly TF_Buffer DangerousBuffer | ||
| 37 | + => ref Unsafe.AsRef<TF_Buffer>(Handle.DangerousGetHandle().ToPointer()); | ||
| 41 | 38 | ||
| 42 | 39 | /// <summary> | |
| 43 | 40 | /// The memory block representing this buffer. | |
| 44 | 41 | /// </summary> | |
| 45 | - /// <remarks>The deallocator is set to null.</remarks> | ||
| 46 | - public UnmanagedMemoryBlock<byte> MemoryBlock | ||
| 42 | + /// <remarks> | ||
| 43 | + /// <para>The deallocator is set to null.</para> | ||
| 44 | + /// | ||
| 45 | + /// <inheritdoc cref="SafeHandleLease" path="/devdoc/usage"/> | ||
| 46 | + /// </remarks> | ||
| 47 | + public unsafe UnmanagedMemoryBlock<byte> DangerousMemoryBlock | ||
| 47 | 48 | { | |
| 48 | 49 | get | |
| 49 | 50 | { | |
| 50 | - unsafe | ||
| 51 | - { | ||
| 52 | - EnsureNotDisposed(); | ||
| 53 | - var buff = (TF_Buffer*) _handle; | ||
| 54 | - return new UnmanagedMemoryBlock<byte>((byte*) buff->data.ToPointer(), (long) buff->length); | ||
| 55 | - } | ||
| 51 | + ref readonly TF_Buffer buffer = ref DangerousBuffer; | ||
| 52 | + return new UnmanagedMemoryBlock<byte>((byte*)buffer.data.ToPointer(), (long)buffer.length); | ||
| 56 | 53 | } | |
| 57 | 54 | } | |
| 58 | 55 | ||
@@ -63,25 +60,23 @@ public ulong Length | |||
| 63 | 60 | { | |
| 64 | 61 | get | |
| 65 | 62 | { | |
| 66 | - EnsureNotDisposed(); | ||
| 67 | - return buffer.length; | ||
| 63 | + using (Handle.Lease()) | ||
| 64 | + { | ||
| 65 | + return DangerousBuffer.length; | ||
| 66 | + } | ||
| 68 | 67 | } | |
| 69 | 68 | } | |
| 70 | 69 | ||
| 71 | - public Buffer() => _handle = TF_NewBuffer(); | ||
| 72 | - | ||
| 73 | - public Buffer(IntPtr handle) | ||
| 74 | - { | ||
| 75 | - if (handle == IntPtr.Zero) | ||
| 76 | - throw new ArgumentException("Handle (IntPtr) can't be zero.", nameof(handle)); | ||
| 70 | + public Buffer() | ||
| 71 | + => Handle = TF_NewBuffer(); | ||
| 77 | 72 | ||
| 78 | - _handle = handle; | ||
| 79 | - } | ||
| 73 | + public Buffer(SafeBufferHandle handle) | ||
| 74 | + => Handle = handle; | ||
| 80 | 75 | ||
| 81 | - public Buffer(byte[] data) : this(_toBuffer(data)) | ||
| 82 | - { } | ||
| 76 | + public Buffer(byte[] data) | ||
| 77 | + => Handle = _toBuffer(data); | ||
| 83 | 78 | ||
| 84 | - private static IntPtr _toBuffer(byte[] data) | ||
| 79 | + private static SafeBufferHandle _toBuffer(byte[] data) | ||
| 85 | 80 | { | |
| 86 | 81 | if (data == null) | |
| 87 | 82 | throw new ArgumentNullException(nameof(data)); | |
@@ -93,38 +88,25 @@ private static IntPtr _toBuffer(byte[] data) | |||
| 93 | 88 | } | |
| 94 | 89 | } | |
| 95 | 90 | ||
| 96 | - public static implicit operator IntPtr(Buffer buffer) | ||
| 97 | - { | ||
| 98 | - buffer.EnsureNotDisposed(); | ||
| 99 | - return buffer._handle; | ||
| 100 | - } | ||
| 101 | - | ||
| 102 | - public static explicit operator byte[](Buffer buffer) => buffer.ToArray(); //has to be explicit, developer will assume it doesn't cost. | ||
| 103 | - | ||
| 104 | 91 | /// <summary> | |
| 105 | 92 | /// Copies this buffer's contents onto a <see cref="byte"/> array. | |
| 106 | 93 | /// </summary> | |
| 107 | 94 | public byte[] ToArray() | |
| 108 | 95 | { | |
| 109 | - EnsureNotDisposed(); | ||
| 110 | - | ||
| 111 | - unsafe | ||
| 96 | + using (Handle.Lease()) | ||
| 112 | 97 | { | |
| 113 | - var len = buffer.length; | ||
| 98 | + var block = DangerousMemoryBlock; | ||
| 99 | + var len = block.Count; | ||
| 114 | 100 | if (len == 0) | |
| 115 | 101 | return Array.Empty<byte>(); | |
| 116 | 102 | ||
| 117 | - byte[] data = new byte[len]; | ||
| 118 | - fixed (byte* dst = data) | ||
| 119 | - System.Buffer.MemoryCopy((void*) bufferptr->data, dst, len, len); | ||
| 120 | - | ||
| 103 | + var data = new byte[len]; | ||
| 104 | + block.CopyTo(data, 0); | ||
| 121 | 105 | return data; | |
| 122 | 106 | } | |
| 123 | 107 | } | |
| 124 | 108 | ||
| 125 | - protected override void DisposeUnmanagedResources(IntPtr handle) | ||
| 126 | - { | ||
| 127 | - TF_DeleteBuffer(handle); | ||
| 128 | - } | ||
| 109 | + public void Dispose() | ||
| 110 | + => Handle.Dispose(); | ||
| 129 | 111 | } | |
| 130 | 112 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,40 @@ | |||
| 1 | + /***************************************************************************** | ||
| 2 | + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. | ||
| 3 | + | ||
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + you may not use this file except in compliance with the License. | ||
| 6 | + You may obtain a copy of the License at | ||
| 7 | + | ||
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + | ||
| 10 | + Unless required by applicable law or agreed to in writing, software | ||
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + See the License for the specific language governing permissions and | ||
| 14 | + limitations under the License. | ||
| 15 | + ******************************************************************************/ | ||
| 16 | + | ||
| 17 | + using System; | ||
| 18 | + using Tensorflow.Util; | ||
| 19 | + | ||
| 20 | + namespace Tensorflow | ||
| 21 | + { | ||
| 22 | + public sealed class SafeBufferHandle : SafeTensorflowHandle | ||
| 23 | + { | ||
| 24 | + private SafeBufferHandle() | ||
| 25 | + { | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + public SafeBufferHandle(IntPtr handle) | ||
| 29 | + : base(handle) | ||
| 30 | + { | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + protected override bool ReleaseHandle() | ||
| 34 | + { | ||
| 35 | + c_api.TF_DeleteBuffer(handle); | ||
| 36 | + SetHandle(IntPtr.Zero); | ||
| 37 | + return true; | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,7 @@ public partial class c_api | |||
| 29 | 29 | /// </summary> | |
| 30 | 30 | /// <returns></returns> | |
| 31 | 31 | [DllImport(TensorFlowLibName)] | |
| 32 | - public static extern IntPtr TF_NewBuffer(); | ||
| 32 | + public static extern SafeBufferHandle TF_NewBuffer(); | ||
| 33 | 33 | ||
| 34 | 34 | [DllImport(TensorFlowLibName)] | |
| 35 | 35 | public static extern IntPtr TF_GetBuffer(TF_Buffer buffer); | |
@@ -42,6 +42,6 @@ public partial class c_api | |||
| 42 | 42 | /// <param name="proto_len">size_t</param> | |
| 43 | 43 | /// <returns></returns> | |
| 44 | 44 | [DllImport(TensorFlowLibName)] | |
| 45 | - public static extern IntPtr TF_NewBufferFromString(IntPtr proto, ulong proto_len); | ||
| 45 | + public static extern SafeBufferHandle TF_NewBufferFromString(IntPtr proto, ulong proto_len); | ||
| 46 | 46 | } | |
| 47 | 47 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,7 +62,7 @@ public static ITensorOrOperation[] import_graph_def(GraphDef graph_def, | |||
| 62 | 62 | { | |
| 63 | 63 | _PopulateTFImportGraphDefOptions(scoped_options, prefix, input_map, return_elements); | |
| 64 | 64 | // need to create a class ImportGraphDefWithResults with IDisposal | |
| 65 | - results = c_api.TF_GraphImportGraphDefWithResults(graph, buffer, scoped_options.Handle, status.Handle); | ||
| 65 | + results = c_api.TF_GraphImportGraphDefWithResults(graph, buffer.Handle, scoped_options.Handle, status.Handle); | ||
| 66 | 66 | status.Check(true); | |
| 67 | 67 | } | |
| 68 | 68 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,7 +30,7 @@ public static Dictionary<string, OpDef> get_registered_ops() | |||
| 30 | 30 | { | |
| 31 | 31 | _registered_ops = new Dictionary<string, OpDef>(); | |
| 32 | 32 | using var buffer = new Buffer(c_api.TF_GetAllOpList()); | |
| 33 | - using var stream = buffer.MemoryBlock.Stream(); | ||
| 33 | + using var stream = buffer.DangerousMemoryBlock.Stream(); | ||
| 34 | 34 | var op_list = OpList.Parser.ParseFrom(stream); | |
| 35 | 35 | foreach (var op_def in op_list.Op) | |
| 36 | 36 | _registered_ops[op_def.Name] = op_def; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,7 +34,7 @@ public GraphDef TransformGraph(GraphDef input_graph_def, | |||
| 34 | 34 | inputs_string, | |
| 35 | 35 | outputs_string, | |
| 36 | 36 | transforms_string, | |
| 37 | - buffer, | ||
| 37 | + buffer.Handle, | ||
| 38 | 38 | status.Handle); | |
| 39 | 39 | ||
| 40 | 40 | status.Check(false); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,7 +27,7 @@ public static extern int TransformGraphWithStringInputs(byte[] graph_def_string, | |||
| 27 | 27 | string inputs_string, | |
| 28 | 28 | string outputs_string, | |
| 29 | 29 | string transforms_string, | |
| 30 | - IntPtr output_buffer, | ||
| 30 | + SafeBufferHandle output_buffer, | ||
| 31 | 31 | SafeStatusHandle status); | |
| 32 | 32 | } | |
| 33 | 33 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,7 +25,7 @@ public partial class Graph | |||
| 25 | 25 | public Buffer ToGraphDef(Status s) | |
| 26 | 26 | { | |
| 27 | 27 | var buffer = new Buffer(); | |
| 28 | - c_api.TF_GraphToGraphDef(_handle, buffer, s.Handle); | ||
| 28 | + c_api.TF_GraphToGraphDef(_handle, buffer.Handle, s.Handle); | ||
| 29 | 29 | s.Check(true); | |
| 30 | 30 | ||
| 31 | 31 | return buffer; | |
@@ -39,7 +39,7 @@ private GraphDef _as_graph_def(bool add_shapes = false) | |||
| 39 | 39 | { | |
| 40 | 40 | status.Check(true); | |
| 41 | 41 | // limit size to 250M, recursion to max 100 | |
| 42 | - var inputStream = CodedInputStream.CreateWithLimits(buffer.MemoryBlock.Stream(), 250 * 1024 * 1024, 100); | ||
| 42 | + var inputStream = CodedInputStream.CreateWithLimits(buffer.DangerousMemoryBlock.Stream(), 250 * 1024 * 1024, 100); | ||
| 43 | 43 | def = GraphDef.Parser.ParseFrom(inputStream); | |
| 44 | 44 | } | |
| 45 | 45 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,7 @@ public unsafe TF_Output[] ImportGraphDefWithReturnOutputs(Buffer graph_def, Impo | |||
| 29 | 29 | int size = Marshal.SizeOf<TF_Output>(); | |
| 30 | 30 | var return_output_handle = Marshal.AllocHGlobal(size * num_return_outputs); | |
| 31 | 31 | ||
| 32 | - c_api.TF_GraphImportGraphDefWithReturnOutputs(_handle, graph_def, opts.Handle, return_output_handle, num_return_outputs, s.Handle); | ||
| 32 | + c_api.TF_GraphImportGraphDefWithReturnOutputs(_handle, graph_def.Handle, opts.Handle, return_output_handle, num_return_outputs, s.Handle); | ||
| 33 | 33 | ||
| 34 | 34 | var tf_output_ptr = (TF_Output*) return_output_handle; | |
| 35 | 35 | for (int i = 0; i < num_return_outputs; i++) | |
@@ -54,7 +54,7 @@ public bool Import(byte[] bytes, string prefix = "") | |||
| 54 | 54 | { | |
| 55 | 55 | as_default(); | |
| 56 | 56 | c_api.TF_ImportGraphDefOptionsSetPrefix(opts.Handle, prefix); | |
| 57 | - c_api.TF_GraphImportGraphDef(_handle, graph_def, opts.Handle, status.Handle); | ||
| 57 | + c_api.TF_GraphImportGraphDef(_handle, graph_def.Handle, opts.Handle, status.Handle); | ||
| 58 | 58 | status.Check(true); | |
| 59 | 59 | return status.Code == TF_Code.TF_OK; | |
| 60 | 60 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments