Пример #1
0
    def sample_program_configs(self, draw):
        #conv or conv_transpose
        Transpose = draw(st.sampled_from([True, False]))

        #conv param or conv_transpose param
        in_shape = draw(
            st.lists(
                st.integers(
                    min_value=2, max_value=128),
                min_size=3,
                max_size=3))
        in_shape = [draw(st.integers(min_value=1, max_value=4))] + in_shape
        weight_shape = draw(
            st.lists(
                st.integers(
                    min_value=1, max_value=8), min_size=4, max_size=4))
        paddings = draw(
            st.lists(
                st.integers(
                    min_value=0, max_value=2), min_size=2, max_size=2))
        dilations = draw(st.sampled_from([[1, 1], [2, 2]]))
        groups = draw(st.sampled_from([1, 2, in_shape[1]]))
        padding_algorithm = draw(st.sampled_from(["VALID", "SAME"]))
        strides = draw(st.sampled_from([[1, 1], [2, 2]]))
        output_padding = draw(
            st.sampled_from([[], [
                draw(
                    st.integers(
                        min_value=0,
                        max_value=max(strides[0], dilations[0]) - 1)), draw(
                            st.integers(
                                min_value=0,
                                max_value=max(strides[1], dilations[1]) - 1))
            ]]))
        scale_in = draw(st.floats(min_value=0.001, max_value=0.1))
        scale_out = draw(st.floats(min_value=0.001, max_value=0.1))

        #active param
        threshold = draw(st.floats(min_value=0, max_value=1))
        alpha = draw(st.floats(min_value=0, max_value=1))
        scale = draw(st.floats(min_value=0.5, max_value=5))
        offset = draw(st.floats(min_value=0, max_value=1))
        slope = draw(st.floats(min_value=0.7, max_value=0.9))

        conv_out_shape = []
        paddings_, dilations_ = UpdatePaddingAndDilation(
            in_shape, weight_shape, paddings, dilations, groups,
            padding_algorithm, strides)
        self.depthwise = False

        if Transpose:
            assume(in_shape[1] == weight_shape[0])
            assume(in_shape[1] % groups == 0)  #TODO
            if len(output_padding):
                assume(output_padding[0] < max(strides[0], dilations_[0]))
                assume(output_padding[1] < max(strides[1], dilations_[1]))
            conv_out_shape = [in_shape[0], weight_shape[1] * groups]
            oh, ow = ConvTransposeOutputSize(in_shape, weight_shape,
                                             dilations_, paddings_, strides)
            if len(output_padding):
                oh = oh + output_padding[0]
                ow = ow + output_padding[1]
            conv_out_shape = conv_out_shape + [int(oh), int(ow)]
            assume(oh > 0 and ow > 0)
            if len(output_padding):
                conv_output_h = (oh + output_padding[0] + paddings[0] +
                                 paddings[1] -
                                 (dilations[0] *
                                  (weight_shape[2] - 1) + 1)) / strides[0] + 1
                conv_output_w = (oh + output_padding[1] + paddings[0] +
                                 paddings[1] -
                                 (dilations[1] *
                                  (weight_shape[3] - 1) + 1)) / strides[1] + 1
                assume(in_shape[2] == conv_output_h)
                assume(in_shape[3] == conv_output_w)

        else:
            self.depthwise = in_shape[1] == weight_shape[1] and in_shape[
                1] == groups
            assume(in_shape[1] == weight_shape[1] * groups)
            assume(weight_shape[0] % groups == 0)
            conv_out_shape = [in_shape[0], weight_shape[0]]
            oh, ow = ConvOutputSize(in_shape, weight_shape, dilations_,
                                    paddings_, strides)
            conv_out_shape = conv_out_shape + [int(oh), int(ow)]
            assume(oh > 0 and ow > 0)

        #bn param
        epsilon = draw(st.floats(min_value=0.00001, max_value=0.001))
        momentum = draw(st.floats(min_value=0.1, max_value=0.9))

        def generate_scale(*args, **kwargs):
            return np.random.random(
                [conv_out_shape[1]]).astype(np.float32) + 0.5

        def generate_bias(*args, **kwargs):
            return np.random.random([conv_out_shape[1]]).astype(np.float32)

        def generate_mean(*args, **kwargs):
            return np.random.random([conv_out_shape[1]]).astype(np.float32)

        def generate_variance(*args, **kwargs):
            return np.random.random([conv_out_shape[1]]).astype(np.float32)

        conv_type = ""
        conv_attrs = {}
        if Transpose:
            conv_type = "conv2d_transpose"
            conv_attrs = {
                "data_format": 'nchw',
                "dilations": dilations,
                "padding_algorithm": padding_algorithm,
                "groups": groups,
                "paddings": paddings,
                "strides": strides,
                "Scale_in": scale_in,
                "Scale_out": scale_out,
                "output_size": [],
                "output_padding": output_padding
            }
        else:
            conv_type = "conv2d"
            conv_attrs = {
                "data_format": 'nchw',
                "dilations": dilations,
                "padding_algorithm": padding_algorithm,
                "groups": groups,
                "paddings": paddings,
                "strides": strides,
                "Scale_in": scale_in,
                "Scale_out": scale_out
            }

        conv_op = OpConfig(
            type=conv_type,
            inputs={"Input": ["input_data"],
                    "Filter": ["filter_data"]},
            outputs={"Output": ["conv_output_data"]},
            attrs=conv_attrs)

        bn_op = OpConfig(
            type="batch_norm",
            inputs={
                "X": ["conv_output_data"],
                "Scale": ["scale_data"],
                "Bias": ["bias_data"],
                "Mean": ["mean_data"],
                "Variance": ["variance_data"]
            },
            outputs={
                "Y": ["output_data"],
                "MeanOut": ["mean_data"],
                "VarianceOut": ["variance_data"],
                "SavedMean": ["saved_mean"],
                "SavedVariance": ["saved_variance"]
            },
            attrs={
                "is_test": True,
                "trainable_statistics": False,
                "data_layout": "NCHW",
                "use_global_stats": False,
                "epsilon": epsilon,
                "momentum": momentum
            })

        ops = [conv_op, bn_op]
        self.ops = ops
        program_config = ProgramConfig(
            ops=ops,
            weights={
                "filter_data": TensorConfig(shape=weight_shape),
                "scale_data": TensorConfig(data_gen=partial(generate_scale)),
                "bias_data": TensorConfig(data_gen=partial(generate_bias)),
                "mean_data": TensorConfig(data_gen=partial(generate_mean)),
                "variance_data":
                TensorConfig(data_gen=partial(generate_variance)),
            },
            inputs={"input_data": TensorConfig(shape=in_shape)},
            outputs=["output_data"])

        return program_config
