当前位置: 首页>>代码示例>>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;未经允许,请勿转载。