Example #1
0
def expand_logsoftmax(expand_info):
    """LogSoftmax expander"""
    # get op info.
    input_desc = expand_info['input_desc'][0]
    attrs = expand_info['attr']
    axis = None
    for item in attrs:
        if 'axis' in item:
            axis = item['axis']
    graph_builder = builder.GraphBuilder()
    if isinstance(axis, int):
        axis = (axis,)
    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc['shape'], input_desc['data_type'], input_desc['format'])
        graph_scope.set_input(input_x)

        # cal logsoftmax.
        max_x = graph_builder.emit('ReduceMax', [input_x], attrs={'reduce_axis': axis, 'keep_dims': True})
        data_sub = graph_builder.emit('Sub', [input_x, max_x])
        data_exp = graph_builder.emit('Exp', [data_sub])
        data_expsum = graph_builder.emit('ReduceSum', [data_exp], attrs={'reduce_axis': axis, 'keep_dims': True})
        log_expsum = graph_builder.emit('Log', [data_expsum])
        result = graph_builder.emit('Sub', [data_sub, log_expsum])

        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #2
0
def expand_softmax(expand_info):
    """Softmax expander"""
    input_desc = expand_info['input_desc'][0]
    axis = expand_info['attr']['axis']

    graph_builder = builder.GraphBuilder()
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc['shape'],
                                       input_desc['data_type'],
                                       input_desc['format'])
        # cal softmax.
        max_x = graph_builder.emit('ReduceMax', [input_x],
                                   attrs={
                                       'reduce_axis': axis,
                                       'keep_dims': True
                                   })
        data_sub = graph_builder.emit('Sub', [input_x, max_x])
        data_exp = graph_builder.emit('Exp', [data_sub])
        data_expsum = graph_builder.emit('ReduceSum', [data_exp],
                                         attrs={
                                             'reduce_axis': axis,
                                             'keep_dims': True
                                         })
        result = graph_builder.emit('RealDiv', [data_exp, data_expsum])
        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #3
0
def expand_gkdropout(expand_info):
    """GkDropOut expander"""
    # get op info.
    input_desc = expand_info['input_desc'][0]
    maks_desc = expand_info['input_desc'][1]
    keep_prob = expand_info['attr']['keep_prob']

    graph_builder = builder.GraphBuilder()
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc['shape'],
                                       input_desc['data_type'],
                                       input_desc['format'])
        input_mask = graph_builder.tensor(maks_desc['shape'],
                                          maks_desc['data_type'],
                                          maks_desc['format'])
        graph_scope.set_input(input_x, input_mask)
        keep_prob_v = graph_builder.value(input_x.dtype, keep_prob)
        r_keep_prob = graph_builder.value(input_x.dtype, 1.0 / keep_prob)

        if input_mask.dtype != input_x.dtype:
            input_mask = graph_builder.emit('Cast', [input_mask],
                                            attrs={'dst_type': input_x.dtype})
        mask = graph_builder.emit(
            'LessEqual', [input_mask, keep_prob_v])  # output is bool type
        mask = graph_builder.emit('Cast', [mask],
                                  attrs={'dst_type': input_x.dtype})

        # compute result
        result = graph_builder.emit('Mul', [r_keep_prob, input_x])
        result = graph_builder.emit('Mul', [result, mask])
        # set graph output.
        graph_scope.set_output(result, mask)
    graph = graph_builder.get()[0]
    return graph
Example #4
0
def expand_sqrtgrad(expand_info):
    """SqrtGrad expander"""
    # cal formula are:
    # sqrt_grad(x, dout) is dout / (2 * x)

    # get op info.
    input_desc_0 = expand_info['input_desc'][0]
    input_desc_1 = expand_info['input_desc'][1]
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc_0['shape'], input_desc_0['data_type'], input_desc_0['format'])
        input_dout = graph_builder.tensor(input_desc_1['shape'], input_desc_1['data_type'], input_desc_1['format'])
        graph_scope.set_input(input_x, input_dout)

        # cal result
        const_two = graph_builder.value(input_x.dtype, 2)
        dividend = graph_builder.emit('Mul', [input_x, const_two])
        result = graph_builder.emit('RealDiv', [input_dout, dividend])

        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #5
0
def expand_biasaddgrad(expand_info):
    """BiasAddGrad expander"""
    # get op info.
    input_desc_0 = expand_info['input_desc'][0]
    graph_builder = builder.GraphBuilder()
    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc_0['shape'],
                                       input_desc_0['data_type'],
                                       input_desc_0['format'])
        graph_scope.set_input(input_x)
        reduce_axis = ()
        if input_x.data_format == 'NHWC':
            reduce_axis = (0, 1, 2)
        elif input_x.data_format == 'NCHW':
            reduce_axis = (0, 2, 3)
        # Default format shape's length maybe equal 2 to 4, so different shape's length reduce axis are differnet
        else:
            if len(input_x.shape) == 2:
                reduce_axis = (0, )
            elif len(input_x.shape) == 3:
                reduce_axis = (0, 1)
            else:
                reduce_axis = (0, 2, 3)
        result = graph_builder.emit('ReduceSum', [input_x],
                                    attrs={
                                        'reduce_axis': reduce_axis,
                                        'keep_dims': False
                                    })
        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #6
0
def expand_tile(expand_info):
    """Tile expander"""

    # get op info.
    input_desc = expand_info['input_desc'][0]
    attrs = expand_info['attr']
    multiples = None
    for item in attrs:
        if 'multiples' in item:
            multiples = item['multiples']
    output_shape, _, _, shape_compatible = _get_tile_output_shape(input_desc['shape'], multiples)
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc['shape'], input_desc['data_type'], input_desc['format'])
        # create op.
        if shape_compatible:
            result = graph_builder.emit('BroadcastTo', [input_x], attrs={'shape': output_shape})
        else:
            result = graph_builder.emit('Tile', [input_x], attrs={'multiples': multiples})
        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #7
0
def expand_dropoutgrad(expand_info):
    """DropoutGrad expander"""
    # get op info.
    dy_desc = expand_info['input_desc'][0]
    mask_desc = expand_info['input_desc'][1]
    keep_prob = None
    for attr in expand_info['attr']:
        if 'keep_prob' in attr:
            keep_prob = attr['keep_prob']
    if keep_prob is None:
        raise RuntimeError("keep_prob does not exist in attrs.")
    # generate a graph.
    graph_builder = builder.GraphBuilder()
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_dy = graph_builder.tensor(dy_desc['shape'], dy_desc['data_type'],
                                        dy_desc['format'])
        input_mask = graph_builder.tensor(mask_desc['shape'],
                                          mask_desc['data_type'],
                                          mask_desc['format'])
        graph_scope.set_input(input_dy, input_mask)
        r_keep_prob = graph_builder.value(input_dy.dtype, 1.0 / keep_prob,
                                          "DefaultFormat")
        # create op.
        result = graph_builder.emit('Mul', [input_dy, r_keep_prob])
        result = graph_builder.emit('Mul', [result, input_mask])
        # set graph output.
        graph_scope.set_output(result)
    graph = graph_builder.get()[0]
    return graph
Example #8
0
def expand_gkdropout(expand_info):
    """GkDropOut expander"""
    # get op info.
    input_desc = expand_info['input_desc'][0]
    maks_desc = expand_info['input_desc'][1]
    keep_prob = None
    for attr in expand_info['attr']:
        if 'keep_prob' in attr:
            keep_prob = attr['keep_prob']
    if keep_prob is None:
        raise RuntimeError("keep_prob does not exist in attrs.")
    # generate a graph.
    graph_builder = builder.GraphBuilder()
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc['shape'], input_desc['data_type'], input_desc['format'])
        input_mask = graph_builder.tensor(maks_desc['shape'], maks_desc['data_type'], maks_desc['format'])
        graph_scope.set_input(input_x, input_mask)
        keep_prob_v = graph_builder.value(input_x.dtype, keep_prob, "DefaultFormat")
        r_keep_prob = graph_builder.value(input_x.dtype, 1.0 / keep_prob, "DefaultFormat")

        mask = graph_builder.emit('LessEqual', [input_mask, keep_prob_v])
        mask = graph_builder.emit('Cast', [mask], attrs={'dst_type': input_x.dtype})

        # compute result
        result = graph_builder.emit('Mul', [r_keep_prob, input_x])
        result = graph_builder.emit('Mul', [result, mask])
        # set graph output.
        graph_scope.set_output(result, mask)
    graph = graph_builder.get()[0]
    return graph
Example #9
0
def expand_minimumgrad(expand_info):
    """MinimumGrad expander"""
    # get op info.
    input_desc_0 = expand_info['input_desc'][0]
    input_desc_1 = expand_info['input_desc'][1]
    input_desc_2 = expand_info['input_desc'][2]
    attrs = expand_info['attr']
    grad_x = attrs['grad_x'] if 'grad_x' in attrs else True
    grad_y = attrs['grad_y'] if 'grad_y' in attrs else True

    graph_builder = builder.GraphBuilder()
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc_0['shape'], input_desc_0['data_type'], input_desc_0['format'])
        input_y = graph_builder.tensor(input_desc_1['shape'], input_desc_1['data_type'], input_desc_1['format'])
        input_dout = graph_builder.tensor(input_desc_2['shape'], input_desc_2['data_type'], input_desc_2['format'])
        graph_scope.set_input(input_x, input_y, input_dout)
        x_dtype = input_x.dtype

        # cal result
        le_result = graph_builder.emit('LessEqual', [input_x, input_y])
        le_result = graph_builder.emit('Cast', [le_result], attrs={'dst_type': x_dtype})
        dx = graph_builder.emit('Mul', [le_result, input_dout])
        dy = graph_builder.emit('Sub', [input_dout, dx])

        # set graph output according to grad_x and grad_y
        if grad_x and grad_y:
            graph_scope.set_output(dx, dy)
        if grad_x and not grad_y:
            graph_scope.set_output(dx)
        if grad_y and not grad_x:
            graph_scope.set_output(dy)

    graph = graph_builder.get()[0]
    return graph
Example #10
0
def expand_tanhgrad(expand_info):
    """TanhGrad expander"""

    # get op info.
    input_desc_0 = expand_info['input_desc'][0]
    input_desc_1 = expand_info['input_desc'][1]
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_y = graph_builder.tensor(input_desc_0['shape'],
                                       input_desc_0['data_type'],
                                       input_desc_0['format'])
        input_dy = graph_builder.tensor(input_desc_1['shape'],
                                        input_desc_1['data_type'],
                                        input_desc_1['format'])
        const_one = graph_builder.value(input_y.dtype, ONE,
                                        input_y.data_format)
        graph_scope.set_input(input_y, input_dy)

        # cal result
        double_y = graph_builder.emit('Mul', [input_y, input_y])
        one_sub_double_y = graph_builder.emit('Sub', [const_one, double_y])
        result = graph_builder.emit('Mul', [input_dy, one_sub_double_y])

        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #11
0
def expand_gelu(expand_info):
    """Gelu expander"""

    # get op info.
    input_desc = expand_info['input_desc'][0]
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc['shape'],
                                       input_desc['data_type'],
                                       input_desc['format'])
        dtype = input_x.dtype
        if dtype == 'float16':
            input_x = graph_builder.emit('Cast', [input_x],
                                         attrs={'dst_type': 'float32'})

        # cal tanh.
        mul_0 = graph_builder.emit('Mul', [input_x, input_x])
        pow_0 = graph_builder.emit('Mul', [mul_0, input_x])
        const_csvalue = graph_builder.value(pow_0.dtype, CSVALUE,
                                            input_desc['format'])
        mul_1 = graph_builder.emit('Mul', [pow_0, const_csvalue])
        tanh_res = graph_builder.emit('TensorAdd', [input_x, mul_1])

        const_csvalue_a = graph_builder.value(tanh_res.dtype, CSVALUE_A,
                                              input_desc['format'])
        mul_0 = graph_builder.emit('Mul', [tanh_res, const_csvalue_a])

        const_zero = graph_builder.value(mul_0.dtype, 0.0,
                                         input_desc['format'])
        mul_0_min = graph_builder.emit('Minimum', [mul_0, const_zero])
        right_mul = graph_builder.emit('Exp', [mul_0_min])

        mul_0_abs = graph_builder.emit('Abs', [mul_0])
        const_neg_one = graph_builder.value(mul_0_abs.dtype, -1.0,
                                            input_desc['format'])
        mul_0_abs_neg = graph_builder.emit('Mul', [mul_0_abs, const_neg_one])

        mul_0_abs_neg_exp = graph_builder.emit('Exp', [mul_0_abs_neg])

        const_one = graph_builder.value(mul_0_abs_neg_exp.dtype, 1.0,
                                        input_desc['format'])
        mul_0_abs_neg_exp_add = graph_builder.emit(
            'TensorAdd', [mul_0_abs_neg_exp, const_one])
        left_mul = graph_builder.emit('RealDiv',
                                      [input_x, mul_0_abs_neg_exp_add])

        result = graph_builder.emit('Mul', [left_mul, right_mul])
        if dtype == 'float16':
            result = graph_builder.emit('Cast', [result],
                                        attrs={'dst_type': 'float16'})
        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #12
0
def expand_reducemean(expand_info):
    """ReduceMean expander"""

    # get op info.
    input_desc = expand_info['input_desc'][0]
    attrs = expand_info['attr']
    axis = None
    keep_dims = None
    for item in attrs:
        if 'axis' in item:
            axis = item['axis']
        if 'keep_dims' in item:
            keep_dims = item['keep_dims']
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc['shape'],
                                       input_desc['data_type'],
                                       input_desc['format'])
        x_shape = input_x.shape
        graph_scope.set_input(input_x)

        # cal reduce_mean
        # when axis = None, reduce axis are all
        all_shape = 1.0
        real_axis = []
        if not axis:
            for i, shape in enumerate(x_shape):
                real_axis.append(i)
                all_shape *= shape
        else:
            for idx in axis:
                all_shape *= x_shape[idx]

        all_shape_value = graph_builder.value(input_x.dtype, all_shape,
                                              input_x.data_format)

        if not axis:
            sum_x = graph_builder.emit('ReduceSum', [input_x],
                                       attrs={
                                           'reduce_axis': real_axis,
                                           'keep_dims': keep_dims
                                       })
        else:
            sum_x = graph_builder.emit('ReduceSum', [input_x],
                                       attrs={
                                           'reduce_axis': axis,
                                           'keep_dims': keep_dims
                                       })
        result = graph_builder.emit('RealDiv', [sum_x, all_shape_value])

        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #13
0
def expand_fusedadam(expand_info):
    """FusedAdma expander"""
    # get op info.
    input_desc_0 = expand_info['input_desc'][0]
    input_desc_1 = expand_info['input_desc'][1]
    input_desc_2 = expand_info['input_desc'][2]
    input_desc_3 = expand_info['input_desc'][3]
    input_desc_4 = expand_info['input_desc'][4]
    input_desc_5 = expand_info['input_desc'][5]
    input_desc_6 = expand_info['input_desc'][6]
    input_desc_7 = expand_info['input_desc'][7]
    input_desc_8 = expand_info['input_desc'][8]
    input_desc_9 = expand_info['input_desc'][9]
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        beta_1 = graph_builder.tensor(input_desc_0['shape'], input_desc_0['data_type'], input_desc_0['format'])
        one_sub_beta_1 = graph_builder.tensor(input_desc_1['shape'], input_desc_1['data_type'], input_desc_1['format'])
        beta_2 = graph_builder.tensor(input_desc_2['shape'], input_desc_2['data_type'], input_desc_2['format'])
        one_sub_beta_2 = graph_builder.tensor(input_desc_3['shape'], input_desc_3['data_type'], input_desc_3['format'])
        eps = graph_builder.tensor(input_desc_4['shape'], input_desc_4['data_type'], input_desc_4['format'])
        lr = graph_builder.tensor(input_desc_5['shape'], input_desc_5['data_type'], input_desc_5['format'])
        param = graph_builder.tensor(input_desc_6['shape'], input_desc_6['data_type'], input_desc_6['format'])
        m = graph_builder.tensor(input_desc_7['shape'], input_desc_7['data_type'], input_desc_7['format'])
        v = graph_builder.tensor(input_desc_8['shape'], input_desc_8['data_type'], input_desc_8['format'])
        gradient = graph_builder.tensor(input_desc_9['shape'], input_desc_9['data_type'], input_desc_9['format'])
        graph_scope.set_input(beta_1, one_sub_beta_1, beta_2, one_sub_beta_2, eps, lr, param, m, v, gradient)

        # compute result
        beta_1_mul_m = graph_builder.emit('Mul', [beta_1, m])
        one_sub_beta_1_mul_grad = graph_builder.emit('Mul', [one_sub_beta_1, gradient])
        next_m = graph_builder.emit('Add', [beta_1_mul_m, one_sub_beta_1_mul_grad])
        beta_2_mul_v = graph_builder.emit('Mul', [beta_2, v])
        grad_square = graph_builder.emit('Mul', [gradient, gradient])
        one_sub_beta_2_mul_grad_square = graph_builder.emit('Mul', [one_sub_beta_2, grad_square])
        next_v = graph_builder.emit('Add', [beta_2_mul_v, one_sub_beta_2_mul_grad_square])
        sqrt_next_v = graph_builder.emit('Sqrt', [next_v])
        sqrt_next_v_add_eps = graph_builder.emit('Add', [sqrt_next_v, eps])
        update = graph_builder.emit('RealDiv', [next_m, sqrt_next_v_add_eps])
        update_with_lr = graph_builder.emit('Mul', [lr, update])
        next_para = graph_builder.emit('Sub', [param, update_with_lr])

        param_result = graph_builder.emit('InplaceAssign', [param, next_para, next_para], attrs={'fake_output': True})
        param_result = graph_builder.emit('InplaceAssign', [m, next_m, param_result], attrs={'fake_output': True})
        param_result = graph_builder.emit('InplaceAssign', [v, next_v, param_result], attrs={'fake_output': True})

        # set graph output.
        graph_scope.set_output(param_result)

    graph = graph_builder.get()[0]
    return graph
Example #14
0
def expand_gelu(expand_info):
    """Gelu expander"""
    # cal formula are:
    # gelu(x) = 0.5 * x * (1.0 + tanh(y))
    # y = sqrt(2.0 / pi) * (x + 0.044715 * x * x * x)

    # get op info.
    input_desc = expand_info['input_desc'][0]
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc['shape'],
                                       input_desc['data_type'],
                                       input_desc['format'])
        graph_scope.set_input(input_x)
        dtype = input_x.dtype
        if dtype == 'float16':
            input_x = graph_builder.emit('Cast', [input_x],
                                         attrs={'dst_type': 'float32'})

        # cal y
        mul_0 = graph_builder.emit('Mul', [input_x, input_x])
        pow_0 = graph_builder.emit('Mul', [mul_0, input_x])
        const_csvalue = graph_builder.value(pow_0.dtype, CSVALUE,
                                            input_desc['format'])
        mul_1 = graph_builder.emit('Mul', [pow_0, const_csvalue])
        tanh_res = graph_builder.emit('TensorAdd', [input_x, mul_1])
        const_csvalue_sqrt_two_div_pi = graph_builder.value(
            tanh_res.dtype, CSVALUE_SQRT_TWO_DIV_PI, input_desc['format'])
        y = graph_builder.emit('Mul',
                               [tanh_res, const_csvalue_sqrt_two_div_pi])

        # cal gelu(x)
        tanh_y = graph_builder.emit('Tanh', [y])
        const_one = graph_builder.value(tanh_y.dtype, ONE,
                                        input_desc['format'])
        const_half = graph_builder.value(tanh_y.dtype, HALF,
                                         input_desc['format'])
        tanh_y_add_one = graph_builder.emit('TensorAdd', [tanh_y, const_one])
        mul_x = graph_builder.emit('Mul', [input_x, tanh_y_add_one])
        result = graph_builder.emit('Mul', [const_half, mul_x])

        if dtype == 'float16':
            result = graph_builder.emit('Cast', [result],
                                        attrs={'dst_type': 'float16'})
        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #15
0
def expand_softmax(expand_info):
    """Softmax expander"""

    # get op info.
    input_desc = expand_info['input_desc'][0]
    attrs = expand_info['attr']
    axis = None
    for item in attrs:
        if 'axis' in item:
            axis = item['axis']
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc['shape'],
                                       input_desc['data_type'],
                                       input_desc['format'])
        # cal softmax.

        if input_x.dtype == 'float32':
            input_x_cast = graph_builder.emit('Cast', [input_x],
                                              attrs={'dst_type': 'float16'})
            max_x = graph_builder.emit('ReduceMax', [input_x_cast],
                                       attrs={
                                           'reduce_axis': axis,
                                           'keep_dims': True
                                       })
            max_x = graph_builder.emit('Cast', [max_x],
                                       attrs={'dst_type': 'float32'})
        else:
            max_x = graph_builder.emit('ReduceMax', [input_x],
                                       attrs={
                                           'reduce_axis': axis,
                                           'keep_dims': True
                                       })
        data_sub = graph_builder.emit('Sub', [input_x, max_x])
        data_exp = graph_builder.emit('Exp', [data_sub])
        data_expsum = graph_builder.emit('ReduceSum', [data_exp],
                                         attrs={
                                             'reduce_axis': axis,
                                             'keep_dims': True
                                         })
        result = graph_builder.emit('RealDiv', [data_exp, data_expsum])
        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #16
0
def expand_biasadd(expand_info):
    """BiasAdd expander"""

    # get op info.
    input_desc_0 = expand_info['input_desc'][0]
    input_desc_1 = expand_info['input_desc'][1]
    graph_builder = builder.GraphBuilder()
    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc_0['shape'],
                                       input_desc_0['data_type'],
                                       input_desc_0['format'])
        input_y = graph_builder.tensor(input_desc_1['shape'],
                                       input_desc_1['data_type'],
                                       input_desc_1['format'])
        graph_scope.set_input(input_x, input_y)
        if input_x.data_format == "NCHW":
            input_y_expand = graph_builder.emit('ExpandDims', [input_y],
                                                attrs={'axis': 1})
            input_y_expand = graph_builder.emit('ExpandDims', [input_y_expand],
                                                attrs={'axis': 2})
            result = graph_builder.emit('Add', [input_x, input_y_expand])
        elif input_x.data_format == "DefaultFormat":
            if len(input_x.shape) == 2:
                result = graph_builder.emit('Add', [input_x, input_y])
            elif len(input_x.shape) == 3:
                input_y_expand = graph_builder.emit('ExpandDims', [input_y],
                                                    attrs={'axis': 1})
                result = graph_builder.emit('Add', [input_x, input_y_expand])
            else:
                input_y_expand = graph_builder.emit('ExpandDims', [input_y],
                                                    attrs={'axis': 1})
                input_y_expand = graph_builder.emit('ExpandDims',
                                                    [input_y_expand],
                                                    attrs={'axis': 2})
                result = graph_builder.emit('Add', [input_x, input_y_expand])
        else:
            result = graph_builder.emit('Add', [input_x, input_y])

        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #17
0
def expand_square(expand_info):
    """Square expander"""

    # get op info.
    input_desc = expand_info['input_desc'][0]
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc['shape'], input_desc['data_type'], input_desc['format'])
        # create op.
        result = graph_builder.emit('Mul', [input_x, input_x])
        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
