layer.cc 6.7 KB
Newer Older
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
// Copyright (c) 2018 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/imperative/layer.h"
#include <deque>
#include <limits>
#include <map>
#include <random>
#include <utility>

#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/string/printf.h"

namespace paddle {
namespace imperative {

using framework::Variable;

void AddTo(Variable* src, Variable* dst) {
  framework::LoDTensor* dst_tensor = dst->GetMutable<framework::LoDTensor>();
  framework::LoDTensor* src_tensor = src->GetMutable<framework::LoDTensor>();
X
Xin Pan 已提交
34 35 36 37 38

  VLOG(3) << "apply var grad " << src_tensor->data<float>()[0] << " "
          << src_tensor->data<float>()[1] << " "
          << src_tensor->data<float>()[2];

39 40 41 42 43 44 45
  PADDLE_ENFORCE(dst_tensor->numel() == src_tensor->numel(), "%lld vs %lld",
                 dst_tensor->numel(), src_tensor->numel());
  float* dst_data = dst_tensor->mutable_data<float>(platform::CPUPlace());
  const float* src_data = src_tensor->data<float>();
  for (size_t i = 0; i < src_tensor->numel(); ++i) {
    dst_data[i] += src_data[i];
  }
X
Xin Pan 已提交
46 47 48 49

  VLOG(3) << "apply var dst grad " << dst_tensor->data<float>()[0] << " "
          << dst_tensor->data<float>()[1] << " "
          << dst_tensor->data<float>()[2];
50 51 52 53
}

class Autograd {
 public:
X
Xin Pan 已提交
54
  Autograd() {}
55 56 57 58

  void RunBackward(VarBase* var) {
    PADDLE_ENFORCE(var->pre_op_->op_desc_);
    // TODO(panyx0718): Only create for vars that "require_grad"
X
Xin Pan 已提交
59 60 61 62 63 64 65 66 67
    LOG(ERROR) << reinterpret_cast<void*>(var->grads_) << " vs "
               << reinterpret_cast<void*>(
                      var->pre_op_
                          ->output_vars_[var->pre_op_out_name_]
                                        [var->pre_op_out_idx_]
                          ->grads_);
    var->pre_op_->output_vars_[var->pre_op_out_name_][var->pre_op_out_idx_]
        ->grads_->GetMutable<framework::LoDTensor>()
        ->ShareDataWith(var->grads_->Get<framework::LoDTensor>());
68 69 70 71 72 73 74 75 76

    std::deque<OpBase*> ready;
    ready.push_back(var->pre_op_);

    std::map<OpBase*, int> dep_counts = ComputeDepCounts(var->pre_op_);

    while (!ready.empty()) {
      OpBase* ready_op = ready.front();
      ready.pop_front();
X
Xin Pan 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
      std::map<std::string, std::vector<VarBase*>> input_grads =
          ready_op->ApplyGrad();
      VLOG(3) << "after apply grad";

      for (auto it : input_grads) {
        const std::vector<VarBase*>& ingrads = it.second;
        for (size_t i = 0; i < ingrads.size(); ++i) {
          if (!ingrads[i]) continue;
          OpBase* pre_op = (*ready_op->pre_ops_)[it.first][i];
          if (!pre_op) continue;

          dep_counts[pre_op] -= 1;
          PADDLE_ENFORCE(dep_counts[pre_op] >= 0);
          bool pre_op_ready = dep_counts[pre_op] == 0;
          if (pre_op_ready) {
            ready.push_back(pre_op);
          }
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        }
      }
    }
  }

