示例#1
0
def test_default_version_IR_V11_seperate_paths():
    core = Core()
    xml_path = "./serialized_function.xml"
    bin_path = "./serialized_function.bin"
    shape = [100, 100, 2]
    parameter_a = ov.parameter(shape, dtype=np.float32, name="A")
    parameter_b = ov.parameter(shape, dtype=np.float32, name="B")
    parameter_c = ov.parameter(shape, dtype=np.float32, name="C")
    parameter_d = ov.parameter(shape, dtype=np.float32, name="D")
    model = ov.floor(
        ov.minimum(ov.abs(parameter_a), ov.multiply(parameter_b, parameter_c)))
    func = Model(model, [parameter_a, parameter_b, parameter_c], "Model")
    pass_manager = Manager()
    pass_manager.register_pass("Serialize",
                               xml_path=xml_path,
                               bin_path=bin_path,
                               version="IR_V11")
    pass_manager.run_passes(func)

    res_func = core.read_model(model=xml_path, weights=bin_path)

    assert func.get_parameters() == res_func.get_parameters()
    assert func.get_ordered_ops() == res_func.get_ordered_ops()

    os.remove(xml_path)
    os.remove(bin_path)
def test_low_latency2():
    X = opset8.parameter(Shape([32, 40, 10]), np.float32, "X")
    Y = opset8.parameter(Shape([32, 40, 10]), np.float32, "Y")
    M = opset8.parameter(Shape([32, 2, 10]), np.float32, "M")

    X_i = opset8.parameter(Shape([32, 2, 10]), np.float32, "X_i")
    Y_i = opset8.parameter(Shape([32, 2, 10]), np.float32, "Y_i")
    M_body = opset8.parameter(Shape([32, 2, 10]), np.float32, "M_body")

    sum = opset8.add(X_i, Y_i)
    Zo = opset8.multiply(sum, M_body)

    body = Model([Zo], [X_i, Y_i, M_body], "body_function")

    ti = opset8.tensor_iterator()
    ti.set_body(body)
    ti.set_sliced_input(X_i, X.output(0), 0, 2, 2, 39, 1)
    ti.set_sliced_input(Y_i, Y.output(0), 0, 2, 2, -1, 1)
    ti.set_invariant_input(M_body, M.output(0))

    out0 = ti.get_iter_value(Zo.output(0), -1)
    out1 = ti.get_concatenated_slices(Zo.output(0), 0, 2, 2, 39, 1)

    result0 = opset8.result(out0)
    result1 = opset8.result(out1)

    model = Model([result0, result1], [X, Y, M])

    m = Manager()
    m.register_pass(LowLatency2())
    m.run_passes(model)

    # TODO: create TI which will be transformed by LowLatency2
    assert count_ops(model, "TensorIterator") == [1]
示例#3
0
def test_simple_pattern_replacement():
    # Simple: for Extensions. Without any classes and inheritance.
    def pattern_replacement():
        param = WrapType("opset8.Parameter")
        relu = WrapType("opset8.Relu", param.output(0))

        def callback(m: Matcher) -> bool:
            root = m.get_match_root()

            # Just to check that capturing works and we can
            # link pattern nodes with matched graph nodes.
            assert relu in m.get_pattern_value_map()

            new_relu = opset8.exp(
                root.input_value(0))  # ot root.input(0).get_source_output()
            replace_node(root, new_relu)
            return True

        return Matcher(relu, "SimpleReplacement"), callback

    model = get_test_function()

    m = Manager()
    m.register_pass(MatcherPass(*pattern_replacement()))
    m.run_passes(model)

    assert count_ops(model, ("Relu", "Exp")) == [0, 1]
def test_convert_precision():
    model = get_model()

    m = Manager()
    m.register_pass(ConvertFP32ToFP16())
    m.run_passes(model)

    assert model is not None
def test_constant_folding():
    model = get_model()

    m = Manager()
    m.register_pass(ConstantFolding())
    m.run_passes(model)

    assert model is not None
    assert count_ops(model, "ShapeOf") == [0]
示例#6
0
def test_negative_pass_registration():
    m = Manager()
    expect_exception(lambda: m.register_pass(PatternReplacement))
    expect_exception(
        lambda: m.register_pass("PatternReplacement", PatternReplacement()))
    expect_exception(
        lambda: m.register_pass("Serialize", Serialize("out.xml", "out.bin")))
    expect_exception(lambda: m.register_pass("Serialize", "out.xml", "out.bin",
                                             "out.wrong"))
def test_make_stateful():
    model = get_model()

    m = Manager()
    p = MakeStateful({"parameter": "result"})
    m.register_pass(p)
    m.run_passes(model)

    assert model is not None
    assert len(model.get_parameters()) == 0
    assert len(model.get_results()) == 0