Пример #2
0
def sample_program_configs(draw):
    in_shape = draw(
        st.lists(st.integers(min_value=1, max_value=64),
                 min_size=4,
                 max_size=4))
    weight_shape = draw(
        st.lists(st.integers(min_value=1, max_value=8), min_size=4,
                 max_size=4))
    paddings = draw(
        st.sampled_from([[1, 2], [4, 2], [1, 1], [0, 0], [1, 0], [1, 1]]))
    dilations = draw(st.sampled_from([[1, 1], [2, 2]]))
    groups = draw(st.sampled_from([1, 2, in_shape[1]]))
    padding_algorithm = draw(st.sampled_from(["VALID", "SAME"]))
    strides = draw(st.sampled_from([[1, 1], [2, 2]]))
    threshold = draw(st.floats(min_value=0, max_value=1))
    alpha = draw(st.floats(min_value=0, max_value=1))
    scale = draw(st.floats(min_value=0.5, max_value=5))
    offset = draw(st.floats(min_value=0, max_value=1))

    assume(in_shape[1] == weight_shape[1] * groups)
    assume(weight_shape[0] % groups == 0)

    paddings_, dilations_ = UpdatePaddingAndDilation(in_shape, weight_shape,
                                                     paddings, dilations,
                                                     groups, padding_algorithm,
                                                     strides)
    conv_out_shape = [in_shape[0], weight_shape[0]]
    oh, ow = ConvOutputSize(in_shape, weight_shape, dilations_, paddings_,
                            strides)
    conv_out_shape = conv_out_shape + [oh, ow]
    assume(oh > 0 and ow > 0)

    Alpha_shape = []
    mode_data = draw(st.sampled_from(["all", "channel", "element"]))
    if mode_data == "all":
        Alpha_shape = [1]
    elif mode_data == "channel":
        Alpha_shape = [conv_out_shape[1]]
    elif mode_data == "element":
        Alpha_shape = conv_out_shape

    act_type = draw(
        st.sampled_from(['relu', 'relu6', 'leaky_relu', 'hard_swish',
                         'prelu']))

    def generate_act_attrs(act_type_str):
        attrs = {}
        if act_type_str == 'relu6':
            attrs = {"threshold": threshold}
        if act_type_str == 'leaky_relu':
            attrs = {"alpha": alpha}
        if act_type_str == 'hard_swish':
            attrs = {"threshold": threshold, "scale": scale, "offset": offset}
        if act_type_str == "prelu":
            attrs = {"mode": mode_data, "data_format": "NCHW"}
        return attrs

    conv_op = OpConfig(type="conv2d",
                       inputs={
                           "Input": ["input_data"],
                           "Filter": ["weight_data"]
                       },
                       outputs={"Output": ["conv_output_data"]},
                       attrs={
                           "data_format": 'nchw',
                           "dilations": dilations,
                           "padding_algorithm": padding_algorithm,
                           "groups": groups,
                           "paddings": paddings,
                           "strides": strides
                       })
    active_op_input = {}
    inputs_data = {}
    if act_type == "prelu":
        active_op_input = {"X": ["conv_output_data"], "Alpha": ["alpha_data"]}
        inputs_data = {
            "input_data": TensorConfig(shape=in_shape),
            "alpha_data": TensorConfig(shape=Alpha_shape)
        }
    else:
        active_op_input = {"X": ["conv_output_data"]}
        inputs_data = {"input_data": TensorConfig(shape=in_shape)}

    active_op = OpConfig(type=act_type,
                         inputs=active_op_input,
                         outputs={"Out": ["output_data"]},
                         attrs=generate_act_attrs(act_type))

    ops = [conv_op, active_op]
    program_config = ProgramConfig(
        ops=ops,
        weights={"weight_data": TensorConfig(shape=weight_shape)},
        inputs=inputs_data,
        outputs=["output_data"])
    return program_config
