-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Skeleton Of fully connected operator #2945
Conversation
* Fc operator is a grouped operator, which combined by may internal operators. * InferShape & Run a FC operator in Python.
66bf6a3
to
d3373c5
Compare
@@ -19,7 +19,10 @@ | |||
namespace paddle { | |||
namespace framework { | |||
|
|||
void PlainNet::CompleteAddOp() { | |||
void PlainNet::CompleteAddOp(bool calc) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the meaning of cacl
} // namespace operators | ||
} // namespace paddle | ||
|
||
USE_OP(mul); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if USE_OP(mul)
is called in another file, the compile will fail with redefinition
, so i suggest we add USE_OP in a common place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
请教之后得知,因为 USE_OP 会在当前文件生成一个static变量,而static是private的,在不同文件中彼此不会冲突。所以不存在问题。
scope.create_var(out).get_tensor() | ||
|
||
tensor = scope.get_var("Y").get_tensor() | ||
op.infer_shape(scope) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we decide to do infer_shape in Python, or may change in the future?
auto& protos = pd::OpRegistry::protos(); | ||
std::vector<std::string> ret_values; | ||
std::vector<py::bytes> ret_values; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看起来这里碰到坑了,能记录一下么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyBind 11的一些使用问题
PyBind11是一个非常简单的库。这个库的使用问题记录在如下几个方面。
内存问题
Python使用GC和引用技术并存的内存问题。C++是手动管理内存。这二者之间的内存交换就有一些问题了。
对于简单类型还比较简单,直接返回值类型即可。但是对于复杂类型,就分为三种情况。
- 由C++端负责申请,释放内存。Python只是读取这段内存。
- 需要在注册函数的时候,添加
py::return_value_policy::reference
- 需要在注册函数的时候,添加
- 由Python端负责申请,释放内存。
- pybind直接注册一个
T* func()
的函数即可,返回的对象直接使用new
即可。
- pybind直接注册一个
- 由C++与Python共同申请,管理内存。
- 返回
std::shared_ptr
即可。
- 返回
字符串问题
C++中std::string
不仅仅是一个字符串类型,还经常在C++端表示一段二进制内存的buffer。
而对应的Python对象有三个bytes
, str
和unicode
。所以,在使用PyBind的时候,需要根据std::string
的使用场景,分别使用其他类型去接受Python参数或者返回参数给Python.
C++ | Python |
---|---|
std::string表示二进制buffer | py::bytes |
std::string表示字符串 | py::str 或者 std::string |
std::string表示unicide字符串 | std::string |
C++ 端返回std::string
会在C++端转换成 unicode
,而C++端接受参数std::string
,python端可以接受str
和unicode
类型。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@@ -41,7 +40,7 @@ namespace framework { | |||
class Net : public OperatorBase { | |||
public: | |||
virtual void AddOp(const OperatorPtr& op) = 0; | |||
virtual void CompleteAddOp() = 0; | |||
virtual void CompleteAddOp(bool calc) = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declared parameter should be same with the implement.
operators.