Skip to content

[Inference]Fix analyzer_capi_exp_tester and analyzer_capi_exp_ner_tester in PIR mode #67248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions paddle/fluid/ir_adaptor/translator/op_compat_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ def insert_new_mutable_attributes(
"out": "Output",
}

op_name_mappings["lookup_table"] = "embedding"
op_arg_name_mappings["lookup_table"] = {
"x": "Ids",
"weight": "W",
"out": "Out",
}

op_arg_name_mappings["set_value_grad"]["values_grad"] = "ValueTensor@GRAD"
op_arg_name_mappings["fetch"] = {"x": "X"}
op_arg_name_mappings["elementwise_add_grad_grad"] = {
Expand Down
191 changes: 146 additions & 45 deletions paddle/fluid/ir_adaptor/translator/op_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,105 @@ struct Conv2dOpTranscriber : public OpTranscriber {
}
};

using ValueInfo =
std::tuple<std::vector<int64_t>, dialect::DenseTensorType, pir::Value>;

ValueInfo GetTensorInfoByVarName(const OpDesc& op_desc,
const std::vector<std::string>& names,
TranslationContext* param_map,
const std::string& var_name) {
PADDLE_ENFORCE_EQ(
names.size(),
1UL,
common::errors::InvalidArgument(
"Expected op[%s]'s input %s has only 1 variable, but got %d",
op_desc.Type(),
var_name,
names.size()));
const auto& name = names[0];
PADDLE_ENFORCE_GT(
param_map->count(name),
0UL,
common::errors::InvalidArgument(
"Expected op[%s]'s input %s has been parsed", op_desc.Type(), name));
const auto& defining_info = param_map->at(name);

pir::Value value = defining_info.value;
PADDLE_ENFORCE_NE(
value,
nullptr,
common::errors::PreconditionNotMet(
"Expected op[%s]'s input %s is not null", op_desc.Type(), name));
const pir::Type& type = value.type();
PADDLE_ENFORCE_EQ(type.isa<dialect::DenseTensorType>(),
true,
common::errors::InvalidArgument(
"Expected op[%s]'s input %s is DenseTensor but got %s",
op_desc.Type(),
name,
type));
dialect::DenseTensorType tensor_type =
type.dyn_cast<dialect::DenseTensorType>();

std::vector<int64_t> shape = common::vectorize(tensor_type.dims());

return std::make_tuple(shape, tensor_type, value);
}

