How python autotune get config


#1

I copy a code to get conv2d source code.But I don’t know how to get the corresponding config.

import numpy as np
import tvm
from tvm import autotvm
import topi
import topi.testing
from tvm.contrib.pickle_memoize import memoize
from topi.util import get_const_tuple

from common import get_all_backend

def verify_conv2d_nchw(batch, in_channel, in_size, num_filter, kernel, stride, padding, dilation=1):
    print("Workload: (%d, %d, %d, %d, %d, %d, %d, %d)" % (batch, in_channel, in_size, num_filter, kernel, stride, padding, dilation))
    in_height = in_width = in_size

A = tvm.placeholder((batch, in_channel, in_height, in_width), name='input')
W = tvm.placeholder((num_filter, in_channel, kernel, kernel), name='filter')

a_shape = get_const_tuple(A.shape)
w_shape = get_const_tuple(W.shape)
dtype = A.dtype

@memoize("topi.tests.test_topi_conv2d_nchw.verify_conv2d_nchw")
def get_ref_data():
    a_np = np.random.uniform(size=a_shape).astype(dtype)
    w_np = np.random.uniform(size=w_shape).astype(dtype)
    dw_np = topi.testing.dilate_python(w_np, (1, 1, dilation, dilation))
    c_np = topi.testing.conv2d_nchw_python(a_np, dw_np, stride, padding)
    return a_np, w_np, c_np

a_np, w_np, c_np = get_ref_data()

def check_device(device):
    ctx = tvm.context(device, 0)
    if not ctx.exist:
        print("Skip because %s is not enabled" % device)
        return
    print("Running on target: %s" % device)
    with tvm.target.create(device):
        C = topi.nn.conv2d(A, W, (stride, stride), (padding, padding),
                           (dilation, dilation), layout='NCHW', out_dtype=dtype)
        s = topi.generic.schedule_conv2d_nchw([C])

    a = tvm.nd.array(a_np, ctx)
    w = tvm.nd.array(w_np, ctx)
    c = tvm.nd.array(np.zeros(get_const_tuple(C.shape), dtype=C.dtype), ctx)
    func = tvm.build(s, [A, W, C], device, name="relu_%d_%d_%d_%d_%d_%d_%d_%d" % (batch, in_channel, in_size, num_filter, kernel, stride, padding, dilation))
    func(a, w, c)
    dev_module = func.imported_modules[0]
    print(dev_module.get_source())
    tvm.testing.assert_allclose(c.asnumpy(), c_np, rtol=1e-4)

for device in ["cuda"]:
    with autotvm.tophub.context(device):  # load tophub pre-tuned parameters
        check_device(device)

def test_conv2d_nchw():
    # ResNet18 workloads
    verify_conv2d_nchw(1,   3, 224,  64, 7, 2, 3)

#2

Not sure if I understood you question !
Are you looking for the autotune config files ?

The auto tune module downloads from https://github.com/uwsampl/tvm-distro


#3

Thank you. I want to know how to get the best config at autotune.


#4

You may refer to below sample to know how to autotune.
https://docs.tvm.ai/tutorials/autotvm/tune_relay_cuda.html

Once autotuned the log_file in above example holds the best tuning parameters. Later while building the module with autotvm.apply_history_best(log_file): will use the best params.