Skip to content

Commit b0c0a22

Browse files
committed
simplify wrap_tfe_src.SetOpAttrs parameters.
1 parent 13b1b63 commit b0c0a22

10 files changed

Lines changed: 184 additions & 70 deletions

File tree

‎src/TensorFlowNET.Core/APIs/tf.random.cs‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,27 @@ namespace Tensorflow
1818
{
1919
public partial class tensorflow
2020
{
21-
/// <summary>
22-
/// Outputs random values from a normal distribution.
23-
/// </summary>
24-
/// <param name="shape"></param>
25-
/// <param name="mean"></param>
26-
/// <param name="stddev"></param>
27-
/// <param name="dtype"></param>
28-
/// <param name="seed"></param>
29-
/// <param name="name"></param>
30-
/// <returns></returns>
31-
public Tensor random_normal(TensorShape shape,
32-
float mean = 0.0f,
33-
float stddev = 1.0f,
34-
TF_DataType dtype = TF_DataType.TF_FLOAT,
35-
int? seed = null,
36-
string name = null) => random_ops.random_normal(shape, mean, stddev, dtype, seed, name);
21+
public Random random => new Random();
22+
23+
public class Random
24+
{
25+
/// <summary>
26+
/// Outputs random values from a normal distribution.
27+
/// </summary>
28+
/// <param name="shape"></param>
29+
/// <param name="mean"></param>
30+
/// <param name="stddev"></param>
31+
/// <param name="dtype"></param>
32+
/// <param name="seed"></param>
33+
/// <param name="name"></param>
34+
/// <returns></returns>
35+
public Tensor normal(TensorShape shape,
36+
float mean = 0.0f,
37+
float stddev = 1.0f,
38+
TF_DataType dtype = TF_DataType.TF_FLOAT,
39+
int? seed = null,
40+
string name = null) => random_ops.random_normal(shape, mean, stddev, dtype, seed, name);
41+
}
3742

3843
public Tensor random_uniform(TensorShape shape,
3944
float minval = 0,

‎src/TensorFlowNET.Core/Eager/Execute.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Tensor execute(Context ctx, string op_name, int num_outputs,
4545
op_name,
4646
inputs.Select(x => (x as EagerTensor).GetTfeTensorHandle()).ToArray(),
4747
inputs.Length,
48-
op => wrap_tfe_src.SetOpAttrs(ctx, op, attrs, status),
48+
op => wrap_tfe_src.SetOpAttrs(op, attrs),
4949
outputs,
5050
num_outputs,
5151
status);

‎src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_FastPathExecute.cs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System;
44
using static Tensorflow.OpDef.Types;
5+
using static Tensorflow.Binding;
56

67
namespace Tensorflow.Eager
78
{
@@ -10,22 +11,23 @@ namespace Tensorflow.Eager
1011
/// </summary>
1112
public partial class wrap_tfe_src
1213
{
13-
public static void SetOpAttrs(Context ctx, TFE_Op op, object[] attrs, Status out_status)
14+
public static void SetOpAttrs(TFE_Op op, params object[] attrs)
1415
{
16+
using var status = new Status();
1517
var len = attrs.Length;
1618
for (int i = 0; i < len; i += 2)
1719
{
1820
var key = attrs[i].ToString();
1921
var value = attrs[i + 1];
2022

2123
byte is_list = 0;
22-
var type = c_api.TFE_OpGetAttrType(op, key, ref is_list, out_status);
23-
if (!out_status.ok()) return;
24+
var type = c_api.TFE_OpGetAttrType(op, key, ref is_list, status);
25+
if (!status.ok()) return;
2426
if (is_list != 0)
25-
SetOpAttrList(ctx, op, key, value, type, null, out_status);
27+
SetOpAttrList(tf.context, op, key, value, type, null, status);
2628
else
27-
SetOpAttrScalar(ctx, op, key, value, type, null, out_status);
28-
out_status.Check(true);
29+
SetOpAttrScalar(tf.context, op, key, value, type, null, status);
30+
status.Check(true);
2931
}
3032
}
3133

‎src/TensorFlowNET.Core/Operations/gen_array_ops.cs‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public static Tensor pack(Tensor[] values, int axis = 0, string name = null)
165165
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
166166
"Pack", name,
167167
values.Select(x => (x as EagerTensor).EagerTensorHandle).ToArray(), values.Length,
168-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "axis", axis } , status),
168+
op => wrap_tfe_src.SetOpAttrs(op, "axis", axis),
169169
status);
170170
status.Check(true);
171171
return new EagerTensor(tensor);
@@ -421,11 +421,8 @@ public static Tensor shape(Tensor input, TF_DataType out_type = TF_DataType.TF_I
421421
"Shape", name, new IntPtr[]
422422
{
423423
input as EagerTensor,
424-
}, 1,
425-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[]
426-
{
427-
"out_type", out_type
428-
}, status),
424+
}, 1,
425+
op => wrap_tfe_src.SetOpAttrs(op, "out_type", out_type),
429426
status);
430427
status.Check(true);
431428
return tensor;
@@ -531,14 +528,12 @@ public static Tensor strided_slice(Tensor input, Tensor begin, Tensor end, Tenso
531528
end as EagerTensor,
532529
strides as EagerTensor,
533530
}, 4,
534-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[]
535-
{
531+
op => wrap_tfe_src.SetOpAttrs(op,
536532
"begin_mask", begin_mask,
537533
"end_mask", end_mask,
538534
"ellipsis_mask", ellipsis_mask,
539535
"new_axis_mask", new_axis_mask,
540-
"shrink_axis_mask", shrink_axis_mask
541-
}, status),
536+
"shrink_axis_mask", shrink_axis_mask),
542537
status);
543538
status.Check(true);
544539
return tensor;

