Thanks for sharing the pointer. But this actually change constant into vars. Is there an approach to rewrite the constant values back to the graph? For example
If you don’t want to change the function, you can use the same approach, just skip the bits in the example where it rewrites the function, so it would become something like
class ExtractConstants(ExprMutator):
def __init__(self):
super().__init__()
self.constants = []
def visit_constant(self, const):
# append the constant to the dict to return it or do whatever checks you need on it
self.constants.append(const.data.asnumpy())
return const
def extract_constants(self, func):
self.visit(func)
return self.constants
If you want to change the constant into something else, you can change the middle function to something like
def visit_constant(self, const):
# define your new constant
new_const = relay.Constant(tvm.nd.array(...))
return new_const
Note that I haven’t tried running that code here, but that’s what I would try