當前位置: 首頁>>代碼示例>>Python>>正文


Python relay.build_config方法代碼示例

本文整理匯總了Python中tvm.relay.build_config方法的典型用法代碼示例。如果您正苦於以下問題:Python relay.build_config方法的具體用法?Python relay.build_config怎麽用?Python relay.build_config使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tvm.relay的用法示例。


在下文中一共展示了relay.build_config方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: compile_nnvm

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def compile_nnvm(symbol, target, input_names, inputs, params,
                 opt_level):
    import nnvm
    import nnvm.compiler

    shape_dict = {}
    dtype_dict = {}
    for name, value in zip(input_names, inputs.values()):
        shape_dict[name] = value.shape
        dtype_dict[name] = value.dtype
    for name, value in params.items():
        shape_dict[name] = value.shape
        dtype_dict[name] = value.dtype
    with nnvm.compiler.build_config(opt_level=opt_level):
        graph, lib, params = nnvm.compiler.build(symbol, target,
                                                 shape=shape_dict,
                                                 dtype=dtype_dict,
                                                 params=params)
    return graph, lib, params 
開發者ID:pfnet-research,項目名稱:chainer-compiler,代碼行數:21,代碼來源:run_onnx_tvm.py

示例2: build_graph_relay

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def build_graph_relay(args, ctx, onnx_model, inputs, input_names):
    import tvm.relay as relay

    shape_dict = {}
    dtype_dict = {}
    for name, value in zip(input_names, inputs.values()):
        shape_dict[name] = value.shape
        dtype_dict[name] = value.dtype

    mod, params = relay.frontend.from_onnx(onnx_model, shape=shape_dict)
    with relay.build_config(opt_level=args.opt_level):
        graph, lib, params = relay.build(mod, args.target, params=params)

        if args.dump_frontend:
            print(graph)

    graph_module = create_graph_module(args, graph, lib, ctx)
    set_inputs(graph_module, ctx, inputs, params)

    return graph_module 
開發者ID:pfnet-research,項目名稱:chainer-compiler,代碼行數:22,代碼來源:run_onnx_tvm.py

示例3: run_relay_graph

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def run_relay_graph(mod, params, shape_dict, target, ctx):
    with relay.build_config(opt_level=3):
        graph, lib, params = relay.build(mod, target=target, params=params)
    input_shape = shape_dict["input_1"]
    dummy_data = np.random.uniform(size=input_shape, low=0, high=input_shape[1]).astype(
        "int32"
    )

    m = graph_runtime.create(graph, lib, ctx)
    m.set_input(0, dummy_data)
    m.set_input(**params)
    m.run()
    tvm_output = m.get_output(0)

    ftimer = m.module.time_evaluator("run", ctx, repeat=5, number=5)
    prof_res = np.array(ftimer().results) * 1000
    print(
        "%-20s %-19s (%s)"
        % ("Runtime:", "%.2f ms" % np.mean(prof_res), "%.2f ms" % np.std(prof_res))
    )
    return tvm_output 
開發者ID:apache,項目名稱:incubator-tvm,代碼行數:23,代碼來源:deploy_sparse.py

示例4: test_compile_and_run

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def test_compile_and_run():
    ctx=tvm.cpu()
    target="llvm"
    tol=1e-3

    with relay.build_config(opt_level=3):
        json, lib, params = relay.build(_create_graph_annotated(), target=target)
    m = tvm.contrib.graph_runtime.create(json, lib, ctx)

    shape = (10, 10)
    x_data = np.random.rand(*shape).astype('float32')
    y_data = np.random.rand(*shape).astype('float32')

    m.set_input("x", x_data)
    m.set_input("y", y_data)
    m.set_input(**params)
    m.run()
    out = tvm.nd.empty(shape, ctx=ctx)
    out = m.get_output(0, out)

    expected = (y_data * y_data) - (x_data + x_data)
    tvm.testing.assert_allclose(out.asnumpy(), expected, rtol=tol, atol=tol) 
開發者ID:apache,項目名稱:incubator-tvm,代碼行數:24,代碼來源:test_coreml_codegen.py

