Exemplo n.º 1
0
def check_and_config_para(filter, out_backprop, y, input_size, strides,
                          pads, dilations, data_format, kernel_name):
    ori_shape_filters = filter.get("ori_shape")
    ori_shape_out_backprop = out_backprop.get("ori_shape")
    ori_range_filters = filter.get("range")
    ori_range_out_backprop = out_backprop.get("range")
    ori_range_y = y.get("range")
    filter_dtype = filter.get("dtype")
    out_backprop_dtype = out_backprop.get("dtype")
    res_dtype = y.get("dtype")
    ori_format_filters = filter.get("ori_format")
    ori_format_out_backprop = out_backprop.get("ori_format")

    if len(strides) == 4:
        h_index = data_format.find('H')
        w_index = data_format.find('W')
        strides = [strides[h_index], strides[w_index]]

    shape_filters = comm.get_filter_shape(
        ori_format_filters, ori_shape_filters
    )
    shape_out_backprop = comm.get_shape_out_backprop(
        ori_format_out_backprop, ori_shape_out_backprop)
    dilations = comm.get_shape_dilation(data_format, dilations)
    input_size = get_input_size(y)
    dynamic_mode = config_dynamic_para(shape_out_backprop)
    dx_shape, dedy, filter_frac, input_size, shape_out_backprop, var_map = \
        config_placeholder(
            shape_out_backprop, shape_filters, input_size,
            filter_dtype, out_backprop_dtype,
            ori_range_filters, ori_range_out_backprop, ori_range_y,
            dynamic_mode)

    shape_filter, shape_out_backprop, input_sizes, strides, pads, \
        dilations, filter_dtype, out_backprop_dtype, res_dtype, kernel_name \
        = comm.check_conv2dbp_input_params(shape_filters, shape_out_backprop,
                                           input_size, strides, pads,
                                           dilations, filter_dtype,
                                           out_backprop_dtype, res_dtype,
                                           kernel_name, None, dynamic_mode)

    return dx_shape, dedy, filter_frac, input_size, shape_filter, shape_out_backprop, \
           strides, pads, dilations, res_dtype, var_map, dynamic_mode, \
               ori_range_out_backprop, ori_range_y
Exemplo n.º 2
0
def conv2d_backprop_input_d(
        filter,  # pylint: disable=W0622,C0103,R0913,R0914
        out_backprop,
        y,
        input_size,
        strides,
        pads,
        dilations=(1, 1, 1, 1),
        groups=None,
        data_format="NHWC",
        kernel_name="conv2d_backprop_input"):
    """
    algorithm: conv2d_backprop_input

    Parameters
    ----------
    filter: dict with keys(shape and dtype)
            input weight tensor

    out_backprop: dict with keys(shape and dtype)
                  The shape of gradients.

    y: dict with keys(shape and dtype)
       conv2d_backprop_input output tensor, dtype must be assigned

    input_size: The shape of feature map.
                 4-D with shape [batch, channels, height, weight].

    strides: tuple/list of 4 integers
             filter move stride

    pads: tuple/list of 4 integers
             [pad_top, pad_bottom, pad_left, pad_right]

    dilations: tuple/list of 4 integers
               filter expand size of dilated conv2d_backprop_input
    groups: int
            param for group conv2d_backprop_input

    data_format: str
            An optional string from: "NHWC", "NCHW". Defaults to "NHWC".
            Specify the data format of the input and output data.

    kernel_name: str
                 kernel name, default value is "conv2d_backprop_input"

    Returns
    -------
    None
    """

    ori_shape_filters = filter.get("ori_shape")
    ori_shape_out_backprop = out_backprop.get("ori_shape")
    ori_shape_res = y.get("ori_shape")

    filters_dtype = filter.get("dtype")
    out_backprop_dtype = out_backprop.get("dtype")
    res_dtype = y.get("dtype")

    ori_format_filters = filter.get("ori_format")
    ori_format_out_backprop = out_backprop.get("ori_format")
    ori_format_res = y.get("ori_format")
    if list(input_size) != list(ori_shape_res):
        dict_args = {}
        dict_args['errCode'] = "E65007"
        dict_args['param1'] = "input_size"
        dict_args['param2'] = "ori_shape of y"
        dict_args['actual_value'] = "{}, {}". \
            format(input_size, ori_shape_res)
        raise RuntimeError(dict_args, err_man.get_error_message(dict_args))
    util.check_kernel_name(kernel_name)
    util.check_shape_rule(ori_shape_filters, CONV_BACKPROP_SHAPE_DIM,
                          CONV_BACKPROP_SHAPE_DIM, DEFAULT_MAX_SHAPE_NUM)
    util.check_shape_rule(ori_shape_out_backprop, CONV_BACKPROP_SHAPE_DIM,
                          CONV_BACKPROP_SHAPE_DIM, DEFAULT_MAX_SHAPE_NUM)
    util.check_shape_rule(input_size, CONV_BACKPROP_SHAPE_DIM,
                          CONV_BACKPROP_SHAPE_DIM, DEFAULT_MAX_SHAPE_NUM)
    util.check_shape_rule(ori_shape_res, CONV_BACKPROP_SHAPE_DIM,
                          CONV_BACKPROP_SHAPE_DIM, DEFAULT_MAX_SHAPE_NUM)
    util.check_shape_rule(dilations, CONV_BACKPROP_SHAPE_DIM,
                          CONV_BACKPROP_SHAPE_DIM, DEFAULT_MAX_SHAPE_NUM)

    if len(strides) == 4:
        h_index = data_format.find('H')
        w_index = data_format.find('W')
        strides = [strides[h_index], strides[w_index]]

    shape_filters = comm.get_filter_shape(ori_format_filters,
                                          ori_shape_filters)

    shape_out_backprop = comm.get_shape_out_backprop(ori_format_out_backprop,
                                                     ori_shape_out_backprop)

    shape_res = comm.get_shape_res(ori_format_res, ori_shape_res)

    dilations = comm.get_shape_dilation(data_format, dilations)

    conv2d_backprop_input_cce(shape_filters, shape_out_backprop, shape_res,
                              strides, pads, dilations, filters_dtype,
                              out_backprop_dtype, res_dtype, kernel_name)
