From 153f8c445c7ddde286657f559de18a9f4690694a Mon Sep 17 00:00:00 2001 From: rikkitook Date: Wed, 17 Mar 2021 14:52:09 +0500 Subject: [PATCH 1/3] fix calls to Session.Run with different default graph --- src/TensorFlowNET.Core/Sessions/_FetchMapper.cs | 4 ++-- src/TensorFlowNET.Core/Sessions/_ListFetchMapper.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/TensorFlowNET.Core/Sessions/_FetchMapper.cs b/src/TensorFlowNET.Core/Sessions/_FetchMapper.cs index 1d5bf1b29..f52cbe60b 100644 --- a/src/TensorFlowNET.Core/Sessions/_FetchMapper.cs +++ b/src/TensorFlowNET.Core/Sessions/_FetchMapper.cs @@ -28,9 +28,9 @@ public static _FetchMapper for_fetch(object fetch, Graph graph = null) var fetches = fetch.GetType().IsArray ? (object[])fetch : new object[] { fetch }; if (fetch is List fetches1) - return new _ListFetchMapper(fetches1.ToArray()); + return new _ListFetchMapper(fetches1.ToArray(), graph: graph); if (fetch.GetType().IsArray) - return new _ListFetchMapper(fetches); + return new _ListFetchMapper(fetches, graph: graph); else return new _ElementFetchMapper(fetches, (List fetched_vals) => fetched_vals[0], graph: graph); } diff --git a/src/TensorFlowNET.Core/Sessions/_ListFetchMapper.cs b/src/TensorFlowNET.Core/Sessions/_ListFetchMapper.cs index f7b25ea58..1f3fdfa19 100644 --- a/src/TensorFlowNET.Core/Sessions/_ListFetchMapper.cs +++ b/src/TensorFlowNET.Core/Sessions/_ListFetchMapper.cs @@ -24,9 +24,9 @@ public class _ListFetchMapper : _FetchMapper { private _FetchMapper[] _mappers; - public _ListFetchMapper(object[] fetches) + public _ListFetchMapper(object[] fetches, Graph graph = null) { - _mappers = fetches.Select(fetch => _FetchMapper.for_fetch(fetch)).ToArray(); + _mappers = fetches.Select(fetch => _FetchMapper.for_fetch(fetch, graph: graph)).ToArray(); (_unique_fetches, _value_indices) = _uniquify_fetches(_mappers); } From 39740dd1974e6abc6e717b870c2e15404cf77c3e Mon Sep 17 00:00:00 2001 From: rikkitook Date: Wed, 17 Mar 2021 14:56:27 +0500 Subject: [PATCH 2/3] Remove Serilog from TensorflowNET.Binding, use Microsoft.Extensions.Logging.Abstractions instead. --- .../Eager/EagerRunner.RecordGradient.cs | 5 +++-- .../Functions/TapeGradientFunctions.cs | 5 +++-- .../Gradients/Tape.RecordOperation.cs | 3 ++- src/TensorFlowNET.Core/Gradients/Tape.cs | 5 +++-- .../Gradients/ops.gradient_function_mapping.cs | 5 +++-- src/TensorFlowNET.Core/Tensorflow.Binding.csproj | 2 +- src/TensorFlowNET.Core/tensorflow.cs | 11 ++++------- src/TensorFlowNET.Keras/Engine/Functional.cs | 7 ++++--- src/TensorFlowNET.Keras/Preprocessings/Tokenizer.cs | 1 - 9 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs b/src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs index d072306a7..ddcdd68c2 100644 --- a/src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs +++ b/src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Linq; using Tensorflow.Gradients; using static Tensorflow.Binding; @@ -40,7 +41,7 @@ public bool RecordGradient(string op_name, } if (!should_record) return should_record; - tf.Logger.Debug($"RecordGradient: op_name={op_name}"); + tf.Logger.LogDebug($"RecordGradient: op_name={op_name}"); Tensor[] op_outputs; #pragma warning disable CS0219 // Variable is assigned but its value is never used diff --git a/src/TensorFlowNET.Core/Functions/TapeGradientFunctions.cs b/src/TensorFlowNET.Core/Functions/TapeGradientFunctions.cs index b4356107e..f20d665b4 100644 --- a/src/TensorFlowNET.Core/Functions/TapeGradientFunctions.cs +++ b/src/TensorFlowNET.Core/Functions/TapeGradientFunctions.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -100,7 +101,7 @@ public void Record(Tensors flat_outputs, Tensors inference_args) break; } - tf.Logger.Debug($"Invoke backward function: {backward.Name}"); + tf.Logger.LogDebug($"Invoke backward function: {backward.Name}"); var gradients = backward.CallFlat(processed_args, remapped_captures); foreach (var unneeded_gradient_index in unneeded_gradients) diff --git a/src/TensorFlowNET.Core/Gradients/Tape.RecordOperation.cs b/src/TensorFlowNET.Core/Gradients/Tape.RecordOperation.cs index 7b0e51f22..d8931c66c 100644 --- a/src/TensorFlowNET.Core/Gradients/Tape.RecordOperation.cs +++ b/src/TensorFlowNET.Core/Gradients/Tape.RecordOperation.cs @@ -4,6 +4,7 @@ using static Tensorflow.tensorflow; using static Tensorflow.Binding; using System.Linq; +using Microsoft.Extensions.Logging; namespace Tensorflow.Gradients { @@ -37,7 +38,7 @@ public void RecordOperation(string op_type, foreach (var o in output_tensors) { tensor_tape_[o.GetID()] = op_id; - tf.Logger.Debug($"RecordOperation: tensor_tape_[{o.GetID()}] = {op_id}"); + tf.Logger.LogDebug($"RecordOperation: tensor_tape_[{o.GetID()}] = {op_id}"); tensor_usage_[o.GetID()] = 1; tensors.Add(o); } diff --git a/src/TensorFlowNET.Core/Gradients/Tape.cs b/src/TensorFlowNET.Core/Gradients/Tape.cs index 08cbc1da9..7b9447482 100644 --- a/src/TensorFlowNET.Core/Gradients/Tape.cs +++ b/src/TensorFlowNET.Core/Gradients/Tape.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using Tensorflow.Util; using static Tensorflow.Binding; @@ -43,7 +44,7 @@ public void Watch(long tensor_id) if (!CouldBackprop()) return; - tf.Logger.Debug($"Watch tensor_id={tensor_id}"); + tf.Logger.LogDebug($"Watch tensor_id={tensor_id}"); tensor_tape_.emplace(tensor_id, -1); } diff --git a/src/TensorFlowNET.Core/Gradients/ops.gradient_function_mapping.cs b/src/TensorFlowNET.Core/Gradients/ops.gradient_function_mapping.cs index 6de420371..921b3eb13 100644 --- a/src/TensorFlowNET.Core/Gradients/ops.gradient_function_mapping.cs +++ b/src/TensorFlowNET.Core/Gradients/ops.gradient_function_mapping.cs @@ -14,6 +14,7 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; @@ -49,14 +50,14 @@ public static void RegisterFromAssembly() RegisterGradientFunction(m.GetCustomAttribute().Name, (oper, out_grads) => { - tf.Logger.Debug($"Caculate Gradient: {oper.name} {m.Name}"); + tf.Logger.LogDebug($"Caculate Gradient: {oper.name} {m.Name}"); var results = g.InvokeMember(m.Name, BindingFlags.InvokeMethod, null, null, args: new object[] { oper, out_grads }) as Tensor[]; foreach (var result in results.Where(x => x != null)) - tf.Logger.Debug($"Gradient: {result.name} {result.TensorShape}"); + tf.Logger.LogDebug($"Gradient: {result.name} {result.TensorShape}"); return results; } ); diff --git a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj index 7c6e3e009..166203c44 100644 --- a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj +++ b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj @@ -87,8 +87,8 @@ tf.net 0.4x.x aligns with TensorFlow v2.4.1 native library. + - diff --git a/src/TensorFlowNET.Core/tensorflow.cs b/src/TensorFlowNET.Core/tensorflow.cs index 60b22f717..d569ae063 100644 --- a/src/TensorFlowNET.Core/tensorflow.cs +++ b/src/TensorFlowNET.Core/tensorflow.cs @@ -14,9 +14,9 @@ You may obtain a copy of the License at limitations under the License. ******************************************************************************/ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using System.Collections.Generic; -using Serilog; -using Serilog.Core; using Tensorflow.Contexts; using Tensorflow.Eager; using Tensorflow.Gradients; @@ -43,14 +43,11 @@ public partial class tensorflow : ITensorFlowObject public OpDefLibrary OpDefLib; public Context Context; public IEagerRunner Runner; - public Logger Logger; + public ILogger Logger; public tensorflow() { - Logger = new LoggerConfiguration() - .MinimumLevel.Error() - .WriteTo.Console() - .CreateLogger(); + Logger = NullLogger.Instance; Status = new Status(); Context = new Context(); diff --git a/src/TensorFlowNET.Keras/Engine/Functional.cs b/src/TensorFlowNET.Keras/Engine/Functional.cs index 78038cff4..6cd38bfee 100644 --- a/src/TensorFlowNET.Keras/Engine/Functional.cs +++ b/src/TensorFlowNET.Keras/Engine/Functional.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.Linq; using Tensorflow.Keras.ArgsDefinition; @@ -335,10 +336,10 @@ Tensors run_internal_graph(Tensors inputs, bool training = false, Tensors mask = var layer_inputs = node.MapArguments(tensor_dict); - tf.Logger.Debug($"Depth {depth}: {node.Layer}: {node.Layer.Name}"); + tf.Logger.LogDebug($"Depth {depth}: {node.Layer}: {node.Layer.Name}"); var outputs = node.Layer.Apply(layer_inputs, is_training: training); foreach (var output in outputs.Where(x => x != null)) - tf.Logger.Information($"Depth {depth}: {node.Layer}: {node.Layer.Name} {output.TensorShape}"); + tf.Logger.LogInformation($"Depth {depth}: {node.Layer}: {node.Layer.Name} {output.TensorShape}"); // Update tensor_dict for next input foreach (var (x_id, y) in zip(node.FlatOutputIds, outputs)) tensor_dict[x_id] = new Queue(Enumerable.Range(0, tensor_usage_count[x_id]).Select(x => y)); diff --git a/src/TensorFlowNET.Keras/Preprocessings/Tokenizer.cs b/src/TensorFlowNET.Keras/Preprocessings/Tokenizer.cs index 29cbec8e2..af2759bb2 100644 --- a/src/TensorFlowNET.Keras/Preprocessings/Tokenizer.cs +++ b/src/TensorFlowNET.Keras/Preprocessings/Tokenizer.cs @@ -1,5 +1,4 @@ using NumSharp; -using Serilog.Debugging; using System; using System.Collections.Generic; using System.Collections.Specialized; From c7b82a3b60b225b0a439b9714466b7ec6f502b8b Mon Sep 17 00:00:00 2001 From: rikkitook Date: Wed, 17 Mar 2021 14:58:41 +0500 Subject: [PATCH 3/3] BaseSession._call_tf_sessionrun use local status object instead of global --- .../Sessions/BaseSession.cs | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/TensorFlowNET.Core/Sessions/BaseSession.cs b/src/TensorFlowNET.Core/Sessions/BaseSession.cs index bfbe028cc..8536f1075 100644 --- a/src/TensorFlowNET.Core/Sessions/BaseSession.cs +++ b/src/TensorFlowNET.Core/Sessions/BaseSession.cs @@ -236,24 +236,25 @@ private unsafe NDArray[] _call_tf_sessionrun(KeyValuePair[] f // Ensure any changes to the graph are reflected in the runtime. _extend_graph(); - var status = tf.Status; - var output_values = fetch_list.Select(x => IntPtr.Zero).ToArray(); - c_api.TF_SessionRun(_handle, - run_options: null, - inputs: feed_dict.Select(f => f.Key).ToArray(), - input_values: feed_dict.Select(f => (IntPtr)f.Value).ToArray(), - ninputs: feed_dict.Length, - outputs: fetch_list, - output_values: output_values, - noutputs: fetch_list.Length, - target_opers: target_list.Select(f => (IntPtr)f).ToArray(), - ntargets: target_list.Count, - run_metadata: IntPtr.Zero, - status: status.Handle); - - status.Check(true); + using (var status = new Status()) + { + c_api.TF_SessionRun(_handle, + run_options: null, + inputs: feed_dict.Select(f => f.Key).ToArray(), + input_values: feed_dict.Select(f => (IntPtr)f.Value).ToArray(), + ninputs: feed_dict.Length, + outputs: fetch_list, + output_values: output_values, + noutputs: fetch_list.Length, + target_opers: target_list.Select(f => (IntPtr)f).ToArray(), + ntargets: target_list.Count, + run_metadata: IntPtr.Zero, + status: status.Handle); + + status.Check(true); + } var result = new NDArray[fetch_list.Length];