def expand_clipbynormnodivsum(expand_info):
    """ClipByNormNoDivSum expander"""

    # get op info.
    input_desc_0 = expand_info['input_desc'][0]
    input_desc_1 = expand_info['input_desc'][1]
    input_desc_2 = expand_info['input_desc'][2]
    input_desc_3 = expand_info['input_desc'][3]
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x0 = graph_builder.tensor(input_desc_0['shape'],
                                        input_desc_0['data_type'],
                                        input_desc_0['format'])
        input_x1 = graph_builder.tensor(input_desc_1['shape'],
                                        input_desc_1['data_type'],
                                        input_desc_1['format'])
        input_x2 = graph_builder.tensor(input_desc_2['shape'],
                                        input_desc_2['data_type'],
                                        input_desc_2['format'])
        input_x3 = graph_builder.tensor(input_desc_3['shape'],
                                        input_desc_3['data_type'],
                                        input_desc_3['format'])
        graph_scope.set_input(input_x0, input_x1, input_x2, input_x3)

        # cal result
        greater_res = graph_builder.emit('Greater', [input_x0, input_x1],
                                         attrs={'fusion': 'SelectGT_000'})
        select_res0 = graph_builder.emit('Select',
                                         [greater_res, input_x0, input_x2],
                                         attrs={'fusion': 'SelectGT_000_end'})
        sqrt_res = graph_builder.emit('Sqrt', [select_res0])
        select_res1 = graph_builder.emit('Select',
                                         [greater_res, sqrt_res, input_x0],
                                         attrs={'fusion': 'SelectGT_000_end'})
        result = graph_builder.emit('Maximum', [select_res1, input_x3])

        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #19
0
    def run(self):
        """
        Expand the operator to a graph.

        `GraphKernelUnsupportedException` would be raised if check failed.
        """
        self._check()
        graph_builder = builder.GraphBuilder()
        with graph_builder.graph_scope(self.name) as graph_scope:
            # transform input_desc to Tensor
            self.inputs = [graph_builder.tensor(inp['shape'], inp['data_type'], inp['format']) for inp in self.inputs]
            graph_scope.set_input(*self.inputs)
            outputs = self._expand(graph_builder)
            if isinstance(outputs, (list, tuple)):
                graph_scope.set_output(*outputs)
            else:
                graph_scope.set_output(outputs)

        graph = graph_builder.get()[0]
        graph.set_processor(self.processor)
        return graph
Example #20
0
def expand_logsoftmaxgrad(expand_info):
    """LogSoftmaxGrad expander"""
    # get op info.
    input_desc_0 = expand_info['input_desc'][0]
    input_desc_1 = expand_info['input_desc'][1]
    attrs = expand_info['attr']
    axis = None
    for item in attrs:
        if 'axis' in item:
            axis = item['axis']
    graph_builder = builder.GraphBuilder()

    if isinstance(axis, int):
        axis = (axis, )
    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_logits = graph_builder.tensor(input_desc_0['shape'],
                                            input_desc_0['data_type'],
                                            input_desc_0['format'])
        input_dy = graph_builder.tensor(input_desc_1['shape'],
                                        input_desc_1['data_type'],
                                        input_desc_1['format'])
        graph_scope.set_input(input_logits, input_dy)

        # cal logsoftmaxgrad.
        softmax = graph_builder.emit('Exp', [input_logits])
        dy_sum = graph_builder.emit('ReduceSum', [input_dy],
                                    attrs={
                                        'reduce_axis': axis,
                                        'keep_dims': True
                                    })
        mul_result = graph_builder.emit('Mul', [softmax, dy_sum])
        result = graph_builder.emit('Sub', [input_dy, mul_result])

        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #21
0
def expand_dropoutgrad(expand_info):
    """DropoutGrad expander"""
    # get op info.
    dy_desc = expand_info['input_desc'][0]
    mask_desc = expand_info['input_desc'][1]
    keep_prob = expand_info['attr']['keep_prob']

    graph_builder = builder.GraphBuilder()
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_dy = graph_builder.tensor(dy_desc['shape'], dy_desc['data_type'],
                                        dy_desc['format'])
        input_mask = graph_builder.tensor(mask_desc['shape'],
                                          mask_desc['data_type'],
                                          mask_desc['format'])
        graph_scope.set_input(input_dy, input_mask)
        r_keep_prob = graph_builder.value(input_dy.dtype, 1.0 / keep_prob)
        # create op.
        result = graph_builder.emit('Mul', [input_dy, r_keep_prob])
        result = graph_builder.emit('Mul', [result, input_mask])
        # set graph output.
        graph_scope.set_output(result)
    graph = graph_builder.get()[0]
    return graph
Example #22
0
def expand_layernorm(expand_info):
    """LayerNorm expander"""

    # get op info.
    input_desc_0 = expand_info['input_desc'][0]
    input_desc_1 = expand_info['input_desc'][1]
    input_desc_2 = expand_info['input_desc'][2]
    attrs = expand_info['attr']
    begin_norm_axis = None
    epsilon = None
    for item in attrs:
        if 'begin_norm_axis' in item:
            begin_norm_axis = item['begin_norm_axis']
        if 'epsilon' in item:
            epsilon = item['epsilon']
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_x = graph_builder.tensor(input_desc_0['shape'],
                                       input_desc_0['data_type'],
                                       input_desc_0['format'])
        input_gamma = graph_builder.tensor(input_desc_1['shape'],
                                           input_desc_1['data_type'],
                                           input_desc_1['format'])
        input_beta = graph_builder.tensor(input_desc_2['shape'],
                                          input_desc_2['data_type'],
                                          input_desc_2['format'])

        # Calculate the scaling ratio of the average
        shape_x = input_desc_0['shape']
        if begin_norm_axis < 0:
            begin_norm_axis += len(shape_x)
        reduce_axis = ()
        for i, _ in enumerate(shape_x):
            if i > begin_norm_axis or i == begin_norm_axis:
                reduce_axis = reduce_axis + (i, )

        reduce_elts = 1.0
        for i in reduce_axis:
            reduce_elts *= shape_x[i]
        mean_cof = 1.0 / reduce_elts
        mean_cof_v = graph_builder.value(input_x.dtype, mean_cof,
                                         input_x.data_format)

        # Calculate mean
        mean_red = graph_builder.emit('ReduceSum', [input_x],
                                      attrs={
                                          'reduce_axis': reduce_axis,
                                          'keep_dims': True
                                      })
        mean = graph_builder.emit('Mul', [mean_red, mean_cof_v])

        # Calculate variance
        variance_sub = graph_builder.emit('Sub', [input_x, mean])
        variance_mul = graph_builder.emit('Mul', [variance_sub, variance_sub])
        variance_red = graph_builder.emit('ReduceSum', [variance_mul],
                                          attrs={
                                              'reduce_axis': reduce_axis,
                                              'keep_dims': True
                                          })
        variance = graph_builder.emit('Mul', [variance_red, mean_cof_v])

        # Calculate normalize
        normalize_sub = graph_builder.emit('Sub', [input_x, mean])
        epsilon_v = graph_builder.value(input_x.dtype, epsilon,
                                        input_x.data_format)
        normalize_add = graph_builder.emit('TensorAdd', [variance, epsilon_v])
        normalize_log = graph_builder.emit('Log', [normalize_add])
        input_y = graph_builder.value(input_x.dtype, -0.5, input_x.data_format)
        normalize_log_mul = graph_builder.emit('Mul', [normalize_log, input_y])
        normalize_exp = graph_builder.emit('Exp', [normalize_log_mul])
        normalize_mul = graph_builder.emit('Mul',
                                           [normalize_sub, normalize_exp])

        # Calculate scale and translate
        scale_mul = graph_builder.emit('Mul', [input_gamma, normalize_mul])
        res = graph_builder.emit('TensorAdd', [scale_mul, input_beta])

        # set graph output.
        graph_scope.set_output(res, mean, variance)

    graph = graph_builder.get()[0]
    return graph
