From d4f3df29cec43ac891efd1381ea5e94c2ae696a0 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 28 Feb 2021 15:49:38 -0600 Subject: [PATCH] optimize_dataset_v2 --- src/TensorFlowNET.Core/Data/DatasetV2.cs | 35 +++++++++++-------- src/TensorFlowNET.Core/Data/IDatasetV2.cs | 2 +- src/TensorFlowNET.Core/Data/ModelDataset.cs | 6 ++-- .../Data/OptimizeDataset.cs | 23 ++++++++---- .../Eager/EagerRunner.TFE_FastPathExecute.cs | 2 +- src/TensorFlowNET.Core/Eager/c_api.eager.cs | 2 +- .../{random_seed.py.cs => random_seed.cs} | 15 ++++++-- .../Operations/dataset_ops.cs | 17 ++++++++- .../Operations/gen_math_ops.cs | 14 ++------ .../Operations/gen_random_ops.cs | 4 +-- .../Operations/random_ops.cs | 2 +- 11 files changed, 80 insertions(+), 42 deletions(-) rename src/TensorFlowNET.Core/Framework/{random_seed.py.cs => random_seed.cs} (83%) diff --git a/src/TensorFlowNET.Core/Data/DatasetV2.cs b/src/TensorFlowNET.Core/Data/DatasetV2.cs index a8033802..11f57bfc 100644 --- a/src/TensorFlowNET.Core/Data/DatasetV2.cs +++ b/src/TensorFlowNET.Core/Data/DatasetV2.cs @@ -82,33 +82,40 @@ namespace Tensorflow public IDatasetV2 flat_map(Func map_func) => new FlatMapDataset(this, map_func); - public IDatasetV2 model(AutotuneAlgorithm algorithm, long cpu_budget) - => new ModelDataset(this, algorithm, cpu_budget); + public IDatasetV2 model(AutotuneAlgorithm algorithm, long cpu_budget, long ram_budget) + => new ModelDataset(this, algorithm, cpu_budget, ram_budget); public IDatasetV2 with_options(DatasetOptions options) => new OptionsDataset(this, options); public IDatasetV2 apply_options() { + IDatasetV2 dataset = this; // (1) Apply threading options + + // (2) Apply autotune options + var autotune = true; + long cpu_budget = 0; + long ram_budget = 0; + if (autotune) + dataset = dataset.model(AutotuneAlgorithm.HILL_CLIMB, cpu_budget, ram_budget); + + // (3) Apply graph rewrite options var graph_rewrites = new[] { - "map_and_batch_fusion", "noop_elimination", + "map_and_batch_fusion", "shuffle_and_repeat_fusion" }; + var graph_rewrite_configs = new string[] + { + "autotune_buffer_sizes:autotune:true", + "disable_prefetch_legacy_autotune:autotune:true", + "enable_gradient_descent:autotune:true", + "map_parallelization:autotune:true" + }; - var graph_rewrite_configs = new string[0]; - - // (2) Apply graph rewrite options - var dataset = optimize(graph_rewrites, graph_rewrite_configs); - - // (3) Apply autotune options - var autotune = true; - long cpu_budget = 0; - - if (autotune) - dataset = dataset.model(AutotuneAlgorithm.HILL_CLIMB, cpu_budget); + dataset = new OptimizeDataset(dataset, new string[0], new string[0], graph_rewrites, graph_rewrite_configs); // (4) Apply stats aggregator options diff --git a/src/TensorFlowNET.Core/Data/IDatasetV2.cs b/src/TensorFlowNET.Core/Data/IDatasetV2.cs index 9ce392d9..143b8f12 100644 --- a/src/TensorFlowNET.Core/Data/IDatasetV2.cs +++ b/src/TensorFlowNET.Core/Data/IDatasetV2.cs @@ -76,7 +76,7 @@ namespace Tensorflow IDatasetV2 flat_map(Func map_func); - IDatasetV2 model(AutotuneAlgorithm algorithm, long cpu_budget); + IDatasetV2 model(AutotuneAlgorithm algorithm, long cpu_budget, long ram_budget); IDatasetV2 with_options(DatasetOptions options); diff --git a/src/TensorFlowNET.Core/Data/ModelDataset.cs b/src/TensorFlowNET.Core/Data/ModelDataset.cs index 97a6ea24..1b01788c 100644 --- a/src/TensorFlowNET.Core/Data/ModelDataset.cs +++ b/src/TensorFlowNET.Core/Data/ModelDataset.cs @@ -9,14 +9,16 @@ namespace Tensorflow { public ModelDataset(IDatasetV2 input_dataset, AutotuneAlgorithm algorithm, - long cpu_budget) : + long cpu_budget, + long ram_budget) : base(input_dataset) { variant_tensor = ops.model_dataset(input_dataset.variant_tensor, output_types, output_shapes, algorithm, - cpu_budget); + cpu_budget, + ram_budget); } } } diff --git a/src/TensorFlowNET.Core/Data/OptimizeDataset.cs b/src/TensorFlowNET.Core/Data/OptimizeDataset.cs index ca3902b3..56f36388 100644 --- a/src/TensorFlowNET.Core/Data/OptimizeDataset.cs +++ b/src/TensorFlowNET.Core/Data/OptimizeDataset.cs @@ -8,19 +8,30 @@ namespace Tensorflow public class OptimizeDataset : UnaryUnchangedStructureDataset { public OptimizeDataset(IDatasetV2 dataset, - string[] optimizations = null, + string[] optimizations_enabled = null, + string[] optimizations_disabled = null, + string[] optimizations_default = null, string[] optimization_configs = null) : base(dataset) { - if (optimizations == null) - optimizations = new string[0]; + if (optimizations_enabled == null) + optimizations_enabled = new string[0]; + if (optimizations_disabled == null) + optimizations_disabled = new string[0]; + if (optimizations_default == null) + optimizations_default = new string[0]; if (optimization_configs == null) optimization_configs = new string[0]; - var _optimizations = tf.convert_to_tensor(optimizations, dtype: TF_DataType.TF_STRING, name: "optimizations"); - variant_tensor = ops.optimize_dataset( + var _optimizations_enabled = tf.convert_to_tensor(optimizations_enabled, dtype: TF_DataType.TF_STRING, name: "optimizations_enabled"); + var _optimizations_disabled = tf.convert_to_tensor(optimizations_disabled, dtype: TF_DataType.TF_STRING, name: "optimizations_disabled"); + var _optimizations_default = tf.convert_to_tensor(optimizations_default, dtype: TF_DataType.TF_STRING, name: "optimizations_default"); + + variant_tensor = ops.optimize_dataset_v2( _input_dataset.variant_tensor, - _optimizations, + _optimizations_enabled, + _optimizations_disabled, + _optimizations_default, output_types, output_shapes, optimization_configs: optimization_configs); diff --git a/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs b/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs index 479d2aa4..61677efb 100644 --- a/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs +++ b/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs @@ -295,7 +295,7 @@ namespace Tensorflow.Eager { 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); + c_api.TFE_OpSetAttrStringList(op, key, values3, values3.Select(x => Convert.ToUInt64(x.Length)).ToArray(), values3.Length); attr_list_sizes[key] = values3.Length; } else if (type == TF_AttrType.TF_ATTR_SHAPE && values is TensorShape[] values1) diff --git a/src/TensorFlowNET.Core/Eager/c_api.eager.cs b/src/TensorFlowNET.Core/Eager/c_api.eager.cs index 02fb8973..77a79661 100644 --- a/src/TensorFlowNET.Core/Eager/c_api.eager.cs +++ b/src/TensorFlowNET.Core/Eager/c_api.eager.cs @@ -252,7 +252,7 @@ namespace Tensorflow 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); + public static extern void TFE_OpSetAttrStringList(SafeOpHandle op, string attr_name, string[] values, ulong[] 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/random_seed.py.cs b/src/TensorFlowNET.Core/Framework/random_seed.cs similarity index 83% rename from src/TensorFlowNET.Core/Framework/random_seed.py.cs rename to src/TensorFlowNET.Core/Framework/random_seed.cs index 8732c030..bd962c74 100644 --- a/src/TensorFlowNET.Core/Framework/random_seed.py.cs +++ b/src/TensorFlowNET.Core/Framework/random_seed.cs @@ -67,8 +67,19 @@ namespace Tensorflow if (seed2 is null) _seed2 = constant_op.constant(0, dtype: TF_DataType.TF_INT64, name: "seed2"); else - _seed2 = constant_op.constant(seed2.Value, dtype: TF_DataType.TF_INT64, name: "seed2"); - + { + _seed2 = tf_with(ops.name_scope("seed2"), scope => + { + _seed2 = constant_op.constant(seed2.Value, dtype: TF_DataType.TF_INT64); + return array_ops.where_v2( + math_ops.logical_and( + math_ops.equal(_seed, 0l), math_ops.equal(_seed2, 0l)), + constant_op.constant(2^31 - 1, dtype: dtypes.int64), + _seed2, + name: scope); + }); + } + return (_seed, _seed2); } } diff --git a/src/TensorFlowNET.Core/Operations/dataset_ops.cs b/src/TensorFlowNET.Core/Operations/dataset_ops.cs index fcad0709..c350ba9e 100644 --- a/src/TensorFlowNET.Core/Operations/dataset_ops.cs +++ b/src/TensorFlowNET.Core/Operations/dataset_ops.cs @@ -168,6 +168,20 @@ namespace Tensorflow optimization_configs = optimization_configs ?? new string[0] })); + public Tensor optimize_dataset_v2(Tensor input_dataset, Tensor optimizations_enabled, + Tensor optimizations_disabled, Tensor optimizations_default, + TF_DataType[] output_types, TensorShape[] output_shapes, + string[] optimization_configs = null, + string name = null) + => tf.Context.ExecuteOp("OptimizeDatasetV2", name, new ExecuteOpArgs(input_dataset, + optimizations_enabled, optimizations_disabled, optimizations_default) + .SetAttributes(new + { + output_types, + output_shapes, + optimization_configs = optimization_configs ?? new string[0] + })); + /// /// Identity transformation that models performance. /// @@ -180,13 +194,14 @@ namespace Tensorflow /// public Tensor model_dataset(Tensor input_dataset, TF_DataType[] output_types, TensorShape[] output_shapes, - AutotuneAlgorithm algorithm, long cpu_budget, + AutotuneAlgorithm algorithm, long cpu_budget, long ram_budget, string name = null) => tf.Context.ExecuteOp("ModelDataset", name, new ExecuteOpArgs(input_dataset) .SetAttributes(new { algorithm, cpu_budget, + ram_budget, output_types, output_shapes })); diff --git a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs index f6775ad9..8b84e019 100644 --- a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs @@ -327,24 +327,16 @@ namespace Tensorflow => tf.Context.ExecuteOp("Log1p", name, new ExecuteOpArgs(x)); public static Tensor logical_and(Tensor x, Tensor y, string name = null) - => tf.OpDefLib._apply_op_helper("LogicalAnd", name, args: new { x, y }); + => tf.Context.ExecuteOp("LogicalAnd", name, new ExecuteOpArgs(x, y)); public static Tensor logical_and(bool x, bool y, string name = null) => tf.Context.ExecuteOp("LogicalAnd", name, new ExecuteOpArgs(x, y)); public static Tensor logical_not(Tensor x, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("LogicalNot", name, args: new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("LogicalNot", name, new ExecuteOpArgs(x)); public static Tensor logical_or(Tensor x, Tensor y, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("LogicalOr", name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("LogicalOr", name, new ExecuteOpArgs(x, y)); public static Tensor logical_xor(Tensor x, Tensor y, string name = "LogicalXor") { diff --git a/src/TensorFlowNET.Core/Operations/gen_random_ops.cs b/src/TensorFlowNET.Core/Operations/gen_random_ops.cs index 12d41bf2..7a554379 100644 --- a/src/TensorFlowNET.Core/Operations/gen_random_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_random_ops.cs @@ -77,10 +77,10 @@ namespace Tensorflow /// /// /// - public static Tensor random_shuffle(Tensor value, int seed = 0, int seed2 = 0, + public static Tensor random_shuffle(Tensor value, int? seed = 0, int? seed2 = 0, string name = null) => tf.Context.ExecuteOp("RandomShuffle", name, new ExecuteOpArgs(value) - .SetAttributes(new { seed = seed, seed2 = seed2 })); + .SetAttributes(new { seed = seed ?? 0, seed2 = seed2 ?? 0 })); /// /// Outputs random values from a truncated normal distribution. diff --git a/src/TensorFlowNET.Core/Operations/random_ops.cs b/src/TensorFlowNET.Core/Operations/random_ops.cs index e638011a..1523d23f 100644 --- a/src/TensorFlowNET.Core/Operations/random_ops.cs +++ b/src/TensorFlowNET.Core/Operations/random_ops.cs @@ -116,7 +116,7 @@ namespace Tensorflow public static Tensor random_shuffle(Tensor value, int? seed = null, string name = null) { var (seed1, seed2) = random_seed.get_seed(seed); - return gen_random_ops.random_shuffle(value, seed: seed1 ?? 0, seed2: seed2 ?? 0, name: name); + return gen_random_ops.random_shuffle(value, seed: seed1, seed2: seed2, name: name); } public static Tensor truncated_normal(int[] shape,