Example #1
0
def load3d_set_dim(tensor_fmap, kernel, stride, pad):
    fmap_n, fmap_c1, fmap_h, fmap_w, fmap_c0 = tensor_fmap.shape
    filter_h, filter_w = kernel
    # calculate the tiling factor.
    ho = (fmap_h + pad[0] + pad[1] - filter_h) // (stride[0]) + 1
    wo = (fmap_w + pad[2] + pad[3] - filter_w) // (stride[1]) + 1
    mode = True
    if (ho.value * wo.value) % 16 == 0:
        h_cut = (ho * wo) // 16
        if has_pad(pad):
            mode = False
    else:
        h_cut = (fmap_n * ho * wo) // 16
        mode = False
    co_cut = filter_h * filter_w

    key = ((fmap_n, fmap_c1, fmap_h, fmap_w, fmap_c0), kernel, stride, pad)

    set_dims = ct_util.set_dims_by_key(str(key), load3d_set_dim_map)
    if set_dims == '':
        dims = ()
        if mode and fmap_n.value > 1:
            dims += ((1, 0), )
        dims += ((h_cut, 0), (co_cut, 0))
        return ct_util.set_dims(dims), str(key)
    return set_dims, str(key)
Example #2
0
def maxpool_with_argmax_set_dim_func(data, kernel, stride, pad):
    """set dim info for attr"""
    key = []
    key.append(tuple(data.shape))
    key.append(tuple(kernel))
    key.append(tuple(stride))
    if isinstance(pad, list):
        pad = tuple(pad)
    elif isinstance(pad, str):
        pad = pad.upper()
    key.append(pad)
    key.append(data.dtype)
    hash_key = str(tuple(key))

    global attr_map_v2
    default_attr_map = get_attrs()
    attr_map_v2.clear()
    for k, v in default_attr_map.items():
        attr_map_v2[k] = v
    if hash_key in maxpool_with_argmax_set_attr_map.keys():
        for k, v in maxpool_with_argmax_set_attr_map[hash_key].items():
            attr_map_v2[k] = v

    if hash_key in maxpool_with_argmax_set_dim_map.keys():
        return ct_util.set_dims(
            maxpool_with_argmax_set_dim_map[hash_key]), hash_key
    return "", hash_key
Example #3
0
def avgpool_set_dim_func(a_value, kernel, stride, pad):
    """set dim info to attr with avgpool_set_dim_map"""
    avgpool_set_dim_map = {
        str(((1, 1, 16, 16, 16), (4, 4), (3, 3), 'VALID', 'float16')):
        ((16, 1), (20, 1), (5, 1)),
        str(((1, 1, 16, 16, 16), (4, 4), (3, 3), (0, 0, 0, 0), 'float16')):
        ((16, 1), (20, 1), (5, 1)),
        str(((10, 3, 16, 16, 16), (4, 4), (3, 3), (0, 0, 0, 0), 'float16')):
        ((2, 2), (3, 3), (16, 16), (5, 5), (5, 5)),
        str(((1, 2, 16, 16, 16), (4, 4), (3, 3), (1, 1, 1, 1), 'float16')):
        ((1, 1), (16, 16), (19, 19)),
    }
    key = []
    key.append(tuple(get_shape(a_value)))
    key.append(kernel)
    key.append(stride)
    if isinstance(pad, list):
        pad = tuple(pad)
    key.append(pad)
    key.append(a_value.dtype)
    hash_key = str(tuple(key))

    if hash_key in avgpool_set_dim_map.keys():
        return ct_util.set_dims(avgpool_set_dim_map[hash_key]), hash_key
    return "", hash_key
Example #4
0
def maxpool_set_dim_func(data, kernel, stride, pad):
    """Set dim info with maxpool_set_dim_map."""
    key = []
    key.append(tuple(data.shape))
    key.append(kernel)
    key.append(stride)
    key.append(pad)
    key.append(data.dtype)
    hash_key = str(tuple(key))

    global attr_map
    default_attr_map = {
        "pragma_reschedule": 1,
        "pragma_reorder_schedule": True,
        "pragma_opt_for_davinci": 1,
        "pragma_disable_loop_reversal": 1,
        "loop_partition_unroll": False,
    }
    attr_map.clear()
    for k, v in default_attr_map.items():
        attr_map[k] = v
    if hash_key in maxpool_set_attr_map.keys():
        for k, v in maxpool_set_attr_map[hash_key].items():
            attr_map[k] = v

    if hash_key in maxpool_set_dim_map.keys():
        return ct_util.set_dims(maxpool_set_dim_map[hash_key]), hash_key
    return "", hash_key
