EnzymeTestUtils.test_forwardMethod
test_forward(f, Activity, args...; kwargs...)

Test Enzyme.autodiff of f in Forward-mode against finite differences.

f has all constraints of the same argument passed to Enzyme.autodiff, with additional constraints:

  • If it mutates one of its arguments, it must return that argument.

Arguments

  • Activity: the activity of the return value of f
  • args: Each entry is either an argument to f, an activity type accepted by autodiff, or a tuple of the form (arg, Activity), where Activity is the activity type of arg. If the activity type specified requires a tangent, a random tangent will be automatically generated.

Keywords

  • rng::AbstractRNG: The random number generator to use for generating random tangents.
  • fdm=FiniteDifferences.central_fdm(5, 1): The finite differences method to use.
  • fkwargs: Keyword arguments to pass to f.
  • rtol: Relative tolerance for isapprox.
  • atol: Absolute tolerance for isapprox.
  • testset_name: Name to use for a testset in which all tests are evaluated.

Examples

Here we test a rule for a function of scalars. Because we don't provide an activity annotation for y, it is assumed to be Const.

using Enzyme, EnzymeTestUtils

x, y = randn(2)
for Tret in (Const, Duplicated, DuplicatedNoNeed), Tx in (Const, Duplicated)
    test_forward(*, Tret, (x, Tx), y)
end

Here we test a rule for a function of an array in batch forward-mode:

x = randn(3)
y = randn()
for Tret in (Const, BatchDuplicated, BatchDuplicatedNoNeed),
    Tx in (Const, BatchDuplicated),
    Ty in (Const, BatchDuplicated)

    test_forward(*, Tret, (x, Tx), (y, Ty))
end
EnzymeTestUtils.test_reverseMethod
test_reverse(f, Activity, args...; kwargs...)

Test Enzyme.autodiff_thunk of f in ReverseSplitWithPrimal-mode against finite differences.

f has all constraints of the same argument passed to Enzyme.autodiff_thunk, with additional constraints:

  • If an Array{<:AbstractFloat} appears in the input/output, then a reshaped version of it may not also appear in the input/output.

Arguments

  • Activity: the activity of the return value of f.
  • args: Each entry is either an argument to f, an activity type accepted by autodiff, or a tuple of the form (arg, Activity), where Activity is the activity type of arg. If the activity type specified requires a shadow, one will be automatically generated.

Keywords

  • rng::AbstractRNG: The random number generator to use for generating random tangents.
  • fdm=FiniteDifferences.central_fdm(5, 1): The finite differences method to use.
  • fkwargs: Keyword arguments to pass to f.
  • rtol: Relative tolerance for isapprox.
  • atol: Absolute tolerance for isapprox.
  • testset_name: Name to use for a testset in which all tests are evaluated.

Examples

Here we test a rule for a function of scalars. Because we don't provide an activity annotation for y, it is assumed to be Const.

using Enzyme, EnzymeTestUtils

x = randn()
y = randn()
for Tret in (Const, Active), Tx in (Const, Active)
    test_reverse(*, Tret, (x, Tx), y)
end

Here we test a rule for a function of an array in batch reverse-mode:

x = randn(3)
for Tret in (Const, Active), Tx in (Const, BatchDuplicated)
    test_reverse(prod, Tret, (x, Tx))
end
EnzymeTestUtils.@test_msgMacro
@test_msg msg condion kws...

This is per Test.@test condion kws... except that if it fails it also prints the msg. If msg=="" then this is just like @test, nothing is printed

Examles

julia> @test_msg "It is required that the total is under 10" sum(1:1000) < 10;
Test Failed at REPL[1]:1
  Expression: sum(1:1000) < 10
  Problem: It is required that the total is under 10
   Evaluated: 500500 < 10
ERROR: There was an error during testing


julia> @test_msg "It is required that the total is under 10" error("not working at all");
Error During Test at REPL[2]:1
  Test threw exception
  Expression: error("not working at all")
  Problem: It is required that the total is under 10
  "not working at all"
  Stacktrace:

julia> a = "";

julia> @test_msg a sum(1:1000) < 10;
  Test Failed at REPL[153]:1
    Expression: sum(1:1000) < 10
     Evaluated: 500500 < 10
  ERROR: There was an error during testing