Skip to content
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

Merged
merged 2 commits into from
Jul 19, 2017

Conversation

reyoung
Copy link
Collaborator

@reyoung reyoung commented Jul 18, 2017

  • Fc operator is a grouped operator, which combined by may internal
    operators.
  • InferShape & Run a FC operator in Python.

* Fc operator is a grouped operator, which combined by may internal
  operators.
* InferShape & Run a FC operator in Python.
@@ -19,7 +19,10 @@
namespace paddle {
namespace framework {

void PlainNet::CompleteAddOp() {
void PlainNet::CompleteAddOp(bool calc) {
Copy link
Member

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

@reyoung reyoung mentioned this pull request Jul 18, 2017
} // namespace operators
} // namespace paddle

USE_OP(mul);
Copy link
Member

@jacquesqiao jacquesqiao Jul 19, 2017

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.

Copy link
Member

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)
Copy link
Member

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;
Copy link
Member

Choose a reason for hiding this comment

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

看起来这里碰到坑了,能记录一下么?

Copy link
Collaborator Author

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即可。
  • 由C++与Python共同申请,管理内存。
    • 返回std::shared_ptr即可。

字符串问题

C++中std::string不仅仅是一个字符串类型,还经常在C++端表示一段二进制内存的buffer。

而对应的Python对象有三个bytes, strunicode。所以,在使用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端可以接受strunicode类型。

Copy link
Member

@jacquesqiao jacquesqiao left a comment

Choose a reason for hiding this comment

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

LGTM!

@reyoung reyoung merged commit e8304bd into PaddlePaddle:develop Jul 19, 2017
@@ -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;
Copy link
Contributor

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.

@reyoung reyoung deleted the feature/grouped_ops branch July 19, 2017 08:48
heavengate pushed a commit to heavengate/Paddle that referenced this pull request Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants