@@ -22,58 +22,54 @@ limitations under the License.
2222using Tensorflow . Util ;
2323
2424namespace Tensorflow
25- {
26-
27- /// <summary>
28- /// Represents a graph node that performs computation on tensors.
29- ///
30- /// An `Operation` is a node in a TensorFlow `Graph` that takes zero or
31- /// more `Tensor` objects as input, and produces zero or more `Tensor`
32- /// objects as output. Objects of type `Operation` are created by
33- /// calling an op constructor(such as `tf.matmul`)
34- /// or `tf.Graph.create_op`.
35- ///
36- /// For example `c = tf.matmul(a, b)` creates an `Operation` of type
37- /// "MatMul" that takes tensors `a` and `b` as input, and produces `c`
38- /// as output.
39- ///
40- /// After the graph has been launched in a session, an `Operation` can
41- /// be executed by passing it to
42- /// `tf.Session.run`.
43- /// `op.run()` is a shortcut for calling `tf.get_default_session().run(op)`.
25+ {
26+ /// <summary>
27+ /// Represents a graph node that performs computation on tensors.
28+ ///
29+ /// An `Operation` is a node in a TensorFlow `Graph` that takes zero or
30+ /// more `Tensor` objects as input, and produces zero or more `Tensor`
31+ /// objects as output. Objects of type `Operation` are created by
32+ /// calling an op constructor(such as `tf.matmul`)
33+ /// or `tf.Graph.create_op`.
34+ ///
35+ /// For example `c = tf.matmul(a, b)` creates an `Operation` of type
36+ /// "MatMul" that takes tensors `a` and `b` as input, and produces `c`
37+ /// as output.
38+ ///
39+ /// After the graph has been launched in a session, an `Operation` can
40+ /// be executed by passing it to
41+ /// `tf.Session.run`.
42+ /// `op.run()` is a shortcut for calling `tf.get_default_session().run(op)`.
4443 /// </summary>
4544 public partial class Operation : ITensorOrOperation
4645 {
4746 private readonly IntPtr _handle ; // _c_op in python
48- private readonly IntPtr _operDesc ;
47+ private readonly IntPtr _operDesc ;
48+ private readonly Graph _graph ;
49+ private NodeDef _node_def ;
4950
50- private Graph _graph ;
5151 public string type => OpType ;
52-
5352 public Graph graph => _graph ;
5453 public int _id => _id_value ;
5554 public int _id_value ;
5655 public Operation op => this ;
57-
5856 public TF_DataType dtype => TF_DataType . DtInvalid ;
59-
6057 public string name => _handle == IntPtr . Zero ? null : c_api . StringPiece ( c_api . TF_OperationName ( _handle ) ) ;
6158 public string OpType => _handle == IntPtr . Zero ? null : c_api . StringPiece ( c_api . TF_OperationOpType ( _handle ) ) ;
6259 public string Device => _handle == IntPtr . Zero ? null : c_api . StringPiece ( c_api . TF_OperationDevice ( _handle ) ) ;
6360
64- private NodeDef _node_def ;
6561 public NodeDef node_def
6662 {
6763 get
6864 {
69- if ( _node_def == null )
65+ if ( _node_def == null )
7066 _node_def = GetNodeDef ( ) ;
7167
7268 return _node_def ;
7369 }
7470 }
7571
76- public Operation ( IntPtr handle , Graph g = null )
72+ public Operation ( IntPtr handle , Graph g = null )
7773 {
7874 if ( handle == IntPtr . Zero )
7975 return ;
@@ -97,14 +93,15 @@ public Operation(Graph g, string opType, string oper_name)
9793
9894 _operDesc = c_api . TF_NewOperation ( g , opType , oper_name ) ;
9995 c_api . TF_SetAttrType ( _operDesc , "dtype" , TF_DataType . TF_INT32 ) ;
100- using ( var status = new Status ( ) )
101- {
102- _handle = c_api . TF_FinishOperation ( _operDesc , status ) ;
103- status . Check ( true ) ;
104- }
105-
106- // Dict mapping op name to file and line information for op colocation
107- // context managers.
96+ lock ( Locks . ProcessWide )
97+ using ( var status = new Status ( ) )
98+ {
99+ _handle = c_api . TF_FinishOperation ( _operDesc , status ) ;
100+ status . Check ( true ) ;
101+ }
102+
103+ // Dict mapping op name to file and line information for op colocation
104+ // context managers.
108105 _control_flow_context = graph . _get_control_flow_context ( ) ;
109106 }
110107
@@ -133,9 +130,9 @@ public Operation(NodeDef node_def, Graph g, Tensor[] inputs = null, TF_DataType[
133130
134131 // Build the list of control inputs.
135132 var control_input_ops = new List < Operation > ( ) ;
136- if ( control_inputs != null )
133+ if ( control_inputs != null )
137134 {
138- foreach ( var c in control_inputs )
135+ foreach ( var c in control_inputs )
139136 {
140137 switch ( c )
141138 {
@@ -196,15 +193,13 @@ private object[] _reconstruct_sequence_inputs(OpDef op_def, Tensor[] inputs, Map
196193 {
197194 if ( ! string . IsNullOrEmpty ( input_arg . NumberAttr ) )
198195 {
199- input_len = ( int ) attrs [ input_arg . NumberAttr ] . I ;
196+ input_len = ( int ) attrs [ input_arg . NumberAttr ] . I ;
200197 is_sequence = true ;
201- }
202- else if ( ! string . IsNullOrEmpty ( input_arg . TypeListAttr ) )
198+ } else if ( ! string . IsNullOrEmpty ( input_arg . TypeListAttr ) )
203199 {
204200 input_len = attrs [ input_arg . TypeListAttr ] . List . Type . Count ;
205201 is_sequence = true ;
206- }
207- else
202+ } else
208203 {
209204 input_len = 1 ;
210205 is_sequence = false ;
@@ -225,22 +220,21 @@ public object get_attr(string name)
225220 {
226221 AttrValue x = null ;
227222
228- using ( var status = new Status ( ) )
229- using ( var buf = new Buffer ( ) )
230- {
231- unsafe
223+ lock ( Locks . ProcessWide )
224+ using ( var status = new Status ( ) )
225+ using ( var buf = new Buffer ( ) )
232226 {
233227 c_api . TF_OperationGetAttrValueProto ( _handle , name , buf , status ) ;
234228 status . Check ( true ) ;
229+
235230 x = AttrValue . Parser . ParseFrom ( buf . MemoryBlock . Stream ( ) ) ;
236231 }
237- }
238232
239233 string oneof_value = x . ValueCase . ToString ( ) ;
240234 if ( string . IsNullOrEmpty ( oneof_value ) )
241235 return null ;
242236
243- if ( oneof_value == "list" )
237+ if ( oneof_value == "list" )
244238 throw new NotImplementedException ( $ "Unsupported field type in { x . ToString ( ) } ") ;
245239
246240 if ( oneof_value == "type" )
@@ -259,60 +253,63 @@ public TF_AttrMetadata GetAttributeMetadata(string attr_name, Status s)
259253
260254 private NodeDef GetNodeDef ( )
261255 {
262- using ( var s = new Status ( ) )
263- using ( var buffer = new Buffer ( ) )
264- {
265- c_api . TF_OperationToNodeDef ( _handle , buffer , s ) ;
266- s . Check ( ) ;
267- return NodeDef . Parser . ParseFrom ( buffer . MemoryBlock . Stream ( ) ) ;
268- }
269- }
270-
271- /// <summary>
272- /// Update the input to this operation at the given index.
273- ///
274- /// NOTE: This is for TF internal use only.Please don't use it.
275- /// </summary>
276- /// <param name="index">the index of the input to update.</param>
277- /// <param name="tensor"> the Tensor to be used as the input at the given index.</param>
278- public void _update_input ( int index , Tensor tensor )
279- {
280- _assert_same_graph ( tensor ) ;
281-
282- var input = _tf_input ( index ) ;
283- var output = tensor . _as_tf_output ( ) ;
284-
285- // Reset cached inputs.
286- _inputs = null ;
287- // after the c_api call next time _inputs is accessed
288- // the updated inputs are reloaded from the c_api
289- using ( var status = new Status ( ) )
290- {
291- c_api . UpdateEdge ( _graph , output , input , status ) ;
292- //var updated_inputs = inputs;
293- status . Check ( ) ;
294- }
295- }
296-
297- private void _assert_same_graph ( Tensor tensor )
298- {
299- //TODO: implement
300- }
301-
302- /// <summary>
303- /// Create and return a new TF_Output for output_idx'th output of this op.
304- /// </summary>
305- public TF_Output _tf_output ( int output_idx )
306- {
307- return new TF_Output ( op , output_idx ) ;
308- }
309-
310- /// <summary>
311- /// Create and return a new TF_Input for input_idx'th input of this op.
312- /// </summary>
313- public TF_Input _tf_input ( int input_idx )
314- {
315- return new TF_Input ( op , input_idx ) ;
316- }
317- }
318- }
256+ lock ( Locks . ProcessWide )
257+ using ( var s = new Status ( ) )
258+ using ( var buffer = new Buffer ( ) )
259+ {
260+ c_api . TF_OperationToNodeDef ( _handle , buffer , s ) ;
261+ s . Check ( ) ;
262+
263+ return NodeDef . Parser . ParseFrom ( buffer . MemoryBlock . Stream ( ) ) ;
264+ }
265+ }
266+
267+ /// <summary>
268+ /// Update the input to this operation at the given index.
269+ ///
270+ /// NOTE: This is for TF internal use only.Please don't use it.
271+ /// </summary>
272+ /// <param name="index">the index of the input to update.</param>
273+ /// <param name="tensor"> the Tensor to be used as the input at the given index.</param>
274+ public void _update_input ( int index , Tensor tensor )
275+ {
276+ _assert_same_graph ( tensor ) ;
277+
278+ var input = _tf_input ( index ) ;
279+ var output = tensor . _as_tf_output ( ) ;
280+
281+ // Reset cached inputs.
282+ _inputs = null ;
283+ // after the c_api call next time _inputs is accessed
284+ // the updated inputs are reloaded from the c_api
285+ lock ( Locks . ProcessWide )
286+ using ( var status = new Status ( ) )
287+ {
288+ c_api . UpdateEdge ( _graph , output , input , status ) ;
289+ //var updated_inputs = inputs;
290+ status . Check ( ) ;
291+ }
292+ }
293+
294+ private void _assert_same_graph ( Tensor tensor )
295+ {
296+ //TODO: implement
297+ }
298+
299+ /// <summary>
300+ /// Create and return a new TF_Output for output_idx'th output of this op.
301+ /// </summary>
302+ public TF_Output _tf_output ( int output_idx )
303+ {
304+ return new TF_Output ( op , output_idx ) ;
305+ }
306+
307+ /// <summary>
308+ /// Create and return a new TF_Input for input_idx'th input of this op.
309+ /// </summary>
310+ public TF_Input _tf_input ( int input_idx )
311+ {
312+ return new TF_Input ( op , input_idx ) ;
313+ }
314+ }
315+ }
0 commit comments