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

[TOPI][Fix] Pool must return error if layout is tiled on H, W, D dimensions #13975

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
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
44 changes: 24 additions & 20 deletions include/tvm/topi/nn/pooling.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,50 +214,54 @@ inline Tensor pool_grad_impl(const Tensor& out_grad, const Tensor& x,
}
}

/*!
* \brief Find index of Depth, Height or Width dimension in a layout string.
*
* \param layout The layout string
* \param depth_axis set as the index of depth ('D') if not nullptr.
* \param height_axis set as the index of height ('H') if not nullptr.
* \param width_axis set as the index of width ('W') if not nullptr.
*
* \return true if the layout is valid (i.e., no tiling on D, H or W dimensions, no duplicates and
* if the requested dimensions are found), otherwise false.
*/
inline bool find_depth_height_width(const std::string& layout, int* depth_axis, int* height_axis,
int* width_axis) {
*depth_axis = -1;
*height_axis = -1;
*width_axis = -1;
if (depth_axis) *depth_axis = -1;
if (height_axis) *height_axis = -1;
if (width_axis) *width_axis = -1;
int curr_idx = 0;
for (size_t i = 0; i < layout.size(); ++i) {
if ((layout[i] >= 'A' && layout[i] <= 'Z') || (layout[i] >= 'a' && layout[i] <= 'z')) {
if (layout[i] == 'D') {
if (layout[i] == 'D' && depth_axis) {
if (*depth_axis != -1) return false;
*depth_axis = curr_idx;
} else if (layout[i] == 'H') {
} else if (layout[i] == 'H' && height_axis) {
if (*height_axis != -1) return false;
*height_axis = curr_idx;
} else if (layout[i] == 'W') {
} else if (layout[i] == 'W' && width_axis) {
if (*width_axis != -1) return false;
*width_axis = curr_idx;
} else if (layout[i] == 'd' || layout[i] == 'h' || layout[i] == 'w') {
// do not support split on height or width, e.g., NCHW16w
// do not support split on height, width or depth, e.g., NCHW16w
return false;
}
++curr_idx;
}
}
if (*depth_axis == -1 || *height_axis == -1 || *width_axis == -1) return false;
if ((depth_axis && *depth_axis == -1) || (height_axis && *height_axis == -1) ||
(width_axis && *width_axis == -1))
return false;
return true;
}

inline bool find_height_width(const std::string& layout, int* height_axis, int* width_axis) {
int dummy;
ICHECK_EQ(find_depth_height_width(layout, &dummy, height_axis, width_axis), false);
if (*height_axis != -1 && *width_axis != -1) {
return true;
}
return false;
return find_depth_height_width(layout, /*depth_axis=*/nullptr, height_axis, width_axis);
}

inline bool find_width(const std::string& layout, int* width_axis) {
int dummy;
ICHECK_EQ(find_depth_height_width(layout, &dummy, &dummy, width_axis), false);
if (*width_axis != -1) {
return true;
}
return false;
return find_depth_height_width(layout, /*depth_axis=*/nullptr, /*height_axis=*/nullptr,
width_axis);
}

/*!
Expand Down
55 changes: 48 additions & 7 deletions tests/python/topi/python/test_topi_pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
# pylint: disable=invalid-name, too-many-locals, too-many-statements, unused-argument
"""Test code for pooling"""
import math
import pytest

import numpy as np
import tvm
import tvm.testing
import tvm.topi.testing
from tvm import te, topi
from tvm import te, topi, TVMError
from tvm.topi.utils import get_const_tuple

_pool_schedule = {
Expand Down Expand Up @@ -762,10 +763,50 @@ def test_pool1d():
verify_pool1d([1, 31, 16], [3], [3], [3], [3, 0], "max", True, layout="NWC")


def test_pool_invalid_tiled_layout():

with pytest.raises(TVMError, match="Unsupported layout NCHWD4d"):
A_3d = te.placeholder([1, 16, 32, 32, 32], name="A")
B = topi.nn.pool3d(
A_3d,
kernel=[2, 2, 2],
stride=[2, 2, 2],
dilation=[1, 1, 1],
padding=[0, 0, 0, 0, 0, 0],
pool_type="avg",
ceil_mode=False,
count_include_pad=True,
layout="NCHWD4d",
)

with pytest.raises(TVMError, match="Unsupported layout NCHW4h4w"):
A_2d = te.placeholder([1, 16, 32, 32], name="A")
B = topi.nn.pool2d(
A_2d,
kernel=[2, 2],
stride=[2, 2],
dilation=[1, 1],
padding=[0, 0, 0, 0],
pool_type="avg",
ceil_mode=False,
count_include_pad=True,
layout="NCHW4h4w",
)

with pytest.raises(TVMError, match="Unsupported layout NCW4w"):
A_1d = te.placeholder([1, 16, 32], name="A")
B = topi.nn.pool1d(
A_1d,
kernel=[2],
stride=[2],
dilation=[1],
padding=[0, 0],
pool_type="avg",
ceil_mode=False,
count_include_pad=True,
layout="NCW4w",
)


if __name__ == "__main__":
test_pool1d()
test_pool2d()
test_pool3d()
test_pool_grad()
test_global_pool()
test_adaptive_pool()
tvm.testing.main()