Skip to content
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
14 changes: 12 additions & 2 deletions include/tvm/topi/nn/pooling.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,18 @@ inline Tensor pool_impl_nd(const Tensor& x, const ffi::Array<PrimExpr>& kernel_s

PrimExpr numerator =
data_shape[ii] - (kernel[i] - 1) * dilation[i] - 1 + pad_head[i] + pad_tail[i];
auto out_dim = analyzer.Simplify(indexdiv(numerator, stride[i]) + 1);
out_shape.Set(ii, out_dim);
auto raw_out = indexdiv(numerator, stride[i]) + 1;
if (ceil_mode) {
// In the case of ceil_mode=True, we need to check if the last pooling window is valid.
// If not, we skip the last window as it would start in the bottom padded region,
// we need to minus 1 to get the correct output shape.
auto invalid_last = (raw_out - 1) * stride[i] >= data_shape[ii] + pad_head[i];
auto out_dim = analyzer.Simplify(if_then_else(invalid_last, raw_out - 1, raw_out));
out_shape.Set(ii, out_dim);
} else {
auto out_dim = analyzer.Simplify(raw_out);
out_shape.Set(ii, out_dim);
}
}

ffi::Map<ffi::String, ffi::Any> attrs;
Expand Down
39 changes: 33 additions & 6 deletions src/relax/op/nn/pooling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ StructInfo InferStructInfoPool1D(const Call& call, const BlockBuilder& ctx) {
if (attrs->ceil_mode) {
numerator_w += attrs->strides[0] - 1;
}
out_NCW_shape[2] = analyzer->Simplify(floordiv(numerator_w, attrs->strides[0]) + 1);
PrimExpr raw_out_w = floordiv(numerator_w, attrs->strides[0]) + 1;
if (attrs->ceil_mode) {
PrimExpr invalid_last_w = (raw_out_w - 1) * attrs->strides[0] >= input_w + attrs->padding[0];
out_NCW_shape[2] = analyzer->Simplify(if_then_else(invalid_last_w, raw_out_w - 1, raw_out_w));
} else {
out_NCW_shape[2] = analyzer->Simplify(raw_out_w);
}
Comment on lines +115 to +120

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic to handle ceil_mode is duplicated across InferStructInfoPool1D, InferStructInfoPool2D, and InferStructInfoPool3D. To improve code maintainability and reduce redundancy, consider extracting this logic into a common helper function. For example:

// Placed within the relax namespace
inline PrimExpr InferPoolOutputSize(arith::Analyzer* analyzer, PrimExpr input_size,
                                    PrimExpr raw_out_size, PrimExpr stride,
                                    PrimExpr padding_before, bool ceil_mode) {
  if (ceil_mode) {
    PrimExpr invalid_last = (raw_out_size - 1) * stride >= input_size + padding_before;
    return analyzer->Simplify(if_then_else(invalid_last, raw_out_size - 1, raw_out_size));
  } else {
    return analyzer->Simplify(raw_out_size);
  }
}

Then, this block and similar blocks in InferStructInfoPool2D and InferStructInfoPool3D can be simplified to a single call, for instance:

out_NCW_shape[2] = InferPoolOutputSize(analyzer, input_w, raw_out_w, attrs->strides[0], attrs->padding[0], attrs->ceil_mode);


ffi::Array<PrimExpr> out_shape = out2NCW.BackwardShape(out_NCW_shape);
return TensorStructInfo(ShapeExpr(out_shape), data_sinfo->dtype, data_sinfo->vdevice);
Expand Down Expand Up @@ -232,8 +238,17 @@ StructInfo InferStructInfoPool2D(const Call& call, const BlockBuilder& ctx) {
numerator_h += attrs->strides[0] - 1;
numerator_w += attrs->strides[1] - 1;
}
out_NCHW_shape[2] = analyzer->Simplify(floordiv(numerator_h, attrs->strides[0]) + 1);
out_NCHW_shape[3] = analyzer->Simplify(floordiv(numerator_w, attrs->strides[1]) + 1);
PrimExpr raw_out_h = floordiv(numerator_h, attrs->strides[0]) + 1;
PrimExpr raw_out_w = floordiv(numerator_w, attrs->strides[1]) + 1;
if (attrs->ceil_mode) {
PrimExpr invalid_last_h = (raw_out_h - 1) * attrs->strides[0] >= input_h + attrs->padding[0];
PrimExpr invalid_last_w = (raw_out_w - 1) * attrs->strides[1] >= input_w + attrs->padding[1];
out_NCHW_shape[2] = analyzer->Simplify(if_then_else(invalid_last_h, raw_out_h - 1, raw_out_h));
out_NCHW_shape[3] = analyzer->Simplify(if_then_else(invalid_last_w, raw_out_w - 1, raw_out_w));
} else {
out_NCHW_shape[2] = analyzer->Simplify(raw_out_h);
out_NCHW_shape[3] = analyzer->Simplify(raw_out_w);
}

ffi::Array<PrimExpr> out_shape = out2NCHW.BackwardShape(out_NCHW_shape);
return TensorStructInfo(ShapeExpr(out_shape), data_sinfo->dtype, data_sinfo->vdevice);
Expand Down Expand Up @@ -380,9 +395,21 @@ StructInfo InferStructInfoPool3D(const Call& call, const BlockBuilder& ctx) {
numerator_h += attrs->strides[1] - 1;
numerator_w += attrs->strides[2] - 1;
}
out_NCDHW_shape[2] = analyzer->Simplify(floordiv(numerator_d, attrs->strides[0]) + 1);
out_NCDHW_shape[3] = analyzer->Simplify(floordiv(numerator_h, attrs->strides[1]) + 1);
out_NCDHW_shape[4] = analyzer->Simplify(floordiv(numerator_w, attrs->strides[2]) + 1);
PrimExpr raw_out_d = floordiv(numerator_d, attrs->strides[0]) + 1;
PrimExpr raw_out_h = floordiv(numerator_h, attrs->strides[1]) + 1;
PrimExpr raw_out_w = floordiv(numerator_w, attrs->strides[2]) + 1;
if (attrs->ceil_mode) {
PrimExpr invalid_last_d = (raw_out_d - 1) * attrs->strides[0] >= input_d + attrs->padding[0];
PrimExpr invalid_last_h = (raw_out_h - 1) * attrs->strides[1] >= input_h + attrs->padding[1];
PrimExpr invalid_last_w = (raw_out_w - 1) * attrs->strides[2] >= input_w + attrs->padding[2];
out_NCDHW_shape[2] = analyzer->Simplify(if_then_else(invalid_last_d, raw_out_d - 1, raw_out_d));
out_NCDHW_shape[3] = analyzer->Simplify(if_then_else(invalid_last_h, raw_out_h - 1, raw_out_h));
out_NCDHW_shape[4] = analyzer->Simplify(if_then_else(invalid_last_w, raw_out_w - 1, raw_out_w));
} else {
out_NCDHW_shape[2] = analyzer->Simplify(raw_out_d);
out_NCDHW_shape[3] = analyzer->Simplify(raw_out_h);
out_NCDHW_shape[4] = analyzer->Simplify(raw_out_w);
}

ffi::Array<PrimExpr> out_shape = out2NCDHW.BackwardShape(out_NCDHW_shape);
return TensorStructInfo(ShapeExpr(out_shape), data_sinfo->dtype, data_sinfo->vdevice);
Expand Down