Exemplo n.º 1
0
Arquivo: mean.py Projeto: zhuyawen/akg
def mean_dynamic_tiling_strategy(tensor, axis):
    """custom tiling for mean with dynamic shape"""
    strategy = list()

    inner_most_to_full = True
    resnet_inner_most_axis_pos = 4

    reduce_axis_to_1 = True
    reduce_axis_to_no_iso = False

    multicore_axis_to_1 = True
    resnet_outer_most_axis_pos = 0

    if inner_most_to_full:
        strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                        values="FULL",
                                                        constraints=ct_util.TileConstraint.MAX,
                                                        tensor_pos=resnet_inner_most_axis_pos)
    if reduce_axis_to_1:
        strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                        values=[1 for _ in axis],
                                                        constraints=ct_util.TileConstraint.FACTOR,
                                                        tensor_pos=axis)
    elif reduce_axis_to_no_iso:
        strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                        values=[1 for _ in axis],
                                                        constraints=ct_util.TileConstraint.FORBID_ISOLATE,
                                                        tensor_pos=axis)
    if multicore_axis_to_1:
        strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                        values=1,
                                                        constraints=ct_util.TileConstraint.FACTOR,
                                                        tensor_pos=resnet_outer_most_axis_pos)
    return strategy
Exemplo n.º 2
0
def four2five_tiling_strategy(tensor, input_format, expansion=None):
    """Custom tiling strategy for four2five op."""
    strategy = ct_util.create_template(tensor=tensor,
                                       template=ct_util.TileTemplate.NC1HWC0)
    if input_format == "NHWC" or expansion:
        priority_map = {
            4: 0,
            1: 1,
            3: 2,
            2: 3,
            0: 4
        }  # tile in C0->C1->W->H->N sequence
        for pos, priority in priority_map.items():
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor=tensor,
                    values=priority,
                    constraints=ct_util.TileConstraint.SET_PRIORITY,
                    tensor_pos=pos)[0])
    if expansion:
        strategy.append(
            ct_util.create_constraint_on_tensor(
                tensor=tensor,
                values=expansion,
                constraints=ct_util.TileConstraint.SET_EXPANSION)[0])
    return strategy
Exemplo n.º 3
0
def bng3_tiling_strategy(tensor):
    """Custom tiling strategy for 3rd part of splited fused_batch_norm_grad op"""
    # bn3 input [C1, C0, N, H, W]
    strategy_nc0 = list()
    n_pos = 0
    c0_pos = 4
    c1_pos = 1
    strategy_nc0 += ct_util.create_constraint_on_tensor(
        tensor=tensor,
        values=1,
        constraints=ct_util.TileConstraint.CANDIDATE,
        tensor_pos=n_pos)
    strategy_nc0 += ct_util.create_constraint_on_tensor(
        tensor=tensor,
        values="FULL",
        constraints=ct_util.TileConstraint.MAX,
        tensor_pos=c0_pos)
    strategy_c1 = list()
    strategy_c1 += ct_util.create_constraint_on_tensor(
        tensor=tensor,
        values=1,
        constraints=ct_util.TileConstraint.CANDIDATE,
        tensor_pos=c1_pos)
    strategy_c1 += ct_util.create_constraint_on_tensor(
        tensor=tensor,
        values=NUM_CORE,
        constraints=ct_util.TileConstraint.CANDIDATE,
        tensor_pos=c1_pos)
    strategy = strategy_nc0 + strategy_c1
    return strategy
Exemplo n.º 4
0
def bng1_tiling_strategy(tensor):
    """Custom tiling strategy for first part of splited fused_batch_norm_grad op"""
    # bn1 input [N, C1, C0, H, W]
    batch, _, in_h, in_w, _ = get_shape(tensor)
    batch_pos = 0
    c0_pos = 4
    c1_pos = 1
    strategy = list()
    if batch > 1:
        strategy += ct_util.create_constraint_on_tensor(
            tensor=tensor,
            values=1,
            constraints=ct_util.TileConstraint.CANDIDATE,
            tensor_pos=batch_pos)
    if in_h != 1 or in_w != 1:
        strategy += ct_util.create_constraint_on_tensor(
            tensor=tensor,
            values="FULL",
            constraints=ct_util.TileConstraint.MAX,
            tensor_pos=c0_pos)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=tensor,
            values=1,
            constraints=ct_util.TileConstraint.CANDIDATE,
            tensor_pos=c1_pos)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=tensor,
            values=NUM_CORE,
            constraints=ct_util.TileConstraint.CANDIDATE,
            tensor_pos=c1_pos)
    return strategy