Пример #3
0
    def sample_program_configs(self, draw):

        #conv param or conv_transpose param
        in_shape = draw(
            st.lists(
                st.integers(
                    min_value=2, max_value=128),
                min_size=3,
                max_size=3))
        in_shape = [draw(st.integers(min_value=1, max_value=4))] + in_shape
        weight_shape = draw(
            st.lists(
                st.integers(
                    min_value=1, max_value=8), min_size=4, max_size=4))
        paddings = draw(
            st.lists(
                st.integers(
                    min_value=0, max_value=2), min_size=2, max_size=2))
        dilations = draw(st.sampled_from([[1, 1], [2, 2]]))
        groups = draw(st.sampled_from([1, 2, in_shape[1]]))
        padding_algorithm = draw(st.sampled_from(["VALID", "SAME"]))
        strides = draw(st.sampled_from([[1, 1], [2, 2]]))
        output_padding = draw(
            st.sampled_from([[], [
                draw(
                    st.integers(
                        min_value=0,
                        max_value=max(strides[0], dilations[0]) - 1)), draw(
                            st.integers(
                                min_value=0,
                                max_value=max(strides[1], dilations[1]) - 1))
            ]]))
        scale_in = draw(st.floats(min_value=0.001, max_value=0.1))
        scale_out = draw(st.floats(min_value=0.001, max_value=0.1))

        scale = draw(st.floats(min_value=0.5, max_value=5))
        scale_bias = draw(st.floats(min_value=0.0, max_value=1.0))

        conv_out_shape = []
        paddings_, dilations_ = UpdatePaddingAndDilation(
            in_shape, weight_shape, paddings, dilations, groups,
            padding_algorithm, strides)

        self.depthwise = in_shape[1] == weight_shape[1] and in_shape[
            1] == groups
        assume(in_shape[1] == weight_shape[1] * groups)
        assume(weight_shape[0] % groups == 0)
        conv_out_shape = [in_shape[0], weight_shape[0]]
        oh, ow = ConvOutputSize(in_shape, weight_shape, dilations_, paddings_,
                                strides)
        conv_out_shape = conv_out_shape + [oh, ow]
        assume(oh > 0 and ow > 0)
        use_mkldnn = False
        if self.target[0] == "X86":
            use_mkldnn = True
        use_mkldnn = True

        inputs_type = {"Input": ["input_data"], "Filter": ["filter_data"]}
        weights_data = {"filter_data": TensorConfig(shape=weight_shape)}
        if use_mkldnn:
            inputs_type["Bias"] = ["bias_data"]
            weights_data["bias_data"] = TensorConfig(shape=[weight_shape[0]])

        conv_type = "conv2d"
        conv_attrs = {
            "data_format": 'nchw',
            "dilations": dilations,
            "use_mkldnn": use_mkldnn,
            "padding_algorithm": padding_algorithm,
            "groups": groups,
            "paddings": paddings,
            "strides": strides,
            "Scale_in": scale_in,
            "Scale_out": scale_out
        }

        conv_op = OpConfig(
            type=conv_type,
            inputs=inputs_type,
            outputs={"Output": ["conv_output_data"]},
            attrs=conv_attrs)

        scale_op = OpConfig(
            type="scale",
            inputs={"X": ["conv_output_data"]},
            outputs={"Out": ["output_data"]},
            attrs={
                "scale": scale,
                "bias": scale_bias,
                "bias_after_scale": True
            })

        ops = [conv_op, scale_op]
        self.ops = ops
        program_config = ProgramConfig(
            ops=ops,
            weights=weights_data,
            inputs={"input_data": TensorConfig(shape=in_shape)},
            outputs=["output_data"])
        return program_config
Пример #4
0
    def sample_program_configs(self, draw):

        in_shape0 = draw(
            st.lists(st.integers(min_value=1, max_value=128),
                     min_size=3,
                     max_size=3))
        in_shape0 = [draw(st.integers(min_value=1, max_value=4))] + in_shape0
        weight_shape0 = [
            draw(st.integers(min_value=3, max_value=64)), in_shape0[1], 1, 1
        ]
        weight_shape1 = [
            draw(st.integers(min_value=3, max_value=64)), weight_shape0[0], 1,
            1
        ]

        scale_in = draw(st.floats(min_value=0.001, max_value=0.1))
        scale_out = draw(st.floats(min_value=0.001, max_value=0.1))

        padding_algorithm0 = draw(st.sampled_from(["VALID", "SAME"]))
        strides0 = draw(st.sampled_from([[1, 1], [2, 2]]))
        paddings0 = draw(
            st.lists(st.integers(min_value=0, max_value=2),
                     min_size=2,
                     max_size=2))
        dilations0 = draw(st.sampled_from([[1, 1], [2, 2]]))

        assume(in_shape0[1] == weight_shape0[1] * 1)
        mula = in_shape0[1] * (weight_shape1[0] - weight_shape0[0])
        mulb = weight_shape0[0] * weight_shape1[0]
        assume(not (mula <= 0 or mula > mulb))

        paddings_, dilations_ = UpdatePaddingAndDilation(
            in_shape=in_shape0,
            weight_shape=weight_shape0,
            paddings=[0, 0],
            dilations=[1, 1],
            groups=1,
            padding_algorithm="VALID",
            strides=[1, 1])
        out_shape = [in_shape0[0], weight_shape0[0]]
        oh, ow = ConvOutputSize(in_shape=in_shape0,
                                weight_shape=weight_shape0,
                                dilations=dilations_,
                                paddings=paddings_,
                                strides=[1, 1])
        out_shape0 = out_shape + [oh, ow]
        assume(oh > 0 and ow > 0)

        in_shape1 = out_shape0
        paddings_, dilations_ = UpdatePaddingAndDilation(
            in_shape=in_shape1,
            weight_shape=weight_shape1,
            paddings=[0, 0],
            dilations=[1, 1],
            groups=1,
            padding_algorithm="VALID",
            strides=[1, 1])
        out_shape = [in_shape1[0], weight_shape1[0]]
        oh, ow = ConvOutputSize(in_shape=in_shape1,
                                weight_shape=weight_shape1,
                                dilations=dilations_,
                                paddings=paddings_,
                                strides=[1, 1])
        out_shape1 = out_shape + [oh, ow]
        assume(oh > 0 and ow > 0)

        conv0_op = OpConfig(type="conv2d",
                            inputs={
                                "Input": ["input_data"],
                                "Filter": ["weight_data0"]
                            },
                            outputs={"Output": ["conv_output_data"]},
                            attrs={
                                "data_format": 'nchw',
                                "dilations": dilations0,
                                "padding_algorithm": padding_algorithm0,
                                "groups": 1,
                                "Scale_in": scale_in,
                                "Scale_out": scale_out,
                                "paddings": paddings0,
                                "strides": strides0
                            })

        conv1_op = OpConfig(type="conv2d",
                            inputs={
                                "Input": ["conv_output_data"],
                                "Filter": ["weight_data1"]
                            },
                            outputs={"Output": ["output_data"]},
                            attrs={
                                "data_format": 'nchw',
                                "dilations": [1, 1],
                                "padding_algorithm": "VALID",
                                "Scale_in": scale_in,
                                "Scale_out": scale_out,
                                "groups": 1,
                                "paddings": [0, 0],
                                "strides": [1, 1]
                            })

        ops = [conv0_op, conv1_op]
        self.ops = ops
        program_config = ProgramConfig(ops=ops,
                                       weights={
                                           "weight_data0":
                                           TensorConfig(shape=weight_shape0),
                                           "weight_data1":
                                           TensorConfig(shape=weight_shape1)
                                       },
                                       inputs={
                                           "input_data":
                                           TensorConfig(shape=in_shape0),
                                       },
                                       outputs=["output_data"])

        return program_config
def sample_program_configs(draw):
    in_shape = draw(
        st.lists(st.integers(min_value=1, max_value=64),
                 min_size=4,
                 max_size=4))
    weight_shape = draw(
        st.lists(st.integers(min_value=1, max_value=64),
                 min_size=4,
                 max_size=4))
    paddings = draw(st.sampled_from([[1, 2], [4, 2]]))
    dilations = draw(st.sampled_from([[1, 1]]))
    groups = draw(st.sampled_from([1, 2, in_shape[1]]))
    padding_algorithm = draw(st.sampled_from(["VALID", "SAME"]))
    strides = draw(st.sampled_from([[1, 1], [2, 2]]))
    scale = draw(st.floats(min_value=0.5, max_value=5))
    scale_bias = draw(st.floats(min_value=0.0, max_value=1.0))

    assume(in_shape[1] == weight_shape[1] * groups)
    assume(weight_shape[0] % groups == 0)

    paddings_, dilations_ = UpdatePaddingAndDilation(in_shape, weight_shape,
                                                     paddings, dilations,
                                                     groups, padding_algorithm,
                                                     strides)
    out_shape = [in_shape[0], weight_shape[0]]
    oh, ow = ConvOutputSize(in_shape, weight_shape, dilations_, paddings_,
                            strides)
    out_shape = out_shape + [oh, ow]

    assume(oh > 0 and ow > 0)

    conv_op = OpConfig(type="conv2d",
                       inputs={
                           "Input": ["input_data"],
                           "Filter": ["weight_data"],
                           "Bias": ["conv_bias"]
                       },
                       outputs={"Output": ["conv_output_data"]},
                       attrs={
                           "data_format": 'nchw',
                           "dilations": dilations,
                           "padding_algorithm": padding_algorithm,
                           "groups": groups,
                           "paddings": paddings,
                           "strides": strides
                       })

    scale_op = OpConfig(type="scale",
                        inputs={"X": ["conv_output_data"]},
                        outputs={"Out": ["output_data"]},
                        attrs={
                            "scale": scale,
                            "bias": scale_bias,
                            "bias_after_scale": True
                        })

    ops = [conv_op, scale_op]
    program_config = ProgramConfig(
        ops=ops,
        weights={
            "conv_bias": TensorConfig(shape=[weight_shape[0]]),
            "weight_data": TensorConfig(shape=weight_shape)
        },
        inputs={"input_data": TensorConfig(shape=in_shape)},
        outputs=["output_data"])
    return program_config
