Skip to content

[RELAY] Support RelayBuild with Only Constants #4748

Description

@IshwaraK

The below reduced test-case shows segmentation fault in TVM compiler stack at the optimization level 2 and above. The problem is in call to 'lib.get_source()', as constant folding optimizes away whole graph and lib object becomes NULL.

import tvm
from tvm import relay
from tvm.contrib import graph_runtime
import numpy as np

data_shape = (2, 2)
matrix_x = relay.var('matrix_x', shape=data_shape, dtype='int32')
matrix_y = relay.var('matrix_y', shape=data_shape, dtype='int32')

add = relay.op.add(matrix_x, matrix_y)
fun = relay.Function([matrix_x,matrix_y], add)
relayMod = relay.Module({})
fname = relay.GlobalVar('main')
relayMod.entry_func = fname
relayMod[fname] = fun
print("Relay Module:\n", relayMod)

x_data = np.matrix('1 2; 3 4', dtype='int32')
y_data = np.matrix('5 6; 7 8', dtype='int32')
params = {
    "matrix_x" : x_data,
    "matrix_y" : y_data,
}
with relay.build_config(opt_level=3):
    graph, lib, params = relay.build(relayMod, "llvm", params=params)
print("Model graph:\n", graph)
print("\nModel parameters:")
for item in params:
    print(item, ":\n", params[item])
print("\nOperator library:\n", lib.get_source())
mod = graph_runtime.create(graph, lib, ctx=tvm.cpu(0))
mod.set_input(**params)
mod.run()
res = mod.get_output(0).asnumpy()
print("inference:\n", res)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions