Skip to content

Commit 70c9a7d

Browse files
tests: make compression + debugging tests cross-platform (#17)
- tests/compression/test_compression.py: replace the file-local `_validate_execution` helper with the shared `validate_numerical_output` util from `tests/utils.py`. The util goes through the standard numerical-validation path that already works across platforms, so the separate helper is no longer needed. - tests/debugging/{test_debug_info,test_inspector,test_validator}.py: add `pytest.mark.skipif(sys.platform != "darwin", ...)` to tests that load a runtime asset via `AIModel.load`, since that path is only supported on macOS today. The tests fail with a "no such file or directory" error on Linux otherwise. Co-authored-by: gokulkrishna98 <gokulkrishna98@users.noreply.github.com>
1 parent 53d6bdd commit 70c9a7d

4 files changed

Lines changed: 72 additions & 56 deletions

File tree

tests/compression/test_compression.py

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import pytest
1414
import torch
1515
from coreai.authoring import AIProgram
16-
from coreai.runtime import AIModel, NDArray
1716
from torch import nn
1817
from torch.export.exported_program import ExportedProgram
1918

@@ -24,7 +23,7 @@
2423
ActivationQuantizeModule,
2524
)
2625

27-
from ..utils import TemporaryModelAsset, filecheck_pattern
26+
from ..utils import filecheck_pattern, validate_numerical_output
2827

2928
# We add "./tests/coreai" path, in order to use some existing utils
3029
sys.path.append(str(Path(__file__).parents[2]))
@@ -58,32 +57,6 @@ async def lower_to_coreai(
5857
return converter.to_coreai()
5958

6059

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-
8760
@pytest.mark.parametrize(
8861
"nbits",
8962
[4, 8],
@@ -164,9 +137,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor:
164137
exported_program,
165138
)
166139

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,
170145
input_tensor=input_tensor,
171146
)
172147

@@ -233,9 +208,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor:
233208
exported_program,
234209
)
235210

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,
239216
input_tensor=input_tensor,
240217
)
241218

@@ -284,9 +261,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor:
284261
exported_program,
285262
)
286263

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,
290269
input_tensor=input_tensor,
291270
)
292271

@@ -372,9 +351,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor:
372351
exported_program,
373352
)
374353

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,
378359
input_tensor=input_tensor,
379360
)
380361

@@ -442,9 +423,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor:
442423
exported_program,
443424
)
444425

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,
448431
input_tensor=input_tensor,
449432
)
450433

@@ -541,9 +524,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor:
541524
exported_program,
542525
)
543526

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,
547532
input_tensor=input_tensor,
548533
)
549534

@@ -619,9 +604,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor:
619604
exported_program,
620605
)
621606

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,
625612
input_tensor=input_tensor,
626613
)
627614

@@ -808,9 +795,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor:
808795

809796
coreai_program = await lower_to_coreai(exported_program)
810797

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,
814803
input_tensor=input_tensor,
815804
)
816805

@@ -954,9 +943,11 @@ def forward(self, input_tensor: torch.Tensor) -> torch.Tensor:
954943
exported_program,
955944
)
956945

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,
960951
input_tensor=input_tensor,
961952
)
962953

@@ -1119,4 +1110,10 @@ async def test_numerical(
11191110
exported_program,
11201111
)
11211112

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+
)

tests/debugging/test_debug_info.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
"""Tests for debug_infos from CompiledLibrary and AIModel."""
77

8+
import sys
89
import tempfile
910
from pathlib import Path
1011

@@ -67,6 +68,10 @@ def _verify_debug_info_record(record: DebugInfoRecord) -> None:
6768
]
6869

6970

71+
@pytest.mark.skipif(
72+
sys.platform != "darwin",
73+
reason="Requires loading a runtime asset (AIModel.load); only supported on macOS",
74+
)
7075
@pytest.mark.asyncio
7176
async def test_compiled_library_debug_infos(
7277
simple_coreai_program: AIProgram,
@@ -91,6 +96,10 @@ async def test_compiled_library_debug_infos(
9196
_verify_debug_info_record(debug_info_records[0])
9297

9398

99+
@pytest.mark.skipif(
100+
sys.platform != "darwin",
101+
reason="Requires loading a runtime asset (AIModel.load); only supported on macOS",
102+
)
94103
@pytest.mark.asyncio
95104
async def test_aimodel_debug_infos(
96105
simple_coreai_program: AIProgram,

tests/debugging/test_inspector.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
"""Test inspector implementations."""
77

8+
import sys
89
import tempfile
910
from pathlib import Path
1011

@@ -105,6 +106,10 @@ async def test_caching_inspector() -> None:
105106
)
106107

107108

109+
@pytest.mark.skipif(
110+
sys.platform != "darwin",
111+
reason="Requires loading a runtime asset (AIModel.load); only supported on macOS",
112+
)
108113
@pytest.mark.asyncio
109114
async def test_coreai_inspector(simple_coreai_program: AIProgram) -> None:
110115
"""Test _CoreAIInspector with a deployed model."""

tests/debugging/test_validator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
"""Tests for validator with hierarchical graphs."""
77

8+
import sys
89
from typing import Any
910

1011
import numpy as np
@@ -406,6 +407,10 @@ async def _create_coreai_program_from_model(
406407
return coreai_program
407408

408409

410+
@pytest.mark.skipif(
411+
sys.platform != "darwin",
412+
reason="Requires loading a runtime asset (AIModel.load); only supported on macOS",
413+
)
409414
@pytest.mark.parametrize(
410415
"nan_branch",
411416
[

0 commit comments

Comments
 (0)