Exemple #1
0
def test_extract_resnet():
    mod, _params = get_workload()
    expected_op_freqs = {
        "nn.batch_norm": 19,
        "nn.conv2d": 21,
        "nn.relu": 18,
        "nn.max_pool2d": 1,
        "add": 8,
        "nn.global_avg_pool2d": 1,
        "nn.batch_flatten": 1,
        "nn.dense": 1,
        "nn.bias_add": 1,
        "nn.softmax": 1,
    }
    op_freqs = relay.analysis.list_op_freqs(mod)
    assert len(op_freqs) == len(expected_op_freqs)
    assert all([op_freqs[op] == expected_op_freqs[op] for op in expected_op_freqs])
def test_end_to_end():
    # Load a resnet model.
    mod, params = get_workload()
    # Apply flexible dispatch pass.
    mod = relay.transform.FlexibleShapeDispatch(axis=0, buckets=[1, 4], auto_pad=True)(mod)
    # Compile and confirm result supports multiple shapes.
    exe = relay.vm.compile(mod, "llvm", params=params)
    vm = runtime.vm.VirtualMachine(exe, tvm.cpu())

    # Evaluate various batch sizes
    batch_1 = np.random.normal(size=[1, 3, 224, 224]).astype("float32")
    assert list(vm.invoke("main", batch_1).shape) == [1, 1000]

    batch_4 = np.random.normal(size=[4, 3, 224, 224]).astype("float32")
    assert list(vm.invoke("main", batch_4).shape) == [4, 1000]

    # Apply autopadding to an input.
    batch_3 = np.random.normal(size=[3, 3, 224, 224]).astype("float32")
    assert list(vm.invoke("main", batch_3).shape) == [3, 1000]
Exemple #3
0
def test_expr2graph():
    mod, _ = resnet.get_workload(num_layers=50, batch_size=1)
    node_dict = {}
    node_list = []
    target_ops = [relay.op.get("nn.conv2d")]
    op_name_list = []
    def _count_node(node):
        if isinstance(node, Call):
            op_name_list.append(node.op)
        elif isinstance(node, (Var, TupleGetItem, Tuple)):
            op_name_list.append(None)
    relay.analysis.post_order_visit(mod["main"], _count_node)

    expr2graph(mod["main"], target_ops, node_dict, node_list)
    assert len(node_list) == len(op_name_list)
    for i, item in enumerate(zip(op_name_list, node_list)):
        op_name, node = item
        assert op_name == node["op"], "%dth Node operator mismatch: expecting %s but got %s" \
                                      % (i, str(op_name), str(node["op"]))
Exemple #4
0
 def verify(num_layers):
     # Load a resnet with a known number of layers.
     mod, _ = resnet.get_workload(num_layers=num_layers)
     # Count the number of conv and dense layers.
     count = count_layers(mod, valid_ops=["nn.conv2d", "nn.dense"])
     assert count == num_layers
def test_extract_resnet():
    mod, _params = get_workload()
    items = relay.analysis.extract_fused_functions(mod)
    assert len(items) == 34
Exemple #6
0
from tvm.relay.build_module import bind_params_by_name
from tvm.ir.instrument import (
    PassTimingInstrument,
    pass_instrument,
)

###############################################################################
# Create An Example Relay Program
# -------------------------------
# We use pre-defined resnet-18 network in Relay.
batch_size = 1
num_of_image_class = 1000
image_shape = (3, 224, 224)
output_shape = (batch_size, num_of_image_class)
relay_mod, relay_params = resnet.get_workload(num_layers=18,
                                              batch_size=1,
                                              image_shape=image_shape)
print("Printing the IR module...")
print(relay_mod.astext(show_meta_data=False))

###############################################################################
# Create PassContext With Instruments
# -----------------------------------
# To run all passes with an instrument, pass it via the ``instruments`` argument to
# the ``PassContext`` constructor. A built-in ``PassTimingInstrument`` is used to
# profile the execution time of each passes.
timing_inst = PassTimingInstrument()
with tvm.transform.PassContext(instruments=[timing_inst]):
    relay_mod = relay.transform.InferType()(relay_mod)
    relay_mod = relay.transform.FoldScaleAxis()(relay_mod)
    # before exiting the context, get profile results.
def test_change_batch_resnet():
    net, params = resnet.get_workload()
    new_net = transform.ChangeBatch({net["main"].params[0]: 0},
                                    batch_size=123)(net)
    assert new_net["main"].checked_type.ret_type == relay.TensorType(
        (123, 1000))
Exemple #8
0
import tvm
from tvm import relay
from tvm.relay.testing import resnet
import numpy as np

image_shape = (3, 32, 32)

epsilon = 2e-5

model, params = resnet.get_workload(batch_size=1,
                                    num_classes=1000,
                                    num_layers=50,
                                    image_shape=image_shape,
                                    dtype='float32')

image = np.random.rand(1, *image_shape).astype('float32')

with open('image.npy', 'wb') as file:
    # Preprocess image: convert to NHWC
    image_nhwc = np.moveaxis(image, 1, 3)
    np.save(file, image_nhwc)

# Preprocess mean
for mean_var in [
        'bn_data_moving_mean',
        # 'stage1_unit1_bn1_moving_mean',
        # 'stage1_unit1_bn2_moving_mean',
        # 'stage1_unit1_bn3_moving_mean',
]:
    val = params[mean_var].asnumpy()
    val = -val