Skip to content

Commit 66f7e6d

Browse files
committed
Can't get Output Context fixed SciSharp#411
1 parent d216d8b commit 66f7e6d

4 files changed

Lines changed: 67 additions & 27 deletions

File tree

‎src/TensorFlowNET.Core/Operations/Operation.Input.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public InputList inputs
5353
for (int i = 0; i < NumInputs; i++)
5454
{
5555
var tf_output = Input(i);
56-
var op = new Operation(tf_output.oper);
56+
var op = GetOperation(tf_output.oper);
5757
retval[i] = op.outputs[tf_output.index];
5858
}
5959

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*****************************************************************************
2+
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using System;
18+
using System.Collections.Generic;
19+
20+
namespace Tensorflow
21+
{
22+
public partial class Operation
23+
{
24+
// cache the mapping between managed and unmanaged op
25+
// some data is stored in managed instance, so when
26+
// create Operation by IntPtr, it will lost some data.
27+
private static Dictionary<IntPtr, Operation> OpInstances = new Dictionary<IntPtr, Operation>();
28+
29+
/// <summary>
30+
/// Get operation by handle
31+
/// </summary>
32+
/// <param name="handle"></param>
33+
/// <returns></returns>
34+
public Operation GetOperation(IntPtr handle)
35+
{
36+
return OpInstances.ContainsKey(handle) ?
37+
OpInstances[handle] :
38+
new Operation(handle);
39+
}
40+
}
41+
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ public Operation(IntPtr handle, Graph g = null)
8484
_control_flow_context = _graph._get_control_flow_context();
8585

8686
// Note: _control_flow_post_processing() must not be called here, the caller is responsible for calling it when using this constructor.
87+
OpInstances[_handle] = this;
8788
}
8889

89-
public Operation(Graph g, string opType, string oper_name)
90+
/*public Operation(Graph g, string opType, string oper_name)
9091
{
9192
_graph = g;
9293
@@ -102,7 +103,7 @@ public Operation(Graph g, string opType, string oper_name)
102103
// Dict mapping op name to file and line information for op colocation
103104
// context managers.
104105
_control_flow_context = graph._get_control_flow_context();
105-
}
106+
}*/
106107

107108
/// <summary>
108109
/// Creates an `Operation`.
@@ -151,11 +152,6 @@ public Operation(NodeDef node_def, Graph g, Tensor[] inputs = null, TF_DataType[
151152
}
152153
}
153154

154-
if(node_def.Name == "define_loss/conv_lobj_branch/batch_normalization/cond/FusedBatchNorm_1")
155-
{
156-
157-
}
158-
159155
// Dict mapping op name to file and line information for op colocation
160156
// context managers.
161157
_control_flow_context = graph._get_control_flow_context();
@@ -164,7 +160,7 @@ public Operation(NodeDef node_def, Graph g, Tensor[] inputs = null, TF_DataType[
164160
if (op_def == null)
165161
op_def = g.GetOpDef(node_def.Op);
166162

167-
var grouped_inputs = _reconstruct_sequence_inputs(op_def, inputs, node_def.Attr);
163+
var grouped_inputs = _reconstruct_sequence_inputs(op_def, inputs, node_def.Attr);
168164
_handle = ops._create_c_op(g, node_def, grouped_inputs, control_input_ops.ToArray());
169165

170166
// Initialize self._outputs.
@@ -180,6 +176,8 @@ public Operation(NodeDef node_def, Graph g, Tensor[] inputs = null, TF_DataType[
180176

181177
if (_handle != IntPtr.Zero)
182178
_control_flow_post_processing();
179+
180+
OpInstances[_handle] = this;
183181
}
184182

185183
public void run(FeedItem[] feed_dict = null, Session session = null)

‎src/TensorFlowNET.Core/ops.cs‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,29 +227,30 @@ public static IntPtr _create_c_op<T>(Graph graph, NodeDef node_def, T[] inputs,
227227
throw new NotImplementedException("_create_c_op");
228228
}
229229

230-
var status = new Status();
231-
232-
// Add control inputs
233-
foreach (var control_input in control_inputs)
234-
c_api.TF_AddControlInput(op_desc, control_input);
235-
236-
// Add attrs
237-
foreach (var attr in node_def.Attr)
230+
using (var status = new Status())
238231
{
239-
var bytes = attr.Value.ToByteArray(); //TODO: we can use attr.Value.WriteTo with a memory stream.
240-
var proto = Marshal.AllocHGlobal(bytes.Length); //TODO: potential memory leak
241-
Marshal.Copy(bytes, 0, proto, bytes.Length);
242-
uint len = (uint) bytes.Length;
243-
c_api.TF_SetAttrValueProto(op_desc, attr.Key, proto, proto_len: len, status: status);
232+
// Add control inputs
233+
foreach (var control_input in control_inputs)
234+
c_api.TF_AddControlInput(op_desc, control_input);
244235

245-
status.Check(true);
246-
}
236+
// Add attrs
237+
foreach (var attr in node_def.Attr)
238+
{
239+
var bytes = attr.Value.ToByteArray(); //TODO: we can use attr.Value.WriteTo with a memory stream.
240+
var protoHandle = Marshal.AllocHGlobal(bytes.Length);
241+
Marshal.Copy(bytes, 0, protoHandle, bytes.Length);
242+
uint len = (uint)bytes.Length;
243+
c_api.TF_SetAttrValueProto(op_desc, attr.Key, protoHandle, proto_len: len, status: status);
244+
status.Check(true);
245+
Marshal.FreeHGlobal(protoHandle);
246+
}
247247

248-
var c_op = c_api.TF_FinishOperation(op_desc, status);
248+
var c_op = c_api.TF_FinishOperation(op_desc, status);
249249

250-
status.Check(true);
250+
status.Check(true);
251251

252-
return c_op;
252+
return c_op;
253+
}
253254
}
254255
}
255256

0 commit comments

Comments
 (0)