Пример #6
0
    def sample_program_configs(self, draw):

        #conv param or conv_transpose param
        in_shape = draw(
            st.lists(st.integers(min_value=2, max_value=128),
                     min_size=3,
                     max_size=3))
        in_shape = [draw(st.integers(min_value=1, max_value=4))] + in_shape
        weight_shape = draw(
            st.lists(st.integers(min_value=1, max_value=8),
                     min_size=2,
                     max_size=2))
        weight_shape = weight_shape + [1, 1]
        paddings = draw(
            st.lists(st.integers(min_value=0, max_value=2),
                     min_size=2,
                     max_size=2))
        dilations = draw(st.sampled_from([[1, 1], [2, 2]]))
        groups = draw(st.sampled_from([1, 2, in_shape[1]]))
        padding_algorithm = draw(st.sampled_from(["VALID", "SAME"]))
        strides = draw(st.sampled_from([[1, 1], [2, 2]]))
        output_padding = draw(
            st.sampled_from([[],
                             draw(
                                 st.lists(st.integers(min_value=0,
                                                      max_value=16),
                                          min_size=2,
                                          max_size=2))]))
        scale_in = draw(st.floats(min_value=0.001, max_value=0.1))
        scale_out = draw(st.floats(min_value=0.001, max_value=0.1))

        conv_out_shape = []
        paddings_, dilations_ = UpdatePaddingAndDilation(
            in_shape, weight_shape, paddings, dilations, groups,
            padding_algorithm, strides)

        self.depthwise = in_shape[1] == weight_shape[1] and in_shape[
            1] == groups
        assume(in_shape[1] == weight_shape[1] * groups)
        assume(weight_shape[0] % groups == 0)
        conv_out_shape = [in_shape[0], weight_shape[0]]
        oh, ow = ConvOutputSize(in_shape, weight_shape, dilations_, paddings_,
                                strides)
        conv_out_shape = conv_out_shape + [oh, ow]
        assume(oh > 0 and ow > 0)

        conv_type = "conv2d"
        conv_attrs = {
            "data_format": 'nchw',
            "dilations": dilations,
            "padding_algorithm": padding_algorithm,
            "groups": groups,
            "paddings": paddings,
            "strides": strides,
            "Scale_in": scale_in,
            "Scale_out": scale_out
        }

        conv_op = OpConfig(type=conv_type,
                           inputs={
                               "Input": ["input_data"],
                               "Filter": ["filter_data"]
                           },
                           outputs={"Output": ["conv_output_data"]},
                           attrs=conv_attrs)

        elementwise_add_op = OpConfig(type="elementwise_add",
                                      inputs={
                                          "X": ["add_input_data"],
                                          "Y": ["conv_output_data"]
                                      },
                                      outputs={"Out": ["output_data"]},
                                      attrs={"axis": -1})

        ops = [conv_op, elementwise_add_op]
        self.ops = ops
        program_config = ProgramConfig(
            ops=ops,
            weights={"filter_data": TensorConfig(shape=weight_shape)},
            inputs={
                "input_data": TensorConfig(shape=in_shape),
                "add_input_data": TensorConfig(shape=conv_out_shape)
            },
            outputs=["output_data"])
        return program_config
def sample_program_configs(draw):
    in_shape = draw(
        st.lists(st.integers(min_value=3, max_value=64),
                 min_size=4,
                 max_size=4))
    weight_shape = draw(
        st.lists(st.integers(min_value=1, max_value=64),
                 min_size=2,
                 max_size=2))
    weight_shape = weight_shape + [1, 1]
    paddings = draw(
        st.sampled_from([[1, 2], [4, 2], [1, 1], [0, 0], [1, 0], [1, 1]]))
    dilations = draw(st.sampled_from([[1, 1], [2, 2]]))
    groups = draw(st.sampled_from([1, 2]))
    padding_algorithm = draw(st.sampled_from(["VALID", "SAME"]))
    strides = draw(st.sampled_from([[1, 1], [2, 2], [1, 2], [2, 1]]))
    elementwise_bias_shape = draw(st.sampled_from([[weight_shape[0]], [1]]))

    assume(in_shape[1] == weight_shape[1] * groups)
    assume(weight_shape[0] % groups == 0)

    paddings_, dilations_ = UpdatePaddingAndDilation(in_shape, weight_shape,
                                                     paddings, dilations,
                                                     groups, padding_algorithm,
                                                     strides)
    out_shape = [in_shape[0], weight_shape[0]]
    oh, ow = ConvOutputSize(in_shape, weight_shape, dilations_, paddings_,
                            strides)
    out_shape = out_shape + [oh, ow]

    assume(oh > 0 and ow > 0)

    conv_op = OpConfig(type="conv2d",
                       inputs={
                           "Input": ["input_data"],
                           "Filter": ["weight_data"]
                       },
                       outputs={"Output": ["conv_output_data"]},
                       attrs={
                           "data_format": 'nchw',
                           "dilations": dilations,
                           "padding_algorithm": padding_algorithm,
                           "groups": groups,
                           "paddings": paddings,
                           "strides": strides
                       })

    elementwise_add_op = OpConfig(type="elementwise_add",
                                  inputs={
                                      "X": ["add_input_data"],
                                      "Y": ["conv_output_data"]
                                  },
                                  outputs={"Out": ["output_data"]},
                                  attrs={"axis": -1})

    ops = [conv_op, elementwise_add_op]
    program_config = ProgramConfig(
        ops=ops,
        weights={"weight_data": TensorConfig(shape=weight_shape)},
        inputs={
            "input_data": TensorConfig(shape=in_shape),
            "add_input_data": TensorConfig(shape=out_shape)
        },
        outputs=["output_data"])
    return program_config