‎src/TensorFlowNET.Core/Operations/gen_math_ops.cs‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ public static Tensor add_n(Tensor[] inputs, string name = null)
4444
if (tf.context.executing_eagerly())
4545
{
4646
using var status = new Status();
47-
var _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
47+
EagerTensorHandle _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
4848
"AddN", name,
4949
inputs.Select(x => (x as EagerTensor).EagerTensorHandle).ToArray(), inputs.Length,
5050
null,
5151
status);
5252
status.Check(true);
53-
return new EagerTensor(_result);
53+
return _result;
5454
}
5555

5656
var _op = _op_def_lib._apply_op_helper("AddN", name, args: new { inputs });
@@ -132,17 +132,17 @@ public static Tensor mean<T1, T2>(T1 input, T2 axis, bool keep_dims= false, stri
132132
if (tf.context.executing_eagerly())
133133
{
134134
using var status = new Status();
135-
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
135+
EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
136136
"Mean", name,
137137
new IntPtr[]
138138
{
139139
input as EagerTensor,
140140
axis as EagerTensor
141141
}, 2,
142-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "keep_dims", keep_dims }, status),
142+
op => wrap_tfe_src.SetOpAttrs(op, "keep_dims", keep_dims),
143143
status);
144144
status.Check(true);
145-
return new EagerTensor(tensor);
145+
return tensor;
146146
}
147147

148148
var _op = _op_def_lib._apply_op_helper("Mean", name, args: new { input, reduction_indices = axis, keep_dims = keep_dims });
@@ -185,10 +185,7 @@ public static Tensor prod<T1, T2>(T1 input, T2 axis, bool keep_dims = false, str
185185
input as EagerTensor,
186186
axis as EagerTensor
187187
}, 2,
188-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[]
189-
{
190-
"keep_dims", keep_dims
191-
}, status),
188+
op => wrap_tfe_src.SetOpAttrs(op, "keep_dims", keep_dims),
192189
status);
193190
status.Check(true);
194191
return tensor;
@@ -232,14 +229,14 @@ public static Tensor add(Tensor x, Tensor y, string name = null)
232229
if (tf.context.executing_eagerly())
233230
{
234231
using var status = new Status();
235-
var _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
232+
EagerTensorHandle _result = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
236233
"Add", name, new IntPtr[]
237234
{
238235
x as EagerTensor,
239236
y as EagerTensor
240237
}, 2, null, status);
241238
status.Check(true);
242-
return new EagerTensor(_result);
239+
return _result;
243240
}
244241