Example #5
0
def gen_kernel_matmul_cube(op_desc: MatmulCubeDesc,
                           _,
                           index_table,
                           config: MatmulCubeConfig = None,
                           idx=None,
                           gen_tiling_spaces=False):
    """Compile kernel module for matmul_cube"""
    if index_table is not None:
        raise RuntimeError('index_table should be none')
    kernel_name = "matmul_cube_poly"
    if idx is not None:
        kernel_name += str(idx)
    if config is None:
        attrs = {'dim': ""}
    else:
        tiling_param = []
        for _ in range(len(op_desc.x_shape) - 2):
            tiling_param.append((1, 1))
        if config.n_l1 > 0:
            tiling_param.append((config.n_l1, config.n_l0))
        if config.m_l1 > 0:
            tiling_param.append((config.m_l1, config.m_l0))
        tiling_param.extend([(16, 16), (16, 16), (config.k_l1, config.k_l0)])
        dim_info = ct_util.set_dims(tuple(tiling_param))
        attrs = {'dim': dim_info, 'bypass': config.bypass}
    return matmul_run.matmul_compile(op_desc.x_shape, op_desc.y_shape,
                                     op_desc.bias, op_desc.left_format,
                                     op_desc.right_format, op_desc.out_format,
                                     op_desc.adj_x, op_desc.adj_y,
                                     op_desc.dtype, op_desc.out_dtype,
                                     kernel_name, attrs, gen_tiling_spaces)
Example #6
0
def set_dim_func_bng3_(*args):
    """set dim for op bn_grad_3"""
    shape = tuple(get_shape(args[0]))
    dtype = args[0].dtype
    hash_key = str((shape, dtype))
    dim_info = set_dim_map_bng3_.get(hash_key, "")
    return ct_util.set_dims(dim_info), hash_key
Example #7
0
def matmul_set_dim(mat_a, mat_b, b, out_dtype, left_format, right_format,
                   output_format, adj_x, adj_y):
    """
    Set dimension info for each matrix 
    
    Returns:
        Dim type, String type dimension info
    """
    shape_a = mat_a.shape[1:5] if len(mat_a.shape) == 5 else mat_a.shape
    shape_b = mat_b.shape[1:5] if len(mat_b.shape) == 5 else mat_b.shape
    bias = 0 if b is None else 1
    key = ()

    key += (tuple(shape_a), tuple(shape_b), bias, left_format, right_format,
            output_format, adj_x, adj_y, mat_a.dtype)
    hash_key = str(key)
    if hash_key in matmul_set_dim_map:
        configs = matmul_set_dim_map.get(hash_key)
        if isinstance(configs, tuple):
            tiles = configs[0]
        else:
            tiles = configs
        set_dims = ct_util.set_dims(tiles)
        return set_dims, hash_key

    return "", hash_key
Example #8
0
def slice_set_dim_func(data, begin, size):
    """setdim function"""
    shape = get_shape(data)

    key = str((tuple(shape), begin, size, data.dtype))
    if key in slice_set_dim_map.keys():
        return ct_util.set_dims(slice_set_dim_map[key]), key
    else:
        return "", key
Example #9
0
def iou_set_dim_func(anchor_box, ground_truth_box):
    tile_list = []
    if anchor_box.shape[0].value > 1:
        tile_list.append((1, 1))
    if anchor_box.shape[1].value > 16:
        tile_list.append((16, 16))
    if len(tile_list) == 0:
        return ""
    return ct_util.set_dims(tuple(tile_list))
Example #10
0
def bn2_set_dim_func(*args):
    """bn2 dim func"""
    hash_key = str((tuple(get_shape(args[0]))))
    if hash_key in DIM_MAP_BN2.keys():
        diminfo = ct_util.set_dims(DIM_MAP_BN2[hash_key])
    else:
        diminfo = ""

    return diminfo, hash_key
Example #11
0
def elemwise_mul_ad_set_dim_func(head, a, a2):
    key = []
    key.append(tuple(a.shape))
    key.append(a.dtype)
    hash_key = str(tuple(key))

    if hash_key in elemwise_mul_ad_set_dim_map.keys():
        return ct_util.set_dims(elemwise_mul_ad_set_dim_map[hash_key])
    else:
        return ""
