Skip to content

Commit

Permalink
fix(pooling): fix the tests and the 1D pooling cases
Browse files Browse the repository at this point in the history
Signed-off-by: Naren Dasan <[email protected]>
Signed-off-by: Naren Dasan <[email protected]>
  • Loading branch information
narendasan committed Jun 15, 2020
1 parent 7dc4af4 commit a90e6db
Show file tree
Hide file tree
Showing 2 changed files with 271 additions and 30 deletions.
93 changes: 77 additions & 16 deletions core/conversion/converters/impl/pooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ bool MaxPoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args& ar
}


auto kernel_size = util::toDimsHW(args[1].unwrapToIntList());
auto kernel_size = util::toDims(args[1].unwrapToIntList());
LOG_DEBUG("kernel_size: " << kernel_size);
auto padding = util::toDimsHW(args[3].unwrapToIntList());
auto padding = util::toDims(args[3].unwrapToIntList());
LOG_DEBUG("padding: " << padding);
auto stride = util::toDims(args[2].unwrapToIntList());
LOG_DEBUG("stride: " << stride);

auto dilation = util::toDims(args[4].unwrapToIntList());

TRTORCH_ASSERT(dilation == util::toDims(std::vector<int64_t>({1,1})), "Pooling dilation is not supported in TensorRT");
TRTORCH_ASSERT(dilation == util::toDims(std::vector<int64_t>(dilation.nbDims, 1)), "Pooling dilation is not supported in TensorRT");

LOG_DEBUG("dilation: " << dilation);
LOG_WARNING("Dilation not used in max pooling converter");
bool ceil_mode = args[5].unwrapToBool();

auto new_layer = ctx->net->addPoolingNd(*in, nvinfer1::PoolingType::kMAX, kernel_size);
TRTORCH_CHECK(new_layer, "Unable to create Max Pool 2D layer from node: " << *n);
TRTORCH_CHECK(new_layer, "Unable to create Max Pooling layer from node: " << *n);

new_layer->setName(util::node_info(n).c_str());
new_layer->setPaddingNd(padding);
Expand Down Expand Up @@ -77,9 +77,9 @@ bool AvgPoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args& ar
}


auto kernel_size = util::toDimsHW(args[1].unwrapToIntList());
auto kernel_size = util::toDims(args[1].unwrapToIntList());
LOG_DEBUG("kernel_size: " << kernel_size);
auto padding = util::toDimsHW(args[3].unwrapToIntList());
auto padding = util::toDims(args[3].unwrapToIntList());
LOG_DEBUG("padding: " << padding);
auto stride = util::toDims(args[2].unwrapToIntList());
LOG_DEBUG("stride: " << stride);
Expand All @@ -88,7 +88,7 @@ bool AvgPoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args& ar
bool count_inlcude_pad = args[5].unwrapToBool();

auto new_layer = ctx->net->addPoolingNd(*in, nvinfer1::PoolingType::kAVERAGE, kernel_size);
TRTORCH_CHECK(new_layer, "Unable to create Avg Pool 2D layer from node: " << *n);
TRTORCH_CHECK(new_layer, "Unable to create Avg Pooling layer from node: " << *n);

