From ea91ca2ff6d0f80fd372dba5bf7f3956826bdad5 Mon Sep 17 00:00:00 2001 From: Zhong Hui Date: Wed, 27 Jul 2022 19:24:06 +0800 Subject: [PATCH] [Eager] Add hierarchical_sigmoid yaml (#44638) --- paddle/phi/api/yaml/generator/api_base.py | 2 +- paddle/phi/api/yaml/legacy_api.yaml | 12 +++++ paddle/phi/api/yaml/legacy_backward.yaml | 13 +++++- .../fluid/tests/unittests/test_hsigmoid_op.py | 45 ++++++++++++++++--- python/paddle/nn/functional/loss.py | 6 ++- 5 files changed, 68 insertions(+), 10 deletions(-) diff --git a/paddle/phi/api/yaml/generator/api_base.py b/paddle/phi/api/yaml/generator/api_base.py index 2659d80615..833eadcf9d 100644 --- a/paddle/phi/api/yaml/generator/api_base.py +++ b/paddle/phi/api/yaml/generator/api_base.py @@ -135,7 +135,7 @@ class BaseAPI(object): 'double': 'double', 'bool': 'bool', 'str': 'const std::string&', - 'str[] ': 'const std::vector&', + 'str[]': 'const std::vector&', 'Place': 'const Place&', 'DataLayout': 'DataLayout', 'DataType': 'DataType', diff --git a/paddle/phi/api/yaml/legacy_api.yaml b/paddle/phi/api/yaml/legacy_api.yaml index 6a4afd3d06..53514ca566 100644 --- a/paddle/phi/api/yaml/legacy_api.yaml +++ b/paddle/phi/api/yaml/legacy_api.yaml @@ -1038,6 +1038,18 @@ func : hard_swish backward : hard_swish_grad +# hierarchical_sigmoid +- api : hierarchical_sigmoid + args : (Tensor x, Tensor w, Tensor label, Tensor path, Tensor code, Tensor bias, int num_classes, bool remote_prefetch, int trainer_id, int64_t[] height_sections, str[] epmap, str[] table_names, bool is_sparse) + output : Tensor(out), Tensor(pre_out), Tensor(w_out) + infer_meta : + func : HierarchicalSigmoidInferMeta + optional: path, code, bias + kernel : + func : hierarchical_sigmoid + data_type : x + backward : hierarchical_sigmoid_grad + # histogram - api : histogram args : (Tensor x, int64_t bins, int min, int max) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 9d73c044db..bbcb6e347e 100644 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -935,6 +935,17 @@ func : hard_swish_grad inplace : (out_grad -> x_grad) +- backward_api : hierarchical_sigmoid_grad + forward : hierarchical_sigmoid (Tensor x, Tensor w, Tensor label, Tensor path, Tensor code, Tensor bias, int num_classes, bool remote_prefetch, int trainer_id, int64_t[] height_sections, str[] epmap, str[] table_names, bool is_sparse) -> Tensor(out), Tensor(pre_out), Tensor(w_out) + args : (Tensor x, Tensor w, Tensor label, Tensor path, Tensor code, Tensor bias, Tensor pre_out, Tensor out_grad, int num_classes, bool remote_prefetch, int trainer_id, int64_t[] height_sections, str[] epmap, str[] table_names, bool is_sparse) + output : Tensor(x_grad), Tensor(w_grad), Tensor(bias_grad) + infer_meta : + func : GeneralTernaryGradInferMeta + param : [x ,w, bias] + optional: path, code, bias + kernel : + func : hierarchical_sigmoid_grad + - backward_api : huber_loss_grad forward : huber_loss (Tensor input, Tensor label, float delta) -> Tensor(out), Tensor(residual) args : (Tensor residual, Tensor out_grad, float delta) @@ -2396,7 +2407,7 @@ backward : unsqueeze_double_grad - backward_api : warpctc_grad - forward : warpctc (Tensor logits, Tensor label, Tensor logits_length, Tensor labels_length, int blank, bool norm_by_times) -> Tensor(loss), Tensor(warpctcgrad) + forward : warpctc (Tensor logits, Tensor label, Tensor logits_length, Tensor labels_length, int blank, bool norm_by_times) -> Tensor(loss), Tensor(warpctcgrad) args : (Tensor logits, Tensor logits_length, Tensor warpctcgrad, Tensor loss_grad, int blank, bool norm_by_times) output : Tensor(logits_grad) infer_meta : diff --git a/python/paddle/fluid/tests/unittests/test_hsigmoid_op.py b/python/paddle/fluid/tests/unittests/test_hsigmoid_op.py index fc8b0d114d..5c5c15cc3c 100644 --- a/python/paddle/fluid/tests/unittests/test_hsigmoid_op.py +++ b/python/paddle/fluid/tests/unittests/test_hsigmoid_op.py @@ -172,10 +172,30 @@ def hsigmoidWithCustomTree(x, w, path_table, path_code, label, bias, return pre_output, out +def python_api(input, + weight, + label, + path_table=None, + path_code=None, + bias=None, + num_classes=-1, + is_sparse=False, + remote_prefetch=False): + assert is_sparse == remote_prefetch, "is_sparse is equal to remote_prefetch in dygraph." + return paddle.nn.functional.hsigmoid_loss(input, label, num_classes, weight, + bias, path_table, path_code, + is_sparse) + + +python_out_sig = ["Out"] + + class TestHSigmoidOp(OpTest): def setUp(self): self.op_type = "hierarchical_sigmoid" + self.python_api = python_api + self.python_out_sig = python_out_sig num_classes = 101 feature_size = 5 batch_size = 20 @@ -193,11 +213,12 @@ class TestHSigmoidOp(OpTest): self.user_grads = hsigmoid_grad(x, w, label, bias, num_classes) def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad(self): self.check_grad(['X', 'W', 'Bias'], ['Out'], - user_defined_grads=self.user_grads) + user_defined_grads=self.user_grads, + check_eager=True) @skip_check_grad_ci( @@ -208,6 +229,8 @@ class TestHSigmoidOpSparse(OpTest): def setUp(self): self.op_type = "hierarchical_sigmoid" + self.python_api = python_api + self.python_out_sig = python_out_sig num_classes = 6 #using 1,2,3,4,5,6 to build a huffman tree and select 1,2,5,6 as sample feature_size = 8 batch_size = 4 @@ -237,7 +260,7 @@ class TestHSigmoidOpSparse(OpTest): self.outputs = {'PreOut': pre_output, 'Out': out} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) class TestHSigmoidOpWithSparseGrad(unittest.TestCase): @@ -318,6 +341,8 @@ class TestHSigmoidOpWithCostumTree(OpTest): def setUp(self): self.op_type = "hierarchical_sigmoid" + self.python_api = python_api + self.python_out_sig = python_out_sig num_classes = 6 #using 1,2,3,4,5,6 to build a huffman tree and select 1,2,5,6 as sample feature_size = 8 batch_size = 4 @@ -347,10 +372,12 @@ class TestHSigmoidOpWithCostumTree(OpTest): self.outputs = {'PreOut': pre_output, 'Out': out} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad(self): - self.check_grad(['Bias', 'X', 'W'], ['Out'], no_grad_set=set('Label')) + self.check_grad(['Bias', 'X', 'W'], ['Out'], + no_grad_set=set('Label'), + check_eager=True) @skip_check_grad_ci( @@ -361,6 +388,8 @@ class TestHSigmoidOpWithCostumTreeWithoutBias(OpTest): def setUp(self): self.op_type = "hierarchical_sigmoid" + self.python_api = python_api + self.python_out_sig = python_out_sig num_classes = 6 #using 1,2,3,4,5,6 to build a huffman tree and select 1,2,5,6 as sample feature_size = 8 batch_size = 4 @@ -394,10 +423,12 @@ class TestHSigmoidOpWithCostumTreeWithoutBias(OpTest): self.outputs = {'PreOut': pre_output, 'Out': out} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad(self): - self.check_grad(['X', 'W'], ['Out'], no_grad_set=set('Label')) + self.check_grad(['X', 'W'], ['Out'], + no_grad_set=set('Label'), + check_eager=True) class TestHSigmoidLossAPI(unittest.TestCase): diff --git a/python/paddle/nn/functional/loss.py b/python/paddle/nn/functional/loss.py index f661d7f9db..9ebc5c03ef 100755 --- a/python/paddle/nn/functional/loss.py +++ b/python/paddle/nn/functional/loss.py @@ -920,7 +920,11 @@ def hsigmoid_loss(input, # [2.11009121] # [1.92374969]] """ - + if in_dygraph_mode(): + out, _, _ = _C_ops.final_state_hierarchical_sigmoid( + input, weight, label, path_table, path_code, bias, num_classes, + is_sparse, 0, [], [], [], is_sparse) + return out if _non_static_mode(): out, _, _ = _C_ops.hierarchical_sigmoid(input, weight, label, path_table, path_code, bias, -- GitLab