示例#8
0
def test_serialize_pass_wrong_num_of_args():
    xml_path = "serialized_function.xml"
    bin_path = "serialized_function.bin"

    pass_manager = Manager()
    with pytest.raises(TypeError) as e:
        pass_manager.register_pass(pass_name="Serialize",
                                   xml_path=xml_path,
                                   bin_path=bin_path,
                                   model=5)
    assert "Invoked with:" in str(e.value)
def test_convert_precision():
    model = get_model()
    assert model.get_parameters()[0].get_element_type().to_dtype(
    ) == np.float32

    m = Manager()
    m.register_pass(ConvertFP32ToFP16())
    m.run_passes(model)

    assert model is not None
    assert model.get_parameters()[0].get_element_type().to_dtype(
    ) == np.float16
示例#10
0
def test_matcher_pass():
    model = get_test_function()

    m = Manager()
    # check that register pass returns pass instance
    p = m.register_pass(PatternReplacement())
    m.run_passes(model)

    assert p.model_changed
    assert count_ops(model, "Relu") == [2]
示例#11
0
def test_graph_rewrite():
    model = get_test_function()

    m = Manager()
    # check that register pass returns pass instance
    anchor = m.register_pass(GraphRewrite())
    anchor.add_matcher(PatternReplacement())
    m.run_passes(model)

    assert count_ops(model, "Relu") == [2]
def test_serialize_pass():
    core = Core()
    xml_path = "serialized_function.xml"
    bin_path = "serialized_function.bin"

    func = get_test_function()

    m = Manager()
    m.register_pass(Serialize(xml_path, bin_path))
    m.run_passes(func)

    assert func is not None

    res_func = core.read_model(model=xml_path, weights=bin_path)

    assert func.get_parameters() == res_func.get_parameters()
    assert func.get_ordered_ops() == res_func.get_ordered_ops()

    os.remove(xml_path)
    os.remove(bin_path)
示例#13
0
def test_constant_folding():
    node_constant = ov.constant(
        np.array([[0.0, 0.1, -0.1], [-2.5, 2.5, 3.0]], dtype=np.float32))
    node_ceil = ov.ceiling(node_constant)
    func = Model(node_ceil, [], "TestFunction")

    assert count_ops_of_type(func, node_ceil) == 1
    assert count_ops_of_type(func, node_constant) == 1

    pass_manager = Manager()
    pass_manager.register_pass("ConstantFolding")
    pass_manager.run_passes(func)

    assert count_ops_of_type(func, node_ceil) == 0
    assert count_ops_of_type(func, node_constant) == 1

    new_const = func.get_results()[0].input(0).get_source_output().get_node()
    values_out = new_const.get_vector()
    values_expected = [0.0, 1.0, 0.0, -2.0, 3.0, 3.0]

    assert np.allclose(values_out, values_expected)
示例#14
0
def test_register_new_node():
    class InsertExp(MatcherPass):
        def __init__(self):
            MatcherPass.__init__(self)
            self.model_changed = False

            param = WrapType("opset8.Parameter")

            def callback(m: Matcher) -> bool:
                # Input->...->Result => Input->Exp->...->Result
                root = m.get_match_value()
                consumers = root.get_target_inputs()

                exp = opset8.exp(root)
                for consumer in consumers:
                    consumer.replace_source_output(exp.output(0))

                # For testing purpose
                self.model_changed = True

                # Use new operation for additional matching
                self.register_new_node(exp)

                # Root node wasn't replaced or changed
                return False

            self.register_matcher(Matcher(param, "InsertExp"), callback)

    class RemoveExp(MatcherPass):
        def __init__(self):
            MatcherPass.__init__(self)
            self.model_changed = False

            param = WrapType("opset8.Exp")

            def callback(m: Matcher) -> bool:
                root = m.get_match_root()
                root.output(0).replace(root.input_value(0))

                # For testing purpose
                self.model_changed = True

                return True

            self.register_matcher(Matcher(param, "RemoveExp"), callback)

    m = Manager()
    ins = m.register_pass(InsertExp())
    rem = m.register_pass(RemoveExp())
    m.run_passes(get_test_function())

    assert ins.model_changed
    assert rem.model_changed
示例#15
0
def test_serialize_pass_mixed_args_kwargs():
    core = Core()
    xml_path = "serialized_function.xml"
    bin_path = "serialized_function.bin"
    shape = [3, 2]
    parameter_a = ov.parameter(shape, dtype=np.float32, name="A")
    parameter_b = ov.parameter(shape, dtype=np.float32, name="B")
    model = parameter_a - parameter_b
    func = Model(model, [parameter_a, parameter_b], "Model")

    pass_manager = Manager()
    pass_manager.register_pass("Serialize", xml_path, bin_path=bin_path)
    pass_manager.run_passes(func)

    res_func = core.read_model(model=xml_path, weights=bin_path)

    assert func.get_parameters() == res_func.get_parameters()
    assert func.get_ordered_ops() == res_func.get_ordered_ops()

    os.remove(xml_path)
    os.remove(bin_path)
