split_op.h 4.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yancey 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

17
#include <chrono>  // NOLINT
18 19
#include <memory>
#include <string>
Y
Yancey 已提交
20
#include <vector>
Y
Yi Wang 已提交
21
#include "paddle/fluid/framework/op_registry.h"
22
#include "paddle/fluid/operators/utils.h"
C
chentianyu03 已提交
23
#include "paddle/pten/kernels/split_kernel.h"
Y
Yancey 已提交
24 25
namespace paddle {
namespace operators {
26 27 28 29 30 31 32 33
static inline std::vector<framework::DDim> UpdateOutsDims(
    const bool is_runtime, const bool each_section_is_known,
    const framework::DDim in_dims, const size_t num, std::vector<int> sections,
    const size_t axis, const int outs_number) {
  std::vector<framework::DDim> outs_dims(outs_number, in_dims);
  int64_t input_axis_dim = in_dims[axis];
  if (num > 0) {
    if (is_runtime || input_axis_dim > 0) {
T
Thunderbrook 已提交
34 35 36 37 38 39 40 41
      PADDLE_ENFORCE_EQ(
          input_axis_dim % num, 0,
          platform::errors::InvalidArgument(
              "The input's size along the split dimension "
              "must be evenly divisible by Attr(num_or_sections). "
              "But received Attr(num_or_sections) "
              "= %d, input(X)'s shape = [%s], Attr(dim) = %d.",
              num, in_dims, axis));
42
      size_t out_axis_dim = input_axis_dim / num;
Y
Yancey 已提交
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      for (auto& out_dim : outs_dims) {
        out_dim[axis] = out_axis_dim;
      }
    } else {
      for (auto& out_dim : outs_dims) {
        out_dim[axis] = -1;
      }
    }
  } else if (sections.size() > 0) {
    if (is_runtime || input_axis_dim > 0) {
      const int unk_dim_val = -1;
      int unk_dim_idx = -1, num_of_unk = 0;
      int sum_of_section = 0;
      for (size_t i = 0; i < sections.size(); ++i) {
        if (sections[i] == unk_dim_val) {
          num_of_unk++;
          unk_dim_idx = i;
        } else {
          sum_of_section += sections[i];
        }
      }

      if (each_section_is_known) {
T
Thunderbrook 已提交
67 68 69 70 71 72 73
        PADDLE_ENFORCE_LE(
            num_of_unk, 1,
            platform::errors::InvalidArgument(
                "Only one dimension value of Attr(num_or_sections) "
                "in SplitOp can be -1. "
                "But received Attr(num_or_sections) = [%s].",
                framework::make_ddim(sections)));
74 75 76 77 78 79 80 81
      }

      if (unk_dim_idx != -1) {
        // for example, input shape = [4 ,5], axis = 1, sections = [2, 3, -1].
        // input_axis_dim = 5, sum_of_sections = 5.
        // the following check will fail.
        PADDLE_ENFORCE_LT(
            sum_of_section, input_axis_dim,
T
Thunderbrook 已提交
82 83 84 85 86 87 88
            platform::errors::InvalidArgument(
                "Sum of Attr(num_or_sections) other than unknown section "
                "must be less than the input's "
                "size "
                "along the split dimension. But received Attr(num_or_sections) "
                "= [%s], input(X)'s shape = [%s], Attr(dim) = %d.",
                framework::make_ddim(sections), in_dims, axis));
89 90 91 92 93 94
        if (each_section_is_known) {
          sections[unk_dim_idx] = input_axis_dim - sum_of_section;
        }
      } else {
        PADDLE_ENFORCE_EQ(
            sum_of_section, input_axis_dim,
T
Thunderbrook 已提交
95 96 97 98 99 100
            platform::errors::InvalidArgument(
                "Sum of Attr(num_or_sections) must be equal to the input's "
                "size "
                "along the split dimension. But received Attr(num_or_sections)"
                " = [%s], input(X)'s shape = [%s], Attr(dim) = %d.",
                framework::make_ddim(sections), in_dims, axis));
101 102
      }
    }
103
    for (int i = 0; i < outs_number; ++i) {
104 105 106 107 108
      outs_dims[i][axis] = sections[i];
    }
  }
  return outs_dims;
}
Y
Yancey 已提交
109

H
hong 已提交
110 111
template <typename T>
class SplitGradMaker : public framework::SingleGradOpMaker<T> {
T
typhoonzero 已提交
112
 public:
H
hong 已提交
113
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
T
typhoonzero 已提交
114 115

 protected:
116
  void Apply(GradOpPtr<T> op) const override {
T
typhoonzero 已提交
117
    op->SetType("concat");
H
hong 已提交
118
    op->SetInput("X", this->OutputGrad("Out"));
H
hong 已提交
119 120 121
    if (this->HasInput("AxisTensor")) {
      op->SetInput("AxisTensor", this->Input("AxisTensor"));
    }
H
hong 已提交
122 123
    op->SetOutput("Out", this->InputGrad("X"));
    op->SetAttrMap(this->Attrs());
T
typhoonzero 已提交
124 125 126
  }
};

Y
Yancey 已提交
127 128
}  // namespace operators
}  // namespace paddle