static_backend.cc 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2023 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/fluid/primitive/backend/static_backend.h"
16
#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_api.h"
17
#include "paddle/fluid/primitive/primitive/primitive.h"
18
#include "paddle/fluid/primitive/type/lazy_tensor.h"
19 20 21 22 23

namespace paddle {
namespace primitive {
namespace backend {

24
using LazyTensor = paddle::primitive::LazyTensor;
25 26

template <>
27 28
Tensor tanh_grad<LazyTensor>(const Tensor& out, const Tensor& grad_out) {
  ir::OpResult out_res = std::static_pointer_cast<LazyTensor>(out.impl())
29 30 31
                             ->getValue()
                             .dyn_cast<ir::OpResult>();
  ir::OpResult grad_out_res =
32
      std::static_pointer_cast<LazyTensor>(grad_out.impl())
33 34 35 36 37
          ->getValue()
          .dyn_cast<ir::OpResult>();

  ir::OpResult op_res = paddle::dialect::tanh_grad(out_res, grad_out_res);

38
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
39 40 41
}

template <>
42 43 44 45 46 47
Tensor mean_grad<LazyTensor>(const Tensor& x,
                             const Tensor& out_grad,
                             const IntArray& axis,
                             bool keepdim,
                             bool reduce_all) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
48 49 50
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult out_grad_res =
51
      std::static_pointer_cast<LazyTensor>(out_grad.impl())
52 53 54 55
          ->getValue()
          .dyn_cast<ir::OpResult>();

  ir::OpResult op_res = paddle::dialect::mean_grad(
56
      x_res, out_grad_res, axis.GetData(), keepdim, reduce_all);
57

58
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
59 60
}

C
Charles-hit 已提交
61
template <>
62 63
Tensor divide<LazyTensor>(const Tensor& x, const Tensor& y) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
C
Charles-hit 已提交
64 65
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  ir::OpResult y_res = std::static_pointer_cast<LazyTensor>(y.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult op_res = paddle::dialect::divide(x_res, y_res);
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
}

template <>
Tensor add<LazyTensor>(const Tensor& x, const Tensor& y) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult y_res = std::static_pointer_cast<LazyTensor>(y.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult op_res = paddle::dialect::add(x_res, y_res);
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
}

template <>
Tensor multiply<LazyTensor>(const Tensor& x, const Tensor& y) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult y_res = std::static_pointer_cast<LazyTensor>(y.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult op_res = paddle::dialect::multiply(x_res, y_res);
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
}

template <>
Tensor elementwise_pow<LazyTensor>(const Tensor& x, const Tensor& y) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult y_res = std::static_pointer_cast<LazyTensor>(y.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult op_res = paddle::dialect::elementwise_pow(x_res, y_res);
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
}

template <>
Tensor scale<LazyTensor>(const Tensor& x,
                         const Scalar& scale,
                         float bias,
                         bool bias_after_scale) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult op_res =
      paddle::dialect::scale(x_res, scale.to<float>(), bias, bias_after_scale);
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
}

template <>
Tensor sum<LazyTensor>(const Tensor& x,
                       const IntArray& axis,
                       phi::DataType dtype,
                       bool keepdim) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult op_res =
      paddle::dialect::sum(x_res, axis.GetData(), dtype, keepdim);
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
}

template <>
Tensor full<LazyTensor>(const IntArray& shape,
                        const Scalar& value,
                        phi::DataType dtype,
                        phi::Place place) {
  ir::OpResult op_res =
      paddle::dialect::full(shape.GetData(), value.to<float>(), dtype, place);
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
}

template <>
std::tuple<Tensor, Tensor> reshape<LazyTensor>(const Tensor& x,
                                               const IntArray& shape) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  std::tuple<ir::OpResult, ir::OpResult> op_res =
      paddle::dialect::reshape(x_res, shape.GetData());
  return std::make_tuple(
      Tensor(std::make_shared<primitive::LazyTensor>(std::get<0>(op_res))),
      Tensor(std::make_shared<primitive::LazyTensor>(std::get<1>(op_res))));
}

template <>
Tensor expand<LazyTensor>(const Tensor& x, const IntArray& shape) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult op_res = paddle::dialect::expand(x_res, shape.GetData());
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
}

