Skip to content

Commit

Permalink
[Paddle-trt]Convert add trt build phase operator to trt layer log (Pa…
Browse files Browse the repository at this point in the history
  • Loading branch information
lizexu123 authored Mar 27, 2024
1 parent a63f17c commit 9c0cb6c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
30 changes: 28 additions & 2 deletions paddle/fluid/inference/tensorrt/convert/op_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ class OpConverter {
platform::errors::Unimplemented("no OpConverter for optype [%s]",
op_desc.Type()));

std::string all_outpus_name = "(Outputs:";
std::string all_inpus_name = "(Inputs:";
for (auto it1 : op_desc.OutputNames()) {
for (auto it2 : op_desc.Output(it1)) {
all_outpus_name += it2;
all_outpus_name += ",";
}
}
all_outpus_name += ")";
for (auto it1 : op_desc.InputNames()) {
for (auto it2 : op_desc.Input(it1)) {
all_inpus_name += it2;
all_inpus_name += ",";
}
}

all_inpus_name += ")";
VLOG(1) << op_desc.Type() << all_inpus_name << all_outpus_name
<< "are to be converted to TensorRT layer";

it->SetEngine(engine);
engine->SetScope(&scope);
it->SetBlockDesc(block);
Expand All @@ -197,6 +217,7 @@ class OpConverter {
"\"Out\" or \"Y\".",
op_desc.Type()));
}

auto* output_itensor = engine->GetITensor(output_name);
engine->SetTensorDynamicRange(output_itensor, out_scale);
VLOG(1) << "Set out scale = " << out_scale << " for tensor "
Expand Down Expand Up @@ -245,12 +266,14 @@ class OpConverter {
}
}

