def gen_function_single_thread(
    fused_func_metadata,
    input_names,
    output_names,
    type_converter,
) -> str:
    """Per thread elementwise function codegen."""
    tensor_to_expr: Dict[Tensor, str] = {}
    float32_t = fused_func_metadata.float32_t
    body = ""

    for tensor, name in zip(fused_func_metadata.original_inputs, input_names):
        if fused_func_metadata.use_fp32_acc and fused_func_metadata.op_t != float32_t:
            input_converter = type_converter.get(fused_func_metadata.op_t).get(
                float32_t
            )
            name = "{}({})".format(input_converter, name)
        tensor_to_expr[tensor] = name

    tmp_output_idx: int = 0
    for func_metadata in fused_func_metadata.sub_funcs:
        params: List[str] = []
        input_converter = None
        output_converter = None
        func_op_t = func_metadata.op_t

        # intermediate input / output converters are not
        # required when doing all computation in float32
        if not fused_func_metadata.use_fp32_acc:
            if func_op_t != fused_func_metadata.op_t:
                input_converter = type_converter.get(fused_func_metadata.op_t).get(
                    func_op_t
                )
                output_converter = type_converter.get(func_op_t).get(
                    fused_func_metadata.op_t
                )
                assert (
                    input_converter is not None
                ), "Unsupported convertion from {} to {}".format(
                    fused_func_metadata.op_t, func_op_t
                )
                assert (
                    output_converter is not None
                ), "Unsupported convertion from {} to {}".format(
                    func_op_t, fused_func_metadata.op_t
                )

        for arg in func_metadata.args:
            if arg in tensor_to_expr:
                param = tensor_to_expr[arg]
                params.append(
                    "{}({})".format(input_converter, param)
                    if input_converter is not None
                    else param
                )
            elif arg.is_a_const_num():
                arg_str = ""
                if math.isinf(arg._attrs["value"]):
                    arg_str = "CUDART_INF_F"
                else:
                    arg_str = str(arg._attrs["value"])
                if func_op_t[-1] == "2":
                    params.append(
                        "{}({},{})".format(
                            func_op_t,
                            arg_str,
                            arg_str,
                        )
                    )
                else:
                    params.append("{}({})".format(func_op_t, arg_str))
            else:
                raise RuntimeError(
                    "Cannot generate expression for node {}, ops: {}".format(
                        arg, func_metadata
                    )
                )
        assert (
            len(func_metadata.outputs) == 1
        ), "Operator has more than 1 output! Operator: {}".format(func_metadata)

        output = func_metadata.outputs[0]
        func_def = "{}({})".format(func_metadata.func_name, ",".join(params))
        func_def = (
            "{}({})".format(output_converter, func_def)
            if output_converter is not None
            else func_def
        )
        if len(output._attrs["dst_ops"]) > 1:
            name = "tmp_" + (str)(tmp_output_idx)
            tmp_output_idx += 1
            temp_t = (
                float32_t
                if fused_func_metadata.use_fp32_acc
                else fused_func_metadata.op_t
            )
            body += "{} {} = {};\n".format(temp_t, name, func_def)
            tensor_to_expr[output] = name
        else:
            tensor_to_expr[output] = func_def

    for tensor, name in zip(fused_func_metadata.original_outputs, output_names):
        if tensor not in tensor_to_expr:
            raise RuntimeError(
                "Cannot generate expression for node {}, outputs: {}".format(
                    tensor, fused_func_metadata.original_outputs
                )
            )
        expr = tensor_to_expr[tensor]
        if fused_func_metadata.use_fp32_acc and fused_func_metadata.op_t != float32_t:
            output_converter = type_converter.get(float32_t).get(
                fused_func_metadata.op_t
            )
            expr = "{}({})".format(output_converter, expr)
        body += "{} = {};\n".format(name, expr)

    return body
Per thread elementwise function codegen.
python/aitemplate/backend/common/elementwise_common.py