add size op (#17412)
parent
6e0df3102e
commit
67b48d7fe7
@ -0,0 +1,57 @@
|
||||
/* Copyright (c) 2019 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/size_op.h"
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class SizeOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("Input"),
|
||||
"Input (Input) of Size op should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
||||
"Output (Out) of Size op should not be null.");
|
||||
ctx->SetOutputDim("Out", {1});
|
||||
}
|
||||
};
|
||||
|
||||
class SizeOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
void Make() override {
|
||||
AddInput("Input", "The input tensor.");
|
||||
AddOutput("Out",
|
||||
"The returned tensor, the data type "
|
||||
"is int64_t, will be on the same device with the input Tensor.");
|
||||
AddComment(R"DOC(
|
||||
Size Operator.
|
||||
|
||||
Return the number of elements in the input.
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OPERATOR(size, ops::SizeOp, ops::SizeOpMaker,
|
||||
paddle::framework::EmptyGradOpMaker);
|
||||
REGISTER_OP_CPU_KERNEL(size, ops::SizeKernel<int>, ops::SizeKernel<int32_t>,
|
||||
ops::SizeKernel<float>, ops::SizeKernel<double>,
|
||||
ops::SizeKernel<bool>);
|
@ -0,0 +1,21 @@
|
||||
/* Copyright (c) 2019 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/size_op.h"
|
||||
|
||||
REGISTER_OP_CUDA_KERNEL(size, paddle::operators::SizeKernel<int>,
|
||||
paddle::operators::SizeKernel<int32_t>,
|
||||
paddle::operators::SizeKernel<float>,
|
||||
paddle::operators::SizeKernel<bool>,
|
||||
paddle::operators::SizeKernel<double>);
|
@ -0,0 +1,34 @@
|
||||
/* Copyright (c) 2019 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 "paddle/fluid/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
|
||||
template <typename T>
|
||||
class SizeKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override {
|
||||
auto* in_t = ctx.Input<Tensor>("Input");
|
||||
auto* out_t = ctx.Output<Tensor>("Out");
|
||||
auto out_data = out_t->mutable_data<int64_t>(platform::CPUPlace());
|
||||
out_data[0] = in_t->numel();
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,57 @@
|
||||
# Copyright (c) 2019 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.
|
||||
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestSizeOp(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "size"
|
||||
self.shape = []
|
||||
self.config()
|
||||
input = np.zeros(self.shape, dtype='bool')
|
||||
self.inputs = {'Input': input}
|
||||
self.outputs = {'Out': np.array([np.size(input)], dtype='int64')}
|
||||
|
||||
def config(self):
|
||||
pass
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
|
||||
class TestRank1Tensor(TestSizeOp):
|
||||
def config(self):
|
||||
self.shape = [2]
|
||||
|
||||
|
||||
class TestRank2Tensor(TestSizeOp):
|
||||
def config(self):
|
||||
self.shape = [2, 3]
|
||||
|
||||
|
||||
class TestRank3Tensor(TestSizeOp):
|
||||
def config(self):
|
||||
self.shape = [2, 3, 100]
|
||||
|
||||
|
||||
class TestLargeTensor(TestSizeOp):
|
||||
def config(self):
|
||||
self.shape = [2**10]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in new issue