Merge branch 'develop' of https://github.com/paddlepaddle/paddle into add_benchmark_for_trt
test=developce_debug
commit
8c8019e388
@ -0,0 +1,3 @@
|
||||
cc_library(layer SRCS layer.cc DEPS proto_desc operator)
|
||||
cc_library(tracer SRCS tracer.cc DEPS proto_desc)
|
||||
cc_library(engine SRCS engine.cc)
|
@ -0,0 +1,53 @@
|
||||
// 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/engine.h"
|
||||
|
||||
#include <mutex> // NOLINT
|
||||
#include <vector>
|
||||
|
||||
#include "glog/logging.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace imperative {
|
||||
|
||||
static std::once_flag init_engine;
|
||||
static Engine* engine;
|
||||
|
||||
class DummyEngine : public Engine {
|
||||
public:
|
||||
void Enqueue(Runnable* runnable) override {
|
||||
queued_runnables_.push_back(runnable);
|
||||
}
|
||||
|
||||
size_t Size() const override { return queued_runnables_.size(); }
|
||||
|
||||
void Sync() override {
|
||||
for (Runnable* l : queued_runnables_) {
|
||||
LOG(INFO) << "running " << reinterpret_cast<void*>(l);
|
||||
}
|
||||
queued_runnables_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Runnable*> queued_runnables_;
|
||||
};
|
||||
|
||||
Engine* GetEngine() {
|
||||
std::call_once(init_engine, []() { engine = new DummyEngine(); });
|
||||
return engine;
|
||||
}
|
||||
|
||||
} // namespace imperative
|
||||
} // namespace paddle
|
@ -0,0 +1,39 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace paddle {
|
||||
namespace imperative {
|
||||
|
||||
struct Runnable {};
|
||||
|
||||
class Engine {
|
||||
public:
|
||||
virtual ~Engine() {}
|
||||
|
||||
virtual void Enqueue(Runnable* runnable) = 0;
|
||||
|
||||
virtual size_t Size() const = 0;
|
||||
|
||||
virtual void Sync() = 0;
|
||||
};
|
||||
|
||||
Engine* GetEngine();
|
||||
|
||||
} // namespace imperative
|
||||
} // namespace paddle
|
@ -0,0 +1,221 @@
|
||||
// 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>();
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
class Autograd {
|
||||
public:
|
||||
explicit Autograd(framework::Scope* scope) : scope_(scope) {}
|
||||
|
||||
void RunBackward(VarBase* var) {
|
||||
PADDLE_ENFORCE(var->pre_op_->op_desc_);
|
||||
// TODO(panyx0718): Only create for vars that "require_grad"
|
||||
(*var->pre_op_->output_vars_)[var->pre_op_out_idx_]->grads_ = var->grads_;
|
||||
|
||||
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();
|
||||
std::vector<Variable*> input_grads = ready_op->ApplyGrad(scope_);
|
||||
|
||||
for (size_t i = 0; i < input_grads.size(); ++i) {
|
||||
if (!input_grads[i]) continue;
|
||||
OpBase* pre_op = ready_op->pre_ops_->at(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
for (OpBase* pre_op : *(candidate->pre_ops_)) {
|
||||
if (!pre_op) continue;
|
||||
if (visited.find(pre_op) == visited.end()) {
|
||||
visited.insert(pre_op);
|
||||
queue.push_back(pre_op);
|
||||
}
|
||||
ret[pre_op] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
framework::Scope* scope_;
|
||||
};
|
||||
|
||||
framework::Variable* CreateVariable(const std::string& name,
|
||||
const framework::DDim& dim, float val,
|
||||
framework::Scope* scope,
|
||||
bool random_name = true) {
|
||||
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::Variable* var = scope->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);
|
||||
return var;
|
||||
}
|
||||
|
||||
framework::LoDTensor& VarBase::Grad() {
|
||||
VLOG(3) << "get var grad " << var_desc_->Name();
|
||||
return *grads_->GetMutable<framework::LoDTensor>();
|
||||
}
|
||||
|
||||
void VarBase::ApplyGrad(framework::Scope* scope, Variable* grad) {
|
||||
VLOG(3) << "apply var grad " << var_desc_->Name() << " "
|
||||
<< grad->Get<framework::LoDTensor>().data<float>()[0];
|
||||
if (!grads_) {
|
||||
grads_ =
|
||||
CreateVariable(string::Sprintf("%s@IGrad", var_desc_->Name()),
|
||||
var_->Get<framework::LoDTensor>().dims(), 0.0, scope);
|
||||
}
|
||||
AddTo(grad, grads_);
|
||||
VLOG(3) << "grad_ after apply var grad " << var_desc_->Name() << " "
|
||||
<< grads_->Get<framework::LoDTensor>().data<float>()[0];
|
||||
}
|
||||
|
||||
std::vector<Variable*> OpBase::ApplyGrad(framework::Scope* scope) {
|
||||
VLOG(3) << "op grad " << grad_op_desc_->Type();
|
||||
|
||||
for (const std::string& grad_invar : grad_op_desc_->InputArgumentNames()) {
|
||||
if (grad_to_var_->find(grad_invar) == grad_to_var_->end()) {
|
||||
// grad op inputs can be forward inputs, so not in grad_to_var.
|
||||
continue;
|
||||
}
|
||||
VLOG(3) << "op grad in var " << grad_invar;
|
||||
block_->FindRecursiveOrCreateVar(grad_invar);
|
||||
framework::Variable* var = scope->Var(grad_invar);
|
||||
const std::string& invar = grad_to_var_->at(grad_invar);
|
||||
for (VarBase* varbase : *output_vars_) {
|
||||
// Use the accumulated grads_ by sharing the input with grads_.
|
||||
if (varbase->var_desc_->Name() == invar) {
|
||||
var->GetMutable<framework::LoDTensor>()->ShareDataWith(
|
||||
varbase->grads_->Get<framework::LoDTensor>());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const std::string& outvar : grad_op_desc_->OutputArgumentNames()) {
|
||||
VLOG(3) << "grad outvar " << outvar;
|
||||
block_->FindRecursiveOrCreateVar(outvar);
|
||||
framework::Variable* var = scope->Var(outvar);
|
||||
if (!var->IsInitialized()) {
|
||||
framework::VarDesc* var_desc = block_->FindVar(outvar);
|
||||
if (var_desc->GetType() == framework::proto::VarType::LOD_TENSOR) {
|
||||
var->GetMutable<framework::LoDTensor>();
|
||||
} else {
|
||||
LOG(ERROR) << "tracer doesn't support yet";
|
||||
}
|
||||
}
|
||||
}
|
||||
grad_op_desc_->InferShape(*block_);
|
||||
grad_op_desc_->InferVarType(block_);
|
||||
std::unique_ptr<framework::OperatorBase> opbase =
|
||||
framework::OpRegistry::CreateOp(*grad_op_desc_);
|
||||
|
||||
opbase->Run(*scope, platform::CPUPlace());
|
||||
|
||||
// `ret` matches exactly with `input_vars_` of forward op.
|
||||
std::vector<Variable*> ret;
|
||||
for (size_t i = 0; i < input_vars_->size(); ++i) {
|
||||
bool found = false;
|
||||
for (const std::string& outvar : grad_op_desc_->OutputArgumentNames()) {
|
||||
Variable* var = scope->FindVar(outvar);
|
||||
VarBase* origin_var = (*input_vars_)[i];
|
||||
std::string orig_var = grad_to_var_->at(outvar);
|
||||
PADDLE_ENFORCE(origin_var->var_desc_->Name() == orig_var);
|
||||
VLOG(3) << "apply grad " << outvar << " with origin " << orig_var;
|
||||
origin_var->ApplyGrad(scope, var);
|
||||
found = true;
|
||||
ret.push_back(var);
|
||||
// TODO(panyx0718): There might be another outvar with the same name.
|
||||
// In that case, it doesn't matter the first one or the second one is
|
||||
// used.
|
||||
break;
|
||||
}
|
||||
if (!found) {
|
||||
ret.push_back(nullptr);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void VarBase::RunBackward(framework::Scope* scope) {
|
||||
grads_ = CreateVariable(framework::GradVarName(var_desc_->Name()),
|
||||
var_->Get<framework::LoDTensor>().dims(), 1.0, scope,
|
||||
false);
|
||||
if (!pre_op_) return;
|
||||
Autograd(scope).RunBackward(this);
|
||||
}
|
||||
|
||||
} // namespace imperative
|
||||
} // namespace paddle
|
@ -0,0 +1,102 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "paddle/fluid/framework/op_desc.h"
|
||||
#include "paddle/fluid/framework/operator.h"
|
||||
#include "paddle/fluid/framework/scope.h"
|
||||
#include "paddle/fluid/framework/var_desc.h"
|
||||
#include "paddle/fluid/platform/enforce.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace imperative {
|
||||
|
||||
class OpBase;
|
||||
|
||||
class VarBase {
|
||||
public:
|
||||
VarBase()
|
||||
: pre_op_(nullptr),
|
||||
pre_op_out_idx_(-1),
|
||||
var_desc_(nullptr),
|
||||
var_(nullptr),
|
||||
grads_(nullptr) {}
|
||||
|
||||
virtual ~VarBase() {}
|
||||
|
||||
void ApplyGrad(framework::Scope* scope, framework::Variable* grad);
|
||||
|
||||
void RunBackward(framework::Scope* scope);
|
||||
|
||||
framework::LoDTensor& Grad();
|
||||
|
||||
OpBase* pre_op_;
|
||||
int pre_op_out_idx_;
|
||||
|
||||
framework::VarDesc* var_desc_;
|
||||
framework::Variable* var_;
|
||||
framework::Variable* grads_;
|
||||
};
|
||||
|
||||
class OpBase {
|
||||
public:
|
||||
OpBase()
|
||||
: input_vars_(new std::vector<VarBase*>()),
|
||||
output_vars_(new std::vector<VarBase*>()),
|
||||
pre_ops_(new std::vector<OpBase*>()),
|
||||
pre_ops_out_idx_(new std::vector<int>()),
|
||||
op_desc_(nullptr),
|
||||
grad_op_desc_(nullptr) {}
|
||||
|
||||
virtual ~OpBase() {
|
||||
delete input_vars_;
|
||||
delete output_vars_;
|
||||
|
||||
delete pre_ops_;
|
||||
delete pre_ops_out_idx_;
|
||||
|
||||
if (grad_op_desc_) delete grad_op_desc_;
|
||||
if (grad_to_var_) delete grad_to_var_;
|
||||
}
|
||||
|
||||
std::vector<framework::Variable*> ApplyGrad(framework::Scope* scope);
|
||||
|
||||
std::vector<VarBase*>* input_vars_;
|
||||
std::vector<VarBase*>* output_vars_;
|
||||
std::vector<OpBase*>* pre_ops_;
|
||||
std::vector<int>* pre_ops_out_idx_;
|
||||
framework::OpDesc* op_desc_;
|
||||
|
||||
framework::OpDesc* grad_op_desc_;
|
||||
std::unordered_map<std::string, std::string>* grad_to_var_;
|
||||
framework::BlockDesc* block_;
|
||||
};
|
||||
|
||||
class Layer {
|
||||
public:
|
||||
virtual ~Layer() {}
|
||||
|
||||
virtual std::vector<VarBase> Forward(const std::vector<VarBase>& inputs) {
|
||||
std::vector<VarBase> vars;
|
||||
return vars;
|
||||
}
|
||||
|
||||
virtual void Backward() { LOG(ERROR) << "To support customize"; }
|
||||
};
|
||||
|
||||
} // namespace imperative
|
||||
} // namespace paddle
|
@ -0,0 +1,19 @@
|
||||
// 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/tracer.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace imperative {} // namespace imperative
|
||||
} // namespace paddle
|
@ -0,0 +1,128 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "paddle/fluid/framework/op_desc.h"
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
#include "paddle/fluid/framework/scope.h"
|
||||
#include "paddle/fluid/imperative/engine.h"
|
||||
#include "paddle/fluid/imperative/layer.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace imperative {
|
||||
|
||||
void CreateGradOp(const framework::OpDesc& op_desc,
|
||||
const std::unordered_set<std::string>& no_grad_set,
|
||||
const std::vector<framework::BlockDesc*>& grad_sub_block,
|
||||
framework::OpDesc** grad_op_desc,
|
||||
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, no_grad_set, grad_to_var, grad_sub_block);
|
||||
PADDLE_ENFORCE(grad_op_descs.size() == 1, "Only support 1 grad op now.");
|
||||
// TODO(panyx0718): Leak?
|
||||
*grad_op_desc = grad_op_descs[0].release();
|
||||
}
|
||||
|
||||
class Tracer {
|
||||
public:
|
||||
explicit Tracer(framework::BlockDesc* root_block) : root_block_(root_block) {
|
||||
root_scope_ = new framework::Scope();
|
||||
scopes_[root_block_] = root_scope_;
|
||||
}
|
||||
|
||||
virtual ~Tracer() { delete root_scope_; }
|
||||
|
||||
void Trace(OpBase* op, const std::vector<VarBase*>& inputs,
|
||||
const std::vector<VarBase*>& outputs,
|
||||
framework::BlockDesc* block) {
|
||||
framework::Scope* scope = GetScope(block);
|
||||
framework::OpDesc* op_desc = op->op_desc_;
|
||||
VLOG(3) << "tracer tracing " << op_desc->Type();
|
||||
op_desc->InferShape(*block);
|
||||
op_desc->InferVarType(block);
|
||||
std::unique_ptr<framework::OperatorBase> op_base =
|
||||
framework::OpRegistry::CreateOp(*op_desc);
|
||||
|
||||
*op->input_vars_ = inputs;
|
||||
for (VarBase* input : inputs) {
|
||||
const std::string vname = input->var_desc_->Name();
|
||||
framework::Variable* var = scope->Var(vname);
|
||||
input->var_ = var;
|
||||
if (!var->IsInitialized()) {
|
||||
framework::VarDesc* var_desc = block->FindVar(vname);
|
||||
if (var_desc->GetType() == framework::proto::VarType::LOD_TENSOR) {
|
||||
var->GetMutable<framework::LoDTensor>();
|
||||
} else {
|
||||
LOG(ERROR) << "tracer doesn't support yet";
|
||||
}
|
||||
}
|
||||
if (input->pre_op_) {
|
||||
op->pre_ops_->push_back(input->pre_op_);
|
||||
op->pre_ops_out_idx_->push_back(input->pre_op_out_idx_);
|
||||
} else {
|
||||
op->pre_ops_->push_back(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
*op->output_vars_ = outputs;
|
||||
for (size_t i = 0; i < outputs.size(); ++i) {
|
||||
const std::string vname = outputs[i]->var_desc_->Name();
|
||||
framework::Variable* var = scope->Var(vname);
|
||||
if (!var->IsInitialized()) {
|
||||
framework::VarDesc* var_desc = block->FindVar(vname);
|
||||
if (var_desc->GetType() == framework::proto::VarType::LOD_TENSOR) {
|
||||
var->GetMutable<framework::LoDTensor>();
|
||||
} else {
|
||||
LOG(ERROR) << "tracer doesn't support yet";
|
||||
}
|
||||
}
|
||||
outputs[i]->var_ = var;
|
||||
outputs[i]->pre_op_ = op;
|
||||
outputs[i]->pre_op_out_idx_ = i;
|
||||
}
|
||||
op_base->Run(*scope, platform::CPUPlace());
|
||||
framework::OpDesc* grad_op_desc;
|
||||
auto grad_to_var = new std::unordered_map<std::string, std::string>();
|
||||
CreateGradOp(*op_desc, {}, {block}, &grad_op_desc, grad_to_var);
|
||||
op->grad_op_desc_ = grad_op_desc;
|
||||
op->grad_to_var_ = grad_to_var;
|
||||
op->block_ = block;
|
||||
}
|
||||
|
||||
framework::Scope* GetScope(framework::BlockDesc* block) {
|
||||
if (scopes_.find(block) != scopes_.end()) {
|
||||
return scopes_.at(block);
|
||||
}
|
||||
framework::BlockDesc* parent_block = block->ParentBlock();
|
||||
PADDLE_ENFORCE(scopes_.find(parent_block) != scopes_.end());
|
||||
framework::Scope* scope = &scopes_[parent_block]->NewScope();
|
||||
scopes_[block] = scope;
|
||||
return scope;
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<framework::BlockDesc*, framework::Scope*> scopes_;
|
||||
framework::BlockDesc* root_block_;
|
||||
framework::Scope* root_scope_;
|
||||
};
|
||||
|
||||
} // namespace imperative
|
||||
} // namespace paddle
|
@ -0,0 +1,145 @@
|
||||
/* Copyright (c) 2016 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/operators/bpr_loss_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class BprLossOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Y"), "Output(Y) should be not null.");
|
||||
|
||||
auto x_dims = ctx->GetInputDim("X");
|
||||
auto label_dims = ctx->GetInputDim("Label");
|
||||
int rank = x_dims.size();
|
||||
PADDLE_ENFORCE_EQ(rank, label_dims.size(),
|
||||
"Input(X) and Input(Label) shall have the same rank.");
|
||||
PADDLE_ENFORCE_EQ(framework::slice_ddim(x_dims, 0, rank - 1),
|
||||
framework::slice_ddim(label_dims, 0, rank - 1),
|
||||
"Input(X) and Input(Label) shall have the same shape "
|
||||
"except the last dimension.");
|
||||
|
||||
auto y_dims = x_dims;
|
||||
y_dims[rank - 1] = 1;
|
||||
ctx->SetOutputDim("Y", y_dims);
|
||||
ctx->ShareLoD("X", /*->*/ "Y");
|
||||
}
|
||||
|
||||
protected:
|
||||
// Explicitly set that the data type of computation kernel of Seq-bpr
|
||||
// is determined by its input "X".
|
||||
framework::OpKernelType GetExpectedKernelType(
|
||||
const framework::ExecutionContext& ctx) const override {
|
||||
return framework::OpKernelType(
|
||||
framework::ToDataType(ctx.Input<Tensor>("X")->type()),
|
||||
platform::CPUPlace());
|
||||
}
|
||||
};
|
||||
|
||||
class BprLossGradientOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Y")),
|
||||
"Input(Y@GRAD) shoudl be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
|
||||
"Output(X@GRAD) should be not null.");
|
||||
|
||||
auto x_dims = ctx->GetInputDim("X");
|
||||
auto label_dims = ctx->GetInputDim("Label");
|
||||
auto dy_dims = ctx->GetInputDim(framework::GradVarName("Y"));
|
||||
int rank = x_dims.size();
|
||||
PADDLE_ENFORCE_EQ(dy_dims.size(), rank,
|
||||
"Input(Y@Grad) and Input(X) should have the same rank.");
|
||||
PADDLE_ENFORCE_EQ(label_dims.size(), rank,
|
||||
"Input(Label) and Input(X) should have the same rank.");
|
||||
PADDLE_ENFORCE_EQ(framework::slice_ddim(x_dims, 0, rank - 1),
|
||||
framework::slice_ddim(label_dims, 0, rank - 1),
|
||||
"The Input(X) and Input(Label) should have the same "
|
||||
"shape except the last dimension.");
|
||||
PADDLE_ENFORCE_EQ(framework::slice_ddim(x_dims, 0, rank - 1),
|
||||
framework::slice_ddim(dy_dims, 0, rank - 1),
|
||||
"The Input(X) and Input(Y@Grad) should have the same "
|
||||
"shape except the last dimension.");
|
||||
PADDLE_ENFORCE_EQ(dy_dims[rank - 1], 1,
|
||||
"The last dimension of Input(Y@Grad) should be 1.");
|
||||
PADDLE_ENFORCE_EQ(label_dims[rank - 1], 1,
|
||||
" the last dimension of Input(Label) should be 1.");
|
||||
ctx->SetOutputDim(framework::GradVarName("X"), x_dims);
|
||||
ctx->ShareLoD("X", framework::GradVarName("X"));
|
||||
}
|
||||
|
||||
protected:
|
||||
// Explicitly set that the data type of computation kernel of cross_entropy
|
||||
// is determined by its input "X".
|
||||
framework::OpKernelType GetExpectedKernelType(
|
||||
const framework::ExecutionContext& ctx) const override {
|
||||
return framework::OpKernelType(
|
||||
framework::ToDataType(ctx.Input<Tensor>("X")->type()),
|
||||
platform::CPUPlace());
|
||||
}
|
||||
};
|
||||
|
||||
class BprLossOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
void Make() override {
|
||||
AddInput("X",
|
||||
"(Tensor, default Tensor<float>), a tensor whose last dimension "
|
||||
"size is equal to the number of classes. This input is a "
|
||||
"real number.");
|
||||
AddInput(
|
||||
"Label",
|
||||
"(Tensor), the tensor which represents the ground truth. It has the "
|
||||
"same shape with 'X' except the last dimension. the last dimension "
|
||||
"size is 1.");
|
||||
AddOutput("Y",
|
||||
"(Tensor, default Tensor<float>), a tensor whose shape is same "
|
||||
"with 'X' except that the last dimension size is 1. It "
|
||||
"represents the sequence bpr loss.");
|
||||
AddComment(R"DOC(
|
||||
Bayesian Personalized Ranking Loss Operator.
|
||||
|
||||
This operator belongs to pairwise ranking loss. Label is the desired item.
|
||||
The loss at a given point in one session is defined as:
|
||||
$Y[i] = -\frac{1}{N_{i}} * \sum_{j=0}^{N_{i}}\log(\sigma(X[i, Label[i]]-X[i, j]))$
|
||||
|
||||
Learn more details by reading paper <session-based recommendations with recurrent
|
||||
neural networks>(https://arxiv.org/abs/1511.06939)
|
||||
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
using CPUCtx = paddle::platform::CPUDeviceContext;
|
||||
|
||||
REGISTER_OPERATOR(bpr_loss, ops::BprLossOp, ops::BprLossOpMaker,
|
||||
paddle::framework::DefaultGradOpDescMaker<true>);
|
||||
REGISTER_OPERATOR(bpr_loss_grad, ops::BprLossGradientOp);
|
||||
REGISTER_OP_CPU_KERNEL(bpr_loss, ops::BprLossOpKernel<CPUCtx, float>,
|
||||
ops::BprLossOpKernel<CPUCtx, double>);
|
||||
REGISTER_OP_CPU_KERNEL(bpr_loss_grad,
|
||||
ops::BprLossGradientOpKernel<CPUCtx, float>,
|
||||
ops::BprLossGradientOpKernel<CPUCtx, double>);
|
@ -0,0 +1,118 @@
|
||||
/* Copyright (c) 2016 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. */
|
||||
|
||||
#pragma once
|
||||
#include "paddle/fluid/framework/eigen.h"
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
#include "paddle/fluid/operators/math/math_function.h"
|
||||
#include "paddle/fluid/platform/for_range.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
/*Todo:
|
||||
*Find a way to adapt TolerableValue, using blas or eigen.
|
||||
*/
|
||||
template <typename T>
|
||||
struct TolerableValue {
|
||||
HOSTDEVICE T operator()(const T& x) const {
|
||||
PADDLE_ASSERT(std::is_floating_point<T>::value);
|
||||
const T kApproInf = 1e20;
|
||||
if (x == INFINITY) return kApproInf;
|
||||
if (x == -INFINITY) return -kApproInf;
|
||||
return x;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename DeviceContext, typename T>
|
||||
class BprLossOpKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override {
|
||||
auto* x = ctx.Input<Tensor>("X");
|
||||
auto* label = ctx.Input<Tensor>("Label");
|
||||
auto* y = ctx.Output<Tensor>("Y");
|
||||
y->mutable_data<T>(ctx.GetPlace());
|
||||
int rank = x->dims().size();
|
||||
|
||||
Tensor x_2d = framework::ReshapeToMatrix(*x, rank - 1);
|
||||
Tensor labels_2d = framework::ReshapeToMatrix(*label, rank - 1);
|
||||
Tensor y_2d = framework::ReshapeToMatrix(*y, rank - 1);
|
||||
|
||||
const framework::Tensor* logits = &x_2d;
|
||||
const framework::Tensor* labels = &labels_2d;
|
||||
framework::Tensor* out = &y_2d;
|
||||
|
||||
const int step_size = logits->dims()[0];
|
||||
const int class_num = logits->dims()[1];
|
||||
const T* logits_data = logits->data<T>();
|
||||
T* loss_data = out->data<T>();
|
||||
|
||||
const int64_t* label_data = labels->data<int64_t>();
|
||||
for (int i = 0; i < step_size; ++i) {
|
||||
int lbl_pos = label_data[i];
|
||||
PADDLE_ENFORCE_GE(lbl_pos, 0);
|
||||
PADDLE_ENFORCE_LT(lbl_pos, class_num);
|
||||
int index_pos = i * class_num + lbl_pos;
|
||||
T sum = static_cast<T>(0);
|
||||
for (int j = 0; j < class_num; j++) {
|
||||
if (j == lbl_pos) continue;
|
||||
int index_neg = i * class_num + j;
|
||||
sum += TolerableValue<T>()(-std::log(
|
||||
1.0f + TolerableValue<T>()(std::exp(logits_data[index_neg] -
|
||||
logits_data[index_pos]))));
|
||||
}
|
||||
loss_data[i] = -sum / (class_num - 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename DeviceContext, typename T>
|
||||
class BprLossGradientOpKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override {
|
||||
auto* x = ctx.Input<Tensor>("X");
|
||||
auto* dy = ctx.Input<Tensor>(framework::GradVarName("Y"));
|
||||
auto* label = ctx.Input<Tensor>("Label");
|
||||
auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
|
||||
|
||||
const int step_size = x->dims()[0];
|
||||
const int num_classes = x->dims()[1];
|
||||
T* dx_data = dx->mutable_data<T>(ctx.GetPlace());
|
||||
const T* dy_data = dy->data<T>();
|
||||
const T* x_data = x->data<T>();
|
||||
const int64_t* label_data = label->data<int64_t>();
|
||||
|
||||
for (size_t sample_id = 0; sample_id < step_size; sample_id++) {
|
||||
for (size_t x_offset = sample_id * num_classes;
|
||||
x_offset < (sample_id + 1) * num_classes; x_offset++) {
|
||||
dx_data[x_offset] = static_cast<T>(0);
|
||||
}
|
||||
auto p_index = sample_id * num_classes + label_data[sample_id];
|
||||
for (size_t ni = 0; ni < num_classes; ni++) {
|
||||
if (label_data[sample_id] == ni) continue;
|
||||
auto n_index = sample_id * num_classes + ni;
|
||||
auto grad_ = -dy_data[sample_id] /
|
||||
((num_classes - 1) *
|
||||
(1.0f + TolerableValue<T>()(std::exp(x_data[p_index] -
|
||||
x_data[n_index]))));
|
||||
dx_data[p_index] += grad_;
|
||||
dx_data[n_index] -= grad_;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,36 @@
|
||||
/* 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/pybind/imperative.h"
|
||||
#include "paddle/fluid/framework/block_desc.h"
|
||||
#include "paddle/fluid/framework/scope.h"
|
||||
#include "paddle/fluid/imperative/tracer.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace pybind {
|
||||
|
||||
// Bind Methods
|
||||
void BindTracer(pybind11::module *m) {
|
||||
pybind11::class_<imperative::Tracer>(*m, "Tracer", "")
|
||||
.def("__init__",
|
||||
[](imperative::Tracer &self, framework::BlockDesc *root_block) {
|
||||
new (&self) imperative::Tracer(root_block);
|
||||
})
|
||||
.def("trace", &imperative::Tracer::Trace)
|
||||
.def("get_scope", &imperative::Tracer::GetScope,
|
||||
pybind11::return_value_policy::reference);
|
||||
}
|
||||
|
||||
} // namespace pybind
|
||||
} // namespace paddle
|
@ -0,0 +1,53 @@
|
||||
/* 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. */
|
||||
#pragma once
|
||||
|
||||
#include <Python.h>
|
||||
#include <vector>
|
||||
#include "paddle/fluid/imperative/layer.h"
|
||||
#include "pybind11/pybind11.h"
|
||||
#include "pybind11/stl.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace pybind {
|
||||
|
||||
class PyLayer : public imperative::Layer {
|
||||
public:
|
||||
using imperative::Layer::Layer; // Inherit constructors
|
||||
|
||||
std::vector<imperative::VarBase> Forward(
|
||||
const std::vector<imperative::VarBase>& inputs) override {
|
||||
PYBIND11_OVERLOAD(std::vector<imperative::VarBase>, Layer, Forward,
|
||||
inputs); // NOLINT
|
||||
}
|
||||
|
||||
void Backward() override {
|
||||
PYBIND11_OVERLOAD(void, Layer, Backward, ); // NOLINT
|
||||
}
|
||||
};
|
||||
|
||||
class PyOpBase : public imperative::OpBase {
|
||||
public:
|
||||
using imperative::OpBase::OpBase; // Inherit constructors
|
||||
};
|
||||
|
||||
class PyVarBase : public imperative::VarBase {
|
||||
public:
|
||||
using imperative::VarBase::VarBase; // Inherit constructors
|
||||
};
|
||||
|
||||
void BindTracer(pybind11::module* m);
|
||||
|
||||
} // namespace pybind
|
||||
} // namespace paddle
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue