Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,11 @@ def ParsePythonAPIInfo(self):
if 'name' in python_api_info.keys():
self.python_api_names = python_api_info['name']
if 'args_alias' in python_api_info.keys():
for arg, alias in python_api_info['args_alias'].items():
alias_set = set(alias)
for arg, alias_or_mode in python_api_info['args_alias'].items():
if arg == 'use_default_mapping':
args_alias.update({arg: alias_or_mode})
continue
alias_set = set(alias_or_mode)
# Add the original argument name to the alias set
alias_set.add(arg)
# Convert to C++ vector format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ def {func_name}():
"""

METHOD_TEMPLATE = """
def _{name}(self,*args, **kwargs):
return _C_ops.{name}(self,*args, **kwargs)
def _{name}(*args, **kwargs):
return _C_ops.{name}(*args, **kwargs)
"""
SET_METHOD_TEMPLATE = """
# set methods for Tensor in dygraph
local_tensor = core.eager.Tensor
for method_name, method in methods_map:
setattr(local_tensor, method_name, method)
setattr(paddle, method_name, method)

"""

Expand Down
34 changes: 28 additions & 6 deletions paddle/fluid/eager/auto_code_generator/generator/python_c_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
IsVectorTensorType,
)

args_default_mapping = {
"x": ["input"],
"y": ["other"],
"axis": ["dim"],
"keepdims": ["keepdim"],
}

#########################
# Global Configurations #
#########################
Expand Down Expand Up @@ -389,6 +396,25 @@ def GeneratePythonCFunction(self, no_input_out_tensor=False):
get_eager_tensor_str = ""
input_names = ""
input_single_tensor_names = ""

def _get_keywords(name, alias_map):
keywords = f'{{"{name}"}}'
if name in args_alias_map.keys():
keywords = args_alias_map[name]
elif (
'use_default_mapping' in args_alias_map.keys()
and args_alias_map['use_default_mapping']
):
# try to use default mapping
if name in args_default_mapping.keys():
alias_set = set(args_default_mapping[name])
alias_set.add(name)
# Convert to C++ vector format
keywords = (
"{" + ",".join(f'"{name}"' for name in alias_set) + "}"
)
return keywords

for name, (ttype, pos) in forward_inputs_position_map.items():
input_names = input_names + ", " + name
if forward_inplace_map and name in forward_inplace_map.keys():
Expand Down Expand Up @@ -445,9 +471,7 @@ def GeneratePythonCFunction(self, no_input_out_tensor=False):
)
)
else:
keywords = f'{{"{name}"}}'
if name in args_alias_map.keys():
keywords = args_alias_map[name]
keywords = _get_keywords(name, args_alias_map)
get_eager_tensor_str += PARSE_PYTHON_C_TENSORS_FROM_ARGS_OR_KWARGS_TEMPLATE.format(
name,
forward_api_name,
Expand Down Expand Up @@ -525,9 +549,7 @@ def GeneratePythonCFunction(self, no_input_out_tensor=False):
name == "place"
), "Only support 'place' as template argument name in FUNCTION_SET_DEVICE_TEMPLATE."
if need_parse_python_api_args:
keywords = f'{{"{name}"}}'
if name in args_alias_map.keys():
keywords = args_alias_map[name]
keywords = _get_keywords(name, args_alias_map)
if default_value is None:
parse_attributes_str += (
PARSE_PYTHON_C_ARGS_KWARGS_TEMPLATE.format(
Expand Down
25 changes: 18 additions & 7 deletions paddle/fluid/pir/dialect/op_generator/python_c_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
CodeGen,
)

args_default_mapping = {
"x": ["input"],
"y": ["other"],
"axis": ["dim"],
"keepdims": ["keepdim"],
}
H_FILE_TEMPLATE = """

#pragma once
Expand Down Expand Up @@ -304,15 +310,20 @@ def _gen_h_file(self, op_info_items, namespaces, h_file_path):
f.write(H_FILE_TEMPLATE.format(body=body))

def _gen_keywords_vector(self, args_alias_map, arg_name):
alias_vector = f'{{"{arg_name}"}}'
alias_set = set()
if arg_name in args_alias_map.keys():
alias_set = set(args_alias_map[arg_name])
# Add the original argument name to the alias set
alias_set.add(arg_name)
# Convert to C++ vector format
alias_vector = (
"{" + ",".join(f'"{name}"' for name in alias_set) + "}"
)
elif (
"use_default_mapping" in args_alias_map.keys()
and args_alias_map['use_default_mapping']
):
# try to use default mapping
if arg_name in args_default_mapping.keys():
alias_set = set(args_default_mapping[arg_name])
# Add the original argument name to the alias set
alias_set.add(arg_name)
# Convert to C++ vector format
alias_vector = "{" + ",".join(f'"{name}"' for name in alias_set) + "}"
return alias_vector

