custom_concat_op.cc 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
// Copyright (c) 2021 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 <iostream>
#include <vector>
#include "concat_and_split.h"  // NOLINT
#include "paddle/extension.h"

#define CHECK_INPUT(x) \
  PD_CHECK(x.place() == paddle::PlaceType::kCPU, #x " must be a CPU Tensor.")

int64_t ComputeAxis(int64_t axis, int64_t rank) {
  PD_CHECK(axis >= -rank && axis < rank,
           "The axis is excepted to be in range of [",
           -rank,
           ", ",
           rank,
           "].");
  if (axis < 0) {
    axis = axis + rank;
  }
  return axis > 0 ? axis : 0;
}

std::vector<int64_t> ComputeOutShape(
    std::vector<std::vector<int64_t>> in_shapes, int64_t axis) {
  size_t n = in_shapes.size();
  auto out_shape = in_shapes[0];
  size_t zero_dim_size = out_shape.size();
  for (size_t i = 1; i < n; ++i) {
    PD_CHECK(in_shapes[i].size() == out_shape.size(),
             "Input dimension must be same.");
    for (size_t j = 0; j < zero_dim_size; ++j) {
      if (j == axis) {
        out_shape[axis] += in_shapes[i][j];
      } else {
        PD_CHECK(in_shapes[0][j] == in_shapes[i][j],
                 "The ",
                 j,
                 "-th dimension of input must be same.");
      }
    }
  }
  return out_shape;
}

std::vector<paddle::Tensor> ConcatForwardDynamicAxis(
    const std::vector<paddle::Tensor>& inputs, const paddle::Tensor& axis_t) {
  // check inputs
  PD_CHECK(inputs.size() >= 1, "No Tensor need to be concat.");
  for (auto& t : inputs) {
    CHECK_INPUT(t);
  }
  CHECK_INPUT(axis_t);

  // compute output shape
  int64_t rank = static_cast<int64_t>(inputs[0].shape().size());
  int64_t axis = axis_t.data<int64_t>()[0];
  axis = ComputeAxis(axis, rank);
  std::vector<std::vector<int64_t>> in_shapes;
  for (auto& t : inputs) {
    in_shapes.emplace_back(t.shape());
  }
  auto out_shape = ComputeOutShape(in_shapes, axis);

  // create output
78
  auto out = paddle::Tensor(paddle::PlaceType::kCPU, out_shape);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

  // calc
  PD_DISPATCH_FLOATING_AND_INTEGRAL_TYPES(
      inputs[0].type(), "ConcatCpuKernel", ([&] {
        ConcatCpuKernel<data_t>(inputs, &out, axis);
      }));

  return {out};
}

std::vector<paddle::Tensor> ConcatBackwardDynamicAxis(
    const std::vector<paddle::Tensor>& inputs,
    const paddle::Tensor& grad_out,
    const paddle::Tensor& axis_t) {
  // check input
  PD_CHECK(inputs.size() >= 1, "No Tensor need to be concat.");
  for (auto& t : inputs) {
    CHECK_INPUT(t);
  }
  CHECK_INPUT(axis_t);
  CHECK_INPUT(grad_out);

  // compate axis
  int64_t rank = static_cast<int64_t>(inputs[0].shape().size());
  int64_t axis = axis_t.data<int64_t>()[0];
  axis = ComputeAxis(axis, rank);

  // create outputs
  std::vector<paddle::Tensor> grad_inputs;
  for (auto& t : inputs) {
109
    auto grad = paddle::Tensor(paddle::PlaceType::kCPU, t.shape());
110 111 112 113 114 115 116 117 118 119 120 121 122
    grad_inputs.emplace_back(grad);
  }

  // calc
  PD_DISPATCH_FLOATING_AND_INTEGRAL_TYPES(
      grad_out.type(), "SplitCpuKernel", ([&] {
        SplitCpuKernel<data_t>(grad_out, inputs, &grad_inputs, axis);
      }));

  return grad_inputs;
}

std::vector<std::vector<int64_t>> ConcatInferShapeDynamicAxis(
123 124
    const std::vector<std::vector<int64_t>>& input_shapes,
    const std::vector<int64_t>& axis_shape) {
125 126 127 128
  return {std::vector<int64_t>(input_shapes[0].size(), -1)};
}

std::vector<paddle::DataType> ConcatInferDtypeDynamicAxis(
129 130
    const std::vector<paddle::DataType>& input_dtypes,
    const paddle::DataType& axis_dtype) {
131 132 133 134 135 136 137 138 139 140 141 142 143 144
  return {input_dtypes[0]};
}

PD_BUILD_OP(custom_concat)
    .Inputs({paddle::Vec("X"), "Axis"})
    .Outputs({"Out"})
    .SetKernelFn(PD_KERNEL(ConcatForwardDynamicAxis))
    .SetInferShapeFn(PD_INFER_SHAPE(ConcatInferShapeDynamicAxis))
    .SetInferDtypeFn(PD_INFER_DTYPE(ConcatInferDtypeDynamicAxis));

PD_BUILD_GRAD_OP(custom_concat)
    .Inputs({paddle::Vec("X"), paddle::Grad("Out"), "Axis"})
    .Outputs({paddle::Grad(paddle::Vec("X"))})
    .SetKernelFn(PD_KERNEL(ConcatBackwardDynamicAxis));
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

std::vector<paddle::Tensor> ConcatForwardStaticAxis(
    const std::vector<paddle::Tensor>& inputs, const int64_t& axis) {
  // check inputs
  PD_CHECK(inputs.size() >= 1, "No Tensor need to be concat.");
  for (auto& t : inputs) {
    CHECK_INPUT(t);
  }

  // compute output shape
  int64_t rank = static_cast<int64_t>(inputs[0].shape().size());
  auto final_axis = ComputeAxis(axis, rank);
  std::vector<std::vector<int64_t>> in_shapes;
  for (auto& t : inputs) {
    in_shapes.emplace_back(t.shape());
  }
  auto out_shape = ComputeOutShape(in_shapes, final_axis);

  // create output
164
  auto out = paddle::Tensor(paddle::PlaceType::kCPU, out_shape);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

  // calc
  PD_DISPATCH_FLOATING_AND_INTEGRAL_TYPES(
      inputs[0].type(), "ConcatCpuKernel", ([&] {
        ConcatCpuKernel<data_t>(inputs, &out, final_axis);
      }));

  return {out};
}

std::vector<paddle::Tensor> ConcatBackwardStaticAxis(
    const std::vector<paddle::Tensor>& inputs,
    const paddle::Tensor& grad_out,
    const int64_t& axis) {
  // check input
  PD_CHECK(inputs.size() >= 1, "No Tensor need to be concat.");
  for (auto& t : inputs) {
    CHECK_INPUT(t);
  }
  CHECK_INPUT(grad_out);

  // compate axis
  int64_t rank = static_cast<int64_t>(inputs[0].shape().size());
  auto final_axis = ComputeAxis(axis, rank);

  // create outputs
  std::vector<paddle::Tensor> grad_inputs;
  for (auto& t : inputs) {
193
    auto grad = paddle::Tensor(paddle::PlaceType::kCPU, t.shape());
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    grad_inputs.emplace_back(grad);
  }

  // calc
  PD_DISPATCH_FLOATING_AND_INTEGRAL_TYPES(
      grad_out.type(), "SplitCpuKernel", ([&] {
        SplitCpuKernel<data_t>(grad_out, inputs, &grad_inputs, final_axis);
      }));

  return grad_inputs;
}

std::vector<std::vector<int64_t>> ConcatInferShapeStaticAxis(
    const std::vector<std::vector<int64_t>>& input_shapes,
    const int64_t& axis) {
  int64_t rank = static_cast<int64_t>(input_shapes[0].size());
  auto final_axis = ComputeAxis(axis, rank);
  auto out_shape = ComputeOutShape(input_shapes, final_axis);
  return {out_shape};
}

std::vector<paddle::DataType> ConcatInferDtypeStaticAxis(
    const std::vector<paddle::DataType>& input_dtypes) {
  return {input_dtypes[0]};
}

PD_BUILD_OP(custom_concat_with_attr)
    .Inputs({paddle::Vec("X")})
    .Outputs({"Out"})
    .Attrs({"axis: int64_t"})
    .SetKernelFn(PD_KERNEL(ConcatForwardStaticAxis))
    .SetInferShapeFn(PD_INFER_SHAPE(ConcatInferShapeStaticAxis))
    .SetInferDtypeFn(PD_INFER_DTYPE(ConcatInferDtypeStaticAxis));

PD_BUILD_GRAD_OP(custom_concat_with_attr)
    .Inputs({paddle::Vec("X"), paddle::Grad("Out")})
    .Outputs({paddle::Grad(paddle::Vec("X"))})
    .Attrs({"axis: int64_t"})
    .SetKernelFn(PD_KERNEL(ConcatBackwardStaticAxis));