# \[VTA\] Transforms for Code Generation: When are they called?

**URL:** <https://discuss.tvm.apache.org/t/vta-transforms-for-code-generation-when-are-they-called/14968>\
**Category:** Questions\
**Created:** [May 19, 2023, 2:46pm UTC](https://discuss.tvm.apache.org/t/vta-transforms-for-code-generation-when-are-they-called/14968 "2023-05-19T14:46:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Necrotos](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Necrotos](https://discuss.tvm.apache.org/u/Necrotos)\
**Post date:** [May 19, 2023, 2:46pm UTC](https://discuss.tvm.apache.org/t/vta-transforms-for-code-generation-when-are-they-called/14968/1 "2023-05-19T14:46:06Z")

</div>

Hello, I am trying to wrap my head around the VTA Software implementation. I found the transform.py file and want to ask how these are called? As an example, let’s look at [InjectDMAIntrin](https://github.com/apache/tvm/blob/main/vta/python/vta/transform.py#L370). There must be some way to identify the part of the TIR where this operation is to be inserted. But how is that done?

In general, I want to understand how to incorporate custom accelerators in TVM. So far I understand the pattern match and replace part, and now I want to take the next step and understand how the code generation works.

---

<div class="post-metadata">

**Author:** ![slai-nick](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@slai-nick](https://discuss.tvm.apache.org/u/slai-nick)\
**Post date:** [May 22, 2023, 6:42am UTC](https://discuss.tvm.apache.org/t/vta-transforms-for-code-generation-when-are-they-called/14968/2 "2023-05-22T06:42:09Z")

</div>

I am not sure to get the initial question well but I can share some resources for code generation. Just about me, I am relatively new to TVM and compilation, I’ve managed to get a dummy codegen running last week under the unity branch.

> [@Necrotos](#):
>
> In general, I want to understand how to incorporate custom accelerators in TVM. So far I understand the pattern match and replace part, and now I want to take the next step and understand how the code generation works.

You should check [BYOC (bring your own codegen)](https://tvm.apache.org/docs/dev/how_to/relay_bring_your_own_codegen.html).

Once you’ll know how to write a codegen you’ll need to know how to get it called. There is a new branch of TVM in active development which makes calling into BYOC much more natural and flexible among other things, it’s called [unity](https://discuss.tvm.apache.org/t/establish-tvm-unity-connection-a-technical-strategy/13344). If that interests you, there is a [notebook](https://github.com/sunggg/tvm/blob/relax_byoc_tutorial/apps/unity_tutorials/unity_byoc.ipynb) showcasing how to you can get your BYOC codegen running under unity.

For calling your codegen without unity you may find what you need in this [BYOC blogpost](https://tvm.apache.org/2020/07/15/how-to-bring-your-own-codegen-to-tvm).

_Note: The BYOC documentation and blogpost might not be fully up to date, I suggest using existing implementations as a complement to validate. For unity it’s in [src/relax/backend/contrib](https://github.com/apache/tvm/tree/unity/src/relax/backend/contrib) (see the cutlass implementation for a C codegen and the tensorrt one for a json codegen) and for pre-unity in [src/relay/backend/contrib](https://github.com/apache/tvm/tree/main/src/relay/backend/contrib)._

---

<div class="post-metadata">

**Author:** ![Necrotos](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Necrotos](https://discuss.tvm.apache.org/u/Necrotos)\
**Post date:** [May 23, 2023, 8:50am UTC](https://discuss.tvm.apache.org/t/vta-transforms-for-code-generation-when-are-they-called/14968/3 "2023-05-23T08:50:19Z")

</div>

Thank you! I am aware of the blogs, but none of them seem to describe the way it is done for VTA. I have found something on the conv2d specifically, but nothing so far that explains the contents of the transforms.py file.

Although, it might be best to ignore the VTA implementation and just do it on the Unity branch. But I still want to know how these Inject functions are called.

---

<div class="post-metadata">

**Author:** ![slai-nick](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@slai-nick](https://discuss.tvm.apache.org/u/slai-nick)\
**Post date:** [May 23, 2023, 1:44pm UTC](https://discuss.tvm.apache.org/t/vta-transforms-for-code-generation-when-are-they-called/14968/4 "2023-05-23T13:44:33Z")

</div>

Sorry I can’t help more

---

<div class="post-metadata">

**Author:** ![Necrotos](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Necrotos](https://discuss.tvm.apache.org/u/Necrotos)\
**Post date:** [May 25, 2023, 7:43pm UTC](https://discuss.tvm.apache.org/t/vta-transforms-for-code-generation-when-are-they-called/14968/5 "2023-05-25T19:43:34Z")

</div>

I spent some more time looking into this. On Unity, at least the pattern matching and code gen seem to be much better integrated, so that’s already nice. The thing that interests me probably happens only after the tutorial, as I want to add a completely new target to TVM. Probably that’s also where the [transform.py](https://github.com/apache/tvm/blob/unity/vta/python/vta/transform.py) file comes into play, as you have to tell TVM when and where to insert certain function calls.

But I still can’t find any documentation of that last part. In any case, thanks for the hint!

---

<div class="post-metadata">

**Author:** ![Necrotos](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Necrotos](https://discuss.tvm.apache.org/u/Necrotos)\
**Post date:** [May 26, 2023, 9:33am UTC](https://discuss.tvm.apache.org/t/vta-transforms-for-code-generation-when-are-they-called/14968/6 "2023-05-26T09:33:08Z")

</div>

Ok, I figured it out. If someone comes across this in the future:

The operations performed in the mentioned file are inserted using the [InjectCopyIntrin](https://tvm.apache.org/docs/reference/api/python/tir.html#tvm.tir.transform.InjectCopyIntrin) pass. It checks TIR for pragmas and inserts the corresponding function call when generating code. E.g. the [InjectDMAIntrin](https://github.com/apache/tvm/blob/main/vta/python/vta/transform.py#L370) inserts DMA access in all locations where the “dma\_copy” pragma is set (see the matching call of InjectCopyIntrin).

The pragmas are set during schdule construction, for examples take a look at the way it is done in [vta\_conv2d.py](https://github.com/apache/tvm/blob/main/vta/python/vta/top/vta_conv2d.py) or the other operators defined in the same folder.
