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

Enhanced is_empty_op for our seq2seq model #10704

Merged
merged 6 commits into from
May 21, 2018

Conversation

ktlichkid
Copy link

Resolved #10512

@ktlichkid ktlichkid requested a review from pkuyym May 16, 2018 10:33
public:
void Compute(const framework::ExecutionContext& context) const override {
// get input
auto* input_tensor = context.Input<framework::LoDTensor>(kInput);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just context.Input<framework::LoDTensor>("X") here.

Copy link
Author

@ktlichkid ktlichkid May 18, 2018

Choose a reason for hiding this comment

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

It is the original design of the op, I'm not sure about the reason either. Maybe due to the requirement of generality? See line 21 of original code is_empty_op.cc

"Input(X) of IsEmptyOp should not be null.");
PADDLE_ENFORCE(ctx->HasOutput("Out"),
"Output(Out) of IsEmptyOp should not be null.");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to set dims for 'Out' here.

Copy link
Author

Choose a reason for hiding this comment

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

SetDim is defined only for LoDTensor and SelectedRows, but this op is for general tensors. Please have a look at

void SetDim(const std::string& name, const DDim& dim) override {

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently, we only use LoDTensor. Normal tensor is LoDTensor without LoD.

Copy link
Author

Choose a reason for hiding this comment

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

The point is that SetDim doesn't work for normal tensors. If you want use SetDim for a normal tensor you have to change the implementation of that function.

// get output
auto* output_tensor = context.Output<framework::LoDTensor>(kOutput);

output_tensor->Resize({1});
Copy link
Contributor

Choose a reason for hiding this comment

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

Please specify the dims of 'Out' in InferShape.

Copy link
Author

Choose a reason for hiding this comment

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

Answered above.

paddle::operators::IsEmptyOpProtoMaker);
namespace ops = paddle::operators;

REGISTER_OPERATOR(is_empty, ops::IsEmptyOp, ops::IsEmptyOpMaker);
Copy link
Contributor

Choose a reason for hiding this comment

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

This operator has no backward logic, so please register the op in this way https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/fluid/operators/accuracy_op.cc#L97

Copy link
Author

Choose a reason for hiding this comment

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

Done. You're definitely correct.


Args:
x(Variable): Operand of *is_empty*
cond(Variable|None): Optional output variable to store the result of *is_empty*
Copy link
Contributor

Choose a reason for hiding this comment

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

Exceed 80 columns here.

Copy link
Author

Choose a reason for hiding this comment

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

Done. Thanks for pointing out!

helper = LayerHelper("is_empty", **locals())
if cond is None:
cond = helper.create_tmp_variable(dtype='bool')
cond.stop_gradient = True
Copy link
Contributor

Choose a reason for hiding this comment

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

Please check the data type in else part.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

// get output
auto* output_tensor = context.Output<framework::LoDTensor>("Out");

output_tensor->Resize({1});
Copy link
Contributor

Choose a reason for hiding this comment

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

This is unnecessary, since the shape is already set in InferShape

cond(Variable|None): Optional output variable to store the result
of *is_empty*

Returns:
Copy link
Contributor

Choose a reason for hiding this comment

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

After this section, we can add Raises section. Please refer to https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/layers/nn.py#L149

Copy link
Author

Choose a reason for hiding this comment

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

Done.

pkuyym
pkuyym previously approved these changes May 18, 2018
Copy link
Contributor

@pkuyym pkuyym left a comment

Choose a reason for hiding this comment

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

LGTM

pkuyym
pkuyym previously approved these changes May 21, 2018
Copy link
Contributor

@pkuyym pkuyym left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@pkuyym pkuyym left a comment

Choose a reason for hiding this comment

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

LGTM

@ktlichkid ktlichkid merged commit 9ff6715 into PaddlePaddle:develop May 21, 2018
@ktlichkid ktlichkid deleted the is-empty-layer branch May 21, 2018 07:27
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.

2 participants