示例#1
0
def test_group_convolution_backprop_data():
    runtime = get_runtime()

    data_shape = [1, 1, 3, 3]
    filters_shape = [1, 1, 1, 3, 3]
    strides = [2, 2]
    output_padding = [1, 1]
    pads_begin = [1, 1]
    pads_end = [1, 1]

    data_node = ng.parameter(data_shape, name="Data", dtype=np.float32)
    filters_node = ng.parameter(filters_shape,
                                name="Filters",
                                dtype=np.float32)
    model = ng.group_convolution_backprop_data(data_node,
                                               filters_node,
                                               strides,
                                               None,
                                               pads_begin,
                                               pads_end,
                                               output_padding=output_padding)

    data_value = np.array(
        [
            0.16857791,
            -0.15161794,
            0.08540368,
            0.1820628,
            -0.21746576,
            0.08245695,
            0.1431433,
            -0.43156421,
            0.30591947,
        ],
        dtype=np.float32,
    ).reshape(data_shape)

    filters_value = np.array(
        [
            -0.06230065,
            0.37932432,
            -0.25388849,
            0.33878803,
            0.43709868,
            -0.22477469,
            0.04118127,
            -0.44696793,
            0.06373066,
        ],
        dtype=np.float32,
    ).reshape(filters_shape)

    computation = runtime.computation(model, data_node, filters_node)
    result = computation(data_value, filters_value)

    expected = np.array(
        [
            0.07368518,
            -0.08925839,
            -0.06627201,
            0.06301362,
            0.03732984,
            -0.01919658,
            -0.00628807,
            -0.02817563,
            -0.01472169,
            0.04392925,
            -0.00689478,
            -0.01549204,
            0.07957941,
            -0.11459791,
            -0.09505399,
            0.07681622,
            0.03604182,
            -0.01853423,
            -0.0270785,
            -0.00680824,
            -0.06650258,
            0.08004665,
            0.07918708,
            0.0724144,
            0.06256775,
            -0.17838378,
            -0.18863615,
            0.20064656,
            0.133717,
            -0.06876295,
            -0.06398046,
            -0.00864975,
            0.19289537,
            -0.01490572,
            -0.13673618,
            0.01949645,
        ],
        dtype=np.float32,
    ).reshape(1, 1, 6, 6)

    assert np.allclose(result, expected)