Exemplo n.º 3
0
def conv2d_backprop_input_d_compute(  # pylint: disable=C0103,W0622,R0913,R0914
        filter,
        out_backprop,
        y,
        input_size,
        strides,
        pads,
        dilations=(1, 1, 1, 1),
        groups=None,
        data_format="NHWC",  # pylint: disable=W0613
        kernel_name="conv2d_backprop_input"):
    """
    used for fusion
    Parameters
    ----------
    filter: Tensor
            input weight tensor

    out_backprop: Tensor
                  conv2d output gradients tenosr.

    y: dict with keys(shape and dtype)
       conv2d_backprop_input output tensor, dtype must be assigned

    input_size: The shape of feature map.
                 4-D with shape [batch, channels, height, weight].

    strides: tuple/list of 4 integers
             filter move stride

    pads: tuple/list of 4 integers
             [pad_top, pad_bottom, pad_left, pad_right]

    dilations: tuple/list of 4 integers
               filter expand size of dilated conv2d_backprop_input
    groups: int
            param for group conv2d_backprop_input
    data_format: str
            An optional string from: "NHWC", "NCHW". Defaults to "NHWC".
            Specify the data format of the input and output data.
    kernel_name: str
                 kernel name, default value is "conv2d_backprop_input"

    Returns
    -------
    Tensor of conv2d_backprop_input
    """

    ori_shape_filters = [i.value for i in filter.op.attrs["ori_shape"]]
    ori_shape_out_backprop = \
        [i.value for i in out_backprop.op.attrs["ori_shape"]]
    ori_shape_res = [i for i in y["ori_shape"]]

    filters_dtype = filter.dtype
    out_backprop_dtype = out_backprop.dtype
    res_dtype = y["dtype"]

    ori_format_filters = filter.op.attrs["ori_format"]
    ori_format_out_backprop = out_backprop.op.attrs["ori_format"]
    ori_format_res = y["ori_format"]
    if list(input_size) != list(ori_shape_res):
        dict_args = {}
        dict_args['errCode'] = "E65007"
        dict_args['param1'] = "input_size"
        dict_args['param2'] = "ori_shape of y"
        dict_args['actual_value'] = "{}, {}". \
            format(input_size, ori_shape_res)
        raise RuntimeError(dict_args, err_man.get_error_message(dict_args))
    if len(strides) == 4:
        h_index = data_format.find('H')
        w_index = data_format.find('W')
        strides = [strides[h_index], strides[w_index]]

    shape_filters = comm.get_filter_shape(ori_format_filters,
                                          ori_shape_filters)
    shape_out_backprop = comm.get_shape_out_backprop(ori_format_out_backprop,
                                                     ori_shape_out_backprop)
    shape_res = comm.get_shape_res(ori_format_res, ori_shape_res)
    dilations = comm.get_shape_dilation(ori_format_out_backprop, dilations)

    comm.check_conv2dbp_input_params(shape_filters, shape_out_backprop,
                                     shape_res, strides, pads, dilations,
                                     filters_dtype, out_backprop_dtype,
                                     res_dtype, kernel_name)

    pads = comm.get_padlist(pads, shape_res, strides, shape_filters, dilations)

    res = te.lang.cce.conv2d_backprop_input_compute(filter,
                                                    out_backprop,
                                                    shape_filters,
                                                    shape_res,
                                                    strides,
                                                    pads,
                                                    dilations,
                                                    res_dtype=res_dtype,
                                                    kernel_name=kernel_name)
    return res