245242
var _op = _op_def_lib._apply_op_helper("Add", name, args: new { x, y });
@@ -273,14 +270,14 @@ public static Tensor add_v2<Tx, Ty>(Tx x, Ty y, string name = null)
273270
if (tf.context.executing_eagerly())
274271
{
275272
using var status = new Status();
276-
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
273+
EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
277274
"AddV2", name, new IntPtr[]
278275
{
279276
x as EagerTensor,
280277
y as EagerTensor
281278
}, 2, null, status);
282279
status.Check(true);
283-
return new EagerTensor(tensor);
280+
return tensor;
284281
}
285282

286283
var _op = _op_def_lib._apply_op_helper("AddV2", name, args: new { x, y });
@@ -574,6 +571,18 @@ public static Tensor squared_difference(Tensor x, Tensor y, string name = null)
574571
/// <returns> A `Tensor`. Has the same type as `x`.</returns>
575572
public static Tensor square(Tensor x, string name = null)
576573
{
574+
if (tf.context.executing_eagerly())
575+
{
576+
using var status = new Status();
577+
EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
578+
"Square", name, new IntPtr[]
579+
{
580+
x as EagerTensor,
581+
}, 1, null, status);
582+
status.Check(true);
583+
return tensor;
584+
}
585+
577586
var _op = _op_def_lib._apply_op_helper("Square", name, args: new { x });
578587

579588
return _op.outputs[0];
@@ -633,7 +642,7 @@ public static Tensor cast(Tensor x, TF_DataType DstT, bool Truncate= false, stri
633642
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
634643
"Cast", name,
635644
new IntPtr[] { x as EagerTensor }, 1,
636-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "DstT", DstT, "Truncate", Truncate }, status),
645+
op => wrap_tfe_src.SetOpAttrs(op, "DstT", DstT, "Truncate", Truncate),
637646
status);
638647
status.Check(true);
639648
return new EagerTensor(tensor);
@@ -918,11 +927,9 @@ public static Tensor mat_mul(Tensor a, Tensor b, bool transpose_a = false, bool
918927
a as EagerTensor,
919928
b as EagerTensor
920929
}, 2,
921-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[]
922-
{
930+
op => wrap_tfe_src.SetOpAttrs(op,
923931
"transpose_a", transpose_a,
924-
"transpose_b", transpose_b
925-
}, status),
932+
"transpose_b", transpose_b),
926933
status);
927934
status.Check(true);
928935
return new EagerTensor(tensor);
@@ -1049,7 +1056,7 @@ public static Tensor _sum<Tx, Ty>(Tx input, Ty axis = default, bool keep_dims =
10491056
input as EagerTensor,
10501057
axis as EagerTensor
10511058
}, 2,
1052-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "keep_dims", keep_dims }, status),
1059+
op => wrap_tfe_src.SetOpAttrs(op, "keep_dims", keep_dims),
10531060
status);
10541061
status.Check(true);
10551062
return new EagerTensor(tensor);

‎src/TensorFlowNET.Core/Operations/gen_random_ops.cs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ You may obtain a copy of the License at
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
******************************************************************************/
16+
using System;
17+
using Tensorflow.Eager;
18+
using static Tensorflow.Binding;
1619

