# \[PyTorch\]\[Layout\] cannot convert F.linear(x, F.linear(y, z))

**URL:** <https://discuss.tvm.apache.org/t/pytorch-layout-cannot-convert-f-linear-x-f-linear-y-z/10866>\
**Category:** Troubleshooting\
**Created:** [August 23, 2021, 4:21pm UTC](https://discuss.tvm.apache.org/t/pytorch-layout-cannot-convert-f-linear-x-f-linear-y-z/10866 "2021-08-23T16:21:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinkun](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jinkun](https://discuss.tvm.apache.org/u/jinkun)\
**Post date:** [August 23, 2021, 4:21pm UTC](https://discuss.tvm.apache.org/t/pytorch-layout-cannot-convert-f-linear-x-f-linear-y-z/10866/1 "2021-08-23T16:21:52Z")

</div>

The following code crashes at compilation

```python
import torch
from torch.nn import functional as F
from tvm import relay
from tvm.ir.transform import PassContext
class A(torch.nn.Module):
    def __init__ (self):
        super(). __init__ ()

    def forward(self, x, y, z):
        return F.linear(x, F.linear(y, z))
model = A()
input_data = tuple(torch.randn(100,100) for _ in range(3))
scripted_model = torch.jit.trace(model, input_data).eval()

shape_list = [('x', (100, 100)), ('y', (100, 100)), ('z', (100, 100))]
mod, params = relay.frontend.from_pytorch(scripted_model, shape_list)
print(mod)
with PassContext(opt_level=3):
    relay.build(mod, target='llvm')

```

The error message: ` Check failed: (tir::BijectiveLayout(new_src_layout, dst_layout).defined()) is false: Cannot insert layout transform because there are inconvertible layouts: NC v.s. NK10n`.

It seems that it’s because F.linear is mapped to `nn.dense`, which assumes a specific layout of its inputs and output. The whole model is converted to something like `nn.dense(x, nn.dense(y, z))` (modulo some transpositions) where the output of `nn.dense` is used as the second input for the other `nn.dense`. The former requries `NC` while the latter requires `NK10n`.

Maybe we should we `nn.matmul` in this case? Or we can also change the layout annotation such that it’s not using letters but the indexes to record the relation between the old and new layouts? (For example, `NC` → `I0_I1`, `NK10n` → `I0_I1_10i0`)

---

<div class="post-metadata">

**Author:** ![masahi](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/masahi/32/74_2.png) [@masahi](https://discuss.tvm.apache.org/u/masahi)\
**Post date:** [August 24, 2021, 1:05am UTC](https://discuss.tvm.apache.org/t/pytorch-layout-cannot-convert-f-linear-x-f-linear-y-z/10866/2 "2021-08-24T01:05:19Z")

</div>

Interesting. Probably there is an implicit assumption somewhere that the rhs of `nn.dense` is a constant. I’ll look into this.

---

<div class="post-metadata">

**Author:** ![masahi](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/masahi/32/74_2.png) [@masahi](https://discuss.tvm.apache.org/u/masahi)\
**Post date:** [September 3, 2021, 10:12am UTC](https://discuss.tvm.apache.org/t/pytorch-layout-cannot-convert-f-linear-x-f-linear-y-z/10866/3 "2021-09-03T10:12:32Z")

</div>

Fixed in [https://github.com/apache/tvm/pull/8921](https://github.com/apache/tvm/pull/8921)
