"""Validate JAX-native certification dependency analysis.
The tests cover public behavior, differentiability, validation, and stable
scientific identity in the supported certification regime.
"""
import jax
import jax.numpy as jnp
from beartype.typing import Any
from diffpes.certify import (
clear_dependency_cache,
dependency_cache_info,
dependency_map,
information_spectrum,
linearized_forward,
sensitivity_map,
)
[docs]
class TestDependencyMap:
"""Verify :func:`~diffpes.certify.dependency_map`.
The cases cover the public behavior in the supported certification regime.
:see: :func:`~diffpes.certify.dependency_map`
"""
[docs]
def test_dependency_map_distinguishes_disconnected_leaf(self) -> None:
"""Trace only leaves consumed by the output JAXPR.
The case uses explicit inputs in the supported certification regime.
It checks the public result or the documented failure state.
Notes
-----
The test compares the result with explicit numerical or structural assertions.
"""
inputs: Any
result: Any
def forward(inputs: Any) -> Any:
x: Any
unused: Any
x, unused = inputs
del unused
return x**2
inputs = (jnp.array([2.0]), jnp.array([7.0]))
result = dependency_map("org.diffpes.model.test", forward, inputs)
assert result.structural.shape == (1, 2)
assert result.structural.tolist() == [[True, False]]
assert result.traced.tolist() == [[True, False]]
[docs]
class TestLinearizedForward:
"""Verify :func:`~diffpes.certify.linearized_forward`.
The cases cover the public behavior in the supported certification regime.
:see: :func:`~diffpes.certify.linearized_forward`
"""
[docs]
def test_linearization_reuses_exact_jvp(self) -> None:
"""Retain the linear map of a cubic function.
The case uses explicit inputs in the supported certification regime.
It checks the public result or the documented failure state.
Notes
-----
The test compares the result with explicit numerical or structural assertions.
"""
value: Any
pushforward: Any
value, pushforward = linearized_forward(
lambda x: x**3, jnp.array([2.0])
)
assert jnp.allclose(value, 8.0)
assert jnp.allclose(pushforward(jnp.ones(1)), 12.0)
[docs]
class TestSensitivityMap:
"""Verify :func:`~diffpes.certify.sensitivity_map`.
The cases cover the public behavior in the supported certification regime.
:see: :func:`~diffpes.certify.sensitivity_map`
"""
[docs]
class TestClearDependencyCache:
"""Verify :func:`~diffpes.certify.clear_dependency_cache`.
The case clears all structural entries and all counters.
:see: :func:`~diffpes.certify.clear_dependency_cache`
"""
[docs]
def test_clear_removes_entries_and_counters(self) -> None:
"""Clear the cache after one structural dependency analysis.
The cache must report no entries, hits, or misses after the clear.
Notes
-----
The test computes one linear dependency map before it clears the cache.
"""
def forward(value: Any) -> Any:
result: Any = 2.0 * value
return result
dependency_map(
"org.diffpes.model.cache.clear",
forward,
jnp.array([1.0]),
)
clear_dependency_cache()
info: tuple[int, int, int] = dependency_cache_info()
assert info == (0, 0, 0)
[docs]
class TestDependencyCacheInfo:
"""Verify :func:`~diffpes.certify.dependency_cache_info`.
The case counts one cache hit for one repeated static model shape.
:see: :func:`~diffpes.certify.dependency_cache_info`
"""
[docs]
def test_static_model_shape_has_one_structural_miss(self) -> None:
"""Verify one structural miss across two map evaluations.
The repeated model ID, callable, shape, and dtype use one cache entry.
Notes
-----
The test clears the cache and calls the same linear model two times.
"""
def forward(value: Any) -> Any:
result: Any = 2.0 * value
return result
clear_dependency_cache()
inputs: Any = jnp.array([1.0, 2.0])
dependency_map("org.diffpes.model.cache", forward, inputs)
dependency_map("org.diffpes.model.cache", forward, inputs)
info: tuple[int, int, int] = dependency_cache_info()
assert info == (1, 1, 1)