Exemplo n.º 4
0
def conv2d_transpose_d(  # pylint: disable=R0913,R0914,W0613,W0622,C0103
        x,
        filter,
        bias,
        offset_w,
        y,
        input_size,
        strides,
        pads,
        dilations=(1, 1, 1, 1),
        groups=None,
        data_format="NHWC",
        output_padding=(0, 0, 0, 0),
        offset_x=0,
        kernel_name="conv2d_transpose_d"):
    """
    algorithm: conv2d_transpose_d

    Parameters
    ----------
    x: dict with keys(shape and dtype)
                  The shape of gradients.

    filter: dict with keys(shape and dtype)
            input filter tensor

    offset_w: the offset for filter

    bias: dict with keys(shape and dtype)
        The shape of bias.

    y: dict with keys(shape and dtype)
       conv2d_transpose_d output tensor, dtype must be assigned

    input_size: The shape of feature map.
                 4-D with shape [batch, channels, height, filter].

    strides: tuple/list of 4 integers
             filter move stride

    pads: tuple/list of 4 integers
             [pad_top, pad_bottom, pad_left, pad_right]

    dilations: tuple/list of 4 integers
               filter expand size of dilated conv2d_transpose_d
    groups: int
            param for group conv2d_transpose_d

    data_format: str
            An optional string from: "NHWC", "NCHW". Defaults to "NHWC".
            Specify the data format of the input and output data.

    kernel_name: str
                 kernel name, default value is "conv2d_transpose_d"

    output_padding: The size will be added in the output shape.

    offset_x: the offset for x

    Returns
    -------
    None
    """

    ori_shape_x = x.get("ori_shape")
    ori_shape_filters = filter.get("ori_shape")
    ori_shape_res = y.get("ori_shape")

    x_dtype = x.get("dtype")
    filters_dtype = filter.get("dtype")
    res_dtype = y.get("dtype")

    ori_format_x = x.get("ori_format")
    ori_format_filters = filter.get("ori_format")
    ori_format_res = y.get("ori_format")

    util.check_kernel_name(kernel_name)
    util.check_shape_rule(ori_shape_filters, CONV_BACKPROP_SHAPE_DIM,
                          CONV_BACKPROP_SHAPE_DIM, DEFAULT_MAX_SHAPE_NUM)
    util.check_shape_rule(ori_shape_x, CONV_BACKPROP_SHAPE_DIM,
                          CONV_BACKPROP_SHAPE_DIM, DEFAULT_MAX_SHAPE_NUM)
    util.check_shape_rule(ori_shape_res, CONV_BACKPROP_SHAPE_DIM,
                          CONV_BACKPROP_SHAPE_DIM, DEFAULT_MAX_SHAPE_NUM)
    util.check_shape_rule(dilations, CONV_BACKPROP_SHAPE_DIM,
                          CONV_BACKPROP_SHAPE_DIM, DEFAULT_MAX_SHAPE_NUM)

    if list(input_size) != list(ori_shape_res):
        dict_args = {}
        dict_args['errCode'] = "E65007"
        dict_args['param1'] = "input_size"
        dict_args['param2'] = "ori_shape of y"
        dict_args['actual_value'] = "{}, {}". \
            format(input_size, ori_shape_res)
        raise RuntimeError(dict_args, err_man.get_error_message(dict_args))

    if len(strides) == 4:
        h_index = data_format.find('H')
        w_index = data_format.find('W')
        strides = [strides[h_index], strides[w_index]]

    shape_filters = comm.get_filter_shape(ori_format_filters,
                                          ori_shape_filters)

    shape_x = comm.get_shape_out_backprop(ori_format_x, ori_shape_x)

    shape_res = comm.get_shape_res(ori_format_res, ori_shape_res)

    dilations = comm.get_shape_dilation(ori_format_x, dilations)

    bias_flag = bias is not None
    conv2d_transpose_cce(shape_filters,
                         shape_x,
                         shape_res,
                         strides,
                         pads,
                         dilations=dilations,
                         filter_dtype=filters_dtype,
                         x_dtype=x_dtype,
                         res_dtype=res_dtype,
                         bias=bias_flag,
                         offset_x=offset_x,
                         kernel_name=kernel_name)
