Skip to content

[AutoDiff] Enable forward-mode differentiation tests. #30918

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 2 commits into from
Apr 9, 2020
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
28 changes: 28 additions & 0 deletions docs/Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,34 @@ code for the target that is not the build machine:

Add ``REQUIRES: executable_test`` to the test.

* ``%target-run-simple-swift(`` *compiler arguments* ``)``: like
``%target-run-simple-swift``, but enables specifying compiler arguments when
compiling the Swift program.

Add ``REQUIRES: executable_test`` to the test.

* ``%target-run-simple-swiftgyb``: build a one-file Swift `.gyb` program and
run it on the target machine.

Use this substitution for executable tests that don't require special
compiler arguments.

Add ``REQUIRES: executable_test`` to the test.

* ``%target-run-simple-swiftgyb(`` *compiler arguments* ``)``: like
``%target-run-simple-swiftgyb``, but enables specifying compiler arguments
when compiling the Swift program.

Add ``REQUIRES: executable_test`` to the test.

* ``%target-run-simple-swift``: build a one-file Swift program and run it on
the target machine.

Use this substitution for executable tests that don't require special
compiler arguments.

Add ``REQUIRES: executable_test`` to the test.

* ``%target-run-stdlib-swift``: like ``%target-run-simple-swift`` with
``-parse-stdlib -Xfrontend -disable-access-control``.

Expand Down
30 changes: 9 additions & 21 deletions test/AutoDiff/stdlib/floating_point.swift.gyb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swiftgyb
// RUN: %target-run-simple-swiftgyb(-Xfrontend -enable-experimental-forward-mode-differentiation)
// REQUIRES: executable_test

#if (arch(i386) || arch(x86_64)) && !os(Windows)
Expand Down Expand Up @@ -38,33 +38,24 @@ FloatingPointDerivativeTests.test("${Self}.+") {
expectEqual((1, 1), gradient(at: ${Self}(4), ${Self}(5), in: +))
expectEqual((10, 10), pullback(at: ${Self}(4), ${Self}(5), in: +)(${Self}(10)))

// TODO(TF-1237): Upstream forward-mode differentiation.
expectCrash {
expectEqual(2, derivative(at: ${Self}(4), ${Self}(5), in: +))
expectEqual(20, differential(at: ${Self}(4), ${Self}(5), in: +)(${Self}(10), ${Self}(10)))
}
expectEqual(2, derivative(at: ${Self}(4), ${Self}(5), in: +))
expectEqual(20, differential(at: ${Self}(4), ${Self}(5), in: +)(${Self}(10), ${Self}(10)))
}

FloatingPointDerivativeTests.test("${Self}.-") {
expectEqual((1, -1), gradient(at: ${Self}(4), ${Self}(5), in: -))
expectEqual((10, -10), pullback(at: ${Self}(4), ${Self}(5), in: -)(${Self}(10)))

// TODO(TF-1237): Upstream forward-mode differentiation.
expectCrash {
expectEqual(0, derivative(at: ${Self}(4), ${Self}(5), in: -))
expectEqual(-5, differential(at: ${Self}(4), ${Self}(5), in: -)(${Self}(5), ${Self}(10)))
}
expectEqual(0, derivative(at: ${Self}(4), ${Self}(5), in: -))
expectEqual(-5, differential(at: ${Self}(4), ${Self}(5), in: -)(${Self}(5), ${Self}(10)))
}

FloatingPointDerivativeTests.test("${Self}.*") {
expectEqual((5, 4), gradient(at: ${Self}(4), ${Self}(5), in: *))
expectEqual((50, 40), pullback(at: ${Self}(4), ${Self}(5), in: *)(${Self}(10)))

// TODO(TF-1237): Upstream forward-mode differentiation.
expectCrash {
expectEqual(9, derivative(at: ${Self}(4), ${Self}(5), in: *))
expectEqual(90, differential(at: ${Self}(4), ${Self}(5), in: *)(${Self}(10), ${Self}(10)))
}
expectEqual(9, derivative(at: ${Self}(4), ${Self}(5), in: *))
expectEqual(90, differential(at: ${Self}(4), ${Self}(5), in: *)(${Self}(10), ${Self}(10)))
}

FloatingPointDerivativeTests.test("${Self}./") {
Expand All @@ -79,11 +70,8 @@ FloatingPointDerivativeTests.test("${Self}./") {
expectEqualWithTolerance(-1.6, dy)
}

// TODO(TF-1237): Upstream forward-mode differentiation.
expectCrash {
expectEqualWithTolerance(0.04, derivative(at: ${Self}(4), ${Self}(5), in: /))
expectEqual(90, differential(at: ${Self}(4), ${Self}(5), in: *)(${Self}(10), ${Self}(10)))
}
expectEqualWithTolerance(0.04, derivative(at: ${Self}(4), ${Self}(5), in: /))
expectEqual(90, differential(at: ${Self}(4), ${Self}(5), in: *)(${Self}(10), ${Self}(10)))
}

FloatingPointDerivativeTests.test("${Self}.squareRoot") {
Expand Down
15 changes: 2 additions & 13 deletions test/AutoDiff/stdlib/tgmath_derivatives.swift.gyb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swiftgyb
// RUN: %target-run-simple-swiftgyb(-Xfrontend -enable-experimental-forward-mode-differentiation)
// REQUIRES: executable_test

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
Expand Down Expand Up @@ -55,10 +55,6 @@ where T == T.TangentVector {
%for T in ['Float', 'Float80']:

DerivativeTests.test("${op}_${T}") {
%if op == 'derivative':
// TODO(TF-1237): Upstream forward-mode differentiation and uncomment the next line.
expectCrash {
%end
expectEqualWithTolerance(7.3890560989306502274, ${op}(at: 2 as ${T}, in: exp))
expectEqualWithTolerance(2.772588722239781145, ${op}(at: 2 as ${T}, in: exp2))
expectEqualWithTolerance(7.3890560989306502274, ${op}(at: 2 as ${T}, in: expm1))
Expand Down Expand Up @@ -86,10 +82,6 @@ DerivativeTests.test("${op}_${T}") {
expectEqualWithTolerance(0, ${op}(at: 2 as ${T}, in: { round($0) }))
expectEqualWithTolerance(0, ${op}(at: 2 as ${T}, in: { trunc($0) }))

%if op == 'derivative':
} // `expectCrash` for forward-mode differentiation
%end

// Differential operator specific tests.

// fma
Expand All @@ -99,10 +91,7 @@ DerivativeTests.test("${op}_${T}") {
expectEqualWithTolerance(4, dfma.1)
expectEqualWithTolerance(1, dfma.2)
%else: # if op == 'derivative'
// TODO(TF-1237): Upstream forward-mode differentiation.
expectCrash {
expectEqualWithTolerance(10, dfma)
}
expectEqualWithTolerance(10, dfma)
%end

// remainder, fmod
Expand Down
31 changes: 23 additions & 8 deletions test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ def use_interpreter_for_simple_runs():
config.target_run_stdlib_swift = make_simple_target_run(stdlib=True)
config.target_run_simple_swift = make_simple_target_run()
config.target_run_simple_swift_parameterized = make_simple_target_run(parameterized=True)
config.target_run_simple_swiftgyb_parameterized = make_simple_target_run(gyb=True, parameterized=True)
config.available_features.add('interpret')

target_specific_module_triple = config.variant_triple
Expand Down Expand Up @@ -1609,13 +1610,24 @@ if not kIsWindows:
.format(all_stdlib_path, libdispatch_path)) + config.target_run

if not getattr(config, 'target_run_simple_swift', None):
config.target_run_simple_swift_parameterized = \
(SubstituteCaptures('%%empty-directory(%%t) && '
'%s %s %%s \\1 -o %%t/a.out -module-name main && '
'%s %%t/a.out && '
'%s %%t/a.out' % (config.target_build_swift,
mcp_opt, config.target_codesign,
config.target_run)))
config.target_run_simple_swift_parameterized = SubstituteCaptures(
"%%empty-directory(%%t) && "
"%s %s %%s \\1 -o %%t/a.out -module-name main && "
"%s %%t/a.out && "
"%s %%t/a.out"
% (config.target_build_swift, mcp_opt, config.target_codesign, config.target_run)
)
config.target_run_simple_swiftgyb_parameterized = SubstituteCaptures(
"%%empty-directory(%%t) && "
"%%gyb %%s -o %%t/main.swift && "
"%%line-directive %%t/main.swift -- "
"%s %s %%t/main.swift \\1 -o %%t/a.out -module-name main && "
"%s %%t/a.out && "
"%%line-directive %%t/main.swift -- "
"%s %%t/a.out"
% (config.target_build_swift, mcp_opt, config.target_codesign, config.target_run)
)

config.target_run_simple_swift = (
'%%empty-directory(%%t) && '
'%s %s %%s -o %%t/a.out -module-name main && '
Expand Down Expand Up @@ -1688,8 +1700,11 @@ config.substitutions.append(('%target-swift-frontend\(mock-sdk:([^)]+)\)',
config.substitutions.append(('%target-swift-frontend', config.target_swift_frontend))


config.substitutions.append(('%target-run-simple-swiftgyb\(([^)]+)\)',
config.target_run_simple_swiftgyb_parameterized))
config.substitutions.append(('%target-run-simple-swiftgyb', config.target_run_simple_swiftgyb))
config.substitutions.append(('%target-run-simple-swift\(([^)]+)\)', config.target_run_simple_swift_parameterized))
config.substitutions.append(('%target-run-simple-swift\(([^)]+)\)',
config.target_run_simple_swift_parameterized))
config.substitutions.append(('%target-run-simple-swift', config.target_run_simple_swift))
config.substitutions.append(('%target-run-stdlib-swiftgyb', config.target_run_stdlib_swiftgyb))
config.substitutions.append(('%target-run-stdlib-swift', config.target_run_stdlib_swift))
Expand Down