vjp.cc 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

15
#include "paddle/fluid/primitive/rule/vjp/vjp.h"
16
#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_api.h"
17
#include "paddle/fluid/prim/utils/static/static_global_utils.h"
18
#include "paddle/fluid/primitive/backend/static_backend.h"
19 20 21
#include "paddle/fluid/primitive/rule/vjp/details.h"
#include "paddle/fluid/primitive/type/lazy_tensor.h"
#include "paddle/fluid/primitive/utils/utils.h"
22 23 24 25 26 27
#include "paddle/ir/core/operation.h"
// TODO(wanghao107):
//  op's vjp will be auto generated.

namespace paddle {
namespace primitive {
28

29 30 31
std::vector<std::vector<paddle::Tensor>> tanh_vjp(
    const Tensor& out,
    const Tensor& grad_out,
32
    const std::vector<std::vector<bool>>& stop_gradients) {
33 34 35
  std::vector<std::vector<paddle::Tensor>> vjp_res(
      1, std::vector<paddle::Tensor>(1));
  // get tanh_grad res.
36
  Tensor op_res = backend::tanh_grad<primitive::LazyTensor>(out, grad_out);
37 38 39 40 41

  // set op stop_gradient info
  // TODO(wanghao107): Replace with more generic code.
  // Support set stop_gradients for all ops.
  ir::Operation* grad_op =
42
      std::static_pointer_cast<primitive::LazyTensor>(op_res.impl())
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
          ->getValue()
          .dyn_cast<ir::OpResult>()
          .owner();
  uint32_t num_res = grad_op->num_results();
  std::vector<ir::Attribute> ir_stop_gradients(num_res);
  for (size_t i = 0; i < num_res; i++) {
    if (stop_gradients[0][i]) {
      ir_stop_gradients[i] =
          ir::BoolAttribute::get(ir::IrContext::Instance(), true);
    } else {
      ir_stop_gradients[i] =
          ir::BoolAttribute::get(ir::IrContext::Instance(), false);
    }
  }
  grad_op->set_attribute(
      "stop_gradient",
      ir::ArrayAttribute::get(ir::IrContext::Instance(), ir_stop_gradients));

  // construct vjp result by op result and stop_gradients info
  if (!stop_gradients[0][0]) {
    vjp_res[0][0] = op_res;
  }
  return vjp_res;
}

std::vector<std::vector<paddle::Tensor>> mean_vjp(
    const Tensor& x,
    const Tensor& out_grad,
71
    const IntArray& axis,
72 73
    bool keepdim,
    bool reduce_all,
74
    const std::vector<std::vector<bool>>& stop_gradients) {
75 76 77
  std::vector<std::vector<paddle::Tensor>> vjp_res(
      1, std::vector<paddle::Tensor>(1));
  // get mean_grad res.
78 79
  Tensor op_res = backend::mean_grad<primitive::LazyTensor>(
      x, out_grad, axis, keepdim, reduce_all);
80 81 82 83 84

  // set op stop_gradient info
  // TODO(wanghao107): Replace with more generic code.
  // Support set stop_gradients for all ops.
  ir::Operation* grad_op =
85
      std::static_pointer_cast<primitive::LazyTensor>(op_res.impl())
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
          ->getValue()
          .dyn_cast<ir::OpResult>()
          .owner();
  uint32_t num_res = grad_op->num_results();
  std::vector<ir::Attribute> ir_stop_gradients(num_res);
  for (size_t i = 0; i < num_res; i++) {
    if (stop_gradients[0][i]) {
      ir_stop_gradients[i] =
          ir::BoolAttribute::get(ir::IrContext::Instance(), true);
    } else {
      ir_stop_gradients[i] =
          ir::BoolAttribute::get(ir::IrContext::Instance(), false);
    }
  }
  grad_op->set_attribute(
      "stop_gradient",
      ir::ArrayAttribute::get(ir::IrContext::Instance(), ir_stop_gradients));

  // construct vjp result by op result and stop_gradients info
  if (!stop_gradients[0][0]) {
    vjp_res[0][0] = op_res;
  }
  return vjp_res;
}

C
Charles-hit 已提交
111 112 113 114 115 116 117 118
std::vector<std::vector<paddle::Tensor>> add_vjp(
    const Tensor& x,
    const Tensor& y,
    const Tensor& out_grad,
    int axis,
    const std::vector<std::vector<bool>>& stop_gradients) {
  std::vector<std::vector<paddle::Tensor>> vjp_res(
      2, std::vector<paddle::Tensor>(1));
119
  // get add_grad res.
C
Charles-hit 已提交
120
  std::tuple<Tensor, Tensor> op_res =
121
      backend::add_grad<primitive::LazyTensor>(x, y, out_grad, axis);
C
Charles-hit 已提交
122 123 124 125

  // set op stop_gradient info
  // TODO(wanghao107): Replace with more generic code.
  // Support set stop_gradients for all ops.
126 127 128 129 130
  ir::Operation* grad_op = std::static_pointer_cast<primitive::LazyTensor>(
                               std::get<0>(op_res).impl())
                               ->getValue()
                               .dyn_cast<ir::OpResult>()
                               .owner();
C
Charles-hit 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  std::vector<ir::Attribute> ir_stop_gradients(2);
  for (size_t i = 0; i < 2; i++) {
    if (stop_gradients[i][0]) {
      ir_stop_gradients[i] =
          ir::BoolAttribute::get(ir::IrContext::Instance(), true);
    } else {
      ir_stop_gradients[i] =
          ir::BoolAttribute::get(ir::IrContext::Instance(), false);
    }
  }
  grad_op->set_attribute(
      "stop_gradient",
      ir::ArrayAttribute::get(ir::IrContext::Instance(), ir_stop_gradients));

  // construct vjp result by op result and stop_gradients info
  vjp_res[0][0] = !stop_gradients[0][0] ? std::get<0>(op_res) : vjp_res[0][0];
  vjp_res[1][0] = !stop_gradients[1][0] ? std::get<1>(op_res) : vjp_res[1][0];
  return vjp_res;
}
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
std::vector<std::vector<paddle::Tensor>> concat_vjp(
    const std::vector<Tensor>& x,
    const Tensor& out_grad,
    const Tensor& axis,
    const std::vector<std::vector<bool>>& stop_gradients) {
  std::vector<std::vector<paddle::Tensor>> vjp_res(2, std::vector<Tensor>());
  // get concat_grad res.
  std::vector<Tensor> op_res =
      backend::concat_grad<primitive::LazyTensor>(x, out_grad, axis);

  // construct vjp result by op result and stop_gradients info
  vjp_res[0].resize(op_res.size());
  for (uint64_t idx = 0; idx < op_res.size(); idx++) {
    if (!stop_gradients[0][idx]) {
      vjp_res[0][idx] = op_res[idx];
    }
  }
  // vjp_res[1] is axis's grad which is attribute (no grad).
  vjp_res[1].resize(1);
  return vjp_res;
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 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
std::vector<std::vector<paddle::Tensor>> divide_vjp(
    const Tensor& x,
    const Tensor& y,
    const Tensor& out,
    const Tensor& out_grad,
    int axis,
    const std::vector<std::vector<bool>>& stop_gradients) {
  std::vector<std::vector<paddle::Tensor>> vjp_res(
      2, std::vector<paddle::Tensor>(1));
  if (!paddle::prim::StaticCompositeContext::Instance().IsBwdPrimEnabled()) {
    // get divide_grad res.
    std::tuple<Tensor, Tensor> op_res =
        backend::divide_grad<primitive::LazyTensor>(x, y, out, out_grad, axis);
    // construct vjp result by op result and stop_gradients info
    vjp_res[0][0] = !stop_gradients[0][0] ? std::get<0>(op_res) : vjp_res[0][0];
    vjp_res[1][0] = !stop_gradients[1][0] ? std::get<1>(op_res) : vjp_res[1][0];
  } else {
    // get divide_grad  prim mode res.
    Tensor* dx = !stop_gradients[0][0] ? &vjp_res[0][0] : nullptr;
    Tensor* dy = !stop_gradients[1][0] ? &vjp_res[1][0] : nullptr;
    details::divide_grad<LazyTensor>(x, y, out, out_grad, axis, dx, dy);
  }
  return vjp_res;
}

std::vector<std::vector<paddle::Tensor>> sum_vjp(
    const Tensor& x,
    const Tensor& out_grad,
    const IntArray& axis,
    bool keepdim,
    bool reduce_all,
    const std::vector<std::vector<bool>>& stop_gradients) {
  std::vector<std::vector<paddle::Tensor>> vjp_res(
      1, std::vector<paddle::Tensor>(1));
  if (!paddle::prim::StaticCompositeContext::Instance().IsBwdPrimEnabled()) {
    // get sum_grad res.
    Tensor op_res = backend::sum_grad<primitive::LazyTensor>(
        x, out_grad, axis, keepdim, reduce_all);
    // construct vjp result by op result and stop_gradients info
    if (!stop_gradients[0][0]) {
      vjp_res[0][0] = op_res;
    }
  } else {
    // get divide_grad  prim mode res.
    Tensor* x_grad = !stop_gradients[0][0] ? &vjp_res[0][0] : nullptr;
    details::sum_grad<LazyTensor>(
        x, out_grad, axis, keepdim, reduce_all, x_grad);
  }
  return vjp_res;
}

224 225
}  // namespace primitive
}  // namespace paddle