You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by Jiawei Liu via Apache TVM Discuss <no...@discuss.tvm.ai> on 2021/08/16 02:35:59 UTC

[Apache TVM Discuss] [Questions] How to efficiently copy an IR


Hi all, I wonder how to copy an IR in tvm:

What I tried:

1. `deepcopy`: not working. will produce error for some IR.
2. `astext` + `fromtext`: will work but kinda slow (2s for big models).

Thanks!





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-efficiently-copy-an-ir/10798/1) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/198d43d0d84a027527875d6dd04612d7af18138631904f202b78339f369dc1db).

[Apache TVM Discuss] [Questions] How to efficiently copy an IR

Posted by Jiawei Liu via Apache TVM Discuss <no...@discuss.tvm.ai>.

@junrushao1994 Thank you Junru! It did become faster!

![image|690x98](upload://47qAvirVT5ML7lD4zlOheGXxLA8.png) 

Another related question is: will passes alter the original module (i mean if `mod` will be modified after applying `new_mod = pass(mod)`). It seems this is the case and I have to do the copy thing manually if I want to maintain the old module (I thought it would be COW or something else).

```python
# we have a variable `mod` which is a module

old_mod_str = mod.astext()

_ = ToBasicBlockNormalForm()(mod)

assert old_mod_str == mod.astext() # will fail
```

To reproduce:

```python
from tvm import relay
import tvm
from tvm.relay import testing, transform
import hashlib

def example(batch_dim=1):
    out_channels = 32

    data = relay.var("data", relay.TensorType((batch_dim, 3, 224, 224), "float32"))
    weight = relay.var("weight")

    simple_net = relay.nn.conv2d(
        data=data, weight=weight, kernel_size=(5, 5), channels=out_channels, padding=(1, 1)
    )
    simple_net = relay.Function(relay.analysis.free_vars(simple_net), simple_net)

    return testing.create_workload(simple_net)


def md5(data):
    return hashlib.md5(data).hexdigest()

mod, params = example()

print(f'before apply pass, md5 of mod: {md5(mod.astext().encode())}')
print(mod.astext())
with tvm.transform.PassContext(opt_level=4):
    with tvm.target.Target('llvm'):
        seq = tvm.transform.Sequential(
            passes=[transform.ToBasicBlockNormalForm()],
            opt_level=4
        )
        new_mod = seq(mod)

print(f'after apply pass, md5 of mod: {md5(mod.astext().encode())}')
print(f'after apply pass, md5 of new mod: {md5(new_mod.astext().encode())}')

print(mod.astext())
```

```shell
before apply pass, md5 of mod: 383f47fec6c1b1ad607b5e66671602f0
#[version = "0.0.5"]
def @main(%data: Tensor[(1, 3, 224, 224), float32], %weight: Tensor[(32, 3, 5, 5), float32]) -> Tensor[(1, 32, 222, 222), float32] {
  nn.conv2d(%data, %weight, padding=[1, 1, 1, 1], channels=32, kernel_size=[5, 5]) /* ty=Tensor[(1, 32, 222, 222), float32] */
}

after apply pass, md5 of mod: 207a065e002a2e9dcc3873ff51059394
after apply pass, md5 of new mod: 207a065e002a2e9dcc3873ff51059394
#[version = "0.0.5"]
def @main(%data: Tensor[(1, 3, 224, 224), float32], %weight: Tensor[(32, 3, 5, 5), float32]) -> Tensor[(1, 32, 222, 222), float32] {
  nn.conv2d(%data, %weight, padding=[1, 1, 1, 1], channels=32, kernel_size=[5, 5])
}
```





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-efficiently-copy-an-ir/10798/3) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/30c27e6467651a89493ff8b0642f1f1e6fbfe5aef4eb225a4f083bd43cf9da6e).

[Apache TVM Discuss] [Questions] How to efficiently copy an IR

Posted by Junru Shao via Apache TVM Discuss <no...@discuss.tvm.ai>.

In terms of deep copy, the most efficient way is SaveJSON and LoadJSON >_<





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-efficiently-copy-an-ir/10798/2) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/8b8c22cea3a7b2c86e783645cd4d33d1fd65b4c554d4e6c01bc37c75c046dac2).

[Apache TVM Discuss] [Questions] How to efficiently copy an IR

Posted by Junru Shao via Apache TVM Discuss <no...@discuss.tvm.ai>.

IIRC we made some immutability assumptions here that the passes won't modify the original `IRModule`. We did find some bugs in the codebase previously the module is incorrectly modified though :-(





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-efficiently-copy-an-ir/10798/5) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/0aa6af2c7ee510f0e5d38fbd4c7fc7f7cde4360beb41e9ee5411d797fbfd9728).

[Apache TVM Discuss] [Questions] How to efficiently copy an IR

Posted by Jiawei Liu via Apache TVM Discuss <no...@discuss.tvm.ai>.

This case seems to occur when we apply the basic block form pass. Other passes, e.g., `FuseOps` will not result in such results. So I am wondering if it is expected to modify the input argument (old module) by some passes.





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-efficiently-copy-an-ir/10798/4) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/ba7087effbb249be23a6c8c633fbefd89642312fa0281a977fb207996b415cc6).