Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MetaSchedule][M4b] Testcases for TensorRT builder/runner #10055

Merged
merged 50 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
eb344fd
[MetatSchedule] testcase for TensorRT builder/runner
sunggg Jan 24, 2022
03646ac
[Runtime][PipelineExecutor] Add Pipeline Executor Interface (#10010)
huajsj Jan 25, 2022
29a77e3
[skip ci][Docker, CI] Update DGL installation, temp disable DGL tutor…
masahi Jan 26, 2022
452e168
[CUTLASS] Profile only the largest-possible alignment by default (#10…
masahi Jan 26, 2022
d2ac944
[Meta Schedule] Add `ApplyHisotryBest` Meta Schedule Context (#10049)
zxybazh Jan 26, 2022
15e0624
[MetaSchedule] Mutator Rule: Mutate Unroll (#10045)
spectrometerHBH Jan 26, 2022
f9e1ff8
[TIR][Schedule] Blockize and Tensorize (#9871)
vinx13 Jan 26, 2022
fa00dc6
[microTVM][tutorial] Add ENV variable to enable testing on physical h…
mehrdadh Jan 26, 2022
c90311a
[microNPU] Refactor base address determination to codegen (#9929)
manupak Jan 26, 2022
b3fcb4b
Add FP requantize flow. Set float32 flow by default for llvm x86 targ…
Icemist Jan 26, 2022
a0c95f8
[Relay][DefuseOps pass] bug fix: To support function body types other…
pranavjon Jan 26, 2022
e7705d7
[Fix Bug]fix the bug of tensorflow frontend when parsing Range layer …
ninesheep Jan 26, 2022
887779b
[MetaSchedule][M4a] Schedule Rule: Multi-Level-Tiling (#10043)
jinhongyii Jan 26, 2022
aa44d7b
Revert "[Frontend] Add Span filling for frontends to Relay (#9723)" (…
chunit-quic Jan 26, 2022
4a15db2
Improve the tensorflow frontend _test_spop_resource_variables to supp…
ophirfrish Jan 26, 2022
b1812bb
[MetaSchedule] postproc: rewrite_parallel_vectorize_unroll (#10071)
Hzfengsy Jan 26, 2022
04aa071
add pytest condition to pass CI. rename test name to be consistent.
sunggg Jan 26, 2022
394ec01
Clear warnings when building with MSVC. (#10059)
Icemist Jan 26, 2022
478896b
[Makefile] Fixed error in "make clean" (#10048)
Lunderberg Jan 26, 2022
5bee3c0
[Relay] QLinearMatMul allows 1D weight_scale, weight_zero_point input…
yuanfz98 Jan 26, 2022
87dd349
Don't explicitly link libgcc.a into libtvm_runtime.so on Android (#10…
Jan 26, 2022
c09d1c7
Change function constructors to WithFields (#9690)
electriclilies Jan 26, 2022
e2ad907
add pyteset decorator to pass CI
sunggg Jan 27, 2022
c6ae124
Document missing qnn operators (#10077)
Jan 27, 2022
8735349
Add temp git dir to test_cc_reviewers test case (#10058)
jacobbohlin Jan 27, 2022
f931fe1
[CI] Fix Rust permissions for wasmtime and sccache (#10015)
Mousius Jan 27, 2022
2790156
[EZ][Typo] Correct gather, scatter type rel error message (#10023)
AndrewZhaoLuo Jan 27, 2022
0740bc3
[microTVM][tvmc] Add TVMC Micro tutorial for Zephyr (#10024)
mehrdadh Jan 27, 2022
f61b172
[CI][Fix] Remove additional qnn.op.transpose_conv2d from docs (#10083)
lhutton1 Jan 27, 2022
b1bf871
[PyTorch] Fix rsub type (#10090)
comaniac Jan 28, 2022
9f0cc50
[microNPU] Removing constant args from PrimFunc (#9951)
manupak Jan 28, 2022
7a355e8
[Relay] fix incorrect binding of Lets in ANF conversion (#10078)
altanh Jan 28, 2022
552fefb
[microTVM] Update Zephyr to 2.7 (#10094)
Mousius Jan 28, 2022
91abbf8
[Runtime][PipelineExecutor] Pipeline Executor Sequential execution (#…
huajsj Jan 28, 2022
d3c0f40
[MetaSchedule][M4a] Mutator: Mutate Parallel (#10096)
jinhongyii Jan 28, 2022
d752e4a
[Hexagon] Update hexagon API build instruction and cleanup hexagon_pr…
mehrdadh Jan 28, 2022
01d7033
[MetatSchedule] testcase for TensorRT builder/runner
sunggg Jan 24, 2022
e8e0b5c
add pytest condition to pass CI. rename test name to be consistent.
sunggg Jan 26, 2022
4bad153
Rebase to pass CI and reflect suggestions
sunggg Jan 29, 2022
e38b24d
Add ASF header for the new file
sunggg Jan 29, 2022
a269c8c
[MetatSchedule] testcase for TensorRT builder/runner
sunggg Jan 24, 2022
8b87320
add pytest condition to pass CI. rename test name to be consistent.
sunggg Jan 26, 2022
46325d0
add pyteset decorator to pass CI
sunggg Jan 27, 2022
ca52292
[MetatSchedule] testcase for TensorRT builder/runner
sunggg Jan 24, 2022
419d756
add pytest condition to pass CI. rename test name to be consistent.
sunggg Jan 26, 2022
c45d16a
Rebase to pass CI and reflect suggestions
sunggg Jan 29, 2022
68fd695
Add ASF header for the new file
sunggg Jan 29, 2022
2390740
add pylint, docstring
sunggg Jan 29, 2022
0fda196
Merge branch 'meta-trt-testcase' of https://github.com/sunggg/tvm int…
sunggg Jan 29, 2022
04cb4ca
fix lint and wish for the best
sunggg Jan 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/tvm/meta_schedule/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
"""Testing utilities in meta schedule"""
from .local_rpc import LocalRPC
from .relay_workload import get_network
from .byoc_trt import relay_build_with_tensorrt
53 changes: 53 additions & 0 deletions python/tvm/meta_schedule/testing/byoc_trt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""TensorRT-MetaSchedule integration"""
# pylint: disable=import-outside-toplevel

from typing import List
import tvm
from tvm.runtime import Module
from tvm.meta_schedule.builder import BuilderResult
from tvm.target import Target


def relay_build_with_tensorrt(
mod: Module,
target: Target,
params: dict,
) -> List[BuilderResult]:
"""Build a Relay IRModule with TensorRT BYOC
Parameters
----------
mod : IRModule
The Relay IRModule to build.
target : Target
The target to build the module for.
params : Dict[str, NDArray]
The parameter dict to build the module with.
Returns
-------
mod : runtime.Module
The built module.
"""
from tvm.relay.op.contrib.tensorrt import partition_for_tensorrt

assert isinstance(target, Target)
mod, config = partition_for_tensorrt(mod, params)
with tvm.transform.PassContext(opt_level=3, config={"relay.ext.tensorrt.options": config}):
result = tvm.relay.build_module._build_module_no_factory(mod, "cuda", "llvm", params)
assert isinstance(result, Module)
return result
228 changes: 228 additions & 0 deletions tests/python/unittest/test_meta_schedule_byoc_tensorrt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
""" Test Meta Schedule Builder """
import sys
import pytest
import itertools
import tvm
from tvm import relay
from tvm.relay import testing
from tvm.relay.op.contrib import tensorrt
import numpy as np
from typing import List
from tvm._ffi import register_func
from tvm.target import Target
from tvm.runtime import Module
from tvm.meta_schedule.arg_info import TensorInfo
from tvm.meta_schedule.builder import BuilderInput, LocalBuilder, BuilderResult
from tvm.meta_schedule.runner import (
EvaluatorConfig,
LocalRunner,
RunnerInput,
)

from tvm.tir import FloatImm
from tvm.meta_schedule.testing import get_network

has_tensorrt_codegen = pytest.mark.skipif(
not tvm.get_global_func("relay.ext.tensorrt", True), reason="TensorRT codegen not available"
)
has_tensorrt_runtime = pytest.mark.skipif(
not tensorrt.is_tensorrt_runtime_enabled(), reason="TensorRT runtime not available"
)


# conv2d+relu network
def get_conv2d_relu(
data_shape,
out_channels,
kernel_size,
strides,
padding,
dilation,
groups,
data_layout,
kernel_layout,
dtype,
):

data = relay.var("data", relay.TensorType(data_shape, dtype))
weight = relay.var("weight")

net = relay.nn.conv2d(
data=data,
weight=weight, # conv kernel
strides=strides,
padding=padding,
dilation=dilation,
groups=groups,
channels=out_channels,
kernel_size=kernel_size,
data_layout=data_layout,
kernel_layout=kernel_layout,
)
net = relay.add(net, net)
net = relay.nn.relu(net)

inputs = relay.analysis.free_vars(net)
return relay.Function(inputs, net)


def verify_meta_schedule_with_tensorrt(
mod, params, data_shape, use_meta_sched: bool = True, use_trt: bool = True, mode: str = "vm"
):
if use_meta_sched:
# With meta_schedule
dev = "cuda"

# Build
if use_trt:
from tvm.meta_schedule.testing import relay_build_with_tensorrt

builder = LocalBuilder(f_build=relay_build_with_tensorrt)
else:

def relay_build_without_tensorrt(
mod: Module,
target: Target,
params: dict,
) -> List[BuilderResult]:
return tvm.relay.build_module._build_module_no_factory(mod, "cuda", "llvm", params)

builder = LocalBuilder(f_build=relay_build_without_tensorrt)

builder_input = BuilderInput(mod, Target(dev, host="llvm"), params)

(builder_result,) = builder.build([builder_input])
assert builder_result.error_msg is None
assert builder_result.artifact_path is not None

# Run
evaluator_config = EvaluatorConfig(
number=5,
repeat=2,
min_repeat_ms=0,
enable_cpu_cache_flush=False,
)

runner_input = RunnerInput(
builder_result.artifact_path, "cuda", [TensorInfo("float32", data_shape)]
)

def eval_func(rt_mod, device, evaluator_config, repeated_args):
rt_mod = tvm.contrib.graph_executor.GraphModule(rt_mod["default"](device))

eval = rt_mod.module.time_evaluator(
func_name="run",
dev=device,
number=evaluator_config.number,
repeat=evaluator_config.repeat,
min_repeat_ms=evaluator_config.min_repeat_ms,
f_preproc="cache_flush_cpu_non_first_arg"
if evaluator_config.enable_cpu_cache_flush
else "",
)
repeated_costs: List[List[float]] = []
for args in repeated_args:
profile_result = eval(*args)
repeated_costs.append(profile_result.results)

costs = [float(cost) for cost in itertools.chain.from_iterable(repeated_costs)]
return costs

runner = LocalRunner(
evaluator_config=evaluator_config,
f_run_evaluator=eval_func,
)

# Run the module
(runner_future,) = runner.run([runner_input])
runner_result = runner_future.result()
assert runner_result is not None
assert runner_result.run_secs is not None
assert runner_result.error_msg is None

for result in runner_result.run_secs:
if isinstance(result, FloatImm):
result = result.value
assert isinstance(result, float)
assert result >= 0.0

else:
# Without meta_schedule
if use_trt:
mod, config = tensorrt.partition_for_tensorrt(mod)
with tvm.transform.PassContext(
opt_level=3, config={"relay.ext.tensorrt.options": config}
):
func = relay.create_executor(
mode, mod=mod, device=tvm.cuda(0), target="cuda"
).evaluate()
else:
with tvm.transform.PassContext(opt_level=3):
func = relay.create_executor(
mode, mod=mod, device=tvm.cuda(0), target="cuda", params=params
).evaluate()


@tvm.testing.requires_cuda
@has_tensorrt_codegen
@has_tensorrt_runtime
def test_conv2d_relu():
data_shape = (1, 1280, 14, 14)
out_channels = 256
kernel_size, strides, padding, dilation, groups = (1, 1), (1, 1), (0, 0, 0, 0), (1, 1), 1
data_layout, kernel_layout = "NCHW", "OIHW"
dtype = "float32"

f = get_conv2d_relu(
data_shape,
out_channels,
kernel_size,
strides,
padding,
dilation,
groups,
data_layout,
kernel_layout,
dtype,
)

mod, params = testing.create_workload(f)
verify_meta_schedule_with_tensorrt(mod, params, data_shape)


@tvm.testing.requires_cuda
@has_tensorrt_codegen
@has_tensorrt_runtime
@pytest.mark.parametrize(
"model_name",
["resnet-50", "mobilenet"],
)
@pytest.mark.parametrize("batch_size", [1])
@pytest.mark.parametrize("use_meta_sched", [True])
@pytest.mark.parametrize("use_trt", [True, False])
def test_relay_model(model_name: str, batch_size: int, use_meta_sched: bool, use_trt: bool):

mod, params, input_shape, output_shape = get_network(name=model_name, batch_size=batch_size)
verify_meta_schedule_with_tensorrt(
mod, params, input_shape, use_meta_sched=use_meta_sched, use_trt=use_trt, mode="vm"
)


if __name__ == "__main__":
sys.exit(pytest.main([__file__] + sys.argv[1:]))