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

[TRT] Fix batch norm converter for rank < 4 inputs. #161

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 14 additions & 0 deletions src/runtime/contrib/tensorrt/tensorrt_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@ class BatchNormOpConverter : public TrtOpConverter {
CHECK_EQ(gamma.count, var.count);
CHECK(bn_attr->axis == 1 || bn_attr->axis == 3);
const bool need_transpose = bn_attr->axis == 3;
const int required_rank = TRT_HAS_IMPLICIT_BATCH(params) ? 3 : 4;
auto input_dims = TrtDimsToVector(input->getDimensions());
CHECK(input_dims.size() > 0 && input_dims.size() <= required_rank);
const bool need_reshape = input_dims.size() != required_rank;

void* weight_scale_ptr = new float[gamma.count];
nvinfer1::Weights weight_scale{nvinfer1::DataType::kFLOAT, weight_scale_ptr, gamma.count};
Expand Down Expand Up @@ -557,10 +561,20 @@ class BatchNormOpConverter : public TrtOpConverter {
if (need_transpose) {
input = Transpose(params, input, {0, 3, 1, 2});
}
if (need_reshape) {
// Add dims of size 1 until rank is required_rank.
std::vector<int> new_shape(input_dims);
while (new_shape.size() < required_rank) new_shape.insert(new_shape.end(), 1);
input = Reshape(params, input, new_shape);
}
nvinfer1::IScaleLayer* scale_layer = params->network->addScale(
*input, nvinfer1::ScaleMode::kCHANNEL, weight_shift, weight_scale, power);
CHECK(scale_layer != nullptr);
auto output = scale_layer->getOutput(0);
if (need_reshape) {
// Remove added dims.
output = Reshape(params, output, input_dims);
}
if (need_transpose) {
output = Transpose(params, output, {0, 2, 3, 1});
}
Expand Down