示例#16
0
def test_registration_and_pass_name():
    m = Manager()

    a = m.register_pass(PatternReplacement())
    a.set_name("PatterReplacement")

    b = m.register_pass(MyModelPass())
    b.set_name("ModelPass")

    c = m.register_pass(GraphRewrite())
    c.set_name("Anchor")

    d = c.add_matcher(PatternReplacement())
    d.set_name("PatterReplacement")

    e = m.register_pass(BackwardGraphRewrite())
    e.set_name("BackAnchor")

    f = e.add_matcher(PatternReplacement())
    f.set_name("PatterReplacement")

    PatternReplacement().set_name("PatternReplacement")
    MyModelPass().set_name("MyModelPass")
    GraphRewrite().set_name("Anchor")
    BackwardGraphRewrite().set_name("BackAnchor")

    # Preserve legacy behaviour when registered pass doesn't exist
    # and in this case we shouldn't throw an exception.
    m.register_pass("NotExistingPass")
示例#17
0
def test_serialize_results():
    core = Core()
    node_constant = ov.constant(
        np.array([[0.0, 0.1, -0.1], [-2.5, 2.5, 3.0]], dtype=np.float32))
    node_ceil = ov.ceiling(node_constant)
    func = Model(node_ceil, [], "Model")

    xml_path = "serialized_function.xml"
    bin_path = "serialized_function.bin"
    pass_manager = Manager()
    pass_manager.register_pass("Serialize",
                               xml_path=xml_path,
                               bin_path=bin_path)
    pass_manager.run_passes(func)

    res_func = core.read_model(model=xml_path, weights=bin_path)
    const = func.get_results()[0].input(0).get_source_output().get_node()
    new_const = res_func.get_results()[0].input(
        0).get_source_output().get_node()

    assert const == new_const

    os.remove(xml_path)
    os.remove(bin_path)
示例#18
0
def test_serialize_seperate_paths_args():
    core = Core()
    xml_path = "serialized_function.xml"
    bin_path = "serialized_function.bin"
    shape = [2, 2]
    parameter_a = ov.parameter(shape, dtype=np.float32, name="A")
    parameter_b = ov.parameter(shape, dtype=np.float32, name="B")
    parameter_c = ov.parameter(shape, dtype=np.float32, name="C")
    parameter_d = ov.parameter(shape, dtype=np.float32, name="D")
    model = ((parameter_a + parameter_b) * parameter_c) / parameter_d
    func = Model(model, [parameter_a, parameter_b, parameter_c, parameter_d],
                 "Model")

    pass_manager = Manager()
    pass_manager.register_pass("Serialize", xml_path, bin_path)
    pass_manager.run_passes(func)

    res_func = core.read_model(model=xml_path, weights=bin_path)

    assert func.get_parameters() == res_func.get_parameters()
    assert func.get_ordered_ops() == res_func.get_ordered_ops()

    os.remove(xml_path)
    os.remove(bin_path)
示例#19
0
def dump_exec_graph(compiled_model, model_path):
    weight_path = model_path[:model_path.find(".xml")] + ".bin"
    pass_manager = Manager()
    pass_manager.register_pass("Serialize", model_path, weight_path)
    pass_manager.run_passes(compiled_model.get_runtime_model())
示例#20
0
def test_model_pass():
    m = Manager()
    p = m.register_pass(MyModelPass())
    m.run_passes(get_test_function())

    assert p.model_changed
示例#21
0
    .convert_element_type() \
    .convert_color(ColorFormat.RGB) \
    .resize(ResizeAlgorithm.RESIZE_LINEAR) \
    .mean([123.675, 116.28, 103.53]) \
    .scale([58.624, 57.12, 57.375])

# Dump preprocessor
print(f'Dump preprocessor: {ppp}')
model = ppp.build()

# ======== Step 2: Change batch size ================
# In this example we also want to change batch size to increase throughput
set_batch(model, 2)

# ======== Step 3: Save the model ================
pass_manager = Manager()
pass_manager.register_pass(pass_name="Serialize",
                           xml_path='/path/to/some_model_saved.xml',
                           bin_path='/path/to/some_model_saved.bin')
pass_manager.run_passes(model)
# ! [ov:preprocess:save]

# ! [ov:preprocess:save_load]
core = Core()
core.set_property({'CACHE_DIR': '/path/to/cache/dir'})

# In case that no preprocessing is needed anymore, we can load model on target device directly
# With cached model available, it will also save some time on reading original model
compiled_model = core.compile_model('/path/to/some_model_saved.xml', 'CPU')
# ! [ov:preprocess:save_load]