Exemplo n.º 5
0
def onehot_tiling_strategy(tensor, axis):
    """Custom tiling strategy for onehot op."""
    tot_axis = ct_util.create_constraint_on_tensor(tensor=tensor,
                                                   values=0,
                                                   constraints=ct_util.TileConstraint.SET_PRIORITY,
                                                   tensor_pos=axis)
    return tot_axis
Exemplo n.º 6
0
def nms_tiling_strategy(tensor):
    """Custom tiling strategy for nms op"""
    strategy = list()
    tensor_shape = get_shape_from_tensor(tensor)
    for i, _ in enumerate(tensor_shape):
        if i == 0:
            strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                            values=1,
                                                            constraints=ct_util.TileConstraint.FACTOR,
                                                            tensor_pos=i)
        else:
            strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                            values="FULL",
                                                            constraints=ct_util.TileConstraint.MAX,
                                                            tensor_pos=i)
    return strategy
Exemplo n.º 7
0
def bn1_tiling_strategy(tensor):
    """Custom tiling strategy for first part of splited fused_batch_norm op"""
    # bn1 input [N, C1, H, W, C0]
    n_pos = 0
    c0_pos = 4
    strategy = list()
    strategy += ct_util.create_constraint_on_tensor(
        tensor=tensor,
        values=1,
        constraints=ct_util.TileConstraint.FACTOR,
        tensor_pos=n_pos)
    strategy += ct_util.create_constraint_on_tensor(
        tensor=tensor,
        values="FULL",
        constraints=ct_util.TileConstraint.MAX,
        tensor_pos=c0_pos)
    return strategy
Exemplo n.º 8
0
def sparse_sf_ce_with_logits_tiling_strategy(tensor, out_shape):
    """Custom tiling strategy for sparse softmax cross entropy op."""
    strategy = list()
    for i in range(len(out_shape)):
        if i != len(out_shape) - 1:
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor=tensor,
                    values=1,
                    constraints=ct_util.TileConstraint.FACTOR,
                    tensor_pos=i)[0])
        else:
            tot_axis = ct_util.create_constraint_on_tensor(
                tensor=tensor,
                values="FULL",
                constraints=ct_util.TileConstraint.MAX,
                tensor_pos=i)[0]
            strategy.append(tot_axis)
    return strategy
Exemplo n.º 9
0
def Softmax(data, axis, target=utils.CCE):
    """
    Map all element of data to (0,1) and sum to 1.

    Args:
        data (tvm.tensor.Tensor): input.
        axis (int): along which normalization is applied.

    Return:
        tvm.tensor.Tensor, output.
    
    Supported Platforms:
        'Ascend'
    """
    utils.check_shape(data.shape)
    shape = data.shape

    utils.ops_dtype_check(data.dtype, utils.DtypeForDavinci.ALL_FLOAT)
    utils.reduce_axis_check(shape, axis)
    axis = ft_util.refine_reduce_axis(data, axis)

    if isinstance(axis, (list, tuple)):
        if len(axis) != 1:
            raise RuntimeError(
                "Reduce axis for softmax op must be 1-dimension, while current is %d-dimension"
                % (len(axis)))
        axis = axis[0]
    output = softmax_op(data, axis, shape)
    attr_map = {}
    if ds.shape_is_dynamic(data):
        # For shifted loops, should have:
        #     dynamic_shape_bound mod tile_size_prime == 2
        # This aims to ensure that the shift constant is a multiple of tile_size_prime.
        # So the generated IR will not have complicated head and tail for shifted blocks.
        attr_map = {
            "pragma_modshift":
            1,
            "pragma_outerband_need_split":
            1,
            "enable_post_poly_loop_partition":
            False,
            "pragma_disable_whole_component":
            False,
            "dynamic_shape":
            ds.set_dynamic_shape_limit_for_tensor(output, 2048, axis) +
            ds.set_poly_upper_bound_for_tensor(output, 2048, axis),
            "custom_tiling":
            ct.create_constraint_on_tensor(
                tensor=output,
                values=[1 for i, _ in enumerate(shape) if i != axis],
                constraints=ct.TileConstraint.FACTOR,
                tensor_pos=[i for i, _ in enumerate(shape) if i != axis])
        }
    return output, attr_map
Exemplo n.º 10
0
def gather_tiling_strategy(data, axis):
    """Custom tiling strategy for gather op"""
    strategy = list()
    base = 0
    for priority_value, pos in enumerate(range(len(data.shape) - 1, axis, -1)):
        priority_value = priority_value + base
        strategy.append(ct_util.create_constraint_on_tensor(tensor=data,
                                                            values=priority_value,
                                                            constraints=ct_util.TileConstraint.SET_PRIORITY,
                                                            tensor_pos=pos)[0])
    return strategy
