|
| 1 | +using NumSharp; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Text; |
| 8 | +using Tensorflow.Eager; |
| 9 | +using static Tensorflow.Binding; |
| 10 | + |
| 11 | +namespace Tensorflow |
| 12 | +{ |
| 13 | + public class EagerTensorV2 : DisposableObject, ITensor |
| 14 | + { |
| 15 | + IntPtr tfe_tensor_handle; |
| 16 | + public IntPtr EagerTensorHandle { get; set; } |
| 17 | + public string Device => c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(tfe_tensor_handle, status)); |
| 18 | + |
| 19 | + static Status status = new Status(); |
| 20 | + |
| 21 | + public EagerTensorV2(IntPtr handle) |
| 22 | + { |
| 23 | + EagerTensorHandle = handle; |
| 24 | + tfe_tensor_handle = c_api.EagerTensor_Handle(handle); |
| 25 | + _handle = c_api.TFE_TensorHandleResolve(tfe_tensor_handle, status); |
| 26 | + } |
| 27 | + |
| 28 | + public unsafe EagerTensorV2(NDArray nd, string device_name = "") |
| 29 | + { |
| 30 | + if (nd.typecode == NPTypeCode.String) |
| 31 | + throw new NotImplementedException("Support for NDArray of type string not implemented yet"); |
| 32 | + |
| 33 | + var arraySlice = nd.Unsafe.Storage.Shape.IsContiguous ? nd.GetData() : nd.CloneData(); |
| 34 | + |
| 35 | + _handle = c_api.TF_NewTensor(nd.dtype.as_dtype(), |
| 36 | + nd.shape.Select(i => (long)i).ToArray(), |
| 37 | + nd.ndim, |
| 38 | + new IntPtr(arraySlice.Address), |
| 39 | + nd.size * nd.dtypesize, |
| 40 | + deallocator: (IntPtr dataPtr, long len, IntPtr args) => |
| 41 | + { |
| 42 | + |
| 43 | + }, IntPtr.Zero); |
| 44 | + |
| 45 | + tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status); |
| 46 | + EagerTensorHandle = c_api.TFE_EagerTensorFromHandle(tf.context, tfe_tensor_handle); |
| 47 | + } |
| 48 | + |
| 49 | + /*public unsafe EagerTensorV2(float[,] value) |
| 50 | + { |
| 51 | + var dims = new long[] { value.Rank, value.Length / value.Rank }; |
| 52 | + fixed (float* pointer = &value[0, 0]) |
| 53 | + { |
| 54 | + // The address stored in pointerToFirst |
| 55 | + // is valid only inside this fixed statement block. |
| 56 | + tensorHandle = c_api.TF_NewTensor(TF_DataType.TF_FLOAT, |
| 57 | + dims, |
| 58 | + value.Rank, |
| 59 | + new IntPtr(pointer), |
| 60 | + value.Length * sizeof(float), |
| 61 | + deallocator: (IntPtr dataPtr, long len, IntPtr args) => |
| 62 | + { |
| 63 | +
|
| 64 | + }, IntPtr.Zero); |
| 65 | +
|
| 66 | +
|
| 67 | + localTensorHandle = c_api.TFE_NewTensorHandle(tensorHandle, status); |
| 68 | + _handle = c_api.TFE_EagerTensorFromHandle(tf.context, localTensorHandle); |
| 69 | + } |
| 70 | + }*/ |
| 71 | + |
| 72 | + protected override void DisposeUnmanagedResources(IntPtr handle) |
| 73 | + { |
| 74 | + c_api.TF_DeleteTensor(_handle); |
| 75 | + c_api.TFE_DeleteTensorHandle(tfe_tensor_handle); |
| 76 | + c_api.TFE_DeleteEagerTensor(EagerTensorHandle); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments