image.png
    如上图所示:
    如果有GPU,可以选择cuda
    target = ‘cuda’
    ctx=tvm.gpu(0)

    如果没有GPU,也可以用CPU, llvm ;
    target = ‘llvm’
    ctx=tvm.cpu(0)

    或是自定义的硬件
    target = ‘llvm’
    ctx=tvm.vacc(0)

    详细请参考tutorials/relay_quick_start.py

    1. import numpy as np
    2. from tvm import relay
    3. from tvm.relay import testing
    4. import tvm
    5. from tvm.contrib import graph_runtime
    6. ######################################################################
    7. # Define Neural Network in Relay
    8. batch_size = 1
    9. num_class = 1000
    10. image_shape = (3, 224, 224)
    11. data_shape = (batch_size,) + image_shape
    12. out_shape = (batch_size, num_class)
    13. mod, params = relay.testing.resnet.get_workload(
    14. num_layers=18, batch_size=batch_size, image_shape=image_shape)
    15. # set show_meta_data=True if you want to show meta data
    16. print(mod.astext(show_meta_data=False))
    17. ######################################################################
    18. # Compilation
    19. opt_level = 3
    20. target = tvm.target.cuda()
    21. with relay.build_config(opt_level=opt_level):
    22. graph, lib, params = relay.build_module.build(
    23. mod, target, params=params)
    24. #####################################################################
    25. # Run the generate library
    26. # create random input
    27. ctx = tvm.gpu()
    28. data = np.random.uniform(-1, 1, size=data_shape).astype("float32")
    29. # create module
    30. module = graph_runtime.create(graph, lib, ctx)
    31. # set input and parameters
    32. module.set_input("data", data)
    33. module.set_input(**params)
    34. # run
    35. module.run()
    36. # get output
    37. out = module.get_output(0, tvm.nd.empty(out_shape)).asnumpy()
    38. # Print first 10 elements of output
    39. print(out.flatten()[0:10])
    40. ######################################################################
    41. # Save and Load Compiled Module
    42. # -----------------------------
    43. # We can also save the graph, lib and parameters into files and load them
    44. # back in deploy environment.
    45. ####################################################
    46. # save the graph, lib and params into separate files
    47. from tvm.contrib import util
    48. temp = util.tempdir()
    49. path_lib = temp.relpath("deploy_lib.tar")
    50. lib.export_library(path_lib)
    51. with open(temp.relpath("deploy_graph.json"), "w") as fo:
    52. fo.write(graph)
    53. with open(temp.relpath("deploy_param.params"), "wb") as fo:
    54. fo.write(relay.save_param_dict(params))
    55. print(temp.listdir())
    56. ####################################################
    57. # load the module back.
    58. loaded_json = open(temp.relpath("deploy_graph.json")).read()
    59. loaded_lib = tvm.module.load(path_lib)
    60. loaded_params = bytearray(open(temp.relpath("deploy_param.params"), "rb").read())
    61. input_data = tvm.nd.array(np.random.uniform(size=data_shape).astype("float32"))
    62. module = graph_runtime.create(loaded_json, loaded_lib, ctx)
    63. module.load_params(loaded_params)
    64. module.run(data=input_data)
    65. out_deploy = module.get_output(0).asnumpy()
    66. # Print first 10 elements of output
    67. print(out_deploy.flatten()[0:10])
    68. # check whether the output from deployed module is consistent with original one
    69. tvm.testing.assert_allclose(out_deploy, out, atol=1e-3)