new_layer->setName(util::node_info(n).c_str());
new_layer->setPaddingNd(padding);
Expand Down Expand Up @@ -118,12 +118,67 @@ bool AvgPoolingConverter(ConversionCtx* ctx, const torch::jit::Node* n, args& ar

auto pooling_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
.pattern({
"aten::max_pool1d(Tensor self, int[2] kernel_size, int[2] stride=[], int[2] padding=[0, 0], int[2] dilation=[1, 1], bool ceil_mode=False) -> (Tensor)",
"aten::max_pool1d(Tensor self, int[1] kernel_size, int[1] stride=[], int[1] padding=[], int[1] dilation=[], bool ceil_mode=False) -> (Tensor)",
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
return MaxPoolingConverter(ctx, n, args);
auto in = args[0].ITensor();
auto shape = util::toVec(in->getDimensions());

// Max Pool needs at least 4D input
if (shape.size() < 4) {
auto new_shape = util::toDimsPad(shape, 4);
LOG_DEBUG("Input shape is less than 4D got: " << util::toDims(shape) << ", inserting shuffle layer to reshape to 4D tensor shape: " << new_shape);
auto shuffle = ctx->net->addShuffle(*in);
shuffle->setReshapeDimensions(new_shape);
shuffle->setName((util::node_info(n) + " [Reshape to " + util::toStr(new_shape) + ']').c_str());
in = shuffle->getOutput(0);
}

auto kernel_vec = args[1].unwrapToIntList().vec();
kernel_vec.insert(kernel_vec.begin(), 1);
auto kernel_size = util::toDims(kernel_vec);
LOG_DEBUG("kernel_size: " << kernel_size);
auto stride_vec = args[2].unwrapToIntList().vec();
stride_vec.insert(stride_vec.begin(), 1);
auto stride = util::toDims(stride_vec);
LOG_DEBUG("stride: " << stride);
auto padding_vec = args[3].unwrapToIntList().vec();
padding_vec.insert(padding_vec.begin(), 0);
auto padding = util::toDims(padding_vec);
LOG_DEBUG("padding: " << padding);

auto dilation = util::toDims(args[4].unwrapToIntList());

TRTORCH_ASSERT(dilation == util::toDims(std::vector<int64_t>(dilation.nbDims, 1)), "Pooling dilation is not supported in TensorRT");

LOG_DEBUG("dilation: " << dilation);
LOG_WARNING("Dilation not used in max pooling converter");
bool ceil_mode = args[5].unwrapToBool();

auto new_layer = ctx->net->addPoolingNd(*in, nvinfer1::PoolingType::kMAX, kernel_size);
TRTORCH_CHECK(new_layer, "Unable to create Max Pooling layer from node: " << *n);

new_layer->setName(util::node_info(n).c_str());
new_layer->setPaddingNd(padding);
if (stride.nbDims != 2 && ctx->settings.device == nvinfer1::DeviceType::kDLA) {
if (!ctx->settings.allow_gpu_fallback) {
TRTORCH_THROW_ERROR("DLA Pooling stride is limited to 2D, allow GPU fallback");
} else {
LOG_WARNING("DLA Pooling stride is limited to 2D, will run on GPU");
}
}
new_layer->setStrideNd(stride);

auto padding_mode = ceil_mode ? nvinfer1::PaddingMode::kEXPLICIT_ROUND_UP : nvinfer1::PaddingMode::kEXPLICIT_ROUND_DOWN;
new_layer->setPaddingMode(padding_mode);

new_layer->setName(util::node_info(n).c_str());
auto out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], new_layer->getOutput(0));

LOG_DEBUG("Output tensor shape: " << out_tensor->getDimensions());
return true;
}
}).pattern({
"aten::avg_pool1d(Tensor self, int[2] kernel_size, int[2] stride=[], int[2] padding=[0, 0], bool ceil_mode=False, bool count_include_pad=True) -> (Tensor)",
"aten::avg_pool1d(Tensor self, int[1] kernel_size, int[1] stride=[], int[1] padding=0, bool ceil_mode=False, bool count_include_pad=True) -> Tensor",
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
auto in = args[0].ITensor();
auto shape = util::toVec(in->getDimensions());
Expand All @@ -139,12 +194,18 @@ auto pooling_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
}


auto kernel_size = util::toDimsHW(args[1].unwrapToIntList());
auto kernel_vec = args[1].unwrapToIntList().vec();
kernel_vec.insert(kernel_vec.begin(), 1);
auto kernel_size = util::toDims(kernel_vec);
LOG_DEBUG("kernel_size: " << kernel_size);
auto padding = util::toDimsHW(args[3].unwrapToIntList());
LOG_DEBUG("padding: " << padding);
auto stride = util::toDims(args[2].unwrapToIntList());
auto stride_vec = args[2].unwrapToIntList().vec();
stride_vec.insert(stride_vec.begin(), 1);
auto stride = util::toDims(stride_vec);
LOG_DEBUG("stride: " << stride);
auto padding_vec = args[3].unwrapToIntList().vec();
padding_vec.insert(padding_vec.begin(), 0);
auto padding = util::toDims(padding_vec);
LOG_DEBUG("padding: " << padding);

bool ceil_mode = args[4].unwrapToBool();
bool count_inlcude_pad = args[5].unwrapToBool();
Expand Down Expand Up @@ -187,12 +248,12 @@ auto pooling_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
return AvgPoolingConverter(ctx, n, args);
}
}).pattern({
"aten::max_pool3d(Tensor self, int[2] kernel_size, int[2] stride=[], int[2] padding=[0, 0], int[2] dilation=[1, 1], bool ceil_mode=False) -> (Tensor)",
"aten::max_pool3d(Tensor self, int[3] kernel_size, int[3] stride=[], int[3] padding=[], int[3] dilation=[], bool ceil_mode=False) -> (Tensor)",
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
return MaxPoolingConverter(ctx, n, args);
}
}).pattern({
"aten::avg_pool3d(Tensor self, int[2] kernel_size, int[2] stride=[], int[2] padding=[0, 0], bool ceil_mode=False, bool count_include_pad=True, int? divisor_override=None) -> (Tensor)",
"aten::avg_pool3d(Tensor self, int[3] kernel_size, int[3] stride=[], int[3] padding=[], bool ceil_mode=False, bool count_include_pad=True, int? divisor_override=None) -> (Tensor)",
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
return AvgPoolingConverter(ctx, n, args);
}
Expand Down
Loading

0 comments on commit a90e6db

Please sign in to comment.