static_backend.cc 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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"
#include "paddle/fluid/ir/dialect/pd_api.h"
#include "paddle/fluid/primitive/primitive/primitive.h"
C
Charles-hit 已提交
18
#include "paddle/fluid/primitive/type/static_tensor.h"
19 20 21 22 23 24

namespace paddle {
namespace primitive {
namespace backend {
namespace experimental {

C
Charles-hit 已提交
25
using StaticTensor = paddle::primitive::experimental::StaticTensor;
26 27

template <>
C
Charles-hit 已提交
28 29
Tensor tanh_grad<StaticTensor>(const Tensor& out, const Tensor& grad_out) {
  ir::OpResult out_res = std::static_pointer_cast<StaticTensor>(out.impl())
30 31 32
                             ->getValue()
                             .dyn_cast<ir::OpResult>();
  ir::OpResult grad_out_res =
C
Charles-hit 已提交
33
      std::static_pointer_cast<StaticTensor>(grad_out.impl())
34 35 36 37 38
          ->getValue()
          .dyn_cast<ir::OpResult>();

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

C
Charles-hit 已提交
39 40
  return Tensor(
      std::make_shared<primitive::experimental::StaticTensor>(op_res));
41 42 43
}

template <>
C
Charles-hit 已提交
44 45 46 47 48 49
Tensor mean_grad<StaticTensor>(const Tensor& x,
                               const Tensor& out_grad,
                               const IntArray& axis,
                               bool keepdim,
                               bool reduce_all) {
  ir::OpResult x_res = std::static_pointer_cast<StaticTensor>(x.impl())
50 51 52
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult out_grad_res =
C
Charles-hit 已提交
53
      std::static_pointer_cast<StaticTensor>(out_grad.impl())
54 55 56 57
          ->getValue()
          .dyn_cast<ir::OpResult>();

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

C
Charles-hit 已提交
60 61
  return Tensor(
      std::make_shared<primitive::experimental::StaticTensor>(op_res));
62 63
}

C
Charles-hit 已提交
64
template <>
C
Charles-hit 已提交
65 66 67 68 69
std::tuple<Tensor, Tensor> add_grad<StaticTensor>(const Tensor& x,
                                                  const Tensor& y,
                                                  const Tensor& out_grad,
                                                  int axis) {
  ir::OpResult x_res = std::static_pointer_cast<StaticTensor>(x.impl())
C
Charles-hit 已提交
70 71
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
C
Charles-hit 已提交
72
  ir::OpResult y_res = std::static_pointer_cast<StaticTensor>(y.impl())
C
Charles-hit 已提交
73 74 75
                           ->getValue()
                           .dyn_cast<ir::OpResult>();
  ir::OpResult out_grad_res =
C
Charles-hit 已提交
76
      std::static_pointer_cast<StaticTensor>(out_grad.impl())
C
Charles-hit 已提交
77 78 79 80 81 82 83
          ->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(
C
Charles-hit 已提交
84
      Tensor(std::make_shared<primitive::experimental::StaticTensor>(
C
Charles-hit 已提交
85
          std::get<0>(op_res))),
C
Charles-hit 已提交
86
      Tensor(std::make_shared<primitive::experimental::StaticTensor>(
C
Charles-hit 已提交
87 88
          std::get<1>(op_res))));
}
89 90 91 92
}  // namespace experimental
}  // namespace backend
}  // namespace primitive
}  // namespace paddle