MCPcopy Create free account
hub / github.com/apache/tvm / convert_to_expr

Function convert_to_expr

python/tvm/relax/utils.py:86–124  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

84
85
86def 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
127def copy_with_new_vars(func: Function) -> Function:

Callers 15

bind_assign_valueFunction · 0.90
visit_returnFunction · 0.90
relax_callFunction · 0.90
call_packedFunction · 0.90
call_py_funcFunction · 0.90
call_tirFunction · 0.90
_wrap_inline_arg_tupleFunction · 0.85
call_inplace_packedFunction · 0.85
call_pure_packedFunction · 0.85
clipFunction · 0.85
strided_sliceFunction · 0.85
call_tir_local_viewFunction · 0.85

Calls 3

PrimValueClass · 0.85
StringImmClass · 0.70
convertMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…