Backup / gmnet /code /journal_exp /tests /test_synthetic.py
YFanwang's picture
Add files using upload-large-folder tool
10979b5 verified
Raw History Blame Contribute Delete
1.65 kB
from __future__ import annotations
import math
import torch
from gmnet.synthetic.metrics import alias_mask, energy_fraction, fft_power, tone_mask
from gmnet.synthetic.protocols import SyntheticExperiment, _tone, load_synthetic_config
def test_linear_control_preserves_single_tone() -> None:
device = torch.device("cpu")
signal = _tone(32, (3, 0), 1.0, 0.31, device)
support = tone_mask(32, [(3, 0)], device)
assert math.isclose(energy_fraction(fft_power(signal), support), 1.0, abs_tol=1e-12)
def test_alias_mask_excludes_fundamental_and_marks_wrapped_harmonic() -> None:
device = torch.device("cpu")
aliases = alias_mask(32, (12, 0), max_order=4, device=device)
fundamental = tone_mask(32, [(12, 0)], device)
wrapped_second = tone_mask(32, [(-8, 0)], device)
assert not bool((aliases & fundamental).any())
assert bool((aliases & wrapped_second).any())
def test_alias_mask_does_not_attribute_dc_collision() -> None:
device = torch.device("cpu")
aliases = alias_mask(32, (4, 0), max_order=8, device=device)
dc = tone_mask(32, [(0, 0)], device)
assert not bool((aliases & dc).any())
def test_small_protocol_has_linear_zero_and_nonlinear_generation() -> None:
config = load_synthetic_config("configs/e2_synthetic/test.yaml")
config["protocols"] = ["single_tone"]
rows = SyntheticExperiment(config).run()
linear = [row["harmonic_generation"] for row in rows if row["gate"] == "no_gate"]
nonlinear = [row["harmonic_generation"] for row in rows if row["gate"] == "relu6_self"]
assert max(abs(value) for value in linear) < 1e-12
assert min(nonlinear) > 0.1