From f4075db8de3dae5f90a3cdaa46a4b578b6c7ff93 Mon Sep 17 00:00:00 2001 From: hong <43953930+phlrain@users.noreply.github.com> Date: Wed, 23 Mar 2022 14:34:47 +0800 Subject: [PATCH] Add yaml config part2 (#40742) * fix error; test=develop * update * close some yaml * fix backward attrite error; test=develop * add div test * polish code; test=develop * remove none gbk charactor; * remove some yaml; * fix optional bug * recover yaml config * resolve confilct; test=develop * close div; test=develop --- .../final_state_generator/eager_gen.py | 22 +++++++--- .../final_state_generator/python_c_gen.py | 10 +++-- paddle/fluid/pybind/eager_utils.cc | 2 +- .../unittests/test_elementwise_div_op.py | 6 ++- python/paddle/tensor/math.py | 10 ++++- python/paddle/utils/code_gen/api.yaml | 10 +++-- python/paddle/utils/code_gen/backward.yaml | 44 ++++++++++++++++--- .../utils/code_gen/wrapped_infermeta_gen.py | 2 + 8 files changed, 80 insertions(+), 26 deletions(-) diff --git a/paddle/fluid/eager/auto_code_generator/final_state_generator/eager_gen.py b/paddle/fluid/eager/auto_code_generator/final_state_generator/eager_gen.py index 1de050d123..1d18cbe782 100644 --- a/paddle/fluid/eager/auto_code_generator/final_state_generator/eager_gen.py +++ b/paddle/fluid/eager/auto_code_generator/final_state_generator/eager_gen.py @@ -470,7 +470,7 @@ def SlotNameMatching(backward_inputs_list, backward_returns_list, backward_input_type, False, backward_input_pos ] else: - assert False + assert False, backward_input_name for backward_output in backward_returns_list: backward_output_name = backward_output[0] @@ -479,7 +479,8 @@ def SlotNameMatching(backward_inputs_list, backward_returns_list, backward_fwd_name = FindForwardName(backward_output_name) assert backward_fwd_name is not None - assert backward_fwd_name in forward_inputs_position_map.keys() + assert backward_fwd_name in forward_inputs_position_map.keys( + ), backward_fwd_name matched_forward_input_type = forward_inputs_position_map[ backward_fwd_name][0] @@ -772,7 +773,7 @@ def GenerateNodeCreationCodes( output_autograd_meta = f" egr::AutogradMeta* {output_autograd_meta_name} = egr::EagerUtils::autograd_meta(&std::get<{pos}>(api_result));" else: assert IsVectorTensorType(rtype) - output_autograd_meta = f" std::vector {output_autograd_meta_vec_name} = egr::EagerUtils::autograd_meta(&api_result[{pos}]);\n" + output_autograd_meta = f" std::vector {output_autograd_meta_vec_name} = egr::EagerUtils::autograd_meta(&std::get<{pos}>(api_result));\n" output_autograd_meta += f" std::vector* {output_autograd_meta_name} = &{output_autograd_meta_vec_name};" outputs_autograd_meta_list.append(output_autograd_meta) @@ -808,8 +809,15 @@ def GenerateNodeCreationCodes( # SetAttributes set_attributes_list = [] - for name, _, _, _ in backward_attrs_list: - set_attributes = f" grad_node->SetAttribute{name}({name});" + forward_attrs_name_set = set() + for name, _, _, _ in forward_attrs_list: + forward_attrs_name_set.add(name) + + for name, _, default_val_attr, _ in backward_attrs_list: + if name in forward_attrs_name_set: + set_attributes = f" grad_node->SetAttribute{name}({name});" + else: + set_attributes = f" grad_node->SetAttribute{name}({default_val_attr});" set_attributes_list.append(set_attributes) set_attributes_str = "\n".join(set_attributes_list) @@ -1253,7 +1261,7 @@ if __name__ == "__main__": inplace_map = ParseInplaceInfo(fwd_api['inplace']) bwd_api_name = fwd_api['backward'] - assert bwd_api_name in grad_api_dict.keys() + assert bwd_api_name in grad_api_dict.keys(), bwd_api_name bwd_api = grad_api_dict[bwd_api_name] assert 'args' in bwd_api.keys() @@ -1325,7 +1333,7 @@ if __name__ == "__main__": print("Generated Backward Grad Output Map: ", backward_grad_output_map) - # Backward Validation Check + # Backward Validation Check BackwardValidationCheck(backward_fwd_input_map, backward_grad_input_map, backward_attrs_list) diff --git a/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py b/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py index 117a2b2b59..5a732212a5 100644 --- a/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py +++ b/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py @@ -59,7 +59,7 @@ def FindParsingFunctionFromAttributeType(atype): ## Refactored Functions ## ########################## PARSE_PYTHON_C_TENSORS_TEMPLATE = \ -" auto {} = {}(\"{}\", \"{}\", args, {}, false);\n" +" auto {} = {}(\"{}\", \"{}\", args, {}, {});\n" PARSE_PYTHON_C_ARGS_TEMPLATE = \ @@ -311,15 +311,17 @@ class PythonCSingleFunctionGenerator: is_optional = (name in optional_inputs) if IsVectorTensorType(ttype): get_eager_tensor_str += PARSE_PYTHON_C_TENSORS_TEMPLATE.format( - name, "GetTensorListFromArgs", forward_api_name, name, pos) + name, "GetTensorListFromArgs", forward_api_name, name, pos, + "false") else: if is_optional: get_eager_tensor_str += PARSE_PYTHON_C_TENSORS_TEMPLATE.format( name, "GetOptionalTensorFromArgs", forward_api_name, - name, pos) + name, pos, "true") else: get_eager_tensor_str += PARSE_PYTHON_C_TENSORS_TEMPLATE.format( - name, "GetTensorFromArgs", forward_api_name, name, pos) + name, "GetTensorFromArgs", forward_api_name, name, pos, + "false") parse_attributes_str = "" diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index d2e96dc2bb..2e884b212a 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -850,7 +850,7 @@ paddle::experimental::ScalarArray CastPyArg2ScalarArray( // obj could be: int, float, bool, paddle.Tensor PyTypeObject* type = obj->ob_type; auto type_name = std::string(type->tp_name); - if (type_name == "list") { + if (type_name == "list" || type_name == "tuple") { std::vector value = CastPyArg2Ints(obj, op_type, arg_pos); return paddle::experimental::ScalarArray(value); diff --git a/python/paddle/fluid/tests/unittests/test_elementwise_div_op.py b/python/paddle/fluid/tests/unittests/test_elementwise_div_op.py index a43e56b081..a86758a9cb 100644 --- a/python/paddle/fluid/tests/unittests/test_elementwise_div_op.py +++ b/python/paddle/fluid/tests/unittests/test_elementwise_div_op.py @@ -24,6 +24,7 @@ from op_test import OpTest, skip_check_grad_ci, convert_float_to_uint16 class ElementwiseDivOp(OpTest): def setUp(self): self.op_type = "elementwise_div" + self.python_api = paddle.divide self.dtype = np.float64 self.init_dtype() """ Warning @@ -37,8 +38,11 @@ class ElementwiseDivOp(OpTest): } self.outputs = {'Out': np.divide(self.inputs['X'], self.inputs['Y'])} + def check_eager(self): + return (self.use_mkldnn == False and self.axis == -1) + def test_check_output(self): - self.check_output() + self.check_output(check_eager=False) def test_check_grad_normal(self): self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.05) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index a79fe5172f..04ca7da104 100755 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -243,8 +243,8 @@ def add(x, y, name=None): """ if paddle.in_dynamic_mode(): - #if _in_eager_mode(): - #return _C_ops.final_state_add(x, y) + if _in_eager_mode(): + return _C_ops.final_state_add( x, y) return _C_ops.elementwise_add(x, y) return _elementwise_op(LayerHelper('elementwise_add', **locals())) @@ -324,6 +324,8 @@ def subtract(x, y, name=None): axis = -1 act = None if paddle.in_dynamic_mode(): + if _in_eager_mode(): + return _C_ops.final_state_subtract(x, y) return _elementwise_op_in_dygraph( x, y, axis=axis, act=act, op_name=op_type) return _elementwise_op(LayerHelper(op_type, **locals())) @@ -381,6 +383,8 @@ def divide(x, y, name=None): axis = -1 act = None if paddle.in_dynamic_mode(): + if _in_eager_mode(): + return _C_ops.final_state_divide( x, y) return _elementwise_op_in_dygraph( x, y, axis=axis, act=act, op_name=op_type) @@ -510,6 +514,8 @@ def multiply(x, y, name=None): axis = -1 if paddle.in_dynamic_mode(): + if _in_eager_mode(): + return _C_ops.final_state_multiply(x, y) return _elementwise_op_in_dygraph( x, y, axis=axis, act=act, op_name=op_type) diff --git a/python/paddle/utils/code_gen/api.yaml b/python/paddle/utils/code_gen/api.yaml index 8684e32957..40d5b593a0 100644 --- a/python/paddle/utils/code_gen/api.yaml +++ b/python/paddle/utils/code_gen/api.yaml @@ -5,7 +5,7 @@ func : ElementwiseInferMeta kernel : func : add - # backward : add_grad + backward : add_grad - api : cast args : (Tensor x, DataType out_dtype) @@ -47,6 +47,7 @@ func : ElementwiseInferMeta kernel : func : divide + backward : divide_grad - api : dot args : (Tensor x, Tensor y) @@ -136,6 +137,7 @@ func : ElementwiseInferMeta kernel : func : multiply + backward : multiply_grad - api : ones_like args : (Tensor x, DataType dtype=DataType::UNDEFINED, Place place={}) @@ -207,6 +209,7 @@ func : ElementwiseInferMeta kernel : func : subtract + backward : subtract_grad - api : sum args : (Tensor x, int64[] axis={}, DataType dtype=DataType::UNDEFINED, bool keep_dim=false) @@ -277,7 +280,6 @@ func : diagonal backward : diagonal_grad - - api : gumbel_softmax args : (Tensor x, float temperature, bool hard, int axis) output : Tensor @@ -368,7 +370,7 @@ func : AdamaxInferMeta kernel : func : adamax - + - api : where @@ -424,7 +426,7 @@ func : CompareInferMeta kernel : func : equal - + - api : not_equal args : (Tensor x, Tensor y, int axis = -1) output : Tensor diff --git a/python/paddle/utils/code_gen/backward.yaml b/python/paddle/utils/code_gen/backward.yaml index 57f4028304..ff5ebd6ef6 100644 --- a/python/paddle/utils/code_gen/backward.yaml +++ b/python/paddle/utils/code_gen/backward.yaml @@ -25,10 +25,9 @@ output : Tensor(x_grad) invoke : scale(out_grad, scale, bias, bias_after_scale) - - backward_api : add_grad forward : add (Tensor x, Tensor y) -> Tensor(out) - args : (Tensor x, Tensor y, Tensor out_grad) + args : (Tensor x, Tensor y, Tensor out_grad, int axis = -1) output : Tensor(x_grad), Tensor(y_grad) infer_meta : func : GeneralBinaryGradInferMeta @@ -36,6 +35,37 @@ kernel : func : add_grad +- backward_api : subtract_grad + forward : subtract (Tensor x, Tensor y) -> Tensor(out) + args : (Tensor x, Tensor y, Tensor out_grad, int axis = -1) + output : Tensor(x_grad), Tensor(y_grad) + infer_meta : + func : GeneralBinaryGradInferMeta + param : [x, y] + kernel : + func : subtract_grad + +- backward_api : multiply_grad + forward : multiply (Tensor x, Tensor y) -> Tensor(out) + args : (Tensor x, Tensor y, Tensor out_grad, int axis = -1) + output : Tensor(x_grad), Tensor(y_grad) + infer_meta : + func : GeneralBinaryGradInferMeta + param : [x, y] + kernel : + func : multiply_grad + +- backward_api : divide_grad + forward : divide (Tensor x, Tensor y) -> Tensor(out) + args : (Tensor x, Tensor y, Tensor out_grad, int axis = -1) + output : Tensor(x_grad), Tensor(y_grad) + infer_meta : + func : GeneralBinaryGradInferMeta + param : [x, y] + kernel : + func : divide_grad + + - backward_api : digamma_grad forward : digamma (Tensor x) -> Tensor(out) args : (Tensor x, Tensor out_grad) @@ -78,7 +108,7 @@ # - backward_api : norm_grad # forward : norm (Tensor x, int axis, float epsilon, bool is_test) -> Tensor(out), Tensor(norm) -# args : (Tensor out_grad, Tensor x, Tensor norm, int axis, float epsilon, bool is_test) +# args : (Tensor x, Tensor norm, Tensor out_grad, int axis, float epsilon, bool is_test) # output : Tensor(x_grad) # infer_meta : # func : UnchangedInferMeta @@ -121,7 +151,7 @@ # param : [out, out_grad, axis] # kernel : # func : gumbel_softmax_grad - + - backward_api : transpose_grad forward : transpose (Tensor x, int[] axis) -> Tensor(out) @@ -132,7 +162,7 @@ param : [out_grad, axis] kernel : func : transpose_grad - + # - backward_api : lerp_grad # forward : transpose (Tensor x, Tensor y, Tensor weight) -> Tensor(out) # args : (Tensor x, Tensor y, Tensor weight, Tensor out, Tensor out_grad) @@ -243,7 +273,7 @@ param : [input] kernel : func : bce_loss_grad - + # - backward_api : dist_grad # forward : dist (Tensor x, Tensor y, float p) -> Tensor(out) @@ -266,7 +296,7 @@ param : [x] kernel : func : gather_nd_grad - + - backward_api : mv_grad forward : mv (Tensor x, Tensor vec) -> Tensor(out) args : (Tensor x, Tensor vec, Tensor out_grad) diff --git a/python/paddle/utils/code_gen/wrapped_infermeta_gen.py b/python/paddle/utils/code_gen/wrapped_infermeta_gen.py index 1cb3c33da7..aab4b21974 100644 --- a/python/paddle/utils/code_gen/wrapped_infermeta_gen.py +++ b/python/paddle/utils/code_gen/wrapped_infermeta_gen.py @@ -43,6 +43,8 @@ PD_REGISTER_INFER_META_FN({api.kernel['func'][0]}, phi::{api.infer_meta['func']} 'const std::vector&': 'const std::vector&', 'Tensor': 'MetaTensor*', 'std::vector': 'std::vector*', + 'const paddle::optional': + 'const paddle::optional' } wrapped_infermeta_name = get_wrapped_infermeta_name(api.api) -- GitLab