Exemplo n.º 11
0
def batch_norm_tiling_strategy_dynamic(tensor):
    """Custom tiling strategy for fused_batch_norm op with dynamic shape."""
    strategy = list()
    forbid_iso = False
    full_tile_reduce = False

    multicore_axis = 0
    c0_axis = 4
    w_axis = 3
    h_axis = 2
    c1_axis = 1

    for i, _ in enumerate(tensor.shape):
        if i in [w_axis, c0_axis]:
            strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                            values="FULL",
                                                            constraints=ct_util.TileConstraint.MAX,
                                                            tensor_pos=i)

        elif i == h_axis and full_tile_reduce:
            strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                            values="FULL",
                                                            constraints=ct_util.TileConstraint.MAX,
                                                            tensor_pos=i)
        elif i == c1_axis and full_tile_reduce:
            strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                            values=4,
                                                            constraints=ct_util.TileConstraint.FACTOR,
                                                            tensor_pos=i)
        elif i == multicore_axis:
            strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                            values=1,
                                                            constraints=ct_util.TileConstraint.FACTOR,
                                                            tensor_pos=i)
        elif forbid_iso:
            strategy += ct_util.create_constraint_on_tensor(tensor=tensor,
                                                            values=1,
                                                            constraints=ct_util.TileConstraint.FORBID_ISOLATE,
                                                            tensor_pos=i)

    return strategy
Exemplo n.º 12
0
def strided_slice_grad_tiling_strategy(tensor, begin, end, strides):
    """Custom tiling strategy for strided slice grad op."""
    strategy = list()
    for i, shp in enumerate(tensor.shape):
        length = end[i] - begin[i]
        if length <= strides[i] or int(shp) % strides[i] != 0:
            strategy += ct_util.create_constraint_on_tensor(
                tensor=tensor,
                values="FULL",
                tensor_pos=i,
                constraints=ct_util.TileConstraint.MAX)
    return strategy
Exemplo n.º 13
0
def proposal_sort_tiling_strategy(tensor, tensor_shape):
    """
    Custom tiling strategy for proposal_sort op
    """
    strategy = list()
    for i, sh in enumerate(tensor_shape):
        if i == 0 and sh > 1:
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor=tensor,
                    values=1,
                    constraints=ct_util.TileConstraint.FACTOR,
                    tensor_pos=0)[0])
        if i == 1 and sh > 4096:
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor=tensor,
                    values=1,
                    constraints=ct_util.TileConstraint.FACTOR,
                    tensor_pos=2)[0])
    return strategy
Exemplo n.º 14
0
def batch_norm_tiling_strategy(tensor, tensor_format):
    """Custom tiling strategy for fused_batch_norm op"""
    if tensor_format == "DefaultFormat":
        return list()
    if tensor_format == "NC1HWC0":
        multi_core_axis = 1
        c0_axis = 4
        dim = 5
    elif tensor_format == "NHWC":
        multi_core_axis = 3
        c0_axis = None
        dim = 4
    else:
        multi_core_axis = 1
        c0_axis = None
        dim = 4

    strategy = list()
    if dim != 4 or get_shape(tensor)[multi_core_axis] != 1:
        strategy += ct_util.create_constraint_on_tensor(
            tensor=tensor,
            values=1,
            constraints=ct_util.TileConstraint.FACTOR,
            tensor_pos=multi_core_axis)
        if c0_axis:
            strategy += ct_util.create_constraint_on_tensor(
                tensor=tensor,
                values="FULL",
                constraints=ct_util.TileConstraint.MAX,
                tensor_pos=c0_axis)

        for i in range(dim):
            strategy += ct_util.create_constraint_on_tensor(
                tensor=tensor,
                values=1,
                constraints=ct_util.TileConstraint.FORBID_ISOLATE,
                tensor_pos=i)
        strategy.append(ct_util.modify_common_constraints(
            value=0.7, constraint=ct_util.TileConstraint.SET_MEM_RATIO))
    return strategy