示例#2
0
def test_space_to_depth_operator():
    runtime = get_runtime()

    data_shape = [1, 2, 4, 4]
    data_value = np.arange(start=0, stop=32, step=1.0,
                           dtype=np.float32).reshape(data_shape)
    mode = "blocks_first"
    block_size = 2

    parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)

    model = ng.space_to_depth(parameter_data, mode, block_size)
    computation = runtime.computation(model, parameter_data)

    result = computation(data_value)
    expected = np.array(
        [
            0,
            2,
            8,
            10,
            16,
            18,
            24,
            26,
            1,
            3,
            9,
            11,
            17,
            19,
            25,
            27,
            4,
            6,
            12,
            14,
            20,
            22,
            28,
            30,
            5,
            7,
            13,
            15,
            21,
            23,
            29,
            31,
        ],
        dtype=np.float32,
    ).reshape(1, 8, 2, 2)
    assert np.allclose(result, expected)

    batch_size = 2
    input_size = 3
    hidden_size = 3

    X_shape = [batch_size, input_size]
    H_t_shape = [batch_size, hidden_size]
    W_shape = [hidden_size, input_size]
    R_shape = [hidden_size, hidden_size]
    B_shape = [hidden_size]

    parameter_X = ng.parameter(X_shape, name="X", dtype=np.float32)
    parameter_H_t = ng.parameter(H_t_shape, name="H_t", dtype=np.float32)
    parameter_W = ng.parameter(W_shape, name="W", dtype=np.float32)
    parameter_R = ng.parameter(R_shape, name="R", dtype=np.float32)
    parameter_B = ng.parameter(B_shape, name="B", dtype=np.float32)

    X_value = np.array(
        [0.3432185, 0.612268, 0.20272376, 0.9513413, 0.30585995, 0.7265472],
        dtype=np.float32).reshape(X_shape)
    H_t_value = np.array(
        [0.12444675, 0.52055854, 0.46489045, 0.4983964, 0.7730452, 0.28439692],
        dtype=np.float32).reshape(H_t_shape)
    W_value = np.array(
        [
            0.41930267,
            0.7872176,
            0.89940447,
            0.23659843,
            0.24676207,
            0.17101714,
            0.3147149,
            0.6555601,
            0.4559603,
        ],
        dtype=np.float32,
    ).reshape(W_shape)
    R_value = np.array(
        [
            0.8374871,
            0.86660194,
            0.82114047,
            0.71549815,
            0.18775631,
            0.3182116,
            0.25392973,
            0.38301638,
            0.85531586,
        ],
        dtype=np.float32,
    ).reshape(R_shape)
    B_value = np.array([1.0289404, 1.6362579, 0.4370661],
                       dtype=np.float32).reshape(B_shape)
    activations = ["sigmoid"]
    activation_alpha = []
    activation_beta = []
    clip = 2.88

    model = ng.rnn_cell(
        parameter_X,
        parameter_H_t,
        parameter_W,
        parameter_R,
        parameter_B,
        hidden_size,
        activations,
        activation_alpha,
        activation_beta,
        clip,
    )
    computation = runtime.computation(model, parameter_X, parameter_H_t,
                                      parameter_W, parameter_R, parameter_B)

    result = computation(X_value, H_t_value, W_value, R_value, B_value)
    expected = np.array(
        [0.94126844, 0.9036043, 0.841243, 0.9468489, 0.934215, 0.873708],
        dtype=np.float32).reshape(batch_size, hidden_size)

    assert np.allclose(result, expected)
示例#3
0
def test_fake_quantize():
    runtime = get_runtime()

    data_value = np.arange(24.0, dtype=np.float32).reshape(1, 2, 3, 4)
    input_low_value = np.float32(0)
    input_high_value = np.float32(23)
    output_low_value = np.float32(2)
    output_high_value = np.float32(16)
    levels = np.float32(4)

    data_shape = [1, 2, 3, 4]
    bound_shape = []
    parameter_data = ng.parameter(data_shape, name="data", dtype=np.float32)
    parameter_input_low = ng.parameter(bound_shape,
                                       name="input_low",
                                       dtype=np.float32)
    parameter_input_high = ng.parameter(bound_shape,
                                        name="input_high",
                                        dtype=np.float32)
    parameter_output_low = ng.parameter(bound_shape,
                                        name="output_low",
                                        dtype=np.float32)
    parameter_output_high = ng.parameter(bound_shape,
                                         name="output_high",
                                         dtype=np.float32)

    model = ng.fake_quantize(
        parameter_data,
        parameter_input_low,
        parameter_input_high,
        parameter_output_low,
        parameter_output_high,
        levels,
    )
    computation = runtime.computation(
        model,
        parameter_data,
        parameter_input_low,
        parameter_input_high,
        parameter_output_low,
        parameter_output_high,
    )

    result = computation(data_value, input_low_value, input_high_value,
                         output_low_value, output_high_value)

    expected = np.array(
        [[[
            [
                [2.0, 2.0, 2.0, 2.0],
                [6.6666669, 6.6666669, 6.6666669, 6.6666669],
                [6.6666669, 6.6666669, 6.6666669, 6.6666669],
            ],
            [
                [11.33333301, 11.33333301, 11.33333301, 11.33333301],
                [11.33333301, 11.33333301, 11.33333301, 11.33333301],
                [16.0, 16.0, 16.0, 16.0],
            ],
        ]]],
        dtype=np.float32,
    )
    assert np.allclose(result, expected)
