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

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
上级 b07d239c
...@@ -470,7 +470,7 @@ def SlotNameMatching(backward_inputs_list, backward_returns_list, ...@@ -470,7 +470,7 @@ def SlotNameMatching(backward_inputs_list, backward_returns_list,
backward_input_type, False, backward_input_pos backward_input_type, False, backward_input_pos
] ]
else: else:
assert False assert False, backward_input_name
for backward_output in backward_returns_list: for backward_output in backward_returns_list:
backward_output_name = backward_output[0] backward_output_name = backward_output[0]
...@@ -479,7 +479,8 @@ def SlotNameMatching(backward_inputs_list, backward_returns_list, ...@@ -479,7 +479,8 @@ def SlotNameMatching(backward_inputs_list, backward_returns_list,
backward_fwd_name = FindForwardName(backward_output_name) backward_fwd_name = FindForwardName(backward_output_name)
assert backward_fwd_name is not None 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[ matched_forward_input_type = forward_inputs_position_map[
backward_fwd_name][0] backward_fwd_name][0]
...@@ -772,7 +773,7 @@ def GenerateNodeCreationCodes( ...@@ -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));" output_autograd_meta = f" egr::AutogradMeta* {output_autograd_meta_name} = egr::EagerUtils::autograd_meta(&std::get<{pos}>(api_result));"
else: else:
assert IsVectorTensorType(rtype) assert IsVectorTensorType(rtype)
output_autograd_meta = f" std::vector<egr::AutogradMeta*> {output_autograd_meta_vec_name} = egr::EagerUtils::autograd_meta(&api_result[{pos}]);\n" output_autograd_meta = f" std::vector<egr::AutogradMeta*> {output_autograd_meta_vec_name} = egr::EagerUtils::autograd_meta(&std::get<{pos}>(api_result));\n"
output_autograd_meta += f" std::vector<egr::AutogradMeta*>* {output_autograd_meta_name} = &{output_autograd_meta_vec_name};" output_autograd_meta += f" std::vector<egr::AutogradMeta*>* {output_autograd_meta_name} = &{output_autograd_meta_vec_name};"
outputs_autograd_meta_list.append(output_autograd_meta) outputs_autograd_meta_list.append(output_autograd_meta)
...@@ -808,8 +809,15 @@ def GenerateNodeCreationCodes( ...@@ -808,8 +809,15 @@ def GenerateNodeCreationCodes(
# SetAttributes # SetAttributes
set_attributes_list = [] set_attributes_list = []
for name, _, _, _ in backward_attrs_list: forward_attrs_name_set = set()
set_attributes = f" grad_node->SetAttribute{name}({name});" 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_list.append(set_attributes)
set_attributes_str = "\n".join(set_attributes_list) set_attributes_str = "\n".join(set_attributes_list)
...@@ -1253,7 +1261,7 @@ if __name__ == "__main__": ...@@ -1253,7 +1261,7 @@ if __name__ == "__main__":
inplace_map = ParseInplaceInfo(fwd_api['inplace']) inplace_map = ParseInplaceInfo(fwd_api['inplace'])
bwd_api_name = fwd_api['backward'] 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] bwd_api = grad_api_dict[bwd_api_name]
assert 'args' in bwd_api.keys() assert 'args' in bwd_api.keys()
...@@ -1325,7 +1333,7 @@ if __name__ == "__main__": ...@@ -1325,7 +1333,7 @@ if __name__ == "__main__":
print("Generated Backward Grad Output Map: ", print("Generated Backward Grad Output Map: ",
backward_grad_output_map) backward_grad_output_map)
# Backward Validation Check # Backward Validation Check
BackwardValidationCheck(backward_fwd_input_map, BackwardValidationCheck(backward_fwd_input_map,
backward_grad_input_map, backward_grad_input_map,
backward_attrs_list) backward_attrs_list)
......
...@@ -59,7 +59,7 @@ def FindParsingFunctionFromAttributeType(atype): ...@@ -59,7 +59,7 @@ def FindParsingFunctionFromAttributeType(atype):
## Refactored Functions ## ## Refactored Functions ##
########################## ##########################
PARSE_PYTHON_C_TENSORS_TEMPLATE = \ PARSE_PYTHON_C_TENSORS_TEMPLATE = \
" auto {} = {}(\"{}\", \"{}\", args, {}, false);\n" " auto {} = {}(\"{}\", \"{}\", args, {}, {});\n"
PARSE_PYTHON_C_ARGS_TEMPLATE = \ PARSE_PYTHON_C_ARGS_TEMPLATE = \
...@@ -311,15 +311,17 @@ class PythonCSingleFunctionGenerator: ...@@ -311,15 +311,17 @@ class PythonCSingleFunctionGenerator:
is_optional = (name in optional_inputs) is_optional = (name in optional_inputs)
if IsVectorTensorType(ttype): if IsVectorTensorType(ttype):
get_eager_tensor_str += PARSE_PYTHON_C_TENSORS_TEMPLATE.format( 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: else:
if is_optional: if is_optional:
get_eager_tensor_str += PARSE_PYTHON_C_TENSORS_TEMPLATE.format( get_eager_tensor_str += PARSE_PYTHON_C_TENSORS_TEMPLATE.format(
name, "GetOptionalTensorFromArgs", forward_api_name, name, "GetOptionalTensorFromArgs", forward_api_name,
name, pos) name, pos, "true")
else: else:
get_eager_tensor_str += PARSE_PYTHON_C_TENSORS_TEMPLATE.format( 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 = "" parse_attributes_str = ""
......
...@@ -850,7 +850,7 @@ paddle::experimental::ScalarArray CastPyArg2ScalarArray( ...@@ -850,7 +850,7 @@ paddle::experimental::ScalarArray CastPyArg2ScalarArray(
// obj could be: int, float, bool, paddle.Tensor // obj could be: int, float, bool, paddle.Tensor
PyTypeObject* type = obj->ob_type; PyTypeObject* type = obj->ob_type;
auto type_name = std::string(type->tp_name); auto type_name = std::string(type->tp_name);
if (type_name == "list") { if (type_name == "list" || type_name == "tuple") {
std::vector<int> value = CastPyArg2Ints(obj, op_type, arg_pos); std::vector<int> value = CastPyArg2Ints(obj, op_type, arg_pos);
return paddle::experimental::ScalarArray(value); return paddle::experimental::ScalarArray(value);
......
...@@ -24,6 +24,7 @@ from op_test import OpTest, skip_check_grad_ci, convert_float_to_uint16 ...@@ -24,6 +24,7 @@ from op_test import OpTest, skip_check_grad_ci, convert_float_to_uint16
class ElementwiseDivOp(OpTest): class ElementwiseDivOp(OpTest):
def setUp(self): def setUp(self):
self.op_type = "elementwise_div" self.op_type = "elementwise_div"
self.python_api = paddle.divide
self.dtype = np.float64 self.dtype = np.float64
self.init_dtype() self.init_dtype()
""" Warning """ Warning
...@@ -37,8 +38,11 @@ class ElementwiseDivOp(OpTest): ...@@ -37,8 +38,11 @@ class ElementwiseDivOp(OpTest):
} }
self.outputs = {'Out': np.divide(self.inputs['X'], self.inputs['Y'])} 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): def test_check_output(self):
self.check_output() self.check_output(check_eager=False)
def test_check_grad_normal(self): def test_check_grad_normal(self):
self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.05) self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.05)
......
...@@ -243,8 +243,8 @@ def add(x, y, name=None): ...@@ -243,8 +243,8 @@ def add(x, y, name=None):
""" """
if paddle.in_dynamic_mode(): if paddle.in_dynamic_mode():
#if _in_eager_mode(): if _in_eager_mode():
#return _C_ops.final_state_add(x, y) return _C_ops.final_state_add( x, y)
return _C_ops.elementwise_add(x, y) return _C_ops.elementwise_add(x, y)
return _elementwise_op(LayerHelper('elementwise_add', **locals())) return _elementwise_op(LayerHelper('elementwise_add', **locals()))
...@@ -324,6 +324,8 @@ def subtract(x, y, name=None): ...@@ -324,6 +324,8 @@ def subtract(x, y, name=None):
axis = -1 axis = -1
act = None act = None
if paddle.in_dynamic_mode(): if paddle.in_dynamic_mode():
if _in_eager_mode():
return _C_ops.final_state_subtract(x, y)
return _elementwise_op_in_dygraph( return _elementwise_op_in_dygraph(
x, y, axis=axis, act=act, op_name=op_type) x, y, axis=axis, act=act, op_name=op_type)
return _elementwise_op(LayerHelper(op_type, **locals())) return _elementwise_op(LayerHelper(op_type, **locals()))
...@@ -381,6 +383,8 @@ def divide(x, y, name=None): ...@@ -381,6 +383,8 @@ def divide(x, y, name=None):
axis = -1 axis = -1
act = None act = None
if paddle.in_dynamic_mode(): if paddle.in_dynamic_mode():
if _in_eager_mode():
return _C_ops.final_state_divide( x, y)
return _elementwise_op_in_dygraph( return _elementwise_op_in_dygraph(
x, y, axis=axis, act=act, op_name=op_type) x, y, axis=axis, act=act, op_name=op_type)
...@@ -510,6 +514,8 @@ def multiply(x, y, name=None): ...@@ -510,6 +514,8 @@ def multiply(x, y, name=None):
axis = -1 axis = -1
if paddle.in_dynamic_mode(): if paddle.in_dynamic_mode():
if _in_eager_mode():
return _C_ops.final_state_multiply(x, y)
return _elementwise_op_in_dygraph( return _elementwise_op_in_dygraph(
x, y, axis=axis, act=act, op_name=op_type) x, y, axis=axis, act=act, op_name=op_type)
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
func : ElementwiseInferMeta func : ElementwiseInferMeta
kernel : kernel :
func : add func : add
# backward : add_grad backward : add_grad
- api : cast - api : cast
args : (Tensor x, DataType out_dtype) args : (Tensor x, DataType out_dtype)
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
func : ElementwiseInferMeta func : ElementwiseInferMeta
kernel : kernel :
func : divide func : divide
backward : divide_grad
- api : dot - api : dot
args : (Tensor x, Tensor y) args : (Tensor x, Tensor y)
...@@ -136,6 +137,7 @@ ...@@ -136,6 +137,7 @@
func : ElementwiseInferMeta func : ElementwiseInferMeta
kernel : kernel :
func : multiply func : multiply
backward : multiply_grad
- api : ones_like - api : ones_like
args : (Tensor x, DataType dtype=DataType::UNDEFINED, Place place={}) args : (Tensor x, DataType dtype=DataType::UNDEFINED, Place place={})
...@@ -207,6 +209,7 @@ ...@@ -207,6 +209,7 @@
func : ElementwiseInferMeta func : ElementwiseInferMeta
kernel : kernel :
func : subtract func : subtract
backward : subtract_grad
- api : sum - api : sum
args : (Tensor x, int64[] axis={}, DataType dtype=DataType::UNDEFINED, bool keep_dim=false) args : (Tensor x, int64[] axis={}, DataType dtype=DataType::UNDEFINED, bool keep_dim=false)
...@@ -277,7 +280,6 @@ ...@@ -277,7 +280,6 @@
func : diagonal func : diagonal
backward : diagonal_grad backward : diagonal_grad
- api : gumbel_softmax - api : gumbel_softmax
args : (Tensor x, float temperature, bool hard, int axis) args : (Tensor x, float temperature, bool hard, int axis)
output : Tensor output : Tensor
...@@ -368,7 +370,7 @@ ...@@ -368,7 +370,7 @@
func : AdamaxInferMeta func : AdamaxInferMeta
kernel : kernel :
func : adamax func : adamax
- api : where - api : where
...@@ -424,7 +426,7 @@ ...@@ -424,7 +426,7 @@
func : CompareInferMeta func : CompareInferMeta
kernel : kernel :
func : equal func : equal
- api : not_equal - api : not_equal
args : (Tensor x, Tensor y, int axis = -1) args : (Tensor x, Tensor y, int axis = -1)
output : Tensor output : Tensor
......
...@@ -25,10 +25,9 @@ ...@@ -25,10 +25,9 @@
output : Tensor(x_grad) output : Tensor(x_grad)
invoke : scale(out_grad, scale, bias, bias_after_scale) invoke : scale(out_grad, scale, bias, bias_after_scale)
- backward_api : add_grad - backward_api : add_grad
forward : add (Tensor x, Tensor y) -> Tensor(out) 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) output : Tensor(x_grad), Tensor(y_grad)
infer_meta : infer_meta :
func : GeneralBinaryGradInferMeta func : GeneralBinaryGradInferMeta
...@@ -36,6 +35,37 @@ ...@@ -36,6 +35,37 @@
kernel : kernel :
func : add_grad 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 - backward_api : digamma_grad
forward : digamma (Tensor x) -> Tensor(out) forward : digamma (Tensor x) -> Tensor(out)
args : (Tensor x, Tensor out_grad) args : (Tensor x, Tensor out_grad)
...@@ -78,7 +108,7 @@ ...@@ -78,7 +108,7 @@
# - backward_api : norm_grad # - backward_api : norm_grad
# forward : norm (Tensor x, int axis, float epsilon, bool is_test) -> Tensor(out), Tensor(norm) # 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) # output : Tensor(x_grad)
# infer_meta : # infer_meta :
# func : UnchangedInferMeta # func : UnchangedInferMeta
...@@ -121,7 +151,7 @@ ...@@ -121,7 +151,7 @@
# param : [out, out_grad, axis] # param : [out, out_grad, axis]
# kernel : # kernel :
# func : gumbel_softmax_grad # func : gumbel_softmax_grad
- backward_api : transpose_grad - backward_api : transpose_grad
forward : transpose (Tensor x, int[] axis) -> Tensor(out) forward : transpose (Tensor x, int[] axis) -> Tensor(out)
...@@ -132,7 +162,7 @@ ...@@ -132,7 +162,7 @@
param : [out_grad, axis] param : [out_grad, axis]
kernel : kernel :
func : transpose_grad func : transpose_grad
# - backward_api : lerp_grad # - backward_api : lerp_grad
# forward : transpose (Tensor x, Tensor y, Tensor weight) -> Tensor(out) # forward : transpose (Tensor x, Tensor y, Tensor weight) -> Tensor(out)
# args : (Tensor x, Tensor y, Tensor weight, Tensor out, Tensor out_grad) # args : (Tensor x, Tensor y, Tensor weight, Tensor out, Tensor out_grad)
...@@ -243,7 +273,7 @@ ...@@ -243,7 +273,7 @@
param : [input] param : [input]
kernel : kernel :
func : bce_loss_grad func : bce_loss_grad
# - backward_api : dist_grad # - backward_api : dist_grad
# forward : dist (Tensor x, Tensor y, float p) -> Tensor(out) # forward : dist (Tensor x, Tensor y, float p) -> Tensor(out)
...@@ -266,7 +296,7 @@ ...@@ -266,7 +296,7 @@
param : [x] param : [x]
kernel : kernel :
func : gather_nd_grad func : gather_nd_grad
- backward_api : mv_grad - backward_api : mv_grad
forward : mv (Tensor x, Tensor vec) -> Tensor(out) forward : mv (Tensor x, Tensor vec) -> Tensor(out)
args : (Tensor x, Tensor vec, Tensor out_grad) args : (Tensor x, Tensor vec, Tensor out_grad)
......
...@@ -43,6 +43,8 @@ PD_REGISTER_INFER_META_FN({api.kernel['func'][0]}, phi::{api.infer_meta['func']} ...@@ -43,6 +43,8 @@ PD_REGISTER_INFER_META_FN({api.kernel['func'][0]}, phi::{api.infer_meta['func']}
'const std::vector<Tensor>&': 'const std::vector<MetaTensor>&', 'const std::vector<Tensor>&': 'const std::vector<MetaTensor>&',
'Tensor': 'MetaTensor*', 'Tensor': 'MetaTensor*',
'std::vector<Tensor>': 'std::vector<MetaTensor>*', 'std::vector<Tensor>': 'std::vector<MetaTensor>*',
'const paddle::optional<Tensor&>':
'const paddle::optional<MetaTensor&>'
} }
wrapped_infermeta_name = get_wrapped_infermeta_name(api.api) wrapped_infermeta_name = get_wrapped_infermeta_name(api.api)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册