Adapting a backend for TVM-TIRx

Contents
  1. Understanding TVM-TIRx
  2. Using TVM-TIRx
  3. IR
  4. AST
  5. Adapting a backend

Understanding TVM-TIRx

The developers of TVM-TIRx see agents writing GPU kernels as a growing trend. Agents can write CUDA or PTX directly, but we still need a way to verify the results programmatically.

TVM-TIRx provides a small, extensible compiler infrastructure, including a collection of intermediate representations.

Using TVM-TIRx

See the official quick start.

import torch

import tvm_tirx
from tvm_tirx.script import tirx as T

n = T.dynamic("n")


@T.function
def add_one(A: T.Tensor((n,), T.float32), B: T.Tensor((n,), T.float32)):
    T.device_entry(launch=T.cuda.LaunchConfig(grid=T.ceildiv(n, 256), block=256))
    i = T.cuda.block_idx("x") * 256 + T.cuda.thread_idx("x")
    if i < n:
        B[i] = A[i] + 1

print(tvm_tirx.ir.format_tree(add_one))

Running it produces this output:

Function
├── params[0]: A = Var(ty=TensorType(shape=[n], dtype=float32))
├── params[1]: B = Var(ty=TensorType(shape=[n], dtype=float32))
└── body: SeqStmt
    └── seq[0]: RegionStmt(op=tirx.device_entry)
        ├── attrs: DictAttrs
        │   └── dict: Map
        │       ├── 'cuda.kernel_attrs': {}
        │       ├── 'cuda.launch_fields'[0]: 'grid.x'
        │       ├── 'cuda.launch_fields'[1]: 'grid.y'
        │       ├── 'cuda.launch_fields'[2]: 'grid.z'
        │       ├── 'cuda.launch_fields'[3]: 'block.x'
        │       ├── 'cuda.launch_fields'[4]: 'block.y'
        │       └── 'cuda.launch_fields'[5]: 'block.z'
        ├── args[0]: FloorDiv
        │   ├── a: Sub
        │   │   ├── a: Add
        │   │   │   ├── a: n
        │   │   │   └── b: IntImm(value=256)
        │   │   └── b: IntImm(value=1)
        │   └── b: IntImm(value=256)
        ├── args[1]: IntImm(value=1)
        ├── args[2]: IntImm(value=1)
        ├── args[3]: IntImm(value=256)
        ├── args[4]: IntImm(value=1)
        ├── args[5]: IntImm(value=1)
        ├── body_params: []
        ├── body: SeqStmt
        │   ├── seq[0]: Bind
        │   │   ├── var: i = Var(ty=int32)
        │   │   └── value: Add
        │   │       ├── a: Mul
        │   │       │   ├── a: Call(op=tirx.cuda.block_idx, attrs=None)
        │   │       │   │   ├── args[0]: StringImm(value='x')
        │   │       │   │   └── ty_args: []
        │   │       │   └── b: IntImm(value=256)
        │   │       └── b: Call(op=tirx.cuda.thread_idx, attrs=None)
        │   │           ├── args[0]: StringImm(value='x')
        │   │           └── ty_args: []
        │   └── seq[1]: If(else_case=None)
        │       ├── condition: LT
        │       │   ├── a: i
        │       │   └── b: n
        │       └── then_case: SeqStmt
        │           └── seq[0]: TensorStore(dest=B, indices=[i])
        │               └── value: Add
        │                   ├── a: TensorLoad(source=A, indices=[i])
        │                   └── b: FloatImm(value=1.0)
        └── result_vars: []

The tree looks complicated at first. Even without knowing exactly what each region represents, you can see the structure of the Python code in the tree.

IR

An intermediate representation is a data structure a compiler uses to describe a program. It lets the compiler analyse, optimise and transform the program.

AST

An abstract syntax tree represents the structure of source code. A tree is easier for a compiler to traverse and analyse than a string of source text.

The output above is a tree too. It represents TIRx IR rather than the Python frontend’s AST.

Adapting a backend

In the source, the CUDA backend inherits from CodeGenC. It lowers TIRx IR to C-like code such as CUDA, which is then compiled by the vendor’s compiler.