Beispiel #1
0
def test_conv2d_nchw():
    schedules = [
        SpatialPack(vh=7, vw=1, vc=16, ba=64, bc=4, unroll=True),
        SpatialPack(vh=7, vw=1, vc=16, ba=64, bc=1, unroll=True),
        SpatialPack(vh=7, vw=7, vc=16, ba=16, bc=64, unroll=False)
    ]
    # verify_conv2d_nchw_all(1, 64, 56, 64, 3, 1, 1, schedules[0])
    verify_conv2d_nchw(1, 64, 56, 64, 3, 1, 1, schedules[0])
    # verify_conv2d_nchw_gemm(1, 64, 56, 64, 3, 1, 1)
    # ResNet18 worklaods
    """
Beispiel #2
0
def test_resnet_1():
    print(
        'MKLDNN - g1mb1_ic64ih56iw56_oc64oh56ow56_kh3kw3_sh1sw1_ph1pw1_n : 0.72514 ms'
    )
    sch = SpatialPack(vh=7, vw=1, vc=16, ba=64, bc=4, unroll=True)
    print('Run separately ...')
    verify_conv2d_nchw(1, 64, 56, 64, 3, 1, 1, sch)
    print('Run together ...')
    verify_conv2d_nchw_all(1, 64, 56, 64, 3, 1, 1, sch)
Beispiel #3
0
def test_resnet_0():
    print(
        'MKLDNN - g1mb1_ic3ih224iw224_oc64oh112ow112_kh7kw7_sh2sw2_ph3pw3_n : 0.773982 ms'
    )
    sch = SpatialPack(vh=28, vw=8, vc=16, ba=224, bc=1,
                      unroll=True)  # 1.42711 ms
    print('Run separately ...')
    verify_conv2d_nchw(1, 3, 224, 64, 7, 2, 3, sch)
    print('Run together ...')
    verify_conv2d_nchw_all(1, 3, 224, 64, 7, 2, 3, sch)
Beispiel #4
0
    Workload('int8', 'int32', 224, 224, 3, 64, 7, 7, 3, 3, 2, 2),
    Workload('int8', 'int32', 56, 56, 64, 64, 3, 3, 1, 1, 1, 1),
    Workload('int8', 'int32', 56, 56, 64, 64, 1, 1, 0, 0, 1, 1),
    Workload('int8', 'int32', 56, 56, 64, 128, 3, 3, 1, 1, 2, 2),
    Workload('int8', 'int32', 56, 56, 64, 128, 1, 1, 0, 0, 2, 2),
    Workload('int8', 'int32', 28, 28, 128, 128, 3, 3, 1, 1, 1, 1),
    Workload('int8', 'int32', 28, 28, 128, 256, 3, 3, 1, 1, 2, 2),
    Workload('int8', 'int32', 28, 28, 128, 256, 1, 1, 0, 0, 2, 2),
    Workload('int8', 'int32', 14, 14, 256, 256, 3, 3, 1, 1, 1, 1),
    Workload('int8', 'int32', 14, 14, 256, 512, 3, 3, 1, 1, 2, 2),
    Workload('int8', 'int32', 14, 14, 256, 512, 1, 1, 0, 0, 2, 2),
    Workload('int8', 'int32', 7, 7, 512, 512, 3, 3, 1, 1, 1, 1),
]
_SCHEDULES = [
    # float32 imagenet
    SpatialPack(1, 8, 4, 1, 4, True),
    SpatialPack(1, 8, 4, 1, 4, True),
    SpatialPack(1, 7, 4, 2, 4, True),
    SpatialPack(1, 4, 8, 4, 1, True),
    SpatialPack(1, 4, 4, 1, 16, False),
    SpatialPack(1, 4, 8, 4, 8, False),
    SpatialPack(1, 7, 4, 3, 8, True),
    SpatialPack(1, 2, 8, 1, 8, True),
    SpatialPack(2, 1, 16, 1, 4, True),
    SpatialPack(1, 7, 4, 1, 1, True),
    Im2ColPack(7, 4, 1, 16, True),
    Im2ColPack(7, 4, 1, 8, False),
    Im2ColPack(7, 4, 1, 16, False),
]

def test_conv2d_nchw():
    cost_all = []
    cost_data = []
    cost_kernel = []
    cost_conv = []
    schedules = []

    # g1mb1_ic3ih224iw224_oc64oh112ow112_kh7kw7_sh2sw2_ph3pw3_n"resnet18_0" : 0.773982 ms
    # in_channel, in_size, num_filter, kernel, stride, padding = 3, 224, 64, 7, 2, 3

    # g1mb1_ic64ih56iw56_oc64oh56ow56_kh3kw3_sh1sw1_ph1pw1_n"resnet18_1" : 0.72514 ms
    # in_channel, in_size, num_filter, kernel, stride, padding = 64, 56, 64, 3, 1, 1

    # g1mb1_ic64ih56iw56_oc64oh56ow56_kh1kw1_sh1sw1_ph0pw0_n"resnet18_2"
    # in_channel, in_size, num_filter, kernel, stride, padding = 64, 56, 64, 1, 1, 0

    # g1mb1_ic64ih56iw56_oc128oh28ow28_kh3kw3_sh2sw2_ph1pw1_n"resnet18_3"
    # in_channel, in_size, num_filter, kernel, stride, padding = 64, 56, 128, 3, 2, 1

    # g1mb1_ic64ih56iw56_oc128oh28ow28_kh1kw1_sh2sw2_ph0pw0_n"resnet18_4"
    in_channel, in_size, num_filter, kernel, stride, padding = 64, 56, 128, 1, 2, 0

    # g1mb1_ic128ih28iw28_oc128oh28ow28_kh3kw3_sh1sw1_ph1pw1_n"resnet18_5"
    # in_channel, in_size, num_filter, kernel, stride, padding = 128, 28, 128, 3, 1, 1

    # g1mb1_ic128ih28iw28_oc256oh14ow14_kh3kw3_sh2sw2_ph1pw1_n"resnet18_6"
    # in_channel, in_size, num_filter, kernel, stride, padding = 128, 28, 256, 3, 2, 1

    # g1mb1_ic128ih28iw28_oc256oh14ow14_kh1kw1_sh2sw2_ph0pw0_n"resnet18_7"
    # in_channel, in_size, num_filter, kernel, stride, padding = 128, 28, 256, 1, 2, 0

    # g1mb1_ic256ih14iw14_oc256oh14ow14_kh3kw3_sh1sw1_ph1pw1_n"resnet18_8"
    # in_channel, in_size, num_filter, kernel, stride, padding = 256, 14, 256, 3, 1, 1

    # g1mb1_ic256ih14iw14_oc512oh7ow7_kh3kw3_sh2sw2_ph1pw1_n"resnet18_9"
    # in_channel, in_size, num_filter, kernel, stride, padding = 256, 14, 512, 3, 2, 1

    # g1mb1_ic256ih14iw14_oc512oh7ow7_kh1kw1_sh2sw2_ph0pw0_n"resnet18_10"
    # in_channel, in_size, num_filter, kernel, stride, padding = 256, 14, 512, 1, 2, 0

    # g1mb1_ic512ih7iw7_oc512oh7ow7_kh3kw3_sh1sw1_ph1pw1_n"resnet18_11"
    # in_channel, in_size, num_filter, kernel, stride, padding = 512, 7, 512, 3, 1, 1

    out_size = (in_size + 2 * padding - kernel) // stride + 1
    vhw_candidates = factors(out_size)
    vc_candidates = factors(num_filter)
    ba_candidates = factors(in_size)
    bc_candidates = factors(kernel)

    with open('report/conv_search.txt', 'a') as report:
        while True:
            vhw_idx = np.random.randint(0, len(vhw_candidates), size=(2,))
            vc_idx = np.random.randint(0, len(vc_candidates))
            ba_idx = np.random.randint(0, len(ba_candidates))
            bc_idx = np.random.randint(0, len(bc_candidates))

            sch = SpatialPack(vh=vhw_candidates[vhw_idx[0]], vw=vhw_candidates[vhw_idx[1]],
                              vc=vc_candidates[vc_idx], ba=ba_candidates[ba_idx], bc=bc_candidates[bc_idx],
                              unroll=bool(np.random.randint(0, 2)))
            try:
                print("Trying " + str(sch))
                cost = verify_conv2d_nchw_all(sch, 1, in_channel, in_size, num_filter, kernel, stride, padding)
                data_cost, kernel_cost, conv_cost = verify_conv2d_nchw(sch, 1, in_channel, in_size, num_filter, kernel, stride, padding)
                print("Successful try with %s, conv total time = %f" % (str(sch), cost))

                cost_all.append(cost)
                cost_data.append(data_cost)
                cost_kernel.append(kernel_cost)
                cost_conv.append(conv_cost)
                schedules.append(sch)

                if len(cost_conv) >= 20:
                    idx = np.argmin(cost_conv)
                    report.write('%s\tall=%f\tconv=%f\tdata=%f\tkernel=%f\n' %
                                 (str(sch), cost_all[idx], cost_conv[idx], cost_data[idx], cost_kernel[idx]))
                    report.flush()
                    cost_all = []
                    cost_conv = []
                    cost_kernel = []
                    cost_data = []
            except Exception as e:
                print(e)
Beispiel #6
0
from __future__ import absolute_import as _abs
import tvm
from tvm import target as _target
from topi import tag
from topi.nn.conv2d import conv2d, _get_schedule
from topi.nn.conv2d import SpatialPack, Im2ColPack
from topi.nn.conv2d import _WORKLOADS, _SCH_TO_DECL_FUNC
from topi.nn.conv2d import _get_workload
from topi.nn.util import infer_pad, infer_stride
from topi import generic

_SCHEDULES = [
    # float32 imagenet
    SpatialPack(vh=28, vw=8, vc=16, ba=224, bc=1, unroll=True),
    SpatialPack(vh=7, vw=1, vc=16, ba=64, bc=4, unroll=True),
    SpatialPack(vh=4, vw=7, vc=16, ba=14, bc=1, unroll=True),
    SpatialPack(vh=2, vw=7, vc=16, ba=56, bc=1, unroll=True),
    SpatialPack(vh=1, vw=14, vc=32, ba=2, bc=1, unroll=False),
    SpatialPack(vh=1, vw=7, vc=16, ba=2, bc=1, unroll=False),
    SpatialPack(vh=7, vw=2, vc=16, ba=7, bc=1, unroll=False),
    SpatialPack(vh=1, vw=7, vc=32, ba=28, bc=1, unroll=False),
    SpatialPack(vh=1, vw=7, vc=16, ba=14, bc=1, unroll=True),
    SpatialPack(vh=1, vw=7, vc=32, ba=1, bc=1, unroll=False),
    SpatialPack(vh=1, vw=7, vc=32, ba=7, bc=1, unroll=False),
    SpatialPack(vh=1, vw=7, vc=8, ba=1, bc=1, unroll=True),

    # float32 mobilenet
    # TODO: need to search for best performance
    SpatialPack(2, 2, 4, 28, 1, True),
    SpatialPack(1, 4, 8, 14, 1, False),
    SpatialPack(1, 2, 16, 8, 1, True),