未验证 提交 1c7ae954 编写于 作者: 姜永久 提交者: GitHub

rm in_legacy part8 (#49386)

* rm legacy layers part6

* rm non_static_mode

* modify non_static

* minor change

* rm loss

* rm in_legacy part8

* minor change
上级 0c52e8a8
......@@ -13,7 +13,7 @@
# limitations under the License.
from ..layer_helper import LayerHelper, unique_name
from ..framework import Variable, in_dygraph_mode, _in_legacy_dygraph
from ..framework import Variable, in_dygraph_mode
import paddle
from paddle import _C_ops, _legacy_C_ops
......@@ -120,18 +120,7 @@ def _c_allgather(x, nranks, ring_id=0, use_calc_stream=False):
task = group.process_group.all_gather(x, out)
task.wait()
return out
if _in_legacy_dygraph():
attrs = (
'nranks',
nranks,
'ring_id',
ring_id,
'use_calc_stream',
use_calc_stream,
)
return _legacy_C_ops.c_allgather(x, *attrs)
else:
helper = LayerHelper(op_type, **locals())
out_shape = list(x.shape[:])
if out_shape[0] > 0:
......
......@@ -21,9 +21,7 @@ from ..framework import (
Program,
Variable,
Operator,
_non_static_mode,
static_only,
_in_legacy_dygraph,
in_dygraph_mode,
)
from ..layer_helper import LayerHelper, unique_name
......@@ -1154,7 +1152,7 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None):
"but given shape as {0}.".format(list(pre_cond.shape))
)
if _non_static_mode():
if in_dygraph_mode():
now_cond = pre_cond.numpy()[0]
while now_cond:
output_vars = body(*loop_vars)
......@@ -1168,7 +1166,7 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None):
now_cond = cond(*output_vars).numpy()[0]
map_structure(assign_skip_lod_tensor_array, output_vars, loop_vars)
return loop_vars
else:
while_loop_block = While(pre_cond, is_test, name)
has_mutable_vars_in_loop = hold_mutable_vars(loop_vars)
with while_loop_block.block():
......
......@@ -24,13 +24,10 @@ from ..framework import (
Variable,
core,
convert_np_dtype_to_dtype_,
_non_static_mode,
in_dygraph_mode,
_in_legacy_dygraph,
)
from ..layer_helper import LayerHelper
from ..data_feeder import check_variable_and_dtype
from paddle.fluid.framework import in_dygraph_mode, _in_legacy_dygraph
from paddle import _C_ops, _legacy_C_ops
__all__ = [
......@@ -276,7 +273,7 @@ def generate_activation_fn(op_type):
return op(x)
# TODO(dev): Because some ops' yaml has not been migrated.
# Replace it with _in_legacy_dygraph while all yaml work is done.
if _non_static_mode():
if in_dygraph_mode() and hasattr(_legacy_C_ops, op_type):
op = getattr(_legacy_C_ops, op_type)
return op(x)
......@@ -327,9 +324,10 @@ def generate_inplace_fn(inplace_op_type):
origin_op_type = inplace_op_type[:-1]
def func(x, name=None):
if _non_static_mode():
if in_dygraph_mode():
op = getattr(_legacy_C_ops, inplace_op_type)
return op(x)
else:
warnings.warn(
"In static mode, {}() is the same as {}() and does not perform inplace operation.".format(
inplace_op_type, origin_op_type
......
......@@ -27,9 +27,14 @@ import paddle
from . import control_flow
from . import nn
from . import tensor
from ..framework import default_main_program, Parameter, unique_name, name_scope
from ..framework import (
default_main_program,
Parameter,
unique_name,
name_scope,
in_dygraph_mode,
)
from ..framework import Variable
from ..framework import _non_static_mode
from ..dygraph import learning_rate_scheduler as imperate_lr
from ..data_feeder import check_variable_and_dtype, check_type
......@@ -99,7 +104,7 @@ def noam_decay(d_model, warmup_steps, learning_rate=1.0):
learning_rate)
"""
with default_main_program()._lr_schedule_guard():
if _non_static_mode():
if in_dygraph_mode():
decay = imperate_lr.NoamDecay(
d_model, warmup_steps, learning_rate=learning_rate
)
......@@ -160,7 +165,7 @@ def exponential_decay(learning_rate, decay_steps, decay_rate, staircase=False):
"""
with default_main_program()._lr_schedule_guard():
if _non_static_mode():
if in_dygraph_mode():
decay = imperate_lr.ExponentialDecay(
learning_rate, decay_steps, decay_rate, staircase
)
......@@ -222,7 +227,7 @@ def natural_exp_decay(learning_rate, decay_steps, decay_rate, staircase=False):
"""
with default_main_program()._lr_schedule_guard():
if _non_static_mode():
if in_dygraph_mode():
decay = imperate_lr.NaturalExpDecay(
learning_rate, decay_steps, decay_rate, staircase
)
......@@ -282,7 +287,7 @@ def inverse_time_decay(learning_rate, decay_steps, decay_rate, staircase=False):
staircase=True))
"""
with default_main_program()._lr_schedule_guard():
if _non_static_mode():
if in_dygraph_mode():
decay = imperate_lr.InverseTimeDecay(
learning_rate, decay_steps, decay_rate, staircase
)
......@@ -337,7 +342,7 @@ def polynomial_decay(
"""
with default_main_program()._lr_schedule_guard():
if _non_static_mode():
if in_dygraph_mode():
decay = imperate_lr.PolynomialDecay(
learning_rate, decay_steps, end_learning_rate, power, cycle
)
......@@ -414,7 +419,7 @@ def piecewise_decay(boundaries, values):
if len(values) - len(boundaries) != 1:
raise ValueError("len(values) - len(boundaries) should be 1")
if _non_static_mode():
if in_dygraph_mode():
decay = imperate_lr.PiecewiseDecay(boundaries, values, 0)
return decay
else:
......@@ -488,7 +493,7 @@ def cosine_decay(learning_rate, step_each_epoch, epochs):
)
with default_main_program()._lr_schedule_guard():
if _non_static_mode():
if in_dygraph_mode():
decay = imperate_lr.CosineDecay(
learning_rate, step_each_epoch, epochs
)
......@@ -569,7 +574,7 @@ def linear_lr_warmup(learning_rate, warmup_steps, start_lr, end_lr):
linear_step = float(end_lr) - float(start_lr)
with default_main_program()._lr_schedule_guard():
if _non_static_mode():
if in_dygraph_mode():
lr = imperate_lr.LinearLrWarmup(
learning_rate, warmup_steps, start_lr, end_lr
)
......
......@@ -22,19 +22,16 @@ import numpy as np
import paddle
from ..layer_helper import LayerHelper
from paddle.fluid.framework import _in_legacy_dygraph
from ..initializer import Normal, Constant
from ..framework import (
Variable,
OpProtoHolder,
_non_static_mode,
dygraph_only,
_dygraph_tracer,
default_main_program,
_varbase_creator,
static_only,
_global_flags,
_in_legacy_dygraph,
in_dygraph_mode,
)
from ..framework import _current_expected_place
......@@ -128,10 +125,6 @@ def _elementwise_op_in_dygraph(
OP_NAMEMAPPING[op_name] if not is_inplace(op_name) else op_name,
)
out = op(x, y)
if _in_legacy_dygraph():
op = getattr(_legacy_C_ops, op_name)
out = op(x, y, 'axis', axis, 'use_mkldnn', use_mkldnn)
return dygraph_utils._append_activation_in_dygraph(
out, act, use_mkldnn=use_mkldnn
)
......@@ -794,10 +787,7 @@ def reduce_sum(input, dim=None, keep_dim=False, name=None):
if in_dygraph_mode():
return _C_ops.sum(input, dim, None, keep_dim)
elif _in_legacy_dygraph():
return _legacy_C_ops.reduce_sum(
input, 'dim', dim, 'keep_dim', keep_dim, 'reduce_all', reduce_all
)
else:
attrs = {'dim': dim, 'keep_dim': keep_dim, 'reduce_all': reduce_all}
check_variable_and_dtype(
input,
......@@ -806,7 +796,9 @@ def reduce_sum(input, dim=None, keep_dim=False, name=None):
'reduce_sum',
)
helper = LayerHelper('reduce_sum', **locals())
out = helper.create_variable_for_type_inference(dtype=helper.input_dtype())
out = helper.create_variable_for_type_inference(
dtype=helper.input_dtype()
)
helper.append_op(
type='reduce_sum',
inputs={'X': input},
......@@ -895,7 +887,7 @@ def unsqueeze(input, axes, name=None):
y = fluid.layers.unsqueeze(input=x, axes=[1])
"""
if _non_static_mode():
if in_dygraph_mode():
if isinstance(axes, int):
axes = [axes]
elif isinstance(axes, Variable):
......@@ -905,11 +897,8 @@ def unsqueeze(input, axes, name=None):
item.numpy().item(0) if isinstance(item, Variable) else item
for item in axes
]
if _in_legacy_dygraph():
out, _ = _legacy_C_ops.unsqueeze2(input, 'axes', axes)
return out
return _C_ops.unsqueeze(input, axes)
else:
check_type(axes, 'axis/axes', (int, list, tuple, Variable), 'unsqueeze')
check_variable_and_dtype(
input,
......@@ -956,12 +945,13 @@ def unsqueeze(input, axes, name=None):
def _logical_op(op_name, x, y, out=None, name=None, binary_op=True):
if _non_static_mode():
if in_dygraph_mode():
op = getattr(_legacy_C_ops, op_name)
if binary_op:
return op(x, y)
else:
return op(x)
else:
check_variable_and_dtype(
x,
"x",
......@@ -972,7 +962,15 @@ def _logical_op(op_name, x, y, out=None, name=None, binary_op=True):
check_variable_and_dtype(
y,
"y",
["bool", "int8", "int16", "int32", "int64", "float32", "float64"],
[
"bool",
"int8",
"int16",
"int32",
"int64",
"float32",
"float64",
],
op_name,
)
if out is not None:
......@@ -994,7 +992,9 @@ def _logical_op(op_name, x, y, out=None, name=None, binary_op=True):
type=op_name, inputs={"X": x, "Y": y}, outputs={"Out": out}
)
else:
helper.append_op(type=op_name, inputs={"X": x}, outputs={"Out": out})
helper.append_op(
type=op_name, inputs={"X": x}, outputs={"Out": out}
)
return out
......@@ -1082,9 +1082,7 @@ def clip_by_norm(x, max_norm, name=None):
if in_dygraph_mode():
return _C_ops.clip_by_norm(x, max_norm)
if _non_static_mode():
return _legacy_C_ops.clip_by_norm(x, 'max_norm', max_norm)
else:
helper = LayerHelper("clip_by_norm", **locals())
check_variable_and_dtype(x, 'X', ['float32', 'float16'], 'clip_by_norm')
check_type(max_norm, 'max_norm', (float), 'clip_by_norm')
......@@ -1132,10 +1130,7 @@ def merge_selected_rows(x, name=None):
"""
if in_dygraph_mode():
return _C_ops.merge_selected_rows(x)
if _non_static_mode():
return _legacy_C_ops.merge_selected_rows(x)
else:
helper = LayerHelper("merge_selected_rows", **locals())
out = helper.create_variable_for_type_inference(dtype=x.dtype)
helper.append_op(
......
......@@ -17,9 +17,7 @@ from .layer_function_generator import templatedoc
from ..framework import (
core,
Variable,
_non_static_mode,
in_dygraph_mode,
_in_legacy_dygraph,
convert_np_dtype_to_dtype_,
)
from ..layer_helper import LayerHelper
......@@ -156,7 +154,7 @@ def sequence_conv(
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
check_variable_and_dtype(
input, 'input', ['float32', 'float64'], 'sequence_conv'
......@@ -258,7 +256,7 @@ def sequence_softmax(input, use_cudnn=False, name=None):
x_sequence_softmax_2 = paddle.static.nn.sequence_softmax(input=y)
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
helper = LayerHelper('sequence_softmax', **locals())
check_variable_and_dtype(
......@@ -363,7 +361,7 @@ def sequence_pool(input, pool_type, is_test=False, pad_value=0.0):
first_x = paddle.static.nn.sequence_pool(input=x, pool_type='first')
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
check_variable_and_dtype(
input, 'input', ['float32', 'float64'], 'sequence_pool'
......@@ -441,7 +439,7 @@ def sequence_concat(input, name=None):
out = paddle.static.nn.sequence_concat(input=[x, y])
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
helper = LayerHelper('sequence_concat', **locals())
......@@ -640,7 +638,7 @@ def sequence_slice(input, offset, length, name=None):
length=length)
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
helper = LayerHelper("sequence_slice", **locals())
......@@ -794,7 +792,7 @@ def sequence_expand(x, y, ref_level=-1, name=None):
# data: [1 2 1 2 3 4 3 4]
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
check_variable_and_dtype(
x, 'x', ['float32', 'float64', 'int32', 'int64'], 'sequence_expand'
......@@ -916,7 +914,7 @@ def sequence_expand_as(x, y, name=None):
# data: [1 1 1 2 2 2 3 4]
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
check_variable_and_dtype(
x, 'x', ['float32', 'float64', 'int32', 'int64'], 'sequence_expand_as'
......@@ -1019,7 +1017,7 @@ def sequence_pad(x, pad_value, maxlen=None, name=None):
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
helper = LayerHelper('sequence_pad', **locals())
check_variable_and_dtype(
......@@ -1108,7 +1106,7 @@ def sequence_unpad(x, length, name=None):
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
helper = LayerHelper('sequence_unpad', **locals())
check_variable_and_dtype(
......@@ -1183,7 +1181,7 @@ def sequence_reshape(input, new_dim):
x_reshaped = paddle.static.nn.sequence_reshape(input=x, new_dim=4)
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
helper = LayerHelper('sequence_reshape', **locals())
check_variable_and_dtype(
......@@ -1268,7 +1266,7 @@ def sequence_scatter(input, index, updates, name=None):
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
helper = LayerHelper('sequence_scatter', **locals())
......@@ -1350,7 +1348,7 @@ def sequence_enumerate(input, win_size, pad_value=0, name=None):
out = paddle.static.nn.sequence_enumerate(input=x, win_size=3, pad_value=0)
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
check_variable_and_dtype(
input, 'input', ['int32', 'int64'], 'sequence_enumerate'
......@@ -1479,7 +1477,7 @@ def sequence_reverse(x, name=None):
x_reversed = paddle.static.nn.sequence_reverse(x)
"""
assert (
not _non_static_mode()
not in_dygraph_mode()
), "sequence layer is not supported in dygraph mode yet."
helper = LayerHelper("sequence_reverse", **locals())
check_variable_and_dtype(
......
......@@ -19,9 +19,7 @@ from ..layer_helper import LayerHelper
from ..framework import (
_current_expected_place,
convert_np_dtype_to_dtype_,
_non_static_mode,
_varbase_creator,
_in_legacy_dygraph,
in_dygraph_mode,
)
from ..framework import Variable
......@@ -81,13 +79,7 @@ def cast(x, dtype):
if not isinstance(dtype, core.VarDesc.VarType):
dtype = convert_np_dtype_to_dtype_(dtype)
return _C_ops.cast(x, dtype)
if _non_static_mode():
if not isinstance(dtype, core.VarDesc.VarType):
dtype = convert_np_dtype_to_dtype_(dtype)
out = _legacy_C_ops.cast(x, 'in_dtype', x.dtype, 'out_dtype', dtype)
return out
else:
check_variable_and_dtype(
x,
'x',
......@@ -191,17 +183,7 @@ def concat(input, axis=0, name=None):
input = [t for t in input if t.shape.count(0) == 0]
out = _C_ops.concat(input, axis)
return out
if _in_legacy_dygraph():
if isinstance(axis, Variable):
axis = axis.numpy()
axis = axis.item(0)
if not isinstance(input, Variable):
input = [t for t in input if t.shape.count(0) == 0]
out = _varbase_creator()
_legacy_C_ops.concat(input, out, 'axis', axis)
return out
else:
check_type(input, 'input', (list, tuple, Variable), 'concat')
if not isinstance(input, Variable):
for id, x in enumerate(input):
......@@ -229,7 +211,9 @@ def concat(input, axis=0, name=None):
)
helper = LayerHelper('concat', **locals())
out = helper.create_variable_for_type_inference(dtype=helper.input_dtype())
out = helper.create_variable_for_type_inference(
dtype=helper.input_dtype()
)
if input[0].desc.type() == core.VarDesc.VarType.LOD_TENSOR_ARRAY:
# NOTE(liym27): Don't remove this if branch!
......@@ -238,7 +222,8 @@ def concat(input, axis=0, name=None):
assert len(input) == 1, (
"If the elements of 'input' in concat are Variable(LoDTensorArray), "
"number of the elements must be 1, but received %s." % len(input)
"number of the elements must be 1, but received %s."
% len(input)
)
out_index = helper.create_variable_for_type_inference(dtype="int32")
helper.append_op(
......@@ -255,7 +240,10 @@ def concat(input, axis=0, name=None):
attrs['axis'] = axis
helper.append_op(
type='concat', inputs=inputs, outputs={'Out': [out]}, attrs=attrs
type='concat',
inputs=inputs,
outputs={'Out': [out]},
attrs=attrs,
)
return out
......@@ -391,22 +379,15 @@ def assign(input, output=None):
input = numpy.array(input)
# NOTE(Aurelius84): Why we judge core.VarBase?
# In case of @to_static, a VarBase can be as input of `assign`,
# but _non_static_mode()==False under @to_static, which means
# but in_dygraph_mode()==False under @to_static, which means
# isinstance(VarBase, Variable) == False. It will cause return None
# after this api.
if isinstance(input, (Variable, core.VarBase)):
if _non_static_mode():
if in_dygraph_mode() and output is None:
output = _C_ops.assign(input)
elif in_dygraph_mode() and output is not None:
_C_ops.assign_out_(input, output)
else:
if in_dygraph_mode():
if output is None:
if _in_legacy_dygraph():
output = core.VarBase()
output = _C_ops.assign(input)
else:
output = core.eager.Tensor()
_legacy_C_ops.assign(input, output)
_C_ops.assign_out_(input, output)
else:
check_dtype(
input.dtype,
......@@ -480,18 +461,6 @@ def assign(input, output=None):
values,
_current_expected_place(),
)
elif _in_legacy_dygraph():
if output is None:
output = core.VarBase()
_legacy_C_ops.assign_value(
output,
'shape',
list(input.shape),
'dtype',
dtype,
value_name,
values,
)
else:
if output is None:
output = helper.create_variable_for_type_inference(
......@@ -507,7 +476,7 @@ def assign(input, output=None):
},
)
if is_inplace and _non_static_mode():
if is_inplace and in_dygraph_mode():
output._bump_inplace_version()
return output
......@@ -591,34 +560,7 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
_C_ops.full_(out, shape, float(value), dtype, place)
out.stop_gradient = True
return out
if _in_legacy_dygraph():
shape = utils.convert_shape_to_list(shape)
if out is None:
out = _varbase_creator(dtype=dtype)
if isinstance(value, Variable):
if dtype in ['uint8', 'int16', 'int32', 'int64']:
attrs['str_value'] = str(int(value.numpy().item(0)))
else:
attrs['str_value'] = str(float(value.numpy().item(0)))
_legacy_C_ops.fill_constant(
out,
'value',
float(value),
'force_cpu',
force_cpu,
'dtype',
out.dtype,
'str_value',
attrs['str_value'],
'shape',
shape,
)
out.stop_gradient = True
return out
helper = LayerHelper("fill_constant", **locals())
inputs = {}
if isinstance(value, Variable):
......@@ -727,7 +669,7 @@ def fill_constant_batch_size_like(
)
out.stop_gradient = True
return out
else:
helper = LayerHelper("fill_constant_batch_size_like", **locals())
out = helper.create_variable_for_type_inference(dtype=dtype)
attrs = {
......
......@@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from paddle import _C_ops, _legacy_C_ops
from paddle import _C_ops
from paddle.fluid.data_feeder import check_variable_and_dtype
from paddle.fluid.framework import _in_legacy_dygraph, in_dygraph_mode
from paddle.fluid.framework import in_dygraph_mode
from paddle.fluid.layer_helper import LayerHelper
__all__ = []
......@@ -52,12 +52,7 @@ def segment_sum(data, segment_ids, name=None):
"""
if in_dygraph_mode():
return _C_ops.segment_pool(data, segment_ids, "SUM")[0]
if _in_legacy_dygraph():
out, tmp = _legacy_C_ops.segment_pool(
data, segment_ids, 'pooltype', "SUM"
)
return out
else:
check_variable_and_dtype(
data,
"X",
......@@ -114,11 +109,7 @@ def segment_mean(data, segment_ids, name=None):
if in_dygraph_mode():
return _C_ops.segment_pool(data, segment_ids, "MEAN")[0]
if _in_legacy_dygraph():
out, tmp = _legacy_C_ops.segment_pool(
data, segment_ids, 'pooltype', "MEAN"
)
return out
else:
check_variable_and_dtype(
data,
......@@ -175,12 +166,7 @@ def segment_min(data, segment_ids, name=None):
if in_dygraph_mode():
return _C_ops.segment_pool(data, segment_ids, "MIN")[0]
if _in_legacy_dygraph():
out, tmp = _legacy_C_ops.segment_pool(
data, segment_ids, 'pooltype', "MIN"
)
return out
else:
check_variable_and_dtype(
data,
"X",
......@@ -236,12 +222,7 @@ def segment_max(data, segment_ids, name=None):
if in_dygraph_mode():
return _C_ops.segment_pool(data, segment_ids, "MAX")[0]
if _in_legacy_dygraph():
out, tmp = _legacy_C_ops.segment_pool(
data, segment_ids, 'pooltype', "MAX"
)
return out
else:
check_variable_and_dtype(
data,
"X",
......
......@@ -14,13 +14,13 @@
import numpy as np
from paddle import _C_ops, _legacy_C_ops
from paddle import _C_ops
from paddle.fluid.data_feeder import (
check_dtype,
check_type,
check_variable_and_dtype,
)
from paddle.fluid.framework import Variable, _in_legacy_dygraph, in_dygraph_mode
from paddle.fluid.framework import Variable, in_dygraph_mode
from paddle.fluid.layer_helper import LayerHelper
from .utils import (
......@@ -118,25 +118,12 @@ def send_u_recv(
# TODO(daisiming): Should we add judgement for out_size: max(dst_index) + 1.
if _in_legacy_dygraph():
out_size = convert_out_size_to_list(out_size)
out, tmp = _legacy_C_ops.graph_send_recv(
x,
src_index,
dst_index,
None,
'reduce_op',
reduce_op.upper(),
'out_size',
out_size,
)
return out
if in_dygraph_mode():
out_size = convert_out_size_to_list(out_size)
return _C_ops.send_u_recv(
x, src_index, dst_index, reduce_op.upper(), out_size
)
else:
check_variable_and_dtype(
x,
"X",
......@@ -158,7 +145,10 @@ def send_u_recv(
)
if isinstance(out_size, Variable):
check_dtype(
out_size.dtype, 'out_size', ['int32', 'int64'], 'graph_send_recv'
out_size.dtype,
'out_size',
['int32', 'int64'],
'graph_send_recv',
)
helper = LayerHelper("send_u_recv", **locals())
......@@ -170,7 +160,10 @@ def send_u_recv(
inputs = {"X": x, "Src_index": src_index, "Dst_index": dst_index}
attrs = {"reduce_op": reduce_op.upper()}
get_out_size_tensor_inputs(
inputs=inputs, attrs=attrs, out_size=out_size, op_type='graph_send_recv'
inputs=inputs,
attrs=attrs,
out_size=out_size,
op_type='graph_send_recv',
)
helper.append_op(
......@@ -302,22 +295,6 @@ def send_ue_recv(
# TODO(daisiming): Should we add judgement for out_size: max(dst_index) + 1.
if _in_legacy_dygraph():
out_size = convert_out_size_to_list(out_size)
out, tmp = _legacy_C_ops.graph_send_ue_recv(
x,
y,
src_index,
dst_index,
None,
'message_op',
message_op.upper(),
'reduce_op',
reduce_op.upper(),
'out_size',
out_size,
)
return out
if in_dygraph_mode():
out_size = convert_out_size_to_list(out_size)
return _C_ops.send_ue_recv(
......@@ -329,7 +306,7 @@ def send_ue_recv(
reduce_op.upper(),
out_size,
)
else:
check_variable_and_dtype(
x,
"X",
......@@ -357,7 +334,10 @@ def send_ue_recv(
)
if isinstance(out_size, Variable):
check_dtype(
out_size.dtype, 'out_size', ['int32', 'int64'], 'graph_send_ue_recv'
out_size.dtype,
'out_size',
['int32', 'int64'],
'graph_send_ue_recv',
)
helper = LayerHelper("send_ue_recv", **locals())
......@@ -366,8 +346,16 @@ def send_ue_recv(
dtype="int32", stop_gradient=True
)
inputs = {"X": x, "Y": y, "Src_index": src_index, "Dst_index": dst_index}
attrs = {"message_op": message_op.upper(), "reduce_op": reduce_op.upper()}
inputs = {
"X": x,
"Y": y,
"Src_index": src_index,
"Dst_index": dst_index,
}
attrs = {
"message_op": message_op.upper(),
"reduce_op": reduce_op.upper(),
}
get_out_size_tensor_inputs(
inputs=inputs,
attrs=attrs,
......@@ -466,11 +454,7 @@ def send_uv(x, y, src_index, dst_index, message_op="add", name=None):
if in_dygraph_mode():
return _C_ops.send_uv(x, y, src_index, dst_index, message_op.upper())
else:
if _in_legacy_dygraph():
return _legacy_C_ops.graph_send_uv(
x, y, src_index, dst_index, "message_op", message_op.upper()
)
else:
helper = LayerHelper("graph_send_uv", **locals())
check_variable_and_dtype(
x,
......
......@@ -1001,7 +1001,7 @@ def _custom_api_content(op_name):
"""
import paddle.fluid.core as core
from paddle.fluid.core import VarBase, CustomOpKernelContext
from paddle.fluid.framework import _non_static_mode, _dygraph_tracer, _in_legacy_dygraph, in_dygraph_mode
from paddle.fluid.framework import _dygraph_tracer, in_dygraph_mode
from paddle.fluid.layer_helper import LayerHelper
def {op_name}({inputs}):
......@@ -1023,11 +1023,6 @@ def _custom_api_content(op_name):
outs[out_name] = core.eager.Tensor()
ctx.add_outputs(outs[out_name])
core.eager._run_custom_op(ctx, "{op_name}", True)
else:
if _in_legacy_dygraph():
for out_name in out_names:
outs[out_name] = VarBase()
_dygraph_tracer().trace_op(type="{op_name}", inputs=ins, outputs=outs, attrs=attrs)
else:
helper = LayerHelper("{op_name}", **locals())
for out_name in out_names:
......
......@@ -18,12 +18,7 @@ from paddle import _C_ops, _legacy_C_ops
from paddle.tensor.math import _add_with_axis
from ..fluid.data_feeder import check_type, check_variable_and_dtype
from ..fluid.framework import (
Variable,
_in_legacy_dygraph,
_non_static_mode,
in_dygraph_mode,
)
from ..fluid.framework import Variable, in_dygraph_mode
from ..fluid.initializer import Normal
from ..fluid.layer_helper import LayerHelper
from ..fluid.layers import utils
......@@ -211,29 +206,7 @@ def yolo_loss(
)
return loss
if _non_static_mode():
loss, _, _ = _legacy_C_ops.yolov3_loss(
x,
gt_box,
gt_label,
gt_score,
'anchors',
anchors,
'anchor_mask',
anchor_mask,
'class_num',
class_num,
'ignore_thresh',
ignore_thresh,
'downsample_ratio',
downsample_ratio,
'use_label_smooth',
use_label_smooth,
'scale_x_y',
scale_x_y,
)
return loss
else:
helper = LayerHelper('yolov3_loss', **locals())
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'yolo_loss')
......@@ -249,7 +222,9 @@ def yolo_loss(
loss = helper.create_variable_for_type_inference(dtype=x.dtype)
objectness_mask = helper.create_variable_for_type_inference(dtype='int32')
objectness_mask = helper.create_variable_for_type_inference(
dtype='int32'
)
gt_match_mask = helper.create_variable_for_type_inference(dtype='int32')
inputs = {
......@@ -409,29 +384,7 @@ def yolo_box(
)
return boxes, scores
if _non_static_mode():
boxes, scores = _legacy_C_ops.yolo_box(
x,
img_size,
'anchors',
anchors,
'class_num',
class_num,
'conf_thresh',
conf_thresh,
'downsample_ratio',
downsample_ratio,
'clip_bbox',
clip_bbox,
'scale_x_y',
scale_x_y,
'iou_aware',
iou_aware,
'iou_aware_factor',
iou_aware_factor,
)
return boxes, scores
else:
helper = LayerHelper('yolo_box', **locals())
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'yolo_box')
......@@ -587,31 +540,6 @@ def prior_box(
)
return box, var
if _in_legacy_dygraph():
attrs = (
'min_sizes',
min_sizes,
'aspect_ratios',
aspect_ratios,
'variances',
variance,
'flip',
flip,
'clip',
clip,
'step_w',
steps[0],
'step_h',
steps[1],
'offset',
offset,
'min_max_aspect_ratios_order',
min_max_aspect_ratios_order,
)
if cur_max_sizes is not None:
attrs += ('max_sizes', cur_max_sizes)
box, var = _legacy_C_ops.prior_box(input, image, *attrs)
return box, var
else:
attrs = {
'min_sizes': min_sizes,
......@@ -783,36 +711,6 @@ def box_coder(
raise TypeError("Input prior_box_var must be Variable or list")
return output_box
if _in_legacy_dygraph():
if isinstance(prior_box_var, Variable):
output_box = _legacy_C_ops.box_coder(
prior_box,
prior_box_var,
target_box,
"code_type",
code_type,
"box_normalized",
box_normalized,
"axis",
axis,
)
elif isinstance(prior_box_var, list):
output_box = _legacy_C_ops.box_coder(
prior_box,
None,
target_box,
"code_type",
code_type,
"box_normalized",
box_normalized,
"axis",
axis,
"variance",
prior_box_var,
)
else:
raise TypeError("Input prior_box_var must be Variable or list")
return output_box
else:
helper = LayerHelper("box_coder", **locals())
......@@ -989,35 +887,6 @@ def deform_conv2d(
out = _add_with_axis(pre_bias, bias, axis=1)
else:
out = pre_bias
elif _in_legacy_dygraph():
attrs = (
'strides',
stride,
'paddings',
padding,
'dilations',
dilation,
'deformable_groups',
deformable_groups,
'groups',
groups,
'im2col_step',
1,
)
if use_deform_conv2d_v1:
op_type = 'deformable_conv_v1'
pre_bias = getattr(_legacy_C_ops, op_type)(
x, offset, weight, *attrs
)
else:
op_type = 'deformable_conv'
pre_bias = getattr(_legacy_C_ops, op_type)(
x, offset, mask, weight, *attrs
)
if bias is not None:
out = _add_with_axis(pre_bias, bias, axis=1)
else:
out = pre_bias
else:
check_variable_and_dtype(
x, "x", ['float32', 'float64'], 'deform_conv2d'
......@@ -1370,31 +1239,6 @@ def distribute_fpn_proposals(
)
return multi_rois, restore_ind, rois_num_per_level
if _non_static_mode():
assert (
rois_num is not None
), "rois_num should not be None in dygraph mode."
attrs = (
'min_level',
min_level,
'max_level',
max_level,
'refer_level',
refer_level,
'refer_scale',
refer_scale,
'pixel_offset',
pixel_offset,
)
(
multi_rois,
restore_ind,
rois_num_per_level,
) = _legacy_C_ops.distribute_fpn_proposals(
fpn_rois, rois_num, num_lvl, num_lvl, *attrs
)
return multi_rois, restore_ind, rois_num_per_level
else:
check_variable_and_dtype(
fpn_rois,
......@@ -1472,9 +1316,9 @@ def read_file(filename, name=None):
# [142915]
"""
if _non_static_mode():
if in_dygraph_mode():
return _legacy_C_ops.read_file('filename', filename)
else:
inputs = dict()
attrs = {'filename': filename}
......@@ -1524,9 +1368,7 @@ def decode_jpeg(x, mode='unchanged', name=None):
"""
if in_dygraph_mode():
return _C_ops.decode_jpeg(x, mode, _current_expected_place())
elif _non_static_mode():
return _legacy_C_ops.decode_jpeg(x, "mode", mode)
else:
inputs = {'X': x}
attrs = {"mode": mode}
......@@ -1594,21 +1436,7 @@ def psroi_pool(x, boxes, boxes_num, output_size, spatial_scale=1.0, name=None):
output_channels,
spatial_scale,
)
if _in_legacy_dygraph():
return _legacy_C_ops.psroi_pool(
x,
boxes,
boxes_num,
"output_channels",
output_channels,
"spatial_scale",
spatial_scale,
"pooled_height",
pooled_height,
"pooled_width",
pooled_width,
)
else:
helper = LayerHelper('psroi_pool', **locals())
dtype = helper.input_dtype()
out = helper.create_variable_for_type_inference(dtype)
......@@ -1721,23 +1549,6 @@ def roi_pool(x, boxes, boxes_num, output_size, spatial_scale=1.0, name=None):
return _C_ops.roi_pool(
x, boxes, boxes_num, pooled_height, pooled_width, spatial_scale
)
if _in_legacy_dygraph():
assert (
boxes_num is not None
), "boxes_num should not be None in dygraph mode."
pool_out, argmaxes = _legacy_C_ops.roi_pool(
x,
boxes,
boxes_num,
"pooled_height",
pooled_height,
"pooled_width",
pooled_width,
"spatial_scale",
spatial_scale,
)
return pool_out
else:
check_variable_and_dtype(x, 'x', ['float32'], 'roi_pool')
check_variable_and_dtype(boxes, 'boxes', ['float32'], 'roi_pool')
......@@ -1903,27 +1714,6 @@ def roi_align(
sampling_ratio,
aligned,
)
if _in_legacy_dygraph():
assert (
boxes_num is not None
), "boxes_num should not be None in dygraph mode."
align_out = _legacy_C_ops.roi_align(
x,
boxes,
boxes_num,
"pooled_height",
pooled_height,
"pooled_width",
pooled_width,
"spatial_scale",
spatial_scale,
"sampling_ratio",
sampling_ratio,
"aligned",
aligned,
)
return align_out
else:
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'roi_align')
check_variable_and_dtype(
......@@ -2143,9 +1933,7 @@ def nms(
if in_dygraph_mode():
return _C_ops.nms(boxes, iou_threshold)
if _non_static_mode():
return _legacy_C_ops.nms(boxes, 'iou_threshold', iou_threshold)
else:
helper = LayerHelper('nms', **locals())
out = helper.create_variable_for_type_inference('int64')
helper.append_op(
......@@ -2222,7 +2010,7 @@ def nms(
if top_k is None:
return keep_boxes_idxs[sorted_sub_indices]
if _non_static_mode():
if in_dygraph_mode():
top_k = shape if shape < top_k else top_k
_, topk_sub_indices = paddle.topk(scores[keep_boxes_idxs], top_k)
return keep_boxes_idxs[topk_sub_indices]
......@@ -2331,34 +2119,7 @@ def generate_proposals(
)
return rpn_rois, rpn_roi_probs, rpn_rois_num
elif _non_static_mode():
assert (
return_rois_num
), "return_rois_num should be True in dygraph mode."
attrs = (
'pre_nms_topN',
pre_nms_top_n,
'post_nms_topN',
post_nms_top_n,
'nms_thresh',
nms_thresh,
'min_size',
min_size,
'eta',
eta,
'pixel_offset',
pixel_offset,
)
(
rpn_rois,
rpn_roi_probs,
rpn_rois_num,
) = _legacy_C_ops.generate_proposals_v2(
scores, bbox_deltas, img_size, anchors, variances, *attrs
)
return rpn_rois, rpn_roi_probs, rpn_rois_num
else:
helper = LayerHelper('generate_proposals_v2', **locals())
check_variable_and_dtype(
......@@ -2368,7 +2129,10 @@ def generate_proposals(
bbox_deltas, 'bbox_deltas', ['float32'], 'generate_proposals_v2'
)
check_variable_and_dtype(
img_size, 'img_size', ['float32', 'float64'], 'generate_proposals_v2'
img_size,
'img_size',
['float32', 'float64'],
'generate_proposals_v2',
)
check_variable_and_dtype(
anchors, 'anchors', ['float32'], 'generate_proposals_v2'
......@@ -2388,7 +2152,9 @@ def generate_proposals(
'RpnRoiProbs': rpn_roi_probs,
}
if return_rois_num:
rpn_rois_num = helper.create_variable_for_type_inference(dtype='int32')
rpn_rois_num = helper.create_variable_for_type_inference(
dtype='int32'
)
rpn_rois_num.stop_gradient = True
outputs['RpnRoisNum'] = rpn_rois_num
......@@ -2535,31 +2301,6 @@ def matrix_nms(
if not return_rois_num:
rois_num = None
return out, rois_num, index
elif _in_legacy_dygraph():
attrs = (
'background_label',
background_label,
'score_threshold',
score_threshold,
'post_threshold',
post_threshold,
'nms_top_k',
nms_top_k,
'gaussian_sigma',
gaussian_sigma,
'use_gaussian',
use_gaussian,
'keep_top_k',
keep_top_k,
'normalized',
normalized,
)
out, index, rois_num = _legacy_C_ops.matrix_nms(bboxes, scores, *attrs)
if not return_index:
index = None
if not return_rois_num:
rois_num = None
return out, rois_num, index
else:
helper = LayerHelper('matrix_nms', **locals())
output = helper.create_variable_for_type_inference(dtype=bboxes.dtype)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册