diff --git a/python/paddle/fluid/dygraph/tracer.py b/python/paddle/fluid/dygraph/tracer.py index 4248e3c310f56325594b1f5a7bbfade11f34c56e..92092458146d9d4fdf91c164fbd8f7a69b168252 100644 --- a/python/paddle/fluid/dygraph/tracer.py +++ b/python/paddle/fluid/dygraph/tracer.py @@ -54,47 +54,24 @@ class Tracer(core.Tracer): self._trace_id = 0 def trace_op(self, op, inputs, outputs, stop_gradient=False): - # TODO(minqiyang): remove this line after we take apart all - # backward grads and forward variables - if self._train_mode: - op.inputs = inputs - inps = defaultdict(list) - for k, vars in six.iteritems(inputs): - if isinstance(vars, framework.Variable): - inps[k].append(vars._ivar) - elif isinstance(vars, list) or isinstance(vars, tuple): - for var in vars: - inps[k].append(var._ivar) - - op.outputs = outputs - outs = defaultdict(list) - for k, vars in six.iteritems(outputs): - if isinstance(vars, framework.Variable): - outs[k].append(vars._ivar) - elif isinstance(vars, list) or isinstance(vars, tuple): - for var in vars: - outs[k].append(var._ivar) - else: - inps = defaultdict(list) - for k, vars in six.iteritems(inputs): - if isinstance(vars, framework.Variable): - op.previous_ops.append(vars.op) - inps[k].append(vars._ivar) - elif isinstance(vars, list) or isinstance(vars, tuple): - for var in vars: - op.previous_ops.append(var.op) - inps[k].append(var._ivar) - - op.outputs = outputs - outs = defaultdict(list) - for k, vars in six.iteritems(outputs): - if isinstance(vars, framework.Variable): - vars.op = op - outs[k].append(vars._ivar) - elif isinstance(vars, list) or isinstance(vars, tuple): - for var in vars: - var.op = op - outs[k].append(var._ivar) + # TODO(hy): previous version will cause memory failed + op.inputs = inputs + inps = defaultdict(list) + for k, vars in six.iteritems(inputs): + if isinstance(vars, framework.Variable): + inps[k].append(vars._ivar) + elif isinstance(vars, list) or isinstance(vars, tuple): + for var in vars: + inps[k].append(var._ivar) + + op.outputs = outputs + outs = defaultdict(list) + for k, vars in six.iteritems(outputs): + if isinstance(vars, framework.Variable): + outs[k].append(vars._ivar) + elif isinstance(vars, list) or isinstance(vars, tuple): + for var in vars: + outs[k].append(var._ivar) # record op's trace id op.iop._trace_id = self._trace_id diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 43ba26248a4e1914b7e39167f24fa34327782d82..3f160b71f44fcd140e92674e3447bdd97cb33579 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -1670,13 +1670,18 @@ class Block(object): Operator: the append Operator. """ if in_dygraph_mode(): + attrs = kwargs.get("attrs", {}) + if _dygraph_tracer_._train_mode == False: + # eval mode + attrs['is_test'] = True + op = Operator( block=self, desc=None, type=kwargs.get("type", None), inputs=None, outputs=None, - attrs=kwargs.get("attrs", {})) + attrs=attrs) # record ops in tracer rather than blocks #