Example #12
0
def erf_ad_set_dim_func(head, x):
    key = []
    key.append(tuple(x.shape))
    key.append(x.dtype)
    hash_key = str(tuple(key))

    if hash_key in erf_ad_set_dim_map.keys():
        return ct_util.set_dims(erf_ad_set_dim_map[hash_key]), hash_key
    else:
        return "", hash_key
Example #13
0
def relu_ad_set_dim_func(head, a):
    """set dim info"""
    key = []
    key.append(tuple(a.shape))
    key.append(a.dtype)
    hash_key = str(tuple(key))

    if hash_key in relu_ad_set_dim_map.keys():
        return ct_util.set_dims(relu_ad_set_dim_map[hash_key]), hash_key
    return "", hash_key
Example #14
0
def elemwise_sum_ad_set_dim_func(a, b):
    """setdim function"""
    key = []
    key.append(tuple(a.shape))
    key.append(a.dtype)
    hash_key = str(tuple(key))

    if hash_key in elemwise_sum_ad_set_dim_map.keys():
        return ct_util.set_dims(elemwise_sum_ad_set_dim_map[hash_key]), hash_key
    else:
        return "", hash_key
Example #15
0
def expand_dims_ad_set_dim_func(head, data, axis):
    key = []
    key.append(tuple(data.shape))
    key.append(axis)
    key.append(data.dtype)
    hash_key = str(tuple(key))

    if hash_key in expand_dims_ad_set_dim_map.keys():
        return ct_util.set_dims(expand_dims_ad_set_dim_map[hash_key]), hash_key
    else:
        return "", hash_key
Example #16
0
def reduce_min_ad_set_dim_func(data, HEAD, axis, keepdims):
    key = []
    key.append(tuple(data.shape))
    key.append(tuple(axis))
    key.append(keepdims)
    hash_key = str(tuple(key))

    if hash_key in reduce_min_ad_set_dim_map.keys():
        return ct_util.set_dims(reduce_min_ad_set_dim_map[hash_key])
    else:
        return ""
Example #17
0
def mul_ad_set_dim_func(head, a, b):
    key = []
    key.append(tuple(a.shape))
    key.append(tuple(b.shape))
    key.append(a.dtype)
    hash_key = str(tuple(key))

    if hash_key in mul_ad_set_dim_map.keys():
        return ct_util.set_dims(mul_ad_set_dim_map[hash_key]), hash_key
    else:
        return "", hash_key
Example #18
0
def set_dim_func(data):
    """dim func."""
    shape = [x.value for x in data.shape]

    hash_key = str((tuple(shape), data.dtype))
    if hash_key in DIM_MAP.keys():
        diminfo = ct_util.set_dims(DIM_MAP[hash_key])
    else:
        diminfo = ""

    return diminfo, hash_key
Example #19
0
def focalloss_grad_set_dim_func(prediction):
    """setdim function"""
    key = []
    key.append(tuple(prediction.shape))
    key.append(prediction.dtype)
    hash_key = str(tuple(key))

    if hash_key in focalloss_grad_set_dim_map.keys():
        return ct_util.set_dims(focalloss_grad_set_dim_map[hash_key]), hash_key
    else:
        return "", hash_key
Example #20
0
def softmax_ad_set_dim_func(head, data, axis):
    """Look up the softmax_ad_set_dim_map, and return hash_value, hash_key."""
    key = []
    key.append(tuple(data.shape))
    key.append(data.dtype)
    key.append(axis)
    hash_key = str(tuple(key))

    if hash_key in softmax_ad_set_dim_map.keys():
        return ct_util.set_dims(softmax_ad_set_dim_map[hash_key]), hash_key
    return "", hash_key
Example #21
0
def bn3_set_dim_func(*args):
    """dim func for fused_bn3"""
    hash_key = str((tuple(get_shape(args[0]))))
    if hash_key in ATTR_MAP_BN3.keys():
        for attr in ATTR_MAP_BN3[hash_key]:
            DEFAULT_ATTR_MAP_BN3[attr[0]] = attr[1]
    if hash_key in DIM_MAP_BN3.keys():
        dim = ct_util.set_dims(DIM_MAP_BN3[hash_key])
    else:
        dim = ""
    return dim, hash_key
