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

Added has_data_op #10513

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions paddle/fluid/operators/has_data_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

2016 --> 2018


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. */

#include "paddle/fluid/operators/has_data_op.h"
#include <string>
Copy link
Contributor

Choose a reason for hiding this comment

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

exchange line15 with line16

#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace operators {

class HasDataOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
// inputs and outputs stored in proto
AddInput("X", "(LoDTensor) the LoDTensor to check");
AddOutput("Out", "(LoDTensor) the ouput of has_data_op");
Copy link
Contributor

Choose a reason for hiding this comment

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

ouput --> output
has_data_op --> HasDataOp
Please add a dot at end of each sentence.

AddComment(R"DOC(
Has Data Operator.

This operator tests whether the input tensor has data or not.
Out is a boolean scalar.
)DOC");
}
};

class HasDataOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

protected:
void InferShape(framework::InferShapeContext *ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"),
"Input(X) of HasDataOp should not be null.");
PADDLE_ENFORCE(ctx->HasOutput("Out"),
"Output(Out) of HasDataOp should not be null.");
ctx->SetOutputDim("Out", {1});
ctx->ShareLoD("X", "Out");
Copy link
Contributor

Choose a reason for hiding this comment

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

LoD of Out shouldn't inherit from X, in fact, Out has no LoD.

}

framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
framework::OpKernelType kt = framework::OpKernelType(
framework::ToDataType(ctx.Input<framework::LoDTensor>("X")->type()),
platform::CPUPlace());
return kt;
}
};

} // namespace operators
} // namespace paddle

namespace ops = paddle::operators;

REGISTER_OPERATOR(has_data, ops::HasDataOp, ops::HasDataOpMaker);
REGISTER_OP_CPU_KERNEL(
has_data, ops::HasDataOpKernel<paddle::platform::CPUDeviceContext, float>,
ops::HasDataOpKernel<paddle::platform::CPUDeviceContext, double>,
ops::HasDataOpKernel<paddle::platform::CPUDeviceContext, int>,
ops::HasDataOpKernel<paddle::platform::CPUDeviceContext, int64_t>);
42 changes: 42 additions & 0 deletions paddle/fluid/operators/has_data_op.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Copyright (c) 2016 PaddlePaddle 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. */

#pragma once
#include <math.h>
#include <type_traits>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/transform.h"

namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class HasDataOpKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* input = context.Input<framework::LoDTensor>("X");
auto* output = context.Output<framework::LoDTensor>("Out");
size_t mem_size = input->memory_size();

auto* output_data = output->mutable_data<bool>(platform::CPUPlace());
if (mem_size > 0) {
output_data[0] = true;
} else {
output_data[0] = false;
}
}
};

} // namespace operators
} // namespace paddle
29 changes: 29 additions & 0 deletions python/paddle/fluid/layers/control_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'reorder_lod_tensor_by_rank',
'ParallelDo',
'Print',
'has_data',
]


Expand Down Expand Up @@ -1562,3 +1563,31 @@ def reorder_lod_tensor_by_rank(x, rank_table):
'RankTable': [rank_table]},
outputs={'Out': [out]})
return out


def has_data(x, cond=None, **ignored):
"""
**Less than**

This layer returns the truth value of whether the variable contains data.

Args:
x(Variable): Operand of *has_data*
cond(Variable|None): Optional output variable to store the result of *has_data*

Returns:
Variable: The tensor variable storing the output of *has_data*.

Examples:
.. code-block:: python

less = fluid.layers.has_data(x=label)
"""
helper = LayerHelper("has_data", **locals())
if cond is None:
cond = helper.create_tmp_variable(dtype='bool')
cond.stop_gradient = True

helper.append_op(
type='has_data', inputs={'X': [x]}, outputs={'Out': [cond]})
return cond
52 changes: 52 additions & 0 deletions python/paddle/fluid/tests/unittests/test_has_data_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (c) 2018 PaddlePaddle 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 paddle.fluid.op import Operator
import paddle.fluid.core as core
import unittest
import numpy as np


class HasDataOpTester(unittest.TestCase):
def setUp(self):
self.scope = core.Scope()
self.scope.var('X')
self.scope.var('Out')
self.place = core.CPUPlace()
x_data = np.array([1])
x_tensor = self.scope.var('X').get_tensor()
x_tensor.set(x_data, self.place)
out_tensor = self.scope.var('Out').get_tensor()

def test_run(self):
op = Operator('has_data', X='X', Out='Out')
op.run(self.scope, self.place)
out_tensor = self.scope.find_var('Out').get_tensor()
print 'output: ', np.array(out_tensor)


class HasDataOpGPUTester(HasDataOpTester):
def setUp(self):
self.scope = core.Scope()
self.scope.var('X')
self.scope.var('Out')
self.place = core.CUDAPlace(0)
x_data = np.array([])
x_tensor = self.scope.var('X').get_tensor()
x_tensor.set(x_data, self.place)
out_tensor = self.scope.var('Out').get_tensor()


if __name__ == '__main__':
unittest.main()