Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions python/tvm/relay/expr_functor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""The expression functor of Relay."""
from tvm.ir import Op

from .function import Function
from .function import Function, FunctionWithFields
from .expr import Call, Let, Var, GlobalVar
from .expr import If, Tuple, TupleGetItem, Constant
from .expr import RefCreate, RefRead, RefWrite
Expand Down Expand Up @@ -204,7 +204,11 @@ class ExprMutator(ExprFunctor):
def visit_function(self, fn):
new_params = [self.visit(x) for x in fn.params]
new_body = self.visit(fn.body)
return Function(list(new_params), new_body, fn.ret_type, fn.type_params, fn.attrs)
return FunctionWithFields(
fn,
list(new_params),
new_body,
)

def visit_let(self, let):
new_var = self.visit(let.var)
Expand Down
21 changes: 21 additions & 0 deletions python/tvm/relay/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,24 @@ def __call__(self, *args):
Arguments.
"""
return Call(self, args, None, None)


@tvm._ffi.register_func("relay.FunctionWithFields")
def FunctionWithFields(
function,
params=None,
body=None,
ret_type=None,
ty_params=None,
attrs=None,
virtual_device=None,
span=None,
):
"""
Returns function with the given properties. A None property denotes 'no change'.
Returns function if all properties are unchanged. Otherwise, returns a copy with the new
fields.
"""
return _ffi_api.FunctionWithFields(
function, params, body, ret_type, ty_params, attrs, virtual_device, span
)
8 changes: 8 additions & 0 deletions src/relay/ir/function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ TVM_REGISTER_GLOBAL("relay.ir.Function")
tvm::Array<TypeVar> ty_params, tvm::DictAttrs attrs) {
return Function(params, body, ret_type, ty_params, attrs);
});
TVM_REGISTER_GLOBAL("relay.ir.FunctionWithFields")
.set_body_typed([](Function function, Optional<Array<Var>> opt_params, Optional<Expr> opt_body,
Optional<Type> opt_ret_type, Optional<Array<TypeVar>> opt_ty_params,
Optional<DictAttrs> opt_attrs, Optional<VirtualDevice> opt_virtual_device,
Optional<Span> opt_span) {
return WithFields(function, opt_params, opt_body, opt_ret_type, opt_ty_params, opt_attrs,
opt_virtual_device, opt_span);
});

TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
.set_dispatch<FunctionNode>([](const ObjectRef& ref, ReprPrinter* p) {
Expand Down