Пример #8
0
    def sample_program_configs(self, draw):
        #conv or conv_transpose
        Transpose = draw(st.sampled_from([True, False]))

        #conv param or conv_transpose param
        in_shape = draw(
            st.lists(st.integers(min_value=2, max_value=128),
                     min_size=3,
                     max_size=3))
        in_shape = [draw(st.integers(min_value=1, max_value=3))] + in_shape
        weight_shape = draw(
            st.lists(st.integers(min_value=1, max_value=8),
                     min_size=4,
                     max_size=4))
        paddings = draw(
            st.lists(st.integers(min_value=0, max_value=2),
                     min_size=2,
                     max_size=2))
        dilations = draw(st.sampled_from([[1, 1], [2, 2]]))
        groups = draw(st.sampled_from([1, 2, in_shape[1]]))
        padding_algorithm = draw(st.sampled_from(["VALID", "SAME"]))
        strides = draw(st.sampled_from([[1, 1], [2, 2]]))
        output_padding = draw(
            st.sampled_from([[],
                             draw(
                                 st.lists(st.integers(min_value=0,
                                                      max_value=16),
                                          min_size=2,
                                          max_size=2))]))
        scale_in = draw(st.floats(min_value=0.001, max_value=0.1))
        scale_out = draw(st.floats(min_value=0.001, max_value=0.1))

        #active param
        threshold = draw(st.floats(min_value=0, max_value=1))
        alpha = draw(st.floats(min_value=0, max_value=1))
        scale = draw(st.floats(min_value=0.5, max_value=5))
        offset = draw(st.floats(min_value=0, max_value=1))
        slope = draw(st.floats(min_value=0.7, max_value=0.9))

        conv_out_shape = []
        paddings_, dilations_ = UpdatePaddingAndDilation(
            in_shape, weight_shape, paddings, dilations, groups,
            padding_algorithm, strides)
        self.depthwise = False

        if Transpose:
            assume(in_shape[1] == weight_shape[0])
            assume(in_shape[1] % groups == 0)  #TODO
            if len(output_padding):
                assume(output_padding[0] < max(strides[0], dilations_[0]))
                assume(output_padding[1] < max(strides[1], dilations_[1]))
            conv_out_shape = [in_shape[0], weight_shape[1] * groups]
            oh, ow = ConvTransposeOutputSize(in_shape, weight_shape,
                                             dilations_, paddings_, strides)
            if len(output_padding):
                oh = oh + output_padding[0]
                ow = ow + output_padding[1]
            conv_out_shape = conv_out_shape + [int(oh), int(ow)]
            assume(oh > 0 and ow > 0)
            if len(output_padding):
                conv_output_h = (oh + output_padding[0] + paddings[0] +
                                 paddings[1] -
                                 (dilations[0] *
                                  (weight_shape[2] - 1) + 1)) / strides[0] + 1
                conv_output_w = (oh + output_padding[1] + paddings[0] +
                                 paddings[1] -
                                 (dilations[1] *
                                  (weight_shape[3] - 1) + 1)) / strides[1] + 1
                assume(in_shape[2] == conv_output_h)
                assume(in_shape[3] == conv_output_w)

        else:
            self.depthwise = in_shape[1] == weight_shape[1] and in_shape[
                1] == groups
            assume(in_shape[1] == weight_shape[1] * groups)
            assume(weight_shape[0] % groups == 0)
            conv_out_shape = [in_shape[0], weight_shape[0]]
            oh, ow = ConvOutputSize(in_shape, weight_shape, dilations_,
                                    paddings_, strides)
            conv_out_shape = conv_out_shape + [int(oh), int(ow)]
            assume(oh > 0 and ow > 0)

        Alpha_shape = []
        mode_data = draw(st.sampled_from(["all", "channel", "element"]))
        if mode_data == "all":
            Alpha_shape = [1]
        elif mode_data == "channel":
            Alpha_shape = [conv_out_shape[1]]
        elif mode_data == "element":
            Alpha_shape = conv_out_shape

        act_type = draw(
            st.sampled_from([
                'relu', 'relu6', 'leaky_relu', 'hard_swish', 'prelu',
                'hard_sigmoid'
            ]))

        def generate_act_attrs(act_type_str):
            attrs = {}
            if act_type_str == 'relu6':
                attrs = {"threshold": threshold}
            if act_type_str == 'leaky_relu':
                attrs = {"alpha": alpha}
            if act_type_str == 'hard_swish':
                attrs = {
                    "threshold": threshold,
                    "scale": scale,
                    "offset": offset
                }
            if act_type_str == "prelu":
                attrs = {"mode": mode_data, "data_format": "NCHW"}
            if act_type_str == "hard_sigmoid":
                attrs = {"slope": slope, "offset": offset}
            return attrs

        conv_type = ""
        conv_attrs = {}
        if Transpose:
            conv_type = "conv2d_transpose"
            conv_attrs = {
                "data_format": 'nchw',
                "dilations": dilations,
                "padding_algorithm": padding_algorithm,
                "groups": groups,
                "paddings": paddings,
                "strides": strides,
                "Scale_in": scale_in,
                "Scale_out": scale_out,
                "output_size": [],
                "output_padding": output_padding
            }
        else:
            conv_type = "conv2d"
            conv_attrs = {
                "data_format": 'nchw',
                "dilations": dilations,
                "padding_algorithm": padding_algorithm,
                "groups": groups,
                "paddings": paddings,
                "strides": strides,
                "Scale_in": scale_in,
                "Scale_out": scale_out
            }

        active_op_input = {}
        inputs_data = {}
        if act_type == "prelu":
            active_op_input = {
                "X": ["conv_output_data"],
                "Alpha": ["alpha_data"]
            }
            inputs_data = {
                "input_data": TensorConfig(shape=in_shape),
                "alpha_data": TensorConfig(shape=Alpha_shape)
            }
        else:
            active_op_input = {"X": ["conv_output_data"]}
            inputs_data = {"input_data": TensorConfig(shape=in_shape)}

        conv_op = OpConfig(type=conv_type,
                           inputs={
                               "Input": ["input_data"],
                               "Filter": ["filter_data"]
                           },
                           outputs={"Output": ["conv_output_data"]},
                           attrs=conv_attrs)

        active_op = OpConfig(type=act_type,
                             inputs=active_op_input,
                             outputs={"Out": ["output_data"]},
                             attrs=generate_act_attrs(act_type))

        ops = [conv_op, active_op]
        self.ops = ops
        program_config = ProgramConfig(
            ops=ops,
            weights={"filter_data": TensorConfig(shape=weight_shape)},
            inputs=inputs_data,
            outputs=["output_data"])
        return program_config