// Convert a fluid block to tensorrt network, NOTE it just convert operators,
// the INetwork's inputs and outputs should specified in some other modules.
// Convert a fluid block to tensorrt network, NOTE it just convert
// operators, the INetwork's inputs and outputs should specified in some
// other modules.
void ConvertBlock(const framework::proto::BlockDesc& block,
const std::unordered_set<std::string>& parameters,
const framework::Scope& scope,
TensorRTEngine* engine) {
VLOG(1) << "Convert a fluid block to tensorrt network";
std::unique_lock<std::mutex> lk(mut_);
for (int i = 0; i < block.ops_size(); i++) {
const auto& op = block.ops(i);
Expand Down Expand Up @@ -787,6 +810,9 @@ class OpConverter {

VLOG(3) << output_tensor_names[i] << "'s dimension :["
<< string::join_strings(tmp_vec, ',') << "]";
VLOG(1) << "Paddle-TRT inferred " << output_tensor_names[i]
<< "'s dimension is :[" << string::join_strings(tmp_vec, ',')
<< "]";
// The following check may cause errors in CI, but is necessary in the
// latest version.
// PADDLE_ENFORCE_GE(
Expand Down
23 changes: 17 additions & 6 deletions paddle/fluid/inference/tensorrt/convert/tile_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ class TileOpConverter : public OpConverter {
auto output_name = op_desc.Output("Out")[0];

if (engine_->with_dynamic_shape()) {
std::vector<int32_t> start(rank, 0);
std::vector<int32_t> stride(rank, 1);
auto start_tensor =
Add1DConstantLayer(start, output_name + "start_tensor");
auto stride_tensor =
Add1DConstantLayer(stride, output_name + "stride_tensor");
auto input_shape_tensor = Shape(input);

nvinfer1::ITensor* repeat_tensor = nullptr;
Expand Down Expand Up @@ -76,9 +70,26 @@ class TileOpConverter : public OpConverter {
itensors.push_back(one_rank_tensor);
itensors.push_back(repeat_tensor);
repeat_expand_tensor = Concat(itensors);
}
if (rank < repeat_rank) {
auto* one_rank_tensor =
Add1DConstantLayer(std::vector<int32_t>(repeat_rank - rank, 1));
std::vector<nvinfer1::ITensor*> itensors;
itensors.push_back(one_rank_tensor);
itensors.push_back(input_shape_tensor);
input_shape_tensor = Concat(itensors);
// need reshape input to more dims.
input = Reshape(input, input_shape_tensor, "reshape_input_befor_slice");
repeat_expand_tensor = repeat_tensor;
} else {
repeat_expand_tensor = repeat_tensor;
}
std::vector<int32_t> start(std::max(rank, repeat_rank), 0);
std::vector<int32_t> stride(std::max(rank, repeat_rank), 1);
auto start_tensor =
Add1DConstantLayer(start, output_name + "start_tensor");
auto stride_tensor =
Add1DConstantLayer(stride, output_name + "stride_tensor");
auto output_shape_tensor = Prod(input_shape_tensor, repeat_expand_tensor);
auto layer = TRT_ENGINE_ADD_LAYER(engine_,
Slice,
Expand Down
7 changes: 6 additions & 1 deletion paddle/fluid/inference/tensorrt/op_teller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bool IsDynamicShapeOp(const framework::OpDesc& desc) {
}
}
}
return true;
return false;
}

// Just tell by the op_types.
Expand Down Expand Up @@ -2281,6 +2281,11 @@ struct SimpleOpTypeSetTeller : public Teller {
auto x_var_name = desc.Input("X")[0];
auto* x_var_desc = block->FindVarRecursive(x_var_name);
const auto x_shape = x_var_desc->GetShape();

auto dtype = x_var_desc->GetDataType();
if (dtype != framework::proto::VarType::FP32) {
return false;
}
if (!with_dynamic_shape && (x_shape.size() == 1 || x_shape.empty())) {
VLOG(3) << op_type
<< " op does not support input's dim is 1 or 0 in tensorrt "
Expand Down
28 changes: 14 additions & 14 deletions test/ir/inference/test_trt_convert_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def is_program_valid(self, program_config: ProgramConfig) -> bool:

def sample_program_configs(self, *args, **kwargs):
def generate_input1(attrs: List[Dict[str, Any]]):
return np.ones([1, 2, 3, 4]).astype(np.float32)
return np.ones([1, 2]).astype(np.float32)

dics = [{"repeat_times": kwargs['repeat_times']}]

Expand Down Expand Up @@ -70,9 +70,9 @@ def sample_predictor_configs(
self, program_config
) -> (paddle_infer.Config, List[int], float):
def generate_dynamic_shape(attrs):
self.dynamic_shape.min_input_shape = {"input_data": [1, 2, 3, 4]}
self.dynamic_shape.max_input_shape = {"input_data": [4, 3, 64, 64]}
self.dynamic_shape.opt_input_shape = {"input_data": [1, 3, 64, 64]}
self.dynamic_shape.min_input_shape = {"input_data": [1, 2]}
self.dynamic_shape.max_input_shape = {"input_data": [4, 3]}
self.dynamic_shape.opt_input_shape = {"input_data": [1, 3]}

def clear_dynamic_shape():
self.dynamic_shape.min_input_shape = {}
Expand Down Expand Up @@ -116,7 +116,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape):
attrs, True
), 1e-3

@given(repeat_times=st.sampled_from([[100], [1, 2], [0, 3], [1, 2, 100]]))
@given(repeat_times=st.sampled_from([[1], [1, 2], [0, 3]]))
def test(self, *args, **kwargs):
self.run_test(*args, **kwargs)

Expand All @@ -127,7 +127,7 @@ def is_program_valid(self, program_config: ProgramConfig) -> bool:

def sample_program_configs(self):
def generate_input1(attrs: List[Dict[str, Any]]):
return np.ones([1, 2, 3, 4]).astype(np.float32)
return np.ones([1, 2]).astype(np.float32)

dics = [{}]
dics_input = [
Expand All @@ -140,7 +140,7 @@ def generate_input1(attrs: List[Dict[str, Any]]):
"op_outputs": {"Out": ["repeat_times"]},
"op_attrs": {
"dtype": 2,
"str_value": "10",
"str_value": "1",
"shape": [1],
},
},
Expand Down Expand Up @@ -169,9 +169,9 @@ def sample_predictor_configs(
self, program_config
) -> (paddle_infer.Config, List[int], float):
def generate_dynamic_shape(attrs):
self.dynamic_shape.min_input_shape = {"tile_input": [1, 2, 3, 4]}
self.dynamic_shape.max_input_shape = {"tile_input": [4, 3, 64, 64]}
self.dynamic_shape.opt_input_shape = {"tile_input": [1, 2, 3, 4]}
self.dynamic_shape.min_input_shape = {"tile_input": [1, 2]}
self.dynamic_shape.max_input_shape = {"tile_input": [4, 3]}
self.dynamic_shape.opt_input_shape = {"tile_input": [1, 2]}

def clear_dynamic_shape():
self.dynamic_shape.min_input_shape = {}
Expand Down Expand Up @@ -215,7 +215,7 @@ def is_program_valid(self, program_config: ProgramConfig) -> bool:

def sample_program_configs(self):
def generate_input1(attrs: List[Dict[str, Any]]):
return np.ones([1, 2, 3, 4]).astype(np.float32)
return np.ones([1, 2]).astype(np.float32)

dics = [{}]
dics_input = [
Expand Down Expand Up @@ -270,9 +270,9 @@ def sample_predictor_configs(
self, program_config
) -> (paddle_infer.Config, List[int], float):
def generate_dynamic_shape(attrs):
self.dynamic_shape.min_input_shape = {"tile_input": [1, 2, 3, 4]}
self.dynamic_shape.max_input_shape = {"tile_input": [4, 3, 64, 64]}
self.dynamic_shape.opt_input_shape = {"tile_input": [1, 2, 3, 4]}
self.dynamic_shape.min_input_shape = {"tile_input": [1, 2]}
self.dynamic_shape.max_input_shape = {"tile_input": [4, 3]}
self.dynamic_shape.opt_input_shape = {"tile_input": [1, 2]}

def clear_dynamic_shape():
self.dynamic_shape.min_input_shape = {}
Expand Down

0 comments on commit 9c0cb6c

Please sign in to comment.