Skip to content

Commit

Permalink
[Fix] Pool must return error if layout is tiled on H, W, D
Browse files Browse the repository at this point in the history
dimensions.
  • Loading branch information
psrivas2 committed Feb 13, 2023
1 parent 266ff51 commit 37821de
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 27 deletions.
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()

0 comments on commit 37821de

Please sign in to comment.