-
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
Added has_data_op #10513
Added has_data_op #10513
Changes from all commits
46a7f39
2de44cc
bc8f059
d0c7494
2d498b1
36e7845
08a1708
38f31b2
13ccca3
a7ae9b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* 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. */ | ||
|
||
#include "paddle/fluid/operators/has_data_op.h" | ||
#include <string> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ouput --> output |
||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LoD of |
||
} | ||
|
||
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>); |
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 |
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() |
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.
2016 --> 2018