From 149134357acea86410511324e403659f74360e80 Mon Sep 17 00:00:00 2001 From: philass Date: Tue, 12 Sep 2023 00:39:41 -0700 Subject: [PATCH 1/2] Add support for opset 19 AveragePool with dilations Signed-off-by: philass --- .../NNPA/Conversion/ONNXToZHigh/ONNXToZHigh.td | 2 +- src/Builder/OpBuildTable.inc | 2 +- src/Conversion/ONNXToKrnl/NN/Pooling.cpp | 16 +--------------- src/Dialect/ONNX/ONNXOps.td.inc | 7 ++----- src/Dialect/ONNX/ONNXOps/NN/Pooling.cpp | 6 ++++-- utils/gen_onnx_mlir.py | 2 +- 6 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/Accelerators/NNPA/Conversion/ONNXToZHigh/ONNXToZHigh.td b/src/Accelerators/NNPA/Conversion/ONNXToZHigh/ONNXToZHigh.td index 2762893b3d..5d6d2ab924 100644 --- a/src/Accelerators/NNPA/Conversion/ONNXToZHigh/ONNXToZHigh.td +++ b/src/Accelerators/NNPA/Conversion/ONNXToZHigh/ONNXToZHigh.td @@ -405,7 +405,7 @@ def GetI64ArrayAttrStridesAveragePool: NativeCodeCall< "($0.getDefiningOp()))">; def replaceONNXAveragePoolPattern : Pattern< - (ONNXAveragePoolOp:$res $x, $_, $_, $_, $_, $_, $_), + (ONNXAveragePoolOp:$res $x, $_, $_, $_, $_, $_, $_, $_), [ // Get attributes using shape helper (GetStrAttrPaddingtypeAveragePool:$padtype $res), diff --git a/src/Builder/OpBuildTable.inc b/src/Builder/OpBuildTable.inc index e6b1ac4d38..b07cf9d9fd 100644 --- a/src/Builder/OpBuildTable.inc +++ b/src/Builder/OpBuildTable.inc @@ -18,7 +18,7 @@ op_dialect_version_map_["Asin"] = {7}; op_dialect_version_map_["Asinh"] = {9}; op_dialect_version_map_["Atan"] = {7}; op_dialect_version_map_["Atanh"] = {9}; -op_dialect_version_map_["AveragePool"] = {11}; +op_dialect_version_map_["AveragePool"] = {19}; op_dialect_version_map_["BatchNormalization"] = {15}; op_dialect_version_map_["Bernoulli"] = {15}; op_dialect_version_map_["Binarizer"] = {1}; diff --git a/src/Conversion/ONNXToKrnl/NN/Pooling.cpp b/src/Conversion/ONNXToKrnl/NN/Pooling.cpp index 97405615b6..f2e8449298 100644 --- a/src/Conversion/ONNXToKrnl/NN/Pooling.cpp +++ b/src/Conversion/ONNXToKrnl/NN/Pooling.cpp @@ -57,15 +57,8 @@ Value emitScalarOpFor( // template std::vector getDilations(PoolOp poolOp) { - return {}; -} - -// MaxPool has dilations attribute. -template <> -std::vector getDilations( - ONNXMaxPoolSingleOutOp poolOp) { std::vector dilations; - auto dilationsAttribute = poolOp.getDilationsAttr(); + ArrayAttr dilationsAttribute = poolOp.getDilationsAttr(); bool isDefaultDilations = true; for (auto dilation : dilationsAttribute.getValue()) { int64_t dilationValue = dilation.cast().getInt(); @@ -84,13 +77,6 @@ std::vector getDilations( // template std::optional getDilationAttr(PoolOp poolOp) { - return std::nullopt; -} - -// MaxPool has dilations attribute. -template <> -std::optional getDilationAttr( - ONNXMaxPoolSingleOutOp poolOp) { return poolOp.getDilations(); } diff --git a/src/Dialect/ONNX/ONNXOps.td.inc b/src/Dialect/ONNX/ONNXOps.td.inc index 77ceec013a..287d90944b 100644 --- a/src/Dialect/ONNX/ONNXOps.td.inc +++ b/src/Dialect/ONNX/ONNXOps.td.inc @@ -424,11 +424,7 @@ def ONNXAveragePoolOp:ONNX_Op<"AveragePool", ``` output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i] + 1) ``` - if ceil_mode is enabled - - ``` - * pad_shape[i] is sum of pads along axis i - ``` + if ceil_mode is enabled `pad_shape[i]` is the sum of pads along axis `i`. `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following: ``` @@ -446,6 +442,7 @@ def ONNXAveragePoolOp:ONNX_Op<"AveragePool", DefaultValuedStrAttr:$auto_pad, DefaultValuedAttr:$ceil_mode, DefaultValuedAttr:$count_include_pad, + OptionalAttr:$dilations, I64ArrayAttr:$kernel_shape, OptionalAttr:$pads, OptionalAttr:$strides); diff --git a/src/Dialect/ONNX/ONNXOps/NN/Pooling.cpp b/src/Dialect/ONNX/ONNXOps/NN/Pooling.cpp index 5426e541f7..95fca161ee 100644 --- a/src/Dialect/ONNX/ONNXOps/NN/Pooling.cpp +++ b/src/Dialect/ONNX/ONNXOps/NN/Pooling.cpp @@ -86,8 +86,8 @@ LogicalResult ONNXAveragePoolOpShapeHelper::computeShape() { ONNXAveragePoolOp poolOp = llvm::cast(op); return customComputeShape(operandAdaptor.getX(), /*W*/ nullptr, poolOp.getKernelShape(), poolOp.getAutoPad(), poolOp.getPads(), - poolOp.getStrides(), - /*dilation*/ std::nullopt, /*hasFilter*/ false, poolOp.getCeilMode()); + poolOp.getStrides(), poolOp.getDilations(), /*hasFilter*/ false, + poolOp.getCeilMode()); } } // namespace onnx_mlir @@ -117,6 +117,8 @@ LogicalResult ONNXAveragePoolOp::verify() { return failure(); if (failed(verifyStrides(this, spatialRank))) return failure(); + if (failed(verifyDilations(this, spatialRank))) + return failure(); if (failed(verifyPadding(this, spatialRank))) return failure(); return success(); diff --git a/utils/gen_onnx_mlir.py b/utils/gen_onnx_mlir.py index 488038eb62..8f3220e131 100755 --- a/utils/gen_onnx_mlir.py +++ b/utils/gen_onnx_mlir.py @@ -87,7 +87,7 @@ 'Asinh': [9], 'Atan': [7], 'Atanh': [9], - 'AveragePool': [11], + 'AveragePool': [19], 'BatchNormalization': [15], 'Bernoulli': [15], 'Binarizer': [1], From 7ec2dfd4ab8b44e31c844d3be55520622ea4065a Mon Sep 17 00:00:00 2001 From: philass Date: Tue, 12 Sep 2023 11:05:50 -0700 Subject: [PATCH 2/2] Add support for AveragePool to docs Signed-off-by: philass --- docs/SupportedONNXOps-cpu.md | 2 +- test/backend/inference_backend.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/SupportedONNXOps-cpu.md b/docs/SupportedONNXOps-cpu.md index 554c097f95..1200e38810 100644 --- a/docs/SupportedONNXOps-cpu.md +++ b/docs/SupportedONNXOps-cpu.md @@ -26,7 +26,7 @@ Onnx-mlir currently supports ONNX operations targeting up to opset 19. Limitatio | **Asinh** |9 - * | | | | **Atan** |7 - * | | | | **Atanh** |9 - * | | | -| **AveragePool** |6 - 18 | | | +| **AveragePool** |6 - * | | | | **BatchNormalization** |6 - * |Training not supported. | | | **Bernoulli** |none | | | | | **Binarizer** |none | | | | diff --git a/test/backend/inference_backend.py b/test/backend/inference_backend.py index 9e58bd08c4..4301977be3 100644 --- a/test/backend/inference_backend.py +++ b/test/backend/inference_backend.py @@ -148,7 +148,6 @@ def get_test_models(): # ==OP== AveragePool # ==MIN== 1 - # ==UNSUPPORTED== 19 # TODO: original comment stated "same_upper/lower with dynamic padding-shapes not supported." # However, I see the dyn shape test being done on all tests, including same_upper. So I am # assuming that this comment is outdated.