未验证 提交 50ddc0b2 编写于 作者: H hong 提交者: GitHub

Add dist norm yamls (#41424)

* add dist erfinv gumbel softmax

* fix test gumbel softmax op bug

* try to fix gumbel softmax error

* add label smooth backlist
上级 90cb337e
...@@ -37,6 +37,7 @@ def dist(x, y, p): ...@@ -37,6 +37,7 @@ def dist(x, y, p):
class TestDistOp(OpTest): class TestDistOp(OpTest):
def setUp(self): def setUp(self):
self.op_type = 'dist' self.op_type = 'dist'
self.python_api = paddle.dist
self.attrs = {} self.attrs = {}
self.init_case() self.init_case()
self.init_data_type() self.init_data_type()
...@@ -106,10 +107,14 @@ class TestDistOp(OpTest): ...@@ -106,10 +107,14 @@ class TestDistOp(OpTest):
return x_grad, y_grad return x_grad, y_grad
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output(check_eager=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad(["X", "Y"], "Out", user_defined_grads=self.gradient) self.check_grad(
["X", "Y"],
"Out",
user_defined_grads=self.gradient,
check_eager=True)
class TestDistOpCase1(TestDistOp): class TestDistOpCase1(TestDistOp):
...@@ -174,4 +179,5 @@ class TestDistAPI(unittest.TestCase): ...@@ -174,4 +179,5 @@ class TestDistAPI(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -28,6 +28,7 @@ np.random.seed(0) ...@@ -28,6 +28,7 @@ np.random.seed(0)
class TestErfinv(OpTest): class TestErfinv(OpTest):
def setUp(self): def setUp(self):
self.op_type = "erfinv" self.op_type = "erfinv"
self.python_api = paddle.erfinv
self.init_dtype() self.init_dtype()
self.shape = [11, 17] self.shape = [11, 17]
self.x = np.random.uniform(-1, 1, size=self.shape).astype(self.dtype) self.x = np.random.uniform(-1, 1, size=self.shape).astype(self.dtype)
...@@ -42,7 +43,7 @@ class TestErfinv(OpTest): ...@@ -42,7 +43,7 @@ class TestErfinv(OpTest):
self.dtype = np.float64 self.dtype = np.float64
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output(check_eager=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad( self.check_grad(
......
...@@ -27,6 +27,7 @@ class TestExpandV2OpRank1(OpTest): ...@@ -27,6 +27,7 @@ class TestExpandV2OpRank1(OpTest):
def setUp(self): def setUp(self):
self.op_type = "expand_v2" self.op_type = "expand_v2"
self.init_data() self.init_data()
self.python_api = paddle.expand
self.inputs = {'X': np.random.random(self.ori_shape).astype("float64")} self.inputs = {'X': np.random.random(self.ori_shape).astype("float64")}
self.attrs = {'shape': self.shape} self.attrs = {'shape': self.shape}
......
...@@ -17,6 +17,7 @@ import paddle.fluid.core as core ...@@ -17,6 +17,7 @@ import paddle.fluid.core as core
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid import Program, program_guard from paddle.fluid import Program, program_guard
from paddle.fluid.framework import _test_eager_guard
paddle.enable_static() paddle.enable_static()
...@@ -177,12 +178,17 @@ class TestGumbelSoftmaxAPI(unittest.TestCase): ...@@ -177,12 +178,17 @@ class TestGumbelSoftmaxAPI(unittest.TestCase):
self.assertEqual(out_np.sum(), self.count_expected) self.assertEqual(out_np.sum(), self.count_expected)
# test dygrapg api # test dygrapg api
paddle.disable_static() with paddle.fluid.dygraph.base.guard():
x = paddle.to_tensor(self.x) x = paddle.to_tensor(self.x)
y = paddle.nn.functional.gumbel_softmax(x, hard=True) y = paddle.nn.functional.gumbel_softmax(x, hard=True)
out_np = np.array(y) out_np = np.array(y)
self.assertEqual(out_np.sum(), self.count_expected) self.assertEqual(out_np.sum(), self.count_expected)
paddle.enable_static()
with _test_eager_guard():
x = paddle.to_tensor(self.x)
y = paddle.nn.functional.gumbel_softmax(x, hard=True)
out_np = np.array(y)
self.assertEqual(out_np.sum(), self.count_expected)
class TestGumbelSoftmaxOpError(unittest.TestCase): class TestGumbelSoftmaxOpError(unittest.TestCase):
......
...@@ -1524,6 +1524,9 @@ def gumbel_softmax(x, temperature=1.0, hard=False, axis=-1, name=None): ...@@ -1524,6 +1524,9 @@ def gumbel_softmax(x, temperature=1.0, hard=False, axis=-1, name=None):
# [0.00000000, 0.00000000, 0.00000000, 0.00001258, 0.99998736, 0.00000000]] # [0.00000000, 0.00000000, 0.00000000, 0.00001258, 0.99998736, 0.00000000]]
""" """
if in_dygraph_mode():
return _C_ops.final_state_gumbel_softmax(x, temperature, hard, axis)
if in_dynamic_mode(): if in_dynamic_mode():
return _C_ops.gumbel_softmax(x, 'temperature', temperature, 'hard', return _C_ops.gumbel_softmax(x, 'temperature', temperature, 'hard',
hard, 'axis', axis) hard, 'axis', axis)
......
...@@ -551,6 +551,9 @@ def dist(x, y, p=2, name=None): ...@@ -551,6 +551,9 @@ def dist(x, y, p=2, name=None):
out = paddle.dist(x, y, float("-inf")) out = paddle.dist(x, y, float("-inf"))
print(out) # out = [0.] print(out) # out = [0.]
""" """
if in_dygraph_mode():
return _C_ops.final_state_dist(x, y, p)
check_variable_and_dtype(x, 'dtype', ['float32', 'float64'], 'dist') check_variable_and_dtype(x, 'dtype', ['float32', 'float64'], 'dist')
check_variable_and_dtype(y, 'dtype', ['float32', 'float64'], 'dist') check_variable_and_dtype(y, 'dtype', ['float32', 'float64'], 'dist')
check_type(p, 'p', (float, int), 'dist') check_type(p, 'p', (float, int), 'dist')
......
...@@ -3636,6 +3636,9 @@ def erfinv(x, name=None): ...@@ -3636,6 +3636,9 @@ def erfinv(x, name=None):
# out: [0, 0.4769, -inf] # out: [0, 0.4769, -inf]
""" """
if in_dygraph_mode():
return _C_ops.final_state_erfinv( x )
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'erfinv') check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'erfinv')
if paddle.in_dynamic_mode(): if paddle.in_dynamic_mode():
......
...@@ -814,7 +814,7 @@ ...@@ -814,7 +814,7 @@
func : GumbelSoftmaxInferMeta func : GumbelSoftmaxInferMeta
kernel : kernel :
func : gumbel_softmax func : gumbel_softmax
# backward : gumbel_softmax_grad backward : gumbel_softmax_grad
# hard_shrink # hard_shrink
- api : hard_shrink - api : hard_shrink
......
# - backward_api : gumbel_softmax_grad
# forward : gumbel_softmax (Tensor x, float temperature, bool hard, int axis) -> Tensor(out)
# args : (Tensor out, Tensor out_grad, int axis)
# output : Tensor(x_grad)
# infer_meta :
# func : GumbelSoftmaxGradInferMeta
# param : [out, out_grad, axis]
# kernel :
# func : gumbel_softmax_grad
- backward_api : abs_grad - backward_api : abs_grad
forward : abs (Tensor x) -> Tensor(out) forward : abs (Tensor x) -> Tensor(out)
args : (Tensor x, Tensor out_grad) args : (Tensor x, Tensor out_grad)
...@@ -548,6 +538,16 @@ ...@@ -548,6 +538,16 @@
func : graph_send_recv_grad func : graph_send_recv_grad
optional: out, dst_count optional: out, dst_count
- backward_api : gumbel_softmax_grad
forward : gumbel_softmax (Tensor x, float temperature, bool hard, int axis) -> Tensor(out)
args : (Tensor out, Tensor out_grad, int axis)
output : Tensor(x_grad)
infer_meta :
func : GumbelSoftmaxGradInferMeta
param : [out, out_grad, axis]
kernel :
func : gumbel_softmax_grad
- backward_api : hard_shrink_grad - backward_api : hard_shrink_grad
forward : hard_shrink (Tensor x, float threshold) -> Tensor(out) forward : hard_shrink (Tensor x, float threshold) -> Tensor(out)
args : (Tensor x, Tensor out_grad, float threshold) args : (Tensor x, Tensor out_grad, float threshold)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册