slice_sig.cc 8.1 KB
Newer Older
H
hong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright (c) 2022 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/phi/core/compat/op_utils.h"

namespace phi {

KernelSignature SliceOpArgumentMapping(const ArgumentMappingContext& ctx) {
  // if input is Tensor Array
  if (ctx.IsDenseTensorVectorInput("Input")) {
Z
zyfncg 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    const char* starts_name = "starts";
    if (ctx.HasInput("StartsTensor")) {
      starts_name = "StartsTensor";
    } else if (ctx.InputSize("StartsTensorList") > 0) {
      starts_name = "StartsTensorList";
    }
    const char* ends_name = "ends";
    if (ctx.HasInput("EndsTensor")) {
      ends_name = "EndsTensor";
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      ends_name = "EndsTensorList";
    }

    if (paddle::any_cast<std::vector<int>>(ctx.Attr("decrease_axis")).size() >
        0) {
      return KernelSignature(
          "slice_array_dense", {"Input"}, {starts_name}, {"Out"});
    } else {
      return KernelSignature(
          "slice_array", {"Input"}, {starts_name, ends_name}, {"Out"});
    }
H
hong 已提交
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 78 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 109 110 111 112 113 114 115 116 117 118 119 120 121
  }

  if (ctx.HasInput("StartsTensor")) {
    if (ctx.HasInput("EndsTensor")) {
      return KernelSignature("slice",
                             {"Input"},
                             {"axes",
                              "StartsTensor",
                              "EndsTensor",
                              "infer_flags",
                              "decrease_axis"},
                             {"Out"});
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      return KernelSignature("slice",
                             {"Input"},
                             {"axes",
                              "StartsTensor",
                              "EndsTensorList",
                              "infer_flags",
                              "decrease_axis"},
                             {"Out"});
    } else {
      return KernelSignature(
          "slice",
          {"Input"},
          {"axes", "StartsTensor", "ends", "infer_flags", "decrease_axis"},
          {"Out"});
    }
  } else if (ctx.InputSize("StartsTensorList") > 0) {
    if (ctx.HasInput("EndsTensor")) {
      return KernelSignature("slice",
                             {"Input"},
                             {"axes",
                              "StartsTensorList",
                              "EndsTensor",
                              "infer_flags",
                              "decrease_axis"},
                             {"Out"});
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      return KernelSignature("slice",
                             {"Input"},
                             {"axes",
                              "StartsTensorList",
                              "EndsTensorList",
                              "infer_flags",
                              "decrease_axis"},
                             {"Out"});
    } else {
      return KernelSignature(
          "slice",
          {"Input"},
          {"axes", "StartsTensorList", "ends", "infer_flags", "decrease_axis"},
          {"Out"});
    }
  } else {
    if (ctx.HasInput("EndsTensor")) {
      return KernelSignature(
          "slice",
          {"Input"},
          {"axes", "starts", "EndsTensor", "infer_flags", "decrease_axis"},
          {"Out"});
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      return KernelSignature(
          "slice",
          {"Input"},
          {"axes", "starts", "EndsTensorList", "infer_flags", "decrease_axis"},
          {"Out"});
    } else {
      return KernelSignature(
          "slice",
          {"Input"},
          {"axes", "starts", "ends", "infer_flags", "decrease_axis"},
          {"Out"});
    }
  }
}

KernelSignature SliceGradOpArgumentMapping(const ArgumentMappingContext& ctx) {
  if (ctx.IsDenseTensorVectorInput("Input")) {
Z
zyfncg 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    const char* starts_name = "starts";
    if (ctx.HasInput("StartsTensor")) {
      starts_name = "StartsTensor";
    } else if (ctx.InputSize("StartsTensorList") > 0) {
      starts_name = "StartsTensorList";
    }
    const char* ends_name = "ends";
    if (ctx.HasInput("EndsTensor")) {
      ends_name = "EndsTensor";
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      ends_name = "EndsTensorList";
    }
    if (paddle::any_cast<std::vector<int>>(ctx.Attr("decrease_axis")).size() >
        0) {
      return KernelSignature("slice_array_dense_grad",
                             {"Input", "Out@GRAD"},
                             {starts_name},
                             {"Input@GRAD"});
    } else {
      return KernelSignature("slice_array_grad",
                             {"Input", "Out@GRAD"},
                             {starts_name, ends_name},
                             {"Input@GRAD"});
    }
H
hong 已提交
146 147 148 149 150
  }

  if (ctx.HasInput("StartsTensor")) {
    if (ctx.HasInput("EndsTensor")) {
      return KernelSignature("slice_grad",
151
                             {"Input", "Out@GRAD"},
H
hong 已提交
152 153 154 155 156
                             {"axes",
                              "StartsTensor",
                              "EndsTensor",
                              "infer_flags",
                              "decrease_axis"},
157
                             {"Input@GRAD"});
H
hong 已提交
158 159
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      return KernelSignature("slice_grad",
160
                             {"Input", "Out@GRAD"},
H
hong 已提交
161 162 163 164 165
                             {"axes",
                              "StartsTensor",
                              "EndsTensorList",
                              "infer_flags",
                              "decrease_axis"},
166
                             {"Input@GRAD"});
H
hong 已提交
167 168 169
    } else {
      return KernelSignature(
          "slice_grad",
170
          {"Input", "Out@GRAD"},
H
hong 已提交
171
          {"axes", "StartsTensor", "ends", "infer_flags", "decrease_axis"},
172
          {"Input@GRAD"});
H
hong 已提交
173 174 175 176
    }
  } else if (ctx.InputSize("StartsTensorList") > 0) {
    if (ctx.HasInput("EndsTensor")) {
      return KernelSignature("slice_grad",
177
                             {"Input", "Out@GRAD"},
H
hong 已提交
178 179 180 181 182
                             {"axes",
                              "StartsTensorList",
                              "EndsTensor",
                              "infer_flags",
                              "decrease_axis"},
183
                             {"Input@GRAD"});
H
hong 已提交
184 185
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      return KernelSignature("slice_grad",
186
                             {"Input", "Out@GRAD"},
H
hong 已提交
187 188 189 190 191
                             {"axes",
                              "StartsTensorList",
                              "EndsTensorList",
                              "infer_flags",
                              "decrease_axis"},
192
                             {"Input@GRAD"});
H
hong 已提交
193 194 195
    } else {
      return KernelSignature(
          "slice_grad",
196
          {"Input", "Out@GRAD"},
H
hong 已提交
197
          {"axes", "StartsTensorList", "ends", "infer_flags", "decrease_axis"},
198
          {"Input@GRAD"});
H
hong 已提交
199 200 201 202 203
    }
  } else {
    if (ctx.HasInput("EndsTensor")) {
      return KernelSignature(
          "slice_grad",
204
          {"Input", "Out@GRAD"},
H
hong 已提交
205
          {"axes", "starts", "EndsTensor", "infer_flags", "decrease_axis"},
206
          {"Input@GRAD"});
H
hong 已提交
207 208 209
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      return KernelSignature(
          "slice_grad",
210
          {"Input", "Out@GRAD"},
H
hong 已提交
211
          {"axes", "starts", "EndsTensorList", "infer_flags", "decrease_axis"},
212
          {"Input@GRAD"});
H
hong 已提交
213 214 215
    } else {
      return KernelSignature(
          "slice_grad",
216
          {"Input", "Out@GRAD"},
H
hong 已提交
217
          {"axes", "starts", "ends", "infer_flags", "decrease_axis"},
218
          {"Input@GRAD"});
H
hong 已提交
219 220 221 222 223 224 225 226
    }
  }
}

}  // namespace phi

PD_REGISTER_ARG_MAPPING_FN(slice, phi::SliceOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(slice_grad, phi::SliceGradOpArgumentMapping);