1720
namespace Tensorflow
1821
{
@@ -36,6 +39,23 @@ public static Tensor random_standard_normal(Tensor shape, TF_DataType dtype = TF
3639
if (!seed2.HasValue)
3740
seed2 = 0;
3841

42+
if (tf.context.executing_eagerly())
43+
{
44+
using var status = new Status();
45+
EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
46+
"RandomStandardNormal", name, new IntPtr[]
47+
{
48+
shape as EagerTensor,
49+
}, 1,
50+
op => wrap_tfe_src.SetOpAttrs(op,
51+
"seed", seed,
52+
"seed2", seed2,
53+
"dtype", dtype),
54+
status);
55+
status.Check(true);
56+
return tensor;
57+
}
58+
3959
var _op = _op_def_lib._apply_op_helper("RandomStandardNormal",
4060
name: name,
4161
args: new { shape, dtype, seed, seed2 });

‎src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ public static class gen_resource_variable_ops
2525
{
2626
public static OpDefLibrary _op_def_lib = new OpDefLibrary();
2727

28+
public static Operation assign_sub_variable_op(Tensor resource, Tensor value, string name = null)
29+
{
30+
if (tf.context.executing_eagerly())
31+
{
32+
using var status = new Status();
33+
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
34+
"AssignSubVariableOp", name,
35+
new IntPtr[]
36+
{
37+
resource as EagerTensor,
38+
value as EagerTensor
39+
}, 2, null, status);
40+
status.Check(true);
41+
return tensor;
42+
}
43+
44+
return null;
45+
}
46+
2847
public static Operation assign_variable_op(Tensor resource, Tensor value, string name = null)
2948
{
3049
if (tf.context.executing_eagerly())
@@ -51,12 +70,12 @@ public static Tensor var_is_initialized_op(Tensor resource, string name = null)
5170
if (tf.context.executing_eagerly())
5271
{
5372
using var status = new Status();
54-
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
73+
EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
5574
"VarIsInitializedOp", name,
5675
new IntPtr[] { resource as EagerTensor },
5776
1, null, status);
5877
status.Check(true);
59-
return new EagerTensor(tensor);
78+
return tensor;
6079
}
6180

6281
var _op = _op_def_lib._apply_op_helper("VarIsInitializedOp", name, new { resource });
@@ -79,18 +98,16 @@ public static Tensor var_handle_op(TF_DataType dtype, TensorShape shape,
7998
if (tf.context.executing_eagerly())
8099
{
81100
using var status = new Status();
82-
var tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
101+
EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
83102
"VarHandleOp", name, null, 0,
84-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[]
85-
{
103+
op => wrap_tfe_src.SetOpAttrs(op,
86104
"container", container,
87105
"shared_name", shared_name,
88106
"dtype", dtype,
89-
"shape", shape.dims
90-
}, status),
107+
"shape", shape.dims),
91108
status);
92109
status.Check(true);
93-
return new EagerTensor(tensor);
110+
return tensor;
94111
}
95112

96113
var _op = _op_def_lib._apply_op_helper("VarHandleOp", name, new {
@@ -118,7 +135,7 @@ public static Tensor read_variable_op(Tensor resource, TF_DataType dtype, string
118135
EagerTensorHandle tensor = c_api.TFE_FastPathExecute(tf.context, tf.context.device_name,
119136
"ReadVariableOp", name,
120137
new IntPtr[] { resource as EagerTensor }, 1,
121-
op => wrap_tfe_src.SetOpAttrs(tf.context, op, new object[] { "dtype", dtype }, status),
138+
op => wrap_tfe_src.SetOpAttrs(op, "dtype", dtype),
122139
status);
123140
status.Check(true);
124141
return tensor;

‎src/TensorFlowNET.Core/Operations/random_ops.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static Tensor random_normal(int[] shape,
4747
var rnd = gen_random_ops.random_standard_normal(shape_tensor, dtype: dtype, seed: seed1, seed2: seed2);
4848
var mul = rnd * stddev_tensor;
4949
var value = math_ops.add(mul, mean_tensor, name: name);
50+
// tensor_util.maybe_set_static_shape(value, shape)
5051
return value;
5152
});
5253
}

0 commit comments

Comments
 (0)