tvm_tirx.tirx.transform.BF16ComputeLegalize

tvm_tirx.tirx.transform.BF16ComputeLegalize#

tvm_tirx.tirx.transform.BF16ComputeLegalize()#

Promote unsupported BF16 computations and internal temporaries to float32.

Inserts conversions at BF16 boundaries. Leaves functions unchanged when their CUDA target reports native BF16 support.

Returns:

fpass (tvm_tirx.ir.transform.Pass) – The result pass

Examples

import tvm_tirx as tvm
from tvm_tirx.script import ir as I, tirx as T
from tvm_tirx.tirx import transform as tr

@I.ir_module
class Before:
    @T.function(private=True)
    def main():
        A = T.alloc_tensor(ty_args=[T.Tensor((1,), "bfloat16")])
        A[0] = T.bfloat16(1)
        T.evaluate(A[0] + T.bfloat16(2))

@I.ir_module
class Expected:
    @T.function(private=True)
    def main():
        A = T.alloc_tensor(ty_args=[T.Tensor((1,), T.float32)])
        A[0] = T.float32(1.0)
        T.evaluate(A[0] + T.float32(2.0))

def run_pass():
    return tr.BF16ComputeLegalize()(Before)

tvm.ir.assert_structural_equal(run_pass(), Expected)