Skip to content
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

Add test for vector inputs #7

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import math

import jax
import numpy as np
import pytest
from correctionlib import schemav2

Expand Down Expand Up @@ -41,18 +42,44 @@ def test_missing_input():


@pytest.mark.parametrize("jit", [False, True])
def test_scale(jit) -> None:
def test_evaluate_scale(jit):
cg = CorrectionWithGradient(schemas["scale"])

# evaluate
evaluate = jax.jit(cg.evaluate) if jit else cg.evaluate
value, grad = jax.value_and_grad(evaluate)(4.2)
assert math.isclose(value, 1.234)
assert grad.item() == 0.0

# eval_dict

@pytest.mark.parametrize("jit", [False, True])
def test_eval_dict_scale(jit):
cg = CorrectionWithGradient(schemas["scale"])
eval_dict = jax.jit(cg.eval_dict) if jit else cg.eval_dict
value, grad = jax.value_and_grad(eval_dict)({"x": 4.2})
assert math.isclose(value, 1.234)
assert list(grad.keys()) == ["x"]
assert grad["x"] == 0.0


@pytest.mark.parametrize("jit", [False, True])
def test_vectorized_evaluate_scale(jit):
cg = CorrectionWithGradient(schemas["scale"])
evaluate = jax.jit(cg.evaluate) if jit else cg.evaluate
x = np.array([0.0, 1.0])
values, grads = jax.value_and_grad(evaluate)(x)
assert np.allclose(values, [1.234, 1.234])
assert len(grads) == 2
assert grads[0] == 0.0
assert grads[1] == 0.0


@pytest.mark.parametrize("jit", [False, True])
def test_vectorized_eval_dict_scale(jit):
cg = CorrectionWithGradient(schemas["scale"])
eval_dict = jax.jit(cg.eval_dict) if jit else cg.eval_dict
x = np.array([0.0, 1.0])
values, grads = jax.value_and_grad(eval_dict)({"x": x})
assert np.allclose(values, [1.234, 1.234])
assert list(grads.keys()) == ["x"]
assert len(grads["x"]) == 2
assert grads["x"][0] == 0.0
assert grads["x"][1] == 0.0