|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/operators/concat_op.h" |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | +using framework::Tensor; |
| 21 | + |
| 22 | +class ConcatOp : public framework::OperatorWithKernel { |
| 23 | + public: |
| 24 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 25 | + |
| 26 | + protected: |
| 27 | + void InferShape(const framework::InferShapeContext &ctx) const override { |
| 28 | + auto ins = ctx.MultiInput<framework::Tensor>("X"); |
| 29 | + auto *out = ctx.Output<framework::Tensor>("Out"); |
| 30 | + size_t axis = static_cast<size_t>(ctx.Attr<int>("axis")); |
| 31 | + size_t n = ins.size(); |
| 32 | + |
| 33 | + PADDLE_ENFORCE_GT(n, 1, "Input tensors count should > 1."); |
| 34 | + |
| 35 | + auto out_dims = ins[0]->dims(); |
| 36 | + size_t in_zero_dims_size = out_dims.size(); |
| 37 | + for (size_t i = 1; i < n; i++) { |
| 38 | + for (size_t j = 0; j < in_zero_dims_size; j++) { |
| 39 | + if (j == axis) { |
| 40 | + out_dims[axis] += ins[i]->dims()[j]; |
| 41 | + continue; |
| 42 | + } |
| 43 | + PADDLE_ENFORCE_EQ(out_dims[j], ins[i]->dims()[j], |
| 44 | + "Input tensors should have the same " |
| 45 | + "elements except the specify axis.") |
| 46 | + } |
| 47 | + } |
| 48 | + out->Resize(out_dims); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +class ConcatOpMaker : public framework::OpProtoAndCheckerMaker { |
| 53 | + public: |
| 54 | + ConcatOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) |
| 55 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 56 | + AddInput("X", "the input tensors of concat operator.").AsDuplicable(); |
| 57 | + AddOutput("Out", "the output tensor of concat operator."); |
| 58 | + AddComment(R"DOC( |
| 59 | + Join the input tensors along with the axis. |
| 60 | + Examples: |
| 61 | + Input[0] = [[1,2],[3,4]] |
| 62 | + Input[1] = [[5,6]] |
| 63 | + axis = 0 |
| 64 | + Output = [[1,2], |
| 65 | + [3,4], |
| 66 | + [5,6]] |
| 67 | + )DOC"); |
| 68 | + AddAttr<int>("axis", "The axis which the inputs will be joined with.") |
| 69 | + .SetDefault(0); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +} // namespace operators |
| 74 | +} // namespace paddle |
| 75 | + |
| 76 | +namespace ops = paddle::operators; |
| 77 | +REGISTER_OP_WITHOUT_GRADIENT(concat, ops::ConcatOp, ops::ConcatOpMaker) |
| 78 | +REGISTER_OP_CPU_KERNEL(concat, |
| 79 | + ops::ConcatKernel<paddle::platform::CPUPlace, float>) |
0 commit comments