Skip to content

Commit af9b64c

Browse files
committed
reousrce variable.
1 parent da54251 commit af9b64c

15 files changed

Lines changed: 116 additions & 33 deletions

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Context : DisposableObject
99

1010
public int default_execution_mode;
1111
public string device_name = "";
12+
public string scope_name = "";
1213
bool _initialized = false;
1314

1415
public Context(ContextOptions opts, Status status)
@@ -33,6 +34,11 @@ protected sealed override void DisposeUnmanagedResources(IntPtr handle)
3334

3435
public bool executing_eagerly() => true;
3536

37+
public string shared_name(string name = null)
38+
=> !string.IsNullOrEmpty(name) || !executing_eagerly() ?
39+
name :
40+
"cd2c89b7-88b7-44c8-ad83-06c2a9158347";
41+
3642
public static implicit operator IntPtr(Context ctx)
3743
=> ctx._handle;
3844

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public static implicit operator IntPtr(TFE_Op tensor)
1818
=> tensor._handle;
1919

2020
public override string ToString()
21-
=> $"TFE_Op {_handle}";
21+
=> $"TFE_Op 0x{_handle.ToString("x16")}";
2222
}
2323
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public static implicit operator IntPtr(TFE_TensorHandle tensor)
1818
=> tensor._handle;
1919

2020
public override string ToString()
21-
=> $"TFE_TensorHandle {_handle}";
21+
=> $"TFE_TensorHandle 0x{_handle.ToString("x16")}";
2222
}
2323
}

‎src/TensorFlowNET.Core/Eager/c_api.eager.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public partial class c_api
8989
/// <param name="num_retvals">int*</param>
9090
/// <param name="status">TF_Status*</param>
9191
[DllImport(TensorFlowLibName)]
92-
public static extern void TFE_Execute(TFE_Op op, IntPtr[] retvals, ref int num_retvals, IntPtr status);
92+
public static extern void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, IntPtr status);
9393

9494
/// <summary>
9595
///

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ private static bool AddInputToOp(object inputs,
147147

148148
c_api.TFE_OpAddInput(op, input_handle, status);
149149
status.Check(true);
150+
150151
return true;
151152
}
152153

@@ -236,8 +237,13 @@ private static bool SetOpAttrScalar(Context ctx, IntPtr op,
236237
case TF_AttrType.TF_ATTR_INT:
237238
c_api.TFE_OpSetAttrInt(op, key, Convert.ToInt64(value));
238239
break;
240+
case TF_AttrType.TF_ATTR_SHAPE:
241+
var dims = (value as int[]).Select(x => (long)x).ToArray();
242+
c_api.TFE_OpSetAttrShape(op, key, dims, dims.Length, status);
243+
status.Check(true);
244+
break;
239245
default:
240-
throw new NotImplementedException("");
246+
throw new NotImplementedException($"SetOpAttrScalar for {type}");
241247
}
242248

