Ground truth pooling operator impelmented in numpy.
(
np_data: np.array,
kernel: tuple[int],
strides: tuple[int],
dilation: tuple[int],
padding_before: tuple[int],
padding_after: tuple[int],
pool_type: str,
count_include_pad: bool = True,
ceil_mode: bool = False,
dtype: str = "float32",
layout: str | None = None,
)
| 124 | |
| 125 | |
| 126 | def poolnd_python( |
| 127 | np_data: np.array, |
| 128 | kernel: tuple[int], |
| 129 | strides: tuple[int], |
| 130 | dilation: tuple[int], |
| 131 | padding_before: tuple[int], |
| 132 | padding_after: tuple[int], |
| 133 | pool_type: str, |
| 134 | count_include_pad: bool = True, |
| 135 | ceil_mode: bool = False, |
| 136 | dtype: str = "float32", |
| 137 | layout: str | None = None, |
| 138 | ) -> np.array: |
| 139 | """Ground truth pooling operator impelmented in numpy.""" |
| 140 | |
| 141 | np_data = _convert_from_layout(np_data, layout) |
| 142 | |
| 143 | out_shape = [np_data.shape[0], np_data.shape[1]] |
| 144 | for dim in range(2, len(np_data.shape)): |
| 145 | i = dim - 2 |
| 146 | val = ( |
| 147 | float( |
| 148 | np_data.shape[dim] |
| 149 | - (kernel[i] - 1) * dilation[i] |
| 150 | - 1 |
| 151 | + padding_before[i] |
| 152 | + padding_after[i] |
| 153 | ) |
| 154 | / strides[i] |
| 155 | ) |
| 156 | |
| 157 | if ceil_mode: |
| 158 | out_shape.append(int(math.ceil(val) + 1)) |
| 159 | else: |
| 160 | out_shape.append(int(math.floor(val) + 1)) |
| 161 | out_shape = tuple(out_shape) |
| 162 | |
| 163 | # Create a padded array, and a boolean mask showing which values are padded values |
| 164 | pad_value = 0 |
| 165 | if pool_type == "max" and not count_include_pad: |
| 166 | pad_value = tvm.te.min_value(dtype).value |
| 167 | pad_data = pad_tensor(np_data, pad_value, padding_before, padding_after, dtype) |
| 168 | pad_map = pad_tensor(np.ones_like(np_data), 0, padding_before, padding_after, "bool") |
| 169 | |
| 170 | # Create iterator which gives all indices for output array |
| 171 | dim_iterators = [] |
| 172 | for spatial_dimension in range(2, len(np_data.shape)): |
| 173 | dim_iterators.append(range(out_shape[spatial_dimension])) |
| 174 | coord_iterator = itertools.product(*dim_iterators) |
| 175 | |
| 176 | ret_np = np.zeros(shape=out_shape).astype(dtype) |
| 177 | for coordinate in coord_iterator: |
| 178 | # Get index into the values that any pool operation will use for given coordinate |
| 179 | np_index = get_slice( |
| 180 | spatial_dimensions=len(out_shape) - 2, |
| 181 | pad_np=pad_data, |
| 182 | dim_coord=coordinate, |
| 183 | kernel=kernel, |
nothing calls this directly
no test coverage detected
searching dependent graphs…