-
Notifications
You must be signed in to change notification settings - Fork 8
L_2: Introduce metadata part in .vnnlib file to persist image, image_class and epsilon for SDP-CROWN #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
L_2: Introduce metadata part in .vnnlib file to persist image, image_class and epsilon for SDP-CROWN #164
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8467268
Add image and image_class to vnnlib_property
henba1 d30d425
Save image and image_class as vnnlib_property sidecar
henba1 e51bf12
In implemmented property generators: epsilon, image and image class a…
henba1 db4d465
Fix linter issues
henba1 a0d3bfd
remove inline comment in save_vnnlib_property()
henba1 c9d0198
Refactor auto_verify_module slightly and make documentation text in v…
henba1 afddba4
sdpcrown: embed image/label/epsilon in vnnlib (no npz sidecar)
henba1 6f047a4
fix docstring misalignment with test
henba1 ff83bd8
remove redundant data from vnnlib_property
henba1 1a9d52a
make condition more readable
henba1 e535ed8
test for sdpcrown metadata
henba1 1b3281e
Merge branch 'main' into henba1-sdp-crown-changes
henba1 c6ac570
adjust test to include Ok(CompleteVerificationData(...)
henba1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
tests/test_verification_module/test_auto_verify_module_sdpcrown_metadata.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # Copyright 2025 ADA Reseach Group and VERONA council. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ============================================================================== | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| import torch | ||
| from result import Ok | ||
|
|
||
| from ada_verona.database.dataset.data_point import DataPoint | ||
| from ada_verona.database.machine_learning_model.onnx_network import ONNXNetwork | ||
| from ada_verona.database.verification_context import VerificationContext | ||
| from ada_verona.database.verification_result import CompleteVerificationData | ||
| from ada_verona.verification_module.auto_verify_module import AutoVerifyModule | ||
| from ada_verona.verification_module.property_generator.one2any_property_generator import One2AnyPropertyGenerator | ||
|
|
||
|
|
||
| class _CapturingVerifier: | ||
| def __init__(self, name: str): | ||
| self.name = name | ||
| self.seen_network: Path | None = None | ||
| self.seen_property: Path | None = None | ||
|
|
||
| def verify_property(self, network: Path, property: Path, **kwargs): # noqa: A002 | ||
| self.seen_network = network | ||
| self.seen_property = property | ||
| return Ok(CompleteVerificationData(result="UNSAT", took=0.0)) | ||
|
|
||
|
|
||
| def _parse_verona_header(vnnlib_text: str) -> tuple[float, int, np.ndarray]: | ||
| epsilon = None | ||
| image_class = None | ||
| image_csv = None | ||
|
|
||
| for raw_line in vnnlib_text.splitlines(): | ||
| line = raw_line.strip() | ||
| if not line.startswith(";"): | ||
| break | ||
| if line.startswith("; verona_epsilon:"): | ||
| epsilon = float(line.split(":", 1)[1].strip()) | ||
| elif line.startswith("; verona_image_class:"): | ||
| image_class = int(line.split(":", 1)[1].strip()) | ||
| elif line.startswith("; verona_image:"): | ||
| image_csv = line.split(":", 1)[1].strip() | ||
|
|
||
| assert epsilon is not None | ||
| assert image_class is not None | ||
| assert image_csv is not None | ||
|
|
||
| return epsilon, image_class, np.fromstring(image_csv, sep=",", dtype=np.float32) | ||
|
|
||
|
|
||
| def test_sdpcrown_injects_verona_metadata_header(tmp_path: Path): | ||
| network_path = tmp_path / "network.onnx" | ||
| network_path.write_text("", encoding="utf-8") | ||
|
|
||
| verification_context = VerificationContext( | ||
| network=ONNXNetwork(network_path), | ||
| data_point=DataPoint(id="1", label=2, data=torch.tensor([0.1, 0.2, 0.3], dtype=torch.float32)), | ||
| tmp_path=tmp_path, | ||
| property_generator=One2AnyPropertyGenerator(number_classes=10, data_lb=0, data_ub=1), | ||
| ) | ||
|
|
||
| verifier = _CapturingVerifier(name="sdpcrown") | ||
| module = AutoVerifyModule(verifier=verifier, timeout=1.0) | ||
|
|
||
| epsilon = 0.1 | ||
| result = module.verify(verification_context, epsilon=epsilon) | ||
| assert result.result == "UNSAT" | ||
|
|
||
| assert verifier.seen_property is not None | ||
| vnnlib_text = verifier.seen_property.read_text(encoding="utf-8") | ||
|
|
||
| assert vnnlib_text.startswith("; verona_metadata_version: 1\n") | ||
|
|
||
| parsed_epsilon, parsed_label, parsed_image = _parse_verona_header(vnnlib_text) | ||
| assert parsed_epsilon == pytest.approx(epsilon) | ||
| assert parsed_label == 2 | ||
| assert np.allclose(parsed_image, verification_context.data_point.data.detach().cpu().numpy().reshape(-1)) | ||
|
|
||
|
|
||
| def test_non_sdpcrown_does_not_inject_metadata(tmp_path: Path): | ||
| network_path = tmp_path / "network.onnx" | ||
| network_path.write_text("", encoding="utf-8") | ||
|
|
||
| verification_context = VerificationContext( | ||
| network=ONNXNetwork(network_path), | ||
| data_point=DataPoint(id="1", label=2, data=torch.tensor([0.1, 0.2, 0.3], dtype=torch.float32)), | ||
| tmp_path=tmp_path, | ||
| property_generator=One2AnyPropertyGenerator(number_classes=10, data_lb=0, data_ub=1), | ||
| ) | ||
|
|
||
| verifier = _CapturingVerifier(name="not_sdpcrown") | ||
| module = AutoVerifyModule(verifier=verifier, timeout=1.0) | ||
|
|
||
| result = module.verify(verification_context, epsilon=0.1) | ||
| assert result.result == "UNSAT" | ||
|
|
||
| assert verifier.seen_property is not None | ||
| vnnlib_text = verifier.seen_property.read_text(encoding="utf-8") | ||
| assert "; verona_metadata_version:" not in vnnlib_text |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.