From cca2111a17ccfe7b578f5630a8f648175530f9e7 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 27 Dec 2020 10:35:50 -0600 Subject: [PATCH] test GpuLeakByCNN. --- .../Functions/TapeGradientFunctions.cs | 3 ++- .../Leak/GpuLeakByCNN.cs | 21 ++++++++++--------- src/TensorFlowNet.Benchmarks/Program.cs | 3 +++ .../Tensorflow.Benchmark.csproj | 8 ++++++- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/TensorFlowNET.Core/Functions/TapeGradientFunctions.cs b/src/TensorFlowNET.Core/Functions/TapeGradientFunctions.cs index 89f87c62..45b0de26 100644 --- a/src/TensorFlowNET.Core/Functions/TapeGradientFunctions.cs +++ b/src/TensorFlowNET.Core/Functions/TapeGradientFunctions.cs @@ -97,7 +97,8 @@ namespace Tensorflow.Functions .ToArray(); foreach(var capture in captures_from_forward) { - _func_graph.Outputs.Add(capture); + if (!_func_graph.Outputs.Contains(capture)) + _func_graph.Outputs.Add(capture); } var forward_function_name = $"{_FORWARD_PREFIX}_{ops.uid()}"; diff --git a/src/TensorFlowNet.Benchmarks/Leak/GpuLeakByCNN.cs b/src/TensorFlowNet.Benchmarks/Leak/GpuLeakByCNN.cs index aec309a9..b581e590 100644 --- a/src/TensorFlowNet.Benchmarks/Leak/GpuLeakByCNN.cs +++ b/src/TensorFlowNet.Benchmarks/Leak/GpuLeakByCNN.cs @@ -6,14 +6,15 @@ using NumSharp; using Tensorflow.Keras; using static Tensorflow.Binding; using static Tensorflow.KerasApi; +using BenchmarkDotNet.Attributes; namespace Tensorflow.Benchmark.Leak { - class GpuLeakByCNN + public class GpuLeakByCNN { protected static LayersApi layers = new LayersApi(); - - public static void Test() + [Benchmark] + public void Run() { int num = 50, width = 64, height = 64; // if width = 128, height = 128, the exception occurs faster @@ -29,16 +30,16 @@ namespace Tensorflow.Benchmark.Leak tf.enable_eager_execution(); - var inputss = keras.Input((height, width, 3)); + var inputs = keras.Input((height, width, 3)); - var inputs = layers.Conv2D(32, (3, 3), activation: keras.activations.Relu).Apply(inputss); - inputs = layers.MaxPooling2D((2, 2)).Apply(inputs); + var layer = layers.Conv2D(32, (3, 3), activation: keras.activations.Relu).Apply(inputs); + layer = layers.MaxPooling2D((2, 2)).Apply(layer); - inputs = layers.Flatten().Apply(inputs); + layer = layers.Flatten().Apply(layer); - var outputs = layers.Dense(10).Apply(inputs); + var outputs = layers.Dense(10).Apply(layer); - var model = keras.Model(inputss, outputs, "gpuleak"); + var model = keras.Model(inputs, outputs, "gpuleak"); model.summary(); @@ -46,7 +47,7 @@ namespace Tensorflow.Benchmark.Leak optimizer: keras.optimizers.RMSprop(), metrics: new[] { "accuracy" }); - model.fit(inputImages, outLables, epochs: 200); + model.fit(inputImages, outLables, batch_size: 1, epochs: 200); } } } diff --git a/src/TensorFlowNet.Benchmarks/Program.cs b/src/TensorFlowNet.Benchmarks/Program.cs index 16269080..1894b1a8 100644 --- a/src/TensorFlowNet.Benchmarks/Program.cs +++ b/src/TensorFlowNet.Benchmarks/Program.cs @@ -2,6 +2,7 @@ using BenchmarkDotNet.Running; using System; using System.Reflection; +using Tensorflow.Benchmark.Leak; namespace TensorFlowBenchmark { @@ -9,6 +10,8 @@ namespace TensorFlowBenchmark { static void Main(string[] args) { + new GpuLeakByCNN().Run(); + if (args?.Length > 0) { for (int i = 0; i < args.Length; i++) diff --git a/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj b/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj index 9069f2ec..1160fa4f 100644 --- a/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj +++ b/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj @@ -23,13 +23,19 @@ true + + + + + + - +