未验证 提交 e0e534ab 编写于 作者: C Chen Weihang 提交者: GitHub

Optimize attribute selected performence (#42294) (#42368)

* opt attr eaque perf

* opt attr select code

* fix one hot infermeta

* polish get attr impl

* fix tests failed

* add testcases
上级 d04a68d3
...@@ -242,7 +242,7 @@ class AttrReader { ...@@ -242,7 +242,7 @@ class AttrReader {
return *attr_value; return *attr_value;
} }
inline const Attribute& GetAttr(const std::string& name) const { const Attribute* GetAttr(const std::string& name) const {
auto it = attrs_.find(name); auto it = attrs_.find(name);
bool found = it != attrs_.end(); bool found = it != attrs_.end();
if (!found) { if (!found) {
...@@ -251,11 +251,10 @@ class AttrReader { ...@@ -251,11 +251,10 @@ class AttrReader {
found = it != default_attrs_->end(); found = it != default_attrs_->end();
} }
} }
PADDLE_ENFORCE_EQ(found, true, if (found) {
platform::errors::NotFound( return &it->second;
"Attribute (%s) should be in AttributeMap.", name)); }
return nullptr;
return it->second;
} }
private: private:
......
...@@ -52,8 +52,11 @@ class InferShapeArgumentMappingContext : public phi::ArgumentMappingContext { ...@@ -52,8 +52,11 @@ class InferShapeArgumentMappingContext : public phi::ArgumentMappingContext {
} }
paddle::any Attr(const std::string& name) const override { paddle::any Attr(const std::string& name) const override {
auto& attr = ctx_.Attrs().GetAttr(name); auto* attr = ctx_.Attrs().GetAttr(name);
return GetAttrValue(attr); PADDLE_ENFORCE_NOT_NULL(
attr, platform::errors::NotFound(
"Attribute (%s) should be in AttributeMap.", name));
return GetAttrValue(*attr);
} }
size_t InputSize(const std::string& name) const override { size_t InputSize(const std::string& name) const override {
...@@ -450,9 +453,78 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx, ...@@ -450,9 +453,78 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx,
auto attr_reader = ctx->Attrs(); auto attr_reader = ctx->Attrs();
for (size_t i = 0; i < attr_names.size(); ++i) { for (size_t i = 0; i < attr_names.size(); ++i) {
auto& attr_name = attr_names[i]; auto& attr_name = attr_names[i];
if (attr_defs[i].type_index == phi::AttributeType::INT_ARRAY) { VLOG(6) << "BuildInferMetaContext: " << attr_name << ": "
<< attr_defs[i].type_index;
auto* attr_ptr = attr_reader.GetAttr(attr_name);
switch (attr_defs[i].type_index) {
case phi::AttributeType::SCALAR:
if (attr_ptr) {
auto& attr = *attr_ptr;
switch (AttrTypeID(attr)) {
case framework::proto::AttrType::FLOAT:
infer_meta_context.EmplaceBackAttr(
phi::Scalar(BOOST_GET_CONST(float, attr)));
break;
case framework::proto::AttrType::INT:
infer_meta_context.EmplaceBackAttr(
phi::Scalar(BOOST_GET_CONST(int, attr)));
break;
case framework::proto::AttrType::STRING:
infer_meta_context.EmplaceBackAttr(
phi::Scalar(BOOST_GET_CONST(std::string, attr)));
break;
default:
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to Scalar when construct "
"InferMetaContext.",
attr_name));
}
} else if (ctx->HasInput(attr_name)) {
auto infershape_input = std::move(ctx->GetInputVarPtrs(attr_name));
if (infershape_input.size() == 1) {
if (ctx->IsRuntime()) {
Variable* var = BOOST_GET_CONST(Variable*, infershape_input[0]);
infer_meta_context.EmplaceBackAttr(
std::move(experimental::MakePhiScalarFromVar(*var)));
} else {
phi::Scalar tensor_scalar(-1);
tensor_scalar.SetFromTensor(true);
infer_meta_context.EmplaceBackAttr(std::move(tensor_scalar));
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"Invalid input.size() when cast op attribute `%s` to Scalar, "
"expected 1, but actually is %d .",
attr_name, infershape_input.size()));
}
} else {
// do nothing, skip current attr
}
break;
case phi::AttributeType::INT_ARRAY:
// When attr is a vector_tensor or tensor, transform it to IntArray // When attr is a vector_tensor or tensor, transform it to IntArray
if (ctx->HasInputs(attr_name) || ctx->HasInput(attr_name)) { if (attr_ptr) {
auto& attr = *attr_ptr;
switch (AttrTypeID(attr)) {
case framework::proto::AttrType::INTS:
infer_meta_context.EmplaceBackAttr(std::move(
phi::IntArray(BOOST_GET_CONST(std::vector<int32_t>, attr))));
break;
case framework::proto::AttrType::LONGS:
infer_meta_context.EmplaceBackAttr(std::move(
phi::IntArray(BOOST_GET_CONST(std::vector<int64_t>, attr))));
break;
case framework::proto::AttrType::INT:
infer_meta_context.EmplaceBackAttr(
phi::IntArray({BOOST_GET_CONST(int, attr)}));
break;
default:
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to IntArray when "
"construct InferMetaContext.",
attr_name));
}
} else if (ctx->HasInputs(attr_name) || ctx->HasInput(attr_name)) {
auto infershape_inputs = std::move(ctx->GetInputVarPtrs(attr_name)); auto infershape_inputs = std::move(ctx->GetInputVarPtrs(attr_name));
if (ctx->IsRuntime()) { if (ctx->IsRuntime()) {
// If is in runtime, we will get tensor's value for IntArray // If is in runtime, we will get tensor's value for IntArray
...@@ -499,65 +571,15 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx, ...@@ -499,65 +571,15 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx,
tensor_attr.SetFromTensor(true); tensor_attr.SetFromTensor(true);
infer_meta_context.EmplaceBackAttr(std::move(tensor_attr)); infer_meta_context.EmplaceBackAttr(std::move(tensor_attr));
} }
} else if (ctx->HasAttr(attr_name)) {
auto& attr = attr_reader.GetAttr(attr_name);
if (AttrTypeID(attr) == proto::AttrType::INTS) {
infer_meta_context.EmplaceBackAttr(std::move(
phi::IntArray(BOOST_GET_CONST(std::vector<int32_t>, attr))));
} else if (AttrTypeID(attr) == proto::AttrType::LONGS) {
infer_meta_context.EmplaceBackAttr(std::move(
phi::IntArray(BOOST_GET_CONST(std::vector<int64_t>, attr))));
} else if (AttrTypeID(attr) == proto::AttrType::INT) {
infer_meta_context.EmplaceBackAttr(
phi::IntArray({BOOST_GET_CONST(int, attr)}));
} else {
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to IntArray when "
"construct InferMetaContext.",
attr_name));
}
}
} else if (attr_defs[i].type_index == phi::AttributeType::SCALAR) {
if (ctx->HasAttr(attr_name)) {
// TODO(chentianyu03): support other attrs later
auto& attr = attr_reader.GetAttr(attr_name);
if (AttrTypeID(attr) == proto::AttrType::FLOAT) {
infer_meta_context.EmplaceBackAttr(
phi::Scalar(BOOST_GET_CONST(float, attr)));
} else if (AttrTypeID(attr) == proto::AttrType::STRING) {
infer_meta_context.EmplaceBackAttr(
phi::Scalar(BOOST_GET_CONST(std::string, attr)));
} else if (AttrTypeID(attr) == proto::AttrType::INT) {
infer_meta_context.EmplaceBackAttr(
phi::Scalar(BOOST_GET_CONST(int, attr)));
} else { } else {
PADDLE_THROW(platform::errors::Unimplemented( // do nothing, skip current attr
"Unsupported cast op attribute `%s` to Scalar when construct " }
"InferMetaContext.", break;
attr_name)); case phi::AttributeType::SCALARS:
} if (attr_ptr) {
} else if (ctx->HasInput(attr_name)) { auto& attr = *attr_ptr;
auto infershape_input = std::move(ctx->GetInputVarPtrs(attr_name)); switch (AttrTypeID(attr)) {
if (infershape_input.size() == 1) { case framework::proto::AttrType::INTS: {
if (ctx->IsRuntime()) {
Variable* var = BOOST_GET_CONST(Variable*, infershape_input[0]);
infer_meta_context.EmplaceBackAttr(
std::move(experimental::MakePhiScalarFromVar(*var)));
} else {
phi::Scalar tensor_scalar(-1);
tensor_scalar.SetFromTensor(true);
infer_meta_context.EmplaceBackAttr(std::move(tensor_scalar));
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"Invalid input.size() when cast op attribute `%s` to Scalar, "
"expected 1, but actually is %d .",
attr_name, infershape_input.size()));
}
}
} else if (attr_defs[i].type_index == phi::AttributeType::SCALARS) {
auto& attr = attr_reader.GetAttr(attr_name);
if (AttrTypeID(attr) == proto::AttrType::INTS) {
const auto& vec = BOOST_GET_CONST(std::vector<int32_t>, attr); const auto& vec = BOOST_GET_CONST(std::vector<int32_t>, attr);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
...@@ -565,7 +587,8 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx, ...@@ -565,7 +587,8 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx,
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
infer_meta_context.EmplaceBackAttr(std::move(scalar_list)); infer_meta_context.EmplaceBackAttr(std::move(scalar_list));
} else if (AttrTypeID(attr) == proto::AttrType::LONGS) { } break;
case framework::proto::AttrType::LONGS: {
const auto& vec = BOOST_GET_CONST(std::vector<int64_t>, attr); const auto& vec = BOOST_GET_CONST(std::vector<int64_t>, attr);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
...@@ -573,7 +596,8 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx, ...@@ -573,7 +596,8 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx,
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
infer_meta_context.EmplaceBackAttr(std::move(scalar_list)); infer_meta_context.EmplaceBackAttr(std::move(scalar_list));
} else if (AttrTypeID(attr) == proto::AttrType::FLOATS) { } break;
case framework::proto::AttrType::FLOATS: {
const auto& vec = BOOST_GET_CONST(std::vector<float>, attr); const auto& vec = BOOST_GET_CONST(std::vector<float>, attr);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
...@@ -581,7 +605,8 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx, ...@@ -581,7 +605,8 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx,
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
infer_meta_context.EmplaceBackAttr(std::move(scalar_list)); infer_meta_context.EmplaceBackAttr(std::move(scalar_list));
} else if (AttrTypeID(attr) == proto::AttrType::FLOAT64S) { } break;
case framework::proto::AttrType::FLOAT64S: {
const auto& vec = BOOST_GET_CONST(std::vector<double>, attr); const auto& vec = BOOST_GET_CONST(std::vector<double>, attr);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
...@@ -589,76 +614,93 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx, ...@@ -589,76 +614,93 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx,
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
infer_meta_context.EmplaceBackAttr(std::move(scalar_list)); infer_meta_context.EmplaceBackAttr(std::move(scalar_list));
} else { } break;
default:
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to vector<Scalar> when " "Unsupported cast op attribute `%s` to vector<Scalar> when "
"construct InferMetaContext.", "construct KernelContext.",
attr_names[i])); attr_names[i]));
} }
} else if (ctx->HasAttr(attr_name)) { } else {
// Emplace Back Attr according to the type of attr. // do nothing, skip current attr
auto& attr = attr_reader.GetAttr(attr_name); }
if (attr_defs[i].type_index == phi::AttributeType::BOOL) { break;
infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(bool, attr)); default:
} else if (attr_defs[i].type_index == phi::AttributeType::INT32) { if (attr_ptr) {
infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(int, attr)); auto& attr = *attr_ptr;
} else if (attr_defs[i].type_index == phi::AttributeType::INT64) { switch (attr_defs[i].type_index) {
infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(int64_t, attr)); case phi::AttributeType::FLOAT32:
} else if (attr_defs[i].type_index == phi::AttributeType::FLOAT32) {
infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(float, attr)); infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(float, attr));
} else if (attr_defs[i].type_index == phi::AttributeType::STRING) { break;
infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(std::string, attr)); case phi::AttributeType::INT32:
} else if (attr_defs[i].type_index == phi::AttributeType::BOOLS) { infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(int, attr));
break;
case phi::AttributeType::BOOL:
infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(bool, attr));
break;
case phi::AttributeType::INT64:
infer_meta_context.EmplaceBackAttr( infer_meta_context.EmplaceBackAttr(
BOOST_GET_CONST(std::vector<bool>, attr)); BOOST_GET_CONST(int64_t, attr));
} else if (attr_defs[i].type_index == phi::AttributeType::INT32S) { break;
case phi::AttributeType::INT32S:
infer_meta_context.EmplaceBackAttr( infer_meta_context.EmplaceBackAttr(
BOOST_GET_CONST(std::vector<int>, attr)); BOOST_GET_CONST(std::vector<int>, attr));
} else if (attr_defs[i].type_index == phi::AttributeType::INT64S) { break;
if (AttrTypeID(attr) == proto::AttrType::INTS) { case phi::AttributeType::DATA_TYPE: {
// Emplace Back Attr according to the type of Phi_Kernel args. auto data_type = paddle::framework::TransToPhiDataType(
const auto& vector_int_attr = BOOST_GET_CONST(std::vector<int>, attr); static_cast<framework::proto::VarType::Type>(
const std::vector<int64_t> vector_int64_attr(vector_int_attr.begin(), BOOST_GET_CONST(int, attr)));
vector_int_attr.end()); infer_meta_context.EmplaceBackAttr(data_type);
infer_meta_context.EmplaceBackAttr(vector_int64_attr); } break;
} else { case phi::AttributeType::STRING:
infer_meta_context.EmplaceBackAttr(
BOOST_GET_CONST(std::string, attr));
break;
case phi::AttributeType::INT64S:
switch (AttrTypeID(attr)) {
case framework::proto::AttrType::LONGS:
infer_meta_context.EmplaceBackAttr( infer_meta_context.EmplaceBackAttr(
BOOST_GET_CONST(std::vector<int64_t>, attr)); BOOST_GET_CONST(std::vector<int64_t>, attr));
break;
case framework::proto::AttrType::INTS: {
const auto& vector_int_attr =
BOOST_GET_CONST(std::vector<int>, attr);
const std::vector<int64_t> vector_int64_attr(
vector_int_attr.begin(), vector_int_attr.end());
infer_meta_context.EmplaceBackAttr(vector_int64_attr);
} break;
default:
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to vector<int64_t> "
"when "
"construct KernelContext.",
attr_names[i]));
} }
} else if (attr_defs[i].type_index == phi::AttributeType::FLOAT32S) { break;
case phi::AttributeType::FLOAT32S:
infer_meta_context.EmplaceBackAttr( infer_meta_context.EmplaceBackAttr(
BOOST_GET_CONST(std::vector<float>, attr)); BOOST_GET_CONST(std::vector<float>, attr));
} else if (attr_defs[i].type_index == phi::AttributeType::FLOAT64S) { break;
infer_meta_context.EmplaceBackAttr( case phi::AttributeType::STRINGS:
BOOST_GET_CONST(std::vector<double>, attr));
} else if (attr_defs[i].type_index == phi::AttributeType::STRINGS) {
infer_meta_context.EmplaceBackAttr( infer_meta_context.EmplaceBackAttr(
BOOST_GET_CONST(std::vector<std::string>, attr)); BOOST_GET_CONST(std::vector<std::string>, attr));
} else if (attr_defs[i].type_index == phi::AttributeType::DATA_TYPE) { break;
auto data_type = paddle::framework::TransToPhiDataType( case phi::AttributeType::BOOLS:
static_cast<framework::proto::VarType::Type>( infer_meta_context.EmplaceBackAttr(
BOOST_GET_CONST(int, attr))); BOOST_GET_CONST(std::vector<bool>, attr));
infer_meta_context.EmplaceBackAttr(data_type); break;
} else { case phi::AttributeType::FLOAT64S:
infer_meta_context.EmplaceBackAttr(
BOOST_GET_CONST(std::vector<double>, attr));
break;
default:
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported attribute type is received when call " "Unsupported cast op attribute `%s` when construct "
"InferShapeFunctor.")); "KernelContext in dygraph.",
} attr_names[i]));
} else if (ctx->HasInput(attr_name)) {
// convert from data
if (attr_defs[i].type_index == phi::AttributeType::INT32) {
if (ctx->IsRuntime()) {
auto infershape_inputs = std::move(ctx->GetInputVarPtrs(attr_name));
auto var_temp = BOOST_GET_CONST(Variable*, infershape_inputs[i]);
auto val = experimental::MakePhiScalarFromVar(*var_temp);
int32_t val_int = val.template to<int32_t>();
infer_meta_context.EmplaceBackAttr(val_int);
} else {
infer_meta_context.EmplaceBackAttr(-1);
} }
} else { } else {
PADDLE_THROW(platform::errors::Unimplemented( // do nothing, skip currnet attr
"Get value from variable only support int yet"));
} }
} }
} }
......
...@@ -2419,20 +2419,58 @@ void OperatorWithKernel::BuildPhiKernelContext( ...@@ -2419,20 +2419,58 @@ void OperatorWithKernel::BuildPhiKernelContext(
VLOG(4) << "Done outputs"; VLOG(4) << "Done outputs";
for (size_t i = 0; i < attr_names.size(); ++i) { for (size_t i = 0; i < attr_names.size(); ++i) {
if (attr_defs[i].type_index == phi::AttributeType::INT_ARRAY) { VLOG(6) << "BuildPhiKernelContext: " << attr_names[i] << ": "
<< attr_defs[i].type_index;
auto attr_iter = Attrs().find(attr_names[i]); auto attr_iter = Attrs().find(attr_names[i]);
if (attr_iter != Attrs().end()) { // shape is in the attribute switch (attr_defs[i].type_index) {
auto& attr = attr_iter->second; case phi::AttributeType::SCALAR:
if (AttrTypeID(attr) == proto::AttrType::LONGS) { if (attr_iter != Attrs().end()) {
// scalar is in the attribute
switch (AttrTypeID(attr_iter->second)) {
case proto::AttrType::FLOAT:
pt_kernel_context->EmplaceBackAttr(std::move( pt_kernel_context->EmplaceBackAttr(std::move(
phi::IntArray(BOOST_GET_CONST(std::vector<int64_t>, attr)))); phi::Scalar(BOOST_GET_CONST(float, attr_iter->second))));
} else if (AttrTypeID(attr) == proto::AttrType::INTS) { break;
case proto::AttrType::INT:
pt_kernel_context->EmplaceBackAttr(std::move( pt_kernel_context->EmplaceBackAttr(std::move(
phi::IntArray(BOOST_GET_CONST(std::vector<int32_t>, attr)))); phi::Scalar(BOOST_GET_CONST(int, attr_iter->second))));
} else if (AttrTypeID(attr) == proto::AttrType::INT) { break;
pt_kernel_context->EmplaceBackAttr( case proto::AttrType::STRING:
std::move(phi::IntArray(&BOOST_GET_CONST(int32_t, attr), 1))); pt_kernel_context->EmplaceBackAttr(std::move(phi::Scalar(
} else { BOOST_GET_CONST(std::string, attr_iter->second))));
break;
default:
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to Scalar when construct "
"KernelContext in dygraph.",
attr_names[i]));
}
} else { // scalar is in the input
auto& ins_vector = ctx.inputs.at(attr_names[i]);
pt_kernel_context->EmplaceBackAttr(std::move(
experimental::MakePhiScalarFromVar(*ins_vector.front())));
}
break;
case phi::AttributeType::INT_ARRAY:
if (attr_iter != Attrs().end()) {
switch (AttrTypeID(attr_iter->second)) {
case proto::AttrType::INTS:
pt_kernel_context->EmplaceBackAttr(std::move(phi::IntArray(
BOOST_GET_CONST(std::vector<int32_t>, attr_iter->second))));
break;
case proto::AttrType::LONGS:
pt_kernel_context->EmplaceBackAttr(std::move(phi::IntArray(
BOOST_GET_CONST(std::vector<int64_t>, attr_iter->second))));
break;
case proto::AttrType::INT:
pt_kernel_context->EmplaceBackAttr(std::move(phi::IntArray(
&BOOST_GET_CONST(int32_t, attr_iter->second), 1)));
break;
case proto::AttrType::LONG:
pt_kernel_context->EmplaceBackAttr(std::move(phi::IntArray(
&BOOST_GET_CONST(int64_t, attr_iter->second), 1)));
break;
default:
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to IntArray when " "Unsupported cast op attribute `%s` to IntArray when "
"construct KernelContext.", "construct KernelContext.",
...@@ -2444,141 +2482,150 @@ void OperatorWithKernel::BuildPhiKernelContext( ...@@ -2444,141 +2482,150 @@ void OperatorWithKernel::BuildPhiKernelContext(
pt_kernel_context->EmplaceBackAttr(std::move( pt_kernel_context->EmplaceBackAttr(std::move(
experimental::MakePhiIntArrayFromVar(*ins_vector.front()))); experimental::MakePhiIntArrayFromVar(*ins_vector.front())));
} else { // ShapeTensorList } else { // ShapeTensorList
pt_kernel_context->EmplaceBackAttr( pt_kernel_context->EmplaceBackAttr(std::move(
std::move(experimental::MakePhiIntArrayFromVarList(ins_vector))); experimental::MakePhiIntArrayFromVarList(ins_vector)));
} }
} }
} else if (attr_defs[i].type_index == phi::AttributeType::SCALAR) { break;
auto attr_iter = Attrs().find(attr_names[i]); case phi::AttributeType::SCALARS: {
if (attr_iter != Attrs().end()) { // scalar is in the attribute PADDLE_ENFORCE_NE(
auto& attr = attr_iter->second; attr_iter, Attrs().end(),
if (AttrTypeID(attr) == proto::AttrType::FLOAT) { platform::errors::NotFound("(%s) is not found in AttributeMap when "
pt_kernel_context->EmplaceBackAttr( "buildind static KernelContext.",
std::move(phi::Scalar(BOOST_GET_CONST(float, attr))));
} else if (AttrTypeID(attr) == proto::AttrType::STRING) {
pt_kernel_context->EmplaceBackAttr(
std::move(phi::Scalar(BOOST_GET_CONST(std::string, attr))));
} else if (AttrTypeID(attr) == proto::AttrType::INT) {
pt_kernel_context->EmplaceBackAttr(
std::move(phi::Scalar(BOOST_GET_CONST(int, attr))));
} else {
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to Scalar when construct "
"KernelContext.",
attr_names[i])); attr_names[i]));
switch (AttrTypeID(attr_iter->second)) {
case proto::AttrType::INTS: {
const auto& vec =
BOOST_GET_CONST(std::vector<int32_t>, attr_iter->second);
std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size());
for (const auto& val : vec) {
scalar_list.emplace_back(val);
} }
} else { pt_kernel_context->EmplaceBackAttr(std::move(scalar_list));
auto& ins_vector = ctx.inputs.at(attr_names[i]); } break;
pt_kernel_context->EmplaceBackAttr( case proto::AttrType::LONGS: {
std::move(experimental::MakePhiScalarFromVar(*ins_vector.front()))); const auto& vec =
} BOOST_GET_CONST(std::vector<int64_t>, attr_iter->second);
} else if (attr_defs[i].type_index == phi::AttributeType::SCALARS) {
auto& attr = Attrs().at(attr_names[i]);
if (AttrTypeID(attr) == proto::AttrType::INTS) {
const auto& vec = BOOST_GET_CONST(std::vector<int32_t>, attr);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
for (const auto& val : vec) { for (const auto& val : vec) {
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
pt_kernel_context->EmplaceBackAttr(std::move(scalar_list)); pt_kernel_context->EmplaceBackAttr(std::move(scalar_list));
} else if (AttrTypeID(attr) == proto::AttrType::LONGS) { } break;
const auto& vec = BOOST_GET_CONST(std::vector<int64_t>, attr); case proto::AttrType::FLOATS: {
const auto& vec =
BOOST_GET_CONST(std::vector<float>, attr_iter->second);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
for (const auto& val : vec) { for (const auto& val : vec) {
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
pt_kernel_context->EmplaceBackAttr(std::move(scalar_list)); pt_kernel_context->EmplaceBackAttr(std::move(scalar_list));
} else if (AttrTypeID(attr) == proto::AttrType::FLOATS) { } break;
const auto& vec = BOOST_GET_CONST(std::vector<float>, attr); case proto::AttrType::FLOAT64S: {
const auto& vec =
BOOST_GET_CONST(std::vector<double>, attr_iter->second);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
for (const auto& val : vec) { for (const auto& val : vec) {
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
pt_kernel_context->EmplaceBackAttr(std::move(scalar_list)); pt_kernel_context->EmplaceBackAttr(std::move(scalar_list));
} else if (AttrTypeID(attr) == proto::AttrType::FLOAT64S) { } break;
const auto& vec = BOOST_GET_CONST(std::vector<double>, attr); case proto::AttrType::BOOLEANS: {
const auto& vec =
BOOST_GET_CONST(std::vector<bool>, attr_iter->second);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
for (const auto& val : vec) { for (const auto& val : vec) {
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
pt_kernel_context->EmplaceBackAttr(std::move(scalar_list)); pt_kernel_context->EmplaceBackAttr(std::move(scalar_list));
} else { } break;
default:
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to vector<Scalar> when " "Unsupported cast op attribute `%s` to vector<Scalar> when "
"construct KernelContext.", "construct KernelContext.",
attr_names[i])); attr_names[i]));
} }
} else { } break;
auto attr_it = attrs_.find(attr_names[i]); default: {
if (attr_defs[i].type_index == phi::AttributeType::INT32) { PADDLE_ENFORCE_NE(
if (attr_it == attrs_.end()) { attr_iter, Attrs().end(),
auto in_it = ctx.inputs.find(attr_names[i]); platform::errors::NotFound("(%s) is not found in AttributeMap when "
if (in_it != ctx.inputs.end()) { "buildind static KernelContext.",
// get data from input
auto val = experimental::MakePhiScalarFromVar(*(in_it->second[0]));
int32_t val_int = val.template to<int32_t>();
pt_kernel_context->EmplaceBackAttr(val_int);
} else {
PADDLE_THROW(platform::errors::NotFound(
"can not find attribute `%s` both in attribute and input ",
attr_names[i])); attr_names[i]));
} switch (attr_defs[i].type_index) {
} else { case phi::AttributeType::FLOAT32:
pt_kernel_context->EmplaceBackAttr( pt_kernel_context->EmplaceBackAttr(
BOOST_GET_CONST(int, attr_it->second)); BOOST_GET_CONST(float, attr_iter->second));
} break;
} else if (attr_defs[i].type_index == phi::AttributeType::FLOAT32) { case phi::AttributeType::INT32:
pt_kernel_context->EmplaceBackAttr( pt_kernel_context->EmplaceBackAttr(
BOOST_GET_CONST(float, attr_it->second)); BOOST_GET_CONST(int, attr_iter->second));
} else if (attr_defs[i].type_index == phi::AttributeType::BOOL) { break;
case phi::AttributeType::BOOL:
pt_kernel_context->EmplaceBackAttr( pt_kernel_context->EmplaceBackAttr(
BOOST_GET_CONST(bool, attr_it->second)); BOOST_GET_CONST(bool, attr_iter->second));
} else if (attr_defs[i].type_index == phi::AttributeType::INT64) { break;
case phi::AttributeType::INT64:
pt_kernel_context->EmplaceBackAttr( pt_kernel_context->EmplaceBackAttr(
BOOST_GET_CONST(int64_t, attr_it->second)); BOOST_GET_CONST(int64_t, attr_iter->second));
} else if (attr_defs[i].type_index == phi::AttributeType::STRING) { break;
case phi::AttributeType::INT32S:
pt_kernel_context->EmplaceBackAttr( pt_kernel_context->EmplaceBackAttr(
BOOST_GET_CONST(std::string, attr_it->second)); BOOST_GET_CONST(std::vector<int>, attr_iter->second));
} else if (attr_defs[i].type_index == phi::AttributeType::DATA_TYPE) { break;
auto data_type = paddle::framework::TransToPhiDataType( case phi::AttributeType::DATA_TYPE: {
auto data_type = framework::TransToPhiDataType(
static_cast<framework::proto::VarType::Type>( static_cast<framework::proto::VarType::Type>(
BOOST_GET_CONST(int, attr_it->second))); BOOST_GET_CONST(int, attr_iter->second)));
pt_kernel_context->EmplaceBackAttr(data_type); pt_kernel_context->EmplaceBackAttr(data_type);
} else if (attr_defs[i].type_index == phi::AttributeType::INT64S) { } break;
if (AttrTypeID(attr_it->second) == proto::AttrType::LONGS) { case phi::AttributeType::STRING:
pt_kernel_context->EmplaceBackAttr( pt_kernel_context->EmplaceBackAttr(
BOOST_GET_CONST(std::vector<int64_t>, attr_it->second)); std::move(BOOST_GET_CONST(std::string, attr_iter->second)));
} else if (AttrTypeID(attr_it->second) == proto::AttrType::INTS) { break;
// Emplace Back Attr according to the type of Phi_Kernel args. case phi::AttributeType::INT64S:
switch (AttrTypeID(attr_iter->second)) {
case proto::AttrType::LONGS:
pt_kernel_context->EmplaceBackAttr(
BOOST_GET_CONST(std::vector<int64_t>, attr_iter->second));
break;
case proto::AttrType::INTS: {
const auto& vector_int_attr = const auto& vector_int_attr =
BOOST_GET_CONST(std::vector<int>, attr_it->second); BOOST_GET_CONST(std::vector<int>, attr_iter->second);
const std::vector<int64_t> vector_int64_attr(vector_int_attr.begin(), const std::vector<int64_t> vector_int64_attr(
vector_int_attr.end()); vector_int_attr.begin(), vector_int_attr.end());
pt_kernel_context->EmplaceBackAttr(vector_int64_attr); pt_kernel_context->EmplaceBackAttr(vector_int64_attr);
} break;
default:
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to vector<int64_t> "
"when "
"construct KernelContext.",
attr_names[i]));
} }
} else if (attr_defs[i].type_index == phi::AttributeType::INT32S) { break;
const auto& vector_int_attr = case phi::AttributeType::FLOAT32S:
BOOST_GET_CONST(std::vector<int>, attr_it->second);
pt_kernel_context->EmplaceBackAttr(vector_int_attr);
} else if (attr_defs[i].type_index == phi::AttributeType::STRINGS) {
pt_kernel_context->EmplaceBackAttr( pt_kernel_context->EmplaceBackAttr(
BOOST_GET_CONST(std::vector<std::string>, attr_it->second)); BOOST_GET_CONST(std::vector<float>, attr_iter->second));
} else if (attr_defs[i].type_index == phi::AttributeType::FLOAT32S) { break;
case phi::AttributeType::STRINGS:
pt_kernel_context->EmplaceBackAttr( pt_kernel_context->EmplaceBackAttr(
BOOST_GET_CONST(std::vector<float>, attr_it->second)); BOOST_GET_CONST(std::vector<std::string>, attr_iter->second));
} else { break;
default:
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` when construct " "Unsupported cast op attribute `%s` when construct "
"KernelContext.", "KernelContext in dygraph.",
attr_names[i])); attr_names[i]));
} }
} }
} }
}
VLOG(4) << "Done attributes"; VLOG(4) << "Done attributes";
} }
......
...@@ -220,7 +220,7 @@ class PreparedOp { ...@@ -220,7 +220,7 @@ class PreparedOp {
static const phi::DefaultKernelSignatureMap& default_phi_kernel_sig_map; static const phi::DefaultKernelSignatureMap& default_phi_kernel_sig_map;
}; };
const inline framework::Attribute& GetAttr( const inline framework::Attribute* GetAttr(
const framework::AttributeMap& attrs, const framework::AttributeMap& attrs,
const framework::AttributeMap& default_attrs, const std::string& name) { const framework::AttributeMap& default_attrs, const std::string& name) {
auto it = attrs.find(name); auto it = attrs.find(name);
...@@ -229,10 +229,10 @@ const inline framework::Attribute& GetAttr( ...@@ -229,10 +229,10 @@ const inline framework::Attribute& GetAttr(
it = default_attrs.find(name); it = default_attrs.find(name);
found = it != default_attrs.end(); found = it != default_attrs.end();
} }
PADDLE_ENFORCE_EQ( if (found) {
found, true, return &it->second;
platform::errors::NotFound("(%s) is not found in AttributeMap.", name)); }
return it->second; return nullptr;
} }
template <typename VarType> template <typename VarType>
...@@ -330,6 +330,7 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -330,6 +330,7 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
} }
kernel_ctx->AssignInputRange(std::make_pair(start_idx, end_idx), i); kernel_ctx->AssignInputRange(std::make_pair(start_idx, end_idx), i);
} }
VLOG(6) << "BuildDygraphPhiKernelContext: Inputs parsing completed.";
for (size_t i = 0; i < output_names.size(); ++i) { for (size_t i = 0; i < output_names.size(); ++i) {
size_t start_idx = (i == 0 ? 0 : kernel_ctx->OutputRangeAt(i - 1).second); size_t start_idx = (i == 0 ? 0 : kernel_ctx->OutputRangeAt(i - 1).second);
...@@ -380,30 +381,65 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -380,30 +381,65 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
} }
kernel_ctx->AssignOutputRange(std::make_pair(start_idx, end_idx), i); kernel_ctx->AssignOutputRange(std::make_pair(start_idx, end_idx), i);
} }
VLOG(6) << "BuildDygraphPhiKernelContext: Outputs parsing completed.";
for (size_t i = 0; i < attr_names.size(); ++i) { for (size_t i = 0; i < attr_names.size(); ++i) {
if (attr_defs[i].type_index == phi::AttributeType::INT_ARRAY) { VLOG(6) << "BuildDygraphPhiKernelContext: " << attr_names[i] << ": "
if (attrs.find(attr_names[i]) != << attr_defs[i].type_index;
attrs.end()) { // shape is in the attribute auto* attr_ptr = GetAttr(attrs, default_attrs, attr_names[i]);
auto& attr = GetAttr(attrs, default_attrs, attr_names[i]); switch (attr_defs[i].type_index) {
if (AttrTypeID(attr) == framework::proto::AttrType::LONGS) { case phi::AttributeType::SCALAR:
if (attr_ptr) {
// scalar is in the attribute
auto& attr = *attr_ptr;
switch (AttrTypeID(attr)) {
case framework::proto::AttrType::FLOAT:
kernel_ctx->EmplaceBackAttr(
std::move(phi::Scalar(BOOST_GET_CONST(float, attr))));
break;
case framework::proto::AttrType::INT:
kernel_ctx->EmplaceBackAttr(
std::move(phi::Scalar(BOOST_GET_CONST(int, attr))));
break;
case framework::proto::AttrType::STRING:
kernel_ctx->EmplaceBackAttr(
std::move(phi::Scalar(BOOST_GET_CONST(std::string, attr))));
break;
default:
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to Scalar when construct "
"KernelContext in dygraph.",
attr_names[i]));
}
} else { // scalar is in the input
auto& ins_vector = ins.at(attr_names[i]);
kernel_ctx->EmplaceBackAttr(std::move( kernel_ctx->EmplaceBackAttr(std::move(
phi::IntArray(BOOST_GET_CONST(std::vector<int64_t>, attr)))); experimental::MakePhiScalarFromVar(ins_vector[0]->Var())));
} else if (AttrTypeID(attr) == framework::proto::AttrType::INTS) { }
break;
case phi::AttributeType::INT_ARRAY:
if (attr_ptr) {
auto& attr = *attr_ptr;
switch (AttrTypeID(attr)) {
case framework::proto::AttrType::INTS:
kernel_ctx->EmplaceBackAttr(std::move( kernel_ctx->EmplaceBackAttr(std::move(
phi::IntArray(BOOST_GET_CONST(std::vector<int32_t>, attr)))); phi::IntArray(BOOST_GET_CONST(std::vector<int32_t>, attr))));
} else if (AttrTypeID(attr) == framework::proto::AttrType::LONG) { break;
kernel_ctx->EmplaceBackAttr( case framework::proto::AttrType::LONGS:
std::move(phi::IntArray(&BOOST_GET_CONST(int64_t, attr), 1))); kernel_ctx->EmplaceBackAttr(std::move(
} else if (AttrTypeID(attr) == framework::proto::AttrType::INT) { phi::IntArray(BOOST_GET_CONST(std::vector<int64_t>, attr))));
break;
case framework::proto::AttrType::INT:
kernel_ctx->EmplaceBackAttr( kernel_ctx->EmplaceBackAttr(
std::move(phi::IntArray(&BOOST_GET_CONST(int32_t, attr), 1))); std::move(phi::IntArray(&BOOST_GET_CONST(int32_t, attr), 1)));
} else if (attr_defs[i].type_index == phi::AttributeType::INT32S) { break;
const auto& vector_int_attr = BOOST_GET_CONST(std::vector<int>, attr); case framework::proto::AttrType::LONG:
kernel_ctx->EmplaceBackAttr(vector_int_attr); kernel_ctx->EmplaceBackAttr(
} else { std::move(phi::IntArray(&BOOST_GET_CONST(int64_t, attr), 1)));
break;
default:
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to VectorTensor when " "Unsupported cast op attribute `%s` to IntArray when "
"construct KernelContext.", "construct KernelContext.",
attr_names[i])); attr_names[i]));
} }
...@@ -422,48 +458,16 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -422,48 +458,16 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
std::move(experimental::MakePhiIntArrayFromVarList(variables))); std::move(experimental::MakePhiIntArrayFromVarList(variables)));
} }
} }
} else if (attr_defs[i].type_index == phi::AttributeType::SCALAR) { break;
// TODO(zhangyunfei): Scalar should hold scaler type, and we should check case phi::AttributeType::SCALARS: {
// attribtue type by attr_defs PADDLE_ENFORCE_NOT_NULL(
if (attrs.find(attr_names[i]) != attrs.end() || attr_ptr,
default_attrs.find(attr_names[i]) != platform::errors::NotFound("(%s) is not found in AttributeMap when "
default_attrs.end()) { // scalar is in the attribute "buildind dygraph KernelContext.",
auto& attr = GetAttr(attrs, default_attrs, attr_names[i]);
if (AttrTypeID(attr) == framework::proto::AttrType::FLOAT) {
kernel_ctx->EmplaceBackAttr(
std::move(phi::Scalar(BOOST_GET_CONST(float, attr))));
} else if (AttrTypeID(attr) == framework::proto::AttrType::STRING) {
kernel_ctx->EmplaceBackAttr(
std::move(phi::Scalar(BOOST_GET_CONST(std::string, attr))));
} else if (AttrTypeID(attr) == framework::proto::AttrType::INT) {
kernel_ctx->EmplaceBackAttr(
std::move(phi::Scalar(BOOST_GET_CONST(int, attr))));
} else {
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to Scalar when construct "
"KernelContext in dygraph.",
attr_names[i])); attr_names[i]));
} auto& attr = *attr_ptr;
} else { // scalar is in the input switch (AttrTypeID(attr)) {
auto& ins_vector = ins.at(attr_names[i]); case framework::proto::AttrType::INTS: {
kernel_ctx->EmplaceBackAttr(std::move(
experimental::MakePhiScalarFromVar(ins_vector[0]->Var())));
}
} else if (ins.find(attr_names[i]) != ins.end()) {
// deal tensor attr here
auto& ins_vector = ins.at(attr_names[i]);
auto tensor_attr =
experimental::MakePhiScalarFromVar(ins_vector[0]->Var());
if (attr_defs[i].type_index == phi::AttributeType::INT32) {
int val = tensor_attr.template to<int>();
kernel_ctx->EmplaceBackAttr(val);
} else {
PADDLE_THROW(platform::errors::Unimplemented("only support int here"));
}
} else if (attr_defs[i].type_index == phi::AttributeType::SCALARS) {
auto& attr = GetAttr(attrs, default_attrs, attr_names[i]);
if (AttrTypeID(attr) == framework::proto::AttrType::INTS) {
const auto& vec = BOOST_GET_CONST(std::vector<int32_t>, attr); const auto& vec = BOOST_GET_CONST(std::vector<int32_t>, attr);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
...@@ -471,7 +475,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -471,7 +475,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
kernel_ctx->EmplaceBackAttr(std::move(scalar_list)); kernel_ctx->EmplaceBackAttr(std::move(scalar_list));
} else if (AttrTypeID(attr) == framework::proto::AttrType::LONGS) { } break;
case framework::proto::AttrType::LONGS: {
const auto& vec = BOOST_GET_CONST(std::vector<int64_t>, attr); const auto& vec = BOOST_GET_CONST(std::vector<int64_t>, attr);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
...@@ -479,7 +484,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -479,7 +484,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
kernel_ctx->EmplaceBackAttr(std::move(scalar_list)); kernel_ctx->EmplaceBackAttr(std::move(scalar_list));
} else if (AttrTypeID(attr) == framework::proto::AttrType::FLOATS) { } break;
case framework::proto::AttrType::FLOATS: {
const auto& vec = BOOST_GET_CONST(std::vector<float>, attr); const auto& vec = BOOST_GET_CONST(std::vector<float>, attr);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
...@@ -487,7 +493,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -487,7 +493,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
kernel_ctx->EmplaceBackAttr(std::move(scalar_list)); kernel_ctx->EmplaceBackAttr(std::move(scalar_list));
} else if (AttrTypeID(attr) == framework::proto::AttrType::FLOAT64S) { } break;
case framework::proto::AttrType::FLOAT64S: {
const auto& vec = BOOST_GET_CONST(std::vector<double>, attr); const auto& vec = BOOST_GET_CONST(std::vector<double>, attr);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
...@@ -495,7 +502,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -495,7 +502,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
kernel_ctx->EmplaceBackAttr(std::move(scalar_list)); kernel_ctx->EmplaceBackAttr(std::move(scalar_list));
} else if (AttrTypeID(attr) == framework::proto::AttrType::BOOLEANS) { } break;
case framework::proto::AttrType::BOOLEANS: {
const auto& vec = BOOST_GET_CONST(std::vector<bool>, attr); const auto& vec = BOOST_GET_CONST(std::vector<bool>, attr);
std::vector<phi::Scalar> scalar_list; std::vector<phi::Scalar> scalar_list;
scalar_list.reserve(vec.size()); scalar_list.reserve(vec.size());
...@@ -503,48 +511,78 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -503,48 +511,78 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
scalar_list.emplace_back(val); scalar_list.emplace_back(val);
} }
kernel_ctx->EmplaceBackAttr(std::move(scalar_list)); kernel_ctx->EmplaceBackAttr(std::move(scalar_list));
} else { } break;
default:
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to vector<Scalar> when " "Unsupported cast op attribute `%s` to vector<Scalar> when "
"construct KernelContext.", "construct KernelContext.",
attr_names[i])); attr_names[i]));
} }
} else { } break;
auto& attr = GetAttr(attrs, default_attrs, attr_names[i]); default: {
if (attr_defs[i].type_index == phi::AttributeType::INT32) { PADDLE_ENFORCE_NOT_NULL(
kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(int, attr)); attr_ptr,
} else if (attr_defs[i].type_index == phi::AttributeType::FLOAT32) { platform::errors::NotFound("(%s) is not found in AttributeMap when "
"buildind dygraph KernelContext.",
attr_names[i]));
auto& attr = *attr_ptr;
switch (attr_defs[i].type_index) {
case phi::AttributeType::FLOAT32:
kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(float, attr)); kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(float, attr));
} else if (attr_defs[i].type_index == phi::AttributeType::BOOL) { break;
case phi::AttributeType::INT32:
kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(int, attr));
break;
case phi::AttributeType::BOOL:
kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(bool, attr)); kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(bool, attr));
} else if (attr_defs[i].type_index == phi::AttributeType::INT64) { break;
case phi::AttributeType::INT64:
kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(int64_t, attr)); kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(int64_t, attr));
} else if (attr_defs[i].type_index == phi::AttributeType::STRING) { break;
kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(std::string, attr)); case phi::AttributeType::INT32S:
} else if (attr_defs[i].type_index == phi::AttributeType::DATA_TYPE) { kernel_ctx->EmplaceBackAttr(
BOOST_GET_CONST(std::vector<int>, attr));
break;
case phi::AttributeType::DATA_TYPE: {
auto data_type = framework::TransToPhiDataType( auto data_type = framework::TransToPhiDataType(
static_cast<framework::proto::VarType::Type>( static_cast<framework::proto::VarType::Type>(
BOOST_GET_CONST(int, attr))); BOOST_GET_CONST(int, attr)));
kernel_ctx->EmplaceBackAttr(data_type); kernel_ctx->EmplaceBackAttr(data_type);
} else if (attr_defs[i].type_index == phi::AttributeType::INT64S) { } break;
if (AttrTypeID(attr) == framework::proto::AttrType::LONGS) { case phi::AttributeType::STRING:
kernel_ctx->EmplaceBackAttr(
std::move(BOOST_GET_CONST(std::string, attr)));
break;
case phi::AttributeType::INT64S: {
switch (AttrTypeID(attr)) {
case framework::proto::AttrType::LONGS:
kernel_ctx->EmplaceBackAttr( kernel_ctx->EmplaceBackAttr(
BOOST_GET_CONST(std::vector<int64_t>, attr)); BOOST_GET_CONST(std::vector<int64_t>, attr));
} else if (AttrTypeID(attr) == framework::proto::AttrType::INTS) { break;
// Emplace Back Attr according to the type of Phi_Kernel args. case framework::proto::AttrType::INTS: {
const auto& vector_int_attr = BOOST_GET_CONST(std::vector<int>, attr); const auto& vector_int_attr =
const std::vector<int64_t> vector_int64_attr(vector_int_attr.begin(), BOOST_GET_CONST(std::vector<int>, attr);
vector_int_attr.end()); const std::vector<int64_t> vector_int64_attr(
vector_int_attr.begin(), vector_int_attr.end());
kernel_ctx->EmplaceBackAttr(vector_int64_attr); kernel_ctx->EmplaceBackAttr(vector_int64_attr);
} break;
default:
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` to vector<int64_t> "
"when "
"construct KernelContext.",
attr_names[i]));
} }
} else if (attr_defs[i].type_index == phi::AttributeType::INT32S) { } break;
kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(std::vector<int>, attr)); case phi::AttributeType::FLOAT32S:
} else if (attr_defs[i].type_index == phi::AttributeType::STRINGS) { kernel_ctx->EmplaceBackAttr(
BOOST_GET_CONST(std::vector<float>, attr));
break;
case phi::AttributeType::STRINGS:
kernel_ctx->EmplaceBackAttr( kernel_ctx->EmplaceBackAttr(
BOOST_GET_CONST(std::vector<std::string>, attr)); BOOST_GET_CONST(std::vector<std::string>, attr));
} else if (attr_defs[i].type_index == phi::AttributeType::FLOAT32S) { break;
kernel_ctx->EmplaceBackAttr(BOOST_GET_CONST(std::vector<float>, attr)); default:
} else {
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"Unsupported cast op attribute `%s` when construct " "Unsupported cast op attribute `%s` when construct "
"KernelContext in dygraph.", "KernelContext in dygraph.",
...@@ -552,6 +590,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -552,6 +590,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
} }
} }
} }
}
VLOG(6) << "BuildDygraphPhiKernelContext: Attributes parsing completed.";
} }
template <typename VarType> template <typename VarType>
......
...@@ -2945,7 +2945,7 @@ void UnStackInferMeta(const MetaTensor& x, ...@@ -2945,7 +2945,7 @@ void UnStackInferMeta(const MetaTensor& x,
} }
void OneHotRawInferMeta(const MetaTensor& x, void OneHotRawInferMeta(const MetaTensor& x,
int32_t depth, const Scalar& depth,
DataType dtype, DataType dtype,
bool allow_out_of_range, bool allow_out_of_range,
MetaTensor* out) { MetaTensor* out) {
...@@ -2955,7 +2955,7 @@ void OneHotRawInferMeta(const MetaTensor& x, ...@@ -2955,7 +2955,7 @@ void OneHotRawInferMeta(const MetaTensor& x,
1, 1,
phi::errors::InvalidArgument("Rank of Input(X) should be at least 1.")); phi::errors::InvalidArgument("Rank of Input(X) should be at least 1."));
auto out_dims_vec = phi::vectorize(x_dims); auto out_dims_vec = phi::vectorize(x_dims);
out_dims_vec.push_back(depth); out_dims_vec.push_back(depth.to<int>());
auto out_dims = phi::make_ddim(out_dims_vec); auto out_dims = phi::make_ddim(out_dims_vec);
out->set_dims(out_dims); out->set_dims(out_dims);
out->share_lod(x); out->share_lod(x);
......
...@@ -426,7 +426,7 @@ void UnStackInferMeta(const MetaTensor& x, ...@@ -426,7 +426,7 @@ void UnStackInferMeta(const MetaTensor& x,
std::vector<MetaTensor*> outs); std::vector<MetaTensor*> outs);
void OneHotRawInferMeta(const MetaTensor& x, void OneHotRawInferMeta(const MetaTensor& x,
int32_t depth, const Scalar& depth,
DataType dtype, DataType dtype,
bool allow_out_of_range, bool allow_out_of_range,
MetaTensor* out); MetaTensor* out);
......
...@@ -64,18 +64,19 @@ struct OneHotV2OpFunctor { ...@@ -64,18 +64,19 @@ struct OneHotV2OpFunctor {
template <typename T, typename Context> template <typename T, typename Context>
void OneHotRawKernel(const Context& dev_ctx, void OneHotRawKernel(const Context& dev_ctx,
const DenseTensor& x, const DenseTensor& x,
int32_t depth, const Scalar& depth,
DataType dtype, DataType dtype,
bool allow_out_of_range, bool allow_out_of_range,
DenseTensor* out) { DenseTensor* out) {
auto depth_v = depth.to<int>();
auto out_dims = out->dims(); auto out_dims = out->dims();
if (out_dims[out_dims.size() - 1] == -1) { if (out_dims[out_dims.size() - 1] == -1) {
out_dims[out_dims.size() - 1] = depth; out_dims[out_dims.size() - 1] = depth_v;
out->Resize(out_dims); out->Resize(out_dims);
} }
phi::VisitDataType(dtype, phi::VisitDataType(dtype,
OneHotV2OpFunctor<Context, T>(&x, out, depth, dev_ctx)); OneHotV2OpFunctor<Context, T>(&x, out, depth_v, dev_ctx));
} }
} // namespace phi } // namespace phi
......
...@@ -73,18 +73,19 @@ struct OneHotV2OpCUDAFunctor { ...@@ -73,18 +73,19 @@ struct OneHotV2OpCUDAFunctor {
template <typename T, typename Context> template <typename T, typename Context>
void OneHotRawKernel(const Context& dev_ctx, void OneHotRawKernel(const Context& dev_ctx,
const DenseTensor& x, const DenseTensor& x,
int32_t depth, const Scalar& depth,
DataType dtype, DataType dtype,
bool allow_out_of_range, bool allow_out_of_range,
DenseTensor* out) { DenseTensor* out) {
auto depth_v = depth.to<int>();
auto out_dims = out->dims(); auto out_dims = out->dims();
if (out_dims[out_dims.size() - 1] == -1) { if (out_dims[out_dims.size() - 1] == -1) {
out_dims[out_dims.size() - 1] = depth; out_dims[out_dims.size() - 1] = depth_v;
out->Resize(out_dims); out->Resize(out_dims);
} }
phi::VisitDataType( phi::VisitDataType(
dtype, OneHotV2OpCUDAFunctor<Context, T>(&x, out, depth, dev_ctx)); dtype, OneHotV2OpCUDAFunctor<Context, T>(&x, out, depth_v, dev_ctx));
} }
} // namespace phi } // namespace phi
......
...@@ -24,9 +24,8 @@ void OneHotKernel(const Context& dev_ctx, ...@@ -24,9 +24,8 @@ void OneHotKernel(const Context& dev_ctx,
const DenseTensor& x, const DenseTensor& x,
const Scalar& num_classes_s, const Scalar& num_classes_s,
DenseTensor* out) { DenseTensor* out) {
int num_classes = num_classes_s.to<int>();
OneHotRawKernel<T>( OneHotRawKernel<T>(
dev_ctx, x, num_classes, phi::DataType::FLOAT32, false, out); dev_ctx, x, num_classes_s, phi::DataType::FLOAT32, false, out);
} }
} // namespace phi } // namespace phi
......
...@@ -28,7 +28,7 @@ void OneHotKernel(const Context& dev_ctx, ...@@ -28,7 +28,7 @@ void OneHotKernel(const Context& dev_ctx,
template <typename T, typename Context> template <typename T, typename Context>
void OneHotRawKernel(const Context& dev_ctx, void OneHotRawKernel(const Context& dev_ctx,
const DenseTensor& x, const DenseTensor& x,
int32_t depth, const Scalar& depth,
DataType dtype, DataType dtype,
bool allow_out_of_range, bool allow_out_of_range,
DenseTensor* out); DenseTensor* out);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册