示例5: tune_and_evaluate

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def tune_and_evaluate(tuning_opt, number, tune=True):
    op, params, data_shape = get_workload(batch_size, image_shape, out_channel, kernel_size, strides, padding)
    tasks = autotvm.task.extract_from_program(op, target=target, params=params, ops=(relay.op.nn.conv2d,))
    log_file = tuning_opt["log_filename"]
    if tune:
        print("Tuning...")
        tune_kernels(tasks, **tuning_opt)

    if not os.path.exists(log_file):
        raise RuntimeError("the log file {} doesn't exists".format(log_file))
    with autotvm.apply_history_best(log_file):
        print("Compile...")
        with relay.build_config(opt_level=3):
            graph, lib, params = relay.build_module.build(op, target=target, params=params)

        ctx = tvm.context(str(target), 0)
        data_tvm = tvm.nd.array((np.random.uniform(size=data_shape)).astype(dtype))
        module = runtime.create(graph, lib, ctx)
        module.set_input("data", data_tvm)
        module.set_input(**params)

        # evaluate
        print("Evaluate inference time cost...")
        ftimer = module.module.time_evaluator("run", ctx, number=1, repeat=number)
        prof_res = np.array(ftimer().results) * 1e3
        print("Time cost is: ", np.mean(prof_res)) 
開發者ID:KnowingNothing,項目名稱:FlexTensor,代碼行數:28,代碼來源:autotvm_opt_topi_conv2d_gpu.py

示例6: tune_and_evaluate

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def tune_and_evaluate(tuning_opt, number, tune=True):
    op, params, data_shape = get_workload(batch_size, image_shape, out_channel, kernel_size, strides, padding)
    print(tvm.autotvm.task.task.TASK_TABLE)
    tasks = autotvm.task.extract_from_program(op, target=target, params=params, ops=(relay.op.nn.dense,))
    log_file = tuning_opt["log_filename"]
    if tune:
        print("Tuning...")
        tune_kernels(tasks, **tuning_opt)

    if not os.path.exists(log_file):
        raise RuntimeError("the log file {} doesn't exists".format(log_file))
    with autotvm.apply_history_best(log_file):
        print("Compile...")
        with relay.build_config(opt_level=3):
            graph, lib, params = relay.build_module.build(op, target=target, params=params)

        ctx = tvm.cpu(np.random.randint(0, 20))
        data_tvm = tvm.nd.array((np.random.uniform(size=data_shape)).astype(dtype))
        module = runtime.create(graph, lib, ctx)
        module.set_input("data", data_tvm)
        module.set_input(**params)

        # evaluate
        print("Evaluate inference time cost...")
        ftimer = module.module.time_evaluator("run", ctx, number=number, repeat=1)
        prof_res = np.array(ftimer().results) * 1e3
        cost = np.mean(prof_res)
        print("Time cost is: ", cost)
    return cost 
開發者ID:KnowingNothing,項目名稱:FlexTensor,代碼行數:31,代碼來源:autotvm_opt_topi_matmul_cpu.py

示例7: tune_and_evaluate

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def tune_and_evaluate(tuning_opt, number, tune=True):
    op, params, data_shape = get_workload(batch_size, image_shape, out_channel, kernel_size, strides, padding)
    tasks = autotvm.task.extract_from_program(op, target=target, params=params, ops=(relay.op.nn.conv2d,))
    log_file = tuning_opt["log_filename"]
    if tune:
        print("Tuning...")
        tune_kernels(tasks, **tuning_opt)

    if not os.path.exists(log_file):
        raise RuntimeError("the log file {} doesn't exists".format(log_file))
    with autotvm.apply_history_best(log_file) as f:
        print(f.best_by_targetkey)
        print("Compile...")
        with relay.build_config(opt_level=3):
            graph, lib, params = relay.build_module.build(op, target=target, params=params)

        ctx = tvm.cpu(np.random.randint(0, 8))
        data_tvm = tvm.nd.array((np.random.uniform(size=data_shape)).astype(dtype))
        module = runtime.create(graph, lib, ctx)
        module.set_input("data", data_tvm)
        module.set_input(**params)

        # evaluate
        print("Evaluate inference time cost...")
        ftimer = module.module.time_evaluator("run", ctx, number=number, repeat=1)
        prof_res = np.array(ftimer().results) * 1e3
        cost = np.mean(prof_res)
        print("Time cost is: ", cost)
    return cost 
開發者ID:KnowingNothing,項目名稱:FlexTensor,代碼行數:31,代碼來源:autotvm_opt_topi_conv2d_cpu.py

