@@ -119,9 +119,14 @@ public virtual Tensor AddValue(Tensor val)
119119 return null ;
120120 }
121121
122- public virtual void AddInnerOp ( Operation resultOp )
122+ /// <summary>
123+ /// Notifies a scope about an operator added to an inner scope.
124+ /// </summary>
125+ /// <param name="op"></param>
126+ public virtual void AddInnerOp ( Operation op )
123127 {
124- // to be overridden
128+ if ( _outer_context != null )
129+ _outer_context . AddInnerOp ( op ) ;
125130 }
126131
127132 protected HashSet < string > _values = new HashSet < string > ( ) ;
@@ -131,68 +136,10 @@ public virtual void AddInnerOp(Operation resultOp)
131136 /// </summary>
132137 protected virtual void _AddOpInternal ( Operation op )
133138 {
134- if ( op . inputs . Length == 0 )
135- {
136- //If we're in a while loop, remove any control inputs from outside the
137- // loop.
138- _RemoveExternalControlEdges ( op ) ;
139- if ( ! op . control_inputs . Any ( input_op => OpInContext ( input_op ) ) )
140- op . _add_control_input ( _pivot . op ) ;
141- }
142- else
143- {
144- // Make each input to 'op' available in this CondContext. If an input is
145- // already part of this context there's nothing to do, but if it's
146- // external, AddValue() will handle adding the appropriate Switch node and
147- // other bookkeeping.
148- for ( int index = 0 ; index < op . inputs . Length ; index ++ )
149- {
150- var x = op . inputs [ index ] ;
151- Tensor real_x = null ;
152- if ( op . type == "Merge" && x . op . type == "NextIteration" )
153- {
154- //# Edge case: if we're importing a while loop inside this CondContext,
155- //# AddValue() will not correctly handle the NextIteration inputs to
156- //# Merge node. The problem is that the NextIteration should also be
157- //# part of this context, but if we're importing it won't have been
158- //# processed and added to the context yet, so AddValue() will try to
159- //# add a Switch which results in an invalid graph. Instead, we use the
160- //# NextIteration input as-is here, and it will eventually be added to
161- //# the context via AddOp().
162- real_x = x ;
163- }
164- else
165- {
166- real_x = AddValue ( x ) ;
167- }
168- if ( real_x != x )
169- op . _update_input ( index , real_x ) ;
170- }
171- // Remove any external control dependency on this op.
172- _RemoveExternalControlEdges ( op ) ;
173- // TODO: implement below code dependencies
174- //if (op.graph._is_function(op.type) || op.type == "SymbolicGradient")
175- // op._add_control_input(_pivot.op);
176- }
177139
178- // Mark op's outputs as seen by this context and any outer contexts.
179- var output_names = op . outputs . Select ( x => x . name ) . ToArray ( ) ;
180- IControlFlowContext ctxt = this ;
181- while ( ctxt != null )
182- {
183- foreach ( var name in output_names )
184- ctxt . values . Add ( name ) ;
185- ctxt = ctxt . outer_context ;
186- }
187-
188- if ( _outer_context != null || ! control_flow_ops . IsLoopExit ( op ) )
189- op . graph . prevent_fetching ( op ) ;
190-
191- if ( _outer_context != null )
192- _outer_context . AddInnerOp ( op ) ;
193140 }
194141
195- private bool OpInContext ( Operation op )
142+ protected bool OpInContext ( Operation op )
196143 {
197144 return IsContainingContext ( op . _get_control_flow_context ( ) , this ) ;
198145 }
0 commit comments