未验证 提交 0d642d3a 编写于 作者: H hong 提交者: GitHub

add norm, segment_pool (#41465)

上级 50ddc0b2
......@@ -126,6 +126,7 @@ class TestBincountOp(OpTest):
# without weights
def setUp(self):
self.op_type = "bincount"
self.python_api = paddle.bincount
self.init_test_case()
self.inputs = {"X": self.np_input}
self.attrs = {"minlength": self.minlength}
......@@ -137,13 +138,14 @@ class TestBincountOp(OpTest):
self.Out = np.bincount(self.np_input, minlength=self.minlength)
def test_check_output(self):
self.check_output()
self.check_output(check_eager=False)
class TestCase1(TestBincountOp):
# with weights(FLOAT32)
def setUp(self):
self.op_type = "bincount"
self.python_api = paddle.bincount
self.init_test_case()
self.inputs = {"X": self.np_input, "Weights": self.np_weights}
self.attrs = {"minlength": self.minlength}
......@@ -163,6 +165,7 @@ class TestCase2(TestBincountOp):
# with weights(other)
def setUp(self):
self.op_type = "bincount"
self.python_api = paddle.bincount
self.init_test_case()
self.inputs = {"X": self.np_input, "Weights": self.np_weights}
self.attrs = {"minlength": self.minlength}
......
......@@ -32,6 +32,7 @@ def l2_norm(x, axis, epsilon):
class TestNormOp(OpTest):
def setUp(self):
self.op_type = "norm"
self.python_api = paddle.fluid.layers.l2_normalize
self.init_test_case()
self.init_dtype()
x = np.random.random(self.shape).astype(self.dtype)
......
......@@ -73,6 +73,17 @@ def compute_segment_min_max(x, segment_ids, pooltype="MAX"):
return results, gradient / results.size
def segment_pool_split(X, SegmentIds, pooltype):
if pooltype == "SUM":
return paddle.incubate.tensor.segment_sum(X, SegmentIds)
elif pooltype == "MEAN":
return paddle.incubate.tensor.segment_mean(X, SegmentIds)
elif pooltype == "MIN":
return paddle.incubate.tensor.segment_min(X, SegmentIds)
elif pooltype == "MAX":
return paddle.incubate.tensor.segment_max(X, SegmentIds)
class TestSegmentOps(OpTest):
def set_data(self):
x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
......@@ -90,6 +101,8 @@ class TestSegmentOps(OpTest):
def prepare(self):
self.op_type = "segment_pool"
self.python_api = segment_pool_split
self.python_out_sig = ["Out"]
self.dtype = np.float64
self.shape = [30, 15]
self.attrs = {"pooltype": "SUM"}
......@@ -105,10 +118,10 @@ class TestSegmentOps(OpTest):
self.outputs = {'Out': result.astype(self.dtype)}
def test_check_output(self):
self.check_output()
self.check_output(check_eager=True)
def test_check_grad(self):
self.check_grad(["X"], "Out")
self.check_grad(["X"], "Out", check_eager=True)
class TestSegmentSum2(TestSegmentOps):
......@@ -259,4 +272,5 @@ class API_SegmentOpsTest(unittest.TestCase):
if __name__ == '__main__':
paddle.enable_static()
unittest.main()
......@@ -52,7 +52,7 @@ def segment_sum(data, segment_ids, name=None):
"""
if in_dygraph_mode():
return _C_ops.final_state_segment_pool(data, segment_idsm, "SUM")[0]
return _C_ops.final_state_segment_pool(data, segment_ids, "SUM")[0]
if _in_legacy_dygraph():
out, tmp = _C_ops.segment_pool(data, segment_ids, 'pooltype', "SUM")
return out
......@@ -109,7 +109,7 @@ def segment_mean(data, segment_ids, name=None):
"""
if in_dygraph_mode():
return _C_ops.final_state_segment_pool(data, segment_idsm, "MEAN")[0]
return _C_ops.final_state_segment_pool(data, segment_ids, "MEAN")[0]
if _non_static_mode():
out, tmp = _C_ops.segment_pool(data, segment_ids, 'pooltype', "MEAN")
return out
......@@ -165,7 +165,7 @@ def segment_min(data, segment_ids, name=None):
"""
if in_dygraph_mode():
return _C_ops.final_state_segment_pool(data, segment_idsm, "MIN")[0]
return _C_ops.final_state_segment_pool(data, segment_ids, "MIN")[0]
if _non_static_mode():
out, tmp = _C_ops.segment_pool(data, segment_ids, 'pooltype', "MIN")
......@@ -222,7 +222,7 @@ def segment_max(data, segment_ids, name=None):
"""
if in_dygraph_mode():
out, tmp = _C_ops.final_state_segment_pool(data, segment_ids, "MAX")[0]
out = _C_ops.final_state_segment_pool(data, segment_ids, "MAX")[0]
return out
if _non_static_mode():
......
......@@ -17,7 +17,7 @@ from ..fluid.layer_helper import LayerHelper
from ..framework import _varbase_creator, _dygraph_tracer
from ..fluid.data_feeder import check_variable_and_dtype, check_type, check_dtype
from ..static import Variable
from ..fluid.framework import _in_legacy_dygraph, in_dygraph_mode
from ..fluid.framework import _in_legacy_dygraph, in_dygraph_mode, _non_static_mode
from ..fluid.layers import transpose, cast # noqa: F401
from ..fluid import layers
import paddle
......@@ -1487,10 +1487,7 @@ def bincount(x, weights=None, minlength=0, name=None):
if x.dtype not in [paddle.int32, paddle.int64]:
raise TypeError("Elements in Input(x) should all be integers")
# if in_dygraph_mode():
# return _C_ops.final_state_bincount(x, weights, minlength)
if _in_legacy_dygraph():
if _non_static_mode():
return _C_ops.bincount(x, weights, "minlength", minlength)
helper = LayerHelper('bincount', **locals())
......
......@@ -1363,6 +1363,16 @@
optional : weight
backward : nll_loss_grad
- api : norm
args : (Tensor x, int axis, float epsilon, bool is_test)
output : Tensor(out), Tensor(norm)
infer_meta :
func : NormInferMeta
kernel :
func : norm
intermediate : norm
backward : norm_grad
- api : not_equal
args : (Tensor x, Tensor y, int axis = -1)
output : Tensor
......@@ -1669,6 +1679,7 @@
func : SegmentPoolInferMeta
kernel :
func : segment_pool
data_type : x
backward : segment_pool_grad
# selu
......
......@@ -980,6 +980,16 @@
data_type : input
optional : weight
- backward_api : norm_grad
forward : norm (Tensor x, int axis, float epsilon, bool is_test) -> Tensor(out), Tensor(norm)
args : (Tensor x, Tensor norm, Tensor out_grad, int axis, float epsilon, bool is_test)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
param : [x]
kernel :
func : norm_grad
- backward_api : p_norm_grad
forward : p_norm(Tensor x, float porder, int axis, float epsilon, bool keepdim, bool asvector=false) -> Tensor(out)
args : (Tensor x, Tensor out, Tensor out_grad, float porder, int axis, float epsilon, bool keepdim, bool asvector)
......@@ -1211,6 +1221,7 @@
param : [x]
kernel :
func : segment_pool_grad
optional : summed_ids
- backward_api : selu_grad
forward : selu (Tensor x, float scale, float alpha) -> Tensor(out)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册