Skip to content

Commit 7cb1746

Browse files
committed
Tensor: added a constructor to create a Tensor from a fixed memory pointer that is owned by the caller
1 parent 464144d commit 7cb1746

File tree

7 files changed

+179
-212
lines changed

7 files changed

+179
-212
lines changed

TensorFlow.NET.sln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29102.190
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.645
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.UnitTest", "test\TensorFlowNET.UnitTest\TensorFlowNET.UnitTest.csproj", "{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}"
77
EndProject
@@ -17,7 +17,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Keras.UnitTest", "test\Kera
1717
EndProject
1818
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "TensorFlowNET.Examples.FSharp", "test\TensorFlowNET.Examples.FSharp\TensorFlowNET.Examples.FSharp.fsproj", "{62BC3801-F0D3-44A9-A0AC-712F40C8F961}"
1919
EndProject
20-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Benchmark", "src\TensorFlowNet.Benchmarks\TensorFlowNET.Benchmark.csproj", "{68861442-971A-4196-876E-C9330F0B3C54}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowBenchmark", "src\TensorFlowNet.Benchmarks\TensorFlowBenchmark.csproj", "{68861442-971A-4196-876E-C9330F0B3C54}"
2121
EndProject
2222
Global
2323
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs

Lines changed: 117 additions & 168 deletions
Large diffs are not rendered by default.

src/TensorFlowNet.Benchmarks/Program.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33
using BenchmarkDotNet.Configs;
44
using BenchmarkDotNet.Running;
55

6-
namespace TensorFlowNet.Benchmark
6+
namespace TensorFlowBenchmark
77
{
88
class Program
99
{
10-
/// <summary>
11-
/// dotnet NumSharp.Benchmark.dll (Benchmark Class Name)
12-
/// dotnet NumSharp.Benchmark.dll nparange
13-
/// </summary>
14-
/// <param name="args"></param>
1510
static void Main(string[] args)
1611
{
1712
#if DEBUG
@@ -24,7 +19,7 @@ static void Main(string[] args)
2419
{
2520
for (int i = 0; i < args.Length; i++)
2621
{
27-
string name = $"TensorFlowNet.Benchmark.{args[i]}";
22+
string name = $"TensorFlowBenchmark.{args[i]}";
2823
var type = Type.GetType(name);
2924
BenchmarkRunner.Run(type, config);
3025
}

src/TensorFlowNet.Benchmarks/TensorBenchmark.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
2-
using System.Linq;
32
using BenchmarkDotNet.Attributes;
4-
using BenchmarkDotNet.Engines;
53
using NumSharp;
64
using Tensorflow;
75

8-
namespace TensorFlowNet.Benchmark
6+
namespace TensorFlowBenchmark
97
{
108
[SimpleJob(launchCount: 1, warmupCount: 2, targetCount: 10)]
119
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
@@ -33,15 +31,16 @@ public void ScalarTensor()
3331
}
3432

3533
[Benchmark]
36-
public void TensorFromSpan()
34+
public unsafe void TensorFromFixedPtr()
3735
{
3836
var g = new Graph();
39-
var span = new Span<double>(data);
4037
for (int i = 0; i < 100; i++)
4138
{
42-
using (var tensor = new Tensor(span, new long[] { data.Length }))
39+
fixed (double* ptr = &data[0])
4340
{
44-
41+
using (var t = new Tensor((IntPtr)ptr, new long[] { data.Length }, tf.float64, 8 * data.Length))
42+
{
43+
}
4544
}
4645
}
4746
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
<NoWin32Manifest>true</NoWin32Manifest>
7+
<AssemblyName>TensorFlowBenchmark</AssemblyName>
8+
<RootNamespace>TensorFlowBenchmark</RootNamespace>
9+
</PropertyGroup>
10+
11+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
12+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
13+
</PropertyGroup>
14+
15+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
16+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<None Remove="tensorflow.dll" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<ProjectReference Include="..\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
29+
</ItemGroup>
30+
31+
</Project>

src/TensorFlowNet.Benchmarks/TensorFlowNet.Benchmark.csproj

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/TensorFlowNET.UnitTest/TensorTest.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,27 @@ public void TensorDeallocationThreadSafety()
5353
}
5454

5555
[TestMethod]
56-
public void TensorFromSpan()
57-
{
58-
var array = new int[1000];
59-
var span = new Span<int>(array, 100, 500);
60-
using (var t = new Tensor(span, new long[] { span.Length }))
56+
public unsafe void TensorFromFixed()
57+
{
58+
var array = new float[1000];
59+
var span = new Span<float>(array, 100, 500);
60+
fixed (float* ptr=&MemoryMarshal.GetReference(span))
61+
{
62+
using (var t = new Tensor((IntPtr)ptr, new long[] {span.Length}, tf.float32, 4*span.Length))
63+
{
64+
Assert.IsFalse(t.IsDisposed);
65+
Assert.IsFalse(t.IsMemoryOwner);
66+
Assert.AreEqual(2000, (int) t.bytesize);
67+
}
68+
}
69+
fixed (float* ptr = &array[0])
6170
{
62-
Assert.IsFalse(t.IsDisposed);
63-
Assert.AreEqual(2000, (int)t.bytesize);
71+
using (var t = new Tensor((IntPtr)ptr, new long[] { array.Length }, tf.float32, 4 * array.Length))
72+
{
73+
Assert.IsFalse(t.IsDisposed);
74+
Assert.IsFalse(t.IsMemoryOwner);
75+
Assert.AreEqual(4000, (int)t.bytesize);
76+
}
6477
}
6578
}
6679

0 commit comments

Comments
 (0)