def _gen_inputs(self, op_info, op_name, args_alias_map={}):
Expand Down
32 changes: 28 additions & 4 deletions paddle/fluid/pybind/eager_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1381,22 +1381,46 @@ PyObject* eager__is_run_in_backward(PyObject* self,
PyObject* eager__add_doc_str(PyObject* self, PyObject* args) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只是好奇,这段代码下沉的必要是什么?使用 Python 直接编写清晰得多,而且这里应该不涉及计算逻辑,应该没什么加速效果

另一方面,这段逻辑只在 import paddle 时候执行一次,不会造成运行时开销

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码本来计划是为C++ 编译出的builtin类型的function添加signature和doc的,但是C++ builtin类型的function加signature有问题,暂时保留了C++接口。

EAGER_TRY
static std::vector<std::string> all_docs;
PyObject* obj = nullptr;
PyObject* func_obj = nullptr;
PyObject* doc_obj = nullptr;
if (!PyArg_ParseTuple(args, "OO", &obj, &doc_obj)) {
PyObject* sig_obj = nullptr;
PyObject* annotatio_obj = nullptr;
if (!PyArg_ParseTuple(
args, "OOOO", &func_obj, &doc_obj, &sig_obj, &annotatio_obj)) {
return nullptr;
}
if (PyDict_Check(annotatio_obj) == false) {
PADDLE_THROW(common::errors::InvalidArgument(
"The 4th arg which be used to init __annotations__ must be dict in "
"python!"));
return nullptr;
}
std::string doc_string = CastPyArg2AttrString(doc_obj, 1);

if (Py_TYPE(obj) == &PyCFunction_Type) {
PyCFunctionObject* f = reinterpret_cast<PyCFunctionObject*>(obj);
if (Py_TYPE(func_obj) == &PyCFunction_Type) {
PyCFunctionObject* f = reinterpret_cast<PyCFunctionObject*>(func_obj);
if (f->m_ml->ml_doc) {
VLOG(6)
<< "eager__add_doc_str will update doc for PyCFunction, original doc "
<< f->m_ml->ml_doc;
}
all_docs.emplace_back(doc_string);
f->m_ml->ml_doc = all_docs.back().c_str();
if (func_obj->ob_type->tp_dict == nullptr) {
func_obj->ob_type->tp_dict = PyDict_New();
}
// if (PyDict_SetItemString(
// func_obj->ob_type->tp_dict, "__text_signature__", sig_obj) < 0) {
// VLOG(6) << "eager__add_doc_str add __text_signature__ failed";
// return nullptr;
// }
// Py_INCREF(sig_obj);
if (PyDict_SetItemString(
func_obj->ob_type->tp_dict, "__annotations__", annotatio_obj) < 0) {
VLOG(6) << "eager__add_doc_str add __annotations__ failed";
return nullptr;
}
Py_INCREF(annotatio_obj);
}
RETURN_PY_NONE
EAGER_CATCH_AND_THROW_RETURN_NULL
Expand Down
6 changes: 2 additions & 4 deletions paddle/phi/ops/yaml/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@
python_api :
name : [paddle.amax,paddle.Tensor.amax]
args_alias:
x : [input,x1]
axis : [dim]
use_default_mapping : True
output : Tensor(out)
infer_meta :
func : ReduceInferMeta
Expand All @@ -257,8 +256,7 @@
python_api :
name : [paddle.amin,paddle.Tensor.amin]
args_alias :
x : [input,x1]
axis : [dim]
use_default_mapping : True
output : Tensor(out)
infer_meta :
func : ReduceInferMeta
Expand Down
3 changes: 2 additions & 1 deletion python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
monkey_patch_value()
monkey_patch_program()
monkey_patch_dtype()
monkey_patch_generated_methods_for_tensor()

monkey_patch_generated_methods_for_value()

from .base.dataset import * # noqa: F403
Expand Down Expand Up @@ -1266,6 +1266,7 @@
]
import os

monkey_patch_generated_methods_for_tensor()
import paddle._paddle_docs

FLAGS_trace_api = os.environ.get("FLAGS_trace_api", None)
Expand Down
Loading
Loading