template <>
Tensor tile<LazyTensor>(const Tensor& x, const IntArray& repeat_times) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult op_res = paddle::dialect::tile(x_res, repeat_times.GetData());
  return Tensor(std::make_shared<primitive::LazyTensor>(op_res));
}

template <>
std::tuple<Tensor, Tensor> add_grad<LazyTensor>(const Tensor& x,
                                                const Tensor& y,
                                                const Tensor& out_grad,
                                                int axis) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult y_res = std::static_pointer_cast<LazyTensor>(y.impl())
C
Charles-hit 已提交
185 186 187
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult out_grad_res =
188
      std::static_pointer_cast<LazyTensor>(out_grad.impl())
C
Charles-hit 已提交
189 190 191 192 193 194 195
          ->getValue()
          .dyn_cast<ir::OpResult>();

  std::tuple<ir::OpResult, ir::OpResult> op_res =
      paddle::dialect::add_grad(x_res, y_res, out_grad_res, axis);

  return std::make_tuple(
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 233 234 235 236 237 238 239 240 241 242 243
      Tensor(std::make_shared<primitive::LazyTensor>(std::get<0>(op_res))),
      Tensor(std::make_shared<primitive::LazyTensor>(std::get<1>(op_res))));
}

template <>
std::tuple<Tensor, Tensor> divide_grad<LazyTensor>(const Tensor& x,
                                                   const Tensor& y,
                                                   const Tensor& out,
                                                   const Tensor& out_grad,
                                                   int axis) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult y_res = std::static_pointer_cast<LazyTensor>(y.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult out_res = std::static_pointer_cast<LazyTensor>(out.impl())
                             ->getValue()
                             .dyn_cast<ir::OpResult>();
  ir::OpResult out_grad_res =
      std::static_pointer_cast<LazyTensor>(out_grad.impl())
          ->getValue()
          .dyn_cast<ir::OpResult>();

  std::tuple<ir::OpResult, ir::OpResult> op_res =
      paddle::dialect::divide_grad(x_res, y_res, out_res, out_grad_res, axis);

  return std::make_tuple(
      Tensor(std::make_shared<LazyTensor>(std::get<0>(op_res))),
      Tensor(std::make_shared<LazyTensor>(std::get<1>(op_res))));
}

template <>
Tensor sum_grad<LazyTensor>(const Tensor& x,
                            const Tensor& out_grad,
                            const IntArray& axis,
                            bool keepdim,
                            bool reduce_all) {
  ir::OpResult x_res = std::static_pointer_cast<LazyTensor>(x.impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult out_grad_res =
      std::static_pointer_cast<LazyTensor>(out_grad.impl())
          ->getValue()
          .dyn_cast<ir::OpResult>();
  ir::OpResult op_res = paddle::dialect::sum_grad(
      x_res, out_grad_res, axis.GetData(), keepdim, reduce_all);
  return Tensor(std::make_shared<LazyTensor>(op_res));
C
Charles-hit 已提交
244
}
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

template <>
std::vector<Tensor> concat_grad<LazyTensor>(const std::vector<Tensor>& x,
                                            const Tensor& out_grad,
                                            const Tensor& axis) {
  std::vector<ir::OpResult> x_res;
  for (uint64_t idx = 0; idx < x.size(); idx++) {
    x_res.emplace_back(std::static_pointer_cast<LazyTensor>(x[idx].impl())
                           ->getValue()
                           .dyn_cast<ir::OpResult>());
  }

  ir::OpResult out_grad_res =
      std::static_pointer_cast<LazyTensor>(out_grad.impl())
          ->getValue()
          .dyn_cast<ir::OpResult>();

  ir::OpResult axis_res = std::static_pointer_cast<LazyTensor>(axis.impl())
                              ->getValue()
                              .dyn_cast<ir::OpResult>();

  std::vector<ir::OpResult> op_res =
      paddle::dialect::concat_grad(x_res, out_grad_res, axis_res);

  std::vector<Tensor> op_result;
  for (uint64_t idx = 0; idx < op_res.size(); idx++) {
    op_result.emplace_back(
        std::make_shared<primitive::LazyTensor>(op_res[idx]));
  }
  return op_result;
}

277 278 279
}  // namespace backend
}  // namespace primitive
}  // namespace paddle