Example #23
0
def expand_layernormgrad(expand_info):
    """LayerNormGrad expander"""
    # get op info.
    x_desc = expand_info['input_desc'][0]
    dy_desc = expand_info['input_desc'][1]
    var_desc = expand_info['input_desc'][2]
    mean_desc = expand_info['input_desc'][3]
    gamma_desc = expand_info['input_desc'][4]
    attrs = expand_info['attr']
    begin_norm_axis = attrs['begin_norm_axis']
    begin_params_axis = attrs['begin_params_axis']
    epsilon = attrs['epsilon'] if 'epsilon' in attrs else 1e-11

    shape_x = x_desc['shape']
    if begin_norm_axis < 0:
        begin_norm_axis += len(shape_x)
    if begin_params_axis < 0:
        begin_params_axis += len(shape_x)
    norm_axis = tuple(range(begin_norm_axis, len(shape_x)))
    param_axis = tuple(range(0, begin_params_axis))
    reduce_size = 1.0
    for i in norm_axis:
        reduce_size *= shape_x[i]

    graph_builder = builder.GraphBuilder()
    with graph_builder.graph_scope('main') as graph_scope:
        # create input tensors.
        x = graph_builder.tensor(x_desc['shape'], x_desc['data_type'],
                                 x_desc['format'])
        dy = graph_builder.tensor(dy_desc['shape'], dy_desc['data_type'],
                                  dy_desc['format'])
        variance = graph_builder.tensor(var_desc['shape'],
                                        var_desc['data_type'],
                                        var_desc['format'])
        mean = graph_builder.tensor(mean_desc['shape'], mean_desc['data_type'],
                                    mean_desc['format'])
        gamma = graph_builder.tensor(gamma_desc['shape'],
                                     gamma_desc['data_type'],
                                     gamma_desc['format'])
        graph_scope.set_input(x, dy, variance, mean, gamma)

        # set some constant val.
        eps = graph_builder.value(x.dtype, epsilon)
        const_one = graph_builder.value(x.dtype, 1.0)
        const_neg_half = graph_builder.value(x.dtype, -0.5)
        const_neg_two = graph_builder.value(x.dtype, -2.0)
        const_two = graph_builder.value(x.dtype, 2.0)
        const_neg_one = graph_builder.value(x.dtype, -1.0)
        mean_cof = graph_builder.value(x.dtype, (1.0 / reduce_size))

        # cal dg db
        var_eps = graph_builder.emit('Add', [variance, eps])
        sqrt_var_eps = graph_builder.emit('Sqrt', [var_eps])
        rsqrt_var_eps = graph_builder.emit('RealDiv',
                                           [const_one, sqrt_var_eps])
        x_sub_mean = graph_builder.emit('Sub', [x, mean])
        x_sub_mean_mul_rsqrt_var_eps = graph_builder.emit(
            'Mul', [rsqrt_var_eps, x_sub_mean])
        dg_mul = graph_builder.emit('Mul', [dy, x_sub_mean_mul_rsqrt_var_eps])
        dg = graph_builder.emit('ReduceSum', [dg_mul],
                                attrs={
                                    'reduce_axis': param_axis,
                                    'keep_dims': False
                                })
        db = graph_builder.emit('ReduceSum', [dy],
                                attrs={
                                    'reduce_axis': param_axis,
                                    'keep_dims': False
                                })

        # cal sum_1
        tmp_var_eps = graph_builder.emit('Mul', [sqrt_var_eps, var_eps])
        r_tmp_var_eps = graph_builder.emit('RealDiv', [const_one, tmp_var_eps])
        x_sub_mean_mul_r_tmp_var_eps = graph_builder.emit(
            'Mul', [x_sub_mean, r_tmp_var_eps])
        dy_mul_gamma = graph_builder.emit('Mul', [dy, gamma])
        tmp_mul = graph_builder.emit(
            'Mul', [dy_mul_gamma, x_sub_mean_mul_r_tmp_var_eps])
        sum_1_mul = graph_builder.emit('Mul', [const_neg_half, tmp_mul])
        sum_1 = graph_builder.emit('ReduceSum', [sum_1_mul],
                                   attrs={
                                       'reduce_axis': norm_axis,
                                       'keep_dims': True
                                   })

        # cal sum_2
        sum_2 = graph_builder.emit('ReduceSum', [dy_mul_gamma],
                                   attrs={
                                       'reduce_axis': norm_axis,
                                       'keep_dims': True
                                   })

        # cal sum_3
        sum_3_mul = graph_builder.emit('Mul', [const_neg_two, x_sub_mean])
        sum_3 = graph_builder.emit('ReduceSum', [sum_3_mul],
                                   attrs={
                                       'reduce_axis': norm_axis,
                                       'keep_dims': True
                                   })

        # cal dx = dx1 + dx2 + dx3
        dx_1 = graph_builder.emit('Mul', [dy_mul_gamma, rsqrt_var_eps])
        sum_1_mul_two = graph_builder.emit('Mul', [sum_1, const_two])
        sum_1_mul_two_tmp = graph_builder.emit('Mul',
                                               [sum_1_mul_two, mean_cof])
        dx_2 = graph_builder.emit('Mul', [sum_1_mul_two_tmp, x_sub_mean])
        neg_rsqrt_var_eps = graph_builder.emit('Mul',
                                               [const_neg_one, rsqrt_var_eps])
        neg_rsqrt_var_eps_mul_sum_2 = graph_builder.emit(
            'Mul', [neg_rsqrt_var_eps, sum_2])
        sum_1_mul_sum_3 = graph_builder.emit('Mul', [sum_1, sum_3])
        mean_cof_mul_sum_1_mul_sum_3 = graph_builder.emit(
            'Mul', [mean_cof, sum_1_mul_sum_3])
        add_tmp = graph_builder.emit(
            'Add', [neg_rsqrt_var_eps_mul_sum_2, mean_cof_mul_sum_1_mul_sum_3])
        dx_3 = graph_builder.emit('Mul', [add_tmp, mean_cof])
        dx_tmp = graph_builder.emit('Add', [dx_1, dx_2])
        dx = graph_builder.emit('Add', [dx_tmp, dx_3])

        # set graph output.
        graph_scope.set_output(dx, dg, db)

    graph = graph_builder.get()[0]
    return graph
