Helper function to convert the input to Expr, which follows the rules: 1. Return the input itself if it's already a `relax.Expr`; 2. Return `relax.PrimValue` if the input is a `PrimExpr`; 3. Return `relax.StringImm` if the input is `tvm.String` or `str`; 4. Return `relax.Tuple` if th
(value: Any)
| 84 | |
| 85 | |
| 86 | def convert_to_expr(value: Any) -> Expr: |
| 87 | """Helper function to convert the input to Expr, which follows the rules: |
| 88 | 1. Return the input itself if it's already a `relax.Expr`; |
| 89 | 2. Return `relax.PrimValue` if the input is a `PrimExpr`; |
| 90 | 3. Return `relax.StringImm` if the input is `tvm.String` or `str`; |
| 91 | 4. Return `relax.Tuple` if the input is a tuple/list of `Expr`. |
| 92 | |
| 93 | Notes |
| 94 | ----- |
| 95 | 1. `tvm.tirx.StringImm` is not allowed because of ambiguity, |
| 96 | which can be either `relax.StringImm` or `relax.PrimValue`. |
| 97 | """ |
| 98 | if isinstance(value, int): |
| 99 | return PrimValue(tirx.IntImm("int64", value)) |
| 100 | |
| 101 | if isinstance(value, float): |
| 102 | return PrimValue(tirx.FloatImm("float64", value)) |
| 103 | |
| 104 | tvm_value = tvm_ffi.convert(value) |
| 105 | # Case 1 |
| 106 | if isinstance(tvm_value, Expr): # type: ignore |
| 107 | return tvm_value |
| 108 | # Note`` 1 |
| 109 | if isinstance(tvm_value, tirx.StringImm): |
| 110 | raise TypeError( |
| 111 | "Cannot convert `tirx.StringImm` to `relax.Expr` because of ambiguity," |
| 112 | "which can be either `relax.StringImm` or `relax.PrimValue` " |
| 113 | ) |
| 114 | # Case 2 |
| 115 | if isinstance(tvm_value, PrimExpr): |
| 116 | return PrimValue(value) |
| 117 | # Case 3 |
| 118 | if isinstance(tvm_value, str): |
| 119 | return StringImm(value) |
| 120 | # Case 4 |
| 121 | if isinstance(value, tuple | list): |
| 122 | # `convert_to_expr` ensures that all elements are `Expr` if no exception raises |
| 123 | return rx_Tuple([convert_to_expr(v) for v in value]) |
| 124 | raise TypeError(f"Cannot convert {value} with type {type(value)} to `relax.Expr`") |
| 125 | |
| 126 | |
| 127 | def copy_with_new_vars(func: Function) -> Function: |
no test coverage detected
searching dependent graphs…