Exemplo n.º 5
0
def conv2d_transpose_d_compute(  # pylint: disable=R0913,R0914,W0613,C0103,W0622
        x,
        filter,
        bias,
        offset_w,
        y,
        input_size,
        strides,
        pads,
        dilations=(1, 1, 1, 1),
        groups=None,
        data_format="NHWC",
        output_padding=(0, 0, 0, 0),
        offset_x=0,
        kernel_name="conv2d_transpose_d"):
    """
    used for fusion
    Parameters
    ----------
    x: dict with keys(shape and dtype)
                  The shape of gradients.

    filter: dict with keys(shape and dtype)
            input filter tensor

    offset_w: the offset for filter

    bias: dict with keys(shape and dtype)
        The shape of bias.

    y: dict with keys(shape and dtype)
       conv2d_transpose_d output tensor, dtype must be assigned

    strides: tuple/list of 4 integers
             filter move stride

    pads: tuple/list of 4 integers
             [pad_top, pad_bottom, pad_left, pad_right]

    dilations: tuple/list of 4 integers
               filter expand size of dilated conv2d_transpose_d
    groups: int
            param for group conv2d_transpose_d

    data_format: str
            An optional string from: "NHWC", "NCHW". Defaults to "NHWC".
            Specify the data format of the input and output data.

    kernel_name: str
                 kernel name, default value is "conv2d_transpose_d"

    output_padding: The size will be added in the output shape.

    offset_x: the offset for x

    Returns
    -------
    None
    """
    ori_shape_filter = [i.value for i in filter.op.attrs["ori_shape"]]
    ori_shape_x = [i.value for i in x.op.attrs["ori_shape"]]
    ori_shape_res = [i for i in y["ori_shape"]]

    filter_dtype = filter.dtype
    x_dtype = x.dtype
    res_dtype = y["dtype"]

    ori_format_filter = filter.op.attrs["ori_format"]
    ori_format_x = x.op.attrs["ori_format"]
    ori_format_res = y["ori_format"]

    if list(input_size) != list(ori_shape_res):
        dict_args = {}
        dict_args['errCode'] = "E65007"
        dict_args['param1'] = "input_size"
        dict_args['param2'] = "ori_shape of y"
        dict_args['actual_value'] = "{}, {}". \
            format(input_size, ori_shape_res)
        raise RuntimeError(dict_args, err_man.get_error_message(dict_args))
    if len(strides) == 4:
        h_index = data_format.find('H')
        w_index = data_format.find('W')
        strides = [strides[h_index], strides[w_index]]

    if filter_dtype == 'int8':
        ori_shape_filter = comm.exchange_filter_nc_axis(
            ori_format_filter, ori_shape_filter)

    shape_filter = comm.get_filter_shape(ori_format_filter, ori_shape_filter)
    shape_x = comm.get_shape_out_backprop(ori_format_x, ori_shape_x)
    shape_res = comm.get_shape_res(ori_format_res, ori_shape_res)
    dilations = comm.get_shape_dilation(ori_format_x, dilations)

    comm.check_conv2dbp_input_params(shape_filter, shape_x, shape_res, strides,
                                     pads, dilations, filter_dtype, x_dtype,
                                     res_dtype, kernel_name)

    pads = comm.get_padlist(pads, shape_res, strides, shape_filter, dilations)

    res = te.lang.cce.conv2d_backprop_input_compute(filter,
                                                    x,
                                                    shape_filter,
                                                    shape_res,
                                                    strides,
                                                    pads,
                                                    dilations,
                                                    res_dtype=res_dtype,
                                                    tensor_bias=bias,
                                                    offset_x=offset_x,
                                                    kernel_name=kernel_name)

    return res