未验证 提交 bd30b9e2 编写于 作者: H Houjiang Chen 提交者: GitHub

Remove python function wrapper. (#6012)

* Remove python function wrapper.

* Increase ref

* Fix doc test

* Update python/oneflow/framework/docstr/utils.py
Co-authored-by: NYao Chi <later@usopp.net>
Co-authored-by: Noneflow-ci-bot <69100618+oneflow-ci-bot@users.noreply.github.com>
Co-authored-by: NYao Chi <later@usopp.net>
上级 69caea81
"""
/*
Copyright 2020 The OneFlow Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
......@@ -12,34 +12,34 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import oneflow as flow
import oneflow._oneflow_internal
class Function:
def __init__(self, func_name, handle):
self.func_name = func_name
self.handle = handle
def __call__(self, *args, **kwargs):
return self.handle(*args, **kwargs)
def RegisterFunctionalApis():
import inspect
import oneflow.F
for s in dir(oneflow._oneflow_internal.F):
f = getattr(oneflow._oneflow_internal.F, s)
if inspect.isbuiltin(f):
func_name = s
if s in _function_name_aliases:
func_name = _function_name_aliases[s]
setattr(oneflow.F, func_name, Function(func_name, f))
setattr(oneflow.F, s, Function(func_name, f))
del inspect
_function_name_aliases = {"add_scalar": "scalar_add"}
*/
#include <pybind11/pybind11.h>
#include "oneflow/api/python/of_api_registry.h"
#include "oneflow/api/python/framework/throw.h"
namespace py = pybind11;
namespace oneflow {
py::object AddFunctionDoc(py::object f, const std::string& doc_string) {
static std::vector<std::string> all_doc_strings;
all_doc_strings.emplace_back(doc_string);
const char* doc_str = all_doc_strings.back().c_str();
PyObject* obj = f.ptr();
if (PyCFunction_Check(obj)) {
auto* f = (PyCFunctionObject*)obj;
if (f->m_ml->ml_doc) {
THROW(RuntimeError) << "function " << f->m_ml->ml_name << " already has a docstring.";
}
f->m_ml->ml_doc = doc_str;
} else {
THROW(RuntimeError) << "function is " << Py_TYPE(obj)->tp_name << ", not a valid PyCFunction.";
}
f.inc_ref();
return f;
}
} // namespace oneflow
ONEFLOW_API_PYBIND11_MODULE("", m) { m.def("add_doc", &oneflow::AddFunctionDoc); }
"""
Copyright 2020 The OneFlow Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from oneflow._oneflow_internal.F import *
......@@ -141,12 +141,10 @@ del hook
del ExitHook
del atexit
del oneflow
import oneflow.F
import oneflow.framework.docstr as docstr
from oneflow.framework.docstr.utils import register_docstr
register_docstr()
del register_docstr
del docstr
from oneflow.autograd import grad_enable, no_grad, inference_mode, is_grad_enabled
import oneflow.nn.image
import oneflow.nn.modules.acosh
......
......@@ -13,13 +13,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
_function_docstr = {}
def add_docstr(fun, docstr: str):
_function_docstr[fun] = docstr
import oneflow._oneflow_internal
def register_docstr():
for (fun, docstr) in _function_docstr.items():
setattr(fun, "__doc__", docstr)
def add_docstr(fun, docstr: str):
return oneflow._oneflow_internal.add_doc(fun, docstr)
......@@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
"""
import oneflow._oneflow_internal
import oneflow.framework.functional as functional
import oneflow.framework.generator as generator
import oneflow.framework.op_expr_util as op_expr_util
import oneflow.framework.tensor as tensor_util
......@@ -23,4 +22,3 @@ import oneflow.framework.tensor as tensor_util
def RegisterMethod4Class():
tensor_util.RegisterMethods()
op_expr_util.RegisterMethod4UserOpExpr()
functional.RegisterFunctionalApis()
......@@ -22,11 +22,6 @@ from test_util import GenArgList
import oneflow as flow
import oneflow.unittest
from oneflow.framework.functional import Function
def _is_oneflow_functional(object):
return isinstance(object, Function)
def _run_functional_doctest(
......@@ -44,7 +39,7 @@ def _run_functional_doctest(
runner = doctest.DebugRunner(verbose=verbose, optionflags=optionflags)
else:
runner = doctest.DocTestRunner(verbose=verbose, optionflags=optionflags)
r = inspect.getmembers(flow.F, _is_oneflow_functional)
r = inspect.getmembers(flow.F)
for (name, fun) in r:
if fun.__doc__ is not None:
print("test on docstr of: ", ".".join([module.__name__, name]))
......
......@@ -121,7 +121,11 @@ namespace functional {{
namespace functional = one::functional;
ONEFLOW_API_PYBIND11_MODULE("F", m) {{
py::options options;
options.disable_function_signatures();
{1}
options.enable_function_signatures();
}}
}} // namespace oneflow
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册