From 2efaaa8826e1f7c9bcc64b02c4e8ee04f6b6b24b Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sat, 25 Jul 2020 15:56:46 -0500 Subject: [PATCH] Add optimize_dataset to IDatasetV2. --- src/TensorFlowNET.Core/Binding.Util.cs | 3 + .../Eager/EagerRunner.TFE_FastPathExecute.cs | 16 +- src/TensorFlowNET.Core/Eager/c_api.eager.cs | 3 + .../Framework/Models/AutotuneAlgorithm.cs | 12 ++ .../Operations/dataset_ops.cs | 153 ++++++++++++++++++ 5 files changed, 180 insertions(+), 7 deletions(-) create mode 100644 src/TensorFlowNET.Core/Framework/Models/AutotuneAlgorithm.cs diff --git a/src/TensorFlowNET.Core/Binding.Util.cs b/src/TensorFlowNET.Core/Binding.Util.cs index d289a9dd..68c2319c 100644 --- a/src/TensorFlowNET.Core/Binding.Util.cs +++ b/src/TensorFlowNET.Core/Binding.Util.cs @@ -271,7 +271,10 @@ namespace Tensorflow foreach(var val in values) { if (i < start) + { + i++; continue; + } yield return (i, val); } } diff --git a/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs b/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs index d3da06e1..6bca3319 100644 --- a/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs +++ b/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs @@ -159,13 +159,9 @@ namespace Tensorflow.Eager if (op_exec_info.run_callbacks) { - if (!RunCallbacks( - op_exec_info, + RunCallbacks(op_exec_info, kFastPathExecuteInputStartIndex + op_def.InputArg.Count(), - flattened_inputs.ToArray(), flattened_attrs.ToArray(), flat_result)) - { - return null; - } + flattened_inputs.ToArray(), flattened_attrs.ToArray(), flat_result); } return flat_result; @@ -319,7 +315,12 @@ namespace Tensorflow.Eager Dictionary attr_list_sizes, Status status) { - if(type == TF_AttrType.TF_ATTR_SHAPE && values is TensorShape[] values1) + if (type == TF_AttrType.TF_ATTR_STRING && values is string[] values3) + { + c_api.TFE_OpSetAttrStringList(op, key, new IntPtr[0], values3.Select(x => x.Length).ToArray(), values3.Length); + attr_list_sizes[key] = values3.Length; + } + else if (type == TF_AttrType.TF_ATTR_SHAPE && values is TensorShape[] values1) { // Make one pass through the input counting the total number of // dims across all the input lists. @@ -340,6 +341,7 @@ namespace Tensorflow.Eager else if(type == TF_AttrType.TF_ATTR_TYPE && values is TF_DataType[] values2) { c_api.TFE_OpSetAttrTypeList(op, key, values2, values2.Length); + attr_list_sizes[key] = values2.Length; } else { diff --git a/src/TensorFlowNET.Core/Eager/c_api.eager.cs b/src/TensorFlowNET.Core/Eager/c_api.eager.cs index eb5bd510..bc215554 100644 --- a/src/TensorFlowNET.Core/Eager/c_api.eager.cs +++ b/src/TensorFlowNET.Core/Eager/c_api.eager.cs @@ -160,6 +160,9 @@ namespace Tensorflow [DllImport(TensorFlowLibName)] public static extern void TFE_OpSetAttrShapeList(SafeOpHandle op, string attr_name, IntPtr[] dims, int[] num_dims, int num_values, SafeStatusHandle out_status); + [DllImport(TensorFlowLibName)] + public static extern void TFE_OpSetAttrStringList(SafeOpHandle op, string attr_name, IntPtr[] values, int[] lengths, int num_values); + [DllImport(TensorFlowLibName)] public static extern void TFE_OpSetAttrBool(SafeOpHandle op, string attr_name, bool value); diff --git a/src/TensorFlowNET.Core/Framework/Models/AutotuneAlgorithm.cs b/src/TensorFlowNET.Core/Framework/Models/AutotuneAlgorithm.cs new file mode 100644 index 00000000..6600d6b0 --- /dev/null +++ b/src/TensorFlowNET.Core/Framework/Models/AutotuneAlgorithm.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Framework.Models +{ + public enum AutotuneAlgorithm + { + HILL_CLIMB = 0, + GRADIENT_DESCENT = 1, + } +} diff --git a/src/TensorFlowNET.Core/Operations/dataset_ops.cs b/src/TensorFlowNET.Core/Operations/dataset_ops.cs index b3f59824..514208e2 100644 --- a/src/TensorFlowNET.Core/Operations/dataset_ops.cs +++ b/src/TensorFlowNET.Core/Operations/dataset_ops.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Framework.Models; using static Tensorflow.Binding; namespace Tensorflow @@ -174,5 +175,157 @@ namespace Tensorflow throw new NotImplementedException(""); } + + /// + /// Creates a dataset by applying optimizations to `input_dataset`. + /// + /// + /// + /// + /// + /// + /// + /// + public Tensor optimize_dataset(Tensor input_dataset, Tensor optimizations, + TF_DataType[] output_types, TensorShape[] output_shapes, + string[] optimization_configs = null, + string name = null) + { + if (optimization_configs == null) + optimization_configs = new string[0]; + + if (tf.context.executing_eagerly()) + { + var results = tf.Runner.TFE_FastPathExecute(tf.context, tf.context.device_name, + "OptimizeDataset", name, + null, + input_dataset, optimizations, + "output_types", output_types, + "output_shapes", output_shapes, + "optimization_configs", optimization_configs); + return results[0]; + } + + throw new NotImplementedException(""); + } + + /// + /// Identity transformation that models performance. + /// + /// + /// + /// + /// + /// + /// + /// + public Tensor model_dataset(Tensor input_dataset, + TF_DataType[] output_types, TensorShape[] output_shapes, + AutotuneAlgorithm algorithm, long cpu_budget, + string name = null) + { + if (tf.context.executing_eagerly()) + { + var results = tf.Runner.TFE_FastPathExecute(tf.context, tf.context.device_name, + "ModelDataset", name, + null, + input_dataset, + "algorithm", algorithm, + "cpu_budget", cpu_budget, + "output_types", output_types, + "output_shapes", output_shapes); + return results[0]; + } + + throw new NotImplementedException(""); + } + + /// + /// A container for an iterator resource. + /// + /// + /// + /// + /// A tuple of `Tensor` objects (handle, deleter). + public (Tensor, Tensor) anonymous_iterator_v2(TF_DataType[] output_types, TensorShape[] output_shapes, string name = null) + { + if (tf.context.executing_eagerly()) + { + var results = tf.Runner.TFE_FastPathExecute(tf.context, tf.context.device_name, + "AnonymousIteratorV2", name, + null, + "output_types", output_types, + "output_shapes", output_shapes); + return (results[0], results[1]); + } + + throw new NotImplementedException(""); + } + + /// + /// Makes a new iterator from the given `dataset` and stores it in `iterator`. + /// + /// + /// + /// + /// The created Operation. + public ITensorOrOperation make_iterator(Tensor dataset, Tensor iterator, string name = null) + { + if (tf.context.executing_eagerly()) + { + var results = tf.Runner.TFE_FastPathExecute(tf.context, tf.context.device_name, + "MakeIterator", name, + null, + dataset, iterator); + return null; + } + + throw new NotImplementedException(""); + } + + /// + /// A container for an iterator resource. + /// + /// + /// + /// + /// The created Operation. + public ITensorOrOperation delete_iterator(Tensor handle, Tensor deleter, string name = null) + { + if (tf.context.executing_eagerly()) + { + var results = tf.Runner.TFE_FastPathExecute(tf.context, tf.context.device_name, + "DeleteIterator", name, + null, + handle, deleter); + return null; + } + + throw new NotImplementedException(""); + } + + /// + /// Gets the next output from the given iterator . + /// + /// + /// + /// + /// + /// + public Tensor[] iterator_get_next(Tensor iterator, TF_DataType[] output_types, TensorShape[] output_shapes, string name = null) + { + if (tf.context.executing_eagerly()) + { + var results = tf.Runner.TFE_FastPathExecute(tf.context, tf.context.device_name, + "IteratorGetNext", name, + null, + iterator, + "output_types", output_types, + "output_shapes", output_shapes); + return results; + } + + throw new NotImplementedException(""); + } } }