 private:
  std::map<OpBase*, int> ComputeDepCounts(OpBase* op) {
    std::map<OpBase*, int> ret;

    std::deque<OpBase*> queue;
    queue.push_back(op);
    std::unordered_set<OpBase*> visited;
    visited.insert(op);
    while (!queue.empty()) {
      OpBase* candidate = queue.front();
      queue.pop_front();
X
Xin Pan 已提交
110 111 112 113 114 115 116 117
      for (auto it : *(candidate->pre_ops_)) {
        for (OpBase* pre_op : it.second) {
          if (!pre_op) continue;
          if (visited.find(pre_op) == visited.end()) {
            visited.insert(pre_op);
            queue.push_back(pre_op);
          }
          ret[pre_op] += 1;
118 119 120 121 122 123 124
        }
      }
    }
    return ret;
  }
};

X
Xin Pan 已提交
125 126 127 128
void CreateVariable(const std::string& name, const framework::DDim& dim,
                    float val, bool random_name, framework::Variable* var) {
  if (var->IsInitialized()) return;

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  std::string varname = name;
  if (random_name) {
    std::mt19937 rng;
    rng.seed(std::random_device()());
    std::uniform_int_distribution<std::mt19937::result_type> dist6(
        1, std::numeric_limits<int>::max());
    int id = dist6(rng);
    varname = string::Sprintf("%s@%d", varname, id);
  }

  VLOG(3) << "creating var " << varname;
  framework::LoDTensor* tensor = var->GetMutable<framework::LoDTensor>();
  float* data = tensor->mutable_data<float>(dim, platform::CPUPlace());
  std::fill(data, data + tensor->numel(), val);
}

framework::LoDTensor& VarBase::Grad() {
  VLOG(3) << "get var grad " << var_desc_->Name();
  return *grads_->GetMutable<framework::LoDTensor>();
}

X
Xin Pan 已提交
150 151 152 153
std::map<std::string, std::vector<VarBase*>> OpBase::ApplyGrad() {
  if (!grad_op_desc_) {
    VLOG(3) << "op with no grad: " << op_desc_->Type();
    return {};
154 155 156
  }
  VLOG(3) << "op grad " << grad_op_desc_->Type();

X
Xin Pan 已提交
157 158 159 160 161 162 163 164 165 166 167 168
  std::map<std::string, std::vector<framework::Variable*>> grad_outputs;
  for (auto it : grad_output_vars_) {
    auto& outputs = grad_outputs[it.first];
    for (size_t i = 0; i < it.second.size(); ++i) {
      outputs.push_back(new framework::Variable());
      outputs.back()->GetMutable<framework::LoDTensor>();
      /*
      auto& accum_grad_t = it.second[i]->Get<framework::LoDTensor>();
      Variable* grad_var = outputs.back();
      float* data = grad_var->GetMutable<framework::LoDTensor>()
          ->mutable_data<float>(accum_grad_t.dims(), platform::CPUPlace());
      std::fill(data, data + accum_grad_t.numel(), 0.0);*/
169 170 171
    }
  }

X
Xin Pan 已提交
172 173 174
  framework::RuntimeContext ctx(grad_input_vars_, grad_outputs);

  // grad_op_desc_->InferShape(*block_);
175
  grad_op_desc_->InferVarType(block_);
X
Xin Pan 已提交
176

177 178
  std::unique_ptr<framework::OperatorBase> opbase =
      framework::OpRegistry::CreateOp(*grad_op_desc_);
X
Xin Pan 已提交
179 180 181 182 183 184 185 186 187
  opbase->Run(ctx, platform::CPUPlace());

  for (auto it : grad_output_vars_) {
    auto& outputs = grad_outputs[it.first];
    auto& origin_outputs = it.second;
    for (size_t i = 0; i < outputs.size(); ++i) {
      framework::Variable* orig_grad = origin_outputs[i];
      AddTo(outputs[i], orig_grad);
      VLOG(3) << "done add to " << grad_op_desc_->Outputs().at(it.first)[i];
188 189
    }
  }
X
Xin Pan 已提交
190
  return input_vars_;
191 192
}

X
Xin Pan 已提交
193 194 195 196 197
void VarBase::RunBackward() {
  auto grads_t = grads_->GetMutable<framework::LoDTensor>();
  float* data = grads_t->mutable_data<float>(platform::CPUPlace());
  std::fill(data, data + grads_t->numel(), 1.0);

198
  if (!pre_op_) return;
X
Xin Pan 已提交
199
  Autograd().RunBackward(this);
200 201 202 203
}

}  // namespace imperative
}  // namespace paddle