From 8048b6258a04f3a348d6f5eedf74003e21d07fa2 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 4 Jul 2021 08:37:23 -0500 Subject: [PATCH] Add RepeatDataSetCrash test. --- src/TensorFlowNET.Core/Data/DatasetV2.cs | 6 +++- src/TensorFlowNET.Keras/Engine/Layer.cs | 2 +- src/TensorFlowNET.Keras/Engine/Model.cs | 6 ++-- src/TensorFlowNET.Keras/Utils/Compress.cs | 3 ++ .../Crash/RepeatDataSetCrash.cs | 28 +++++++++++++++++++ src/TensorFlowNet.Benchmarks/Program.cs | 4 +++ .../Tensorflow.Benchmark.csproj | 5 ++-- 7 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 src/TensorFlowNet.Benchmarks/Crash/RepeatDataSetCrash.cs diff --git a/src/TensorFlowNET.Core/Data/DatasetV2.cs b/src/TensorFlowNET.Core/Data/DatasetV2.cs index ab07168f..b460501b 100644 --- a/src/TensorFlowNET.Core/Data/DatasetV2.cs +++ b/src/TensorFlowNET.Core/Data/DatasetV2.cs @@ -25,6 +25,8 @@ namespace Tensorflow public TensorSpec[] element_spec => structure; + public int length => cardinality().numpy(); + public IDatasetV2 cache(string filename = "") => new CacheDataset(this, filename: filename); @@ -136,7 +138,9 @@ namespace Tensorflow => tf.Context.ExecuteOp("DatasetCardinality", name, new ExecuteOpArgs(variant_tensor)); public override string ToString() - => $"{GetType().Name} shapes: {string.Join(", ", structure.Select(x => x.shape))}, types: {string.Join(", ", structure.Select(x => "tf." + x.dtype.as_numpy_name()))}"; + => $"{GetType().Name} shapes: {string.Join(", ", structure.Select(x => x.shape))}, " + + $"types: {string.Join(", ", structure.Select(x => "tf." + x.dtype.as_numpy_name()))}, " + + $"len: {length}"; public IEnumerator<(Tensor, Tensor)> GetEnumerator() { diff --git a/src/TensorFlowNET.Keras/Engine/Layer.cs b/src/TensorFlowNET.Keras/Engine/Layer.cs index 40ca550c..3bc8cd80 100644 --- a/src/TensorFlowNET.Keras/Engine/Layer.cs +++ b/src/TensorFlowNET.Keras/Engine/Layer.cs @@ -158,7 +158,7 @@ namespace Tensorflow.Keras.Engine /// protected virtual Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) { - throw new NotImplementedException(""); + return inputs; } protected virtual string _name_scope() diff --git a/src/TensorFlowNET.Keras/Engine/Model.cs b/src/TensorFlowNET.Keras/Engine/Model.cs index 3ac9cd38..9e38d59a 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.cs @@ -57,15 +57,15 @@ namespace Tensorflow.Keras.Engine void _init_batch_counters() { - _train_counter = tf.Variable(0, + _train_counter = tf.Variable(0L, dtype: TF_DataType.TF_INT64, aggregation: VariableAggregation.OnlyFirstReplica); - _test_counter = tf.Variable(0, + _test_counter = tf.Variable(0L, dtype: TF_DataType.TF_INT64, aggregation: VariableAggregation.OnlyFirstReplica); - _predict_counter = tf.Variable(0, + _predict_counter = tf.Variable(0L, dtype: TF_DataType.TF_INT64, aggregation: VariableAggregation.OnlyFirstReplica); } diff --git a/src/TensorFlowNET.Keras/Utils/Compress.cs b/src/TensorFlowNET.Keras/Utils/Compress.cs index a865d2ae..39710886 100644 --- a/src/TensorFlowNET.Keras/Utils/Compress.cs +++ b/src/TensorFlowNET.Keras/Utils/Compress.cs @@ -53,6 +53,9 @@ namespace Tensorflow.Keras.Utils var flag = gzArchiveName.Split(Path.DirectorySeparatorChar).Last().Split('.').First() + ".bin"; if (File.Exists(Path.Combine(destFolder, flag))) return; + var destFileName = gzArchiveName.Replace(".zip", string.Empty); + if (File.Exists(destFileName)) return; + Binding.tf_output_redirect.WriteLine($"Extracting."); var task = Task.Run(() => { diff --git a/src/TensorFlowNet.Benchmarks/Crash/RepeatDataSetCrash.cs b/src/TensorFlowNet.Benchmarks/Crash/RepeatDataSetCrash.cs new file mode 100644 index 00000000..90b04f93 --- /dev/null +++ b/src/TensorFlowNet.Benchmarks/Crash/RepeatDataSetCrash.cs @@ -0,0 +1,28 @@ +using BenchmarkDotNet.Attributes; +using System; +using System.Collections.Generic; +using NumSharp; +using static Tensorflow.Binding; +using static Tensorflow.KerasApi; + +namespace Tensorflow.Benchmark.Crash +{ + public class RepeatDataSetCrash + { + [Benchmark] + public void Run() + { + var data = tf.convert_to_tensor(np.arange(0, 50000 * 10).astype(np.float32).reshape(50000, 10)); + + var dataset = keras.preprocessing.timeseries_dataset_from_array(data, + sequence_length: 10, + sequence_stride: 1, + shuffle: false, + batch_size: 32); + + while (true) + foreach (var d in dataset) + ; + } + } +} diff --git a/src/TensorFlowNet.Benchmarks/Program.cs b/src/TensorFlowNet.Benchmarks/Program.cs index 1894b1a8..9d6f9d6f 100644 --- a/src/TensorFlowNet.Benchmarks/Program.cs +++ b/src/TensorFlowNet.Benchmarks/Program.cs @@ -2,7 +2,9 @@ using BenchmarkDotNet.Running; using System; using System.Reflection; +using Tensorflow.Benchmark.Crash; using Tensorflow.Benchmark.Leak; +using static Tensorflow.Binding; namespace TensorFlowBenchmark { @@ -10,6 +12,8 @@ namespace TensorFlowBenchmark { static void Main(string[] args) { + print(tf.VERSION); + new RepeatDataSetCrash().Run(); new GpuLeakByCNN().Run(); if (args?.Length > 0) diff --git a/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj b/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj index 419dc308..0b01acc6 100644 --- a/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj +++ b/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj @@ -14,6 +14,7 @@ true + DEBUG;TRACE @@ -35,8 +36,8 @@ - - + +