@@ -1,7 +1,7 @@ | |||
| |||
Microsoft Visual Studio Solution File, Format Version 12.00 | |||
# Visual Studio Version 17 | |||
VisualStudioVersion = 17.0.31423.177 | |||
# Visual Studio Version 16 | |||
VisualStudioVersion = 16.0.31624.102 | |||
MinimumVisualStudioVersion = 10.0.40219.1 | |||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Binding", "src\TensorFlowNET.Core\Tensorflow.Binding.csproj", "{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}" | |||
EndProject | |||
@@ -77,8 +77,8 @@ Global | |||
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x86.Build.0 = Debug|Any CPU | |||
{03F06299-3F4B-4449-A709-3A647657BC0C}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{03F06299-3F4B-4449-A709-3A647657BC0C}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{03F06299-3F4B-4449-A709-3A647657BC0C}.Release|x64.ActiveCfg = Release|Any CPU | |||
{03F06299-3F4B-4449-A709-3A647657BC0C}.Release|x64.Build.0 = Release|Any CPU | |||
{03F06299-3F4B-4449-A709-3A647657BC0C}.Release|x64.ActiveCfg = Release|x64 | |||
{03F06299-3F4B-4449-A709-3A647657BC0C}.Release|x64.Build.0 = Release|x64 | |||
{03F06299-3F4B-4449-A709-3A647657BC0C}.Release|x86.ActiveCfg = Release|Any CPU | |||
{03F06299-3F4B-4449-A709-3A647657BC0C}.Release|x86.Build.0 = Release|Any CPU | |||
{49D71826-C03D-4FA7-9BAC-22C1327E65CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
@@ -101,8 +101,8 @@ Global | |||
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Debug|x86.Build.0 = Debug|Any CPU | |||
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|x64.ActiveCfg = Release|Any CPU | |||
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|x64.Build.0 = Release|Any CPU | |||
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|x64.ActiveCfg = Release|x64 | |||
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|x64.Build.0 = Release|x64 | |||
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|x86.ActiveCfg = Release|Any CPU | |||
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|x86.Build.0 = Release|Any CPU | |||
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
@@ -113,8 +113,8 @@ Global | |||
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x86.Build.0 = Debug|Any CPU | |||
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x64.ActiveCfg = Release|Any CPU | |||
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x64.Build.0 = Release|Any CPU | |||
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x64.ActiveCfg = Release|x64 | |||
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x64.Build.0 = Release|x64 | |||
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x86.ActiveCfg = Release|Any CPU | |||
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x86.Build.0 = Release|Any CPU | |||
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
@@ -125,8 +125,8 @@ Global | |||
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug|x86.Build.0 = Debug|Any CPU | |||
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|x64.ActiveCfg = Release|Any CPU | |||
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|x64.Build.0 = Release|Any CPU | |||
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|x64.ActiveCfg = Release|x64 | |||
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|x64.Build.0 = Release|x64 | |||
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|x86.ActiveCfg = Release|Any CPU | |||
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|x86.Build.0 = Release|Any CPU | |||
{79EB56DF-E29E-4AE2-A7D9-FE403FD919BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
@@ -137,8 +137,8 @@ Global | |||
{79EB56DF-E29E-4AE2-A7D9-FE403FD919BA}.Debug|x86.Build.0 = Debug|Any CPU | |||
{79EB56DF-E29E-4AE2-A7D9-FE403FD919BA}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{79EB56DF-E29E-4AE2-A7D9-FE403FD919BA}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{79EB56DF-E29E-4AE2-A7D9-FE403FD919BA}.Release|x64.ActiveCfg = Release|Any CPU | |||
{79EB56DF-E29E-4AE2-A7D9-FE403FD919BA}.Release|x64.Build.0 = Release|Any CPU | |||
{79EB56DF-E29E-4AE2-A7D9-FE403FD919BA}.Release|x64.ActiveCfg = Release|x64 | |||
{79EB56DF-E29E-4AE2-A7D9-FE403FD919BA}.Release|x64.Build.0 = Release|x64 | |||
{79EB56DF-E29E-4AE2-A7D9-FE403FD919BA}.Release|x86.ActiveCfg = Release|Any CPU | |||
{79EB56DF-E29E-4AE2-A7D9-FE403FD919BA}.Release|x86.Build.0 = Release|Any CPU | |||
{3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
@@ -149,8 +149,8 @@ Global | |||
{3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3}.Debug|x86.Build.0 = Debug|Any CPU | |||
{3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3}.Release|x64.ActiveCfg = Release|Any CPU | |||
{3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3}.Release|x64.Build.0 = Release|Any CPU | |||
{3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3}.Release|x64.ActiveCfg = Release|x64 | |||
{3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3}.Release|x64.Build.0 = Release|x64 | |||
{3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3}.Release|x86.ActiveCfg = Release|Any CPU | |||
{3F5388FF-FBB4-462B-8F6F-829FFBAEB8A3}.Release|x86.Build.0 = Release|Any CPU | |||
EndGlobalSection | |||
@@ -289,7 +289,7 @@ namespace Tensorflow | |||
[DllImport(TensorFlowLibName)] | |||
public static extern IntPtr TF_LoadSessionFromSavedModel(SafeSessionOptionsHandle session_options, IntPtr run_options, | |||
string export_dir, string[] tags, int tags_len, | |||
IntPtr graph, ref TF_Buffer meta_graph_def, SafeStatusHandle status); | |||
IntPtr graph, IntPtr meta_graph_def, SafeStatusHandle status); | |||
[DllImport(TensorFlowLibName)] | |||
public static extern IntPtr TF_NewGraph(); | |||
@@ -36,6 +36,12 @@ namespace Tensorflow | |||
protected byte[] _target; | |||
public Graph graph => _graph; | |||
public BaseSession(IntPtr handle, Graph g) | |||
{ | |||
_handle = handle; | |||
_graph = g ?? ops.get_default_graph(); | |||
} | |||
public BaseSession(string target = "", Graph g = null, ConfigProto config = null, Status status = null) | |||
{ | |||
_graph = g ?? ops.get_default_graph(); | |||
@@ -291,12 +297,8 @@ namespace Tensorflow | |||
protected override void DisposeUnmanagedResources(IntPtr handle) | |||
{ | |||
lock (Locks.ProcessWide) | |||
using (var status = new Status()) | |||
{ | |||
c_api.TF_DeleteSession(handle, status.Handle); | |||
status.Check(true); | |||
} | |||
// c_api.TF_CloseSession(handle, tf.Status.Handle); | |||
c_api.TF_DeleteSession(handle, tf.Status.Handle); | |||
} | |||
} | |||
} |
@@ -26,10 +26,8 @@ namespace Tensorflow | |||
public Session(string target = "", Graph g = null) : base(target, g, null) | |||
{ } | |||
public Session(IntPtr handle, Graph g = null) : base("", g, null) | |||
{ | |||
_handle = handle; | |||
} | |||
public Session(IntPtr handle, Graph g = null) : base(handle, g) | |||
{ } | |||
public Session(Graph g, ConfigProto config = null, Status s = null) : base("", g, config, s) | |||
{ } | |||
@@ -39,51 +37,29 @@ namespace Tensorflow | |||
return ops.set_default_session(this); | |||
} | |||
[MethodImpl(MethodImplOptions.NoOptimization)] | |||
public static Session LoadFromSavedModel(string path) | |||
{ | |||
lock (Locks.ProcessWide) | |||
{ | |||
var graph = c_api.TF_NewGraph(); | |||
using var status = new Status(); | |||
var opt = new SessionOptions(); | |||
var tags = new string[] { "serve" }; | |||
var buffer = new TF_Buffer(); | |||
IntPtr sess; | |||
try | |||
{ | |||
sess = c_api.TF_LoadSessionFromSavedModel(opt.Handle, | |||
IntPtr.Zero, | |||
path, | |||
tags, | |||
tags.Length, | |||
graph, | |||
ref buffer, | |||
status.Handle); | |||
status.Check(true); | |||
} | |||
catch (TensorflowException ex) when (ex.Message.Contains("Could not find SavedModel")) | |||
{ | |||
sess = c_api.TF_LoadSessionFromSavedModel(opt.Handle, | |||
IntPtr.Zero, | |||
Path.GetFullPath(path), | |||
tags, | |||
tags.Length, | |||
graph, | |||
ref buffer, | |||
status.Handle); | |||
status.Check(true); | |||
} | |||
// load graph bytes | |||
// var data = new byte[buffer.length]; | |||
// Marshal.Copy(buffer.data, data, 0, (int)buffer.length); | |||
// var meta_graph = MetaGraphDef.Parser.ParseFrom(data);*/ | |||
return new Session(sess, g: new Graph(graph)).as_default(); | |||
} | |||
using var graph = new Graph(); | |||
using var status = new Status(); | |||
using var opt = c_api.TF_NewSessionOptions(); | |||
var tags = new string[] { "serve" }; | |||
var sess = c_api.TF_LoadSessionFromSavedModel(opt, | |||
IntPtr.Zero, | |||
path, | |||
tags, | |||
tags.Length, | |||
graph, | |||
IntPtr.Zero, | |||
status.Handle); | |||
status.Check(true); | |||
// load graph bytes | |||
// var data = new byte[buffer.length]; | |||
// Marshal.Copy(buffer.data, data, 0, (int)buffer.length); | |||
// var meta_graph = MetaGraphDef.Parser.ParseFrom(data);*/ | |||
return new Session(sess, g: graph); | |||
} | |||
public static implicit operator IntPtr(Session session) => session._handle; | |||
@@ -21,6 +21,18 @@ namespace Tensorflow | |||
{ | |||
public partial class c_api | |||
{ | |||
/// <summary> | |||
/// Close a session. | |||
/// | |||
/// Contacts any other processes associated with the session, if applicable. | |||
/// May not be called after TF_DeleteSession(). | |||
/// </summary> | |||
/// <param name="s"></param> | |||
/// <param name="status"></param> | |||
[DllImport(TensorFlowLibName)] | |||
public static extern void TF_CloseSession(IntPtr session, SafeStatusHandle status); | |||
/// <summary> | |||
/// Destroy a session object. | |||
/// | |||
@@ -0,0 +1,28 @@ | |||
using BenchmarkDotNet.Attributes; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
using System.Linq; | |||
using System.Reflection; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
using static Tensorflow.Binding; | |||
namespace Tensorflow.Benchmark.Leak | |||
{ | |||
public class SavedModelCleanup | |||
{ | |||
[Benchmark] | |||
public void Run() | |||
{ | |||
var modelDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | |||
var ClassifierModelPath = Path.Combine(modelDir, "Leak", "TestModel", "saved_model"); | |||
for (var i = 0; i < 1024; i++) | |||
{ | |||
using var sess = Session.LoadFromSavedModel(ClassifierModelPath); | |||
} | |||
} | |||
} | |||
} |
@@ -13,7 +13,9 @@ namespace TensorFlowBenchmark | |||
static void Main(string[] args) | |||
{ | |||
print(tf.VERSION); | |||
/*new RepeatDataSetCrash().Run(); | |||
/*new SavedModelCleanup().Run(); | |||
new RepeatDataSetCrash().Run(); | |||
new GpuLeakByCNN().Run();*/ | |||
if (args?.Length > 0) | |||
@@ -37,11 +37,27 @@ | |||
<ItemGroup> | |||
<PackageReference Include="BenchmarkDotNet" Version="0.13.0" /> | |||
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.6.0-rc0" /> | |||
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.6.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\TensorFlowNET.Keras\Tensorflow.Keras.csproj" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Folder Include="Leak\TestModel\saved_model\assets\" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<None Update="Leak\TestModel\saved_model\saved_model.pb"> | |||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |||
</None> | |||
<None Update="Leak\TestModel\saved_model\variables\variables.data-00000-of-00001"> | |||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |||
</None> | |||
<None Update="Leak\TestModel\saved_model\variables\variables.index"> | |||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |||
</None> | |||
</ItemGroup> | |||
</Project> |