|
|
|
@ -110,22 +110,125 @@ class CompileTimeInferShapeContext : public InferShapeContext {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<InferShapeVarPtr> GetInputVarPtrs(
|
|
|
|
|
const std::string &name) override {
|
|
|
|
|
const std::vector<std::string> arg_names = Inputs(name);
|
|
|
|
|
std::vector<InferShapeVarPtr> res;
|
|
|
|
|
res.reserve(arg_names.size());
|
|
|
|
|
std::transform(arg_names.begin(), arg_names.end(), std::back_inserter(res),
|
|
|
|
|
[this](const std::string &name) {
|
|
|
|
|
return block_.FindVarRecursive(name);
|
|
|
|
|
});
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<InferShapeVarPtr> GetOutputVarPtrs(
|
|
|
|
|
const std::string &name) override {
|
|
|
|
|
const std::vector<std::string> arg_names = Outputs(name);
|
|
|
|
|
std::vector<InferShapeVarPtr> res;
|
|
|
|
|
res.reserve(arg_names.size());
|
|
|
|
|
std::transform(arg_names.begin(), arg_names.end(), std::back_inserter(res),
|
|
|
|
|
[this](const std::string &name) {
|
|
|
|
|
return block_.FindVarRecursive(name);
|
|
|
|
|
});
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DDim GetInputDim(const std::string &name) const override {
|
|
|
|
|
const std::vector<std::string> &arg_names = Inputs(name);
|
|
|
|
|
PADDLE_ENFORCE_EQ(arg_names.size(), 1UL,
|
|
|
|
|
"Input(%s) should hold one element, but now it holds %d",
|
|
|
|
|
name, arg_names.size());
|
|
|
|
|
return this->GetDim(arg_names[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<DDim> GetInputsDim(const std::string &name) const override {
|
|
|
|
|
const std::vector<std::string> &arg_names = Inputs(name);
|
|
|
|
|
return GetDims(arg_names);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IsRuntime() const override;
|
|
|
|
|
|
|
|
|
|
std::vector<proto::VarType::Type> GetInputsVarType(
|
|
|
|
|
const std::string &name) const override {
|
|
|
|
|
return GetVarTypes(Inputs(name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<proto::VarType::Type> GetOutputsVarType(
|
|
|
|
|
const std::string &name) const override {
|
|
|
|
|
return GetVarTypes(Outputs(name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetOutputDim(const std::string &name, const DDim &dim) override {
|
|
|
|
|
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());
|
|
|
|
|
SetDim(arg_names[0], dim);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetOutputsDim(const std::string &name,
|
|
|
|
|
const std::vector<DDim> &dims) override {
|
|
|
|
|
auto &names = Outputs(name);
|
|
|
|
|
SetDims(names, dims);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
proto::VarType::Type GetVarType(const std::string &name) const override;
|
|
|
|
|
std::vector<proto::VarType::Type> GetVarTypes(
|
|
|
|
|
const std::vector<std::string> &names) const {
|
|
|
|
|
std::vector<proto::VarType::Type> retv;
|
|
|
|
|
retv.resize(names.size());
|
|
|
|
|
std::transform(
|
|
|
|
|
names.begin(), names.end(), retv.begin(),
|
|
|
|
|
std::bind(std::mem_fn(&CompileTimeInferShapeContext::GetVarType), this,
|
|
|
|
|
std::placeholders::_1));
|
|
|
|
|
return retv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
proto::VarType::Type GetVarType(const std::string &name) const;
|
|
|
|
|
|
|
|
|
|
DDim GetDim(const std::string &name) const {
|
|
|
|
|
auto var = block_.FindVarRecursive(name);
|
|
|
|
|
PADDLE_ENFORCE(var != nullptr, "Cannot find variable %s", name);
|
|
|
|
|
DDim res;
|
|
|
|
|
try {
|
|
|
|
|
auto shape = var->GetShape();
|
|
|
|
|
res = shape.empty() ? make_ddim({0UL}) : make_ddim(shape);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
VLOG(5) << "GetDim of variable " << name << " error";
|
|
|
|
|
std::rethrow_exception(std::current_exception());
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DDim GetDim(const std::string &name) const override;
|
|
|
|
|
std::vector<DDim> GetDims(const std::vector<std::string> &names) const {
|
|
|
|
|
std::vector<DDim> ret;
|
|
|
|
|
ret.reserve(names.size());
|
|
|
|
|
std::transform(
|
|
|
|
|
names.begin(), names.end(), std::back_inserter(ret),
|
|
|
|
|
[this](const std::string &name) { return this->GetDim(name); });
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetDim(const std::string &name, const DDim &dim);
|
|
|
|
|
|
|
|
|
|
void SetDim(const std::string &name, const DDim &dim) override;
|
|
|
|
|
void SetDims(const std::vector<std::string> &names,
|
|
|
|
|
const std::vector<DDim> &dims) {
|
|
|
|
|
size_t length = names.size();
|
|
|
|
|
PADDLE_ENFORCE_EQ(length, dims.size());
|
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
|
if (names[i] == framework::kEmptyVarName) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
SetDim(names[i], dims[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<DDim> GetRepeatedDims(const std::string &name) const override;
|
|
|
|
|
|
|
|
|
|
void SetRepeatedDims(const std::string &name,
|
|
|
|
|
const std::vector<DDim> &dims) override;
|
|
|
|
|
|
|
|
|
|
InferShapeVarPtr GetVarPtr(const std::string &name) override;
|
|
|
|
|
|
|
|
|
|
const OpDesc &op_;
|
|
|
|
|
const BlockDesc &block_;
|
|
|
|
|
};
|
|
|
|
@ -644,20 +747,6 @@ const std::vector<std::string> &CompileTimeInferShapeContext::Outputs(
|
|
|
|
|
return op_.Output(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DDim CompileTimeInferShapeContext::GetDim(const std::string &name) const {
|
|
|
|
|
auto var = block_.FindVarRecursive(name);
|
|
|
|
|
PADDLE_ENFORCE(var != nullptr, "Cannot find variable %s", name);
|
|
|
|
|
DDim res;
|
|
|
|
|
try {
|
|
|
|
|
auto shape = var->GetShape();
|
|
|
|
|
res = shape.empty() ? make_ddim({0UL}) : make_ddim(shape);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
VLOG(5) << "GetDim of variable " << name << " error";
|
|
|
|
|
std::rethrow_exception(std::current_exception());
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<DDim> CompileTimeInferShapeContext::GetRepeatedDims(
|
|
|
|
|
const std::string &name) const {
|
|
|
|
|
auto var = block_.FindVarRecursive(name);
|
|
|
|
@ -696,10 +785,5 @@ proto::VarType::Type CompileTimeInferShapeContext::GetVarType(
|
|
|
|
|
return block_.FindVarRecursive(name)->GetType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InferShapeVarPtr CompileTimeInferShapeContext::GetVarPtr(
|
|
|
|
|
const std::string &name) {
|
|
|
|
|
return block_.FindVarRecursive(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace framework
|
|
|
|
|
} // namespace paddle
|
|
|
|
|