How to add a relay pass

From the tvm docs, there are two ways to implement a pass: Using Python Decorator or Use C++. I found most relay passes are implemented by C++, such as fold_constant, etc,. Only one pass named ChangeBatch(https://github.com/apache/tvm/blob/main/python/tvm/relay/transform/transform.py#L1062) is impled using Python Decorator.

So if I want to add a custom relay pass, is it better to implemented it by C++?

both implementions is ok. in most condition, writing python pass is enough.

But there are few examples of passes writing by python. Only one pass named ChangeBatch is writen by python. So it may be hard for pass beginers.

BTY, if I want to insert a op to the relay graph ir, I should use module-level pass?

Hi, @zhaoyang-star.

I believe that is because Python interface is more recommended for the quick tryout, rather than the formal integration. If you are planning to push your pass to the main branch, I think C++ implementation is generally recommended.

You can either use a module-level pass or a relay function-level pass. But since a function-level pass applies transformation for each relay function independently, you would need to use a module-level pass if you want to make change across functions.

Thanks for your kind reply.