# Adding new kernel layout to backend

**URL:** <https://discuss.tvm.apache.org/t/adding-new-kernel-layout-to-backend/8824>\
**Category:** Development\
**Created:** [January 10, 2021, 4:14pm UTC](https://discuss.tvm.apache.org/t/adding-new-kernel-layout-to-backend/8824 "2021-01-10T16:14:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wheest](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/wheest/32/607_2.png) [@Wheest](https://discuss.tvm.apache.org/u/Wheest)\
**Post date:** [January 10, 2021, 4:14pm UTC](https://discuss.tvm.apache.org/t/adding-new-kernel-layout-to-backend/8824/1 "2021-01-10T16:14:45Z")

</div>

For the pull request [#6137](https://github.com/apache/tvm/pull/6137) (Better grouped convolution for CPU targets), I’ve been asked to add an `alter_op` so that the kernel is reshaped ahead of time.

It took me some time to figure out how to do this, but I’ve now figured out what is required to do it on the TOPI side. However, I’m having an issue with the C++ backend.

The reason for this is that my kernel reshape is not `NCHW` to “OIHW%di%do” % (ic\_bn, oc\_bn)`, but to `“GOIHW%di%do” % (ic\_bn, oc\_bn)`, where `G` is the number of groups. Therefore, when I try to have this layout for alter op, I get the following error:

```auto
An internal invariant was violated during the execution of TVM.
Please read TVM's error reporting guidelines.
More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.
---------------------------------------------------------------
  Check failed: false == false: [15:58:23] ../src/tir/ir/data_layout.cc:364:
---------------------------------------------------------------
An internal invariant was violated during the execution of TVM.
Please read TVM's error reporting guidelines.
More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.
---------------------------------------------------------------
  Check failed: GetStoreRule(&n->backward_rule, n->dst_layout, n->src_layout) == false:

```

I think this is happening because there is no `"GOIHW%di%do" % (ic_bn, oc_bn)` layout in the C++ backend, and I guess `conv2d_alter_op` does the reshape in that backend. The condition that fails in this function is:

```auto
if (tir::is_zero(store)) {
      // Not convertible
      return false;
}

```

Just using `"OIHW%di%do" % (ic_bn, oc_bn)` in my conv2d\_alter\_op doesn’t work, because it means that my TOPI function gets a 6D kernel, rather than a 7D kernel.

So I’m wondering _how I would add this new kernel layout to this process_? Ideally reusing the rules of the reshape in TOPI ([see here](https://github.com/apache/tvm/pull/6137/files#diff-1c244579b0854030f65e2eaa0918dc133b6bbc4f371ee97a3772d0fe55b14ca2R196))

You can see my [working branch here](https://github.com/Wheest/incubator-tvm/tree/wheest-public/gspc-dev-alter-op-2), where the issue happens. [This test script](https://github.com/Wheest/tvm-grouped-conv-test) generates group convolution networks and runs them on x86.

---

<div class="post-metadata">

**Author:** ![jinchenglee](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/jinchenglee/32/1519_2.png) [@jinchenglee](https://discuss.tvm.apache.org/u/jinchenglee)\
**Post date:** [March 31, 2021, 12:44am UTC](https://discuss.tvm.apache.org/t/adding-new-kernel-layout-to-backend/8824/2 "2021-03-31T00:44:57Z")

</div>

@Wheest , can you elaborate what are the files to modify to add new kernel layout? I hit similar issue that requires pre-process kernel layout in compilation time. Thanks.

---

<div class="post-metadata">

**Author:** ![Wheest](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/wheest/32/607_2.png) [@Wheest](https://discuss.tvm.apache.org/u/Wheest)\
**Post date:** [May 10, 2021, 4:06pm UTC](https://discuss.tvm.apache.org/t/adding-new-kernel-layout-to-backend/8824/3 "2021-05-10T16:06:53Z")

</div>

Hi @jinchenglee, unfortunately this was not an issue I was able to solve, and had to merge the PR without this feature for now. Still looking into it.

[@merrymercy](https://github.com/merrymercy) and [@minminsun](https://github.com/minminsun) may have been looking into [something similar](https://github.com/apache/tvm/pull/6137#discussion_r563686468).
