|
13 | 13 | import pytest |
14 | 14 | import torch |
15 | 15 | from coreai.authoring import AIProgram |
16 | | -from coreai.runtime import AIModel, NDArray |
17 | 16 | from torch import nn |
18 | 17 | from torch.export.exported_program import ExportedProgram |
19 | 18 |
|
|
24 | 23 | ActivationQuantizeModule, |
25 | 24 | ) |
26 | 25 |
|
27 | | -from ..utils import TemporaryModelAsset, filecheck_pattern |
| 26 | +from ..utils import filecheck_pattern, validate_numerical_output |
28 | 27 |
|
29 | 28 | # We add "./tests/coreai" path, in order to use some existing utils |
30 | 29 | sys.path.append(str(Path(__file__).parents[2])) |
@@ -58,32 +57,6 @@ async def lower_to_coreai( |
58 | 57 | return converter.to_coreai() |
59 | 58 |
|
60 | 59 |
|
61 | | -async def _validate_execution( |
62 | | - coreai_program: AIProgram, |
63 | | - torch_out: torch.Tensor, |
64 | | - atol: float = 1e-4, |
65 | | - rtol: float = 1e-4, |
66 | | - **kwargs: Any, |
67 | | -) -> None: |
68 | | - """Run the Core AI program using ref kernels and match with torch output.""" |
69 | | - with TemporaryModelAsset() as tempdir: |
70 | | - coreai_program.save_asset(Path(tempdir)) |
71 | | - ai_model = await AIModel.load(Path(tempdir)) |
72 | | - rt_func = ai_model.load_function("main") |
73 | | - |
74 | | - # Wrap all kwargs with NDArray |
75 | | - nd_kwargs = {k: NDArray(data=v) for k, v in kwargs.items()} |
76 | | - coreai_outs = await rt_func(nd_kwargs) |
77 | | - |
78 | | - coreai_out_np = {k: v.numpy() for k, v in coreai_outs.items()} |
79 | | - np.testing.assert_allclose( |
80 | | - torch_out.detach().numpy(), |
81 | | - next(iter(coreai_out_np.values())), # type: ignore[arg-type] |
82 | | - rtol=rtol, |
83 | | - atol=atol, |
84 | | - ) |
85 | | - |
86 | | - |
87 | 60 | @pytest.mark.parametrize( |
88 | 61 | "nbits", |
89 | 62 | [4, 8], |
@@ -164,9 +137,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: |
164 | 137 | exported_program, |
165 | 138 | ) |
166 | 139 |
|
167 | | - await _validate_execution( |
168 | | - coreai_program, |
169 | | - torch_output, |
| 140 | + await validate_numerical_output( |
| 141 | + coreai_program=coreai_program, |
| 142 | + torch_out=torch_output, |
| 143 | + atol=1e-4, |
| 144 | + rtol=1e-4, |
170 | 145 | input_tensor=input_tensor, |
171 | 146 | ) |
172 | 147 |
|
@@ -233,9 +208,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: |
233 | 208 | exported_program, |
234 | 209 | ) |
235 | 210 |
|
236 | | - await _validate_execution( |
237 | | - coreai_program, |
238 | | - torch_output, |
| 211 | + await validate_numerical_output( |
| 212 | + coreai_program=coreai_program, |
| 213 | + torch_out=torch_output, |
| 214 | + atol=1e-4, |
| 215 | + rtol=1e-4, |
239 | 216 | input_tensor=input_tensor, |
240 | 217 | ) |
241 | 218 |
|
@@ -284,9 +261,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: |
284 | 261 | exported_program, |
285 | 262 | ) |
286 | 263 |
|
287 | | - await _validate_execution( |
288 | | - coreai_program, |
289 | | - torch_output, |
| 264 | + await validate_numerical_output( |
| 265 | + coreai_program=coreai_program, |
| 266 | + torch_out=torch_output, |
| 267 | + atol=1e-4, |
| 268 | + rtol=1e-4, |
290 | 269 | input_tensor=input_tensor, |
291 | 270 | ) |
292 | 271 |
|
@@ -372,9 +351,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: |
372 | 351 | exported_program, |
373 | 352 | ) |
374 | 353 |
|
375 | | - await _validate_execution( |
376 | | - coreai_program, |
377 | | - torch_output, |
| 354 | + await validate_numerical_output( |
| 355 | + coreai_program=coreai_program, |
| 356 | + torch_out=torch_output, |
| 357 | + atol=1e-4, |
| 358 | + rtol=1e-4, |
378 | 359 | input_tensor=input_tensor, |
379 | 360 | ) |
380 | 361 |
|
@@ -442,9 +423,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: |
442 | 423 | exported_program, |
443 | 424 | ) |
444 | 425 |
|
445 | | - await _validate_execution( |
446 | | - coreai_program, |
447 | | - torch_output, |
| 426 | + await validate_numerical_output( |
| 427 | + coreai_program=coreai_program, |
| 428 | + torch_out=torch_output, |
| 429 | + atol=1e-4, |
| 430 | + rtol=1e-4, |
448 | 431 | input_tensor=input_tensor, |
449 | 432 | ) |
450 | 433 |
|
@@ -541,9 +524,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: |
541 | 524 | exported_program, |
542 | 525 | ) |
543 | 526 |
|
544 | | - await _validate_execution( |
545 | | - coreai_program, |
546 | | - torch_output, |
| 527 | + await validate_numerical_output( |
| 528 | + coreai_program=coreai_program, |
| 529 | + torch_out=torch_output, |
| 530 | + atol=1e-4, |
| 531 | + rtol=1e-4, |
547 | 532 | input_tensor=input_tensor, |
548 | 533 | ) |
549 | 534 |
|
@@ -619,9 +604,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: |
619 | 604 | exported_program, |
620 | 605 | ) |
621 | 606 |
|
622 | | - await _validate_execution( |
623 | | - coreai_program, |
624 | | - torch_output, |
| 607 | + await validate_numerical_output( |
| 608 | + coreai_program=coreai_program, |
| 609 | + torch_out=torch_output, |
| 610 | + atol=1e-4, |
| 611 | + rtol=1e-4, |
625 | 612 | input_tensor=input_tensor, |
626 | 613 | ) |
627 | 614 |
|
@@ -808,9 +795,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: |
808 | 795 |
|
809 | 796 | coreai_program = await lower_to_coreai(exported_program) |
810 | 797 |
|
811 | | - await _validate_execution( |
812 | | - coreai_program, |
813 | | - torch_output, |
| 798 | + await validate_numerical_output( |
| 799 | + coreai_program=coreai_program, |
| 800 | + torch_out=torch_output, |
| 801 | + atol=1e-4, |
| 802 | + rtol=1e-4, |
814 | 803 | input_tensor=input_tensor, |
815 | 804 | ) |
816 | 805 |
|
@@ -954,9 +943,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor: |
954 | 943 | exported_program, |
955 | 944 | ) |
956 | 945 |
|
957 | | - await _validate_execution( |
958 | | - coreai_program, |
959 | | - torch_output, |
| 946 | + await validate_numerical_output( |
| 947 | + coreai_program=coreai_program, |
| 948 | + torch_out=torch_output, |
| 949 | + atol=1e-4, |
| 950 | + rtol=1e-4, |
960 | 951 | input_tensor=input_tensor, |
961 | 952 | ) |
962 | 953 |
|
@@ -1119,4 +1110,10 @@ async def test_numerical( |
1119 | 1110 | exported_program, |
1120 | 1111 | ) |
1121 | 1112 |
|
1122 | | - await _validate_execution(coreai_program, torch_out, input_tensor=input_tensor) |
| 1113 | + await validate_numerical_output( |
| 1114 | + coreai_program=coreai_program, |
| 1115 | + torch_out=torch_out, |
| 1116 | + atol=1e-4, |
| 1117 | + rtol=1e-4, |
| 1118 | + input_tensor=input_tensor, |
| 1119 | + ) |
0 commit comments