Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def rewriter(expr, matches):
if conv_attrs["kernel_layout"] == "OIHW":
wt = relax.op.reshape(wt, shape=(bn_weight.ty.shape[0], 1, 1, 1))
elif conv_attrs["kernel_layout"] == "IOHW":
wt = wt.reshape(1, bn_weight.ty.shape[0], 1, 1)
wt = relax.op.reshape(wt, shape=(1, bn_weight.ty.shape[0], 1, 1))
else:
return expr
wt_conv = relax.op.multiply(conv_weight, wt)
Expand Down
35 changes: 35 additions & 0 deletions tests/python/relax/test_transform_fold_batch_norm_to_conv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,40 @@ def test_fold_batchnorm_info_conv2d_transform():
VerifyFolding().visit(mod)


def test_fold_batchnorm_iohw_kernel_layout():
@I.ir_module
class Module:
@R.function
def main(
x: R.Tensor((1, 1, 2, 2), "float32"),
weight: R.Tensor((1, 2, 1, 1), "float32"),
gamma: R.Tensor((2,), "float32"),
beta: R.Tensor((2,), "float32"),
mean: R.Tensor((2,), "float32"),
variance: R.Tensor((2,), "float32"),
):
conv = R.nn.conv2d(
x,
weight,
data_layout="NCHW",
kernel_layout="IOHW",
out_layout="NCHW",
out_dtype="float32",
)
return R.nn.batch_norm(conv, gamma, beta, mean, variance, axis=1, training=False)[0]

params = {
"weight": tvm.runtime.tensor(np.array([[[[1.5]], [[-2.0]]]], "float32")),
"gamma": tvm.runtime.tensor(np.array([2.0, 3.0], "float32")),
"beta": tvm.runtime.tensor(np.array([5.0, 7.0], "float32")),
"mean": tvm.runtime.tensor(np.array([1.0, -1.0], "float32")),
"variance": tvm.runtime.tensor(np.array([4.0, 9.0], "float32")),
}
source = relax.transform.BindParams("main", params)(Module)
folded = relax.transform.FoldBatchnormToConv2D()(source)
assert relax.analysis.check_well_formed(folded, check_ty=True)
VerifyFolding().visit(folded)


if __name__ == "__main__":
tvm.testing.main()
Loading