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

15
#include "paddle/contrib/tape/tape.h"
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

#include <list>
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/dim.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/pybind/pybind.h"

namespace paddle {
32
namespace tape {
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 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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

// borrowed from
// https://stackoverflow.com/questions/874134/find-if-string-ends-with-another-string-in-c
inline bool ends_with(std::string const &value, std::string const &ending) {
  if (ending.size() > value.size()) return false;
  return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}

std::ostream &operator<<(std::ostream &os, const framework::VarDesc &var_desc) {
  os << var_desc.Name();
  os << "[" << var_desc.GetType() << "]";
  os << "[" << var_desc.GetDataType() << "]";
  os << "{";
  for (auto &i : var_desc.GetShape()) {
    os << i << ",";
  }
  os << "}";
  return os;
}

std::string to_string(const std::string &type,
                      const VariableHandleMap &in_vars,
                      const VariableHandleMap &out_vars,
                      const framework::AttributeMap &attrs) {
  std::stringstream ss;
  ss << type << " ";
  for (auto &param_name : in_vars) {
    for (auto &var : param_name.second) {
      ss << param_name.first << ":(" << var->Desc() << ") ";
    }
  }
  for (auto &param_name : out_vars) {
    for (auto &var : param_name.second) {
      ss << param_name.first << ":(" << var->Desc() << ") ";
    }
  }
  return ss.str();
}

framework::OpDesc CreateOpDesc(const std::string &type,
                               const VariableHandleMap &in_vars,
                               const VariableHandleMap &out_vars,
                               const framework::AttributeMap &attrs) {
  framework::VariableNameMap inputs;
  for (auto &param_name : in_vars) {
    for (auto &var : param_name.second) {
      inputs[param_name.first].emplace_back(var->Name());
    }
  }
  framework::VariableNameMap outputs;
  for (auto &param_name : out_vars) {
    for (auto &var : param_name.second) {
      outputs[param_name.first].emplace_back(var->Name());
    }
  }
  return framework::OpDesc(type, inputs, outputs, attrs);
}

void InferShapeAndVarType(const std::string &type,
                          const VariableHandleMap &in_vars,
                          VariableHandleMap *out_vars,
                          const framework::AttributeMap &attrs) {
  framework::OpDesc op_desc = CreateOpDesc(type, in_vars, *out_vars, attrs);

  // Create a temporary block for compile-time
  framework::ProgramDesc program_desc;
  framework::BlockDesc *block_desc = program_desc.MutableBlock(0);
  PADDLE_ENFORCE(block_desc);

  for (auto &param_name : in_vars) {
    for (auto &var : param_name.second) {
      *block_desc->Var(var->Name())->Proto() = *var->MutableDesc()->Proto();
    }
  }
  for (auto &param_name : *out_vars) {
    for (auto &var : param_name.second) {
      *block_desc->Var(var->Name())->Proto() = *var->MutableDesc()->Proto();
    }
  }

  LOG(INFO) << "- " << to_string(type, in_vars, *out_vars, attrs);
  op_desc.InferShape(*block_desc);
  op_desc.InferVarType(block_desc);
  for (auto &param_name : *out_vars) {
    for (auto &var : param_name.second) {
      *var->MutableDesc()->Proto() = *block_desc->Var(var->Name())->Proto();
    }
  }
  LOG(INFO) << "+ " << to_string(type, in_vars, *out_vars, attrs);
}

void Tape::AddOp(const std::string &type,
                 const VariableHandleMap &in_vars,
                 VariableHandleMap out_vars,
                 const framework::AttributeMap &attrs) {
  InferShapeAndVarType(type, in_vars, &out_vars, attrs);
  tape_.emplace_back(type, in_vars, out_vars, attrs);
}

// Temporary Scope for Operator::Run()
class ScopeWrapper : public framework::Scope {
 public:
  ScopeWrapper(const VariableHandleMap &in_vars,
               const VariableHandleMap &out_vars) {
    for (auto &v : in_vars) {
      for (auto &vv : v.second) {
        if (!vars_.count(vv->Name())) {
          vars_[vv->Name()].reset(vv->Var());
        }
      }
    }
    for (auto &v : out_vars) {
      for (auto &vv : v.second) {
        if (!vars_.count(vv->Name())) {
          vars_[vv->Name()].reset(vv->Var());
        }
      }
    }
  }

  ~ScopeWrapper() {
    for (auto &pair : vars_) {
      pair.second.release();
    }
  }
};

void Tape::Forward() {
  LOG(INFO) << "Starting forward -------------------------";
  PADDLE_ENFORCE(!has_been_backwarded_);
  while (current_position_ < tape_.size()) {
    OpHandle &op = tape_[current_position_];

    // Create Output Tensor, this is only necessary for OpWithKernel
    for (auto &param2var : op.outputs_) {
      for (auto &var : param2var.second) {
        var->InitializeVariable();
      }
    }

    framework::OpDesc op_desc =
        CreateOpDesc(op.type_, op.inputs_, op.outputs_, op.attrs_);
    ScopeWrapper scope(op.inputs_, op.outputs_);
    framework::OpRegistry::CreateOp(op_desc)->Run(scope, platform::CPUPlace());
    current_position_++;
  }

  LOG(INFO) << "Finishing forward -------------------------";
}

void Tape::Backward(VariableHandle target) {
  PADDLE_ENFORCE(!has_been_backwarded_);

  Forward();

  // TODO(tonyyang-svail): check output of last op is target
  backward_tape_.reset(new Tape());

  framework::AttributeMap attrs;

  // FIXME(tonyyang-svail): Need to infer_data_type
  attrs["dtype"] = framework::proto::VarType::Type::VarType_Type_FP32;
  attrs["shape"] = std::vector<int>{1};
  attrs["value"] = 1.0f;
  backward_tape_->AddOp(
      "fill_constant", {}, {{"Out", {target->Grad()}}}, attrs);

  for (auto it = tape_.rbegin(); it != tape_.rend(); ++it) {
    framework::OpDesc op_desc =
        CreateOpDesc(it->type_, it->inputs_, it->outputs_, it->attrs_);
    std::unordered_map<std::string, std::string> grad_to_var;
    std::vector<std::unique_ptr<framework::OpDesc>> grad_op_descs =
        framework::OpInfoMap::Instance()
            .Get(op_desc.Type())
            .GradOpMaker()(op_desc, {}, &grad_to_var, {});

    for (auto &op_desc : grad_op_descs) {
      std::unordered_map<std::string, VariableHandle> name2var;
      for (auto &param2vars : it->inputs_) {
        for (auto &a : param2vars.second) {
          name2var[a->Name()] = a;
        }
      }
      for (auto &param2vars : it->outputs_) {
        for (auto &a : param2vars.second) {
          name2var[a->Name()] = a;
        }
      }

      VariableHandleMap in_vars;
      VariableHandleMap out_vars;
      std::map<const framework::VariableNameMap *, VariableHandleMap *>
          loop_over{{&op_desc->Inputs(), &in_vars},
                    {&op_desc->Outputs(), &out_vars}};
      for (auto &each : loop_over) {
        auto &vmp = *each.first;
        auto &vhm = *each.second;
        for (auto &p2a : vmp) {
          for (auto &argu : p2a.second) {
            if (name2var.count(argu)) {
              vhm[p2a.first].push_back(name2var[argu]);
            } else {
              PADDLE_ENFORCE(ends_with(argu, framework::kGradVarSuffix),
                             argu.c_str());
              std::string name = argu.substr(
                  0, argu.size() - std::strlen(framework::kGradVarSuffix));
              PADDLE_ENFORCE(name2var.count(name), name.c_str());
              vhm[p2a.first].push_back(name2var[name]->Grad());
            }
          }
        }
      }

      backward_tape_->AddOp(
          op_desc->Type(), in_vars, out_vars, op_desc->GetAttrMap());
    }

    // TODO(tonyyang-svail): how to fill empty grad?
    // TODO(tonyyang-svail): Sum var grad is necessary
  }

  backward_tape_->Forward();
  has_been_backwarded_ = true;
}

Tape &get_global_tape() {
  static Tape T;
  return T;
}

void reset_global_tape() { get_global_tape() = Tape(); }
}
}