示例8: tune_and_evaluate

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def tune_and_evaluate(tuning_opt, number, tune=True):
    op, params, data_shape = get_workload(batch_size, image_shape, out_channel, kernel_size, strides, padding)
    tasks = autotvm.task.extract_from_program(op, target=target, params=params, ops=(relay.op.nn.conv2d,))
    log_file = tuning_opt["log_filename"]
    if tune:
        print("Tuning...")
        tune_kernels(tasks, **tuning_opt)

    if not os.path.exists(log_file):
        raise RuntimeError("the log file {} doesn't exists".format(log_file))
    with autotvm.apply_history_best(log_file) as f:
        print(f.best_by_targetkey)
        print("Compile...")
        with relay.build_config(opt_level=3):
            graph, lib, params = relay.build_module.build(op, target=target, params=params)

        ctx = tvm.cpu(np.random.randint(0, 20))
        data_tvm = tvm.nd.array((np.random.uniform(size=data_shape)).astype(dtype))
        module = runtime.create(graph, lib, ctx)
        module.set_input("data", data_tvm)
        module.set_input(**params)

        # evaluate
        print("Evaluate inference time cost...")
        ftimer = module.module.time_evaluator("run", ctx, number=number, repeat=1)
        prof_res = np.array(ftimer().results) * 1e3
        cost = np.mean(prof_res)
        print("Time cost is: ", cost)
    return cost 
開發者ID:KnowingNothing,項目名稱:FlexTensor,代碼行數:31,代碼來源:autotvm_opt_topi_conv2d_cpu.py

示例9: tune_and_evaluate

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def tune_and_evaluate(tuning_opt):
    # extract workloads from relay program
    print("Extract tasks...")
    net, params, input_shape, out_shape = get_network(network, batch_size=1)
    tasks = autotvm.task.extract_from_program(net, target=target,
                                            params=params, ops=(relay.op.nn.conv2d,))

    # run tuning tasks
    print("Tuning...")
    tune_tasks(tasks, **tuning_opt)

    # compile kernels with history best records
    with autotvm.apply_history_best(log_file):
        print("Compile...")
        with relay.build_config(opt_level=3):
            graph, lib, params = relay.build_module.build(
                net, target=target, params=params)

        # export library
        tmp = tempdir()
        filename = "net.tar"
        lib.export_library(tmp.relpath(filename))

        # load parameters
        ctx = tvm.context(str(target), 0)
        module = runtime.create(graph, lib, ctx)
        data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))
        module.set_input('data', data_tvm)
        module.set_input(**params)

        # evaluate
        print("Evaluate inference time cost...")
        ftimer = module.module.time_evaluator("run", ctx, number=1, repeat=600)
        prof_res = np.array(ftimer().results) * 1000  # convert to millisecond
        print("Mean inference time (std dev): %.2f ms (%.2f ms)" %
              (np.mean(prof_res), np.std(prof_res)))

# We do not run the tuning in our webpage server since it takes too long.
# Uncomment the following line to run it by yourself. 
開發者ID:KnowingNothing,項目名稱:FlexTensor,代碼行數:41,代碼來源:tune_relay_cuda.py

示例10: benchmark_tvm_v1

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def benchmark_tvm_v1(net, input_shape=(1, 3, 224, 224)):
    inputs = torch.rand(*input_shape).float()
    with torch2trt.core.tvm_network():
        trace, graph_pth = torch2trt.core.torch2tvm(
            net,
            inputs,
            verbose=True)

    outputs = graph_pth.get_resolved_outputs()
    tvm_weight_dict = graph_pth.context.tvm_weight_dict
    params = {k.name_hint: v for k, v in tvm_weight_dict.items()}
    func = expr.Function(analysis.free_vars(outputs), outputs)
    target = 'cuda -libs=cudnn'
    with relay.build_config(opt_level=3):
        graph, lib, params = relay.build(func, target, params=params)
    ctx = TVMInferenceContext(graph, lib, params, tvm.gpu(0), ["x"])
    times = []
    ctx.inference_async(inputs.numpy())
    for i in range(100):
        torch.cuda.synchronize()
        t = time.time()
        ctx.execute_async() # use this to avoid htod and dtoh overhead.
        torch.cuda.synchronize()
        times.append(time.time() - t)

    print("tvm time:", np.mean(times[2:])) 
開發者ID:traveller59,項目名稱:torch2trt,代碼行數:28,代碼來源:benchmark.py

示例11: __init__

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def __init__(self, mod, params, ctx=None, input_names=None, cudnn=False, opt_level=3):
        target = "cuda"
        if cudnn:
            target += " -libs=cudnn"
        # target = 'llvm -mcpu=core-avx2'
        with relay.build_config(opt_level=3):
            graph, lib, params = relay.build(mod, target, params=params)
        self.graph = graph
        self.lib = lib
        self.ctx = ctx or tvm.gpu(0)
        # self.ctx = ctx or tvm.cpu()
        self.tvm_context = graph_runtime.create(graph, lib, self.ctx)
        self.params = params
        self.tvm_context.set_input(**params)
        self.input_names = input_names 
開發者ID:traveller59,項目名稱:torch2trt,代碼行數:17,代碼來源:module.py