Example #24
0
def expand_gelugrad(expand_info):
    """GeLUGrad expander"""
    # cal formula are:
    # gelu_grad(dy, x) is dy * y'
    # y' is 0.5 * (1.0 + tanh(tanh_para)) + 0.5 * x * (1.0 - tanh(tanh_para) * tanh(para)) * mul_right
    # tanh_para is sqrt(2.0 / pi) * (x + 0.044715 * x * x * x)
    # mul_right is sqrt(2.0 / pi) * (1 + 3 * 0.044715 * x * x)

    # get op info.
    input_desc_0 = expand_info['input_desc'][0]
    input_desc_1 = expand_info['input_desc'][1]
    input_desc_2 = expand_info['input_desc'][2]
    graph_builder = builder.GraphBuilder()

    # generate a graph.
    with graph_builder.graph_scope('main') as graph_scope:
        # create tensor input.
        input_dy = graph_builder.tensor(input_desc_0['shape'],
                                        input_desc_0['data_type'],
                                        input_desc_0['format'])
        input_x = graph_builder.tensor(input_desc_1['shape'],
                                       input_desc_1['data_type'],
                                       input_desc_1['format'])
        input_y = graph_builder.tensor(input_desc_2['shape'],
                                       input_desc_2['data_type'],
                                       input_desc_2['format'])
        graph_scope.set_input(input_dy, input_x, input_y)

        # create some const var
        const_csvalue = graph_builder.value(input_dy.dtype, CSVALUE)
        const_csvalue_sqrt_two_div_pi = graph_builder.value(
            input_dy.dtype, CSVALUE_SQRT_TWO_DIV_PI)
        const_csvalue_tri = graph_builder.value(input_dy.dtype, CSVALUE_TRI)
        const_one = graph_builder.value(input_dy.dtype, ONE)
        const_half = graph_builder.value(input_dy.dtype, HALF)

        # cal mul_right
        mul_double = graph_builder.emit('Mul', [input_x, input_x])
        mul_double_mul_tri = graph_builder.emit(
            'Mul', [const_csvalue_tri, mul_double])
        mul_add_one = graph_builder.emit('Add',
                                         [const_one, mul_double_mul_tri])
        mul_right = graph_builder.emit(
            'Mul', [const_csvalue_sqrt_two_div_pi, mul_add_one])

        # cal tanh_para
        mul_triple = graph_builder.emit('Mul', [input_x, mul_double])
        mul_triple_mul_csvalue = graph_builder.emit(
            'Mul', [const_csvalue, mul_triple])
        mul_add_x = graph_builder.emit('Add',
                                       [input_x, mul_triple_mul_csvalue])
        tanh_para = graph_builder.emit(
            'Mul', [const_csvalue_sqrt_two_div_pi, mul_add_x])

        # cal 0.5 * (1.0 + tanh(tahn_para))
        tanh_res = graph_builder.emit('Tanh', [tanh_para])
        tanh_res_add_one = graph_builder.emit('Add', [const_one, tanh_res])
        half_mul_tanh_res_add_one = graph_builder.emit(
            'Mul', [const_half, tanh_res_add_one])

        # cal 0.5 * x * (1.0 - tanh(tanh_para) * tanh(tanh_para)) * mul_right
        tan_res_double = graph_builder.emit('Mul', [tanh_res, tanh_res])
        one_sub_tan_res_double = graph_builder.emit(
            'Sub', [const_one, tan_res_double])
        half_mul_x = graph_builder.emit('Mul', [const_half, input_x])
        mul_tmp = graph_builder.emit('Mul',
                                     [half_mul_x, one_sub_tan_res_double])
        mul_final = graph_builder.emit('Mul', [mul_tmp, mul_right])

        # cal result
        result_tmp = graph_builder.emit('Add',
                                        [half_mul_tanh_res_add_one, mul_final])
        result = graph_builder.emit('Mul', [input_dy, result_tmp])

        # set graph output.
        graph_scope.set_output(result)

    graph = graph_builder.get()[0]
    return graph
