alexander | 7301078 | 2021-10-18 19:17:24 +0100 | [diff] [blame] | 1 | # Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | import pytest |
| 4 | |
| 5 | from pyarmnn import BackendOptions, BackendOption, BackendId, OptimizerOptions, ShapeInferenceMethod_InferAndValidate |
| 6 | |
| 7 | |
| 8 | @pytest.mark.parametrize("data", (True, -100, 128, 0.12345, 'string')) |
| 9 | def test_backend_option_ctor(data): |
| 10 | bo = BackendOption("name", data) |
| 11 | assert "name" == bo.GetName() |
| 12 | |
| 13 | |
| 14 | def test_backend_options_ctor(): |
| 15 | backend_id = BackendId('a') |
| 16 | bos = BackendOptions(backend_id) |
| 17 | |
| 18 | assert 'a' == str(bos.GetBackendId()) |
| 19 | |
| 20 | another_bos = BackendOptions(bos) |
| 21 | assert 'a' == str(another_bos.GetBackendId()) |
| 22 | |
| 23 | |
| 24 | def test_backend_options_add(): |
| 25 | backend_id = BackendId('a') |
| 26 | bos = BackendOptions(backend_id) |
| 27 | bo = BackendOption("name", 1) |
| 28 | bos.AddOption(bo) |
| 29 | |
| 30 | assert 1 == bos.GetOptionCount() |
| 31 | assert 1 == len(bos) |
| 32 | |
| 33 | assert 'name' == bos[0].GetName() |
| 34 | assert 'name' == bos.GetOption(0).GetName() |
| 35 | for option in bos: |
| 36 | assert 'name' == option.GetName() |
| 37 | |
| 38 | bos.AddOption(BackendOption("name2", 2)) |
| 39 | |
| 40 | assert 2 == bos.GetOptionCount() |
| 41 | assert 2 == len(bos) |
| 42 | |
| 43 | |
| 44 | def test_backend_option_ownership(): |
| 45 | backend_id = BackendId('b') |
| 46 | bos = BackendOptions(backend_id) |
| 47 | bo = BackendOption('option', True) |
| 48 | bos.AddOption(bo) |
| 49 | |
| 50 | assert bo.thisown |
| 51 | |
| 52 | del bo |
| 53 | |
| 54 | assert 1 == bos.GetOptionCount() |
| 55 | option = bos[0] |
| 56 | assert not option.thisown |
| 57 | assert 'option' == option.GetName() |
| 58 | |
| 59 | del option |
| 60 | |
| 61 | option_again = bos[0] |
| 62 | assert not option_again.thisown |
| 63 | assert 'option' == option_again.GetName() |
| 64 | |
| 65 | |
| 66 | def test_optimizer_options_with_model_opt(): |
| 67 | a = BackendOptions(BackendId('a')) |
| 68 | |
| 69 | oo = OptimizerOptions(True, |
| 70 | False, |
| 71 | False, |
| 72 | ShapeInferenceMethod_InferAndValidate, |
| 73 | True, |
| 74 | [a]) |
| 75 | |
| 76 | mo = oo.m_ModelOptions |
| 77 | |
| 78 | assert 1 == len(mo) |
| 79 | assert 'a' == str(mo[0].GetBackendId()) |
| 80 | |
| 81 | b = BackendOptions(BackendId('b')) |
| 82 | |
| 83 | c = BackendOptions(BackendId('c')) |
| 84 | |
| 85 | oo.m_ModelOptions = (a, b, c) |
| 86 | |
| 87 | mo = oo.m_ModelOptions |
| 88 | |
| 89 | assert 3 == len(oo.m_ModelOptions) |
| 90 | |
| 91 | assert 'a' == str(mo[0].GetBackendId()) |
| 92 | assert 'b' == str(mo[1].GetBackendId()) |
| 93 | assert 'c' == str(mo[2].GetBackendId()) |
| 94 | |
| 95 | |
| 96 | def test_optimizer_option_default(): |
| 97 | oo = OptimizerOptions(True, |
| 98 | False, |
| 99 | False, |
| 100 | ShapeInferenceMethod_InferAndValidate, |
| 101 | True) |
| 102 | |
| 103 | assert 0 == len(oo.m_ModelOptions) |
| 104 | |
| 105 | |
| 106 | def test_optimizer_options_fail(): |
| 107 | a = BackendOptions(BackendId('a')) |
| 108 | |
| 109 | with pytest.raises(TypeError) as err: |
| 110 | OptimizerOptions(True, |
| 111 | False, |
| 112 | False, |
| 113 | ShapeInferenceMethod_InferAndValidate, |
| 114 | True, |
| 115 | a) |
| 116 | |
| 117 | assert "Wrong number or type of arguments" in str(err.value) |
| 118 | |
| 119 | with pytest.raises(RuntimeError) as err: |
| 120 | OptimizerOptions(True, |
| 121 | False, |
| 122 | True, |
| 123 | ShapeInferenceMethod_InferAndValidate, |
| 124 | True, |
| 125 | [a]) |
| 126 | |
| 127 | assert "BFloat16 and Float16 optimization cannot be enabled at the same time" in str(err.value) |
| 128 | |
| 129 | with pytest.raises(TypeError) as err: |
| 130 | oo = OptimizerOptions(True, |
| 131 | False, |
| 132 | False, |
| 133 | ShapeInferenceMethod_InferAndValidate, |
| 134 | True) |
| 135 | |
| 136 | oo.m_ModelOptions = 'nonsense' |
| 137 | |
| 138 | assert "in method 'OptimizerOptions_m_ModelOptions_set', argument 2" in str(err.value) |
| 139 | |
| 140 | with pytest.raises(TypeError) as err: |
| 141 | oo = OptimizerOptions(True, |
| 142 | False, |
| 143 | False, |
| 144 | ShapeInferenceMethod_InferAndValidate, |
| 145 | True) |
| 146 | |
| 147 | oo.m_ModelOptions = ['nonsense', a] |
| 148 | |
| 149 | assert "in method 'OptimizerOptions_m_ModelOptions_set', argument 2" in str(err.value) |