Access Relay Meta Constant Pool

Hi everyone,

I was wondering if anyone knew the easiest way to access the relay meta dict, e.g. for verification.

After rewriting a module, it want to check if the value in meta[relay.Constant][0] has the new correct value:

fn (%x: Tensor[(1, 1, 1, 128), float32]) -> Tensor[(1, 1, 1, 128), float32] {
   add(%x, meta[relay.Constant][0] /* ty=Tensor[(1, 1, 1, 128), float32] */) /* ty=Tensor[(1, 1, 1, 128), float32] */
}

is there something like

x = mod.meta[relay.Constant][0]

@comaniac @areusch

1 Like

I got similar questions here. Waiting for a pointer.

I am not aware of an easy way of looking at the constants that are embedded into the module without actually going in and looking at them… There are some examples of the visitors that dig out the constants from the function, e.g. https://github.com/apache/tvm/blob/main/python/tvm/relay/backend/contrib/ethosu/tir/compiler.py#L132-L159

1 Like

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

fn (%x: Tensor[(1, 1, 1, 128), float32]) -> Tensor[(1, 1, 1, 128), float32] {
   add(%x, meta[relay.Constant][0])
}
fn (%x: Tensor[(1, 1, 1, 128), float32]) -> Tensor[(1, 1, 1, 128), float32] {
   add(%x, 1.0)
}

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 :slight_smile:

2 Likes

The following code may help those who want to to extract constants tagged with Relay.Const

class ExtractMetaConstants(ExprMutator):
    # Dirty fix for unknown relay.Const[Meta] occurance.
    def __init__(self):
        super().__init__()
        self.constants = []

    def visit_constant(self, const: relay.expr.Constant):
        np_data = const.data.numpy()
        new_const = relay.const(np_data)
        if "relay.Constant" in str(const):
            self.constants.append(np_data)
        return new_const

    def extract_constants(self, func):
        expr = self.visit(func)
        return expr, self.constants
2 Likes