未验证 提交 a3cf25e3 编写于 作者: C chen 提交者: GitHub

[Paddle-TRT] Convert 0D tensor to 1D tensor, increase the shape tensor's...

[Paddle-TRT] Convert 0D tensor to 1D tensor, increase the shape tensor's number count when collecting shape (#55503)

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

* recover identity_op_clean_pass.cc
上级 76530a2a
...@@ -2236,10 +2236,10 @@ void AnalysisPredictor::HookCollectShapeRangeInfo() { ...@@ -2236,10 +2236,10 @@ void AnalysisPredictor::HookCollectShapeRangeInfo() {
// We need collect value range for shape tensor for Paddle-TRT's use. // 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 // 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 // This is a simple method to identify all shape tensors with some
// mistakes, but it doesn't matter. // 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 || if ((tensor.dtype() == phi::DataType::INT32 ||
tensor.dtype() == phi::DataType::INT64) && tensor.dtype() == phi::DataType::INT64) &&
is_shape_tensor) { is_shape_tensor) {
......
...@@ -30,7 +30,7 @@ class ElementwiseTensorOpConverter : public OpConverter { ...@@ -30,7 +30,7 @@ class ElementwiseTensorOpConverter : public OpConverter {
auto* X = engine_->GetITensor(op_desc.Input("X").front()); auto* X = engine_->GetITensor(op_desc.Input("X").front());
nvinfer1::ITensor* Y = nullptr; nvinfer1::ITensor* Y = nullptr;
auto* Y_v = scope.FindVar(op_desc.Input("Y").front()); auto* Y_v = scope.FindVar(op_desc.Input("Y").front());
if (Y_v) { if (Y_v && !engine_->with_dynamic_shape()) {
// Y is weight // Y is weight
auto* Y_t = Y_v->GetMutable<phi::DenseTensor>(); auto* Y_t = Y_v->GetMutable<phi::DenseTensor>();
std::vector<int> dims_y = phi::vectorize<int>(Y_t->dims()); std::vector<int> dims_y = phi::vectorize<int>(Y_t->dims());
......
...@@ -308,9 +308,15 @@ class OpConverter { ...@@ -308,9 +308,15 @@ class OpConverter {
auto var_shape = var->GetShape(); auto var_shape = var->GetShape();
if (engine->with_dynamic_shape()) { if (engine->with_dynamic_shape()) {
#if IS_TRT_VERSION_GE(6000) #if IS_TRT_VERSION_GE(6000)
auto min_input_shape = engine->min_input_shape()[input]; if (!(engine->min_input_shape().count(input) &&
auto max_input_shape = engine->max_input_shape()[input]; engine->max_input_shape().count(input) &&
auto optim_input_shape = engine->optim_input_shape()[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(); size_t ranks = min_input_shape.size();
std::vector<int64_t> input_shape; std::vector<int64_t> input_shape;
...@@ -732,6 +738,23 @@ class OpConverter { ...@@ -732,6 +738,23 @@ class OpConverter {
layer_name += output_tensor_names[i]; layer_name += output_tensor_names[i];
if (i != num_out - 1) layer_name += ", "; 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()); layer->setName((layer_name + ")").c_str());
} }
void SetEngine(TensorRTEngine* engine) { engine_ = engine; } void SetEngine(TensorRTEngine* engine) { engine_ = engine; }
......
...@@ -532,6 +532,11 @@ nvinfer1::ITensor *TensorRTEngine::ConvertWeight2ITensor( ...@@ -532,6 +532,11 @@ nvinfer1::ITensor *TensorRTEngine::ConvertWeight2ITensor(
for (int64_t i = 0; i < trt_in_shape.nbDims; i++) { for (int64_t i = 0; i < trt_in_shape.nbDims; i++) {
trt_in_shape.d[i] = var_dims[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 // In fact , this is not always right, because we can't determine if the 0th
// dimension is batch. Just for run chenqu's model // dimension is batch. Just for run chenqu's model
if (!this->with_dynamic_shape()) { if (!this->with_dynamic_shape()) {
......
...@@ -524,6 +524,10 @@ class TensorRTEngine { ...@@ -524,6 +524,10 @@ class TensorRTEngine {
for (const auto& it : runtime_input_shape) { for (const auto& it : runtime_input_shape) {
auto name = it.first; auto name = it.first;
auto input_shape = it.second; 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 min_change = false;
bool max_change = false; bool max_change = false;
std::vector<int> bak_min_shape; std::vector<int> bak_min_shape;
......
...@@ -594,6 +594,18 @@ class TensorRTEngineOp : public framework::OperatorBase { ...@@ -594,6 +594,18 @@ class TensorRTEngineOp : public framework::OperatorBase {
t.ShareDataWith(out); t.ShareDataWith(out);
} }
auto t_shape = phi::vectorize<int64_t>(t.dims()); 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 // Get index of profile 0 first, then plus binding offset
const int bind_index = const int bind_index =
engine->engine()->getBindingIndex(x.c_str()) + binding_offset; engine->engine()->getBindingIndex(x.c_str()) + binding_offset;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册