Skip to content

[Frontend][Pytorch] Add axis N when maxpool3d layout is (C,D,H,W)#12467

Merged
masahi merged 2 commits intoapache:mainfrom
blackkker:fix_max_pool_3d
Aug 18, 2022
Merged

[Frontend][Pytorch] Add axis N when maxpool3d layout is (C,D,H,W)#12467
masahi merged 2 commits intoapache:mainfrom
blackkker:fix_max_pool_3d

Conversation

@blackkker
Copy link
Contributor

@blackkker blackkker commented Aug 17, 2022

According to the pytorch doc torch.nn.MaxPool3d. The shape of input can be (N,C,D,H,W) or (C,D,H,W). While the MaxPool3DAttrs thinks the dimension should be 5. When importing a model contains MaxPool3d with shape (C,D,H,W)`,

TVM_ATTR_FIELD(layout).set_default("NCDHW").describe(
"Dimension ordering of input data. Can be 'NCDHW', 'NDHWC', etc."
"'N', 'C', 'D', 'H', 'W' stands for batch, channel, depth, height, and width"
"dimensions respectively. Pooling is applied on the 'D', 'H' and"
"'W' dimensions.");
TVM_ATTR_FIELD(out_layout)
.set_default("")
.describe(
"Dimension ordering of output data. Can be 'NCDHW', 'NDHWC', etc."
"'N', 'C', 'D', 'H', 'W' stands for batch, channel, depth, height, and width"
"dimensions respectively. Pooling is applied on the 'D', 'H' and"
"'W' dimensions.");

which could leads to out of bound error Check failed: (0 <= i && i < p->size_) is false: IndexError: indexing 4 on an array of size 4 when ii == 4 and dshape == 4.

tvm/src/relay/op/nn/pooling.cc

Lines 1211 to 1216 in bcc7cde

if (dshape[ii].as<tir::AnyNode>()) {
oshape[ii] = dshape[ii];
} else {
oshape[ii] =
calculate_pool_dimension(dshape[ii], pad[i], param->pool_size[i], param->dilation[i],
param->strides[i], param->ceil_mode);

Therefore, I simply add an expand_dims op on the frontend to avoid this situation.

@blackkker
Copy link
Contributor Author

cc @masahi

@masahi masahi merged commit f64a3bd into apache:main Aug 18, 2022
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 25, 2022
…ache#12467)

* Add axis N if input is (C,D,H,W) layout.

* Add (C,D,H,W) test case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants