未验证 提交 ac854670 编写于 作者: H hong 提交者: GitHub

Add dygraph execution context (#20157)

* add_dygraph_execution_context

* add dygraph infershape context and execution context; test=develop

* fix imperative bug; test=develop

* remove inputs outputs interface from execution context,
because it have same function with inputNames;
test=develop

* remove tracer_test ctest; test=develop

* fix split op bug; test=develop

* fix unitests bug; test=develop

* fix distribute test bug; test=develop

* fix ngraph compile bug; test=develop

* fix grad maker bug; test=develop

* fix load op bugs; test=develop

* fix operator.cc construct bug; test=develop

* remove useless name find in operator; test=develop

* add tracer_test; test=develop

* fix concat, split bug; test=develop

* remove tracer_test unitest; test=develop

* fix attribute check bug; test=develop

* add test code to fix converage; test=develop

* remove useless code, change check backward input in engin; test=develop

* unlock var type infer shape;test=develop

* add ShareAllLoD api; test=develop

* add dygraph infershape context unitest; test=develop

* remove increase and decrease lod in dygraph; test=develop

* addd override; test=develop

* fix increase descrease lod; test=develop

* fix paddle_enforce; test=develop

* disable lod op dygraph check; test=develop

* fix paddle enforce error; test=develop

* add comment for op_registry and OperatorBase; test=develop

* optimize the comment of op_registry; test=develop

* fix format of comment; test=develop

* fix format of comment; test=develop

* optimize the format of comment; test=develop

* optimize the format of the comment; test=develop

* optimize comment of op_registry; test=develop
上级 a6b089c6
......@@ -220,7 +220,7 @@ class DefaultValueSetter {
public:
explicit DefaultValueSetter(T default_value)
: default_value_(default_value) {}
void operator()(T* value) const { *value = default_value_; }
const T& operator()() const { return default_value_; }
private:
T default_value_;
......@@ -259,7 +259,7 @@ class EnumInContainer {
// an attribute can have more than one limits
template <typename T>
class TypedAttrChecker {
typedef std::function<void(T*)> DefaultValueChecker;
typedef std::function<const T&()> DefaultValueChecker;
typedef std::function<void(const T&)> ValueChecker;
public:
......@@ -297,18 +297,17 @@ class TypedAttrChecker {
}
void operator()(AttributeMap* attr_map) const {
if (!attr_map->count(attr_name_)) {
auto it = attr_map->find(attr_name_);
if (it == attr_map->end()) {
// user do not set this attr
PADDLE_ENFORCE(!default_value_setter_.empty(),
"Attribute '%s' is required!", attr_name_);
// default_value_setter_ has no more than one element
T val;
(default_value_setter_[0])(&val);
(*attr_map)[attr_name_] = val;
attr_map->emplace(attr_name_, default_value_setter_[0]());
}
Attribute& attr = attr_map->at(attr_name_);
it = attr_map->find(attr_name_);
ExtractAttribute<T> extract_attr(attr_name_);
T* attr_value = extract_attr(attr);
T* attr_value = extract_attr(it->second);
for (const auto& checker : value_checkers_) {
checker(*attr_value);
}
......
......@@ -64,7 +64,7 @@ template <typename DeviceContext, typename T>
class TestKernel : public OpKernel<float> {
public:
void Compute(const ExecutionContext& ctx) const {
std::cout << ctx.op().DebugString() << std::endl;
std::cout << ctx.DebugString() << std::endl;
const Tensor* input = ctx.Input<Tensor>("input");
......
......@@ -47,18 +47,16 @@ class CompileTimeInferShapeContext : public InferShapeContext {
AttrReader Attrs() const override;
const std::vector<std::string> &Inputs(
const std::string &name) const override;
std::vector<std::string> Inputs(const std::string &name) const override;
const std::vector<std::string> &Outputs(
const std::string &name) const override;
std::vector<std::string> Outputs(const std::string &name) const override;
void ShareDim(const std::string &in, const std::string &out, size_t i = 0,
size_t j = 0) override {
PADDLE_ENFORCE_LT(i, Inputs(in).size());
PADDLE_ENFORCE_LT(j, Outputs(out).size());
const std::string &input_n = Inputs(in)[i];
const std::string &output_n = Outputs(out)[j];
std::string input_n = Inputs(in)[i];
std::string output_n = Outputs(out)[j];
PADDLE_ENFORCE(input_n != framework::kEmptyVarName, "The %s[%d] is @EMPTY@",
in, i);
......@@ -74,6 +72,33 @@ class CompileTimeInferShapeContext : public InferShapeContext {
SetDim(output_n, GetDim(input_n));
}
void ShareAllLoD(const std::string &in,
const std::string &out) const override {
auto &in_var_names = op_.Input(in);
auto &out_var_names = op_.Output(out);
PADDLE_ENFORCE_EQ(
in_var_names.size(), out_var_names.size(),
platform::errors::PreconditionNotMet(
"Op [%s]: Input var number shoule be equal with output var number",
op_.Type()));
for (size_t i = 0; i < in_var_names.size(); ++i) {
if (out_var_names[i] == framework::kEmptyVarName) {
continue;
}
auto *in_var = block_.FindVarRecursive(in_var_names[i]);
auto *out_var = block_.FindVarRecursive(out_var_names[i]);
if (in_var->GetType() != proto::VarType::LOD_TENSOR &&
in_var->GetType() != proto::VarType::LOD_TENSOR_ARRAY) {
VLOG(3) << "input " << in << " is not LoDTensor or LoDTensorArray.";
return;
}
out_var->SetLoDLevel(in_var->GetLoDLevel());
}
}
void ShareLoD(const std::string &in, const std::string &out, size_t i = 0,
size_t j = 0) const override {
PADDLE_ENFORCE_LT(i, Inputs(in).size());
......@@ -173,7 +198,7 @@ class CompileTimeInferShapeContext : public InferShapeContext {
}
void SetOutputDim(const std::string &name, const DDim &dim) override {
auto &arg_names = Outputs(name);
auto arg_names = Outputs(name);
PADDLE_ENFORCE_EQ(arg_names.size(), 1UL,
"Output(%s) should hold one element, but now it holds %d",
name, arg_names.size());
......@@ -182,7 +207,7 @@ class CompileTimeInferShapeContext : public InferShapeContext {
void SetOutputsDim(const std::string &name,
const std::vector<DDim> &dims) override {
auto &names = Outputs(name);
auto names = Outputs(name);
SetDims(names, dims);
}
......@@ -789,12 +814,12 @@ AttrReader CompileTimeInferShapeContext::Attrs() const {
return AttrReader(op_.GetAttrMap());
}
const std::vector<std::string> &CompileTimeInferShapeContext::Inputs(
std::vector<std::string> CompileTimeInferShapeContext::Inputs(
const std::string &name) const {
return op_.Input(name);
}
const std::vector<std::string> &CompileTimeInferShapeContext::Outputs(
std::vector<std::string> CompileTimeInferShapeContext::Outputs(
const std::string &name) const {
return op_.Output(name);
}
......
......@@ -21,9 +21,9 @@ namespace framework {
std::unique_ptr<OperatorBase> OpRegistry::CreateOp(
const std::string& type, const VariableNameMap& inputs,
const VariableNameMap& outputs, AttributeMap attrs) {
const VariableNameMap& outputs, AttributeMap attrs, bool attr_check) {
auto& info = OpInfoMap::Instance().Get(type);
if (info.Checker() != nullptr) {
if (attr_check && info.Checker() != nullptr) {
info.Checker()->Check(&attrs);
}
auto op = info.Creator()(type, inputs, outputs, attrs);
......
......@@ -67,10 +67,34 @@ struct OperatorRegistrar : public Registrar {
class OpRegistry {
public:
/**
* @brief Return an OperatorBase constructed by type, inputs, outputs, attrs.
* In dygraph mode, inputs, output, attrs will be set to empty map to
* improve the execution efficiency of dygraph.
* Dygraph mode will use:
* framework::OpRegistry::CreateOp(type, {}, {}, {}, false).
*
* @param[str] type The operator type.
* @param[map] inputs Inputs map of the operator.
* @param[map] outputs Outputs map of the operator.
* @param[unordered_map] attrs Attributes map of the operator.
* @param[bool] attr_check
* Whether do the attribute check before OperatorBase construction.
* Default is true.
* Attr_check is used to control the check of attribute map.
* The check of attribute map have two purposes:
* 1. check whether the attribute item is valid or not.
* 2. add attribute item which has default value
* if it is not in attrs.
* In dygraph mode, attrs is an empty unordered_map,
* attr_check is set to false, otherwise it will be failed
* when check function called.
*/
static std::unique_ptr<OperatorBase> CreateOp(const std::string& type,
const VariableNameMap& inputs,
const VariableNameMap& outputs,
AttributeMap attrs);
AttributeMap attrs,
bool attr_check = true);
static std::unique_ptr<OperatorBase> CreateOp(const proto::OpDesc& op_desc);
......
......@@ -321,8 +321,14 @@ OperatorBase::OperatorBase(const std::string& type,
attrs_(attrs),
// NOTE(zjl): why op_info may be nullptr?
info_(OpInfoMap::Instance().GetNullable(type)) {
GenerateTemporaryNames();
CheckAllInputOutputSet();
// In dygraph mode, all the OperatorBase will be constructed by function:
// framework::OpRegistry::CreateOp(type, {}, {}, {}, false).
// Inputs, outputs and attrs will be set to empty map
// to improve the execution efficiency of dygraph.
if (inputs_.size() > 0 || outputs_.size() > 0) {
GenerateTemporaryNames();
CheckAllInputOutputSet();
}
}
std::vector<std::string> OperatorBase::InputVars() const {
......@@ -457,15 +463,14 @@ const Tensor* ExecutionContext::Input<Tensor>(const std::string& name) const {
template <>
const std::vector<const Tensor*> ExecutionContext::MultiInput<Tensor>(
const std::string& name) const {
auto it = ctx_.inputs.find(name);
if (it == ctx_.inputs.end()) {
auto vars = MultiInputVar(name);
if (vars.size() == 0) {
return {};
}
const std::vector<Variable*>& vars = it->second;
std::vector<const Tensor*> res;
res.reserve(vars.size());
std::transform(vars.begin(), vars.end(), std::back_inserter(res),
[&](Variable* var) -> const Tensor* {
[&](const Variable* var) -> const Tensor* {
if (var == nullptr) return nullptr;
PADDLE_ENFORCE(
var->IsType<LoDTensor>(),
......@@ -484,11 +489,11 @@ Tensor* ExecutionContext::Output<Tensor>(const std::string& name) const {
template <>
std::vector<Tensor*> ExecutionContext::MultiOutput<Tensor>(
const std::string& name) const {
auto it = ctx_.outputs.find(name);
if (it == ctx_.outputs.end()) {
auto vars = MultiOutputVar(name);
if (vars.size() == 0) {
return {};
}
const std::vector<Variable*>& vars = it->second;
std::vector<Tensor*> res;
res.reserve(vars.size());
std::transform(vars.begin(), vars.end(), std::back_inserter(res),
......@@ -580,13 +585,11 @@ class RuntimeInferShapeContext : public InferShapeContext {
AttrReader Attrs() const override { return AttrReader(op_.Attrs()); }
const std::vector<std::string>& Inputs(
const std::string& name) const override {
std::vector<std::string> Inputs(const std::string& name) const override {
return op_.Inputs(name);
}
const std::vector<std::string>& Outputs(
const std::string& name) const override {
std::vector<std::string> Outputs(const std::string& name) const override {
return op_.Outputs(name);
}
......@@ -622,6 +625,51 @@ class RuntimeInferShapeContext : public InferShapeContext {
}
}
void ShareAllLoD(const std::string& in,
const std::string& out) const override {
auto in_it = ctx_.inputs.find(in);
auto out_it = ctx_.outputs.find(out);
PADDLE_ENFORCE_NE(in_it, ctx_.inputs.end(),
platform::errors::NotFound(
"Input [%s] found error in Op [%s]", in, op_.Type()));
PADDLE_ENFORCE_NE(
out_it, ctx_.outputs.end(),
platform::errors::NotFound("Output [%s] found error in Op [%s]", out,
op_.Type()));
auto& in_var_list = in_it->second;
auto& out_var_list = out_it->second;
PADDLE_ENFORCE_EQ(
in_var_list.size(), out_var_list.size(),
platform::errors::PreconditionNotMet(
"Op [%s]: Input var size should be equal with ouput var size",
op_.Type()));
auto& out_var_names = op_.Outputs(out);
for (size_t i = 0; i < in_var_list.size(); ++i) {
if (out_var_names[i] == framework::kEmptyVarName) {
continue;
}
Variable* in_var = in_var_list[i];
if (!in_var->IsType<LoDTensor>()) return;
Variable* out_var = out_var_list[i];
PADDLE_ENFORCE_EQ(out_var->IsType<LoDTensor>(), true,
platform::errors::PreconditionNotMet(
"The %d-th output of Output(%s) must be LoDTensor.",
i, out_var_names[i]));
auto& in_tensor = in_var->Get<LoDTensor>();
auto* out_tensor = out_var->GetMutable<LoDTensor>();
out_tensor->set_lod(in_tensor.lod());
#ifdef PADDLE_WITH_MKLDNN
if (in_tensor.layout() != DataLayout::kMKLDNN)
#endif
out_tensor->set_layout(in_tensor.layout());
}
}
void ShareLoD(const std::string& in, const std::string& out, size_t i = 0,
size_t j = 0) const override {
auto in_it = ctx_.inputs.find(in);
......@@ -1138,7 +1186,7 @@ void OperatorWithKernel::ParseInputDataType(
proto::VarType::Type* data_type) const {
proto::VarType::Type dafault_data_type =
static_cast<proto::VarType::Type>(-1);
const std::vector<const Variable*> vars = ctx.MultiInputVar(name);
const std::vector<Variable*> vars = ctx.MultiInputVar(name);
for (size_t i = 0; i < vars.size(); ++i) {
const Variable* var = vars[i];
if (var != nullptr) {
......@@ -1156,7 +1204,7 @@ void OperatorWithKernel::ParseInputDataType(
platform::errors::InvalidArgument(
"The Tensor in the %s Op's Input Variable %s(%s) is "
"not initialized.",
Type(), name, ctx.Inputs(name).at(i)));
Type(), name, ctx.InputNames(name).at(i)));
proto::VarType::Type tmp = t->type();
PADDLE_ENFORCE(
tmp == *data_type || *data_type == dafault_data_type,
......@@ -1177,8 +1225,8 @@ proto::VarType::Type OperatorWithKernel::IndicateDataType(
proto::VarType::Type dafault_data_type =
static_cast<proto::VarType::Type>(-1);
proto::VarType::Type data_type = dafault_data_type;
for (auto& input : ctx.Context().inputs) {
ParseInputDataType(ctx, input.first, &data_type);
for (auto& input : ctx.InNameList()) {
ParseInputDataType(ctx, input, &data_type);
}
PADDLE_ENFORCE_NE(data_type, dafault_data_type,
"DataType should be indicated by input Variable.");
......
......@@ -238,35 +238,57 @@ class ExecutionContext {
device_context_(device_context),
ctx_(ctx),
kernel_configs_(configs) {}
virtual ~ExecutionContext() {}
const OperatorBase& op() const { return op_; }
virtual std::string InputName(const std::string& name) const {
return op_.Input(name);
}
virtual std::vector<std::string> InputNames(const std::string& name) const {
return op_.Inputs(name);
}
virtual std::string OutputName(const std::string& name) const {
return op_.Output(name);
}
virtual std::vector<std::string> OutputNames(const std::string& name) const {
return op_.Outputs(name);
}
virtual bool HasAttr(const std::string& name) const {
return op_.HasAttr(name);
}
virtual const AttributeMap& Attrs() const { return op_.Attrs(); }
const std::string& Type() const { return op_.Type(); }
const Scope& scope() const { return scope_; }
template <typename T>
inline const T& Attr(const std::string& name) const {
return op_.Attr<T>(name);
return boost::get<T>(GetAttr(name));
}
bool HasAttr(const std::string& name) const { return op_.HasAttr(name); }
virtual const Attribute& GetAttr(const std::string& name) const {
return op_.Attrs().at(name);
}
bool HasInput(const std::string& name) const;
virtual bool HasInput(const std::string& name) const;
bool HasOutput(const std::string& name) const;
virtual bool HasOutput(const std::string& name) const;
size_t InputSize(const std::string& name) const {
virtual size_t InputSize(const std::string& name) const {
return op_.Inputs(name).size();
}
size_t OutputSize(const std::string& name) const {
virtual size_t OutputSize(const std::string& name) const {
return op_.Outputs(name).size();
}
const Variable* InputVar(const std::string& name) const;
virtual const Variable* InputVar(const std::string& name) const;
Variable* OutputVar(const std::string& name) const;
virtual Variable* OutputVar(const std::string& name) const;
const std::vector<const Variable*> MultiInputVar(
virtual const std::vector<Variable*> MultiInputVar(
const std::string& name) const {
auto it = ctx_.inputs.find(name);
if (it == ctx_.inputs.end()) {
......@@ -275,8 +297,7 @@ class ExecutionContext {
return {it->second.begin(), it->second.end()};
}
std::vector<Variable*> MultiOutputVar(const std::string& name) const {
auto names = op_.Outputs(name);
virtual std::vector<Variable*> MultiOutputVar(const std::string& name) const {
auto it = ctx_.outputs.find(name);
if (it == ctx_.outputs.end()) {
return {};
......@@ -284,6 +305,17 @@ class ExecutionContext {
return it->second;
}
virtual std::vector<std::string> InNameList() const {
std::vector<std::string> vec_temp;
vec_temp.reserve(ctx_.inputs.size());
for (auto& input : ctx_.inputs) {
vec_temp.push_back(input.first);
}
return vec_temp;
}
template <typename T>
const T* Input(const std::string& name) const {
auto* var = InputVar(name);
......@@ -298,15 +330,14 @@ class ExecutionContext {
template <typename T>
const std::vector<const T*> MultiInput(const std::string& name) const {
auto it = ctx_.inputs.find(name);
if (it == ctx_.inputs.end()) {
auto vars = MultiInputVar(name);
if (vars.size() == 0) {
return {};
}
const std::vector<Variable*>& vars = it->second;
std::vector<const T*> res;
res.reserve(vars.size());
std::transform(vars.begin(), vars.end(), std::back_inserter(res),
[&](Variable* var) -> const T* {
[&](const Variable* var) -> const T* {
return var == nullptr ? nullptr : &var->Get<T>();
});
return res;
......@@ -314,17 +345,19 @@ class ExecutionContext {
template <typename T>
std::vector<T*> MultiOutput(const std::string& name) const {
auto it = ctx_.outputs.find(name);
if (it == ctx_.outputs.end()) {
auto vars = MultiOutputVar(name);
if (vars.size() == 0) {
return {};
}
const std::vector<Variable*>& vars = it->second;
std::vector<T*> res;
res.reserve(vars.size());
std::transform(vars.begin(), vars.end(), std::back_inserter(res),
[&](Variable* var) -> T* {
return var == nullptr ? nullptr : var->GetMutable<T>();
});
return res;
}
......@@ -347,16 +380,6 @@ class ExecutionContext {
}
#endif
//! Get actual name vector for this input.
const std::vector<std::string>& Inputs(const std::string& name) const {
return op_.Inputs(name);
}
//! Get actual name vector for this output.
const std::vector<std::string>& Outputs(const std::string& name) const {
return op_.Outputs(name);
}
template <typename T, typename DevContext>
Tensor AllocateTmpTensor(const framework::DDim& dim,
const DevContext& dev_ctx) const {
......@@ -385,7 +408,9 @@ class ExecutionContext {
return *boost::get<std::shared_ptr<T>>((*kernel_configs_)[idx]);
}
const RuntimeContext& Context() const { return ctx_; }
const RuntimeContext Context() const { return ctx_; }
std::string DebugString() const { return op_.DebugString(); }
private:
const OperatorBase& op_;
......
......@@ -135,10 +135,10 @@ template <typename T1, typename T2>
class CPUKernelTest : public OpKernel<float> {
public:
void Compute(const ExecutionContext& ctx) const {
std::cout << ctx.op().DebugString() << std::endl;
std::cout << ctx.DebugString() << std::endl;
cpu_kernel_run_num++;
ASSERT_EQ(ctx.op().Input("x"), "IN1");
ASSERT_EQ(ctx.op().Output("y"), "OUT1");
ASSERT_EQ(ctx.InputName("x"), "IN1");
ASSERT_EQ(ctx.OutputName("y"), "OUT1");
}
};
......@@ -146,10 +146,10 @@ template <typename T1, typename T2>
class CPUKernel2Test : public OpKernel<float> {
public:
void Compute(const ExecutionContext& ctx) const {
std::cout << ctx.op().DebugString() << std::endl;
std::cout << ctx.DebugString() << std::endl;
cpu_kernel2_run_num++;
ASSERT_EQ(ctx.op().Input("x"), "IN1");
ASSERT_EQ(ctx.op().Output("y"), "OUT1");
ASSERT_EQ(ctx.InputName("x"), "IN1");
ASSERT_EQ(ctx.OutputName("y"), "OUT1");
}
};
......@@ -172,7 +172,7 @@ class OpKernelTestMultiInputsProtoAndCheckerMaker
class CPUKernalMultiInputsTest : public OpKernel<float> {
public:
void Compute(const ExecutionContext& ctx) const {
auto xs = ctx.op().Inputs("xs");
auto xs = ctx.InputNames("xs");
ASSERT_EQ(xs.size(), 3UL);
ASSERT_EQ(xs[0], "x0");
ASSERT_EQ(xs[1], "x1");
......@@ -196,10 +196,10 @@ class CPUKernalMultiInputsTest : public OpKernel<float> {
auto outTensor0 = ctx.MultiOutput<Tensor>("ys");
ASSERT_EQ(outTensor0.size(), 2U);
auto k = ctx.op().Input("k");
auto k = ctx.InputName("k");
ASSERT_EQ(k, "k0");
auto ys = ctx.op().Outputs("ys");
auto ys = ctx.OutputNames("ys");
ASSERT_EQ(ys.size(), 2UL);
ASSERT_EQ(ys[0], "y0");
ASSERT_EQ(ys[1], "y1");
......@@ -496,6 +496,41 @@ TEST(IndicateVarDataTypeTest, other) {
ASSERT_TRUE(caught);
}
TEST(ExecutionContextAttrAndInOut, new_api) {
paddle::framework::InitDevices(true);
paddle::framework::proto::OpDesc op_desc;
op_desc.set_type("test_operator");
BuildVar("input", {"IN1"}, op_desc.add_inputs());
BuildVar("output", {"OUT1"}, op_desc.add_outputs());
auto attr = op_desc.mutable_attrs()->Add();
attr->set_name("scale");
attr->set_type(paddle::framework::proto::AttrType::FLOAT);
attr->set_f(3.14);
paddle::platform::CPUPlace cpu_place;
paddle::framework::Scope scope;
auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
auto* var = scope.Var("OUT1");
var->GetMutable<paddle::framework::LoDTensorArray>();
paddle::platform::DeviceContextPool& pool =
paddle::platform::DeviceContextPool::Instance();
auto* dev_ctx = pool.Get(cpu_place);
paddle::framework::RuntimeContext ctx({}, {});
paddle::framework::ExecutionContext exe_context(*(op.get()), scope, *dev_ctx,
ctx, nullptr);
ASSERT_EQ(exe_context.InputSize("input"), 1u);
ASSERT_EQ(exe_context.OutputSize("output"), 1u);
auto attr_map = exe_context.Attrs();
ASSERT_EQ(boost::get<float>(attr_map["scale"]), 3.14f);
ASSERT_EQ(exe_context.Type(), "test_operator");
}
namespace paddle {
namespace framework {
......
......@@ -54,16 +54,18 @@ class InferShapeContext {
const std::vector<DDim> &dims);
virtual AttrReader Attrs() const = 0;
virtual const std::vector<std::string> &Inputs(
const std::string &name) const = 0;
virtual const std::vector<std::string> &Outputs(
const std::string &name) const = 0;
virtual std::vector<std::string> Inputs(const std::string &name) const = 0;
virtual std::vector<std::string> Outputs(const std::string &name) const = 0;
virtual void ShareDim(const std::string &in, const std::string &out,
size_t i = 0, size_t j = 0) = 0;
virtual void ShareLoD(const std::string &in, const std::string &out,
size_t i = 0, size_t j = 0) const = 0;
// share the lod information of all the tensor from in to out.
// out_vars[i].lod = in_vars[i].lod
virtual void ShareAllLoD(const std::string &in,
const std::string &out) const = 0;
virtual int32_t GetLoDLevel(const std::string &in, size_t i = 0) const = 0;
......
......@@ -152,8 +152,6 @@ void BasicEngine::PrepareDeps() {
q.pop();
VLOG(3) << "Checking grads of op " << cur_op->Type();
CheckBackwardInputs(cur_op);
SetBackwardOutputs(cur_op);
PrepareGradAccumulators(cur_op);
......@@ -189,6 +187,9 @@ void BasicEngine::Execute() {
OpBase* cur_op = q.front();
q.pop();
// CheckBackWardInput
CheckBackwardInputs(cur_op);
// Step 1: Run Backward
auto& bwd_ins = cur_op->GetInsMap();
auto& bwd_outs = cur_op->GetOutsMap();
......@@ -210,7 +211,6 @@ void BasicEngine::Execute() {
}
}
}
VLOG(3) << "Start to execute grad op " << cur_op->Type();
RunOp(cur_op, bwd_ins, tmp_outs, cur_op->place());
// Step 2: Sum Gradient
......
......@@ -190,6 +190,7 @@ void VarBase::AddGradOps(const std::weak_ptr<OpBase>& op) {
void VarBase::ClearGradient() {
if (grad_var_) {
auto* grad_t = grad_var_->var_.GetMutable<framework::LoDTensor>();
if (grad_t->IsInitialized()) {
auto* dev_ctx =
platform::DeviceContextPool::Instance().Get(grad_t->place());
......@@ -241,18 +242,9 @@ OpBase::OpBase(size_t id, const std::string& type, const NameVarBaseMap& ins,
info.Checker()->Check(&attrs_);
}
auto input_name_map = CreateVarNameMap(info, type, ins, true);
auto output_name_map = CreateVarNameMap(info, type, outs, false);
op_ = framework::OpRegistry::CreateOp(type, std::move(input_name_map),
std::move(output_name_map), attrs);
VLOG(3) << "Construct Op: " << type << std::endl;
}
op_ = framework::OpRegistry::CreateOp(type, {}, {}, {}, false);
// create OpBase from opdesc
OpBase::OpBase(size_t id, const framework::OpDesc& op_desc,
const platform::Place& place)
: id_(id), op_(framework::OpRegistry::CreateOp(op_desc)), place_(place) {
VLOG(3) << "Construct Op: " << op_desc.Type() << std::endl;
VLOG(3) << "Construct Op: " << type << std::endl;
}
void OpBase::CreateOperatorBase() {
......@@ -260,11 +252,7 @@ void OpBase::CreateOperatorBase() {
if (info.Checker() != nullptr) {
info.Checker()->Check(&attrs_);
}
auto input_name_map = CreateVarNameMap(info, type_, ins_, true);
auto output_name_map = CreateVarNameMap(info, type_, outs_, false);
op_ = framework::OpRegistry::CreateOp(type_, std::move(input_name_map),
std::move(output_name_map), attrs_);
op_ = framework::OpRegistry::CreateOp(type_, {}, {}, {}, false);
}
void OpBase::Run(const NameVarBaseMap& ins, const NameVarBaseMap& outs) {
......@@ -272,10 +260,9 @@ void OpBase::Run(const NameVarBaseMap& ins, const NameVarBaseMap& outs) {
PADDLE_ENFORCE_NOT_NULL(op_kernel, "only support op with kernel");
auto& info = op_->Info();
if (info.infer_var_type_) {
RuntimeInferVarTypeContext infer_var_type_ctx(ins, &outs, op_->Attrs());
RuntimeInferVarTypeContext infer_var_type_ctx(ins, &outs, attrs_);
info.infer_var_type_(&infer_var_type_ctx);
}
// Initialize output var type
for (auto& var_pair : outs) {
for (auto& var : var_pair.second) {
......@@ -285,13 +272,11 @@ void OpBase::Run(const NameVarBaseMap& ins, const NameVarBaseMap& outs) {
VLOG(3) << "Running Op " << Type();
VLOG(5) << LayerDebugString(Type(), ins, outs);
auto runtime_ctx = PrepareRuntimeContext(ins, outs);
VLOG(6) << "start preparing op: " << Type();
auto prepared_op = PreparedOp::Prepare(runtime_ctx, *op_kernel, place(), ins);
framework::RuntimeContext runtime_ctx({}, {});
auto prepared_op =
PreparedOp::Prepare(ins, outs, *op_kernel, place(), &attrs_);
VLOG(6) << "finish preparing op: " << Type();
prepared_op.Run();
prepared_op.Run(&ins, &outs, &attrs_);
VLOG(4) << LayerDebugString(Type(), ins, outs);
}
......
......@@ -27,6 +27,10 @@
#include <vector>
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/shape_inference.h"
#include "paddle/fluid/framework/type_defs.h"
#include "paddle/fluid/framework/var_desc.h"
#include "paddle/fluid/framework/var_type.h"
#include "paddle/fluid/framework/var_type_inference.h"
#include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/imperative/flags.h"
......@@ -209,6 +213,160 @@ class Layer {
}
};
class DygraphExecutionContext : public framework::ExecutionContext {
using Variable = framework::Variable;
public:
DygraphExecutionContext(const framework::OperatorBase& op,
const framework::Scope& scope,
const platform::DeviceContext& device_context,
const framework::RuntimeContext& ctx,
std::vector<framework::KernelConfig>* configs,
const NameVarBaseMap& var_base_map_in,
const NameVarBaseMap& var_base_map_out,
const framework::AttributeMap* attrs)
: ExecutionContext(op, scope, device_context, ctx, configs),
var_base_map_in_(var_base_map_in),
var_base_map_out_(var_base_map_out),
attrs_(attrs) {}
std::string InputName(const std::string& name) const {
auto it = var_base_map_in_.find(name);
PADDLE_ENFORCE_NE(it, var_base_map_in_.end(),
platform::errors::PreconditionNotMet(
"Can not find [%s] in Input", name));
return it->second[0]->Name();
}
std::vector<std::string> InputNames(const std::string& name) const {
auto it = var_base_map_in_.find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_in_.end(),
platform::errors::NotFound("Can not find [%s] in Input", name));
std::vector<std::string> vec_res;
vec_res.reserve(it->second.size());
for (size_t i = 0; i < it->second.size(); ++i) {
vec_res.push_back(it->second[i]->Name());
}
return vec_res;
}
std::string OuputName(const std::string& name) const {
auto it = var_base_map_out_.find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_out_.end(),
platform::errors::NotFound("Can not find [%s] in Output", name));
return it->second[0]->Name();
}
std::vector<std::string> OutputNames(const std::string& name) const {
auto it = var_base_map_out_.find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_out_.end(),
platform::errors::NotFound("Can not find [%s] in Output", name));
std::vector<std::string> vec_res;
vec_res.reserve(it->second.size());
for (size_t i = 0; i < it->second.size(); ++i) {
vec_res.push_back(it->second[i]->Name());
}
return vec_res;
}
bool HasAttr(const std::string& name) const { return attrs_->count(name); }
const framework::AttributeMap& Attrs() const { return *attrs_; }
const framework::Attribute& GetAttr(const std::string& name) const {
auto it = attrs_->find(name);
PADDLE_ENFORCE_NE(
it, attrs_->end(),
platform::errors::NotFound("can not find [%s] in attrs", name));
return it->second;
}
std::vector<std::string> InNameList() const {
std::vector<std::string> vec_temp;
vec_temp.reserve(var_base_map_in_.size());
for (auto& v : var_base_map_in_) {
vec_temp.push_back(v.first);
}
return vec_temp;
}
bool HasInput(const std::string& name) const {
auto it = var_base_map_in_.find(name);
return (it != var_base_map_in_.end() && it->second.size() > 0);
}
virtual bool HasOutput(const std::string& name) const {
auto it = var_base_map_out_.find(name);
return (it != var_base_map_out_.end() && it->second.size() > 0);
}
size_t InputSize(const std::string& name) const {
return InputNames(name).size();
}
size_t OutputSize(const std::string& name) const {
return OutputNames(name).size();
}
const Variable* InputVar(const std::string& name) const override {
auto it = var_base_map_in_.find(name);
if (it == var_base_map_in_.end()) {
return nullptr;
}
return it->second.empty() ? nullptr : it->second[0]->MutableVar();
}
Variable* OutputVar(const std::string& name) const {
auto it = var_base_map_out_.find(name);
if (it == var_base_map_out_.end()) {
return nullptr;
}
return it->second.empty() ? nullptr : it->second[0]->MutableVar();
}
const std::vector<Variable*> MultiInputVar(
const std::string& name) const override {
auto it = var_base_map_in_.find(name);
if (it == var_base_map_in_.end()) {
return {};
}
std::vector<Variable*> vec_res;
vec_res.reserve(it->second.size());
for (size_t i = 0; i < it->second.size(); ++i) {
vec_res.push_back(it->second[i]->MutableVar());
}
return vec_res;
}
std::vector<Variable*> MultiOutputVar(
const std::string& name) const override {
auto it = var_base_map_out_.find(name);
if (it == var_base_map_out_.end()) {
return {};
}
std::vector<Variable*> vec_res;
vec_res.reserve(it->second.size());
for (size_t i = 0; i < it->second.size(); ++i) {
vec_res.push_back(it->second[i]->MutableVar());
}
return vec_res;
}
private:
const NameVarBaseMap& var_base_map_in_;
const NameVarBaseMap& var_base_map_out_;
const framework::AttributeMap* attrs_;
};
// infer var type context for imperative mode
class RuntimeInferVarTypeContext : public framework::InferVarTypeContext {
public:
......@@ -377,7 +535,7 @@ class OpBase : public std::enable_shared_from_this<OpBase> {
return op_->Outputs();
}
const framework::AttributeMap& Attrs() const { return op_->Attrs(); }
const framework::AttributeMap& Attrs() const { return attrs_; }
const framework::OpInfo& Info() const { return op_->Info(); }
void ClearBackwardTrace();
......@@ -419,9 +577,6 @@ class OpBase : public std::enable_shared_from_this<OpBase> {
const NameVarBaseMap& outs, const framework::AttributeMap& attrs,
const platform::Place& place);
OpBase(size_t id, const framework::OpDesc& op_desc,
const platform::Place& place);
public:
OpBase() {}
......@@ -485,5 +640,318 @@ class OpBase : public std::enable_shared_from_this<OpBase> {
framework::AttributeMap attrs_;
};
class DygraphInferShapeContext : public framework::InferShapeContext {
using DDim = framework::DDim;
public:
DygraphInferShapeContext(const NameVarBaseMap* in, const NameVarBaseMap* out,
const framework::AttributeMap* attr)
: var_base_map_in_(in), var_base_map_out_(out), attrs_(attr) {}
bool HasInput(const std::string& name) const override {
// has only one input
auto it = var_base_map_in_->find(name);
if (it == var_base_map_in_->end()) {
return false;
}
const auto& in = it->second;
if (in.size() == 0) return false;
PADDLE_ENFORCE_EQ(
in.size(), 1UL,
platform::errors::PreconditionNotMet(
"Input %s should not have more than one inputs", name));
return in[0] != nullptr;
}
bool HasOutput(const std::string& name) const override {
// has only one output
auto it = var_base_map_out_->find(name);
if (it == var_base_map_out_->end()) {
return false;
}
const auto& out = it->second;
if (out.size() == 0) {
return false;
}
PADDLE_ENFORCE_EQ(
out.size(), 1UL,
platform::errors::PreconditionNotMet(
"Output %s should not have more than one outputs", name));
return out[0] != nullptr;
}
bool HasInputs(const std::string& name) const override {
auto it = var_base_map_in_->find(name);
if (it == var_base_map_in_->end() || it->second.empty()) {
return false;
}
for (auto& input : it->second) {
if (input == nullptr) {
return false;
}
}
return true;
}
bool HasOutputs(const std::string& name) const override {
auto it = var_base_map_out_->find(name);
if (it == var_base_map_out_->end() || it->second.empty()) {
return false;
}
for (auto& output : it->second) {
if (output == nullptr) {
return false;
}
}
return true;
}
framework::AttrReader Attrs() const override {
return framework::AttrReader(*attrs_);
}
std::vector<std::string> Inputs(const std::string& name) const override {
// return op_.Inputs(name);
std::vector<std::string> vec_res;
auto it = var_base_map_in_->find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_in_->end(),
platform::errors::NotFound("can not find [%s] in input", name));
vec_res.reserve(it->second.size());
for (auto& var : it->second) {
vec_res.push_back(var->Name());
}
return vec_res;
}
std::vector<std::string> Outputs(const std::string& name) const override {
std::vector<std::string> vec_res;
auto it = var_base_map_out_->find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_out_->end(),
platform::errors::NotFound("can not find [%s] in output", name));
vec_res.reserve(it->second.size());
for (auto& var : it->second) {
vec_res.push_back(var->Name());
}
return vec_res;
}
void ShareDim(const std::string& in, const std::string& out, size_t i = 0,
size_t j = 0) override {
auto in_it = var_base_map_in_->find(in);
auto out_it = var_base_map_out_->find(out);
PADDLE_ENFORCE_NE(
in_it, var_base_map_in_->end(),
platform::errors::NotFound("can not found [%s] in input", in));
PADDLE_ENFORCE_GT(in_it->second.size(), i,
platform::errors::PreconditionNotMet(
"Inputs %s should have %llu argument", in, i));
PADDLE_ENFORCE_NE(
out_it, var_base_map_out_->end(),
platform::errors::NotFound("can not found [%s] in input", in));
PADDLE_ENFORCE_GT(out_it->second.size(), j,
platform::errors::PreconditionNotMet(
"Outputs %s should have %llu argument", out, j));
framework::Variable* in_var = in_it->second[i]->MutableVar();
framework::Variable* out_var = out_it->second[j]->MutableVar();
PADDLE_ENFORCE_EQ(in_var->Type(), out_var->Type(),
platform::errors::PreconditionNotMet(
"The type of %s and %s is not the same.", in, out));
auto& in_lod_tensor = in_var->Get<framework::LoDTensor>();
auto* out_lod_tensor = out_var->GetMutable<framework::LoDTensor>();
out_lod_tensor->Resize(in_lod_tensor.dims());
}
void ShareAllLoD(const std::string& in,
const std::string& out) const override {
// do nothing
}
void ShareLoD(const std::string& in, const std::string& out, size_t i = 0,
size_t j = 0) const override {
// do nothing
}
bool IsRuntime() const override { return true; }
// TODO(paddle-dev): Can this be template?
std::vector<framework::InferShapeVarPtr> GetInputVarPtrs(
const std::string& name) override {
PADDLE_THROW(platform::errors::PermissionDenied(
"GetInputVarPtrs not support in dygraph runtime context"));
}
std::vector<framework::InferShapeVarPtr> GetOutputVarPtrs(
const std::string& name) override {
PADDLE_THROW(platform::errors::PermissionDenied(
"GetOutputVarPtrs not support in dygraph runtime context"));
}
DDim GetInputDim(const std::string& name) const override {
auto it = var_base_map_in_->find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_in_->end(),
platform::errors::NotFound("can not find [%s] in input", name));
PADDLE_ENFORCE_EQ(
it->second.size(), 1UL,
platform::errors::PreconditionNotMet(
"Input(%s) should hold one element, but now it holds %d", name,
it->second.size()));
return this->GetDim(it->second[0]->MutableVar());
}
std::vector<DDim> GetInputsDim(const std::string& name) const override {
// const std::vector<Variable*>& vars = InputVars(name);
std::vector<DDim> vec_res;
auto it = var_base_map_in_->find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_in_->end(),
platform::errors::NotFound("can not find [%s] in output", name));
vec_res.reserve(it->second.size());
for (size_t i = 0; i < it->second.size(); ++i) {
vec_res.emplace_back(GetDim(it->second[i]->MutableVar()));
}
return vec_res;
}
std::vector<framework::proto::VarType::Type> GetInputsVarType(
const std::string& name) const override {
std::vector<framework::proto::VarType::Type> vec_res;
auto it = var_base_map_in_->find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_in_->end(),
platform::errors::NotFound("can not find [%s] in input", name));
vec_res.reserve(it->second.size());
for (size_t i = 0; i < it->second.size(); ++i) {
vec_res.emplace_back(
framework::ToVarType(it->second[i]->MutableVar()->Type()));
}
return vec_res;
}
std::vector<framework::proto::VarType::Type> GetOutputsVarType(
const std::string& name) const override {
std::vector<framework::proto::VarType::Type> vec_res;
auto it = var_base_map_out_->find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_out_->end(),
platform::errors::NotFound("can not find [%s] in output", name));
vec_res.reserve(it->second.size());
for (size_t i = 0; i < it->second.size(); ++i) {
vec_res.emplace_back(
framework::ToVarType(it->second[i]->MutableVar()->Type()));
}
return vec_res;
}
void SetOutputDim(const std::string& name, const DDim& dim) override {
auto it = var_base_map_out_->find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_out_->end(),
platform::errors::NotFound("can not find [%s] in output", name));
SetDim(it->second[0]->MutableVar(), dim);
}
void SetOutputsDim(const std::string& name,
const std::vector<DDim>& dims) override {
// auto& vars = OutputVars(name);
// SetDims(vars, dims);
auto it = var_base_map_out_->find(name);
PADDLE_ENFORCE_NE(
it, var_base_map_out_->end(),
platform::errors::NotFound("can not find [%s] in output", name));
PADDLE_ENFORCE_EQ(it->second.size(), dims.size(),
platform::errors::PreconditionNotMet(
"dim size [%d] is not match output var number [%d]",
dims.size(), it->second.size()));
for (size_t i = 0; i < dims.size(); ++i) {
SetDim(it->second[i]->MutableVar(), dims[i]);
}
}
int32_t GetLoDLevel(const std::string& in, size_t i = 0) const override {
PADDLE_THROW(platform::errors::PermissionDenied(
"GetLoDLevel function not support in dygraph mode"));
}
void SetLoDLevel(const std::string& out, int32_t lod_level,
size_t j = 0) const override {
PADDLE_THROW(platform::errors::PermissionDenied(
"SetLoDLevel function not support in dygraph mode"));
}
protected:
DDim GetDim(framework::Variable* var) const {
PADDLE_ENFORCE_NOT_NULL(var, platform::errors::PreconditionNotMet(
"Input variable should not be null"));
if (var->IsType<framework::LoDTensor>()) {
return var->Get<framework::LoDTensor>().dims();
} else if (var->IsType<framework::SelectedRows>()) {
return var->Get<framework::SelectedRows>().GetCompleteDims();
} else {
PADDLE_THROW(platform::errors::PermissionDenied(
"Only LoDTensor/SelectedRows support 'GetDim', but Variables "
"type_id is xx."));
}
}
std::vector<DDim> GetRepeatedDims(const std::string& name) const override {
PADDLE_THROW(platform::errors::PermissionDenied(
"GetRepeatedDims not support in dygraph runtime"));
}
void SetDim(framework::Variable* var, const DDim& dim) {
if (var->IsType<framework::LoDTensor>()) {
var->GetMutable<framework::LoDTensor>()->Resize(dim);
} else if (var->IsType<framework::SelectedRows>()) {
var->GetMutable<framework::SelectedRows>()->set_height(dim[0]);
} else {
PADDLE_THROW(platform::errors::PermissionDenied(
"Variable type_id %s, expect LoDTensor/SelectedRows."));
}
}
void SetDims(const std::vector<framework::Variable*>& vars,
const std::vector<DDim>& dims) {
size_t length = vars.size();
PADDLE_ENFORCE_EQ(
length, dims.size(),
platform::errors::PreconditionNotMet(
"Vars number [%d] should be equal with dims number [%d]", length,
dims.size()));
for (size_t i = 0; i < length; ++i) {
if (vars[i] == nullptr) {
continue;
}
SetDim(vars[i], dims[i]);
}
}
void SetRepeatedDims(const std::string& name,
const std::vector<DDim>& dims) override {
PADDLE_THROW(platform::errors::PermissionDenied(
"SetRepeatedDims not support in dygraph runtime"));
}
private:
const NameVarBaseMap* var_base_map_in_;
const NameVarBaseMap* var_base_map_out_;
std::string type_;
const framework::AttributeMap* attrs_;
};
} // namespace imperative
} // namespace paddle
......@@ -70,10 +70,11 @@ PreparedOp::PreparedOp(const framework::OperatorBase& op,
dev_ctx_(dev_ctx),
kernel_configs_(kernel_configs) {}
PreparedOp PreparedOp::Prepare(const framework::RuntimeContext& ctx,
PreparedOp PreparedOp::Prepare(const NameVarBaseMap& ins,
const NameVarBaseMap& outs,
const framework::OperatorWithKernel& op,
platform::Place place,
const NameVarBaseMap& ins) {
const framework::AttributeMap* attrs) {
platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
auto* dev_ctx = pool.Get(place);
......@@ -88,9 +89,9 @@ PreparedOp PreparedOp::Prepare(const framework::RuntimeContext& ctx,
auto& kernels = kernels_iter->second;
auto expected_kernel_key =
op.GetExpectedKernelType(framework::ExecutionContext(
op, framework::Scope(), *dev_ctx, ctx, nullptr));
framework::RuntimeContext ctx({}, {});
auto expected_kernel_key = op.GetExpectedKernelType(DygraphExecutionContext(
op, framework::Scope(), *dev_ctx, ctx, nullptr, ins, outs, attrs));
VLOG(3) << "expected_kernel_key:" << expected_kernel_key;
auto kernel_iter = kernels.find(expected_kernel_key);
......@@ -111,13 +112,20 @@ PreparedOp PreparedOp::Prepare(const framework::RuntimeContext& ctx,
return PreparedOp(op, ctx, kernel_iter->second, dev_ctx, kernel_configs);
}
void PreparedOp::Run() {
void PreparedOp::Run(const NameVarBaseMap* in, const NameVarBaseMap* out,
const framework::AttributeMap* attrs) {
// TODO(zjl): remove scope in dygraph
framework::Scope scope;
op_.RuntimeInferShape(scope, dev_ctx_->GetPlace(), ctx_);
VLOG(6) << "Finish Runtime infer shape";
func_(framework::ExecutionContext(op_, scope, *dev_ctx_, ctx_,
kernel_configs_));
DygraphInferShapeContext infer_shape_ctx(in, out, attrs);
framework::OperatorWithKernel* op_ker =
(framework::OperatorWithKernel*)(&op_);
op_ker->InferShape(&infer_shape_ctx);
func_(DygraphExecutionContext(op_, scope, *dev_ctx_, ctx_, kernel_configs_,
*in, *out, attrs));
}
} // namespace imperative
......
......@@ -30,13 +30,16 @@ const framework::Tensor* GetTensorFromVar(const framework::Variable& var);
class PreparedOp {
public:
static PreparedOp Prepare(const framework::RuntimeContext& ctx,
static PreparedOp Prepare(const NameVarBaseMap& ins,
const NameVarBaseMap& outs,
const framework::OperatorWithKernel& op,
platform::Place place, const NameVarBaseMap& ins);
platform::Place place,
const framework::AttributeMap* attrs);
inline platform::DeviceContext* GetDeviceContext() const { return dev_ctx_; }
void Run();
void Run(const NameVarBaseMap* in, const NameVarBaseMap* out,
const framework::AttributeMap* attrs);
static void PrepareData(const platform::Place& place,
const NameVarBaseMap& ins,
......
......@@ -148,6 +148,67 @@ TEST(test_layer, test_varbase_basic) {
}
// TODO(jiabin): Add more ut here for layer
TEST(test_layer, test_dygraph_execution_context) {
std::shared_ptr<imperative::VarBase> vin(
new imperative::VarBase(false, "vin"));
std::shared_ptr<imperative::VarBase> vout(
new imperative::VarBase(false, "vout"));
framework::OpDesc desc;
platform::CPUPlace place;
var_pair x_pair = var_pair("X", vb_vector(1, vin));
var_pair y_pair = var_pair("Y", vb_vector(1, vin));
var_pair out_pair = var_pair("Out", vb_vector(1, vout));
imperative::NameVarBaseMap ins = {x_pair, y_pair};
imperative::NameVarBaseMap outs = {out_pair};
framework::AttributeMap concat_att_map;
concat_att_map["axis"] = 1;
auto op = framework::OpRegistry::CreateOp("mul", {}, {}, {}, false);
paddle::platform::CPUPlace cpu_place;
paddle::platform::DeviceContextPool& pool =
paddle::platform::DeviceContextPool::Instance();
auto* dev_ctx = pool.Get(cpu_place);
paddle::framework::RuntimeContext ctx({}, {});
framework::Scope scope;
DygraphExecutionContext dy_exe_context(*(op.get()), scope, *dev_ctx, ctx,
nullptr, ins, outs, &concat_att_map);
ASSERT_EQ(dy_exe_context.InputSize("X"), 1u);
ASSERT_EQ(dy_exe_context.InputName("X"), "vin");
ASSERT_EQ(dy_exe_context.HasAttr("axis"), true);
auto attr_map = dy_exe_context.Attrs();
ASSERT_EQ(boost::get<int>(attr_map["axis"]), 1);
ASSERT_EQ(dy_exe_context.OutputSize("Out"), 1u);
ASSERT_EQ(dy_exe_context.HasOutput("Out"), true);
}
TEST(test_layer, test_dygraph_infershape_context) {
std::shared_ptr<imperative::VarBase> vin(
new imperative::VarBase(false, "vin"));
std::shared_ptr<imperative::VarBase> vout(
new imperative::VarBase(false, "vout"));
framework::OpDesc desc;
platform::CPUPlace place;
var_pair x_pair = var_pair("X", vb_vector(1, vin));
var_pair y_pair = var_pair("Y", vb_vector(1, vin));
var_pair out_pair = var_pair("Out", vb_vector(1, vout));
imperative::NameVarBaseMap ins = {x_pair, y_pair};
imperative::NameVarBaseMap outs = {out_pair};
framework::AttributeMap concat_att_map;
concat_att_map["axis"] = 1;
DygraphInferShapeContext infer_shape_ctx(&ins, &outs, &concat_att_map);
bool have_x = infer_shape_ctx.HasOutputs("Out");
ASSERT_EQ(have_x, true);
bool have_z = infer_shape_ctx.HasOutputs("Z");
ASSERT_EQ(have_z, false);
}
} // namespace imperative
} // namespace paddle
......
......@@ -110,8 +110,8 @@ TEST(test_prepare_op, test_prepare_op) {
framework::OperatorWithKernel op("split", var_in_map, var_out_map,
split_attr_map);
framework::RuntimeContext ctx = PrepareRuntimeContext(ins, outs);
ASSERT_NO_FATAL_FAILURE(PreparedOp preparedOp =
PreparedOp::Prepare(ctx, op, place, ins));
ASSERT_NO_FATAL_FAILURE(PreparedOp preparedOp = PreparedOp::Prepare(
ins, outs, op, place, &split_attr_map));
}
const framework::Tensor* GetTensorFromVar(const framework::Variable& var);
......@@ -158,7 +158,8 @@ TEST(test_prepare_op, test_prepare_data) {
framework::RuntimeContext ctx = PrepareRuntimeContext(ins, outs);
// test if it can be transformed to GPU place
PreparedOp prepared_op = PreparedOp::Prepare(ctx, assign_op, gpu_place, ins);
PreparedOp prepared_op =
PreparedOp::Prepare(ins, outs, assign_op, gpu_place, &assign_attr_map);
for (const auto& name_pair : ins) {
for (const auto& vb : name_pair.second) {
ASSERT_TRUE(platform::is_same_place(
......@@ -201,7 +202,8 @@ TEST(test_prepare_op, test_prepare_data_same_place) {
framework::RuntimeContext ctx = PrepareRuntimeContext(ins, outs);
// test if it never transfered on GPU place
PreparedOp prepared_op = PreparedOp::Prepare(ctx, assign_op, cpu_place, ins);
PreparedOp prepared_op =
PreparedOp::Prepare(ins, outs, assign_op, cpu_place, &assign_attr_map);
for (const auto& name_pair : ins) {
for (const auto& vb : name_pair.second) {
ASSERT_TRUE(platform::is_same_place(
......
......@@ -82,10 +82,9 @@ static void PassStopGradient(const NameVarBaseMap& outs, bool generate_grad) {
void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins,
const NameVarBaseMap& outs, framework::AttributeMap attrs,
const platform::Place& place, bool trace_backward) {
platform::RecordEvent event(type);
VLOG(1) << "Trace Op: " << type;
size_t op_id = GenerateUniqueId();
auto op = OpBase::Create(op_id, type, ins, outs, std::move(attrs), place);
auto op = OpBase::Create(op_id, type, ins, outs, attrs, place);
op->Run(ins, outs);
if (enable_program_desc_tracing_) {
......
......@@ -62,11 +62,11 @@ inline void ExtractActivationTensor(const framework::ExecutionContext& context,
auto out_var = context.OutputVar("Out");
PADDLE_ENFORCE(x_var != nullptr,
"Cannot get input Variable X, variable name = %s",
context.op().Input("X"));
context.InputName("X"));
PADDLE_ENFORCE(out_var != nullptr,
"Cannot get output Variable Out, variable name = %s",
context.op().Output("Out"));
if (CanBeUsedBySelectedRows.count(context.op().Type())) {
context.OutputName("Out"));
if (CanBeUsedBySelectedRows.count(context.Type())) {
*X = paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(*x_var);
*Out = paddle::framework::GetMutableLoDTensorOrSelectedRowsValueFromVar(
out_var);
......@@ -77,7 +77,7 @@ inline void ExtractActivationTensor(const framework::ExecutionContext& context,
PADDLE_ENFORCE(*Out != nullptr,
"Cannot get output tensor Out, variable name = %s",
context.op().Output("Out"));
context.OutputName("Out"));
}
template <ActBwdOpFwdDeps kDepValue>
......@@ -93,18 +93,18 @@ inline void ExtractActivationGradTensor(
out_var = context.InputVar("Out");
PADDLE_ENFORCE(out_var != nullptr,
"Cannot get input Variable Out, variable name = %s",
context.op().Input("Out"));
context.InputName("Out"));
}
PADDLE_ENFORCE(out_grad_var != nullptr,
"Cannot get input Variable %s, variable name = %s",
framework::GradVarName("Out"),
context.op().Input(framework::GradVarName("Out")));
context.InputName(framework::GradVarName("Out")));
PADDLE_ENFORCE(x_grad_var != nullptr,
"Cannot get output Variable %s, variable name = %s",
framework::GradVarName("X"),
context.op().Output(framework::GradVarName("X")));
context.OutputName(framework::GradVarName("X")));
if (CanBeUsedBySelectedRows.count(context.op().Type())) {
if (CanBeUsedBySelectedRows.count(context.Type())) {
*dOut = paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(
*out_grad_var);
*dX = paddle::framework::GetMutableLoDTensorOrSelectedRowsValueFromVar(
......@@ -132,20 +132,20 @@ inline void ExtractActivationGradTensor(
PADDLE_ENFORCE(*dX != nullptr,
"Cannot get output tensor %s, variable name = %s",
framework::GradVarName("X"),
context.op().Output(framework::GradVarName("X")));
context.OutputName(framework::GradVarName("X")));
if (static_cast<int>(kDepValue) & static_cast<int>(kDepX)) {
auto x_var = context.InputVar("X");
PADDLE_ENFORCE(x_var != nullptr,
"Cannot get input tensor X, variable name = %s",
context.op().Input("X"));
if (CanBeUsedBySelectedRows.count(context.op().Type())) {
context.InputName("X"));
if (CanBeUsedBySelectedRows.count(context.Type())) {
*X = paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(*x_var);
} else {
*X = context.Input<framework::Tensor>("X");
}
} else {
VLOG(10) << " Inplace activation of Op : " << context.op().Type();
VLOG(10) << " Inplace activation of Op : " << context.Type();
*X = *dX;
}
}
......@@ -1273,8 +1273,8 @@ inline void ExtractActivationDoubleGradTensor(
auto ddo_var = ctx.OutputVar("DDOut");
PADDLE_ENFORCE(ddx_var != nullptr,
"Cannot get input Variable Out, variable name = %s",
ctx.op().Input("DDX"));
if (CanBeUsedBySelectedRows.count(ctx.op().Type())) {
ctx.InputName("DDX"));
if (CanBeUsedBySelectedRows.count(ctx.Type())) {
*ddX = paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(*ddx_var);
if (ddo_var) {
*ddOut = paddle::framework::GetMutableLoDTensorOrSelectedRowsValueFromVar(
......@@ -1288,15 +1288,15 @@ inline void ExtractActivationDoubleGradTensor(
}
PADDLE_ENFORCE(*ddX != nullptr,
"Cannot get output tensor DDX, variable name = %s",
ctx.op().Output("DDX"));
ctx.OutputName("DDX"));
if (static_cast<int>(kDepValue) & static_cast<int>(kDepX)) {
auto x_var = ctx.InputVar("X");
PADDLE_ENFORCE(x_var != nullptr,
"Cannot get input Variable Out, variable name = %s",
ctx.op().Input("X"));
ctx.InputName("X"));
auto dx_var = ctx.OutputVar("DX");
if (CanBeUsedBySelectedRows.count(ctx.op().Type())) {
if (CanBeUsedBySelectedRows.count(ctx.Type())) {
*X = paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(*x_var);
if (dx_var) {
*dX = paddle::framework::GetMutableLoDTensorOrSelectedRowsValueFromVar(
......@@ -1309,16 +1309,16 @@ inline void ExtractActivationDoubleGradTensor(
}
}
} else {
VLOG(10) << "Inplace activation of Op: " << ctx.op().Type();
VLOG(10) << "Inplace activation of Op: " << ctx.Type();
*X = *ddX;
}
if (static_cast<int>(kDepValue) & static_cast<int>(kDepOut)) {
auto out_var = ctx.InputVar("Out");
PADDLE_ENFORCE(out_var != nullptr,
"Cannot get input tensor Out, variable name = %s",
ctx.op().Input("Out"));
ctx.InputName("Out"));
auto dout_var = ctx.OutputVar("DOut");
if (CanBeUsedBySelectedRows.count(ctx.op().Type())) {
if (CanBeUsedBySelectedRows.count(ctx.Type())) {
*Out =
paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(*out_var);
if (dout_var) {
......@@ -1333,7 +1333,7 @@ inline void ExtractActivationDoubleGradTensor(
}
}
} else {
VLOG(10) << "Inplace activation of Op: " << ctx.op().Type();
VLOG(10) << "Inplace activation of Op: " << ctx.Type();
*Out = *ddX;
}
}
......@@ -1471,20 +1471,20 @@ inline void ExtractDoubleGradTensorWithInputDOut(
auto ddo_var = ctx.OutputVar("DDOut");
PADDLE_ENFORCE(ddx_var != nullptr,
"Cannot get input Variable Out, variable name = %s",
ctx.op().Input("DDX"));
ctx.InputName("DDX"));
*ddX = ctx.Input<framework::Tensor>("DDX");
if (ddo_var) {
*ddOut = ctx.Output<framework::Tensor>("DDOut");
}
PADDLE_ENFORCE(*ddX != nullptr,
"Cannot get output tensor DDX, variable name = %s",
ctx.op().Output("DDX"));
ctx.OutputName("DDX"));
// extract x(input), dx(output)
auto x_var = ctx.InputVar("X");
PADDLE_ENFORCE(x_var != nullptr,
"Cannot get input Variable Out, variable name = %s",
ctx.op().Input("X"));
ctx.InputName("X"));
auto dx_var = ctx.OutputVar("DX");
*X = ctx.Input<framework::Tensor>("X");
if (dx_var) {
......@@ -1537,20 +1537,20 @@ class SqrtDoubleGradKernel
auto ddo_var = ctx.OutputVar("DDOut");
PADDLE_ENFORCE(ddx_var != nullptr,
"Cannot get input Variable DDX, variable name = %s",
ctx.op().Input("DDX"));
ctx.InputName("DDX"));
ddX = ctx.Input<framework::Tensor>("DDX");
if (ddo_var) {
ddOut = ctx.Output<framework::Tensor>("DDOut");
}
PADDLE_ENFORCE(ddX != nullptr,
"Cannot get input Variable DDX, variable name = %s",
ctx.op().Input("DDX"));
ctx.InputName("DDX"));
// extract out(input), dout(output)
auto out_var = ctx.InputVar("Out");
PADDLE_ENFORCE(out_var != nullptr,
"Cannot get input Variable Out, variable name = %s",
ctx.op().Input("Out"));
ctx.InputName("Out"));
auto dout_var = ctx.OutputVar("DOut");
Out = ctx.Input<framework::Tensor>("Out");
if (dout_var) {
......@@ -1561,7 +1561,7 @@ class SqrtDoubleGradKernel
auto dx_var = ctx.InputVar("DX");
PADDLE_ENFORCE(dx_var != nullptr,
"Cannot get input Variable DX, variable name = %s",
ctx.op().Input("DX"));
ctx.InputName("DX"));
if (dx_var) {
dX = ctx.Input<framework::Tensor>("DX");
}
......
......@@ -27,8 +27,8 @@ template <typename DeviceContext, typename T>
class CoalesceTensorOpKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext &context) const override {
auto &in_var_names = context.Inputs("Input");
auto &out_var_names = context.Outputs("Output");
auto in_var_names = context.InputNames("Input");
auto out_var_names = context.OutputNames("Output");
auto &in_vars = context.MultiInputVar("Input");
auto out_vars = context.MultiOutputVar("Output");
......
......@@ -32,6 +32,7 @@ class ConcatOp : public framework::OperatorWithKernel {
void InferShape(framework::InferShapeContext *ctx) const override {
PADDLE_ENFORCE_GE(ctx->Inputs("X").size(), 1UL,
"Inputs(X) of ConcatOp should not be empty.");
PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true,
"Output(Out) of ConcatOp should not be null.");
......@@ -152,17 +153,8 @@ class ConcatOpGrad : public framework::OperatorWithKernel {
auto in_x = "X";
auto out_x_g_n = framework::GradVarName(in_x);
ctx->SetOutputsDim(out_x_g_n, ctx->GetInputsDim(in_x));
auto &in_names = ctx->Inputs(in_x);
auto &out_names = ctx->Outputs(out_x_g_n);
PADDLE_ENFORCE_EQ(
in_names.size(), out_names.size(),
"The number of arguments in %s[%d] and %s[%d] is not equal.", in_x,
in_names.size(), out_x_g_n, out_names.size());
for (size_t i = 0; i < in_names.size(); ++i) {
if (out_names[i] != framework::kEmptyVarName) {
ctx->ShareLoD(in_x, out_x_g_n, i, i);
}
}
ctx->ShareAllLoD(in_x, out_x_g_n);
}
protected:
......@@ -197,7 +189,9 @@ class ConcatGradOpMaker : public framework::SingleGradOpMaker<T> {
std::unique_ptr<T> op(new T());
op->SetType("concat_grad");
op->SetInput("X", this->Input("X"));
op->SetInput("AxisTensor", this->Input("AxisTensor"));
if (this->HasInput("AxisTensor")) {
op->SetInput("AxisTensor", this->Input("AxisTensor"));
}
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
op->SetOutput(framework::GradVarName("X"), this->InputGrad("X", false));
op->SetAttrMap(this->Attrs());
......
......@@ -139,7 +139,7 @@ class ConcatGradKernel : public framework::OpKernel<T> {
auto* out_grad =
ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
auto ins = ctx.MultiInput<framework::LoDTensor>("X");
auto out_var_names = ctx.Outputs(framework::GradVarName("X"));
auto out_var_names = ctx.OutputNames(framework::GradVarName("X"));
auto outs =
ctx.MultiOutput<framework::LoDTensor>(framework::GradVarName("X"));
......
......@@ -665,7 +665,7 @@ class GemmConvDoubleGradKernel : public framework::OpKernel<T> {
Tensor* dX = ctx.Output<Tensor>("DInput");
Tensor W = detail::Ref(ctx.Input<Tensor>("Filter"),
"Cannot find input Filter(%s) in scope)",
ctx.Inputs("Filter")[0]);
ctx.InputNames("Filter")[0]);
if (!ddY && !dW && !dX) return;
const int groups = ctx.Attr<int>("groups");
......
......@@ -62,7 +62,7 @@ class CudnnLSTMGPUKernel : public framework::OpKernel<T> {
// multi-devices before the first running.
// use parent scope to make cache persistable
auto *scope = const_cast<framework::Scope *>(ctx.scope().parent());
auto cache_var_name = ctx.Inputs("Cache")[0];
auto cache_var_name = ctx.InputNames("Cache")[0];
cache_var = scope->Var(cache_var_name);
}
CudnnRNNCache *cudnn_rnn_cache = nullptr;
......
......@@ -31,11 +31,11 @@ class CumKernel : public framework::OpKernel<typename Functor::ELEMENT_TYPE> {
void Compute(const framework::ExecutionContext& context) const override {
auto& X = detail::Ref(context.Input<framework::Tensor>("X"),
"Cannot get input tensor X, variable name = %s",
context.op().Input("X"));
context.InputName("X"));
auto& Out = detail::Ref(context.Output<framework::Tensor>("Out"),
"Cannot get output tensor Out, variable name = %s",
context.op().Output("Out"));
context.OutputName("Out"));
int axis = context.Attr<int>("axis");
bool exclusive = context.Attr<bool>("exclusive");
bool reverse = context.Attr<bool>("reverse");
......
......@@ -295,10 +295,10 @@ class GenerateProposalsKernel : public framework::OpKernel<T> {
auto *im_info = context.Input<Tensor>("ImInfo");
auto anchors = detail::Ref(context.Input<Tensor>("Anchors"),
"Cannot find input Anchors(%s) in scope",
context.Inputs("Anchors")[0]);
context.InputNames("Anchors")[0]);
auto variances = detail::Ref(context.Input<Tensor>("Variances"),
"Cannot find input Variances(%s) in scope",
context.Inputs("Variances")[0]);
context.InputNames("Variances")[0]);
auto *rpn_rois = context.Output<LoDTensor>("RpnRois");
auto *rpn_roi_probs = context.Output<LoDTensor>("RpnRoiProbs");
......
......@@ -369,10 +369,10 @@ class CUDAGenerateProposalsKernel : public framework::OpKernel<T> {
auto *im_info = context.Input<Tensor>("ImInfo");
auto anchors = detail::Ref(context.Input<Tensor>("Anchors"),
"Cannot find input Anchors(%s) in scope",
context.Inputs("Anchors")[0]);
context.InputNames("Anchors")[0]);
auto variances = detail::Ref(context.Input<Tensor>("Variances"),
"Cannot find input Variances(%s) in scope",
context.Inputs("Variances")[0]);
context.InputNames("Variances")[0]);
auto *rpn_rois = context.Output<LoDTensor>("RpnRois");
auto *rpn_roi_probs = context.Output<LoDTensor>("RpnRoiProbs");
......
......@@ -59,7 +59,7 @@ class NCCLBroadcastOpKernel : public framework::OpKernel<T> {
send_recv_buffer, static_cast<size_t>(in->numel()),
platform::ToNCCLDataType(in->type()), root_dev_id, comm, stream));
VLOG(3) << "Bcast " << ctx.Inputs("X")[0] << ", (" << in->numel() << ")"
VLOG(3) << "Bcast " << ctx.InputNames("X")[0] << ", (" << in->numel() << ")"
<< " From " << root_dev_id << " to " << dev_id;
if (ctx.Attr<bool>("sync_mode")) {
......
......@@ -84,9 +84,9 @@ class DistributedLookupTableKernel : public framework::OpKernel<T> {
auto ids_vars = context.MultiInputVar("Ids");
auto emb_vars = context.MultiOutput<framework::Tensor>("Embeddings");
auto id_names = context.Inputs("Ids");
auto embedding_name = context.Inputs("W").front();
auto out_names = context.Outputs("Outputs");
auto id_names = context.InputNames("Ids");
auto embedding_name = context.InputNames("W").front();
auto out_names = context.OutputNames("Outputs");
auto lookup_tables = context.Attr<std::vector<std::string>>("table_names");
auto height_sections =
......
......@@ -116,7 +116,7 @@ class SplitIdsOpKernel : public framework::OpKernel<T> {
} else {
PADDLE_THROW(
"% should be LoDTensor or SelectedRows, but the received type is %s",
ctx.Inputs("Ids")[0], framework::ToTypeName(ids_var->Type()));
ctx.InputNames("Ids")[0], framework::ToTypeName(ids_var->Type()));
}
}
};
......
......@@ -94,7 +94,7 @@ class ElementwiseMulKernel : public framework::OpKernel<T> {
auto x_var = ctx.InputVar("X");
PADDLE_ENFORCE(x_var != nullptr,
"Cannot get input Variable X, variable name = %s",
ctx.op().Input("X"));
ctx.InputName("X"));
auto* y = ctx.Input<framework::LoDTensor>("Y");
framework::Tensor x, *z;
......
......@@ -31,7 +31,7 @@ class ElementwisePowKernel : public framework::OpKernel<T> {
auto* x = ctx.Input<Tensor>("X");
PADDLE_ENFORCE(x != nullptr,
"Cannot get input Variable X, variable name = %s",
ctx.op().Input("X"));
ctx.InputName("X"));
auto* y = ctx.Input<Tensor>("Y");
auto* z = ctx.Output<Tensor>("Out");
z->mutable_data<T>(ctx.GetPlace());
......
......@@ -138,7 +138,7 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel<T> {
std::vector<float> scales = {1.0f, 1.0f};
const std::string key =
platform::CreateKey(src_x_tz, ctx.op().Output("Out"));
platform::CreateKey(src_x_tz, ctx.OutputName("Out"));
platform::SumMKLDNNHandler handler(dev_ctx, mkldnn_engine, key);
......
......@@ -76,6 +76,7 @@ class FillConstantKernel : public framework::OpKernel<T> {
void Compute(const paddle::framework::ExecutionContext &ctx) const override {
auto data_type =
static_cast<framework::proto::VarType::Type>(ctx.Attr<int>("dtype"));
auto str_value = ctx.Attr<std::string>("str_value");
auto float_value = ctx.Attr<float>("value");
auto force_cpu = ctx.Attr<bool>("force_cpu");
......
......@@ -47,7 +47,7 @@ class FillKernel : public framework::OpKernel<T> {
auto &out =
detail::Ref(ctx.Output<framework::LoDTensor>("Out"),
"Cannot get output lod tensor Out, variable name = %s",
ctx.op().Output("Out"));
ctx.OutputName("Out"));
out.Resize(framework::make_ddim(ctx.Attr<std::vector<int>>("shape")));
auto dtype =
static_cast<framework::proto::VarType::Type>(ctx.Attr<int>("dtype"));
......
......@@ -385,10 +385,10 @@ class FusedElemwiseActivationKernel : public framework::OpKernel<T> {
void Compute(const framework::ExecutionContext &ctx) const override {
auto &in_x = detail::Ref(ctx.Input<framework::Tensor>("X"),
"Cannot get input tensor %s, variable name = %s",
"X", ctx.op().Input("X"));
"X", ctx.InputName("X"));
auto &in_y = detail::Ref(ctx.Input<framework::Tensor>("Y"),
"Cannot get input tensor %s, variable name = %s",
"Y", ctx.op().Input("Y"));
"Y", ctx.InputName("Y"));
PADDLE_ENFORCE(ctx.HasOutput("Out"), "The output(Out) should not be empty");
auto output = ctx.Output<framework::Tensor>("Out");
......
......@@ -97,7 +97,7 @@ class HierarchicalSigmoidOpKernel : public framework::OpKernel<T> {
#ifdef PADDLE_WITH_DISTRIBUTE
// w_Out is set to used by prefetch, never change it in other cases
auto weight = ctx.Outputs("W_Out").front();
auto weight = ctx.OutputNames("W_Out").front();
operators::distributed::prefetch("Ids@Prefetch", "W@Prefetch", weight,
true, table_names, epmap,
height_sections, ctx, local_scope);
......
......@@ -94,7 +94,7 @@ class HuberLossGradKernel : public framework::OpKernel<T> {
auto* in1 = context.Input<Tensor>(framework::GradVarName("Out"));
auto* out0 = context.Output<Tensor>(framework::GradVarName("X"));
auto* out1 = context.Output<Tensor>(framework::GradVarName("Y"));
auto delta = static_cast<T>(context.op().Attr<float>("delta"));
auto delta = static_cast<T>(context.Attr<float>("delta"));
auto& place =
*context.template device_context<DeviceContext>().eigen_device();
......
......@@ -33,7 +33,7 @@ class LoadCombineOpKernel : public framework::OpKernel<T> {
auto filename = ctx.Attr<std::string>("file_path");
auto load_as_fp16 = ctx.Attr<bool>("load_as_fp16");
auto model_from_memory = ctx.Attr<bool>("model_from_memory");
auto &out_var_names = ctx.Outputs("Out");
auto out_var_names = ctx.OutputNames("Out");
PADDLE_ENFORCE_GT(
static_cast<int>(out_var_names.size()), 0,
......
......@@ -36,7 +36,7 @@ class LoadOpKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE(static_cast<bool>(fin), "Cannot open file %s for load op",
filename);
auto out_var_name = ctx.Outputs("Out").data();
auto out_var_name = ctx.OutputNames("Out").data();
auto *out_var = ctx.OutputVar("Out");
PADDLE_ENFORCE(out_var != nullptr, "Output variable %s cannot be found ",
......
......@@ -94,8 +94,8 @@ class LookupTableCUDAKernel : public framework::OpKernel<T> {
auto *output_t = context.Output<LoDTensor>("Out");
int64_t padding_idx = context.Attr<int64_t>("padding_idx");
auto id_name = context.Inputs("Ids").front();
auto out_name = context.Outputs("Out").front();
auto id_name = context.InputNames("Ids").front();
auto out_name = context.OutputNames("Out").front();
size_t N = table_t->dims()[0];
size_t D = table_t->dims()[1];
......
......@@ -45,9 +45,9 @@ class LookupTableKernel : public framework::OpKernel<T> {
auto *output_t = context.Output<LoDTensor>("Out"); // float tensor
auto *table_var = context.InputVar("W");
auto id_name = context.Inputs("Ids").front();
auto embedding_name = context.Inputs("W").front();
auto out_name = context.Outputs("Out").front();
auto id_name = context.InputNames("Ids").front();
auto embedding_name = context.InputNames("W").front();
auto out_name = context.OutputNames("Out").front();
// for remote prefetch
auto epmap = context.Attr<std::vector<std::string>>("epmap");
......
......@@ -94,8 +94,8 @@ class LookupTableV2CUDAKernel : public framework::OpKernel<T> {
auto *output_t = context.Output<LoDTensor>("Out");
int64_t padding_idx = context.Attr<int64_t>("padding_idx");
auto id_name = context.Inputs("Ids").front();
auto out_name = context.Outputs("Out").front();
auto id_name = context.InputNames("Ids").front();
auto out_name = context.OutputNames("Out").front();
size_t N = table_t->dims()[0];
size_t D = table_t->dims()[1];
......
......@@ -45,9 +45,9 @@ class LookupTableV2Kernel : public framework::OpKernel<T> {
auto *output_t = context.Output<LoDTensor>("Out"); // float tensor
auto *table_var = context.InputVar("W");
auto id_name = context.Inputs("Ids").front();
auto embedding_name = context.Inputs("W").front();
auto out_name = context.Outputs("Out").front();
auto id_name = context.InputNames("Ids").front();
auto embedding_name = context.InputNames("W").front();
auto out_name = context.OutputNames("Out").front();
// for remote prefetch
auto epmap = context.Attr<std::vector<std::string>>("epmap");
......
......@@ -73,8 +73,8 @@ void eltwise_forward(const framework::ExecutionContext &ctx,
const auto *x = ctx.Input<Tensor>("X");
auto *y = ctx.Output<Tensor>("Out");
const T alpha = ctx.op().HasAttr("alpha") ? ctx.Attr<T>("alpha") : 0;
const T beta = ctx.op().HasAttr("beta") ? ctx.Attr<T>("beta") : 0;
const T alpha = ctx.HasAttr("alpha") ? ctx.Attr<T>("alpha") : 0;
const T beta = ctx.HasAttr("beta") ? ctx.Attr<T>("beta") : 0;
PADDLE_ENFORCE(
x->dims().size() == 2 || x->dims().size() == 3 || x->dims().size() == 4,
......@@ -88,7 +88,7 @@ void eltwise_forward(const framework::ExecutionContext &ctx,
platform::ActivationMKLDNNHandler<T> handler(
src_tz, algorithm, alpha, beta, src_format, is_test, dev_ctx,
ctx.GetPlace(), ctx.op().Input("X"));
ctx.GetPlace(), ctx.InputName("X"));
auto src_memory_p = handler.AcquireSrcMemory(x);
auto dst_memory_p = handler.AcquireDstMemory(y);
......@@ -113,8 +113,8 @@ void eltwise_grad(const framework::ExecutionContext &ctx,
const auto *diff_y = ctx.Input<Tensor>(framework::GradVarName("Out"));
auto *diff_x = ctx.Output<Tensor>(framework::GradVarName("X"));
const T alpha = ctx.op().HasAttr("alpha") ? ctx.Attr<T>("alpha") : 0;
const T beta = ctx.op().HasAttr("beta") ? ctx.Attr<T>("beta") : 0;
const T alpha = ctx.HasAttr("alpha") ? ctx.Attr<T>("alpha") : 0;
const T beta = ctx.HasAttr("beta") ? ctx.Attr<T>("beta") : 0;
auto diff_dst_tz = framework::vectorize<int>(diff_y->dims());
......@@ -127,7 +127,7 @@ void eltwise_grad(const framework::ExecutionContext &ctx,
platform::ActivationMKLDNNHandler<T> handler(
diff_dst_tz, algorithm, alpha, beta, src_format, diff_y_format, dev_ctx,
ctx.GetPlace(), ctx.op().Input("X"));
ctx.GetPlace(), ctx.InputName("X"));
auto src_memory_p = handler.AcquireBackwardSrcMemory(x);
auto diff_dst_memory_p = handler.AcquireDiffDstMemory(diff_y);
......
......@@ -149,6 +149,7 @@ class BatchNormMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
const unsigned int C = scale_tz[0];
// MKLDNN requires a single piece of memory for scale and shift/bias data
std::vector<T> scaleshift_data(scale->data<T>(), scale->data<T>() + C);
scaleshift_data.reserve(2 * C);
scaleshift_data.insert(scaleshift_data.end(), shift->data<T>(),
......@@ -162,7 +163,7 @@ class BatchNormMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
BatchNormMKLDNNHandler<T> handler(
src_tz, epsilon, flags, global_stats,
platform::MKLDNNFormatForSize(src_tz.size(), x->format()), dev_ctx,
ctx.GetPlace(), ctx.op().Output("SavedMean"));
ctx.GetPlace(), ctx.OutputName("SavedMean"));
auto src_memory = handler.AcquireSrcMemory(x);
auto scaleshift_memory =
......@@ -261,7 +262,7 @@ class BatchNormMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
BatchNormMKLDNNHandler<T> handler(
src_tz, epsilon, mkldnn::use_scale_shift, dst_format, input_format,
dev_ctx, ctx.GetPlace(), ctx.op().Input("SavedMean"));
dev_ctx, ctx.GetPlace(), ctx.InputName("SavedMean"));
// MKLDNN requires a single piece of memory for scale and shift/bias data
const size_t scaleshift_size = 2 * C;
......
......@@ -151,7 +151,7 @@ class ConcatMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
ConcatPrimitiveFactory<T> prim_creator;
std::string key = platform::CreateKey(
paddle::framework::vectorize<int>(multi_input[0]->dims()),
ctx.op().Output("Out"), dt, platform::ThreadIDasStr());
ctx.OutputName("Out"), dt, platform::ThreadIDasStr());
const std::string key_prim = key + "@concat_p";
const std::string key_concat_pd = key + "@concat_pd";
const std::string key_srcs = key + "@concat_srcs";
......
......@@ -203,7 +203,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
// Get unique name for storing MKLDNN primitives
const std::string key = platform::CreateKey(
src_tz, ctx.op().Input("Input") + ctx.op().Input("Filter"));
src_tz, ctx.InputName("Input") + ctx.InputName("Filter"));
std::vector<primitive> pipeline;
......@@ -377,7 +377,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
paddle::framework::ToMKLDNNDataType(input->type());
std::string key = platform::CreateKey(
src_tz, src_dt, ctx.op().Input("Input") + ctx.op().Input("Filter"));
src_tz, src_dt, ctx.InputName("Input") + ctx.InputName("Filter"));
const std::string key_conv_pd = key + "@conv_pd";
......@@ -755,7 +755,7 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
// as well as attributes of primitive to be created
// This name will be used as key when saving info into device context
const std::string key = platform::CreateKey(
src_tz, ctx.op().Input("Input") + ctx.op().Input("Filter"));
src_tz, ctx.InputName("Input") + ctx.InputName("Filter"));
const std::string key_conv_pd = key + "@conv_pd";
std::vector<primitive> pipeline;
......
......@@ -140,8 +140,9 @@ class ConvTransposeMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
auto dst_tz = paddle::framework::vectorize<int>(output->dims());
// Get unique name for storing MKLDNN primitives
const std::string key =
platform::CreateKey(src_tz, ctx.op().Output("Output"));
platform::CreateKey(src_tz, ctx.OutputName("Output"));
std::vector<mkldnn::primitive> pipeline;
......
......@@ -53,7 +53,7 @@ class DeQuantOpKernel : public framework::OpKernel<T> {
paddle::framework::ToMKLDNNDataType(input->type());
MKLDNNMemoryFormat src_fmt = input->format();
std::string key =
platform::CreateKey(src_dt, src_tz, ctx.op().Output("Output"));
platform::CreateKey(src_dt, src_tz, ctx.OutputName("Output"));
const std::string key_prim = key + "@reorder_p";
const std::string key_src_mem = key + "@src_mem";
const std::string key_dst_mem = key + "@dst_mem";
......
......@@ -404,7 +404,7 @@ GetPrimitiveFactory(const MKLDNNDeviceContext& dev_ctx,
const mkldnn::engine& mkldnn_engine) {
const std::string key = platform::CreateKey(
platform::ThreadIDasStr(), input->format(),
framework::vectorize<int>(weights->dims()), ctx.op().Output("Out"));
framework::vectorize<int>(weights->dims()), ctx.OutputName("Out"));
auto prim_creator =
std::static_pointer_cast<FCPrimitiveFactory<T_in, T_w, T_out>>(
......
......@@ -53,7 +53,7 @@ class LRNMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
platform::LRNMKLDNNHandler<T> handler(dims, n, alpha, beta, k, x->format(),
is_test, dev_ctx, ctx.GetPlace(),
ctx.op().Output("Out"));
ctx.OutputName("Out"));
auto src_memory = handler.AcquireSrcMemory(x);
auto dst_memory = handler.AcquireDstMemory(out);
......@@ -109,9 +109,9 @@ class LRNMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
auto dims = paddle::framework::vectorize<int>(x->dims());
platform::LRNMKLDNNHandler<T> handler(
dims, n, alpha, beta, k, x->format(), out_grad->format(), dev_ctx,
ctx.GetPlace(), ctx.op().Input("Out"));
platform::LRNMKLDNNHandler<T> handler(dims, n, alpha, beta, k, x->format(),
out_grad->format(), dev_ctx,
ctx.GetPlace(), ctx.InputName("Out"));
auto src_memory = handler.AcquireSrcMemory(x);
auto workspace = handler.AcquireBackwardWorkspaceMemory(mid);
......
......@@ -33,10 +33,10 @@ class MKLDNNActivationKernel
void Compute(const framework::ExecutionContext& context) const override {
PADDLE_ENFORCE(context.Input<framework::Tensor>("X") != nullptr,
"Cannot get input tensor X, variable name = %s",
context.op().Input("X"));
context.InputName("X"));
PADDLE_ENFORCE(context.Output<framework::Tensor>("Out") != nullptr,
"Cannot find output tensor Out, variable name = %s",
context.op().Output("Out"));
context.OutputName("Out"));
Functor functor;
auto attrs = functor.GetAttrs();
......
......@@ -342,7 +342,7 @@ std::shared_ptr<MulPrimitiveFactory<XT, YT, OT>> GetPrimitiveFactory(
const std::string key = platform::CreateKey(
input_x->type(), framework::vectorize<int>(input_x->dims()),
input_y->type(), framework::vectorize<int>(input_y->dims()),
ctx.op().Output("Out"));
ctx.OutputName("Out"));
auto prim_creator = std::static_pointer_cast<MulPrimitiveFactory<XT, YT, OT>>(
dev_ctx.GetBlob(key));
......
......@@ -80,7 +80,7 @@ class PoolMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
src_tz, dst_tz, ksize, strides, paddings, pooling_type,
ctx.Attr<bool>("ceil_mode"), input->format(),
paddle::framework::ToMKLDNNDataType(input->type()), is_test, dev_ctx,
ctx.GetPlace(), ctx.op().Output("Out"), ctx.Attr<bool>("exclusive"));
ctx.GetPlace(), ctx.OutputName("Out"), ctx.Attr<bool>("exclusive"));
auto src_memory = handler.AcquireSrcMemory(input);
auto dst_memory = handler.AcquireDstMemory(output);
......@@ -162,13 +162,13 @@ class PoolMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
// This name will be used as key when referring info from device context
const std::string key = platform::CreateKey(
diff_src_tz, pooling_type, ksize, strides, paddings,
memory::data_type::f32, in_x->format(), ctx.op().Input("Out"));
memory::data_type::f32, in_x->format(), ctx.InputName("Out"));
platform::PoolingMKLDNNHandler<T> handler(
diff_dst_tz, diff_src_tz, ksize, strides, paddings, pooling_type,
ctx.Attr<bool>("ceil_mode"), in_x->format(), out_grad->format(),
paddle::framework::ToMKLDNNDataType(out_grad->type()), dev_ctx,
ctx.GetPlace(), ctx.op().Input("Out"), ctx.Attr<bool>("exclusive"));
ctx.GetPlace(), ctx.InputName("Out"), ctx.Attr<bool>("exclusive"));
auto diff_dst_memory = handler.AcquireDiffDstMemory(out_grad);
auto diff_src_memory = handler.AcquireDiffSrcMemory(in_x_grad);
......
......@@ -49,7 +49,7 @@ class QuantOpKernel : public framework::OpKernel<T> {
bool is_negative = ctx.Attr<bool>("is_negative_input");
std::string key = platform::CreateKey(src_tz, scale_data, is_negative,
ctx.op().Output("Output"));
ctx.OutputName("Output"));
const std::string key_prim = key + "@reorder_p";
const std::string key_src_mem = key + "@src_mem";
const std::string key_dst_mem = key + "@dst_mem";
......
......@@ -90,7 +90,7 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel<T> {
auto softmax_tz = paddle::framework::vectorize<int>(dims);
SoftmaxMKLDNNHandler<T> handler(softmax_tz, input->format(), axis, dev_ctx,
ctx.GetPlace(), ctx.op().Output("Out"));
ctx.GetPlace(), ctx.OutputName("Out"));
auto softmax_src_memory_p = handler.AcquireSrcMemory(input);
auto softmax_dst_memory_p = handler.AcquireDstMemory(output);
......@@ -140,7 +140,7 @@ class SoftmaxMKLDNNGradKernel : public paddle::framework::OpKernel<T> {
SoftmaxMKLDNNHandler<T> handler(softmax_tz, output->format(),
dout->format(), axis, dev_ctx,
ctx.GetPlace(), ctx.op().Input("Out"));
ctx.GetPlace(), ctx.InputName("Out"));
auto dst_memory_p = handler.AcquireDstMemory(output);
auto diff_dst_memory_p = handler.AcquireDiffDstMemory(dout);
......
......@@ -46,8 +46,7 @@ class TransposeMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
auto nchw_tz = paddle::framework::vectorize<int>(input->dims());
const std::string key =
platform::CreateKey(nchw_tz, ctx.op().Output("Out"));
const std::string key = platform::CreateKey(nchw_tz, ctx.OutputName("Out"));
platform::TransposeMKLDNNHandler<T> handler(nchw_tz, axis, dev_ctx,
mkldnn_engine, key);
......@@ -99,7 +98,7 @@ class TransposeMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
auto nchw_tz = paddle::framework::vectorize<int>(out_grad->dims());
const std::string key = platform::CreateKey(
nchw_tz, ctx.op().Output(framework::GradVarName("X")));
nchw_tz, ctx.OutputName(framework::GradVarName("X")));
platform::TransposeMKLDNNHandler<T> handler(nchw_tz, reversed_axis, dev_ctx,
mkldnn_engine, key);
......
......@@ -114,7 +114,7 @@ class MultiplexGradOp : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override {
auto& dxs = ctx->Outputs(framework::GradVarName("X"));
auto dxs = ctx->Outputs(framework::GradVarName("X"));
PADDLE_ENFORCE(!dxs.empty(), "Output(X@Grad) should not be null.");
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
"Input(Out@GRAD) should not be null.");
......
......@@ -217,7 +217,7 @@ class NCEKernel : public framework::OpKernel<T> {
w_tensor->Resize(framework::make_ddim(w_dims));
#ifdef PADDLE_WITH_DISTRIBUTE
auto weight = context.Inputs("Weight").front();
auto weight = context.InputNames("Weight").front();
operators::distributed::prefetch("Ids@Prefetch", "Weight@Prefetch",
weight, false, table_names, epmap,
height_sections, context, local_scope);
......
......@@ -305,13 +305,13 @@ void NgraphEngine::Prepare(const framework::ExecutionContext& ctx) {
++idx;
}
auto input_vars = ctx.Inputs("Xs");
auto input_vars = ctx.InputNames("Xs");
if (!input_vars.empty()) {
feed_vars = input_vars;
var_in_ = input_vars;
}
auto output_vars = ctx.Outputs("Ys");
auto output_vars = ctx.OutputNames("Ys");
if (!output_vars.empty()) {
var_out_ = output_vars;
}
......
......@@ -27,13 +27,13 @@ class AdadeltaOpKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE(param_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Param").front(),
ctx.InputNames("Param").front(),
framework::ToTypeName(param_var->Type()));
const auto* grad_var = ctx.InputVar("Grad");
PADDLE_ENFORCE(grad_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Grad").front(),
ctx.InputNames("Grad").front(),
framework::ToTypeName(grad_var->Type()));
auto param_out_tensor = ctx.Output<framework::Tensor>("ParamOut");
......
......@@ -50,7 +50,7 @@ class AdagradOpKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE(param_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Param").front(),
ctx.InputNames("Param").front(),
framework::ToTypeName(param_var->Type()));
auto *param_out_tensor = ctx.Output<framework::Tensor>("ParamOut");
......
......@@ -368,7 +368,7 @@ class AdamOpKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE(param_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Param").front(),
ctx.InputNames("Param").front(),
framework::ToTypeName(param_var->Type()));
using paddle::framework::LoDTensor;
......
......@@ -27,13 +27,13 @@ class AdamaxOpKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE(param_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Param").front(),
ctx.InputNames("Param").front(),
framework::ToTypeName(param_var->Type()));
const auto* grad_var = ctx.InputVar("Grad");
PADDLE_ENFORCE(grad_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Grad").front(),
ctx.InputNames("Grad").front(),
framework::ToTypeName(grad_var->Type()));
auto param_out_tensor = ctx.Output<framework::Tensor>("ParamOut");
......
......@@ -27,13 +27,13 @@ class DecayedAdagradOpKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE(param_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Param").front(),
ctx.InputNames("Param").front(),
framework::ToTypeName(param_var->Type()));
const auto* grad_var = ctx.InputVar("Grad");
PADDLE_ENFORCE(grad_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Grad").front(),
ctx.InputNames("Grad").front(),
framework::ToTypeName(grad_var->Type()));
auto param_out_tensor = ctx.Output<framework::Tensor>("ParamOut");
......
......@@ -30,14 +30,14 @@ class DpsgdOpKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE_EQ(param_var->IsType<framework::LoDTensor>(), true,
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Param").front(),
ctx.InputNames("Param").front(),
framework::ToTypeName(param_var->Type()));
const auto *grad_var = ctx.InputVar("Grad");
PADDLE_ENFORCE_EQ(grad_var->IsType<framework::LoDTensor>(), true,
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Grad").front(),
ctx.InputNames("Grad").front(),
framework::ToTypeName(grad_var->Type()));
const auto *learning_rate = ctx.Input<framework::Tensor>("LearningRate");
......
......@@ -32,13 +32,13 @@ class FTRLOpKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE(param_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Param").front(),
ctx.InputNames("Param").front(),
framework::ToTypeName(param_var->Type()));
const auto* grad_var = ctx.InputVar("Grad");
PADDLE_ENFORCE(grad_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Grad").front(),
ctx.InputNames("Grad").front(),
framework::ToTypeName(grad_var->Type()));
auto* param_out = ctx.Output<Tensor>("ParamOut");
......
......@@ -181,7 +181,7 @@ class LambOpKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE(param_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Param").front(),
ctx.InputNames("Param").front(),
framework::ToTypeName(param_var->Type()));
using paddle::framework::LoDTensor;
......
......@@ -61,7 +61,7 @@ class SGDOpKernel<platform::CUDADeviceContext, T>
PADDLE_ENFORCE(param_var->IsType<framework::LoDTensor>(),
"The Var(%s)'s type should be LoDTensor, "
"but the received is %s",
ctx.Inputs("Param").front(),
ctx.InputNames("Param").front(),
framework::ToTypeName(param_var->Type()));
auto* param = ctx.Input<framework::Tensor>("Param");
......
......@@ -431,7 +431,9 @@ class Reshape2GradMaker : public framework::SingleGradOpMaker<T> {
auto *grad_op = new T();
grad_op->SetType("reshape2_grad");
grad_op->SetInput("XShape", this->Output("XShape"));
grad_op->SetInput("ShapeTensor", this->Input("ShapeTensor"));
if (this->HasInput("ShapeTensor")) {
grad_op->SetInput("ShapeTensor", this->Input("ShapeTensor"));
}
grad_op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
grad_op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
grad_op->SetAttrMap(this->Attrs());
......
......@@ -50,7 +50,7 @@ class SaveCombineOpKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE(static_cast<bool>(fout), "Cannot open %s to write",
filename);
auto &inp_var_names = ctx.Inputs("X");
auto inp_var_names = ctx.InputNames("X");
auto &inp_vars = ctx.MultiInputVar("X");
PADDLE_ENFORCE_GT(static_cast<int>(inp_var_names.size()), 0,
"The number of input variables should be greater than 0");
......
......@@ -40,7 +40,7 @@ class SaveOpKernel : public framework::OpKernel<T> {
auto place = ctx.GetPlace();
auto *input_var = ctx.InputVar("X");
auto iname = ctx.Inputs("X").data();
auto iname = ctx.InputNames("X").data();
PADDLE_ENFORCE(input_var != nullptr, "Cannot find variable %s for save_op",
iname);
......
......@@ -271,10 +271,18 @@ class SliceOpGradMaker : public framework::SingleGradOpMaker<T> {
std::unique_ptr<T> Apply() const override {
auto *bind = new T();
bind->SetInput("Input", this->Input("Input"));
bind->SetInput("StartsTensor", this->Input("StartsTensor"));
bind->SetInput("EndsTensor", this->Input("EndsTensor"));
bind->SetInput("StartsTensorList", this->Input("StartsTensorList"));
bind->SetInput("EndsTensorList", this->Input("EndsTensorList"));
if (this->HasInput("StartsTensor")) {
bind->SetInput("StartsTensor", this->Input("StartsTensor"));
}
if (this->HasInput("EndsTensor")) {
bind->SetInput("EndsTensor", this->Input("EndsTensor"));
}
if (this->HasInput("StartsTensorList")) {
bind->SetInput("StartsTensorList", this->Input("StartsTensorList"));
}
if (this->HasInput("EndsTensorList")) {
bind->SetInput("EndsTensorList", this->Input("EndsTensorList"));
}
bind->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
bind->SetOutput(framework::GradVarName("Input"), this->InputGrad("Input"));
bind->SetAttrMap(this->Attrs());
......
......@@ -163,7 +163,9 @@ class SplitGradMaker : public framework::SingleGradOpMaker<T> {
auto op = new T();
op->SetType("concat");
op->SetInput("X", this->OutputGrad("Out"));
op->SetInput("AxisTensor", this->Input("AxisTensor"));
if (this->HasInput("AxisTensor")) {
op->SetInput("AxisTensor", this->Input("AxisTensor"));
}
op->SetOutput("Out", this->InputGrad("X"));
op->SetAttrMap(this->Attrs());
return std::unique_ptr<T>(op);
......
......@@ -108,7 +108,7 @@ class SumOp : public framework::OperatorWithKernel {
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
auto x_vars = ctx.MultiInputVar("X");
auto x_vars_name = ctx.Inputs("X");
auto x_vars_name = ctx.InputNames("X");
framework::LibraryType library{framework::LibraryType::kPlain};
framework::DataLayout layout{framework::DataLayout::kAnyLayout};
......
......@@ -39,13 +39,14 @@ class TestAddPositionEncodingTensorOp(OpTest):
"""
check the correctness of output
"""
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
"""
check the correctness of grad
"""
self.check_grad(['X'], 'Out', max_relative_error=0.005)
self.check_grad(
['X'], 'Out', max_relative_error=0.005, check_dygraph=False)
def init_input_output(self):
"""
......@@ -93,13 +94,14 @@ class TestAddPositionEncodingLoDTensorOp(OpTest):
"""
check the correctness of output
"""
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
"""
check the correctness of grad
"""
self.check_grad(['X'], 'Out', max_relative_error=0.005)
self.check_grad(
['X'], 'Out', max_relative_error=0.005, check_dygraph=False)
def init_input_output(self):
"""
......
......@@ -152,7 +152,7 @@ class TestAttentionLSTMOp(OpTest):
}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestAttentionOpNonInit(TestAttentionLSTMOp):
......
......@@ -93,7 +93,7 @@ class TestCollectFPNProposalstOp(OpTest):
self.set_data()
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
if __name__ == '__main__':
......
......@@ -27,10 +27,10 @@ class TestSumOp1(OpTest):
self.outputs = {'Out': self.inputs['X'].cumsum(axis=2)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
self.check_grad(['X'], 'Out', check_dygraph=False)
class TestSumOp2(OpTest):
......@@ -45,10 +45,10 @@ class TestSumOp2(OpTest):
}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
self.check_grad(['X'], 'Out', check_dygraph=False)
class TestSumOp3(OpTest):
......@@ -59,10 +59,10 @@ class TestSumOp3(OpTest):
self.outputs = {'Out': self.inputs['X'].cumsum(axis=1)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
self.check_grad(['X'], 'Out', check_dygraph=False)
class TestSumOp4(OpTest):
......@@ -73,10 +73,10 @@ class TestSumOp4(OpTest):
self.outputs = {'Out': self.inputs['X'].cumsum(axis=0)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
self.check_grad(['X'], 'Out', check_dygraph=False)
class TestSumOp5(OpTest):
......@@ -86,10 +86,10 @@ class TestSumOp5(OpTest):
self.outputs = {'Out': self.inputs['X'].cumsum(axis=1)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
self.check_grad(['X'], 'Out', check_dygraph=False)
class TestSumOp7(OpTest):
......@@ -99,10 +99,10 @@ class TestSumOp7(OpTest):
self.outputs = {'Out': self.inputs['X'].cumsum(axis=0)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
self.check_grad(['X'], 'Out', check_dygraph=False)
class TestSumOp8(OpTest):
......@@ -119,10 +119,10 @@ class TestSumOp8(OpTest):
}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
self.check_grad(['X'], 'Out', check_dygraph=False)
if __name__ == '__main__':
......
......@@ -77,7 +77,7 @@ class TestCVMOpWithLodTensor(OpTest):
self.outputs = {'Y': (np.array(out), lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
user_grads = np.array(
......@@ -85,7 +85,8 @@ class TestCVMOpWithLodTensor(OpTest):
(self.batch_size, self.item_width)).astype("float32")
user_grads[:, :2] = self.inputs['CVM'].reshape(self.batch_size, 2)
user_grads = [user_grads]
self.check_grad(['X'], 'Y', user_defined_grads=user_grads)
self.check_grad(
['X'], 'Y', user_defined_grads=user_grads, check_dygraph=False)
class TestCVMOpWithOutLodTensor1(OpTest):
......@@ -111,7 +112,7 @@ class TestCVMOpWithOutLodTensor1(OpTest):
self.outputs = {'Y': output}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
numel = self.batch_size * self.item_width
......@@ -119,7 +120,8 @@ class TestCVMOpWithOutLodTensor1(OpTest):
(self.batch_size, self.item_width)).astype("float32")
user_grads[:, :2] = self.inputs['CVM'].reshape(self.batch_size, 2)
user_grads = [user_grads]
self.check_grad(['X'], 'Y', user_defined_grads=user_grads)
self.check_grad(
['X'], 'Y', user_defined_grads=user_grads, check_dygraph=False)
class TestCVMOpWithOutLodTensor2(OpTest):
......@@ -145,7 +147,7 @@ class TestCVMOpWithOutLodTensor2(OpTest):
self.outputs = {'Y': output}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
numel = self.batch_size * self.item_width
......@@ -154,7 +156,8 @@ class TestCVMOpWithOutLodTensor2(OpTest):
(self.batch_size, self.item_width)).astype("float32")
user_grads[:, :2] = self.inputs['CVM'].reshape(self.batch_size, 2)
user_grads = [user_grads]
self.check_grad(['X'], 'Y', user_defined_grads=user_grads)
self.check_grad(
['X'], 'Y', user_defined_grads=user_grads, check_dygraph=False)
if __name__ == '__main__':
......
......@@ -35,7 +35,7 @@ class TestFillOp1(OpTest):
self.outputs = {'Out': val.astype('float64')}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestFillOp2(OpTest):
......@@ -52,7 +52,7 @@ class TestFillOp2(OpTest):
self.outputs = {'Out': val.astype('float64')}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestFillOp3(OpTest):
......
......@@ -140,7 +140,7 @@ class TestFusionLSTMOp(OpTest):
def test_check_output(self):
for use_seq in {True, False}:
self.attrs['use_seq'] = use_seq
self.check_output()
self.check_output(check_dygraph=False)
class TestFusionLSTMOpInit(TestFusionLSTMOp):
......
......@@ -95,7 +95,7 @@ class TestFusionGRUOp(OpTest):
def test_check_output(self):
for use_seq in {True, False}:
self.attrs['use_seq'] = use_seq
self.check_output()
self.check_output(check_dygraph=False)
class TestFusionGRUOpNoInitial(TestFusionGRUOp):
......
......@@ -116,7 +116,7 @@ class TestFusionLSTMOp(OpTest):
def test_check_output(self):
for use_seq in {True, False}:
self.attrs['use_seq'] = use_seq
self.check_output()
self.check_output(check_dygraph=False)
class TestFusionLSTMOpInit(TestFusionLSTMOp):
......
......@@ -92,7 +92,7 @@ class TestFusionSeqExpandConcatFCOp(OpTest):
self.attrs = {'fc_activation': self.fc_act}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestFusionSECFCOpNonBias(TestFusionSeqExpandConcatFCOp):
......
......@@ -155,10 +155,11 @@ class TestGRUOp(OpTest):
}
def test_check_output(self):
self.check_output(atol=1e-8, check_dygraph=True)
self.check_output(atol=1e-8, check_dygraph=False)
def test_check_grad(self):
self.check_grad(['Input', 'H0', 'Weight', 'Bias'], ['Hidden'])
self.check_grad(
['Input', 'H0', 'Weight', 'Bias'], ['Hidden'], check_dygraph=False)
class TestGRUOriginMode(TestGRUOp):
......@@ -207,7 +208,8 @@ class TestGRUOpNoInitial(TestGRUOp):
self.with_h0 = False
def test_check_grad(self):
self.check_grad(['Input', 'Weight', 'Bias'], ['Hidden'])
self.check_grad(
['Input', 'Weight', 'Bias'], ['Hidden'], check_dygraph=False)
class TestGRUOpNoBias(TestGRUOp):
......@@ -215,7 +217,8 @@ class TestGRUOpNoBias(TestGRUOp):
self.with_bias = False
def test_check_grad(self):
self.check_grad(['Input', 'H0', 'Weight'], ['Hidden'])
self.check_grad(
['Input', 'H0', 'Weight'], ['Hidden'], check_dygraph=False)
class TestGRUOpReverse(TestGRUOp):
......
......@@ -221,7 +221,6 @@ class TestDygraphPtbRnn(unittest.TestCase):
init_scale = 0.1
batch_size = 4
batch_num = 200
traced_layer = None
with fluid.dygraph.guard():
......
......@@ -24,7 +24,7 @@ from op_test import OpTest
class TestIOUSimilarityOp(OpTest):
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def setUp(self):
self.op_type = "iou_similarity"
......@@ -56,7 +56,7 @@ class TestIOUSimilarityOp(OpTest):
class TestIOUSimilarityOpWithLoD(TestIOUSimilarityOp):
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def setUp(self):
super(TestIOUSimilarityOpWithLoD, self).setUp()
......
......@@ -188,7 +188,7 @@ class TestLstmOp(OpTest):
}
def test_check_output(self):
self.check_output(atol=1e-8)
self.check_output(atol=1e-8, check_dygraph=False)
def test_check_grad(self):
# TODO(qingqing) remove folowing lines after the check_grad is refined.
......@@ -197,7 +197,9 @@ class TestLstmOp(OpTest):
self.outputs['BatchCellPreAct'] = np.zeros(
(N, self.D)).astype('float64')
self.check_grad(
['Input', 'Weight', 'Bias'], ['Hidden'], max_relative_error=5e-4)
['Input', 'Weight', 'Bias'], ['Hidden'],
max_relative_error=5e-4,
check_dygraph=False)
class TestLstmOpCase1(TestLstmOp):
......
......@@ -185,7 +185,7 @@ class TestLstmpOp(LstmTest.TestLstmOp):
}
def test_check_output(self):
self.check_output(atol=1e-8)
self.check_output(atol=1e-8, check_dygraph=False)
def test_check_grad(self):
# TODO(qingqing) remove folowing lines after the check_grad is refined.
......@@ -197,7 +197,8 @@ class TestLstmpOp(LstmTest.TestLstmOp):
self.check_grad(
['Input', 'Weight', 'ProjWeight', 'Bias'], ['Projection'],
max_relative_error=1e-2,
numeric_grad_delta=0.0000005)
numeric_grad_delta=0.0000005,
check_dygraph=False)
class TestLstmpOpHasInitial(TestLstmpOp):
......@@ -215,7 +216,8 @@ class TestLstmpOpHasInitial(TestLstmpOp):
['Input', 'Weight', 'ProjWeight', 'Bias', 'H0', 'C0'],
['Projection'],
numeric_grad_delta=0.0000005,
max_relative_error=1e-2)
max_relative_error=1e-2,
check_dygraph=False)
def test_check_grad_ingore_bias(self):
N = len(self.lod[0])
......@@ -227,7 +229,8 @@ class TestLstmpOpHasInitial(TestLstmpOp):
['Input', 'ProjWeight', 'Weight'], ['Projection'],
max_relative_error=1e-2,
numeric_grad_delta=0.0000005,
no_grad_set=set('Bias'))
no_grad_set=set('Bias'),
check_dygraph=False)
def test_check_grad_ingore_weight(self):
N = len(self.lod[0])
......@@ -239,7 +242,8 @@ class TestLstmpOpHasInitial(TestLstmpOp):
['Input', 'ProjWeight', 'Bias'], ['Projection'],
max_relative_error=1e-2,
numeric_grad_delta=0.0000005,
no_grad_set=set('Weight'))
no_grad_set=set('Weight'),
check_dygraph=False)
def test_check_grad_ingore_proj_weight(self):
N = len(self.lod[0])
......@@ -251,7 +255,8 @@ class TestLstmpOpHasInitial(TestLstmpOp):
['Input', 'Weight', 'Bias'], ['Projection'],
max_relative_error=1e-2,
numeric_grad_delta=0.0000005,
no_grad_set=set('ProjWeight'))
no_grad_set=set('ProjWeight'),
check_dygraph=False)
def test_check_grad_ingore_input(self):
N = len(self.lod[0])
......@@ -263,7 +268,8 @@ class TestLstmpOpHasInitial(TestLstmpOp):
['Weight', 'ProjWeight', 'Bias'], ['Projection'],
max_relative_error=1e-2,
numeric_grad_delta=0.0000005,
no_grad_set=set('Input'))
no_grad_set=set('Input'),
check_dygraph=False)
def test_check_grad_ingore_h0(self):
N = len(self.lod[0])
......@@ -275,7 +281,8 @@ class TestLstmpOpHasInitial(TestLstmpOp):
['Input', 'Weight', 'ProjWeight', 'Bias', 'C0'], ['Projection'],
max_relative_error=1e-2,
numeric_grad_delta=0.0000005,
no_grad_set=set('H0'))
no_grad_set=set('H0'),
check_dygraph=False)
def test_check_grad_ingore_c0(self):
N = len(self.lod[0])
......@@ -287,7 +294,8 @@ class TestLstmpOpHasInitial(TestLstmpOp):
['Input', 'Weight', 'ProjWeight', 'Bias', 'H0'], ['Projection'],
max_relative_error=1e-2,
numeric_grad_delta=0.0000005,
no_grad_set=set('C0'))
no_grad_set=set('C0'),
check_dygraph=False)
class TestLstmpOpRerverse(TestLstmpOp):
......
......@@ -71,10 +71,11 @@ class TestMatchMatrixTensorOp(OpTest):
self.outputs = {'Out': (out, out_lod), 'Tmp': tmp}
def test_check_output(self):
self.check_output(check_compile_vs_runtime=True)
self.check_output(check_compile_vs_runtime=True, check_dygraph=False)
def test_check_grad(self):
self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.005)
self.check_grad(
['X', 'Y'], 'Out', max_relative_error=0.005, check_dygraph=False)
class TestMatchMatrixTensorOpCase1(TestMatchMatrixTensorOp):
......
......@@ -45,7 +45,7 @@ class TestOneHotOp(OpTest):
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestOneHotOp_attr(OpTest):
......@@ -68,7 +68,7 @@ class TestOneHotOp_attr(OpTest):
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestOneHotOp_default_dtype(OpTest):
......@@ -92,7 +92,7 @@ class TestOneHotOp_default_dtype(OpTest):
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestOneHotOp_default_dtype_attr(OpTest):
......@@ -115,7 +115,7 @@ class TestOneHotOp_default_dtype_attr(OpTest):
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestOneHotOp_out_of_range(OpTest):
......@@ -134,7 +134,7 @@ class TestOneHotOp_out_of_range(OpTest):
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestOneHotOp_exception(OpTest):
......
......@@ -44,7 +44,7 @@ class TestOneHotOp(OpTest):
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestOneHotOp_attr(OpTest):
......@@ -67,7 +67,7 @@ class TestOneHotOp_attr(OpTest):
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestOneHotOp_default_dtype(OpTest):
......@@ -90,7 +90,7 @@ class TestOneHotOp_default_dtype(OpTest):
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestOneHotOp_default_dtype_attr(OpTest):
......@@ -113,7 +113,7 @@ class TestOneHotOp_default_dtype_attr(OpTest):
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestOneHotOp_out_of_range(OpTest):
......@@ -131,7 +131,7 @@ class TestOneHotOp_out_of_range(OpTest):
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
class TestOneHotOp_exception(OpTest):
......
......@@ -60,18 +60,30 @@ class TestRowConvOp1(OpTest):
self.outputs = {'Out': (out, lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad_normal(self):
self.check_grad(['X', 'Filter'], 'Out', max_relative_error=0.05)
self.check_grad(
['X', 'Filter'],
'Out',
max_relative_error=0.05,
check_dygraph=False)
def test_check_grad_ignore_x(self):
self.check_grad(
['Filter'], 'Out', max_relative_error=0.05, no_grad_set=set('X'))
['Filter'],
'Out',
max_relative_error=0.05,
no_grad_set=set('X'),
check_dygraph=False)
def test_check_grad_ignore_wt(self):
self.check_grad(
['X'], 'Out', max_relative_error=0.05, no_grad_set=set('Filter'))
['X'],
'Out',
max_relative_error=0.05,
no_grad_set=set('Filter'),
check_dygraph=False)
class TestRowConvOp2(OpTest):
......@@ -91,21 +103,33 @@ class TestRowConvOp2(OpTest):
self.outputs = {'Out': (out, lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
#max_relative_error is increased from 0.05 to 0.06 as for higher
#dimensional input, the dX on CPU for some values has max_rel_error
#slightly more than 0.05
def test_check_grad_normal(self):
self.check_grad(['X', 'Filter'], 'Out', max_relative_error=0.06)
self.check_grad(
['X', 'Filter'],
'Out',
max_relative_error=0.06,
check_dygraph=False)
def test_check_grad_ignore_x(self):
self.check_grad(
['Filter'], 'Out', max_relative_error=0.06, no_grad_set=set('X'))
['Filter'],
'Out',
max_relative_error=0.06,
no_grad_set=set('X'),
check_dygraph=False)
def test_check_grad_ignore_wt(self):
self.check_grad(
['X'], 'Out', max_relative_error=0.06, no_grad_set=set('Filter'))
['X'],
'Out',
max_relative_error=0.06,
no_grad_set=set('Filter'),
check_dygraph=False)
def row_conv_foward_Tensor(x, wt):
......@@ -141,18 +165,30 @@ class TestRowOpWithTensorInput(OpTest):
self.outputs = {'Out': out}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad_ignore_x(self):
self.check_grad(
['Filter'], 'Out', max_relative_error=0.05, no_grad_set=set('X'))
['Filter'],
'Out',
max_relative_error=0.05,
no_grad_set=set('X'),
check_dygraph=False)
def test_check_grad_normal(self):
self.check_grad(['X', 'Filter'], 'Out', max_relative_error=0.05)
self.check_grad(
['X', 'Filter'],
'Out',
max_relative_error=0.05,
check_dygraph=False)
def test_check_grad_ignore_wt(self):
self.check_grad(
['X'], 'Out', max_relative_error=0.05, no_grad_set=set('Filter'))
['X'],
'Out',
max_relative_error=0.05,
no_grad_set=set('Filter'),
check_dygraph=False)
if __name__ == '__main__':
......
......@@ -77,7 +77,7 @@ class TestSeqAvgPool(OpTest):
self.outputs = {'Out': (out, [self.set_lod()[0]])}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
# Remove MaxIndex after check_grad is refined.
......@@ -85,7 +85,7 @@ class TestSeqAvgPool(OpTest):
if isinstance(out, tuple): out = out[0]
self.outputs['MaxIndex'] = \
np.zeros(out.shape).astype('int32')
self.check_grad(["X"], "Out")
self.check_grad(["X"], "Out", check_dygraph=False)
class TestSeqAvgPoolLen0(TestSeqAvgPool):
......@@ -298,7 +298,8 @@ class TestSeqSqrtPool2D(TestSeqAvgPool2D):
out = out[0]
self.outputs['MaxIndex'] = \
np.zeros(out.shape).astype('int32')
self.check_grad(["X"], "Out", max_relative_error=0.06)
self.check_grad(
["X"], "Out", max_relative_error=0.06, check_dygraph=False)
class TestSeqSqrtPool2DLen0(TestSeqSqrtPool2D):
......
......@@ -72,10 +72,10 @@ class TestSequenceExpand(OpTest):
self.compute()
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
self.check_grad(["X"], "Out")
self.check_grad(["X"], "Out", check_dygraph=False)
class TestSequenceExpandCase1(TestSequenceExpand):
......
......@@ -49,10 +49,10 @@ class TestSequenceExpandAs(OpTest):
self.outputs = {'Out': (out_data, y_lod)}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
self.check_grad(["X"], "Out")
self.check_grad(["X"], "Out", check_dygraph=False)
class TestSequenceExpandAsCase1(TestSequenceExpandAs):
......
......@@ -72,10 +72,10 @@ class TestSequencePadOp(OpTest):
self.compute()
def test_check_output(self):
self.check_output(check_compile_vs_runtime=True)
self.check_output(check_compile_vs_runtime=True, check_dygraph=False)
def test_check_grad(self):
self.check_grad(["X"], "Out")
self.check_grad(["X"], "Out", check_dygraph=False)
class TestSequencePadOp2(TestSequencePadOp):
......
......@@ -47,10 +47,10 @@ class TestSequenceReverseBase(OpTest):
return np.reshape(tmp_y, newshape=self.x.shape).astype(self.dtype)
def test_output(self):
self.check_output(0)
self.check_output(0, check_dygraph=False)
def test_grad(self):
self.check_grad(['X'], 'Y')
self.check_grad(['X'], 'Y', check_dygraph=False)
class TestSequenceReserve1(TestSequenceReverseBase):
......
......@@ -47,10 +47,10 @@ class TestSequenceScatterOp(OpTest):
self.outputs = {'Out': Out_data}
def test_check_output(self):
self.check_output()
self.check_output(check_dygraph=False)
def test_check_grad(self):
self.check_grad(['Updates'], 'Out', in_place=True)
self.check_grad(['Updates'], 'Out', in_place=True, check_dygraph=False)
class TestSequenceScatterOpSeqLen0(TestSequenceScatterOp):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册