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

Rnn make stepnet member #3469

Merged
merged 6 commits into from
Aug 15, 2017

Conversation

Superjomn
Copy link
Contributor

@Superjomn Superjomn commented Aug 14, 2017

fix: #3457

@Superjomn
Copy link
Contributor Author

fix: #3457

static const rnn::ArgumentName kArgName;

private:
RecurrentAlgorithm alg_;
std::shared_ptr<NetOp> stepnet_;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why would we need to define stepnet_ as a std::shared_ptr? Can we define

class RecurrentOp ... {
 private:
  NetOp step_;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In pybind.cc, a std::shared_ptr is passed to RecurrentOp as an argument, so here is a shared pointer too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need shared_ptr in our Python binding?

Copy link
Contributor Author

@Superjomn Superjomn Aug 14, 2017

Choose a reason for hiding this comment

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

just like NetOp, RNNOp has its own python binding to expose some methods, such as set_stepnet.

In pybind.cc, all the operators created by user are handled by shared_ptr, maybe easier to be referenced across different levels, when user create a Operator, a shared_ptr is returned, it is free to use the pointer, or pass the pointer into multiple NetOp (add an op into a net).

Operators will live across the whole execution period, so a shared_ptr seems reasonable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you mean something like here:

py::class_<operators::NetOp, std::shared_ptr<operators::NetOp>> net(m, "Net");

It seem that calling Python new Net() returns a shared_ptr<operator::NetOp> -- however, when will the reference count become 0 and the C++ instance get freed?

@@ -33,7 +34,12 @@ class RecurrentAlgorithm {
void Run(const framework::Scope& scope,
const platform::DeviceContext& dev_ctx) const;

void Init(std::unique_ptr<rnn::Argument> arg) { arg_ = std::move(arg); }
void Init(std::unique_ptr<rnn::Argument> arg,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why unique_ptr here? Could we just do

class RecurrentOp ... {
 private:
  rnn::Argument arg_;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the rnn::Argument is a argument that should be passed to RecurrentAlgorithm, a unique pointer may be lighter to pass, and std::move is used here, finally only RecurrentAlgorithm owns this memory.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't get it. By defining arg_ a data member of RecurrentOp, we can of course call RecurrentAlgorithm(&arg_);, could we?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

get it.

@Superjomn Superjomn merged commit 0079fa3 into PaddlePaddle:develop Aug 15, 2017
@Superjomn Superjomn deleted the rnn-make-stepnet-member branch August 15, 2017 03:01
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.

rnnop make stepnet an member of RNNOP
3 participants