示例#4
0
def test_backend_config():
    dummy_config = {"dummy_option": "dummy_value"}
    # Expect no throw
    runtime = get_runtime()
    runtime.set_config(dummy_config)
def test_reverse_sequence():
    input_data = np.array(
        [
            0,
            0,
            3,
            0,
            6,
            0,
            9,
            0,
            1,
            0,
            4,
            0,
            7,
            0,
            10,
            0,
            2,
            0,
            5,
            0,
            8,
            0,
            11,
            0,
            12,
            0,
            15,
            0,
            18,
            0,
            21,
            0,
            13,
            0,
            16,
            0,
            19,
            0,
            22,
            0,
            14,
            0,
            17,
            0,
            20,
            0,
            23,
            0,
        ],
        dtype=np.int32,
    ).reshape([2, 3, 4, 2])
    seq_lenghts = np.array([1, 2, 1, 2], dtype=np.int32)
    batch_axis = 2
    sequence_axis = 1

    input_param = ng.parameter(input_data.shape, name="input", dtype=np.int32)
    seq_lengths_param = ng.parameter(seq_lenghts.shape,
                                     name="sequence lengths",
                                     dtype=np.int32)
    model = ng.reverse_sequence(input_param, seq_lengths_param, batch_axis,
                                sequence_axis)

    runtime = get_runtime()
    computation = runtime.computation(model, input_param, seq_lengths_param)
    result = computation(input_data, seq_lenghts)

    expected = np.array([
        0,
        0,
        4,
        0,
        6,
        0,
        10,
        0,
        1,
        0,
        3,
        0,
        7,
        0,
        9,
        0,
        2,
        0,
        5,
        0,
        8,
        0,
        11,
        0,
        12,
        0,
        16,
        0,
        18,
        0,
        22,
        0,
        13,
        0,
        15,
        0,
        19,
        0,
        21,
        0,
        14,
        0,
        17,
        0,
        20,
        0,
        23,
        0,
    ], ).reshape([1, 2, 3, 4, 2])
    assert np.allclose(result, expected)
示例#6
0
def check_if(if_model, cond_val, exp_results):
    last_node = if_model(cond_val)
    runtime = get_runtime()
    computation = runtime.computation(last_node)
    results = computation()
    check_results(results, exp_results)
示例#7
0
def test_max_pool():
    # test 1d
    element_type = Type.f32
    shape = Shape([1, 1, 10])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(10, dtype=np.float32).reshape([1, 1, 10])
    window_shape = [3]

    strides = [1] * len(window_shape)
    pads_begin = [0] * len(window_shape)
    pads_end = [0] * len(window_shape)

    model = ng.max_pool(A, strides, pads_begin, pads_end, window_shape)
    function = Function([model], parameter_list, "test")

    runtime = get_runtime()
    computation = runtime.computation(function, *parameter_list)
    result = computation(input_arr)[0]

    expected = (np.arange(8) + 2).reshape(1, 1, 8)
    assert np.allclose(result, expected)

    # test 1d with strides
    strides = [2]
    pads_begin = [0] * len(window_shape)
    pads_end = [0] * len(window_shape)

    model = ng.max_pool(A, strides, pads_begin, pads_end, window_shape)
    function = Function([model], parameter_list, "test")

    size = 4
    computation = runtime.computation(function, *parameter_list)
    result = computation(input_arr)[0]

    expected = ((np.arange(size) + 1) * 2).reshape(1, 1, size)
    assert np.allclose(result, expected)

    # test 2d
    element_type = Type.f32
    shape = Shape([1, 1, 10, 10])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    window_shape = [3, 3]

    strides = [1, 1]
    pads_begin = [0, 0]
    pads_end = [0, 0]

    model = ng.max_pool(A, strides, pads_begin, pads_end, window_shape)
    function = Function([model], parameter_list, "test")

    computation = runtime.computation(function, *parameter_list)
    result = computation(input_arr)[0]

    expected = ((np.arange(100).reshape(10, 10))[2:, 2:]).reshape(1, 1, 8, 8)
    assert np.allclose(result, expected)

    # test 2d with strides
    strides = [2, 2]
    pads_begin = [0, 0]
    pads_end = [0, 0]

    model = ng.max_pool(A, strides, pads_begin, pads_end, window_shape)
    function = Function([model], parameter_list, "test")
    computation = runtime.computation(function, *parameter_list)
    result = computation(input_arr)[0]

    size = 4
    expected = ((np.arange(100).reshape(10,
                                        10))[2::2,
                                             2::2]).reshape(1, 1, size, size)
    assert np.allclose(result, expected)
