Skip to content

Commit

Permalink
[Paddle-TRT] Convert 0D tensor to 1D tensor, increase the shape tenso…
Browse files Browse the repository at this point in the history
…r's number count when collecting shape (PaddlePaddle#55503)

* make 0-D tensor to 1-D tensor to support Grounding-SAM and add shape check

* recover identity_op_clean_pass.cc
  • Loading branch information
ckl117 authored and wyf committed Aug 30, 2023
1 parent fdac449 commit dd808d5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
4 changes: 2 additions & 2 deletions paddle/fluid/inference/api/analysis_predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2236,10 +2236,10 @@ void AnalysisPredictor::HookCollectShapeRangeInfo() {

// We need collect value range for shape tensor for Paddle-TRT's use.
// To be noticed, this method to identify all shape tensors is based on
// assumption that all shape tensors in the model have numbers <= 7.
// assumption that all shape tensors in the model have numbers <= 8.
// This is a simple method to identify all shape tensors with some
// mistakes, but it doesn't matter.
auto is_shape_tensor = tensor.numel() <= 7 && tensor.numel() >= 1;
auto is_shape_tensor = tensor.numel() <= 8 && tensor.numel() >= 1;
if ((tensor.dtype() == phi::DataType::INT32 ||
tensor.dtype() == phi::DataType::INT64) &&
is_shape_tensor) {
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/inference/tensorrt/convert/elementwise_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ElementwiseTensorOpConverter : public OpConverter {
auto* X = engine_->GetITensor(op_desc.Input("X").front());
nvinfer1::ITensor* Y = nullptr;
auto* Y_v = scope.FindVar(op_desc.Input("Y").front());
if (Y_v) {
if (Y_v && !engine_->with_dynamic_shape()) {
// Y is weight
auto* Y_t = Y_v->GetMutable<phi::DenseTensor>();
std::vector<int> dims_y = phi::vectorize<int>(Y_t->dims());
Expand Down
29 changes: 26 additions & 3 deletions paddle/fluid/inference/tensorrt/convert/op_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,15 @@ class OpConverter {
auto var_shape = var->GetShape();
if (engine->with_dynamic_shape()) {
#if IS_TRT_VERSION_GE(6000)
auto min_input_shape = engine->min_input_shape()[input];
auto max_input_shape = engine->max_input_shape()[input];
auto optim_input_shape = engine->optim_input_shape()[input];
if (!(engine->min_input_shape().count(input) &&
engine->max_input_shape().count(input) &&
engine->optim_input_shape().count(input))) {
PADDLE_THROW(platform::errors::InvalidArgument(
"Cannot get %s min/max/opt shape", input));
}
auto min_input_shape = engine->min_input_shape().at(input);
auto max_input_shape = engine->max_input_shape().at(input);
auto optim_input_shape = engine->optim_input_shape().at(input);
size_t ranks = min_input_shape.size();

std::vector<int64_t> input_shape;
Expand Down Expand Up @@ -732,6 +738,23 @@ class OpConverter {
layer_name += output_tensor_names[i];
if (i != num_out - 1) layer_name += ", ";
}
for (size_t i = 0; i < num_out; i++) {
nvinfer1::Dims tmp_dims = layer->getOutput(i)->getDimensions();
std::vector<int> tmp_vec;
for (int i = 0; i < tmp_dims.nbDims; i++)
tmp_vec.push_back(tmp_dims.d[i]);

VLOG(3) << output_tensor_names[i] << "'s dimension :["
<< string::join_strings(tmp_vec, ',') << "]";
// The following check may cause errors in CI, but is necessary in the
// latest version.
// PADDLE_ENFORCE_GE(
// layer->getOutput(i)->getDimensions().nbDims,
// 0,
// platform::errors::InvalidArgument(
// "Error occures in Paddle-TRT layer with output name: %s",
// output_tensor_names[i].c_str()));
}
layer->setName((layer_name + ")").c_str());
}
void SetEngine(TensorRTEngine* engine) { engine_ = engine; }
Expand Down
5 changes: 5 additions & 0 deletions paddle/fluid/inference/tensorrt/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,11 @@ nvinfer1::ITensor *TensorRTEngine::ConvertWeight2ITensor(
for (int64_t i = 0; i < trt_in_shape.nbDims; i++) {
trt_in_shape.d[i] = var_dims[i];
}
// Make 0-D tensor to 1-D tensor.
if (trt_in_shape.nbDims == 0) {
trt_in_shape.nbDims = 1;
trt_in_shape.d[0] = 1;
}
// In fact , this is not always right, because we can't determine if the 0th
// dimension is batch. Just for run chenqu's model
if (!this->with_dynamic_shape()) {
Expand Down
4 changes: 4 additions & 0 deletions paddle/fluid/inference/tensorrt/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ class TensorRTEngine {
for (const auto& it : runtime_input_shape) {
auto name = it.first;
auto input_shape = it.second;
// Make 0-D tensor to 1-D tensor.
if (input_shape.size() == 0) {
input_shape.push_back(1);
}
bool min_change = false;
bool max_change = false;
std::vector<int> bak_min_shape;
Expand Down
12 changes: 12 additions & 0 deletions paddle/fluid/operators/tensorrt/tensorrt_engine_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,18 @@ class TensorRTEngineOp : public framework::OperatorBase {
t.ShareDataWith(out);
}
auto t_shape = phi::vectorize<int64_t>(t.dims());

// This must be a zero dimension tensor.
// At present, we convert it to a 1D tensor to feed them into Trt.
if (t_shape.size() == 0) {
PADDLE_ENFORCE_EQ(
t.numel(),
1UL,
platform::errors::PreconditionNotMet(
"This tensor must have one element, but got %ld.", t.numel()));
t_shape.push_back(1);
}

// Get index of profile 0 first, then plus binding offset
const int bind_index =
engine->engine()->getBindingIndex(x.c_str()) + binding_offset;
Expand Down

0 comments on commit dd808d5

Please sign in to comment.