aboutsummaryrefslogtreecommitdiff
path: root/python/pyarmnn/test/test_modeloption.py
blob: a47d2da358f421d05a9ae79c202e8d25151d85dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
# SPDX-License-Identifier: MIT
import pytest

from pyarmnn import BackendOptions, BackendOption, BackendId, OptimizerOptions, ShapeInferenceMethod_InferAndValidate


@pytest.mark.parametrize("data", (True, -100, 128, 0.12345, 'string'))
def test_backend_option_ctor(data):
    bo = BackendOption("name", data)
    assert "name" == bo.GetName()


def test_backend_options_ctor():
    backend_id = BackendId('a')
    bos = BackendOptions(backend_id)

    assert 'a' == str(bos.GetBackendId())

    another_bos = BackendOptions(bos)
    assert 'a' == str(another_bos.GetBackendId())


def test_backend_options_add():
    backend_id = BackendId('a')
    bos = BackendOptions(backend_id)
    bo = BackendOption("name", 1)
    bos.AddOption(bo)

    assert 1 == bos.GetOptionCount()
    assert 1 == len(bos)

    assert 'name' == bos[0].GetName()
    assert 'name' == bos.GetOption(0).GetName()
    for option in bos:
        assert 'name' == option.GetName()

    bos.AddOption(BackendOption("name2", 2))

    assert 2 == bos.GetOptionCount()
    assert 2 == len(bos)


def test_backend_option_ownership():
    backend_id = BackendId('b')
    bos = BackendOptions(backend_id)
    bo = BackendOption('option', True)
    bos.AddOption(bo)

    assert bo.thisown

    del bo

    assert 1 == bos.GetOptionCount()
    option = bos[0]
    assert not option.thisown
    assert 'option' == option.GetName()

    del option

    option_again = bos[0]
    assert not option_again.thisown
    assert 'option' == option_again.GetName()


def test_optimizer_options_with_model_opt():
    a = BackendOptions(BackendId('a'))

    oo = OptimizerOptions(True,
                          False,
                          False,
                          ShapeInferenceMethod_InferAndValidate,
                          True,
                          [a],
                          True)

    mo = oo.m_ModelOptions

    assert 1 == len(mo)
    assert 'a' == str(mo[0].GetBackendId())

    b = BackendOptions(BackendId('b'))

    c = BackendOptions(BackendId('c'))

    oo.m_ModelOptions = (a, b, c)

    mo = oo.m_ModelOptions

    assert 3 == len(oo.m_ModelOptions)

    assert 'a' == str(mo[0].GetBackendId())
    assert 'b' == str(mo[1].GetBackendId())
    assert 'c' == str(mo[2].GetBackendId())


def test_optimizer_option_default():
    oo = OptimizerOptions(True,
                          False,
                          False,
                          ShapeInferenceMethod_InferAndValidate,
                          True)

    assert 0 == len(oo.m_ModelOptions)


def test_optimizer_options_fail():
    a = BackendOptions(BackendId('a'))

    with pytest.raises(TypeError) as err:
        OptimizerOptions(True,
                         False,
                         False,
                         ShapeInferenceMethod_InferAndValidate,
                         True,
                         a,
                         True)

    assert "Wrong number or type of arguments" in str(err.value)

    with pytest.raises(RuntimeError) as err:
        OptimizerOptions(True,
                         False,
                         True,
                         ShapeInferenceMethod_InferAndValidate,
                         True,
                         [a],
                         True)

    assert "BFloat16 and Float16 optimization cannot be enabled at the same time" in str(err.value)

    with pytest.raises(TypeError) as err:
        oo = OptimizerOptions(True,
                              False,
                              False,
                              ShapeInferenceMethod_InferAndValidate,
                              True)

        oo.m_ModelOptions = 'nonsense'

    assert "in method 'OptimizerOptions_m_ModelOptions_set', argument 2" in str(err.value)

    with pytest.raises(TypeError) as err:
        oo = OptimizerOptions(True,
                              False,
                              False,
                              ShapeInferenceMethod_InferAndValidate,
                              True)

        oo.m_ModelOptions = ['nonsense', a]

    assert "in method 'OptimizerOptions_m_ModelOptions_set', argument 2" in str(err.value)