示例#8
0
 def __init__(self, ng_model_function, device="CPU"):  # type: (List[Function], str) -> None
     super().__init__()
     self.device = device
     self.ng_model_function = ng_model_function
     self.runtime = get_runtime()
     self.computation = self.runtime.computation(ng_model_function)
示例#9
0
def test_ngraph_preprocess_model():
    model = bytes(b"""<net name="add_model" version="10">
    <layers>
    <layer id="0" name="x" type="Parameter" version="opset1">
        <data element_type="i32" shape="2,2,2"/>
        <output>
            <port id="0" precision="FP32">
                <dim>2</dim>
                <dim>2</dim>
                <dim>2</dim>
            </port>
        </output>
    </layer>
    <layer id="1" name="y" type="Parameter" version="opset1">
        <data element_type="i32" shape="2,2,2"/>
        <output>
            <port id="0" precision="FP32">
                <dim>2</dim>
                <dim>2</dim>
                <dim>2</dim>
            </port>
        </output>
    </layer>
    <layer id="2" name="sum" type="Add" version="opset1">
        <input>
            <port id="0">
                <dim>2</dim>
                <dim>2</dim>
                <dim>2</dim>
            </port>
            <port id="1">
                <dim>2</dim>
                <dim>2</dim>
                <dim>2</dim>
            </port>
        </input>
        <output>
            <port id="2" precision="FP32">
                <dim>2</dim>
                <dim>2</dim>
                <dim>2</dim>
            </port>
        </output>
    </layer>
    <layer id="3" name="sum/sink_port_0" type="Result" version="opset1">
        <input>
            <port id="0">
                <dim>2</dim>
                <dim>2</dim>
                <dim>2</dim>
            </port>
        </input>
    </layer>
    </layers>
    <edges>
    <edge from-layer="0" from-port="0" to-layer="2" to-port="0"/>
    <edge from-layer="1" from-port="0" to-layer="2" to-port="1"/>
    <edge from-layer="2" from-port="2" to-layer="3" to-port="0"/>
    </edges>
</net>""")
    core = Core()
    function = core.read_model(model=model)

    @custom_preprocess_function
    def custom_preprocess(output: Output):
        return ops.abs(output)

    p = PrePostProcessor(function)
    p.input(1).preprocess().convert_element_type(Type.f32).scale(0.5)
    p.input(0).preprocess().convert_element_type(Type.f32).mean(5.)
    p.output(0).postprocess().custom(custom_preprocess)
    function = p.build()

    input_data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]).astype(np.float32)
    expected_output = np.array([[[2, 1], [4, 7]], [[10, 13], [16, 19]]]).astype(np.float32)

    runtime = get_runtime()
    computation = runtime.computation(function)
    output = computation(input_data, input_data)

    assert np.equal(output, expected_output).all()
示例#10
0
def import_and_compute_conv(x, weights, transpose=False, **attributes):
    x, weights = np.array(x), np.array(weights)
    onnx_model = make_onnx_model_for_conv_op(x.shape, weights.shape, transpose=transpose, **attributes)
    ng_model_function = import_onnx_model(onnx_model)
    computation = get_runtime().computation(ng_model_function)
    return computation(x, weights)[0]