Example #25
0
def expand_layernormgrad(expand_info):
    """LayerNormGrad expander"""
    # get op info.
    x_desc = expand_info['input_desc'][0]
    dy_desc = expand_info['input_desc'][1]
    var_desc = expand_info['input_desc'][2]
    mean_desc = expand_info['input_desc'][3]
    gamma_desc = expand_info['input_desc'][4]
    begin_norm_axis = None
    begin_params_axis = None
    epsilon = 1e-11
    for item in expand_info['attr']:
        if 'begin_norm_axis' in item:
            begin_norm_axis = item['begin_norm_axis']
        if 'begin_params_axis' in item:
            begin_params_axis = item['begin_params_axis']
        if 'epsilon' in item:
            epsilon = item['epsilon']

    shape_x = x_desc['shape']
    if begin_norm_axis < 0:
        begin_norm_axis += len(shape_x)
    if begin_params_axis < 0:
        begin_params_axis += len(shape_x)
    norm_axis = tuple(range(begin_norm_axis, len(shape_x)))
    param_axis = tuple(range(0, begin_params_axis))
    reduce_size = 1.0
    for i in norm_axis:
        reduce_size *= shape_x[i]

    graph_builder = builder.GraphBuilder()
    with graph_builder.graph_scope('main') as graph_scope:
        # create input tensors.
        x = graph_builder.tensor(x_desc['shape'], x_desc['data_type'], x_desc['format'])
        dy = graph_builder.tensor(dy_desc['shape'], dy_desc['data_type'], dy_desc['format'])
        variance = graph_builder.tensor(var_desc['shape'], var_desc['data_type'], var_desc['format'])
        mean = graph_builder.tensor(mean_desc['shape'], mean_desc['data_type'], mean_desc['format'])
        gamma = graph_builder.tensor(gamma_desc['shape'], gamma_desc['data_type'], gamma_desc['format'])
        graph_scope.set_input(x, dy, variance, mean, gamma)

        # set some constant val.
        eps = graph_builder.value(x.dtype, epsilon, x.data_format)
        const_one = graph_builder.value(x.dtype, 1.0, x.data_format)
        const_neg_half = graph_builder.value(x.dtype, -0.5, x.data_format)
        const_neg_two = graph_builder.value(x.dtype, -2.0, x.data_format)
        const_two = graph_builder.value(x.dtype, 2.0, x.data_format)
        const_neg_one = graph_builder.value(x.dtype, -1.0, x.data_format)
        mean_cof = graph_builder.value(x.dtype, (1.0 / reduce_size), x.data_format)

        # cal dg db
        # dg = np.sum(dy * np.power(var + epsilon, -0.5) * (x - mean), axis=tuple(param_axis), keepdims=True)
        # db = np.sum(dy, axis=tuple(param_axis), keepdims=True)
        var_eps = graph_builder.emit('TensorAdd', [variance, eps])
        sqrt_var_eps = graph_builder.emit('Sqrt', [var_eps])
        rsqrt_var_eps = graph_builder.emit('RealDiv', [const_one, sqrt_var_eps])
        x_sub_mean = graph_builder.emit('Sub', [x, mean])
        x_sub_mean_mul_rsqrt_var_eps = graph_builder.emit('Mul', [rsqrt_var_eps, x_sub_mean])
        dg_mul = graph_builder.emit('Mul', [dy, x_sub_mean_mul_rsqrt_var_eps])
        dg = graph_builder.emit('ReduceSum', [dg_mul], attrs={'reduce_axis': param_axis, 'keep_dims': False})
        db = graph_builder.emit('ReduceSum', [dy], attrs={'reduce_axis': param_axis, 'keep_dims': False})

        # cal sum_1
        # sum1 = np.sum((-0.5) * dy * gamma * (x - mean) * np.power(var + epsilon, -1.5), axis=tuple(norm_axis),
        #          keepdims=True)
        tmp_var_eps = graph_builder.emit('Mul', [sqrt_var_eps, var_eps])
        r_tmp_var_eps = graph_builder.emit('RealDiv', [const_one, tmp_var_eps])
        x_sub_mean_mul_r_tmp_var_eps = graph_builder.emit('Mul', [x_sub_mean, r_tmp_var_eps])
        dy_mul_gamma = graph_builder.emit('Mul', [dy, gamma])
        tmp_mul = graph_builder.emit('Mul', [dy_mul_gamma, x_sub_mean_mul_r_tmp_var_eps])
        sum_1_mul = graph_builder.emit('Mul', [const_neg_half, tmp_mul])
        sum_1 = graph_builder.emit('ReduceSum', [sum_1_mul], attrs={'reduce_axis': norm_axis, 'keep_dims': True})

        # cal sum_2
        # sum2 = np.sum(dy * gamma, axis=tuple(norm_axis), keepdims=True)
        sum_2 = graph_builder.emit('ReduceSum', [dy_mul_gamma], attrs={'reduce_axis': norm_axis, 'keep_dims': True})

        # cal sum_3
        # sum3 = np.sum(-2.0 * (x - mean), axis=tuple(norm_axis), keepdims=True)
        sum_3_mul = graph_builder.emit('Mul', [const_neg_two, x_sub_mean])
        sum_3 = graph_builder.emit('ReduceSum', [sum_3_mul], attrs={'reduce_axis': norm_axis, 'keep_dims': True})

        # cal dx = dx1 + dx2 + dx3
        # dx1 = dy * gamma * rsqrt_var_eps
        # dx2 = sum1 * 2.0 / mean_cof * x_sub_mean
        # dx3 = (1.0 / mean_cof) * (-1.0 * rsqrt_var_eps * sum2 + 1.0 / mean_cof * sum1 * sum3)
        dx_1 = graph_builder.emit('Mul', [dy_mul_gamma, rsqrt_var_eps])
        sum_1_mul_two = graph_builder.emit('Mul', [sum_1, const_two])
        sum_1_mul_two_tmp = graph_builder.emit('Mul', [sum_1_mul_two, mean_cof])
        dx_2 = graph_builder.emit('Mul', [sum_1_mul_two_tmp, x_sub_mean])
        neg_rsqrt_var_eps = graph_builder.emit('Mul', [const_neg_one, rsqrt_var_eps])
        neg_rsqrt_var_eps_mul_sum_2 = graph_builder.emit('Mul', [neg_rsqrt_var_eps, sum_2])
        sum_1_mul_sum_3 = graph_builder.emit('Mul', [sum_1, sum_3])
        mean_cof_mul_sum_1_mul_sum_3 = graph_builder.emit('Mul', [mean_cof, sum_1_mul_sum_3])
        add_tmp = graph_builder.emit('TensorAdd', [neg_rsqrt_var_eps_mul_sum_2, mean_cof_mul_sum_1_mul_sum_3])
        dx_3 = graph_builder.emit('Mul', [add_tmp, mean_cof])
        dx_tmp = graph_builder.emit('TensorAdd', [dx_1, dx_2])
        dx = graph_builder.emit('TensorAdd', [dx_tmp, dx_3])

        # set graph output.
        graph_scope.set_output(dx, dg, db)

    graph = graph_builder.get()[0]
    return graph