File size: 1,645 Bytes
10979b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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