Skip to content
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

Add reduce_min prod trt converter #49615

Merged
merged 10 commits into from
Jan 10, 2023
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
2 changes: 2 additions & 0 deletions paddle/fluid/inference/api/analysis_predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2349,7 +2349,9 @@ USE_TRT_CONVERTER(reshape2);
USE_TRT_CONVERTER(gather_nd);
USE_TRT_CONVERTER(reduce_mean);
USE_TRT_CONVERTER(reduce_max);
USE_TRT_CONVERTER(reduce_min);
USE_TRT_CONVERTER(reduce_sum);
USE_TRT_CONVERTER(reduce_prod);
USE_TRT_CONVERTER(tile);
USE_TRT_CONVERTER(conv3d);
USE_TRT_CONVERTER(conv3d_transpose);
Expand Down
16 changes: 15 additions & 1 deletion paddle/fluid/inference/tensorrt/convert/reduce_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class ReduceOpConverter : public OpConverter {
VLOG(4) << "convert a paddle " << op_type << " op to tensorrt reduce layer";
framework::OpDesc op_desc(op, nullptr);
auto reduce_type = ops_.find(op_type);

auto* x = engine_->GetITensor(op_desc.Input("X").front());
nvinfer1::Dims input_shape = x->getDimensions();
int input_dims = input_shape.nbDims;
Expand Down Expand Up @@ -104,6 +103,8 @@ const std::unordered_map<std::string, std::vector<nvinfer1::ReduceOperation>>
{"reduce_mean", {nvinfer1::ReduceOperation::kAVG}},
{"reduce_sum", {nvinfer1::ReduceOperation::kSUM}},
{"reduce_max", {nvinfer1::ReduceOperation::kMAX}},
{"reduce_min", {nvinfer1::ReduceOperation::kMIN}},
{"reduce_prod", {nvinfer1::ReduceOperation::kPROD}},
};

class ReduceSumOpConverter : public ReduceOpConverter {
Expand All @@ -120,10 +121,23 @@ class ReduceMaxOpConverter : public ReduceOpConverter {
public:
ReduceMaxOpConverter() { op_type = "reduce_max"; }
};

class ReduceMinOpConverter : public ReduceOpConverter {
public:
ReduceMinOpConverter() { op_type = "reduce_min"; }
};

class ReduceProdOpConverter : public ReduceOpConverter {
public:
ReduceProdOpConverter() { op_type = "reduce_prod"; }
};

} // namespace tensorrt
} // namespace inference
} // namespace paddle

REGISTER_TRT_OP_CONVERTER(reduce_sum, ReduceSumOpConverter);
REGISTER_TRT_OP_CONVERTER(reduce_mean, ReduceMeanOpConverter);
REGISTER_TRT_OP_CONVERTER(reduce_max, ReduceMaxOpConverter);
REGISTER_TRT_OP_CONVERTER(reduce_min, ReduceMinOpConverter);
REGISTER_TRT_OP_CONVERTER(reduce_prod, ReduceProdOpConverter);
3 changes: 2 additions & 1 deletion paddle/fluid/inference/tensorrt/op_teller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2098,7 +2098,8 @@ struct SimpleOpTypeSetTeller : public Teller {
}

if (op_type == "reduce_sum" || op_type == "reduce_mean" ||
op_type == "reduce_max") {
op_type == "reduce_max" || op_type == "reduce_min" ||
op_type == "reduce_prod") {
if (!desc.HasAttr("dim", /*with_attr_var=*/false)) {
VLOG(3) << "Skip to convert into TRT while found Attribute('dim') is "
"Variable type in "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ def generate_input1(dtype, attrs: List[Dict[str, Any]]):
for out_dtype in [-1, 2, 5]:
for op_type in [
"reduce_max",
"reduce_min",
"reduce_mean",
"reduce_sum",
"reduce_prod",
]:
dics = [
dics1 = [
{
"keep_dim": keep_dim,
"dim": dim,
Expand All @@ -81,36 +83,43 @@ def generate_input1(dtype, attrs: List[Dict[str, Any]]):
},
{},
]

ops_config = [
dics2 = [
{
"op_type": op_type,
"op_inputs": {"X": ["input_data"]},
"op_outputs": {
"Out": ["reduce_output_data"]
},
"op_attrs": dics[0],
}
"out_dtype": out_dtype,
"in_dtype": out_dtype,
},
{},
]
ops = self.generate_op_config(ops_config)

program_config = ProgramConfig(
ops=ops,
weights={},
inputs={
"input_data": TensorConfig(
data_gen=partial(
generate_input1, out_dtype, dics
for dics in [dics1, dics2]:
ops_config = [
{
"op_type": op_type,
"op_inputs": {"X": ["input_data"]},
"op_outputs": {
"Out": ["reduce_output_data"]
},
"op_attrs": dics[0],
}
]
ops = self.generate_op_config(ops_config)

program_config = ProgramConfig(
ops=ops,
weights={},
inputs={
"input_data": TensorConfig(
data_gen=partial(
generate_input1, out_dtype, dics
)
)
)
},
outputs=["reduce_output_data"],
)
},
outputs=["reduce_output_data"],
)

if not self.is_program_valid(program_config):
continue
if not self.is_program_valid(program_config):
continue

yield program_config
yield program_config

def sample_predictor_configs(
self, program_config
Expand Down