MCPcopy Index your code
hub / github.com/apache/tvm / poolnd_python

Function poolnd_python

python/tvm/topi/testing/poolnd_python.py:126–209  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

124
125
126def 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,

Callers

nothing calls this directly

Calls 11

_convert_from_layoutFunction · 0.85
tupleFunction · 0.85
pad_tensorFunction · 0.85
get_sliceFunction · 0.85
_convert_to_layoutFunction · 0.85
productMethod · 0.80
maxMethod · 0.80
appendMethod · 0.45
astypeMethod · 0.45
zerosMethod · 0.45
sumMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…