Browse Source

Remove AllocationType.

tags/v0.60-tf.numpy
Oceania2018 4 years ago
parent
commit
16d48a1d0e
3 changed files with 3 additions and 73 deletions
  1. +0
    -27
      src/TensorFlowNET.Core/Tensors/AllocationType.cs
  2. +1
    -22
      src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs
  3. +2
    -24
      src/TensorFlowNET.Core/Tensors/Tensor.cs

+ 0
- 27
src/TensorFlowNET.Core/Tensors/AllocationType.cs View File

@@ -1,27 +0,0 @@
namespace Tensorflow
{
/// <summary>
/// Used internally to
/// </summary>
public enum AllocationType
{
None = 0,
/// <summary>
/// Allocation was done by passing in a pointer, might be also holding reference to a C# object.
/// </summary>
FromPointer = 1,
/// <summary>
/// Allocation was done by calling c_api.TF_AllocateTensor or TF decided it has to copy data during c_api.TF_NewTensor. <br></br>
/// Deallocation is handled solely by Tensorflow.
/// </summary>
Tensorflow = 2,
/// <summary>
/// Allocation was done by Marshal.AllocateHGlobal
/// </summary>
Marshal = 3,
/// <summary>
/// Allocation was done by GCHandle.Alloc
/// </summary>
GCHandle = 4,
}
}

+ 1
- 22
src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs View File

@@ -19,8 +19,6 @@ using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using static Tensorflow.c_api;

@@ -29,21 +27,6 @@ namespace Tensorflow
[SuppressMessage("ReSharper", "InvokeAsExtensionMethod")]
public partial class Tensor
{
/// <summary>
/// The handle that was used to allocate this tensor, dependent on <see cref="AllocationType"/>.
/// </summary>
protected object AllocationHandle;

/// <summary>
/// True if this Tensor holds data allocated by C#.
/// </summary>
public bool IsMemoryOwner => AllocationType >= AllocationType.Marshal;

/// <summary>
/// The allocation method used to create this Tensor.
/// </summary>
public AllocationType AllocationType { get; protected set; }

public IntPtr TensorDataPointer => _handle == IntPtr.Zero ? IntPtr.Zero : TF_TensorData(_handle);

public Tensor()
@@ -125,11 +108,7 @@ namespace Tensorflow
/// <param name="num_bytes">Size of the tensor in memory</param>
public Tensor(IntPtr data_ptr, long[] shape, TF_DataType dType, int num_bytes)
{
unsafe
{
_handle = TF_NewTensor(dType, dims: shape, num_dims: shape.Length, data: data_ptr, len: (ulong)num_bytes);
AllocationType = TF_TensorData(_handle) == data_ptr ? AllocationType.FromPointer : AllocationType.Tensorflow;
}
_handle = TF_NewTensor(dType, dims: shape, num_dims: shape.Length, data: data_ptr, len: (ulong)num_bytes);
}

public unsafe Tensor(NDArray nd)


+ 2
- 24
src/TensorFlowNET.Core/Tensors/Tensor.cs View File

@@ -93,7 +93,8 @@ namespace Tensorflow
/// TFE_TensorHandle
/// </summary>
public SafeTensorHandleHandle EagerTensorHandle { get; set; }

protected bool _createdInGraphMode;
public bool CreatedInGraphMode => _createdInGraphMode;
public bool IsEagerTensor => this is EagerTensor;
public bool IsSparseTensor => this is SparseTensor;

@@ -262,29 +263,6 @@ namespace Tensorflow
#if TRACK_TENSOR_LIFE
print($"Delete Tensor 0x{handle.ToString("x16")} {AllocationType} Data: 0x{TensorDataPointer.ToString("x16")}");
#endif
if (AllocationHandle != null)
{
if (AllocationType == AllocationType.GCHandle)
{
((GCHandle)AllocationHandle).Free();
AllocationHandle = null;
AllocationType = AllocationType.None;
}
else if (AllocationType == AllocationType.Marshal)
{
Marshal.FreeHGlobal((IntPtr)AllocationHandle);
AllocationHandle = null;
AllocationType = AllocationType.None;
}
else if (AllocationType == AllocationType.FromPointer)
{
AllocationHandle = null;
AllocationType = AllocationType.None;
}
else
throw new InvalidOperationException($"Tensor.AllocationHandle is not null ({AllocationHandle}) but AllocationType is not matched to a C# allocation type ({AllocationType}).");
}

if (dtype == TF_DataType.TF_STRING)
{
long size = 1;


Loading…
Cancel
Save