243249
return true;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using Tensorflow.Eager;
18+
using static Tensorflow.Binding;
19+
1720
namespace Tensorflow
1821
{
1922
public static class gen_resource_variable_ops
@@ -29,6 +32,14 @@ public static Operation assign_variable_op(Tensor resource, Tensor value, string
2932

3033
public static Tensor var_is_initialized_op(Tensor resource, string name = null)
3134
{
35+
if (tf.context.executing_eagerly())
36+
{
37+
var _result = wrap_tfe_src.TFE_FastPathExecute(tf.context, tf.context.device_name,
38+
"VarIsInitializedOp", name, null,
39+
resource);
40+
return _result;
41+
}
42+
3243
var _op = _op_def_lib._apply_op_helper("VarIsInitializedOp", name, new { resource });
3344

3445
return _op.output;
@@ -46,6 +57,14 @@ public static Tensor var_is_initialized_op(Tensor resource, string name = null)
4657
public static Tensor var_handle_op(TF_DataType dtype, TensorShape shape,
4758
string container ="", string shared_name = "", string name = null)
4859
{
60+
if (tf.context.executing_eagerly())
61+
{
62+
var _result = wrap_tfe_src.TFE_FastPathExecute(tf.context, tf.context.device_name,
63+
"VarHandleOp", name, null,
64+
"container", container, "shared_name", shared_name, "dtype", dtype, "shape", shape.dims);
65+
return _result;
66+
}
67+
4968
var _op = _op_def_lib._apply_op_helper("VarHandleOp", name, new {
5069
dtype,
5170
shape,

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ public static Tensor eager_safe_variable_handle(Tensor initial_value, TensorShap
100100
/// <param name="shared_name"></param>
101101
/// <param name="name"></param>
102102
/// <param name="graph_mode"></param>
103-
/// <param name="extra_handle_data"></param>
103+
/// <param name="initial_value"></param>
104104
/// <returns></returns>
105105
public static Tensor variable_handle_from_shape_and_dtype(TensorShape shape, TF_DataType dtype,
106-
string shared_name, string name, bool graph_mode, Tensor extra_handle_data = null)
106+
string shared_name, string name, bool graph_mode, Tensor initial_value = null)
107107
{
108108
var container = "";// ops.get_default_graph().container;
109109
var handle = gen_resource_variable_ops.var_handle_op(shape: shape,
@@ -112,17 +112,22 @@ public static Tensor variable_handle_from_shape_and_dtype(TensorShape shape, TF_
112112
name: name,
113113
container: container);
114114

115-
if (extra_handle_data == null)
116-
extra_handle_data = handle;
115+
if (initial_value == null)
116+
initial_value = handle;
117117

118118
if (graph_mode)
119119
{
120-
var full_handle_data = _combine_handle_data(handle, extra_handle_data);
120+
var full_handle_data = _combine_handle_data(handle, initial_value);
121121
_set_handle_shapes_and_types(handle, full_handle_data, graph_mode);
122122
return handle;
123123
}
124124
else
125125
{
126+
// We do not want two distinct ResourceVariable objects for the same
127+
// underlying resource in the runtime.
128+
// When in eager mode, explicitly ensure so here. When in graph mode, it's
129+
// ensured by always generating different variable names.
130+
var exists = gen_resource_variable_ops.var_is_initialized_op(handle);
126131
throw new NotImplementedException("");
127132
}
128133
}

‎src/TensorFlowNET.Core/Status/Status.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,8 @@ public static implicit operator IntPtr(Status status)
7171

7272
protected override void DisposeUnmanagedResources(IntPtr handle)
7373
=> TF_DeleteStatus(handle);
74+
75+
public override string ToString()
76+
=> $"{Code} 0x{_handle.ToString("x16")}";
7477
}
7578
}

‎src/TensorFlowNET.Core/Summaries/Summary.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ public void collect(ITensorOrOperation val, List<string> collections, List<strin
9090
string scope_base_name = string.IsNullOrEmpty(family) ? name : $"{family}/{name}";
9191
return tf_with(ops.name_scope(scope_base_name, default_name: default_name, values), scope =>
9292
{
93-
var tag = scope._name_scope;
93+
var tag = scope.scope_name;
9494
if (string.IsNullOrEmpty(family))
9595
tag = tag.Remove(tag.Length - 1);
9696
else
9797
tag = $"{family}/{tag.Remove(tag.Length - 1)}";
9898

99-
return (tag, scope._name_scope);
99+
return (tag, scope.scope_name);
100100
});
101101
}
102102
}

‎src/TensorFlowNET.Core/Tensors/Tensor.Value.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ private unsafe NDArray GetScalar(TF_DataType dtype)
181181
return (NDArray)StringData()[0];
182182
case TF_DataType.TF_INT32:
183183
return *(int*)buffer;
184+
case TF_DataType.TF_FLOAT:
185+
return *(float*)buffer;
184186
case TF_DataType.TF_DOUBLE:
185187
return *(double*)buffer;
186188
default:

0 commit comments

Comments
 (0)