struct EmbeddingOpTranscriber : public OpTranscriber {
void RecordOpResultMapping(pir::IrContext* ctx,
TranslationContext* param_map,
const OpDesc& op_desc,
pir::Operation* operation,
const OpOutputMapping& arg_to_idx) override {
OpTranscriber::RecordOpResultMapping(
ctx, param_map, op_desc, operation, arg_to_idx);
if (op_desc.Type() == "lookup_table") {
ValueInfo out_info = GetTensorInfoByVarName(
op_desc, op_desc.Output("Out"), param_map, "Out");
const auto& output_vars = op_desc.Output("Out");
const auto& output_name = output_vars[0];

const dialect::DenseTensorType& out_tensor_type = std::get<1>(out_info);
pir::Value& out_value = std::get<2>(out_info);

ValueInfo ids_info = GetTensorInfoByVarName(
op_desc, op_desc.Input("Ids", true), param_map, "Ids");
const std::vector<int64_t>& ids_shape = std::get<0>(ids_info);

ValueInfo w_info = GetTensorInfoByVarName(
op_desc, op_desc.Input("W", true), param_map, "W");

const std::vector<int64_t>& w_shape = std::get<0>(w_info);

std::vector<int64_t> out_new_shape(
ids_shape.begin(), ids_shape.begin() + ids_shape.size() - 1);
out_new_shape.insert(out_new_shape.end(), w_shape[1]);

pir::Builder builder(ctx, operation->GetParent());
dialect::ReshapeOp reshape_op_out =
builder.Build<dialect::ReshapeOp>(out_value, out_new_shape);
pir::Value out_new = reshape_op_out.out();
VLOG(6) << "[" << op_desc.Type() << "] out_shape change from "
<< out_tensor_type.dims() << " to "
<< common::make_ddim(out_new_shape);

param_map->PushValue(output_name,
VariableDefiningInfo(out_new, false, -1));
}
}

pir::OpInfo LookUpOpInfo(pir::IrContext* ctx,
const OpDesc& op_desc) override {
auto op_info = ctx->GetRegisteredOpInfo("pd_op.embedding");
if (!op_info) {
IR_THROW("Op %d should have corresponding OpInfo %d",
op_desc.Type(),
"pd_op.embedding");
}
return op_info;
}

void HandleNonexistentAttribute(pir::IrContext* ctx,
pir::AttributeMap* attribute_map,
const OpAttributeInfo& info) override {
Expand Down Expand Up @@ -1393,6 +1491,20 @@ struct CrossEntropyWithSoftmaxOpTranscriber : public OpTranscriber {
}
};

struct BoxCoderOpTranscriber : public OpTranscriber {
void HandleNonexistentAttribute(pir::IrContext* ctx,
pir::AttributeMap* attribute_map,
const OpAttributeInfo& info) override {
if (info.name == "axis") {
(*attribute_map)[info.name] = pir::Int32Attribute::get(ctx, 0);
}
if (info.name == "variance") {
std::vector<pir::Attribute> variance;
(*attribute_map)[info.name] = pir::ArrayAttribute::get(ctx, variance);
}
}
};

struct DepthwiseConv2dOpTranscriber : public OpTranscriber {
void HandleNonexistentAttribute(pir::IrContext* ctx,
pir::AttributeMap* attribute_map,
Expand Down Expand Up @@ -1657,51 +1769,6 @@ struct TrilAndTriuGradOpTranscriber : public OpTranscriber {
}
};

using ValueInfo =
std::tuple<std::vector<int64_t>, dialect::DenseTensorType, pir::Value>;

ValueInfo GetTensorInfoByVarName(const OpDesc& op_desc,
const std::vector<std::string>& names,
TranslationContext* param_map,
const std::string& var_name) {
PADDLE_ENFORCE_EQ(
names.size(),
1UL,
common::errors::InvalidArgument(
"Expected op[%s]'s input %s has only 1 variable, but got %d",
op_desc.Type(),
var_name,
names.size()));
const auto& name = names[0];
PADDLE_ENFORCE_GT(
param_map->count(name),
0UL,
common::errors::InvalidArgument(
"Expected op[%s]'s input %s has been parsed", op_desc.Type(), name));
const auto& defining_info = param_map->at(name);

pir::Value value = defining_info.value;
PADDLE_ENFORCE_NE(
value,
nullptr,
common::errors::PreconditionNotMet(
"Expected op[%s]'s input %s is not null", op_desc.Type(), name));
const pir::Type& type = value.type();
PADDLE_ENFORCE_EQ(type.isa<dialect::DenseTensorType>(),
true,
common::errors::InvalidArgument(
"Expected op[%s]'s input %s is DenseTensor but got %s",
op_desc.Type(),
name,
type));
dialect::DenseTensorType tensor_type =
type.dyn_cast<dialect::DenseTensorType>();

std::vector<int64_t> shape = common::vectorize(tensor_type.dims());

return std::make_tuple(shape, tensor_type, value);
}

struct MulOpTranscriber : public OpTranscriber {
pir::Operation* operator()(pir::IrContext* ctx,
TranslationContext* param_map,
Expand Down Expand Up @@ -3566,6 +3633,37 @@ struct CEmbeddingOpTranscriber : public OpTranscriber {
}
};

struct GatherOpTranscriber : public OpTranscriber {
pir::Value GetAttributeAsInput(pir::IrContext* ctx,
pir::Block* block,
const OpDesc& op_desc,
const OpInputInfo& input_info) override {
auto& attribute_translator = AttributeTranslator::instance();
auto& op_normalizer = OpNameNormalizer::instance();

auto legacy_attr_name =
op_normalizer.GetLegacyAttrName(op_desc.Type(), input_info.name);

if (!op_desc.HasAttr(legacy_attr_name)) {
VLOG(10) << "[" << op_desc.Type() << "][attribute]"
<< " name: " << legacy_attr_name << " not found and fill 0.";
pir::Attribute new_attr = pir::Int64Attribute::get(ctx, 0);
pir::Operation* defining_op =
InsertFullOperationForAttributeInput(ctx, block, new_attr);
return defining_op->result(0);
} else {
paddle::framework::Attribute legacy_attr =
op_desc.GetAttr(legacy_attr_name);
VLOG(10) << "[" << op_desc.Type() << "][attribute]"
<< " name: " << legacy_attr_name << " " << legacy_attr.index();
pir::Attribute new_attr = attribute_translator(legacy_attr);
pir::Operation* defining_op =
InsertFullOperationForAttributeInput(ctx, block, new_attr);
return defining_op->result(0);
}
}
};

struct QuantizeLinearOpTranscriber : public OpTranscriber {
void HandleNonexistentAttribute(pir::IrContext* ctx,
pir::AttributeMap* attribute_map,
Expand Down Expand Up @@ -3679,6 +3777,7 @@ OpTranslator::OpTranslator() {
special_handlers["increment"] = IncrementOpTranscriber();
special_handlers["lookup_table_v2"] = EmbeddingOpTranscriber();
special_handlers["lookup_table_v2_grad"] = EmbeddingGradOpTranscriber();
special_handlers["lookup_table"] = EmbeddingOpTranscriber();
special_handlers["one_hot_v2"] = OneHotTranscriber();
special_handlers["pool2d"] = Pool2dOpTranscriber();
special_handlers["randint"] = RandIntOpTranscriber();
Expand Down Expand Up @@ -3707,6 +3806,8 @@ OpTranslator::OpTranslator() {
special_handlers["softmax"] = SoftmaxOpTranscriber();
special_handlers["softmax_with_cross_entropy"] =
SoftmaxWithCrossEntropyOpTranscriber();
special_handlers["gather"] = GatherOpTranscriber();
special_handlers["box_coder"] = BoxCoderOpTranscriber();

// To adapt LodTensorArray
special_handlers["lod_array_length"] = LodArrayLengthOpTranscriber();
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2905,6 +2905,7 @@ void MatmulInferMeta(const MetaTensor& x,
out->set_dtype(x.dtype());
}
out->set_layout(x.layout());
out->share_lod(x);
}

void MatmulWithFlattenInferMeta(const MetaTensor& x,
Expand Down
22 changes: 22 additions & 0 deletions test/cpp/inference/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1007,9 +1007,31 @@ if(WITH_TESTING AND WITH_INFERENCE_API_TEST)
--infer_model=${INT8_DATA_DIR}/resnet50/model)
endif()

# chinese_ner
set(CHINESE_NER_INSTALL_DIR "${INFERENCE_DEMO_INSTALL_DIR}/chinese_ner")
download_model_and_data_without_verify(
${CHINESE_NER_INSTALL_DIR} "chinese_ner_model.tar.gz"
"chinese_ner-data.txt.tar.gz")
inference_analysis_test(
test_analyzer_capi_exp_ner
SRCS
analyzer_capi_exp_ner_tester.cc
EXTRA_DEPS
paddle_inference_c_shared
ARGS
--infer_model=${CHINESE_NER_INSTALL_DIR}/model)

# resnet50
set(RESNET50_MODEL_DIR "${INFERENCE_DEMO_INSTALL_DIR}/resnet50")
download_data_without_verify(${RESNET50_MODEL_DIR} "resnet50_model.tar.gz")
inference_analysis_test(
test_analyzer_capi_exp
SRCS
analyzer_capi_exp_tester.cc
EXTRA_DEPS
paddle_inference_c_shared
ARGS
--infer_model=${RESNET50_MODEL_DIR}/model)

if(WITH_GPU)
inference_analysis_test(
Expand Down
18 changes: 0 additions & 18 deletions test/deprecated/cpp/inference/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -500,15 +500,6 @@ if(WITH_TESTING AND WITH_INFERENCE_API_TEST)
test_analyzer_multi_model_prediction ${MMP_INSTALL_DIR}
analyzer_mmp_tester.cc EXTRA_DEPS common)

inference_analysis_test(
test_analyzer_capi_exp
SRCS
analyzer_capi_exp_tester.cc
EXTRA_DEPS
paddle_inference_c_shared
ARGS
--infer_model=${RESNET50_MODEL_DIR}/model)

inference_analysis_test(
test_analyzer_capi_exp_pd_tensor
SRCS
Expand Down Expand Up @@ -563,15 +554,6 @@ if(WITH_TESTING AND WITH_INFERENCE_API_TEST)
--infer_data=${OCR_INSTALL_DIR}/data.txt
--refer_result=${OCR_INSTALL_DIR}/result.txt)

inference_analysis_test(
test_analyzer_capi_exp_ner
SRCS
analyzer_capi_exp_ner_tester.cc
EXTRA_DEPS
paddle_inference_c_shared
ARGS
--infer_model=${CHINESE_NER_INSTALL_DIR}/model)

set_tests_properties(test_analyzer_mobilenet_transpose PROPERTIES TIMEOUT 120)
set_tests_properties(test_analyzer_resnet50 PROPERTIES TIMEOUT 120)
set_tests_properties(test_analyzer_ner PROPERTIES TIMEOUT 120)
Expand Down
6 changes: 2 additions & 4 deletions test/legacy_test/test_uniform_random_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,12 @@ def test_api(self):
y = linear(x)

place = base.CPUPlace()
x_tensor = base.create_lod_tensor(
np.random.rand(3, 16).astype("float32"), [[1, 2]], place
)
x_data = np.random.rand(3, 16).astype("float32")
exe = base.Executor(place)
exe.run(paddle.static.default_startup_program())
ret = exe.run(
paddle.static.default_main_program(),
feed={'x': x_tensor},
feed={'x': x_data},
fetch_list=[y],
return_numpy=False,
)
Expand Down