Exemplo n.º 15
0
def four2five_tiling_strategy_dynamic(tensor, input_format):
    """Custom tiling strategy for four2five op."""
    strategy = list()
    if input_format == "NCHW":
        shape = get_shape(tensor)
        if shape[1] == 1:
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor, 1, ct_util.TileConstraint.FACTOR, 0)[0])
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor, 1, ct_util.TileConstraint.FACTOR, 1)[0])
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor, 1, ct_util.TileConstraint.FACTOR, 2)[0])
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor, 112, ct_util.TileConstraint.FACTOR, 3)[0])
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor, 16, ct_util.TileConstraint.FACTOR, 4)[0])
        elif shape[1] == 128:
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor, 1, ct_util.TileConstraint.FACTOR, 0)[0])
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor, 1, ct_util.TileConstraint.FACTOR, 1)[0])
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor, 1, ct_util.TileConstraint.FACTOR, 2)[0])
            strategy.append(
                ct_util.create_constraint_on_tensor(tensor, "FULL",
                                                    ct_util.TileConstraint.MAX,
                                                    3)[0])
            strategy.append(
                ct_util.create_constraint_on_tensor(
                    tensor, 16, ct_util.TileConstraint.FACTOR, 4)[0])
    return strategy
Exemplo n.º 16
0
def five2four_tiling_strategy(tensor, c_value=None, expansion=None):
    """Custom tiling strategy for five2four op."""
    strategy = list()
    if c_value is None:
        strategy = ct_util.create_template(
            tensor=tensor, template=ct_util.TileTemplate.NC1HWC0)
    elif not shape_is_dynamic(tensor):
        c_value = 16 if c_value < 16 else c_value
        node_n = ct_util.create_constraint_on_tensor(
            tensor=tensor,
            values=1,
            constraints=ct_util.TileConstraint.FACTOR,
            tensor_pos=0)
        node_c1 = ct_util.create_constraint_on_tensor(
            tensor=tensor,
            values="FULL",
            constraints=ct_util.TileConstraint.MAX,
            tensor_pos=1)
        node_c0 = ct_util.create_constraint_on_tensor(
            tensor=tensor,
            values=c_value,
            constraints=ct_util.TileConstraint.FACTOR,
            tensor_pos=4)
        strategy = node_n + node_c1 + node_c0
    if expansion:
        strategy.append(
            ct_util.create_constraint_on_tensor(
                tensor=tensor,
                values=expansion,
                constraints=ct_util.TileConstraint.SET_EXPANSION)[0])
    if shape_is_dynamic(tensor):
        # axis should be full tiled due to cast operator
        strategy.append(
            ct_util.modify_common_constraints(
                value=0.85, constraint=ct_util.TileConstraint.SET_MEM_RATIO))
    return strategy
Exemplo n.º 17
0
def maxpool_with_argmax_dynamic_tensor_strategy(data, im2col, mask):
    """Custom tiling for maxpool with argmax version."""
    _, _, _, _, c0 = data.shape
    strategy = list()
    if data.ndim == 5 and c0.value == 16:
        strategy += ct_util.create_constraint_on_tensor(
            tensor=im2col,
            values=1,
            constraints=ct_util.TileConstraint.FACTOR,
            tensor_pos=0)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=im2col,
            values=1,
            constraints=ct_util.TileConstraint.FACTOR,
            tensor_pos=1)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=im2col,
            values="FULL",
            constraints=ct_util.TileConstraint.MAX,
            tensor_pos=2)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=im2col,
            values="FULL",
            constraints=ct_util.TileConstraint.MAX,
            tensor_pos=3)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=im2col,
            values=1,
            constraints=ct_util.TileConstraint.FACTOR,
            tensor_pos=4)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=im2col,
            values="FULL",
            constraints=ct_util.TileConstraint.MAX,
            tensor_pos=5)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=im2col,
            values="FULL",
            constraints=ct_util.TileConstraint.MAX,
            tensor_pos=6)

        strategy += ct_util.create_constraint_on_tensor(
            tensor=mask,
            values=1,
            constraints=ct_util.TileConstraint.FACTOR,
            tensor_pos=0)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=mask,
            values=1,
            constraints=ct_util.TileConstraint.FACTOR,
            tensor_pos=1)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=mask,
            values=1,
            constraints=ct_util.TileConstraint.FACTOR,
            tensor_pos=2)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=mask,
            values=1,
            constraints=ct_util.TileConstraint.FACTOR,
            tensor_pos=3)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=mask,
            values="FULL",
            constraints=ct_util.TileConstraint.MAX,
            tensor_pos=4)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=mask,
            values="FULL",
            constraints=ct_util.TileConstraint.MAX,
            tensor_pos=5)
        strategy += ct_util.create_constraint_on_tensor(
            tensor=mask,
            values="FULL",
            constraints=ct_util.TileConstraint.MAX,
            tensor_pos=6)
    return strategy