Пример #9
0
    def sample_program_configs(self, draw):
        #conv or conv_transpose
        Transpose = draw(st.sampled_from([True, False]))

        #conv param or conv_transpose param
        in_shape = draw(
            st.lists(st.integers(min_value=3, max_value=128),
                     min_size=3,
                     max_size=3))
        in_shape = [draw(st.integers(min_value=1, max_value=4))] + in_shape
        weight_shape = draw(
            st.lists(st.integers(min_value=1, max_value=8),
                     min_size=4,
                     max_size=4))
        paddings = draw(
            st.lists(st.integers(min_value=0, max_value=2),
                     min_size=2,
                     max_size=2))
        dilations = draw(st.sampled_from([[2, 2]]))
        groups = draw(st.sampled_from([1, 2, in_shape[1]]))
        padding_algorithm = draw(st.sampled_from(["VALID", "SAME"]))
        strides = draw(st.sampled_from([[1, 1], [2, 2]]))
        output_padding = draw(
            st.sampled_from(
                [[],
                 [
                     draw(
                         st.integers(min_value=0,
                                     max_value=max(strides[0], dilations[0]) -
                                     1)),
                     draw(
                         st.integers(min_value=0,
                                     max_value=max(strides[1], dilations[1]) -
                                     1))
                 ]]))
        scale_in = draw(st.floats(min_value=0.001, max_value=0.1))
        scale_out = draw(st.floats(min_value=0.001, max_value=0.1))
        if Transpose:
            bias_sample_shape = weight_shape[1] * groups
        else:
            bias_sample_shape = weight_shape[0]
        elementwise_bias_shape = [bias_sample_shape]

        conv_out_shape = []
        paddings_, dilations_ = UpdatePaddingAndDilation(
            in_shape, weight_shape, paddings, dilations, groups,
            padding_algorithm, strides)

        if Transpose:
            assume(in_shape[1] == weight_shape[0])
            assume(in_shape[1] % groups == 0)  #TODO
            if len(output_padding):
                assume(output_padding[0] < max(strides[0], dilations_[0]))
                assume(output_padding[1] < max(strides[1], dilations_[1]))
            conv_out_shape = [in_shape[0], weight_shape[1] * groups]
            oh, ow = ConvTransposeOutputSize(in_shape, weight_shape,
                                             dilations_, paddings_, strides)
            if len(output_padding):
                oh = oh + output_padding[0]
                ow = ow + output_padding[1]
            conv_out_shape = conv_out_shape + [int(oh), int(ow)]
            assume(oh > 0 and ow > 0)
            if len(output_padding):
                conv_output_h = (oh + output_padding[0] + paddings[0] +
                                 paddings[1] -
                                 (dilations[0] *
                                  (weight_shape[2] - 1) + 1)) / strides[0] + 1
                conv_output_w = (oh + output_padding[1] + paddings[0] +
                                 paddings[1] -
                                 (dilations[1] *
                                  (weight_shape[3] - 1) + 1)) / strides[1] + 1
                assume(in_shape[2] == (int)(conv_output_h))
                assume(in_shape[3] == (int)(conv_output_w))
        else:
            assume(in_shape[1] == weight_shape[1] * groups)
            assume(weight_shape[0] % groups == 0)
            conv_out_shape = [in_shape[0], weight_shape[0]]
            oh, ow = ConvOutputSize(in_shape, weight_shape, dilations_,
                                    paddings_, strides)
            conv_out_shape = conv_out_shape + [int(oh), int(ow)]
            assume(oh > 0 and ow > 0)

        conv_type = ""
        conv_attrs = {}
        if Transpose:
            conv_type = "conv2d_transpose"
            conv_attrs = {
                "data_format": 'nchw',
                "dilations": dilations,
                "padding_algorithm": padding_algorithm,
                "groups": groups,
                "paddings": paddings,
                "strides": strides,
                "Scale_in": scale_in,
                "Scale_out": scale_out,
                "output_size": [],
                "output_padding": output_padding
            }
        else:
            conv_type = "conv2d"
            conv_attrs = {
                "data_format": 'nchw',
                "dilations": dilations,
                "padding_algorithm": padding_algorithm,
                "groups": groups,
                "paddings": paddings,
                "strides": strides,
                "Scale_in": scale_in,
                "Scale_out": scale_out
            }

        conv_op = OpConfig(type=conv_type,
                           inputs={
                               "Input": ["input_data"],
                               "Filter": ["filter_data"]
                           },
                           outputs={"Output": ["conv_output_data"]},
                           attrs=conv_attrs)

        elementwise_add_op = OpConfig(type="elementwise_add",
                                      inputs={
                                          "X": ["conv_output_data"],
                                          "Y": ["add_bias_data"]
                                      },
                                      outputs={"Out": ["output_data"]},
                                      attrs={"axis": 1})

        ops = [conv_op, elementwise_add_op]
        self.ops = ops
        program_config = ProgramConfig(
            ops=ops,
            weights={
                "filter_data": TensorConfig(shape=weight_shape),
                "add_bias_data": TensorConfig(shape=elementwise_bias_shape)
            },
            inputs={"input_data": TensorConfig(shape=in_shape)},
            outputs=["output_data"])
        return program_config
