diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 38675d2cac0bd7a48857c727d76914de6fb33ce6..51b7f572c97e58a24654892ea0082671324dba50 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -182,7 +182,7 @@ void OperatorBase::Run(const Scope& scope, const platform::Place& place) { void OperatorBase::Run(const RuntimeContext& ctx, const platform::Place& place) { - RunImpl(ctx, place); + RunImplPrepared(ctx, place); } bool OperatorBase::HasInputs(const std::string& name) const { @@ -959,9 +959,9 @@ void OperatorWithKernel::RunImpl(const Scope& scope, } } -void OperatorWithKernel::RunImpl(const RuntimeContext& ctx, - const platform::Place& place) const { - Scope scope; +void OperatorWithKernel::RunImplPrepared(const RuntimeContext& ctx, + const platform::Place& place) const { + Scope dummy_scope; platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance(); auto* dev_ctx = pool.Get(place); @@ -976,7 +976,7 @@ void OperatorWithKernel::RunImpl(const RuntimeContext& ctx, OpKernelMap& kernels = kernels_iter->second; auto expected_kernel_key = this->GetExpectedKernelType( - ExecutionContext(*this, scope, *dev_ctx, ctx)); + ExecutionContext(*this, dummy_scope, *dev_ctx, ctx)); VLOG(3) << "expected_kernel_key:" << expected_kernel_key; auto kernel_iter = kernels.find(expected_kernel_key); @@ -999,9 +999,9 @@ void OperatorWithKernel::RunImpl(const RuntimeContext& ctx, dev_ctx = pool.Get(expected_kernel_key.place_); } - RuntimeInferShapeContext infer_shape_ctx(*this, scope, ctx); + RuntimeInferShapeContext infer_shape_ctx(*this, dummy_scope, ctx); this->InferShape(&infer_shape_ctx); - kernel_iter->second(ExecutionContext(*this, scope, *dev_ctx, ctx)); + kernel_iter->second(ExecutionContext(*this, dummy_scope, *dev_ctx, ctx)); } void OperatorWithKernel::TransferInplaceVarsBack( diff --git a/paddle/fluid/framework/operator.h b/paddle/fluid/framework/operator.h index 446d27efa04e15185a798cc7d36b6fc4e62d063e..3605bf22fc779a874e145912a07809f557e8af19 100644 --- a/paddle/fluid/framework/operator.h +++ b/paddle/fluid/framework/operator.h @@ -173,8 +173,10 @@ class OperatorBase { virtual void RunImpl(const Scope& scope, const platform::Place& place) const = 0; - virtual void RunImpl(const RuntimeContext& ctx, - const platform::Place& place) const {} + virtual void RunImplPrepared(const RuntimeContext& ctx, + const platform::Place& place) const { + PADDLE_THROW("%s doesn't support RunPreparedImpl", Type()); + } }; class ExecutionContext { @@ -466,8 +468,8 @@ class OperatorWithKernel : public OperatorBase { // same. proto::VarType::Type IndicateDataType(const ExecutionContext& ctx) const; void RunImpl(const Scope& scope, const platform::Place& place) const final; - void RunImpl(const RuntimeContext& ctx, - const platform::Place& place) const final; + void RunImplPrepared(const RuntimeContext& ctx, + const platform::Place& place) const final; /** * Transfer data from scope to a transfered scope. If there is no data need to diff --git a/paddle/fluid/imperative/layer.cc b/paddle/fluid/imperative/layer.cc index 239ff029dba8ddfdf2084a3f4f5005139c732225..7741865f9f699697057cd60f993abd8c3a269d42 100644 --- a/paddle/fluid/imperative/layer.cc +++ b/paddle/fluid/imperative/layer.cc @@ -31,11 +31,6 @@ using framework::Variable; void AddTo(Variable* src, Variable* dst) { framework::LoDTensor* dst_tensor = dst->GetMutable(); framework::LoDTensor* src_tensor = src->GetMutable(); - - VLOG(3) << "apply var grad " << src_tensor->data()[0] << " " - << src_tensor->data()[1] << " " - << src_tensor->data()[2]; - PADDLE_ENFORCE(dst_tensor->numel() == src_tensor->numel(), "%lld vs %lld", dst_tensor->numel(), src_tensor->numel()); float* dst_data = dst_tensor->mutable_data(platform::CPUPlace()); @@ -43,10 +38,6 @@ void AddTo(Variable* src, Variable* dst) { for (size_t i = 0; i < src_tensor->numel(); ++i) { dst_data[i] += src_data[i]; } - - VLOG(3) << "apply var dst grad " << dst_tensor->data()[0] << " " - << dst_tensor->data()[1] << " " - << dst_tensor->data()[2]; } class Autograd { @@ -55,16 +46,10 @@ class Autograd { void RunBackward(VarBase* var) { PADDLE_ENFORCE(var->pre_op_->op_desc_); - // TODO(panyx0718): Only create for vars that "require_grad" - LOG(ERROR) << reinterpret_cast(var->grads_) << " vs " - << reinterpret_cast( - 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() - ->ShareDataWith(var->grads_->Get()); + PADDLE_ENFORCE( + var->grads_ == + var->pre_op_->output_vars_[var->pre_op_out_name_][var->pre_op_out_idx_] + ->grads_); std::deque ready; ready.push_back(var->pre_op_); @@ -76,7 +61,6 @@ class Autograd { ready.pop_front(); std::map> input_grads = ready_op->ApplyGrad(); - VLOG(3) << "after apply grad"; for (auto it : input_grads) { const std::vector& ingrads = it.second; @@ -160,17 +144,12 @@ std::map> OpBase::ApplyGrad() { for (size_t i = 0; i < it.second.size(); ++i) { outputs.push_back(new framework::Variable()); outputs.back()->GetMutable(); - /* - auto& accum_grad_t = it.second[i]->Get(); - Variable* grad_var = outputs.back(); - float* data = grad_var->GetMutable() - ->mutable_data(accum_grad_t.dims(), platform::CPUPlace()); - std::fill(data, data + accum_grad_t.numel(), 0.0);*/ } } framework::RuntimeContext ctx(grad_input_vars_, grad_outputs); + // No need to do static infer shape here. // grad_op_desc_->InferShape(*block_); grad_op_desc_->InferVarType(block_); @@ -184,7 +163,6 @@ std::map> OpBase::ApplyGrad() { 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]; } } return input_vars_; diff --git a/paddle/fluid/imperative/tracer.h b/paddle/fluid/imperative/tracer.h index e7a60621cd559781a8f043b1e25c81b88a72141b..6b2e97873759da88e4b0e19b8e2c919c37a1f30b 100644 --- a/paddle/fluid/imperative/tracer.h +++ b/paddle/fluid/imperative/tracer.h @@ -20,7 +20,6 @@ #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" @@ -53,19 +52,14 @@ class Tracer { public: explicit Tracer(framework::BlockDesc* root_block, framework::BlockDesc* startup_block) - : root_block_(root_block), startup_block_(startup_block) { - root_scope_ = new framework::Scope(); - scopes_[root_block_] = root_scope_; - scopes_[startup_block_] = root_scope_; - } + : root_block_(root_block), startup_block_(startup_block) {} - virtual ~Tracer() { delete root_scope_; } + virtual ~Tracer() {} void Trace(OpBase* op, const std::map>& inputs, const std::map>& outputs, framework::BlockDesc* block) { - // framework::Scope* scope = GetScope(block); std::map vars; framework::OpDesc* op_desc = op->op_desc_; @@ -94,8 +88,7 @@ class Tracer { (*op->pre_ops_)[it.first].push_back(nullptr); } VLOG(3) << "input vname " << inp->var_desc_->Name() << " " - << inp->var_->Get().dims().size() - << reinterpret_cast(inp->var_); + << inp->var_->IsInitialized(); } } @@ -119,8 +112,6 @@ class Tracer { out->pre_op_out_idx_ = i; VLOG(3) << "output vname " << out->var_desc_->Name() << " " - << out->var_->Get().dims().size() << " " - << reinterpret_cast(out->var_) << " " << out->var_->IsInitialized(); } } @@ -167,7 +158,6 @@ class Tracer { if (!var->grads_->IsInitialized()) { InitVar(var->var_, var->grads_); } - LOG(ERROR) << grad_outvar << " map to " << var->var_desc_->Name(); grad_out_vars.push_back(var->grads_); } } @@ -175,22 +165,9 @@ class Tracer { 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 scopes_; framework::BlockDesc* root_block_; framework::BlockDesc* startup_block_; - framework::Scope* root_scope_; }; } // namespace imperative diff --git a/paddle/fluid/operators/fill_constant_op.cc b/paddle/fluid/operators/fill_constant_op.cc index 7b04c5d21f4390886582b70ec8fd0206466c7aa1..d10fb1214c7f2c2b21d16af720e6a206a43517bb 100644 --- a/paddle/fluid/operators/fill_constant_op.cc +++ b/paddle/fluid/operators/fill_constant_op.cc @@ -69,8 +69,8 @@ class FillConstantOp : public framework::OperatorBase { math::set_constant(dev_ctx, tensor, value); } - void RunImpl(const framework::RuntimeContext &ctx, - const platform::Place &dev_place) const override { + void RunImplPrepared(const framework::RuntimeContext &ctx, + const platform::Place &dev_place) const override { auto data_type = static_cast(Attr("dtype")); auto value = Attr("value"); diff --git a/paddle/fluid/pybind/imperative.cc b/paddle/fluid/pybind/imperative.cc index be63fb877869b64035207342e5d4398e481dbc99..7f9d937981ae83894aab91cefafccdbbc73918af 100644 --- a/paddle/fluid/pybind/imperative.cc +++ b/paddle/fluid/pybind/imperative.cc @@ -28,9 +28,7 @@ void BindTracer(pybind11::module *m) { framework::BlockDesc *startup_block) { new (&self) imperative::Tracer(root_block, startup_block); }) - .def("trace", &imperative::Tracer::Trace) - .def("get_scope", &imperative::Tracer::GetScope, - pybind11::return_value_policy::reference); + .def("trace", &imperative::Tracer::Trace); } } // namespace pybind diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index d83e2735ffe52cf3ef74ed8d17d500a08ff807cb..cc1fdbd285611379cc4fa44d2373748aa6e24faf 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -20,7 +20,6 @@ from __future__ import print_function import numpy as np import six import os -import sys import inspect from ..layer_helper import LayerHelper from ..initializer import Normal, Constant @@ -9683,7 +9682,6 @@ class FC(layers.PyLayer): shape=param_shape, dtype=self._dtype, is_bias=False) - sys.stderr.write('created w: %s\n' % self._w.name) def forward(self, inputs): tmp = self._helper.create_variable_for_type_inference(self._dtype)