From 460c04f802663d9cd0841ba0996423dea9fa6456 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 28 Nov 2021 09:44:19 -0600 Subject: [PATCH] make op_id as int --- .../Gradients/BackpropInitialState.cs | 5 +- src/TensorFlowNET.Core/Gradients/OpTape.cs | 2 +- .../Gradients/Tape.ComputeGradient.cs | 8 ++-- .../Gradients/Tape.PrepareBackprop.cs | 5 +- .../Gradients/Tape.RecordOperation.cs | 5 +- src/TensorFlowNET.Core/Gradients/Tape.cs | 2 +- .../Gradients/TensorTape.cs | 2 +- src/TensorFlowNET.Core/Operations/math_ops.cs | 8 ---- .../Tensorflow.Binding.csproj | 2 +- .../Tensors/c_api.tensor.cs | 47 +------------------ .../Tensorflow.Keras.csproj | 2 +- 11 files changed, 15 insertions(+), 73 deletions(-) diff --git a/src/TensorFlowNET.Core/Gradients/BackpropInitialState.cs b/src/TensorFlowNET.Core/Gradients/BackpropInitialState.cs index ec0e8484..eee98a61 100644 --- a/src/TensorFlowNET.Core/Gradients/BackpropInitialState.cs +++ b/src/TensorFlowNET.Core/Gradients/BackpropInitialState.cs @@ -1,5 +1,4 @@ using Tensorflow.Util; -using static Tensorflow.tensorflow; namespace Tensorflow.Gradients { @@ -15,13 +14,13 @@ namespace Tensorflow.Gradients /// Maps from op ID to how many output tensors of this op still need to have /// their gradients computed. /// - public UnorderedMap op_missing_tensor { get; set; } + public UnorderedMap op_missing_tensor { get; set; } public BackpropInitialState() { op_tape = new OpTape(); tensor_usage_counts = new UnorderedMap(); - op_missing_tensor = new UnorderedMap(); + op_missing_tensor = new UnorderedMap(); } } } diff --git a/src/TensorFlowNET.Core/Gradients/OpTape.cs b/src/TensorFlowNET.Core/Gradients/OpTape.cs index 61030e62..cb9d0de7 100644 --- a/src/TensorFlowNET.Core/Gradients/OpTape.cs +++ b/src/TensorFlowNET.Core/Gradients/OpTape.cs @@ -5,7 +5,7 @@ namespace Tensorflow.Gradients /// /// Map from operation-id to tape entry. /// - public class OpTape : UnorderedMap + public class OpTape : UnorderedMap { } diff --git a/src/TensorFlowNET.Core/Gradients/Tape.ComputeGradient.cs b/src/TensorFlowNET.Core/Gradients/Tape.ComputeGradient.cs index d955582b..0d0ecbe2 100644 --- a/src/TensorFlowNET.Core/Gradients/Tape.ComputeGradient.cs +++ b/src/TensorFlowNET.Core/Gradients/Tape.ComputeGradient.cs @@ -144,7 +144,7 @@ namespace Tensorflow.Gradients } var op_id = tape_it; - if (op_id == null) + if (op_id == -1) continue; if (state.op_missing_tensor.find(op_id, out var missing_it)) @@ -235,10 +235,10 @@ namespace Tensorflow.Gradients return result; } - Queue InitialStack(OpTape op_tape, - UnorderedMap op_missing_tensor) + Queue InitialStack(OpTape op_tape, + UnorderedMap op_missing_tensor) { - var result = new Queue(); + var result = new Queue(); foreach (var op_entry in op_tape) { if (!op_missing_tensor.find(op_entry.Key)) diff --git a/src/TensorFlowNET.Core/Gradients/Tape.PrepareBackprop.cs b/src/TensorFlowNET.Core/Gradients/Tape.PrepareBackprop.cs index dd1a1401..2ab4ddbb 100644 --- a/src/TensorFlowNET.Core/Gradients/Tape.PrepareBackprop.cs +++ b/src/TensorFlowNET.Core/Gradients/Tape.PrepareBackprop.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using Tensorflow.Util; -using static Tensorflow.tensorflow; namespace Tensorflow.Gradients { @@ -21,7 +20,7 @@ namespace Tensorflow.Gradients if (!tensor_tape.find(tensor_id, out var op_id)) continue; - if (op_id == null || + if (op_id == -1 || !op_tape.find(op_id, out var op_it) || result.op_tape.find(op_id, out var result_op_it)) continue; @@ -46,7 +45,7 @@ namespace Tensorflow.Gradients foreach (var pair in result.tensor_usage_counts) { - if (tensor_tape.find(pair.Key, out var it) && it != null) + if (tensor_tape.find(pair.Key, out var it) && it != -1) result.op_missing_tensor[it] += 1; } diff --git a/src/TensorFlowNET.Core/Gradients/Tape.RecordOperation.cs b/src/TensorFlowNET.Core/Gradients/Tape.RecordOperation.cs index c76d620c..a692f1f4 100644 --- a/src/TensorFlowNET.Core/Gradients/Tape.RecordOperation.cs +++ b/src/TensorFlowNET.Core/Gradients/Tape.RecordOperation.cs @@ -1,10 +1,7 @@ using System; using System.Collections.Generic; using Tensorflow.Util; -using static Tensorflow.tensorflow; using static Tensorflow.Binding; -using System.Linq; -using Tensorflow.Eager; namespace Tensorflow.Gradients { @@ -21,7 +18,7 @@ namespace Tensorflow.Gradients if (!ShouldRecord(input_tensors)) return; - var op_id = new EagerTensor(next_op_id_++); + var op_id = next_op_id_++; foreach (var i in input_tensors) tensor_usage_[i]++; diff --git a/src/TensorFlowNET.Core/Gradients/Tape.cs b/src/TensorFlowNET.Core/Gradients/Tape.cs index 982ffe1f..15caf81b 100644 --- a/src/TensorFlowNET.Core/Gradients/Tape.cs +++ b/src/TensorFlowNET.Core/Gradients/Tape.cs @@ -42,7 +42,7 @@ namespace Tensorflow.Gradients public void Watch(Tensor x) { tf.Logger.Debug($"Watch tensor id={x.Id}, name={x.name}"); - tensor_tape_.emplace(x, null); + tensor_tape_.emplace(x, -1); } public bool ShouldRecord(Tensor[] tensors) diff --git a/src/TensorFlowNET.Core/Gradients/TensorTape.cs b/src/TensorFlowNET.Core/Gradients/TensorTape.cs index de478bee..b9424f91 100644 --- a/src/TensorFlowNET.Core/Gradients/TensorTape.cs +++ b/src/TensorFlowNET.Core/Gradients/TensorTape.cs @@ -7,7 +7,7 @@ namespace Tensorflow.Gradients /// produced this tensor. A value of -1 means that the tensor was directly /// watched and not the result of any operation in the tape. /// - public class TensorTape : UnorderedMap + public class TensorTape : UnorderedMap { } diff --git a/src/TensorFlowNET.Core/Operations/math_ops.cs b/src/TensorFlowNET.Core/Operations/math_ops.cs index 3411308c..df960ad4 100644 --- a/src/TensorFlowNET.Core/Operations/math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/math_ops.cs @@ -656,9 +656,6 @@ namespace Tensorflow } else { - if (x.rank > -1 && tf.executing_eagerly()) - return constant_op.constant(np.arange(x.rank)); - var rank = array_ops.rank(x); return range(0, rank, 1); } @@ -678,11 +675,6 @@ namespace Tensorflow // we rely on Range and Rank to do the right thing at run-time. if (rank == -1) return range(0, array_ops.rank(x)); - if (rank.HasValue && rank.Value > -1) - { - return constant_op.constant(np.arange(rank.Value), TF_DataType.TF_INT32); - } - return range(0, rank, 1); } } diff --git a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj index 1e0a8c10..456aac61 100644 --- a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj +++ b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj @@ -6,7 +6,7 @@ Tensorflow 2.2.0 0.60.5 - 9.0 + 10.0 enable Haiping Chen, Meinrad Recheis, Eli Belash SciSharp STACK diff --git a/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs b/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs index 0af8859e..2e7edc66 100644 --- a/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs @@ -71,38 +71,7 @@ namespace Tensorflow /// /// [DllImport(TensorFlowLibName)] - public static extern IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, UIntPtr len, Deallocator deallocator, ref DeallocatorArgs deallocator_arg); - - [DllImport(TensorFlowLibName)] - public static extern TF_Tensor TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, long len, DeallocatorV2 deallocator, IntPtr args); - - /// - /// Return a new tensor that holds the bytes data[0,len-1] - /// - /// - /// - /// - /// - /// num_bytes, ex: 6 * sizeof(float) - /// - /// - /// - [DllImport(TensorFlowLibName)] - public static extern IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, ulong len, Deallocator deallocator, IntPtr deallocator_arg); - - /// - /// Return a new tensor that holds the bytes data[0,len-1] - /// - /// - /// - /// - /// - /// num_bytes, ex: 6 * sizeof(float) - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, ulong len) - { - return TF_NewTensor(dataType, dims, num_dims, data, len, EmptyDeallocator, DeallocatorArgs.Empty); - } + public static extern SafeTensorHandle TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, ulong len, Deallocator deallocator, IntPtr deallocator_arg); public static unsafe SafeTensorHandle TF_NewTensor(byte[] data, Shape shape, TF_DataType dtype) { @@ -137,20 +106,6 @@ namespace Tensorflow return handle; } - /// - /// Return a new tensor that holds the bytes data[0,len-1] - /// - /// - /// - /// - /// - /// num_bytes, ex: 6 * sizeof(float) - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, void* data, ulong len) - { - return TF_NewTensor(dataType, dims, num_dims, new IntPtr(data), len); - } - /// /// Return the number of dimensions that the tensor has. /// diff --git a/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj b/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj index 03d9ad99..a8e88d6c 100644 --- a/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj +++ b/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj @@ -3,7 +3,7 @@ netstandard2.0 Tensorflow.Keras - 9.0 + 10.0 enable Tensorflow.Keras AnyCPU;x64