slice_sig.cc 6.6 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 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 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
// 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")) {
    return KernelSignature("unregistered", {}, {}, {});
  }

  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")) {
    return KernelSignature("unregistered", {}, {}, {});
  }

  if (ctx.HasInput("StartsTensor")) {
    if (ctx.HasInput("EndsTensor")) {
      return KernelSignature("slice_grad",
108
                             {"Input", "Out@GRAD"},
H
hong 已提交
109 110 111 112 113
                             {"axes",
                              "StartsTensor",
                              "EndsTensor",
                              "infer_flags",
                              "decrease_axis"},
114
                             {"Input@GRAD"});
H
hong 已提交
115 116
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      return KernelSignature("slice_grad",
117
                             {"Input", "Out@GRAD"},
H
hong 已提交
118 119 120 121 122
                             {"axes",
                              "StartsTensor",
                              "EndsTensorList",
                              "infer_flags",
                              "decrease_axis"},
123
                             {"Input@GRAD"});
H
hong 已提交
124 125 126
    } else {
      return KernelSignature(
          "slice_grad",
127
          {"Input", "Out@GRAD"},
H
hong 已提交
128
          {"axes", "StartsTensor", "ends", "infer_flags", "decrease_axis"},
129
          {"Input@GRAD"});
H
hong 已提交
130 131 132 133
    }
  } else if (ctx.InputSize("StartsTensorList") > 0) {
    if (ctx.HasInput("EndsTensor")) {
      return KernelSignature("slice_grad",
134
                             {"Input", "Out@GRAD"},
H
hong 已提交
135 136 137 138 139
                             {"axes",
                              "StartsTensorList",
                              "EndsTensor",
                              "infer_flags",
                              "decrease_axis"},
140
                             {"Input@GRAD"});
H
hong 已提交
141 142
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      return KernelSignature("slice_grad",
143
                             {"Input", "Out@GRAD"},
H
hong 已提交
144 145 146 147 148
                             {"axes",
                              "StartsTensorList",
                              "EndsTensorList",
                              "infer_flags",
                              "decrease_axis"},
149
                             {"Input@GRAD"});
H
hong 已提交
150 151 152
    } else {
      return KernelSignature(
          "slice_grad",
153
          {"Input", "Out@GRAD"},
H
hong 已提交
154
          {"axes", "StartsTensorList", "ends", "infer_flags", "decrease_axis"},
155
          {"Input@GRAD"});
H
hong 已提交
156 157 158 159 160
    }
  } else {
    if (ctx.HasInput("EndsTensor")) {
      return KernelSignature(
          "slice_grad",
161
          {"Input", "Out@GRAD"},
H
hong 已提交
162
          {"axes", "starts", "EndsTensor", "infer_flags", "decrease_axis"},
163
          {"Input@GRAD"});
H
hong 已提交
164 165 166
    } else if (ctx.InputSize("EndsTensorList") > 0) {
      return KernelSignature(
          "slice_grad",
167
          {"Input", "Out@GRAD"},
H
hong 已提交
168
          {"axes", "starts", "EndsTensorList", "infer_flags", "decrease_axis"},
169
          {"Input@GRAD"});
H
hong 已提交
170 171 172
    } else {
      return KernelSignature(
          "slice_grad",
173
          {"Input", "Out@GRAD"},
H
hong 已提交
174
          {"axes", "starts", "ends", "infer_flags", "decrease_axis"},
175
          {"Input@GRAD"});
H
hong 已提交
176 177 178 179 180 181 182 183
    }
  }
}

}  // namespace phi

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