Example #22
0
def test_single_file(input_file, use_custom, poly=False):
    with open(input_file, 'r') as f:
        desc = f.read()
        attrs = {}
        if use_custom:
            attrs["dim"] = custom_tiling.set_dims(((4, 1), (4, 1)))
        use_poly = _add_composite_attrs(desc, attrs, poly)
        flag = get_result(desc, use_poly, attrs)
        if flag:
            logging.info("Run Pass!")
        else:
            logging.info("Precision Error")
Example #23
0
def squeeze_ad_set_dim_func(head, data, axis):
    """Lookup squeeze_ad_set_dim_map and return the hash_value and hash_key."""
    key = []
    key.append(tuple(data.shape))
    key.append(axis)
    key.append(data.dtype)

    hash_key = str(tuple(key))

    if hash_key in squeeze_ad_set_dim_map.keys():
        return ct_util.set_dims(squeeze_ad_set_dim_map[hash_key]), hash_key
    return "", hash_key
Example #24
0
def focalloss_ad_set_dim_func(head, logits, labels, gamma):
    key = []
    key.append(tuple(logits.shape))
    key.append(logits.dtype)
    key.append(labels.dtype)
    key.append(gamma)
    hash_key = str(tuple(key))

    if hash_key in focalloss_ad_set_dim_map.keys():
        return ct_util.set_dims(focalloss_ad_set_dim_map[hash_key]), hash_key
    else:
        return "", hash_key
Example #25
0
def square_difference_ad_set_dim_func(head, a_up, b_up):
    """Lookup the square_difference_ad_set_dim_map and return hash_value and hash_key."""
    key = []
    key.append(tuple(a_up.shape))
    key.append(tuple(b_up.shape))
    key.append(a_up.dtype)
    hash_key = str(tuple(key))

    if hash_key in square_difference_ad_set_dim_map.keys():
        return ct_util.set_dims(
            square_difference_ad_set_dim_map[hash_key]), hash_key
    return "", hash_key
Example #26
0
def logsoftmaxgrad_set_dim_func(Y, dY, axis):
    shape = [x.value for x in Y.shape]

    if axis < 0:
        axis += len(shape)

    key = str((tuple(shape), axis, dY.dtype))

    if key in logsoftmaxgrad_set_dim_map.keys():
        return ct_util.set_dims(logsoftmaxgrad_set_dim_map[key]), key
    else:
        return "", key
Example #27
0
def bn1_set_dim_func(data):
    """bn1 dim func"""
    hash_key = data.dtype
    if hash_key in ATTR_MAP_BN1.keys():
        for attr, value in ATTR_MAP_BN1[hash_key].items():
            DEFAULT_ATTR_MAP_BN1[attr] = value
    hash_key = str((tuple(get_shape(data))))
    if hash_key in DIM_MAP_BN1.keys():
        diminfo = ct_util.set_dims(DIM_MAP_BN1[hash_key])
    else:
        diminfo = ""

    return diminfo, hash_key
Example #28
0
def matmul4d_ad_set_dim_func(head, x, y, b, out_dtype, adj_x=False, adj_y=False):
    key = []
    key.append(get_shape(x))
    key.append(get_shape(y))
    key.append(adj_x)
    key.append(adj_y)

    hash_key = str(tuple(key))

    if hash_key in matmul4d_ad_set_dim_map.keys():
        return ct_util.set_dims(matmul4d_ad_set_dim_map[hash_key])
    else:
        return ""
Example #29
0
def gather_v2_set_dim_func(params, indices, axis):
    """set dim info for attr"""
    key = []
    key.append(tuple(params.shape))
    key.append(tuple(indices.shape))
    key.append(axis)
    key.append(params.dtype)
    key.append(indices.dtype)
    hash_key = str(tuple(key))

    if hash_key in gather_v2_set_dim_map.keys():
        return ct_util.set_dims(gather_v2_set_dim_map[hash_key]), hash_key
    return "", hash_key
Example #30
0
def test_single_file(input_file, use_custom):
    with open(input_file, 'r') as f:
        desc = f.read()
        if use_custom:
            attrs = {}
            attrs["dim"] = custom_tiling.set_dims(((4, 1), (4, 1)))
            flag = get_result(desc, attrs)
        else:
            flag = get_result(desc)
        if flag:
            logging.info("Run Pass!")
        else:
            logging.info("Precision Error")