Skip to content

looser numeric tolerance for TestUtils.test_interface #455

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 11 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "KernelFunctions"
uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
version = "0.10.37"
version = "0.10.38"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 1 addition & 1 deletion src/KernelFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ include("mokernels/lmm.jl")
include("chainrules.jl")
include("zygoterules.jl")

include("test_utils.jl")
include("TestUtils.jl")

function __init__()
@require Kronecker = "2c470bb0-bcc8-11e8-3dad-c9649493f05e" begin
Expand Down
34 changes: 22 additions & 12 deletions src/test_utils.jl → src/TestUtils.jl
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
module TestUtils

const __ATOL = 1e-9
const __RTOL = 1e-9

using Distances
using LinearAlgebra
using KernelFunctions
using Random
using Test

# default tolerance values for test_interface:
const __ATOL = sqrt(eps(Float64))
const __RTOL = sqrt(eps(Float64))
# ≈ 1.5e-8; chosen for no particular reason other than because it seems to
# satisfy our own test cases within KernelFunctions.jl

"""
test_interface(
k::Kernel,
x0::AbstractVector,
x1::AbstractVector,
x2::AbstractVector;
atol=__ATOL,
rtol=__RTOL,
)

Run various consistency checks on `k` at the inputs `x0`, `x1`, and `x2`.
`x0` and `x1` should be of the same length with different values, while `x0` and `x2` should
be of different lengths.

test_interface([rng::AbstractRNG], k::Kernel, T::Type{<:AbstractVector}; atol=__ATOL)
These tests are intended to pick up on really substantial issues with a kernel implementation
(e.g. substantial asymmetry in the kernel matrix, large negative eigenvalues), rather than to
test the numerics in detail, which can be kernel-specific.
The default value of `__ATOL` and `__RTOL` is `sqrt(eps(Float64)) ≈ 1.5e-8`, which satisfied
this intention in the cases tested within KernelFunctions.jl itself.

test_interface([rng::AbstractRNG], k::Kernel, T::Type{<:Real}; atol=__ATOL, rtol=__RTOL)

`test_interface` offers certain types of test data generation to make running these tests
require less code for common input types. For example, `Vector{<:Real}`, `ColVecs{<:Real}`,
and `RowVecs{<:Real}` are supported. For other input vector types, please provide the data
manually.
`test_interface` offers automated test data generation for kernels whose inputs are reals.
This will run the tests for `Vector{T}`, `Vector{Vector{T}}`, `ColVecs{T}`, and `RowVecs{T}`.
For other input vector types, please provide the data manually.
"""
function test_interface(
k::Kernel,
Expand All @@ -50,11 +59,12 @@ function test_interface(
@test size(kernelmatrix(k, x0, x2)) == (length(x0), length(x2))

# Check that elementwise is consistent with pairwise.
@test kernelmatrix_diag(k, x0, x1) ≈ diag(kernelmatrix(k, x0, x1)) atol = atol
@test kernelmatrix_diag(k, x0, x1) ≈ diag(kernelmatrix(k, x0, x1)) atol = atol rtol =
rtol

# Check additional binary elementwise properties for kernels.
@test kernelmatrix_diag(k, x0, x1) ≈ kernelmatrix_diag(k, x1, x0)
@test kernelmatrix(k, x0, x2) ≈ kernelmatrix(k, x2, x0)' atol = atol
@test kernelmatrix(k, x0, x2) ≈ kernelmatrix(k, x2, x0)' atol = atol rtol = rtol

# Check that unary elementwise basically works.
@test kernelmatrix_diag(k, x0) isa AbstractVector
Expand All @@ -63,10 +73,10 @@ function test_interface(
# Check that unary pairwise basically works.
@test kernelmatrix(k, x0) isa AbstractMatrix
@test size(kernelmatrix(k, x0)) == (length(x0), length(x0))
@test kernelmatrix(k, x0) ≈ kernelmatrix(k, x0)' atol = atol
@test kernelmatrix(k, x0) ≈ kernelmatrix(k, x0)' atol = atol rtol = rtol

# Check that unary elementwise is consistent with unary pairwise.
@test kernelmatrix_diag(k, x0) ≈ diag(kernelmatrix(k, x0)) atol = atol
@test kernelmatrix_diag(k, x0) ≈ diag(kernelmatrix(k, x0)) atol = atol rtol = rtol

# Check that unary pairwise produces a positive definite matrix (approximately).
@test eigmin(Matrix(kernelmatrix(k, x0))) > -atol
Expand Down