Skip to content

Commit 40487d3

Browse files
committed
restructure unit test folder.
1 parent c71b4a5 commit 40487d3

44 files changed

Lines changed: 276 additions & 103 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/TensorFlowNET.Console/MemoryLeakTesting.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,26 @@ public void WarmUp()
1717
/// </summary>
1818
public void TensorCreation()
1919
{
20-
int total = 10 * 1000 * 1000;
21-
for(int i = 0; i < total; i++)
20+
int total = 1 * 1000 * 1000;
21+
for (int i = 0; i < total; i++)
2222
{
23-
var const1 = tf.constant(3112.0f);
24-
// const1.Dispose();
23+
/*var const1 = new Tensor(new float[,]
24+
{
25+
{ 3.0f, 1.0f },
26+
{ 1.0f, 2.0f }
27+
});
28+
const1.Dispose();*/
29+
30+
var tensor = new EagerTensorV2(new float[,]
31+
{
32+
{ 3.0f, 1.0f },
33+
{ 1.0f, 2.0f }
34+
});
35+
36+
tensor.Dispose();
2537
}
38+
39+
GC.Collect();
2640
}
2741
}
2842
}

src/TensorFlowNET.Console/TensorFlowNET.Console.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<AssemblyName>Tensorflow</AssemblyName>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.2.0.1" />
12+
</ItemGroup>
13+
1014
<ItemGroup>
1115
<ProjectReference Include="..\TensorFlowNET.Core\Tensorflow.Binding.csproj" />
1216
</ItemGroup>

src/TensorFlowNET.Core/APIs/c_api.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ namespace Tensorflow
4343
/// </summary>
4444
public partial class c_api
4545
{
46-
public const string TensorFlowLibName = @"D:\SciSharp\tensorflow-google\bazel-bin\tensorflow\tensorflow.dll";
46+
public const string TensorFlowLibName = "tensorflow";
4747

4848
public static string StringPiece(IntPtr handle)
4949
{
5050
return handle == IntPtr.Zero ? String.Empty : Marshal.PtrToStringAnsi(handle);
5151
}
5252

5353
public delegate void Deallocator(IntPtr data, IntPtr size, ref DeallocatorArgs args);
54-
54+
public delegate void DeallocatorV2(IntPtr data, long size, IntPtr args);
5555
public struct DeallocatorArgs
5656
{
5757
internal static unsafe c_api.DeallocatorArgs* EmptyPtr;

src/TensorFlowNET.Core/Eager/EagerTensor.Creation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public EagerTensor(IntPtr handle) : base(handle)
1515
_handle = c_api.TFE_TensorHandleResolve(tfe_tensor_handle, status);
1616
}
1717

18-
public EagerTensor(int value, string device_name) : base(value)
18+
/*public EagerTensor(int value, string device_name) : base(value)
1919
{
2020
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
2121
EagerTensorHandle = c_api.TFE_EagerTensorFromHandle(tf.context, tfe_tensor_handle);
@@ -31,14 +31,14 @@ public EagerTensor(float value, string device_name) : base(value)
3131
{
3232
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
3333
EagerTensorHandle = c_api.TFE_EagerTensorFromHandle(tf.context, tfe_tensor_handle);
34-
}
35-
34+
}*/
35+
3636
public EagerTensor(string value, string device_name) : base(value)
3737
{
3838
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
3939
EagerTensorHandle = c_api.TFE_EagerTensorFromHandle(tf.context, tfe_tensor_handle);
4040
}
41-
41+
4242
public EagerTensor(NDArray value, string device_name) : base(value)
4343
{
4444
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
4+
using System.Text;
5+
6+
namespace Tensorflow.Eager
7+
{
8+
[StructLayout(LayoutKind.Sequential)]
9+
public struct TFE_TensorHandle
10+
{
11+
IntPtr _handle;
12+
13+
public static implicit operator IntPtr(TFE_TensorHandle tensor)
14+
=> tensor._handle;
15+
16+
public override string ToString()
17+
=> $"TFE_TensorHandle 0x{_handle.ToString("x16")}";
18+
}
19+
}

src/TensorFlowNET.Core/Eager/c_api.eager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public delegate void delete_backward_function_callback(string op_name,
214214
/// <param name="t">const tensorflow::Tensor&</param>
215215
/// <returns>TFE_TensorHandle*</returns>
216216
[DllImport(TensorFlowLibName)]
217-
public static extern IntPtr TFE_NewTensorHandle(IntPtr t, IntPtr status);
217+
public static extern TFE_TensorHandle TFE_NewTensorHandle(IntPtr t, IntPtr status);
218218

219219
[DllImport(TensorFlowLibName)]
220220
public static extern IntPtr EagerTensor_Handle(IntPtr t);

src/TensorFlowNET.Core/TensorFlow.Binding.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<AssemblyName>TensorFlow.NET</AssemblyName>
66
<RootNamespace>Tensorflow</RootNamespace>
77
<TargetTensorFlow>2.2.0</TargetTensorFlow>
8-
<Version>0.20.0-alpha</Version>
8+
<Version>0.20.0-alpha2</Version>
99
<LangVersion>8.0</LangVersion>
1010
<Authors>Haiping Chen, Meinrad Recheis, Eli Belash</Authors>
1111
<Company>SciSharp STACK</Company>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow
6+
{
7+
public interface ITensor
8+
{
9+
10+
}
11+
}

src/TensorFlowNET.Core/Tensors/Tensor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ namespace Tensorflow
3232
/// Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes.
3333
/// </summary>
3434
[SuppressMessage("ReSharper", "ConvertToAutoProperty")]
35-
public partial class Tensor : DisposableObject,
35+
public partial class Tensor : DisposableObject,
36+
ITensor,
3637
ITensorOrOperation,
3738
_TensorLike,
3839
ITensorOrTensorArray,

0 commit comments

Comments
 (0)