Пример #10
0
def sample_program_configs(draw):
    in_shape0 = draw(
        st.lists(
            st.integers(
                min_value=3, max_value=64), min_size=4, max_size=4))
    weight_shape0 = [
        draw(st.integers(
            min_value=3, max_value=64)), in_shape0[1], 1, 1
    ]
    weight_shape1 = [
        draw(st.integers(
            min_value=3, max_value=64)), weight_shape0[0], 1, 1
    ]

    assume(in_shape0[1] == weight_shape0[1])
    mula = in_shape0[1] * (weight_shape1[0] - weight_shape0[0])
    mulb = weight_shape0[0] * weight_shape1[0]
    assume(not (mula <= 0 or mula > mulb))

    paddings_, dilations_ = UpdatePaddingAndDilation(
        in_shape=in_shape0,
        weight_shape=weight_shape0,
        paddings=[0, 0],
        dilations=[1, 1],
        groups=1,
        padding_algorithm="VALID",
        strides=[1, 1])
    out_shape = [in_shape0[0], weight_shape0[0]]
    oh, ow = ConvOutputSize(
        in_shape=in_shape0,
        weight_shape=weight_shape0,
        dilations=dilations_,
        paddings=paddings_,
        strides=[1, 1])
    out_shape0 = out_shape + [oh, ow]
    assume(oh > 0 and ow > 0)

    in_shape1 = out_shape0
    paddings_, dilations_ = UpdatePaddingAndDilation(
        in_shape=in_shape1,
        weight_shape=weight_shape1,
        paddings=[0, 0],
        dilations=[1, 1],
        groups=1,
        padding_algorithm="VALID",
        strides=[1, 1])
    out_shape = [in_shape1[0], weight_shape1[0]]
    oh, ow = ConvOutputSize(
        in_shape=in_shape1,
        weight_shape=weight_shape1,
        dilations=dilations_,
        paddings=paddings_,
        strides=[1, 1])
    out_shape1 = out_shape + [oh, ow]
    assume(oh > 0 and ow > 0)

    conv0_op = OpConfig(
        type="conv2d",
        inputs={"Input": ["input_data"],
                "Filter": ["weight_data0"]},
        outputs={"Output": ["conv_output_data"]},
        attrs={
            "data_format": 'nchw',
            "dilations": [1, 1],
            "padding_algorithm": "VALID",
            "groups": 1,
            "paddings": [0, 0],
            "strides": [1, 1]
        })

    conv1_op = OpConfig(
        type="conv2d",
        inputs={"Input": ["conv_output_data"],
                "Filter": ["weight_data1"]},
        outputs={"Output": ["output_data"]},
        attrs={
            "data_format": 'nchw',
            "dilations": [1, 1],
            "padding_algorithm": "VALID",
            "groups": 1,
            "paddings": [0, 0],
            "strides": [1, 1]
        })

    ops = [conv0_op, conv1_op]
    program_config = ProgramConfig(
        ops=ops,
        weights={
            "weight_data0": TensorConfig(shape=weight_shape0),
            "weight_data1": TensorConfig(shape=weight_shape1)
        },
        inputs={"input_data": TensorConfig(shape=in_shape0), },
        outputs=["output_data"])
    return program_config