未验证 提交 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,
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<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};"
outputs_autograd_meta_list.append(output_autograd_meta)
......@@ -808,8 +809,15 @@ def GenerateNodeCreationCodes(
# SetAttributes
set_attributes_list = []
for name, _, _, _ in backward_attrs_list:
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()
......
......@@ -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 = ""
......
......@@ -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<int> value = CastPyArg2Ints(obj, op_type, arg_pos);
return paddle::experimental::ScalarArray(value);
......
......@@ -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)
......
......@@ -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)
......
......@@ -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
......
......@@ -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
......
......@@ -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>&',
'Tensor': 'MetaTensor*',
'std::vector<Tensor>': 'std::vector<MetaTensor>*',
'const paddle::optional<Tensor&>':
'const paddle::optional<MetaTensor&>'
}
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.
先完成此消息的编辑!
想要评论请 注册