aboutsummaryrefslogtreecommitdiff
path: root/tests/mlia/test_devices_ethosu_advice_generation.py
blob: 98c8a57f54bc5b986cf3435226bdc636d3be1409 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for Ethos-U advice generation."""
from typing import List
from typing import Optional

import pytest

from mlia.cli.helpers import CLIActionResolver
from mlia.core.advice_generation import Advice
from mlia.core.common import AdviceCategory
from mlia.core.common import DataItem
from mlia.core.context import ExecutionContext
from mlia.core.helpers import ActionResolver
from mlia.core.helpers import APIActionResolver
from mlia.devices.ethosu.advice_generation import EthosUAdviceProducer
from mlia.devices.ethosu.advice_generation import EthosUStaticAdviceProducer
from mlia.devices.ethosu.data_analysis import AllOperatorsSupportedOnNPU
from mlia.devices.ethosu.data_analysis import HasCPUOnlyOperators
from mlia.devices.ethosu.data_analysis import HasUnsupportedOnNPUOperators
from mlia.devices.ethosu.data_analysis import OptimizationDiff
from mlia.devices.ethosu.data_analysis import OptimizationResults
from mlia.devices.ethosu.data_analysis import PerfMetricDiff
from mlia.nn.tensorflow.optimizations.select import OptimizationSettings


@pytest.mark.parametrize(
    "input_data, advice_category, action_resolver, expected_advice",
    [
        [
            AllOperatorsSupportedOnNPU(),
            AdviceCategory.OPERATORS,
            APIActionResolver(),
            [
                Advice(
                    [
                        "You don't have any unsupported operators, your model will "
                        "run completely on NPU."
                    ]
                )
            ],
        ],
        [
            AllOperatorsSupportedOnNPU(),
            AdviceCategory.OPERATORS,
            CLIActionResolver(
                {
                    "target_profile": "sample_target",
                    "model": "sample_model.tflite",
                }
            ),
            [
                Advice(
                    [
                        "You don't have any unsupported operators, your model will "
                        "run completely on NPU.",
                        "Check the estimated performance by running the "
                        "following command: ",
                        "mlia performance --target-profile sample_target "
                        "sample_model.tflite",
                    ]
                )
            ],
        ],
        [
            HasCPUOnlyOperators(cpu_only_ops=["OP1", "OP2", "OP3"]),
            AdviceCategory.OPERATORS,
            APIActionResolver(),
            [
                Advice(
                    [
                        "You have at least 3 operators that is CPU only: "
                        "OP1,OP2,OP3.",
                        "Using operators that are supported by the NPU will "
                        "improve performance.",
                    ]
                )
            ],
        ],
        [
            HasCPUOnlyOperators(cpu_only_ops=["OP1", "OP2", "OP3"]),
            AdviceCategory.OPERATORS,
            CLIActionResolver({}),
            [
                Advice(
                    [
                        "You have at least 3 operators that is CPU only: "
                        "OP1,OP2,OP3.",
                        "Using operators that are supported by the NPU will "
                        "improve performance.",
                        "For guidance on supported operators, run: mlia operators "
                        "--supported-ops-report",
                    ]
                )
            ],
        ],
        [
            HasUnsupportedOnNPUOperators(npu_unsupported_ratio=0.4),
            AdviceCategory.OPERATORS,
            APIActionResolver(),
            [
                Advice(
                    [
                        "You have 40% of operators that cannot be placed on the NPU.",
                        "For better performance, please review the reasons reported "
                        "in the table, and adjust the model accordingly "
                        "where possible.",
                    ]
                )
            ],
        ],
        [
            HasUnsupportedOnNPUOperators(npu_unsupported_ratio=0.4),
            AdviceCategory.OPERATORS,
            CLIActionResolver({}),
            [
                Advice(
                    [
                        "You have 40% of operators that cannot be placed on the NPU.",
                        "For better performance, please review the reasons reported "
                        "in the table, and adjust the model accordingly "
                        "where possible.",
                    ]
                )
            ],
        ],
        [
            OptimizationResults(
                [
                    OptimizationDiff(
                        opt_type=[OptimizationSettings("pruning", 0.5, None)],
                        opt_diffs={
                            "sram": PerfMetricDiff(100, 150),
                            "dram": PerfMetricDiff(100, 50),
                            "on_chip_flash": PerfMetricDiff(100, 100),
                            "off_chip_flash": PerfMetricDiff(100, 100),
                            "npu_total_cycles": PerfMetricDiff(10, 5),
                        },
                    ),
                ]
            ),
            AdviceCategory.OPTIMIZATION,
            APIActionResolver(),
            [
                Advice(
                    [
                        "With the selected optimization (pruning: 0.5)",
                        "- You have achieved 50.00% performance improvement in "
                        "DRAM used (KB)",
                        "- You have achieved 50.00% performance improvement in "
                        "NPU total cycles",
                        "- SRAM used (KB) have degraded by 50.00%",
                        "You can try to push the optimization target higher "
                        "(e.g. pruning: 0.6) "
                        "to check if those results can be further improved.",
                    ]
                ),
                Advice(
                    [
                        "The applied tooling techniques have an impact "
                        "on accuracy. Additional hyperparameter tuning may be required "
                        "after any optimization."
                    ]
                ),
            ],
        ],
        [
            OptimizationResults(
                [
                    OptimizationDiff(
                        opt_type=[OptimizationSettings("pruning", 0.5, None)],
                        opt_diffs={
                            "sram": PerfMetricDiff(100, 150),
                            "dram": PerfMetricDiff(100, 50),
                            "on_chip_flash": PerfMetricDiff(100, 100),
                            "off_chip_flash": PerfMetricDiff(100, 100),
                            "npu_total_cycles": PerfMetricDiff(10, 5),
                        },
                    ),
                ]
            ),
            AdviceCategory.OPTIMIZATION,
            CLIActionResolver({"model": "sample_model.h5"}),
            [
                Advice(
                    [
                        "With the selected optimization (pruning: 0.5)",
                        "- You have achieved 50.00% performance improvement in "
                        "DRAM used (KB)",
                        "- You have achieved 50.00% performance improvement in "
                        "NPU total cycles",
                        "- SRAM used (KB) have degraded by 50.00%",
                        "You can try to push the optimization target higher "
                        "(e.g. pruning: 0.6) "
                        "to check if those results can be further improved.",
                        "For more info: mlia optimization --help",
                        "Optimization command: "
                        "mlia optimization --optimization-type pruning "
                        "--optimization-target 0.6 sample_model.h5",
                    ]
                ),
                Advice(
                    [
                        "The applied tooling techniques have an impact "
                        "on accuracy. Additional hyperparameter tuning may be required "
                        "after any optimization."
                    ]
                ),
            ],
        ],
        [
            OptimizationResults(
                [
                    OptimizationDiff(
                        opt_type=[
                            OptimizationSettings("pruning", 0.5, None),
                            OptimizationSettings("clustering", 32, None),
                        ],
                        opt_diffs={
                            "sram": PerfMetricDiff(100, 150),
                            "dram": PerfMetricDiff(100, 50),
                            "on_chip_flash": PerfMetricDiff(100, 100),
                            "off_chip_flash": PerfMetricDiff(100, 100),
                            "npu_total_cycles": PerfMetricDiff(10, 5),
                        },
                    ),
                ]
            ),
            AdviceCategory.OPTIMIZATION,
            APIActionResolver(),
            [
                Advice(
                    [
                        "With the selected optimization (pruning: 0.5, clustering: 32)",
                        "- You have achieved 50.00% performance improvement in "
                        "DRAM used (KB)",
                        "- You have achieved 50.00% performance improvement in "
                        "NPU total cycles",
                        "- SRAM used (KB) have degraded by 50.00%",
                        "You can try to push the optimization target higher "
                        "(e.g. pruning: 0.6 and/or clustering: 16) "
                        "to check if those results can be further improved.",
                    ]
                ),
                Advice(
                    [
                        "The applied tooling techniques have an impact "
                        "on accuracy. Additional hyperparameter tuning may be required "
                        "after any optimization."
                    ]
                ),
            ],
        ],
        [
            OptimizationResults(
                [
                    OptimizationDiff(
                        opt_type=[
                            OptimizationSettings("clustering", 2, None),
                        ],
                        opt_diffs={
                            "sram": PerfMetricDiff(100, 150),
                            "dram": PerfMetricDiff(100, 50),
                            "on_chip_flash": PerfMetricDiff(100, 100),
                            "off_chip_flash": PerfMetricDiff(100, 100),
                            "npu_total_cycles": PerfMetricDiff(10, 5),
                        },
                    ),
                ]
            ),
            AdviceCategory.OPTIMIZATION,
            APIActionResolver(),
            [
                Advice(
                    [
                        "With the selected optimization (clustering: 2)",
                        "- You have achieved 50.00% performance improvement in "
                        "DRAM used (KB)",
                        "- You have achieved 50.00% performance improvement in "
                        "NPU total cycles",
                        "- SRAM used (KB) have degraded by 50.00%",
                    ]
                ),
                Advice(
                    [
                        "The applied tooling techniques have an impact "
                        "on accuracy. Additional hyperparameter tuning may be required "
                        "after any optimization."
                    ]
                ),
            ],
        ],
        [
            OptimizationResults(
                [
                    OptimizationDiff(
                        opt_type=[OptimizationSettings("pruning", 0.5, None)],
                        opt_diffs={
                            "sram": PerfMetricDiff(100, 150),
                            "dram": PerfMetricDiff(100, 150),
                            "on_chip_flash": PerfMetricDiff(100, 150),
                            "off_chip_flash": PerfMetricDiff(100, 150),
                            "npu_total_cycles": PerfMetricDiff(10, 100),
                        },
                    ),
                ]
            ),
            AdviceCategory.OPTIMIZATION,
            APIActionResolver(),
            [
                Advice(
                    [
                        "With the selected optimization (pruning: 0.5)",
                        "- DRAM used (KB) have degraded by 50.00%",
                        "- SRAM used (KB) have degraded by 50.00%",
                        "- On chip flash used (KB) have degraded by 50.00%",
                        "- Off chip flash used (KB) have degraded by 50.00%",
                        "- NPU total cycles have degraded by 900.00%",
                        "The performance seems to have degraded after "
                        "applying the selected optimizations, "
                        "try exploring different optimization types/targets.",
                    ]
                ),
                Advice(
                    [
                        "The applied tooling techniques have an impact "
                        "on accuracy. Additional hyperparameter tuning may be required "
                        "after any optimization."
                    ]
                ),
            ],
        ],
        [
            OptimizationResults(
                [
                    OptimizationDiff(
                        opt_type=[OptimizationSettings("pruning", 0.5, None)],
                        opt_diffs={
                            "sram": PerfMetricDiff(100, 150),
                            "dram": PerfMetricDiff(100, 150),
                            "on_chip_flash": PerfMetricDiff(100, 150),
                            "off_chip_flash": PerfMetricDiff(100, 150),
                            "npu_total_cycles": PerfMetricDiff(10, 100),
                        },
                    ),
                    OptimizationDiff(
                        opt_type=[OptimizationSettings("pruning", 0.6, None)],
                        opt_diffs={
                            "sram": PerfMetricDiff(100, 150),
                            "dram": PerfMetricDiff(100, 150),
                            "on_chip_flash": PerfMetricDiff(100, 150),
                            "off_chip_flash": PerfMetricDiff(100, 150),
                            "npu_total_cycles": PerfMetricDiff(10, 100),
                        },
                    ),
                ]
            ),
            AdviceCategory.OPTIMIZATION,
            APIActionResolver(),
            [],  # no advice for more than one optimization result
        ],
    ],
)
def test_ethosu_advice_producer(
    tmpdir: str,
    input_data: DataItem,
    expected_advice: List[Advice],
    advice_category: AdviceCategory,
    action_resolver: ActionResolver,
) -> None:
    """Test Ethos-U Advice producer."""
    producer = EthosUAdviceProducer()

    context = ExecutionContext(
        advice_category=advice_category,
        working_dir=tmpdir,
        action_resolver=action_resolver,
    )

    producer.set_context(context)
    producer.produce_advice(input_data)

    assert producer.get_advice() == expected_advice


@pytest.mark.parametrize(
    "advice_category, action_resolver, expected_advice",
    [
        [
            None,
            None,
            [],
        ],
        [
            AdviceCategory.OPERATORS,
            None,
            [],
        ],
        [
            AdviceCategory.PERFORMANCE,
            APIActionResolver(),
            [
                Advice(
                    [
                        "You can improve the inference time by using only operators "
                        "that are supported by the NPU.",
                    ]
                ),
                Advice(
                    [
                        "Check if you can improve the performance by applying "
                        "tooling techniques to your model."
                    ]
                ),
            ],
        ],
        [
            AdviceCategory.PERFORMANCE,
            CLIActionResolver({"model": "test_model.h5"}),
            [
                Advice(
                    [
                        "You can improve the inference time by using only operators "
                        "that are supported by the NPU.",
                        "Try running the following command to verify that:",
                        "mlia operators test_model.h5",
                    ]
                ),
                Advice(
                    [
                        "Check if you can improve the performance by applying "
                        "tooling techniques to your model.",
                        "For example: mlia optimization --optimization-type "
                        "pruning,clustering --optimization-target 0.5,32 "
                        "test_model.h5",
                        "For more info: mlia optimization --help",
                    ]
                ),
            ],
        ],
        [
            AdviceCategory.OPTIMIZATION,
            APIActionResolver(),
            [
                Advice(
                    [
                        "For better performance, make sure that all the operators "
                        "of your final TFLite model are supported by the NPU.",
                    ]
                )
            ],
        ],
        [
            AdviceCategory.OPTIMIZATION,
            CLIActionResolver({"model": "test_model.h5"}),
            [
                Advice(
                    [
                        "For better performance, make sure that all the operators "
                        "of your final TFLite model are supported by the NPU.",
                        "For more details, run: mlia operators --help",
                    ]
                )
            ],
        ],
    ],
)
def test_ethosu_static_advice_producer(
    tmpdir: str,
    advice_category: Optional[AdviceCategory],
    action_resolver: ActionResolver,
    expected_advice: List[Advice],
) -> None:
    """Test static advice generation."""
    producer = EthosUStaticAdviceProducer()

    context = ExecutionContext(
        advice_category=advice_category,
        working_dir=tmpdir,
        action_resolver=action_resolver,
    )
    producer.set_context(context)
    assert producer.get_advice() == expected_advice