From 822e6d787ce825cd750d01cc4c7bbd34b6dc9634 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sat, 20 Feb 2021 11:37:55 -0600 Subject: [PATCH] Consolidate TFE_FastPathExecute and _apply_op_helper. --- src/TensorFlowNET.Console/MemoryBasicTest.cs | 44 +- src/TensorFlowNET.Core/APIs/tf.linalg.cs | 34 +- .../Contexts/AutoModeArgs.cs | 13 - ...ntext.AutoMode.cs => Context.ExecuteOp.cs} | 40 +- src/TensorFlowNET.Core/Contexts/Context.cs | 5 +- .../Contexts/ExecuteOpArgs.cs | 25 + src/TensorFlowNET.Core/Data/DatasetV2.cs | 13 +- .../Eager/EagerRunner.TFE_FastPathExecute.cs | 76 +- .../Eager/FastPathOpExecInfo.cs | 15 +- src/TensorFlowNET.Core/Eager/IEagerRunner.cs | 15 +- src/TensorFlowNET.Core/Gradients/math_grad.cs | 16 +- .../Operations/NnOps/gen_nn_ops.cs | 454 ++--------- .../Operations/OpDefLibrary.cs | 8 +- .../Operations/array_ops.cs | 86 +- .../Operations/bitwise_ops.cs | 31 +- .../Operations/dataset_ops.cs | 447 ++--------- .../Operations/gen_array_ops.cs | 315 +------- .../Operations/gen_image_ops.cs | 108 +-- .../Operations/gen_logging_ops.cs | 5 +- .../Operations/gen_math_ops.cs | 734 ++---------------- .../Operations/gen_math_ops.eager.cs | 9 +- .../Operations/gen_random_ops.cs | 99 +-- .../Operations/gen_resource_variable_ops.cs | 87 +-- src/TensorFlowNET.Core/Operations/math_ops.cs | 36 +- .../Operations/string_ops.cs | 73 +- .../Training/gen_training_ops.cs | 66 +- .../Variables/gen_state_ops.py.cs | 27 +- .../Activations/Activations.Relu.cs | 18 +- .../Activations/Activations.Sigmoid.cs | 18 +- .../Activations/Activations.Tanh.cs | 18 +- 30 files changed, 532 insertions(+), 2403 deletions(-) delete mode 100644 src/TensorFlowNET.Core/Contexts/AutoModeArgs.cs rename src/TensorFlowNET.Core/Contexts/{Context.AutoMode.cs => Context.ExecuteOp.cs} (76%) create mode 100644 src/TensorFlowNET.Core/Contexts/ExecuteOpArgs.cs diff --git a/src/TensorFlowNET.Console/MemoryBasicTest.cs b/src/TensorFlowNET.Console/MemoryBasicTest.cs index bbb23391..9586fe4e 100644 --- a/src/TensorFlowNET.Console/MemoryBasicTest.cs +++ b/src/TensorFlowNET.Console/MemoryBasicTest.cs @@ -112,16 +112,18 @@ namespace Tensorflow var strides = new[] { 1, 1, 1, 1 }; var dilations = new[] { 1, 1, 1, 1 }; - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Conv2D", null, - null, - input, filter, - "strides", strides, - "use_cudnn_on_gpu", true, - "padding", "VALID", - "explicit_paddings", new int[0], - "data_format", "NHWC", - "dilations", dilations); + var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("Conv2D", null, input, filter) + { + attrs = ConvertToDict(new + { + strides, + use_cudnn_on_gpu = true, + padding = "VALID", + explicit_paddings = new int[0], + data_format = "NHWC", + dilations + }) + }); }; public Action Conv2DWithVariable @@ -132,16 +134,18 @@ namespace Tensorflow var strides = new[] { 1, 1, 1, 1 }; var dilations = new[] { 1, 1, 1, 1 }; - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Conv2D", null, - null, - input, filter, - "strides", strides, - "use_cudnn_on_gpu", true, - "padding", "VALID", - "explicit_paddings", new int[0], - "data_format", "NHWC", - "dilations", dilations); + var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("Conv2D", null, input, filter) + { + attrs = ConvertToDict(new + { + strides, + use_cudnn_on_gpu = true, + padding = "VALID", + explicit_paddings = new int[0], + data_format = "NHWC", + dilations + }) + }); }; public Action Dataset diff --git a/src/TensorFlowNET.Core/APIs/tf.linalg.cs b/src/TensorFlowNET.Core/APIs/tf.linalg.cs index bf8c358c..e6968c8c 100644 --- a/src/TensorFlowNET.Core/APIs/tf.linalg.cs +++ b/src/TensorFlowNET.Core/APIs/tf.linalg.cs @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. ******************************************************************************/ +using static Tensorflow.Binding; namespace Tensorflow { @@ -37,8 +38,8 @@ namespace Tensorflow public Tensor matmul(Tensor a, Tensor b) => math_ops.matmul(a, b); - public Tensor batch_matmul(Tensor x, Tensor y) - => gen_math_ops.batch_mat_mul(x, y); + public Tensor batch_matmul(Tensor x, Tensor y, bool adj_x = false, bool adj_y = false, string name = null) + => tf.Context.ExecuteOp("BatchMatMul", name, new ExecuteOpArgs(x, y).SetAttributes(new { adj_x, adj_y })); } public Tensor diag(Tensor diagonal, string name = null) @@ -47,7 +48,32 @@ namespace Tensorflow public Tensor matmul(Tensor a, Tensor b) => math_ops.matmul(a, b); - public Tensor batch_matmul(Tensor x, Tensor y) - => gen_math_ops.batch_mat_mul(x, y); + /// + /// Multiply slices of the two matrices "x" and "y". + /// + /// + /// The `BatchMatMul` operation is embedded into the + /// `MatMul` operation on the DLL side. However the expected + /// attributes are not the same, hence we need to expose this + /// method to have the right args list on the `_apply_op_helper` + /// function. + /// + /// For each rank > 2 the first rank - 2 dimensions are considered + /// as fixed, and have to be consistent across the two matrices. A + /// common matrix multiplication is then applied over the residual + /// 2 dimensions. + /// + /// e.g. + /// x is (3, 6, 12); y is (3, 12, 6) + /// batch_matmul(x, y) ==> (3, 6, 6) + /// + /// + /// + /// + /// + /// + /// + public Tensor batch_matmul(Tensor x, Tensor y, bool adj_x = false, bool adj_y = false, string name = null) + => tf.Context.ExecuteOp("BatchMatMul", name, new ExecuteOpArgs(x, y).SetAttributes(new { adj_x, adj_y })); } } diff --git a/src/TensorFlowNET.Core/Contexts/AutoModeArgs.cs b/src/TensorFlowNET.Core/Contexts/AutoModeArgs.cs deleted file mode 100644 index 4fcd4bf0..00000000 --- a/src/TensorFlowNET.Core/Contexts/AutoModeArgs.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Tensorflow -{ - public class AutoModeArgs - { - public Func GetGradientAttrs { get; set; } - public object OpInputArgs { get; set; } - public object OpAttrs { get; set; } - } -} diff --git a/src/TensorFlowNET.Core/Contexts/Context.AutoMode.cs b/src/TensorFlowNET.Core/Contexts/Context.ExecuteOp.cs similarity index 76% rename from src/TensorFlowNET.Core/Contexts/Context.AutoMode.cs rename to src/TensorFlowNET.Core/Contexts/Context.ExecuteOp.cs index ee5d0765..def787a9 100644 --- a/src/TensorFlowNET.Core/Contexts/Context.AutoMode.cs +++ b/src/TensorFlowNET.Core/Contexts/Context.ExecuteOp.cs @@ -30,37 +30,35 @@ namespace Tensorflow.Contexts public sealed partial class Context { // [DebuggerStepThrough] - public Tensors ExecuteOp(string OpType, string Name, AutoModeArgs args) + public Tensors ExecuteOp(string OpType, string Name, ExecuteOpArgs args) { - var inputArgs = ConvertToDict(args.OpInputArgs); - var attrDict = ConvertToDict(args.OpAttrs); - Func graphAction = () => { - foreach (var attr in attrDict) - inputArgs[attr.Key] = attr.Value; - return tf.OpDefLib._apply_op_helper(OpType, Name, inputArgs).outputs; + var keywords = new Dictionary(); + if(args.OpInputArgs != null) + { + foreach (var (i, input) in enumerate(args.OpInputArgs)) + keywords[$"input_{i}"] = input; + } + + if(args.OpAttrs != null) + { + foreach (var attr in args.OpAttrs) + keywords[attr.Key] = attr.Value; + } + + return tf.OpDefLib._apply_op_helper(OpType, Name, keywords).outputs; }; Func eagerAction = () => { - var attrs = new object[attrDict.Count() * 2]; - int i = 0; - foreach(var arg in attrDict) + return tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(OpType, Name, args.OpInputArgs) { - attrs[i]= arg.Key; - attrs[i + 1] = arg.Value; - i += 2; - } - - return tf.Runner.TFE_FastPathExecute2(tf.Context, tf.Context.DeviceName, - OpType, Name, - null, - inputArgs.Values.ToArray(), - attrs); + attrs = args.OpAttrs + }); }; - if (tf.Context.has_graph_arg(inputArgs.Values)) + if (tf.Context.has_graph_arg(args.OpInputArgs)) { if (executing_eagerly()) { diff --git a/src/TensorFlowNET.Core/Contexts/Context.cs b/src/TensorFlowNET.Core/Contexts/Context.cs index be4b56b2..4d894702 100644 --- a/src/TensorFlowNET.Core/Contexts/Context.cs +++ b/src/TensorFlowNET.Core/Contexts/Context.cs @@ -115,7 +115,10 @@ namespace Tensorflow.Contexts public bool has_graph_arg(params object[] args) { var flatten_args = nest.flatten(args); - bool has_graph_arg = false; + /*if (flatten_args.Count(x => x.GetType().IsValueType) == flatten_args.Count()) + return tf.Context.executing_eagerly() == false*/ + + bool has_graph_arg = !tf.Context.executing_eagerly(); foreach (var el in flatten_args) { if (el is Tensor tensor && !tensor.IsEagerTensor) diff --git a/src/TensorFlowNET.Core/Contexts/ExecuteOpArgs.cs b/src/TensorFlowNET.Core/Contexts/ExecuteOpArgs.cs new file mode 100644 index 00000000..ecdcff8e --- /dev/null +++ b/src/TensorFlowNET.Core/Contexts/ExecuteOpArgs.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; +using static Tensorflow.Binding; + +namespace Tensorflow +{ + public class ExecuteOpArgs + { + public Func GetGradientAttrs { get; set; } + public object[] OpInputArgs { get; set; } + public Dictionary OpAttrs { get; set; } + + public ExecuteOpArgs(params object[] inputArgs) + { + OpInputArgs = inputArgs; + } + + public ExecuteOpArgs SetAttributes(object attrs) + { + OpAttrs = ConvertToDict(attrs); + return this; + } + } +} diff --git a/src/TensorFlowNET.Core/Data/DatasetV2.cs b/src/TensorFlowNET.Core/Data/DatasetV2.cs index 0297eb6b..c4298bc2 100644 --- a/src/TensorFlowNET.Core/Data/DatasetV2.cs +++ b/src/TensorFlowNET.Core/Data/DatasetV2.cs @@ -105,18 +105,7 @@ namespace Tensorflow } public Tensor dataset_cardinality(string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "DatasetCardinality", name, - null, - variant_tensor); - return results[0]; - } - - throw new NotImplementedException(""); - } + => 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()))}"; diff --git a/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs b/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs index a7d8503a..479d2aa4 100644 --- a/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs +++ b/src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs @@ -15,84 +15,54 @@ namespace Tensorflow.Eager /// public partial class EagerRunner { - int kFastPathExecuteInputStartIndex = 0; UnorderedMap thread_local_eager_operation_map = new UnorderedMap(); - public Tensor[] TFE_FastPathExecute2(Context ctx, - string device_name, - string opName, - string name, - Action callbacks, - object[] inputArgs, - object[] attrs) + public Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info) { - var args = new List(); - args.AddRange(inputArgs); - if (attrs != null) - args.AddRange(attrs); - return TFE_FastPathExecute(ctx, device_name, opName, name, callbacks, args.ToArray()); - } - - public Tensor[] TFE_FastPathExecute(Context ctx, - string device_name, - string opName, - string name, - Action callbacks, - params object[] args) - { - if (ctx == null) - throw new ValueError("This function does not handle the case of the path where " + - "all inputs are not already EagerTensors."); + if (op_exec_info.ctx == null) + op_exec_info.ctx = tf.Context; + if (string.IsNullOrEmpty(op_exec_info.device_name)) + op_exec_info.device_name = tf.Context.DeviceName; - int args_size = args.Length; var attr_list_sizes = new Dictionary(); - FastPathOpExecInfo op_exec_info = new FastPathOpExecInfo() - { - ctx = ctx, - args = args, - device_name = device_name, - op_name = opName, - name = name, - }; - op_exec_info.run_gradient_callback = HasAccumulatorOrTape(); - op_exec_info.run_post_exec_callbacks = callbacks != null; + op_exec_info.run_post_exec_callbacks = op_exec_info.callbacks != null; op_exec_info.run_callbacks = op_exec_info.run_gradient_callback || op_exec_info.run_post_exec_callbacks; var status = tf.Status; - using var op = GetOp(ctx, opName, status); + using var op = GetOp(op_exec_info.ctx, op_exec_info.op_name, status); - var op_def = tf.get_default_graph().GetOpDef(opName); + var op_def = tf.get_default_graph().GetOpDef(op_exec_info.op_name); var flattened_attrs = new List(op_def.Attr.Count * 2); var flattened_inputs = new List(op_def.InputArg.Count); // Set non-inferred attrs, including setting defaults if the attr is passed in // as None. - for (int i = kFastPathExecuteInputStartIndex + op_def.InputArg.Count; i < args_size; i += 2) + if(op_exec_info.attrs != null) { - var attr_name = args[i].ToString(); - var attr_value = args[i + 1]; - - var attr = op_def.Attr.FirstOrDefault(x => x.Name == attr_name); - if (attr != null) + foreach (var attr1 in op_exec_info.attrs) { - flattened_attrs.Add(attr_name); - flattened_attrs.Add(attr_value); + var attr = op_def.Attr.FirstOrDefault(x => x.Name == attr1.Key); + if (attr != null) + { + flattened_attrs.Add(attr.Name); + flattened_attrs.Add(attr1.Value); - SetOpAttrWithDefaults(ctx, op, attr, attr_name, attr_value, attr_list_sizes, status); - status.Check(true); + SetOpAttrWithDefaults(op_exec_info.ctx, op, attr, attr.Name, attr1.Value, attr_list_sizes, status); + status.Check(true); + } } } - c_api.TFE_OpSetDevice(op, device_name, status.Handle); + c_api.TFE_OpSetDevice(op, op_exec_info.device_name, status.Handle); status.Check(true); // Add inferred attrs and inputs. for (int i = 0; i < op_def.InputArg.Count; i++) { - var input = args[kFastPathExecuteInputStartIndex + i]; + var input = op_exec_info.args[i]; var input_arg = op_def.InputArg[i]; if (!string.IsNullOrEmpty(input_arg.NumberAttr)) { @@ -107,7 +77,7 @@ namespace Tensorflow.Eager if (len > 0) { - var fast_input_array = (object[])args[i]; + var fast_input_array = (object[])op_exec_info.args[i]; // First item adds the type attr. if (!AddInputToOp(fast_input_array[i], true, input_arg, flattened_attrs, flattened_inputs, op, status)) return null; @@ -151,7 +121,7 @@ namespace Tensorflow.Eager else { // The item is a single item. - AddInputToOp(args[i], true, input_arg, flattened_attrs, flattened_inputs, op, status); + AddInputToOp(op_exec_info.args[i], true, input_arg, flattened_attrs, flattened_inputs, op, status); } } @@ -179,7 +149,7 @@ namespace Tensorflow.Eager if (op_exec_info.run_callbacks) { RunCallbacks(op_exec_info, - kFastPathExecuteInputStartIndex + op_def.InputArg.Count(), + op_def.InputArg.Count(), flattened_inputs.ToArray(), flattened_attrs.ToArray(), flat_result); } diff --git a/src/TensorFlowNET.Core/Eager/FastPathOpExecInfo.cs b/src/TensorFlowNET.Core/Eager/FastPathOpExecInfo.cs index 654c25b2..2cdf025a 100644 --- a/src/TensorFlowNET.Core/Eager/FastPathOpExecInfo.cs +++ b/src/TensorFlowNET.Core/Eager/FastPathOpExecInfo.cs @@ -1,6 +1,8 @@ -using Tensorflow.Contexts; +using System; +using System.Collections.Generic; +using Tensorflow.Contexts; -namespace Tensorflow.Eager +namespace Tensorflow { public class FastPathOpExecInfo { @@ -9,8 +11,17 @@ namespace Tensorflow.Eager public string op_name { get; set; } public string name { get; set; } public object[] args { get; set; } + public Dictionary attrs { get; set; } public bool run_gradient_callback { get; set; } public bool run_post_exec_callbacks { get; set; } public bool run_callbacks { get; set; } + public Action callbacks { get; set; } + + public FastPathOpExecInfo(string opName, string name, params object[] inputArgs) + { + this.op_name = opName; + this.name = name; + this.args = inputArgs; + } } } diff --git a/src/TensorFlowNET.Core/Eager/IEagerRunner.cs b/src/TensorFlowNET.Core/Eager/IEagerRunner.cs index c1fb8607..38202af6 100644 --- a/src/TensorFlowNET.Core/Eager/IEagerRunner.cs +++ b/src/TensorFlowNET.Core/Eager/IEagerRunner.cs @@ -16,20 +16,7 @@ namespace Tensorflow.Eager TF_DataType default_dtype = TF_DataType.DtInvalid, object[] args = null); - Tensor[] TFE_FastPathExecute2(Context ctx, - string device_name, - string opName, - string name, - Action callbacks, - object[] inputArgs, - object[] attrs); - - Tensor[] TFE_FastPathExecute(Context ctx, - string device_name, - string opName, - string name, - Action callbacks, - params object[] args); + Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info); Tensor[] TFE_Execute(Context ctx, string device_name, diff --git a/src/TensorFlowNET.Core/Gradients/math_grad.cs b/src/TensorFlowNET.Core/Gradients/math_grad.cs index 2d0d7d28..a071d234 100644 --- a/src/TensorFlowNET.Core/Gradients/math_grad.cs +++ b/src/TensorFlowNET.Core/Gradients/math_grad.cs @@ -291,23 +291,23 @@ namespace Tensorflow.Gradients var b = math_ops.conj(op.inputs[1]); if (!t_a && !t_b) { - grad_a = gen_math_ops.batch_mat_mul(grad, b, adj_y: true); - grad_b = gen_math_ops.batch_mat_mul(a, grad, adj_x: true); + grad_a = math_ops.batch_matmul(grad, b, adj_y: true); + grad_b = math_ops.batch_matmul(a, grad, adj_x: true); } else if (!t_a && t_b) { - grad_a = gen_math_ops.batch_mat_mul(grad, b); - grad_b = gen_math_ops.batch_mat_mul(grad, a, adj_x: true); + grad_a = math_ops.batch_matmul(grad, b); + grad_b = math_ops.batch_matmul(grad, a, adj_x: true); } else if (t_a && !t_b) { - grad_a = gen_math_ops.batch_mat_mul(grad, b); - grad_b = gen_math_ops.batch_mat_mul(grad, a, adj_x: true); + grad_a = math_ops.batch_matmul(grad, b); + grad_b = math_ops.batch_matmul(grad, a, adj_x: true); } else if (t_a && t_b) { - grad_a = gen_math_ops.batch_mat_mul(b, grad, adj_x: true, adj_y: true); - grad_b = gen_math_ops.batch_mat_mul(grad, a, adj_x: true, adj_y: true); + grad_a = math_ops.batch_matmul(b, grad, adj_x: true, adj_y: true); + grad_b = math_ops.batch_matmul(grad, a, adj_x: true, adj_y: true); } return new Tensor[] { grad_a, grad_b }; diff --git a/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs b/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs index bc3cc735..346ba2dd 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs @@ -40,37 +40,16 @@ namespace Tensorflow.Operations /// /// public static Tensor conv2d(Conv2dParams parameters) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Conv2D", parameters.Name, - null, - parameters.Input, parameters.Filter, - "strides", parameters.Strides, - "use_cudnn_on_gpu", parameters.UseCudnnOnGpu, - "padding", parameters.Padding, - "explicit_paddings", parameters.ExplicitPaddings, - "data_format", parameters.DataFormat, - "dilations", parameters.Dilations); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Conv2D", name: parameters.Name, args: new - { - input = parameters.Input, - filter = parameters.Filter, - strides = parameters.Strides, - padding = parameters.Padding, - use_cudnn_on_gpu = parameters.UseCudnnOnGpu, - explicit_paddings = parameters.ExplicitPaddings, - data_format = parameters.DataFormat, - dilations = parameters.Dilations - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Conv2D", parameters.Name, new ExecuteOpArgs(parameters.Input, parameters.Filter) + .SetAttributes(new + { + strides = parameters.Strides, + padding = parameters.Padding, + use_cudnn_on_gpu = parameters.UseCudnnOnGpu, + explicit_paddings = parameters.ExplicitPaddings, + data_format = parameters.DataFormat, + dilations = parameters.Dilations + })); /// /// Computes the gradients of convolution with respect to the filter. @@ -83,43 +62,16 @@ namespace Tensorflow.Operations string data_format = "NHWC", int[] dilations = null, string name = null) - { - if (explicit_paddings == null) - explicit_paddings = new int[0]; - if (dilations == null) - dilations = new int[] { 1, 1, 1, 1 }; - - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Conv2DBackpropFilter", name, - null, - input, filter_sizes, out_backprop, - "strides", strides, - "use_cudnn_on_gpu", use_cudnn_on_gpu, - "padding", padding, - "explicit_paddings", explicit_paddings, - "data_format", data_format, - "dilations", dilations); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Conv2DBackpropFilter", name: name, args: new - { - input, - filter_sizes, - out_backprop, - strides, - padding, - use_cudnn_on_gpu, - explicit_paddings, - data_format, - dilations - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Conv2DBackpropFilter", name, new ExecuteOpArgs(input, filter_sizes, out_backprop) + .SetAttributes(new + { + strides, + padding, + use_cudnn_on_gpu, + explicit_paddings = explicit_paddings ?? new int[0], + data_format, + dilations = dilations ?? new int[] { 1, 1, 1, 1 } + })); /// /// Computes the gradients of convolution with respect to the input. @@ -132,99 +84,29 @@ namespace Tensorflow.Operations string data_format = "NHWC", int[] dilations = null, string name = null) - { - if (explicit_paddings == null) - explicit_paddings = new int[0]; - if (dilations == null) - dilations = new int[] { 1, 1, 1, 1 }; - - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Conv2DBackpropInput", name, - null, - input_sizes, filter, out_backprop, - "strides", strides, - "use_cudnn_on_gpu", use_cudnn_on_gpu, - "padding", padding, - "explicit_paddings", explicit_paddings, - "data_format", data_format, - "dilations", dilations); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Conv2DBackpropInput", name: name, args: new - { - input_sizes, - filter, - out_backprop, - strides, - padding, - use_cudnn_on_gpu, - explicit_paddings, - data_format, - dilations - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Conv2DBackpropInput", name, new ExecuteOpArgs(input_sizes, filter, out_backprop) + .SetAttributes(new + { + strides, + padding, + use_cudnn_on_gpu, + explicit_paddings = explicit_paddings ?? new int[0], + data_format, + dilations = dilations ?? new int[] { 1, 1, 1, 1 } + })); public static Tensor bias_add(Tensor value, IVariableV1 bias, string data_format = null, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "BiasAdd", name, - null, - value, bias, - "data_format", data_format); - - return results[0]; - } - - if (data_format == null) - data_format = "NHWC"; - - var _op = tf.OpDefLib._apply_op_helper("BiasAdd", name: name, args: new - { - value, - bias, - data_format - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("BiasAdd", name, new ExecuteOpArgs(value, bias) + .SetAttributes(new { data_format = data_format ?? "NHWC" })); public static Tensor bias_add_grad(Tensor out_backprop, string data_format = "NHWC", string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "BiasAddGrad", name, - null, - out_backprop, - "data_format", data_format); - - return results[0]; - } - - if (data_format == null) - data_format = "NHWC"; - - var _op = tf.OpDefLib._apply_op_helper("BiasAddGrad", name: name, args: new - { - out_backprop, - data_format - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("BiasAddGrad", name, new ExecuteOpArgs(out_backprop) + .SetAttributes(new { data_format = data_format ?? "NHWC" })); /// /// Computes exponential linear: exp(features) - 1 if &lt; 0, features otherwise. @@ -269,24 +151,19 @@ namespace Tensorflow.Operations } public static Tensor[] fused_batch_norm_grad_v3(FusedBatchNormParams @params) - => tf.Context.ExecuteOp("FusedBatchNormGradV3", @params.Name, new AutoModeArgs - { - OpInputArgs = new - { - y_backprop = @params.YBackprop, - x = @params.X, - scale = @params.Scale, - reserve_space_1 = @params.ReserveSpace1, - reserve_space_2 = @params.ReserveSpace2, - reserve_space_3 = @params.ReserveSpace3 - }, - OpAttrs = new + => tf.Context.ExecuteOp("FusedBatchNormGradV3", @params.Name, + new ExecuteOpArgs(@params.YBackprop, + @params.X, + @params.Scale, + @params.ReserveSpace1, + @params.ReserveSpace2, + @params.ReserveSpace3) + .SetAttributes(new { epsilon = @params.Epsilon, data_format = @params.DataFormat, is_training = @params.IsTraining - } - }); + })); public static Tensor[] fused_batch_norm(Tensor x, Tensor scale, @@ -323,39 +200,8 @@ namespace Tensorflow.Operations string data_format = "NHWC", bool is_training = true, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "FusedBatchNormV3", name, - null, - x, - scale, - offset, - mean, - variance, - "epsilon", epsilon, - "exponential_avg_factor", exponential_avg_factor, - "data_format", data_format, - "is_training", is_training); - - return results; - } - - var _op = tf.OpDefLib._apply_op_helper("FusedBatchNormV3", name: name, args: new - { - x, - scale, - offset, - mean, - variance, - epsilon, - data_format, - is_training - }); - - return _op.outputs; - } + => tf.Context.ExecuteOp("FusedBatchNormV3", name, new ExecuteOpArgs(x, scale, offset, mean, variance) + .SetAttributes(new { epsilon, data_format, is_training })); /// /// Local Response Normalization. @@ -383,10 +229,7 @@ namespace Tensorflow.Operations } public static Tensor log_softmax(Tensor logits, string name = null) - => tf.Context.ExecuteOp("LogSoftmax", name, new AutoModeArgs - { - OpInputArgs = new { logits } - }); + => tf.Context.ExecuteOp("LogSoftmax", name, new ExecuteOpArgs(logits)); /// /// Says whether the targets are in the top `K` predictions. @@ -409,11 +252,8 @@ namespace Tensorflow.Operations } public static Tensor leaky_relu(Tensor features, float alpha = 0.2f, string name = null) - => tf.Context.ExecuteOp("LeakyRelu", name, new AutoModeArgs - { - OpInputArgs = new { features }, - OpAttrs = new { alpha } - }); + => tf.Context.ExecuteOp("LeakyRelu", name, + new ExecuteOpArgs(features).SetAttributes(new { alpha })); public static Tensor max_pool(Tensor input, int[] ksize, @@ -421,63 +261,25 @@ namespace Tensorflow.Operations string padding, string data_format = "NHWC", string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "MaxPool", name, - null, - input, - "ksize", ksize, - "strides", strides, - "padding", padding, - "data_format", data_format); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("MaxPool", name: name, args: new - { - input, - ksize, - strides, - padding, - data_format, - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("MaxPool", name, new ExecuteOpArgs(input) + .SetAttributes(new + { + ksize, + strides, + padding, + data_format + })); public static Tensor max_pool_grad(Tensor orig_input, Tensor orig_output, Tensor grad, int[] ksize, int[] strides, string padding, string data_format = "NHWC", string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "MaxPoolGrad", name, - null, - orig_input, orig_output, grad, - "ksize", ksize, - "strides", strides, - "padding", padding, - "data_format", data_format); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("MaxPoolGrad", name: name, args: new - { - orig_input, - orig_output, - grad, - ksize, - strides, - padding, - data_format - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("MaxPoolGrad", name, new ExecuteOpArgs(orig_input, orig_output, grad) + .SetAttributes(new + { + ksize, + strides, + padding, + data_format + })); public static Tensor[] top_kv2(Tensor input, int k, bool sorted = true, string name = null) { @@ -492,68 +294,14 @@ namespace Tensorflow.Operations } public static Tensor relu_grad(Tensor gradients, Tensor features, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ReluGrad", name, - null, - gradients, features); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("ReluGrad", name: name, args: new - { - gradients, - features - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("ReluGrad", name, new ExecuteOpArgs(gradients, features)); public static Tensor leaky_relu_grad(Tensor gradients, Tensor features, float alpha = 0.2f, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "LeakyReluGrad", name, - null, - gradients, features, - "alpha", alpha); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("LeakyReluGrad", name: name, args: new - { - gradients, - features, - alpha - }); - - return _op.output; - } + => tf.Context.ExecuteOp("LeakyReluGrad", name, new ExecuteOpArgs(gradients, features) + .SetAttributes(new { alpha })); public static Tensor softmax(Tensor logits, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Softmax", name, - null, - logits); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Softmax", name: name, args: new - { - logits - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Softmax", name, new ExecuteOpArgs(logits)); /// /// Computes softmax cross entropy cost and gradients to backpropagate. @@ -564,23 +312,9 @@ namespace Tensorflow.Operations /// public static (Tensor, Tensor) softmax_cross_entropy_with_logits(Tensor features, Tensor labels, string name = null) { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "SoftmaxCrossEntropyWithLogits", name, - null, - features, labels); - - return (results[0], results[1]); - } + var results = tf.Context.ExecuteOp("SoftmaxCrossEntropyWithLogits", name, new ExecuteOpArgs(features, labels)); - var _op = tf.OpDefLib._apply_op_helper("SoftmaxCrossEntropyWithLogits", name: name, args: new - { - features, - labels - }); - - return (_op.outputs[0], _op.outputs[1]); + return (results[0], results[1]); } /// @@ -612,21 +346,9 @@ namespace Tensorflow.Operations /// public static (Tensor loss, Tensor backprop) sparse_softmax_cross_entropy_with_logits(Tensor features, Tensor labels, string name = "SparseSoftmaxCrossEntropyWithLogits") { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "SparseSoftmaxCrossEntropyWithLogits", name, - null, - features, labels); - - return (results[0], results[1]); - } - - var op = tf.OpDefLib._apply_op_helper("SparseSoftmaxCrossEntropyWithLogits", name: name, args: new { features, labels }); - int _idx = 0; - var loss = op.outputs[_idx++]; - var backprop = op.outputs[_idx++]; - return (loss, backprop); + var results = tf.Context.ExecuteOp("SparseSoftmaxCrossEntropyWithLogits", name, new ExecuteOpArgs(features, labels)); + + return (results[0], results[1]); } /// @@ -636,35 +358,9 @@ namespace Tensorflow.Operations /// A name for the operation (optional). /// A `Tensor`. Has the same type as `features`. public static Tensor relu(Tensor features, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Relu", name, - null, - features); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Relu", name: name, args: new { features }); - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Relu", name, new ExecuteOpArgs(features)); public static Tensor tanh(Tensor x, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Tanh", name, - null, - x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Tanh", name: name, args: new { x }); - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Tanh", name, new ExecuteOpArgs(x)); } } diff --git a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs index 752b1d51..560b681e 100644 --- a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs +++ b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs @@ -68,10 +68,10 @@ namespace Tensorflow string _scope_name = scope; // Perform input type inference - foreach (var input_arg in op_def.InputArg) + foreach (var (i, input_arg) in enumerate(op_def.InputArg)) { var input_name = input_arg.Name; - + if (keywords.ContainsKey(input_name)) values = keywords[input_name]; else if (keywords.ContainsKey(input_name + "_")) @@ -79,6 +79,10 @@ namespace Tensorflow input_name += "_"; values = keywords[input_name]; } + else if (keywords.ContainsKey($"input_{i}")) + { + values = keywords[$"input_{i}"]; + } else throw new TypeError("No argument for input " + input_name); diff --git a/src/TensorFlowNET.Core/Operations/array_ops.cs b/src/TensorFlowNET.Core/Operations/array_ops.cs index 235d2a95..2eb32775 100644 --- a/src/TensorFlowNET.Core/Operations/array_ops.cs +++ b/src/TensorFlowNET.Core/Operations/array_ops.cs @@ -57,20 +57,8 @@ namespace Tensorflow /// gradients in some corner cases. /// public static Tensor prevent_gradient(Tensor input, string message = "", string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "PreventGradient", name, - null, - input, - "message", message); - return results[0]; - } - - var op = tf.OpDefLib._apply_op_helper("PreventGradient", name: name, args: new { input, message }); - return op.output; - } + => tf.Context.ExecuteOp("PreventGradient", name, new ExecuteOpArgs(input) + .SetAttributes(new { message })); internal static Tensor constant(object value, TF_DataType dtype = TF_DataType.DtInvalid, @@ -737,35 +725,27 @@ namespace Tensorflow public static Tensor strided_slice_grad(Tensor shape, Tensor begin, Tensor end, Tensor strides, Tensor dy, long begin_mask = 0, long end_mask = 0, long ellipsis_mask = 0, long new_axis_mask = 0, long shrink_axis_mask = 0, string name = null) - => tf.Context.ExecuteOp("StridedSliceGrad", name, new AutoModeArgs - { - OpInputArgs = new + => tf.Context.ExecuteOp("StridedSliceGrad", name, + new ExecuteOpArgs(shape, begin, end, strides, dy) { - shape, - begin, - end, - strides, - dy - }, - OpAttrs = new + GetGradientAttrs = (op) => new + { + T = op.get_attr("T"), + Index = op.get_attr("Index"), + begin_mask = op.get_attr("begin_mask"), + end_mask = op.get_attr("end_mask"), + ellipsis_mask = op.get_attr("ellipsis_mask"), + new_axis_mask = op.get_attr("new_axis_mask"), + shrink_axis_mask = op.get_attr("shrink_axis_mask") + } + }.SetAttributes(new { begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask - }, - GetGradientAttrs = (op) => new - { - T = op.get_attr("T"), - Index = op.get_attr("Index"), - begin_mask = op.get_attr("begin_mask"), - end_mask = op.get_attr("end_mask"), - ellipsis_mask = op.get_attr("ellipsis_mask"), - new_axis_mask = op.get_attr("new_axis_mask"), - shrink_axis_mask = op.get_attr("shrink_axis_mask") - } - }); + })); /// /// Removes dimensions of size 1 from the shape of a tensor. @@ -800,38 +780,17 @@ namespace Tensorflow int num_cols = -1, float padding_value = 0, string align = "RIGHT_LEFT") - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "MatrixDiagV3", name, - null, - diagonal, k, num_rows, num_cols, padding_value, - "align", align); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("MatrixDiagV3", name, + new ExecuteOpArgs(diagonal, k, num_rows, num_cols, padding_value) + .SetAttributes(new { align })); public static Tensor matrix_set_diag(Tensor input, Tensor diagonal, string name = "set_diag", int k = 0, string align = "RIGHT_LEFT") - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "MatrixSetDiagV3", name, - null, - input, diagonal, k, - "align", align); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("MatrixSetDiagV3", name, new ExecuteOpArgs(input, diagonal, k) + .SetAttributes(new { align })); /// /// Computes the shape of a broadcast given symbolic shapes. @@ -960,9 +919,8 @@ namespace Tensorflow => gen_array_ops.slice(input, begin, size, name: name); public static Tensor slice(Tensor input, Tensor begin, Tensor size, string name = null) - => tf.Context.ExecuteOp("Slice", name, new AutoModeArgs + => tf.Context.ExecuteOp("Slice", name, new ExecuteOpArgs(input, begin, size) { - OpInputArgs = new { input, begin, size }, GetGradientAttrs = (op) => new { T = op.get_attr("T"), diff --git a/src/TensorFlowNET.Core/Operations/bitwise_ops.cs b/src/TensorFlowNET.Core/Operations/bitwise_ops.cs index 4b4e0f5e..7536357c 100644 --- a/src/TensorFlowNET.Core/Operations/bitwise_ops.cs +++ b/src/TensorFlowNET.Core/Operations/bitwise_ops.cs @@ -94,20 +94,7 @@ namespace Tensorflow.Operations /// /// Tensor unary_op(Tensor x, string opName, string name) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - opName, name, - null, - x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper(opName, name, args: new { x }); - return _op.output; - } + => tf.Context.ExecuteOp(opName, name, new ExecuteOpArgs(x)); /// /// Helper method to invoke binary operator with specified name. @@ -118,21 +105,7 @@ namespace Tensorflow.Operations /// /// Tensor binary_op(Tensor x, Tensor y, string opName, string name) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - opName, name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper(opName, name, args: new { x, y }); - return _op.output; - } - + => tf.Context.ExecuteOp(opName, name, new ExecuteOpArgs(x, y)); #endregion } } diff --git a/src/TensorFlowNET.Core/Operations/dataset_ops.cs b/src/TensorFlowNET.Core/Operations/dataset_ops.cs index 3a8d70b4..fcad0709 100644 --- a/src/TensorFlowNET.Core/Operations/dataset_ops.cs +++ b/src/TensorFlowNET.Core/Operations/dataset_ops.cs @@ -8,26 +8,10 @@ namespace Tensorflow public class dataset_ops { public Tensor tensor_dataset(Tensor[] components, TensorShape[] output_shapes, string name = null) - { - if (tf.Context.executing_eagerly()) + => tf.Context.ExecuteOp("TensorDataset", name, new ExecuteOpArgs() { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "TensorDataset", name, - null, - new object[] - { - components, - "output_shapes", output_shapes - }); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("TensorDataset", - name: name, - args: new { components, output_shapes }); - - return _op.output; - } + OpInputArgs = new object[] { components } + }.SetAttributes(new { output_shapes })); /// /// Creates a dataset that emits each dim-0 slice of `components` once. @@ -37,192 +21,62 @@ namespace Tensorflow /// /// public Tensor tensor_slice_dataset(Tensor[] components, TensorShape[] output_shapes, string name = null) - { - if (tf.Context.executing_eagerly()) + => tf.Context.ExecuteOp("TensorSliceDataset", name, new ExecuteOpArgs() { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "TensorSliceDataset", name, - null, - new object[] - { - components, - "output_shapes", output_shapes - }); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("TensorSliceDataset", - name: name, - args: new { components, output_shapes }); - - return _op.outputs[0]; - } + OpInputArgs = new object[] { components } + }.SetAttributes(new { output_shapes })); public Tensor range_dataset(Tensor start, Tensor stop, Tensor step, 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.DeviceName, - "RangeDataset", name, - null, - start, stop, step, - "output_types", output_types, - "output_shapes", output_shapes); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("RangeDataset", name, new ExecuteOpArgs(start, stop, step) + .SetAttributes(new { output_types, output_shapes })); public Tensor repeat_dataset(Tensor input_dataset, Tensor count, 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.DeviceName, - "RepeatDataset", name, - null, - input_dataset, count, - "output_types", output_types, - "output_shapes", output_shapes); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("RepeatDataset", name, new ExecuteOpArgs(input_dataset, count) + .SetAttributes(new { output_types, output_shapes })); public Tensor shard_dataset(Tensor input_dataset, Tensor num_shards, Tensor index, TF_DataType[] output_types, TensorShape[] output_shapes, bool require_non_empty = false, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ShardDataset", name, - null, - input_dataset, num_shards, index, - "require_non_empty", require_non_empty, - "output_types", output_types, - "output_shapes", output_shapes); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("ShardDataset", name, new ExecuteOpArgs(input_dataset, num_shards, index) + .SetAttributes(new { require_non_empty, output_types, output_shapes })); public Tensor zip_dataset(Tensor[] input_datasets, 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.DeviceName, - "ZipDataset", name, - null, - new object[] - { - input_datasets, - "output_types", output_types, - "output_shapes", output_shapes - }); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("ZipDataset", name, new ExecuteOpArgs() + { + OpInputArgs = new object[] { input_datasets } + }.SetAttributes(new { output_types, output_shapes })); public Tensor shuffle_dataset_v3(Tensor input_dataset, Tensor buffer_size, Tensor seed, Tensor seed2, Tensor seed_generator, TF_DataType[] output_types, TensorShape[] output_shapes, bool reshuffle_each_iteration = true, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ShuffleDatasetV3", name, - null, - input_dataset, buffer_size, - seed, seed2, seed_generator, - "reshuffle_each_iteration", reshuffle_each_iteration, - "output_types", output_types, - "output_shapes", output_shapes); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("ShuffleDatasetV3", name, new ExecuteOpArgs(input_dataset, buffer_size, seed, seed2, seed_generator) + .SetAttributes(new { reshuffle_each_iteration, output_types, output_shapes })); public Tensor skip_dataset(Tensor input_dataset, Tensor count, 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.DeviceName, - "SkipDataset", name, - null, - input_dataset, count, - "output_types", output_types, - "output_shapes", output_shapes); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("SkipDataset", name, new ExecuteOpArgs(input_dataset, count) + .SetAttributes(new { output_types, output_shapes })); public Tensor dummy_seed_generator(string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "DummySeedGenerator", name, - null); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("DummySeedGenerator", name, new ExecuteOpArgs()); public Tensor concatenate_dataset(Tensor input_dataset, Tensor another_dataset, 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.DeviceName, - "ConcatenateDataset", name, - null, - input_dataset, another_dataset, - "output_types", output_types, - "output_shapes", output_shapes); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("ConcatenateDataset", - name: name, - args: new { input_dataset, another_dataset, output_types, output_shapes }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("ConcatenateDataset", name, new ExecuteOpArgs(input_dataset, another_dataset) + .SetAttributes(new { output_types, output_shapes })); public Tensor cache_dataset_v2(Tensor input_dataset, Tensor filename, Tensor cache, 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.DeviceName, - "CacheDatasetV2", name, - null, - input_dataset, filename, cache, - "output_types", output_types, - "output_shapes", output_shapes); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("CacheDatasetV2", name, new ExecuteOpArgs(input_dataset, filename, cache) + .SetAttributes(new { output_types, output_shapes })); /// /// Creates a dataset that batches `batch_size` elements from `input_dataset`. @@ -240,21 +94,9 @@ namespace Tensorflow TF_DataType[] output_types, TensorShape[] output_shapes, bool parallel_copy = false, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "BatchDatasetV2", name, - null, - input_dataset, buffer_size, drop_remainder, - "parallel_copy", parallel_copy, - "output_types", output_types, - "output_shapes", output_shapes); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("BatchDatasetV2", name, + new ExecuteOpArgs(input_dataset, buffer_size, drop_remainder) + .SetAttributes(new { parallel_copy, output_types, output_shapes })); /// /// @@ -262,17 +104,7 @@ namespace Tensorflow /// /// public Tensor dummy_memory_cache(string name = "") - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "DummyMemoryCache", name, - null); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("DummyMemoryCache", name, new ExecuteOpArgs()); /// /// Creates a dataset that asynchronously prefetches elements from `input_dataset`. @@ -290,22 +122,14 @@ namespace Tensorflow int? slack_period = 0, bool legacy_autotune = true, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "PrefetchDataset", name, - null, - input_dataset, buffer_size, - "output_types", output_types, - "output_shapes", output_shapes, - "slack_period", slack_period, - "legacy_autotune", legacy_autotune); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("PrefetchDataset", name, new ExecuteOpArgs(input_dataset, buffer_size) + .SetAttributes(new + { + output_types, + output_shapes, + slack_period, + legacy_autotune + })); /// /// Creates a dataset that contains `count` elements from the `input_dataset`. @@ -319,20 +143,8 @@ namespace Tensorflow public Tensor take_dataset(Tensor input_dataset, Tensor count, 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.DeviceName, - "TakeDataset", name, - null, - input_dataset, count, - "output_types", output_types, - "output_shapes", output_shapes); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("TakeDataset", name, new ExecuteOpArgs(input_dataset, count) + .SetAttributes(new { output_types, output_shapes })); /// /// Creates a dataset by applying optimizations to `input_dataset`. @@ -348,24 +160,13 @@ namespace Tensorflow 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.DeviceName, - "OptimizeDataset", name, - null, - input_dataset, optimizations, - "output_types", output_types, - "output_shapes", output_shapes, - "optimization_configs", optimization_configs); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("OptimizeDataset", name, new ExecuteOpArgs(input_dataset, optimizations) + .SetAttributes(new + { + output_types, + output_shapes, + optimization_configs = optimization_configs ?? new string[0] + })); /// /// Identity transformation that models performance. @@ -381,22 +182,14 @@ namespace Tensorflow 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.DeviceName, - "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(""); - } + => tf.Context.ExecuteOp("ModelDataset", name, new ExecuteOpArgs(input_dataset) + .SetAttributes(new + { + algorithm, + cpu_budget, + output_types, + output_shapes + })); /// /// A container for an iterator resource. @@ -407,17 +200,9 @@ namespace Tensorflow /// 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.DeviceName, - "AnonymousIteratorV2", name, - null, - "output_types", output_types, - "output_shapes", output_shapes); - return (results[0], results[1]); - } - - throw new NotImplementedException(""); + var results = tf.Context.ExecuteOp("AnonymousIteratorV2", name, + new ExecuteOpArgs().SetAttributes(new { output_types, output_shapes })); + return (results[0], results[1]); } /// @@ -427,19 +212,8 @@ namespace Tensorflow /// /// /// 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.DeviceName, - "MakeIterator", name, - null, - dataset, iterator); - return null; - } - - throw new NotImplementedException(""); - } + public void make_iterator(Tensor dataset, Tensor iterator, string name = null) + => tf.Context.ExecuteOp("MakeIterator", name, new ExecuteOpArgs(dataset, iterator)); /// /// @@ -450,23 +224,15 @@ namespace Tensorflow /// public Tensor map_dataset(Tensor dataset, ConcreteFunction f, TF_DataType[] output_types, TensorShape[] output_shapes, bool use_inter_op_parallelism = true, bool preserve_cardinality = false, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "MapDataset", name, - null, - dataset, new Tensor[0], - "f", f, - "output_types", output_types, - "output_shapes", output_shapes, - "use_inter_op_parallelism", use_inter_op_parallelism, - "preserve_cardinality", preserve_cardinality); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("MapDataset", name, new ExecuteOpArgs(dataset, new Tensor[0]) + .SetAttributes(new + { + f, + output_types, + output_shapes, + use_inter_op_parallelism, + preserve_cardinality + })); /// /// Creates a dataset that applies `f` to the outputs of `input_dataset`. @@ -479,21 +245,8 @@ namespace Tensorflow /// public Tensor flat_map_dataset(Tensor dataset, ConcreteFunction f, 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.DeviceName, - "FlatMapDataset", name, - null, - dataset, new Tensor[0], - "f", f, - "output_types", output_types, - "output_shapes", output_shapes); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("FlatMapDataset", name, new ExecuteOpArgs(dataset, new Tensor[0]) + .SetAttributes(new { f, output_types, output_shapes })); /// /// Creates a dataset that applies `f` to the outputs of `input_dataset`. @@ -512,24 +265,17 @@ namespace Tensorflow string deterministic = "default", bool preserve_cardinality = false, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ParallelMapDatasetV2", name, - null, - dataset, new Tensor[0], num_parallel_calls, - "f", f, - "output_types", output_types, - "output_shapes", output_shapes, - "use_inter_op_parallelism", use_inter_op_parallelism, - "deterministic", deterministic, - "preserve_cardinality", preserve_cardinality); - return results[0]; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("ParallelMapDatasetV2", name, + new ExecuteOpArgs(dataset, new Tensor[0], num_parallel_calls) + .SetAttributes(new + { + f, + output_types, + output_shapes, + use_inter_op_parallelism, + deterministic, + preserve_cardinality + })); /// /// A container for an iterator resource. @@ -538,19 +284,8 @@ namespace Tensorflow /// /// /// 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.DeviceName, - "DeleteIterator", name, - null, - handle, deleter); - return null; - } - - throw new NotImplementedException(""); - } + public void delete_iterator(Tensor handle, Tensor deleter, string name = null) + => tf.Context.ExecuteOp("DeleteIterator", name, new ExecuteOpArgs(handle, deleter)); /// /// Gets the next output from the given iterator . @@ -561,19 +296,7 @@ namespace Tensorflow /// /// 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.DeviceName, - "IteratorGetNext", name, - null, - iterator, - "output_types", output_types, - "output_shapes", output_shapes); - return results; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("IteratorGetNext", name, new ExecuteOpArgs(iterator) + .SetAttributes(new { output_types, output_shapes })); } } diff --git a/src/TensorFlowNET.Core/Operations/gen_array_ops.cs b/src/TensorFlowNET.Core/Operations/gen_array_ops.cs index fd1b4c8d..c034c7fd 100644 --- a/src/TensorFlowNET.Core/Operations/gen_array_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_array_ops.cs @@ -45,20 +45,7 @@ namespace Tensorflow /// /// public static Tensor concat_v2(T[] values, Ta axis, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ConcatV2", name, - null, - values, axis); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("ConcatV2", name: name, args: new { values, axis }); - return _op.output; - } + => tf.Context.ExecuteOp("ConcatV2", name, new ExecuteOpArgs(values, axis)); public static Tensor concat_v2(Tensor[] values, Tensor axis, string name = null) { @@ -72,10 +59,7 @@ namespace Tensorflow } public static Tensor concat_v2(Tensor[] values, int axis, string name = null) - => tf.Context.ExecuteOp("ConcatV2", name, new AutoModeArgs - { - OpInputArgs = new { values, axis } - }); + => tf.Context.ExecuteOp("ConcatV2", name, new ExecuteOpArgs(values, axis)); private static Tensor concat_v2_eager_fallback(T1[] values, T2 axis, string name, Context ctx) { @@ -127,38 +111,11 @@ namespace Tensorflow /// /// public static Tensor diag(Tensor diagonal, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Diag", name, - null, - diagonal); - - return results[0]; - } - - var op = tf.OpDefLib._apply_op_helper("Diag", name: name, args: new { diagonal }); - - return op.output; - } + => tf.Context.ExecuteOp("Diag", name, new ExecuteOpArgs(diagonal)); public static Tensor expand_dims(Tensor input, int axis, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ExpandDims", name, - null, - input, tf.convert_to_tensor(axis)); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("ExpandDims", name: name, args: new { input, dim = axis }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("ExpandDims", name, new ExecuteOpArgs(input, axis) + .SetAttributes(new { dim = axis })); public static Tensor gather_v2(T1 @params, T2 indices, int axis, string name = null) { @@ -198,11 +155,10 @@ namespace Tensorflow } public static Tensor pack(Tensor[] values, int axis = 0, string name = null) - => tf.Context.ExecuteOp("Pack", name, new AutoModeArgs + => tf.Context.ExecuteOp("Pack", name, new ExecuteOpArgs() { - OpInputArgs = new { values }, - OpAttrs = new { axis } - }); + OpInputArgs = new object[] { values } + }.SetAttributes(new { axis })); /// /// Return a tensor with the same shape and contents as the input tensor or value. @@ -210,29 +166,7 @@ namespace Tensorflow /// /// public static Tensor identity(Tensor input, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Identity", name, - null, - input); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Identity", name, new { input }); - - if (tf.Runner.MustRecordGradient()) - { - tf.Runner.RecordGradient("Identity", _op.inputs, new object[] - { - "T", _op.get_attr("T") - }, _op.outputs); - } - - return _op.output; - } + => tf.Context.ExecuteOp("Identity", name, new ExecuteOpArgs(input)); public static Tensor invert_permutation(Tensor x, string name = null) { @@ -249,21 +183,7 @@ namespace Tensorflow } public static Tensor rank(Tensor input, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Rank", name, - null, - input); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Rank", name: name, args: new { input }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Rank", name, new ExecuteOpArgs(input)); /// /// Creates a tensor filled with a scalar value. @@ -273,20 +193,7 @@ namespace Tensorflow /// A name for the operation (optional). /// A `Tensor`. Has the same type as `value`. public static Tensor fill(Tensor dims, T value, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Fill", name, - null, - dims, value); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Fill", name, new { dims, value }); - return _op.output; - } + => tf.Context.ExecuteOp("Fill", name, new ExecuteOpArgs(dims, value)); /// /// Return the reduction indices for computing gradients of s0 op s1 with broadcast. @@ -297,19 +204,8 @@ namespace Tensorflow /// A tuple of `Tensor` objects (r0, r1). public static (Tensor, Tensor) broadcast_gradient_args(Tensor s0, Tensor s1, string name = "") { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "BroadcastGradientArgs", name, - null, - s0, s1); - - return (results[0], results[1]); - } - - var _op = tf.OpDefLib._apply_op_helper("BroadcastGradientArgs", name, new { s0, s1 }); - - return (_op.outputs[0], _op.outputs[1]); + var results = tf.Context.ExecuteOp("BroadcastGradientArgs", name, new ExecuteOpArgs(s0, s1)); + return (results[0], results[1]); } public static Tensor reverse(Tensor tensor, T axis, string name = null) @@ -319,16 +215,10 @@ namespace Tensorflow } public static Tensor reshape(Tensor tensor, T shape, string name = null) - => tf.Context.ExecuteOp("Reshape", name, new AutoModeArgs - { - OpInputArgs = new { tensor, shape } - }); + => tf.Context.ExecuteOp("Reshape", name, new ExecuteOpArgs(tensor, shape)); public static Tensor reshape(Tensor tensor, object[] shape, string name = null) - => tf.Context.ExecuteOp("Reshape", name, new AutoModeArgs - { - OpInputArgs = new { tensor, shape } - }); + => tf.Context.ExecuteOp("Reshape", name, new ExecuteOpArgs(tensor, shape)); private static Tensor reshape_eager_fallback(Tensor tensor, object[] shape, string name, Context ctx) { @@ -378,21 +268,8 @@ namespace Tensorflow TF_DataType dtype = TF_DataType.DtInvalid, int axis = -1, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "OneHot", name, - null, - indices, depth, on_value, off_value, - "axis", axis); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("OneHot", name, new { indices, depth, on_value, off_value, axis }); - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("OneHot", name, new ExecuteOpArgs(indices, depth, on_value, off_value) + .SetAttributes(new { axis })); /// /// A placeholder op that passes through `input` when its output is not fed. @@ -408,35 +285,10 @@ namespace Tensorflow } public static Tensor select(Tensor condition, Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Select", name, - null, - condition, x, y); - - return results[0]; - } + => tf.Context.ExecuteOp("Select", name, new ExecuteOpArgs(condition, x, y)); - var _op = tf.OpDefLib._apply_op_helper("Select", name, new { condition, t = x, e = y }); - return _op.outputs[0]; - } public static Tensor select_v2(Tensor condition, Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "SelectV2", name, - null, - condition, x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("SelectV2", name, new { condition, t = x, e = y }); - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("SelectV2", name, new ExecuteOpArgs(condition, x, y)); public static Tensor scatter_nd(Tensor indices, Tensor updates, Tensor[] shape, string name = null) { @@ -445,11 +297,8 @@ namespace Tensorflow } public static Tensor shape(Tensor input, TF_DataType out_type = TF_DataType.TF_INT32, string name = null) - => tf.Context.ExecuteOp("Shape", name, new AutoModeArgs - { - OpInputArgs = new { input }, - OpAttrs = new { out_type } - }); + => tf.Context.ExecuteOp("Shape", name, new ExecuteOpArgs(input) + .SetAttributes(new { out_type })); /// /// Returns shape of tensors. @@ -459,21 +308,10 @@ namespace Tensorflow /// /// public static Tensor[] shape_n(Tensor[] input, TF_DataType out_type = TF_DataType.TF_INT32, string name = null) - { - if (tf.executing_eagerly()) + => tf.Context.ExecuteOp("ShapeN", name, new ExecuteOpArgs() { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ShapeN", name, - null, - input, - "out_type", out_type); - - return results; - } - - var _op = tf.OpDefLib._apply_op_helper("ShapeN", name, new { input, out_type }); - return _op.outputs; - } + OpInputArgs = new object[] { input } + }.SetAttributes(new { out_type })); public static Tensor size(Tensor input, TF_DataType out_type = TF_DataType.TF_INT32, string name = null) { @@ -516,60 +354,23 @@ namespace Tensorflow public static Tensor[] split_v(Tensor value, Tensor size_splits, int axis, int num_split, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "SplitV", name, - null, - value, size_splits, axis, - "num_split", num_split); - - return results; - } - - var _op = tf.OpDefLib._apply_op_helper("SplitV", name, new { split_dim = axis, value, num_split }); - return _op.outputs; - } + => tf.Context.ExecuteOp("SplitV", name, new ExecuteOpArgs(value, size_splits, axis) + .SetAttributes(new { num_split })); public static Tensor tile(Tensor input, Tensor multiples, string name = null) - => tf.Context.ExecuteOp("Tile", name, new AutoModeArgs - { - OpInputArgs = new { input, multiples } - }); + => tf.Context.ExecuteOp("Tile", name, new ExecuteOpArgs(input, multiples)); public static Tensor tile(Tensor input, object[] multiples, string name = null) - => tf.Context.ExecuteOp("Tile", name, new AutoModeArgs - { - OpInputArgs = new { input, multiples } - }); + => tf.Context.ExecuteOp("Tile", name, new ExecuteOpArgs(input, multiples)); public static Tensor transpose(Tensor x, T1 perm, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Transpose", name, - null, - x, perm); - - return results[0]; - } - var _op = tf.OpDefLib._apply_op_helper("Transpose", name, new { x, perm }); - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Transpose", name, new ExecuteOpArgs(x, perm)); public static Tensor ones_like(Tensor x, string name = null) - => tf.Context.ExecuteOp("OnesLike", name, new AutoModeArgs - { - OpInputArgs = new { x } - }); + => tf.Context.ExecuteOp("OnesLike", name, new ExecuteOpArgs(x)); public static Tensor zeros_like(Tensor x, string name = null) - => tf.Context.ExecuteOp("ZerosLike", name, new AutoModeArgs - { - OpInputArgs = new { x } - }); + => tf.Context.ExecuteOp("ZerosLike", name, new ExecuteOpArgs(x)); public static Tensor stop_gradient(Tensor x, string name = null) { @@ -585,18 +386,15 @@ namespace Tensorflow long new_axis_mask = 0, long shrink_axis_mask = 0, string name = null) - => tf.Context.ExecuteOp("StridedSlice", name, new AutoModeArgs - { - OpInputArgs = new { input, begin, end, strides }, - OpAttrs = new + => tf.Context.ExecuteOp("StridedSlice", name, new ExecuteOpArgs(input, begin, end, strides) + .SetAttributes(new { begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask - } - }); + })); public static Tensor resource_strided_slice_assign(Tensor input, Tensor begin, Tensor end, Tensor strides, Tensor value, int begin_mask = 0, @@ -605,17 +403,15 @@ namespace Tensorflow int new_axis_mask = 0, int shrink_axis_mask = 0, string name = null) - => tf.Context.ExecuteOp("ResourceStridedSliceAssign", name, new AutoModeArgs - { - OpInputArgs = new { input, begin, end, strides, value }, - OpAttrs = new { + => tf.Context.ExecuteOp("ResourceStridedSliceAssign", name, new ExecuteOpArgs(input, begin, end, strides, value) + .SetAttributes(new + { begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask - } - }); + })); public static Tensor strided_slice(Tensor input, T[] begin, T[] end, T[] strides, int begin_mask = 0, @@ -653,23 +449,8 @@ namespace Tensorflow /// A name for the operation (optional). /// A `Tensor`. Has the same type as `input`. public static Tensor squeeze(Tensor input, int[] axis = null, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Squeeze", name, - null, - input, - "squeeze_dims", axis); - - return results[0]; - } - - if (axis == null) axis = new int[0]; - var _op = tf.OpDefLib._apply_op_helper("Squeeze", name, args: new { input, squeeze_dims = axis }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Squeeze", name, new ExecuteOpArgs(input) + .SetAttributes(new { squeeze_dims = axis })); /// /// Return the shape of s0 op s1 with broadcast. @@ -695,20 +476,6 @@ namespace Tensorflow /// /// public static Tensor broadcast_to(Tensor input, T shape, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "BroadcastTo", name, - null, - input, shape); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("BroadcastTo", name, args: new { input, shape, name }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("BroadcastTo", name, new ExecuteOpArgs(input, shape)); } } diff --git a/src/TensorFlowNET.Core/Operations/gen_image_ops.cs b/src/TensorFlowNET.Core/Operations/gen_image_ops.cs index 19a5be0d..8b81dc8a 100644 --- a/src/TensorFlowNET.Core/Operations/gen_image_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_image_ops.cs @@ -70,38 +70,17 @@ namespace Tensorflow float acceptable_fraction = 1, string dct_method = "", string name = null) - { - // Add nodes to the TensorFlow graph. - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "DecodeJpeg", name, - null, - contents, - "channels", channels, - "ratio", ratio, - "fancy_upscaling", fancy_upscaling, - "try_recover_truncated", try_recover_truncated, - "acceptable_fraction", acceptable_fraction, - "dct_method", dct_method); - return results[0]; - } - else - { - var _op = tf.OpDefLib._apply_op_helper("DecodeJpeg", name: name, args: new - { - contents, - channels, - ratio, - fancy_upscaling, - try_recover_truncated, - acceptable_fraction, - dct_method - }); - - return _op.outputs[0]; - } - } + => tf.Context.ExecuteOp("DecodeJpeg", name, + new ExecuteOpArgs(contents).SetAttributes( + new + { + channels, + ratio, + fancy_upscaling, + try_recover_truncated, + acceptable_fraction, + dct_method + })); public static Tensor decode_gif(Tensor contents, string name = null) @@ -171,75 +150,36 @@ namespace Tensorflow bool align_corners = false, bool half_pixel_centers = false, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ResizeBilinear", name, - null, - images, size, - "align_corners", align_corners, - "half_pixel_centers", half_pixel_centers); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("ResizeBilinear", name: name, args: new - { - images, - size, - align_corners - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("ResizeBilinear", name, + new ExecuteOpArgs(images, size).SetAttributes(new + { + align_corners, + half_pixel_centers + })); public static Tensor resize_bicubic(Tensor images, Tensor size, bool align_corners = false, bool half_pixel_centers = false, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ResizeBicubic", name, - null, - images, size, - "align_corners", align_corners, - "half_pixel_centers", half_pixel_centers); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("ResizeBicubic", name: name, args: new - { - images, - size, - align_corners - }); - - return _op.outputs[0]; - } - + => tf.Context.ExecuteOp("ResizeBicubic", name, + new ExecuteOpArgs(images, size).SetAttributes(new { align_corners, half_pixel_centers })); + public static Tensor resize_nearest_neighbor(Tensor images, Tsize size, bool align_corners = false, bool half_pixel_centers = false, string name = null) - => tf.Context.ExecuteOp("ResizeNearestNeighbor", name, new AutoModeArgs - { - OpInputArgs = new { images, size }, - OpAttrs = new { align_corners, half_pixel_centers } - }); + => tf.Context.ExecuteOp("ResizeNearestNeighbor", name, + new ExecuteOpArgs(images, size).SetAttributes(new { align_corners, half_pixel_centers })); public static Tensor resize_nearest_neighbor_grad(Tensor grads, Tensor size, bool align_corners = false, bool half_pixel_centers = false, string name = null) - => tf.Context.ExecuteOp("ResizeNearestNeighborGrad", name, new AutoModeArgs + => tf.Context.ExecuteOp("ResizeNearestNeighborGrad", name, new ExecuteOpArgs(grads, size) { - OpInputArgs = new { grads, size }, - OpAttrs = new { align_corners, half_pixel_centers }, GetGradientAttrs = (op) => new { T = op.get_attr("T"), align_corners = op.get_attr("align_corners"), half_pixel_centers = op.get_attr("half_pixel_centers") } - }); + }.SetAttributes(new { align_corners, half_pixel_centers })); } } diff --git a/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs b/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs index c62a8b8a..03159aaa 100644 --- a/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs @@ -25,10 +25,9 @@ namespace Tensorflow { if (tf.Context.executing_eagerly()) { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, + var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo( "Assert", name, - null, - new object[] { condition, data, summarize }); + new object[] { condition, data, summarize })); return results[0]; } diff --git a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs index ba981d6f..d990b13b 100644 --- a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs @@ -37,20 +37,10 @@ namespace Tensorflow /// /// public static Tensor add_n(Tensor[] inputs, string name = null) - { - if (tf.Context.executing_eagerly()) + => tf.Context.ExecuteOp("AddN", name, new ExecuteOpArgs() { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "AddN", name, - null, - new[] { inputs }); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("AddN", name, args: new { inputs }); - - return _op.outputs[0]; - } + OpInputArgs = new object[] { inputs } + }); /// /// Returns the index with the largest value across dimensions of a tensor. @@ -61,20 +51,9 @@ namespace Tensorflow /// /// public static Tensor arg_max(Tensor input, int dimension, TF_DataType output_type = TF_DataType.TF_INT64, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ArgMax", name, - null, - input, dimension, - "output_type", output_type); - - return results[0]; - } + => tf.Context.ExecuteOp("ArgMax", name, new ExecuteOpArgs(input, dimension) + .SetAttributes(new { output_type })); - return tf.OpDefLib._apply_op_helper("ArgMax", name, args: new { input, dimension, output_type }).output; - } /// /// Returns the index with the smallest value across dimensions of a tensor. @@ -116,10 +95,7 @@ namespace Tensorflow /// [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) /// public static Tensor div_no_nan(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("DivNoNan", name, new AutoModeArgs - { - OpInputArgs = new { x, y } - }); + => tf.Context.ExecuteOp("DivNoNan", name, new ExecuteOpArgs(x, y)); public static Tensor mean(Tensor input, int axis, bool keep_dims = false, string name = null) => mean(input, ops.convert_to_tensor(axis), keep_dims: keep_dims, name: name); @@ -138,17 +114,15 @@ namespace Tensorflow /// A name for the operation (optional). /// A `Tensor`. Has the same type as `input`. public static Tensor mean(Tensor input, Tensor axis, bool keep_dims = false, string name = null) - => tf.Context.ExecuteOp("Mean", name, new AutoModeArgs + => tf.Context.ExecuteOp("Mean", name, new ExecuteOpArgs(input, axis) { - OpInputArgs = new { input, axis }, - OpAttrs = new { keep_dims, reduction_indices = axis }, GetGradientAttrs = (op) => new { T = op.get_attr("T"), Tidx = op.get_attr("Tidx"), keep_dims = op.get_attr("keep_dims") } - }); + }.SetAttributes(new { keep_dims, reduction_indices = axis })); public static Tensor mean(Tensor[] inputs, Tensor axis, bool keep_dims = false, string name = null) { @@ -173,28 +147,8 @@ namespace Tensorflow } public static Tensor prod(T1 input, T2 axis, bool keep_dims = false, string name = null) - { - if (tf.Context.executing_eagerly()) - { - try - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Prod", name, - null, - input, axis, - "keep_dims", keep_dims); - - return results[0]; - } - catch (Exception) - { - return prod_eager_fallback(input as Tensor, axis as int[], keep_dims, name, tf.Context); - } - } - - var _op = tf.OpDefLib._apply_op_helper("Prod", name, args: new { input, reduction_indices = axis, keep_dims }); - return _op.output; - } + => tf.Context.ExecuteOp("Prod", name, + new ExecuteOpArgs(input, axis).SetAttributes(new { keep_dims, reduction_indices = axis })); private static Tensor prod_eager_fallback(Tensor input_t, int[] axis, bool keep_dims, string name, Context ctx = null) { @@ -221,84 +175,22 @@ namespace Tensorflow } public static Tensor add(Tensor x, Tensor y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Add", name, null, - x, y); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Add", name, args: new { x, y }); - - return _op.output; - } + => tf.Context.ExecuteOp("Add", name, new ExecuteOpArgs(x, y)); public static Tensor add(Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Add", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Add", name, args: new { x, y }); - - return _op.output; - } + => tf.Context.ExecuteOp("Add", name, new ExecuteOpArgs(x, y)); public static Tensor add_v2(Tx x, Ty y, string name = null) - { - // forward_compatible(2019, 6, 25): - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "AddV2", name, - null, - x, y); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("AddV2", name, args: new { x, y }); - - return _op.output; - } + => tf.Context.ExecuteOp("AddV2", name, new ExecuteOpArgs(x, y)); public static Tensor atan(Tensor x, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("Atan", name, args: new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Atan", name, new ExecuteOpArgs(x)); public static Tensor ceil(Tensor x, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("Ceil", name, args: new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Ceil", name, new ExecuteOpArgs(x)); public static Tensor sin(Tensor x, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Sin", name, - null, - x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Sin", name, args: new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Sin", name, new ExecuteOpArgs(x)); /// /// Computes sigmoid of x element-wise. @@ -315,10 +207,7 @@ namespace Tensorflow /// Specifically, y = 1 / (1 + exp(-x)). /// public static Tensor sigmoid(Tensor x, string name = "Sigmoid") - => tf.Context.ExecuteOp("Sigmoid", name, new AutoModeArgs - { - OpInputArgs = new { x } - }); + => tf.Context.ExecuteOp("Sigmoid", name, new ExecuteOpArgs(x)); /// /// Computes the gradient of the sigmoid of x wrt its input. @@ -338,27 +227,10 @@ namespace Tensorflow /// dy is the corresponding input gradient. /// public static Tensor sigmoid_grad(Tensor y, Tensor dy, string name = "SigmoidGrad") - => tf.Context.ExecuteOp("SigmoidGrad", name, new AutoModeArgs - { - OpInputArgs = new { y, dy } - }); + => tf.Context.ExecuteOp("SigmoidGrad", name, new ExecuteOpArgs(y, dy)); public static Tensor sign(T x, string name = "Sign") - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Sign", name, - null, - x); - - return results[0]; - } - - var op = tf.OpDefLib._apply_op_helper("Sign", name: name, args: new { x }); - - return op.outputs[0]; - } + => tf.Context.ExecuteOp("Sign", name, new ExecuteOpArgs(x)); public static Tensor sinh(Tensor x, string name = null) { @@ -368,21 +240,7 @@ namespace Tensorflow } public static Tensor cos(T x, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Cos", name, - null, - x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Cos", name, args: new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Cos", name, new ExecuteOpArgs(x)); public static Tensor cosh(Tensor x, string name = null) { @@ -413,38 +271,10 @@ namespace Tensorflow } public static Tensor tan(Tensor x, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Tan", name, - null, - x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Tan", name, args: new { x }); - - return _op.output; - } + => tf.Context.ExecuteOp("Tan", name, new ExecuteOpArgs(x)); public static Tensor tanh(Tensor x, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Tanh", name, - null, - x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Tanh", name, args: new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Tanh", name, new ExecuteOpArgs(x)); /// /// Computes the gradient for the tanh of `x` wrt its input. @@ -454,20 +284,7 @@ namespace Tensorflow /// /// public static Tensor tanh_grad(Tensor y, Tensor dy, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "TanhGrad", name, - null, - y, dy); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("TanhGrad", name: name, args: new { y, dy }).output; - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("TanhGrad", name, new ExecuteOpArgs(y, dy)); public static Tensor floor(Tensor x, string name = null) { @@ -484,21 +301,7 @@ namespace Tensorflow } public static Tensor greater(Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Greater", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Greater", name: name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Greater", name, new ExecuteOpArgs(x, y)); /// /// Computes the log of the absolute value of `Gamma(x)` element-wise. @@ -519,79 +322,22 @@ namespace Tensorflow } public static Tensor greater_equal(Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "GreaterEqual", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("GreaterEqual", name: name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("GreaterEqual", name, new ExecuteOpArgs(x, y)); public static Tensor less(Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Less", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Less", name: name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Less", name, new ExecuteOpArgs(x, y)); public static Tensor less_equal(Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "LessEqual", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("LessEqual", name: name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("LessEqual", name, new ExecuteOpArgs(x, y)); public static Tensor log1p(Tensor x, string name = null) - => tf.Context.ExecuteOp("Log1p", name, new AutoModeArgs - { - OpInputArgs = new { x } - }); + => 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 }); public static Tensor logical_and(bool x, bool y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "LogicalAnd", name, - null, - x, y); - - return results[0]; - } - - return tf.OpDefLib._apply_op_helper("LogicalAnd", name, args: new { x, y }); - } + => tf.Context.ExecuteOp("LogicalAnd", name, new ExecuteOpArgs(x, y)); public static Tensor logical_not(Tensor x, string name = null) { @@ -616,21 +362,7 @@ namespace Tensorflow } public static Tensor squared_difference(Tensor x, Tensor y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "SquaredDifference", name, - null, - x,y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("SquaredDifference", name, args: new { x, y, name }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("SquaredDifference", name, new ExecuteOpArgs(x, y)); /// /// Computes square of x element-wise. @@ -639,21 +371,7 @@ namespace Tensorflow /// A name for the operation (optional). /// A `Tensor`. Has the same type as `x`. public static Tensor square(Tensor x, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Square", name, - null, - x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Square", name, args: new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Square", name, new ExecuteOpArgs(x)); /// /// Returns which elements of x are finite. @@ -682,10 +400,7 @@ namespace Tensorflow /// A name for the operation (optional). /// A `Tensor`. Has the same type as `x`. public static Tensor exp(Tensor x, string name = null) - => tf.Context.ExecuteOp("Exp", name, new AutoModeArgs - { - OpInputArgs = new { x } - }); + => tf.Context.ExecuteOp("Exp", name, new ExecuteOpArgs(x)); /// /// Computes natural logarithm of x element-wise. @@ -694,101 +409,26 @@ namespace Tensorflow /// name: A name for the operation (optional). /// A `Tensor`. Has the same type as `x`. public static Tensor log(Tensor x, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Log", name, - null, - x); + => tf.Context.ExecuteOp("Log", name, new ExecuteOpArgs(x)); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Log", name, args: new { x }); - - return _op.outputs[0]; - } public static Tensor softplus(Tensor features, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Softplus", name, - null, - features); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Softplus", name, args: new { features }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Softplus", name, new ExecuteOpArgs(features)); public static Tensor cast(Tensor x, TF_DataType DstT, bool Truncate = false, string name = null) - => tf.Context.ExecuteOp("Cast", name, new AutoModeArgs - { - OpInputArgs = new { x }, - OpAttrs = new { DstT, Truncate } - }); + => tf.Context.ExecuteOp("Cast", name, new ExecuteOpArgs(x) + .SetAttributes(new { DstT, Truncate })); public static Tensor neg(Tensor x, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Neg", name, - null, - x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Neg", name, args: new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Neg", name, new ExecuteOpArgs(x)); public static Tensor sqrt(Tensor x, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Sqrt", name, - null, - x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Sqrt", name, args: new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Sqrt", name, new ExecuteOpArgs(x)); public static Tensor sub(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("Sub", name, new AutoModeArgs - { - OpInputArgs = new { x, y } - }); + => tf.Context.ExecuteOp("Sub", name, new ExecuteOpArgs(x, y)); public static Tensor sub(Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Sub", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Sub", name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Sub", name, new ExecuteOpArgs(x, y)); /// /// Returns the truth value of (x == y) element-wise. @@ -798,20 +438,7 @@ namespace Tensorflow /// /// public static Tensor equal(Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Equal", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Equal", name, args: new { x, y }); - return _op.output; - } + => tf.Context.ExecuteOp("Equal", name, new ExecuteOpArgs(x, y)); /// /// Returns the truth value of (x != y) element-wise. @@ -823,54 +450,13 @@ namespace Tensorflow /// The name. /// public static Tensor not_equal(Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "NotEqual", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("NotEqual", name, args: new { x, y }); - return _op.output; - } - + => tf.Context.ExecuteOp("NotEqual", name, new ExecuteOpArgs(x, y)); public static Tensor atan2(Tensor y, Tensor x, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Atan2", name, - null, - y, x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Atan2", name, args: new { y, x }); - return _op.output; - } + => tf.Context.ExecuteOp("Atan2", name, new ExecuteOpArgs(y, x)); public static Tensor mul(Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Mul", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Mul", name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Mul", name, new ExecuteOpArgs(x, y)); public static Tensor mul_no_nan(Tx x, Ty y, string name = null) { @@ -880,71 +466,16 @@ namespace Tensorflow } public static Tensor real_div(Tensor x, Tensor y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "RealDiv", name, - null, - x, y); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("RealDiv", name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("RealDiv", name, new ExecuteOpArgs(x, y)); public static Tensor reciprocal(Tensor x, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Reciprocal", name, - null, - x); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Reciprocal", name, args: new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Reciprocal", name, new ExecuteOpArgs(x)); public static Tensor floor_mod(Tensor x, Tensor y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "FloorMod", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("FloorMod", name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("FloorMod", name, new ExecuteOpArgs(x, y)); public static Tensor floor_div(Tensor x, Tensor y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "FloorDiv", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("FloorDiv", name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("FloorDiv", name, new ExecuteOpArgs(x, y)); /// /// Multiply the matrix "a" by the matrix "b". @@ -956,56 +487,12 @@ namespace Tensorflow /// /// public static Tensor mat_mul(Tensor a, Tensor b, bool transpose_a = false, bool transpose_b = false, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "MatMul", name, - null, - a, b, - "transpose_a", transpose_a, "transpose_b", transpose_b); - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("MatMul", name, args: new { a, b, transpose_a, transpose_b }); - - return _op.output; - } - - /// - /// Multiply slices of the two matrices "x" and "y". - /// - /// - /// The `BatchMatMul` operation is embedded into the - /// `MatMul` operation on the DLL side. However the expected - /// attributes are not the same, hence we need to expose this - /// method to have the right args list on the `_apply_op_helper` - /// function. - /// - /// For each rank > 2 the first rank - 2 dimensions are considered - /// as fixed, and have to be consistent across the two matrices. A - /// common matrix multiplication is then applied over the residual - /// 2 dimensions. - /// - /// e.g. - /// x is (3, 6, 12); y is (3, 12, 6) - /// batch_matmul(x, y) ==> (3, 6, 6) - /// - /// - /// - /// - /// - /// - /// - public static Tensor batch_mat_mul(Tensor x, Tensor y, bool adj_x = false, bool adj_y = false, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper( - "BatchMatMul", - name, - args: new { x, y, adj_x, adj_y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("MatMul", name, new ExecuteOpArgs(a, b) + .SetAttributes(new + { + transpose_a, + transpose_b + })); /// /// Returns the max of x and y (i.e. x > y ? x : y) element-wise. @@ -1015,54 +502,13 @@ namespace Tensorflow /// /// public static Tensor maximum(T1 x, T2 y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Maximum", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Maximum", name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Maximum", name, new ExecuteOpArgs(x, y)); public static Tensor minimum(T1 x, T2 y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Minimum", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Minimum", name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Minimum", name, new ExecuteOpArgs(x, y)); public static Tensor _abs(Tensor x, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Abs", name, - null, - x); - - return results[0]; - } - var _op = tf.OpDefLib._apply_op_helper("Abs", name, args: new { x }); - - return _op.output; - } + => tf.Context.ExecuteOp("Abs", name, new ExecuteOpArgs(x)); public static Tensor _any(Tx input, Ty axis, bool keep_dims = false, string name = null) { @@ -1072,17 +518,15 @@ namespace Tensorflow } public static Tensor _max(Tx input, Ty axis, bool keep_dims = false, string name = null) - => tf.Context.ExecuteOp("Max", name, new AutoModeArgs + => tf.Context.ExecuteOp("Max", name, new ExecuteOpArgs(input, axis) { - OpInputArgs = new { input, axis }, - OpAttrs = new { keep_dims, reduction_indices = axis }, GetGradientAttrs = (op) => new { T = op.get_attr("T"), align_corners = op.get_attr("align_corners"), half_pixel_centers = op.get_attr("half_pixel_centers") } - }); + }.SetAttributes(new { keep_dims, reduction_indices = axis })); public static Tensor _min(Tx input, Ty axis, bool keep_dims = false, string name = null) { @@ -1092,39 +536,11 @@ namespace Tensorflow } public static Tensor pow(Tx x, Ty y, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Pow", name, - null, - x, y); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Pow", name, args: new { x, y }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Pow", name, new ExecuteOpArgs(x, y)); public static Tensor _sum(Tx input, Ty axis = default, bool keep_dims = false, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Sum", name, - null, - input, axis, - "keep_dims", keep_dims); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Sum", name, args: new { input, reduction_indices = axis, keep_dims }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Sum", name, + new ExecuteOpArgs(input, axis).SetAttributes(new { keep_dims, reduction_indices = axis })); public static Tensor _sum(Tensor[] inputs, Tensor axis = default, bool keep_dims = false, string name = null) { @@ -1158,10 +574,7 @@ namespace Tensorflow /// /// public static Tensor range(Tensor start, Tensor limit, Tensor delta, string name = null) - => tf.Context.ExecuteOp("Range", name, new AutoModeArgs - { - OpInputArgs = new { start, limit, delta } - }); + => tf.Context.ExecuteOp("Range", name, new ExecuteOpArgs(start, limit, delta)); /// /// Rounds the values of a tensor to the nearest integer, element-wise. @@ -1192,20 +605,7 @@ namespace Tensorflow /// /// public static Tensor rsqrt(Tensor x, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Rsqrt", name, - null, - x); - - return results[0]; - } - var _op = tf.OpDefLib._apply_op_helper("Rsqrt", name, new { x }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("Rsqrt", name, new ExecuteOpArgs(x)); /// /// Returns the fraction of zeros in value. @@ -1214,10 +614,6 @@ namespace Tensorflow /// A name for the operation (optional). /// The fraction of zeros in value, with type float32. public static Tensor zero_fraction(Tensor value, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("zero_fraction", name, new { value, name }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("zero_fraction", name, new ExecuteOpArgs(value)); } } diff --git a/src/TensorFlowNET.Core/Operations/gen_math_ops.eager.cs b/src/TensorFlowNET.Core/Operations/gen_math_ops.eager.cs index f80b5f0f..8e6e72d1 100644 --- a/src/TensorFlowNET.Core/Operations/gen_math_ops.eager.cs +++ b/src/TensorFlowNET.Core/Operations/gen_math_ops.eager.cs @@ -6,13 +6,6 @@ namespace Tensorflow public static partial class gen_math_ops { public static Tensor mul(IntPtr x, IntPtr y, string name = null) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Mul", name, - null, - x, y); - - return results[0]; - } + => tf.Context.ExecuteOp("Mul", name, new ExecuteOpArgs(x, y)); } } diff --git a/src/TensorFlowNET.Core/Operations/gen_random_ops.cs b/src/TensorFlowNET.Core/Operations/gen_random_ops.cs index 8528f4c4..19d774d6 100644 --- a/src/TensorFlowNET.Core/Operations/gen_random_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_random_ops.cs @@ -29,31 +29,8 @@ namespace Tensorflow /// /// public static Tensor random_standard_normal(Tensor shape, TF_DataType dtype = TF_DataType.DtInvalid, int? seed = null, int? seed2 = null, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "RandomStandardNormal", name, - null, - shape, - "seed", seed, - "seed2", seed2, - "dtype", dtype); - - return results[0]; - } - - if (!seed.HasValue) - seed = 0; - if (!seed2.HasValue) - seed2 = 0; - - var _op = tf.OpDefLib._apply_op_helper("RandomStandardNormal", - name: name, - args: new { shape, dtype, seed, seed2 }); - - return _op.output; - } + => tf.Context.ExecuteOp("RandomStandardNormal", name, new ExecuteOpArgs(shape) + .SetAttributes(new { dtype, seed = seed ?? 0, seed2 = seed2 ?? 0 })); /// /// Outputs random integers from a uniform distribution. @@ -89,31 +66,8 @@ namespace Tensorflow /// /// public static Tensor random_uniform(Tensor shape, TF_DataType dtype, int? seed = 0, int? seed2 = 0, string name = null) - { - if (!seed.HasValue) - seed = 0; - if (!seed2.HasValue) - seed2 = 0; - - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "RandomUniform", name, - null, - shape, - "seed", seed, - "seed2", seed2, - "dtype", dtype); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("RandomUniform", - name: name, - args: new { shape, dtype, seed, seed2 }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("RandomUniform", name, new ExecuteOpArgs(shape) + .SetAttributes(new { dtype, seed = seed ?? 0, seed2 = seed2 ?? 0 })); /// /// @@ -125,23 +79,7 @@ namespace Tensorflow /// public static Tensor random_shuffle(Tensor value, int seed = 0, int seed2 = 0, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "RandomShuffle", name, - null, - value, seed, seed2); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("RandomShuffle", - name: name, - args: new { value, seed, seed2 }); - - return _op.output; - } + => tf.Context.ExecuteOp("RandomShuffle", name, new ExecuteOpArgs(value, seed, seed2)); /// /// Outputs random values from a truncated normal distribution. @@ -154,31 +92,8 @@ namespace Tensorflow /// public static Tensor truncated_normal(Tensor shape, TF_DataType dtype, int? seed = 0, int? seed2 = 0, string name = null) - { - if (!seed.HasValue) - seed = 0; - if (!seed2.HasValue) - seed2 = 0; - - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "TruncatedNormal", name, - null, - shape, - "seed", seed, - "seed2", seed2, - "dtype", dtype); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("TruncatedNormal", - name: name, - args: new { shape, dtype, seed, seed2 }); - - return _op.output; - } + => tf.Context.ExecuteOp("TruncatedNormal", name, new ExecuteOpArgs(shape) + .SetAttributes(new { dtype, seed = seed ?? 0, seed2 = seed2 ?? 0 })); public static Tensor multinomial(Tensor logits, int num_samples, int? seed = 0, int? seed2 = 0, TF_DataType output_dtype = TF_DataType.TF_INT64, string name = null) diff --git a/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs b/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs index a59dda67..e9c4a1f2 100644 --- a/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs @@ -24,10 +24,8 @@ namespace Tensorflow { if (tf.Context.executing_eagerly()) { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "AssignSubVariableOp", name, - null, - resource, value); + tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo( + "AssignSubVariableOp", name, resource, value)); return null; } @@ -46,10 +44,8 @@ namespace Tensorflow { if (tf.Context.executing_eagerly()) { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "AssignAddVariableOp", name, - null, - resource, value); + tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("AssignAddVariableOp", name, + resource, value)); return null; } @@ -63,10 +59,8 @@ namespace Tensorflow { if (tf.Context.executing_eagerly()) { - tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "AssignVariableOp", name, - null, - resource, value); + tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("AssignVariableOp", name, + resource, value)); return null; } @@ -80,10 +74,8 @@ namespace Tensorflow { if (tf.Context.executing_eagerly()) { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "VarIsInitializedOp", name, - null, - resource); + var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("VarIsInitializedOp", name, + resource)); return results[0]; } @@ -107,14 +99,17 @@ namespace Tensorflow { if (tf.Context.executing_eagerly()) { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "VarHandleOp", name, - null, - "container", container, - "shared_name", shared_name, - "dtype", dtype, - "shape", shape.dims, - "allowed_devices", new string[0]); + var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("VarHandleOp", name) + { + attrs = ConvertToDict(new + { + dtype, + shape = shape.dims, + container, + shared_name, + allowed_devices = new string[0] + }) + }); return results[0]; } @@ -131,26 +126,8 @@ namespace Tensorflow } public static Tensor destroy_resource_op(Tensor resource, bool ignore_lookup_error = true, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "DestroyResourceOp", name, - null, - resource, - "ignore_lookup_error", ignore_lookup_error); - - return results.Length == 0 ? null : results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("DestroyResourceOp", name, new - { - resource, - ignore_lookup_error - }); - - return _op.output; - } + => tf.Context.ExecuteOp("DestroyResourceOp", name, + new ExecuteOpArgs(resource).SetAttributes(new { ignore_lookup_error })); /// /// Reads the value of a variable. @@ -160,26 +137,8 @@ namespace Tensorflow /// /// public static Tensor read_variable_op(Tensor resource, TF_DataType dtype, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ReadVariableOp", name, - null, - resource, - "dtype", dtype); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("ReadVariableOp", name, new - { - resource, - dtype - }); - - return _op.output; - } + => tf.Context.ExecuteOp("ReadVariableOp", name, new ExecuteOpArgs(resource) + .SetAttributes(new { dtype })); public static Tensor resource_gather(Tensor resource, Tensor indices, TF_DataType dtype, int batch_dims = 0, bool validate_indices = true, string name = null) diff --git a/src/TensorFlowNET.Core/Operations/math_ops.cs b/src/TensorFlowNET.Core/Operations/math_ops.cs index b26d84d6..1ffa6cc4 100644 --- a/src/TensorFlowNET.Core/Operations/math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/math_ops.cs @@ -45,10 +45,7 @@ namespace Tensorflow => gen_math_ops.add(x, y, name); public static Tensor add_v2(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("AddV2", name, new AutoModeArgs - { - OpInputArgs = new { x, y } - }); + => tf.Context.ExecuteOp("AddV2", name, new ExecuteOpArgs(x, y)); public static Tensor add_v2(Tx x, Ty y, string name = null) => gen_math_ops.add_v2(x, y, name); @@ -261,19 +258,13 @@ namespace Tensorflow /// /// public static Tensor erf(Tensor x, string name = null) - => tf.Context.ExecuteOp("Erf", name, new AutoModeArgs - { - OpInputArgs = new { x } - }); + => tf.Context.ExecuteOp("Erf", name, new ExecuteOpArgs(x)); public static Tensor sqrt(Tensor x, string name = null) => gen_math_ops.sqrt(x, name: name); public static Tensor multiply(Tensor x, Tensor y, string name = null) - => tf.Context.ExecuteOp("Mul", name, new AutoModeArgs - { - OpInputArgs = new { x, y } - }); + => tf.Context.ExecuteOp("Mul", name, new ExecuteOpArgs(x, y)); public static Tensor multiply(Tx x, Ty y, string name = null) => gen_math_ops.mul(x, y, name: name); @@ -720,23 +711,10 @@ namespace Tensorflow => tf_with(ops.name_scope(name, "Pow", new { x, y }), scope => { name = scope; + var x_tensor = ops.convert_to_tensor(x, name: "x"); + var y_tensor = ops.convert_to_tensor(y, name: "y", dtype: x_tensor.dtype.as_base_dtype()); - if (tf.executing_eagerly()) - { - var x_tensor = ops.convert_to_tensor(x, name: "x"); - var y_tensor = ops.convert_to_tensor(y, name: "y", dtype: x_tensor.dtype.as_base_dtype()); - - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Pow", name, - null, - x_tensor, y_tensor); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Pow", name, args: new { x, y }); - - return _op.output; + return tf.Context.ExecuteOp("Pow", name, new ExecuteOpArgs(x_tensor, y_tensor)); }); public static Tensor range(object start, object limit = null, object delta = null, TF_DataType dtype = TF_DataType.DtInvalid, string name = "range") @@ -828,7 +806,7 @@ namespace Tensorflow x = ops.convert_to_tensor(x, name: "a"); y = ops.convert_to_tensor(y, name: "b"); - result = gen_math_ops.batch_mat_mul(x, y, adj_x, adj_y, name); + result = math_ops.batch_matmul(x, y, adj_x, adj_y, name); }); return result; diff --git a/src/TensorFlowNET.Core/Operations/string_ops.cs b/src/TensorFlowNET.Core/Operations/string_ops.cs index deba7be4..0bd32e7f 100644 --- a/src/TensorFlowNET.Core/Operations/string_ops.cs +++ b/src/TensorFlowNET.Core/Operations/string_ops.cs @@ -21,53 +21,13 @@ namespace Tensorflow public class string_ops { public Tensor lower(Tensor input, string encoding = "", string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "StringLower", name, - null, - input, encoding); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("StringLower", name: name, args: new - { - input, - encoding - }); - - return _op.output; - } + => tf.Context.ExecuteOp("StringLower", name, new ExecuteOpArgs(input, encoding)); public Tensor regex_replace(Tensor input, string pattern, string rewrite, bool replace_global = true, string name = null) - { - if (tf.Context.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "StaticRegexReplace", name, - null, - input, - "pattern", pattern, - "rewrite", rewrite, - "replace_global", replace_global); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("StaticRegexReplace", name: name, args: new - { - input, - pattern, - rewrite, - replace_global - }); - - return _op.output; - } - + => tf.Context.ExecuteOp("StaticRegexReplace", name, new ExecuteOpArgs(input) + .SetAttributes(new { pattern, rewrite, replace_global })); + /// /// Return substrings from `Tensor` of strings. /// @@ -79,28 +39,7 @@ namespace Tensorflow /// public Tensor substr(T input, int pos, int len, string @uint = "BYTE", string name = null) - { - if (tf.Context.executing_eagerly()) - { - var input_tensor = tf.constant(input); - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Substr", name, - null, - input, pos, len, - "unit", @uint); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Substr", name: name, args: new - { - input, - pos, - len, - unit = @uint - }); - - return _op.output; - } + => tf.Context.ExecuteOp("Substr", name, new ExecuteOpArgs(input, pos, len) + .SetAttributes(new { unit = @uint })); } } diff --git a/src/TensorFlowNET.Core/Training/gen_training_ops.cs b/src/TensorFlowNET.Core/Training/gen_training_ops.cs index 675f920f..abe85a14 100644 --- a/src/TensorFlowNET.Core/Training/gen_training_ops.cs +++ b/src/TensorFlowNET.Core/Training/gen_training_ops.cs @@ -21,46 +21,19 @@ namespace Tensorflow { public class gen_training_ops { - public static Operation resource_apply_adam(Tensor var, Tensor m, Tensor v, Tensor beta1_power, Tensor beta2_power, + public static Tensor resource_apply_adam(Tensor var, Tensor m, Tensor v, Tensor beta1_power, Tensor beta2_power, Tensor lr, Tensor beta1, Tensor beta2, Tensor epsilon, Tensor grad, bool use_locking = false, bool use_nesterov = false, string name = null) - { - if (tf.executing_eagerly()) - { - var result = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ResourceApplyAdam", name, - null, - var, m, v, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad, - "use_locking", use_locking, - "use_nesterov", use_nesterov); - return null; - } - - throw new NotImplementedException(""); - } + => tf.Context.ExecuteOp("ResourceApplyAdam", name, + new ExecuteOpArgs(var, m, v, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad) + .SetAttributes(new { use_locking, use_nesterov })); public static Tensor apply_adam(Tensor var, Tensor m, Tensor v, Tensor beta1_power, Tensor beta2_power, Tensor lr, Tensor beta1, Tensor beta2, Tensor epsilon, Tensor grad, bool use_locking = false, bool use_nesterov = false, string name = null) - { - var _op = tf.OpDefLib._apply_op_helper("ApplyAdam", name, new - { - var, - m, - v, - beta1_power, - beta2_power, - lr, - beta1, - beta2, - epsilon, - grad, - use_locking, - use_nesterov - }); - - return _op.outputs[0]; - } + => tf.Context.ExecuteOp("ApplyAdam", name, + new ExecuteOpArgs(var, m, v, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad) + .SetAttributes(new { use_locking, use_nesterov })); public static Tensor apply_gradient_descent(IVariableV1 var, Tensor alpha, Tensor delta, bool use_locking = false, string name = null) { @@ -75,27 +48,8 @@ namespace Tensorflow return _op.output; } - public static Operation resource_apply_gradient_descent(Tensor var, Tensor alpha, Tensor delta, bool use_locking = false, string name = null) - { - if (tf.executing_eagerly()) - { - var result = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "ResourceApplyGradientDescent", name, - null, - var, alpha, delta, - "use_locking", use_locking); - return null; - } - - var _op = tf.OpDefLib._apply_op_helper("ResourceApplyGradientDescent", name, new - { - var, - alpha, - delta, - use_locking - }); - - return _op; - } + public static Tensor resource_apply_gradient_descent(Tensor var, Tensor alpha, Tensor delta, bool use_locking = false, string name = null) + => tf.Context.ExecuteOp("ResourceApplyGradientDescent", name, + new ExecuteOpArgs(var, alpha, delta).SetAttributes(new { use_locking })); } } diff --git a/src/TensorFlowNET.Core/Variables/gen_state_ops.py.cs b/src/TensorFlowNET.Core/Variables/gen_state_ops.py.cs index df964d5f..8d8c0699 100644 --- a/src/TensorFlowNET.Core/Variables/gen_state_ops.py.cs +++ b/src/TensorFlowNET.Core/Variables/gen_state_ops.py.cs @@ -59,31 +59,8 @@ namespace Tensorflow bool validate_shape = true, bool use_locking = true, string name = null) - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Assign", name, - null, - @ref, value, - "validate_shape", validate_shape, - "use_locking", use_locking); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Assign", name: name, args: new { @ref, value, validate_shape, use_locking }); - - var _result = _op.outputs; - var _inputs_flat = _op.inputs; - - var _attrs = new Dictionary(); - _attrs["T"] = _op.get_attr("T"); - _attrs["validate_shape"] = _op.get_attr("validate_shape"); - _attrs["use_locking"] = _op.get_attr("use_locking"); - - return _result[0]; - } + => tf.Context.ExecuteOp("Assign", name, new ExecuteOpArgs(@ref, value) + .SetAttributes(new { validate_shape, use_locking })); public static Tensor assign_add(IVariableV1 @ref, T value, bool use_locking = false, string name = null) { diff --git a/src/TensorFlowNET.Keras/Activations/Activations.Relu.cs b/src/TensorFlowNET.Keras/Activations/Activations.Relu.cs index d4f08088..dfebfb29 100644 --- a/src/TensorFlowNET.Keras/Activations/Activations.Relu.cs +++ b/src/TensorFlowNET.Keras/Activations/Activations.Relu.cs @@ -4,21 +4,7 @@ namespace Tensorflow.Keras { public partial class Activations { - public Activation Relu = (features, name) => - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Relu", name, - null, - features); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Relu", name: name, args: new { features }); - - return _op.output; - }; + public Activation Relu = (features, name) + => tf.Context.ExecuteOp("Relu", name, new ExecuteOpArgs(features)); } } diff --git a/src/TensorFlowNET.Keras/Activations/Activations.Sigmoid.cs b/src/TensorFlowNET.Keras/Activations/Activations.Sigmoid.cs index 84220f4f..ad900bde 100644 --- a/src/TensorFlowNET.Keras/Activations/Activations.Sigmoid.cs +++ b/src/TensorFlowNET.Keras/Activations/Activations.Sigmoid.cs @@ -5,21 +5,7 @@ namespace Tensorflow.Keras { public partial class Activations { - public Activation Sigmoid = (features, name) => - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Sigmoid", name, - null, - features); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Sigmoid", name: name, args: new { x = features }); - - return _op.output; - }; + public Activation Sigmoid = (features, name) + => tf.Context.ExecuteOp("Sigmoid", name, new ExecuteOpArgs(features)); } } diff --git a/src/TensorFlowNET.Keras/Activations/Activations.Tanh.cs b/src/TensorFlowNET.Keras/Activations/Activations.Tanh.cs index 30bbdbf4..33dc5ba6 100644 --- a/src/TensorFlowNET.Keras/Activations/Activations.Tanh.cs +++ b/src/TensorFlowNET.Keras/Activations/Activations.Tanh.cs @@ -5,21 +5,7 @@ namespace Tensorflow.Keras { public partial class Activations { - public Activation Tanh = (features, name) => - { - if (tf.executing_eagerly()) - { - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, - "Tanh", name, - null, - features); - - return results[0]; - } - - var _op = tf.OpDefLib._apply_op_helper("Tanh", name: name, args: new { x = features }); - - return _op.output; - }; + public Activation Tanh = (features, name) + => tf.Context.ExecuteOp("Tanh", name, new ExecuteOpArgs(features)); } }