示例12: build_config

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def build_config(opt_level=2,
                 required_pass=None,
                 disabled_pass=None,
                 trace=None):
    """Configure the build behavior by setting config variables. This function
    will be deprecated in TVM v0.7. Instead, we should directly use
    tvm.transform.PassContext.

    Parameters
    ----------
    opt_level: int, optional
        Optimization level. The optimization pass name and level are as the
        following:

        .. code-block:: python

            OPT_PASS_LEVEL = {
                "SimplifyInference": 0,
                "OpFusion": 1,
                "FoldConstant": 2,
                "FoldScaleAxis": 3,
                "AlterOpLayout": 3,
                "CanonicalizeOps": 3,
                "CanonicalizeCast": 3,
                "EliminateCommonSubexpr": 3,
                "CombineParallelConv2D": 4,
                "CombineParallelDense": 4,
                "CombineParallelBatchMatmul": 4,
                "FastMath": 4
            }

    required_pass: set of str, optional
        Optimization passes that are required regardless of optimization level.

    disabled_pass: set of str, optional
        Optimization passes to be disabled during optimization.

    trace: Callable[[IRModule, PassInfo, bool], None]
        A tracing function for debugging or introspection.

    Returns
    -------
    pass_context: PassContext
        The pass context for optimizations.
    """
    warnings.warn("relay.build_config will be deprecated. Please use \
                  tvm.transform.PassContext directly", DeprecationWarning)
    return tvm.transform.PassContext(opt_level, required_pass, disabled_pass, trace) 
開發者ID:apache,項目名稱:incubator-tvm,代碼行數:50,代碼來源:transform.py

示例13: test_alter_layout_strided_slice

# 需要導入模塊: from tvm import relay [as 別名]
# 或者: from tvm.relay import build_config [as 別名]
def test_alter_layout_strided_slice():
    """Test rewriting strided_slice during alter_iop_layout"""
    def before():
        x = relay.var("x", shape=(1, 32, 28, 28))
        weight = relay.var('weight', shape=(32, 32, 3, 3))
        y = relay.nn.conv2d(x, weight, channels=32, kernel_size=(3, 3), padding=(1, 1))
        y = relay.strided_slice(y,
                                begin=relay.const([0, 16], "int32"),
                                end=relay.const([1, 33], "int32"),
                                strides=relay.const([1, 1], "int32"))
        y = relay.Function(analysis.free_vars(y), y)
        return y

    def alter_conv2d(attrs, inputs, tinfos, out_type):
        data, weight = inputs
        new_attrs = dict(attrs)
        new_attrs['data_layout'] = 'NCHW4c'
        return relay.nn.conv2d(data, weight, **new_attrs)

    def expected():
        x = relay.var("x", shape=(1, 32, 28, 28))
        weight = relay.var("weight", shape=(32, 32, 3, 3))
        weight = relay.layout_transform(weight, "OIHW", "OIHW4i4o")
        x = relay.layout_transform(x, "NCHW", "NCHW4c")
        y = relay.op.nn.contrib_conv2d_nchwc(x, weight, channels=32, kernel_size=(3, 3), padding=(1, 1),
                                             data_layout="NCHW4c")

        y = relay.strided_slice(y,
                                begin=relay.const([0, 4], "int32"),
                                end=relay.const([1, 21], "int32"),
                                strides=relay.const([1, 1], "int32"))

        y = relay.layout_transform(y, "NCHW4c", "NCHW")
        y = relay.Function(analysis.free_vars(y), y)
        return y

    with TempOpAttr("nn.conv2d", "FTVMAlterOpLayout", alter_conv2d):
        a = before()
        b = run_opt_pass(expected(), transform.InferType())

    # Verify inference result
    mod_before = tvm.IRModule()
    mod_new = tvm.IRModule()
    mod_before['main'] = a
    mod_new['main'] = b
    with relay.build_config(opt_level=3):
        for target, ctx in ctx_list():
            for kind in ["graph", "debug", "vm"]:
                ex_before = relay.create_executor(kind, mod=mod_before, ctx=ctx, target=target)
                ex_new = relay.create_executor(kind, mod=mod_new, ctx=ctx, target=target)
                np_data = np.random.uniform(size=(1, 32, 28, 28)).astype("float32")
                np_weight = np.random.uniform(size=(32, 32, 3, 3)).astype("float32")
                result_before = ex_before.evaluate()(np_data, np_weight)
                result_new = ex_new.evaluate()(np_data, np_weight)
                tvm.testing.assert_allclose(result_before.asnumpy(), result_new.asnumpy(), rtol=1e-5, atol=1e-5) 
開發者ID:apache,項目名稱:incubator-tvm,代碼行數:57,代碼來源:test_pass_alter_op_layout.py


注:本文中的tvm.relay.build_config方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。