Skip to content

Commit 5b94396

Browse files
authored
Merge pull request swiftlang#30918 from dan-zheng/autodiff-forward-mode
[AutoDiff] Enable forward-mode differentiation tests.
2 parents d99d8da + 3f06912 commit 5b94396

File tree

4 files changed

+62
-42
lines changed

4 files changed

+62
-42
lines changed

docs/Testing.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,34 @@ code for the target that is not the build machine:
275275

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

278+
* ``%target-run-simple-swift(`` *compiler arguments* ``)``: like
279+
``%target-run-simple-swift``, but enables specifying compiler arguments when
280+
compiling the Swift program.
281+
282+
Add ``REQUIRES: executable_test`` to the test.
283+
284+
* ``%target-run-simple-swiftgyb``: build a one-file Swift `.gyb` program and
285+
run it on the target machine.
286+
287+
Use this substitution for executable tests that don't require special
288+
compiler arguments.
289+
290+
Add ``REQUIRES: executable_test`` to the test.
291+
292+
* ``%target-run-simple-swiftgyb(`` *compiler arguments* ``)``: like
293+
``%target-run-simple-swiftgyb``, but enables specifying compiler arguments
294+
when compiling the Swift program.
295+
296+
Add ``REQUIRES: executable_test`` to the test.
297+
298+
* ``%target-run-simple-swift``: build a one-file Swift program and run it on
299+
the target machine.
300+
301+
Use this substitution for executable tests that don't require special
302+
compiler arguments.
303+
304+
Add ``REQUIRES: executable_test`` to the test.
305+
278306
* ``%target-run-stdlib-swift``: like ``%target-run-simple-swift`` with
279307
``-parse-stdlib -Xfrontend -disable-access-control``.
280308

test/AutoDiff/stdlib/floating_point.swift.gyb

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swiftgyb
1+
// RUN: %target-run-simple-swiftgyb(-Xfrontend -enable-experimental-forward-mode-differentiation)
22
// REQUIRES: executable_test
33

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

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

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

52-
// TODO(TF-1237): Upstream forward-mode differentiation.
53-
expectCrash {
54-
expectEqual(0, derivative(at: ${Self}(4), ${Self}(5), in: -))
55-
expectEqual(-5, differential(at: ${Self}(4), ${Self}(5), in: -)(${Self}(5), ${Self}(10)))
56-
}
49+
expectEqual(0, derivative(at: ${Self}(4), ${Self}(5), in: -))
50+
expectEqual(-5, differential(at: ${Self}(4), ${Self}(5), in: -)(${Self}(5), ${Self}(10)))
5751
}
5852

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

63-
// TODO(TF-1237): Upstream forward-mode differentiation.
64-
expectCrash {
65-
expectEqual(9, derivative(at: ${Self}(4), ${Self}(5), in: *))
66-
expectEqual(90, differential(at: ${Self}(4), ${Self}(5), in: *)(${Self}(10), ${Self}(10)))
67-
}
57+
expectEqual(9, derivative(at: ${Self}(4), ${Self}(5), in: *))
58+
expectEqual(90, differential(at: ${Self}(4), ${Self}(5), in: *)(${Self}(10), ${Self}(10)))
6859
}
6960

7061
FloatingPointDerivativeTests.test("${Self}./") {
@@ -79,11 +70,8 @@ FloatingPointDerivativeTests.test("${Self}./") {
7970
expectEqualWithTolerance(-1.6, dy)
8071
}
8172

82-
// TODO(TF-1237): Upstream forward-mode differentiation.
83-
expectCrash {
84-
expectEqualWithTolerance(0.04, derivative(at: ${Self}(4), ${Self}(5), in: /))
85-
expectEqual(90, differential(at: ${Self}(4), ${Self}(5), in: *)(${Self}(10), ${Self}(10)))
86-
}
73+
expectEqualWithTolerance(0.04, derivative(at: ${Self}(4), ${Self}(5), in: /))
74+
expectEqual(90, differential(at: ${Self}(4), ${Self}(5), in: *)(${Self}(10), ${Self}(10)))
8775
}
8876

8977
FloatingPointDerivativeTests.test("${Self}.squareRoot") {

test/AutoDiff/stdlib/tgmath_derivatives.swift.gyb

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swiftgyb
1+
// RUN: %target-run-simple-swiftgyb(-Xfrontend -enable-experimental-forward-mode-differentiation)
22
// REQUIRES: executable_test
33

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

5757
DerivativeTests.test("${op}_${T}") {
58-
%if op == 'derivative':
59-
// TODO(TF-1237): Upstream forward-mode differentiation and uncomment the next line.
60-
expectCrash {
61-
%end
6258
expectEqualWithTolerance(7.3890560989306502274, ${op}(at: 2 as ${T}, in: exp))
6359
expectEqualWithTolerance(2.772588722239781145, ${op}(at: 2 as ${T}, in: exp2))
6460
expectEqualWithTolerance(7.3890560989306502274, ${op}(at: 2 as ${T}, in: expm1))
@@ -86,10 +82,6 @@ DerivativeTests.test("${op}_${T}") {
8682
expectEqualWithTolerance(0, ${op}(at: 2 as ${T}, in: { round($0) }))
8783
expectEqualWithTolerance(0, ${op}(at: 2 as ${T}, in: { trunc($0) }))
8884

89-
%if op == 'derivative':
90-
} // `expectCrash` for forward-mode differentiation
91-
%end
92-
9385
// Differential operator specific tests.
9486

9587
// fma
@@ -99,10 +91,7 @@ DerivativeTests.test("${op}_${T}") {
9991
expectEqualWithTolerance(4, dfma.1)
10092
expectEqualWithTolerance(1, dfma.2)
10193
%else: # if op == 'derivative'
102-
// TODO(TF-1237): Upstream forward-mode differentiation.
103-
expectCrash {
104-
expectEqualWithTolerance(10, dfma)
105-
}
94+
expectEqualWithTolerance(10, dfma)
10695
%end
10796

10897
// remainder, fmod

test/lit.cfg

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ def use_interpreter_for_simple_runs():
785785
config.target_run_stdlib_swift = make_simple_target_run(stdlib=True)
786786
config.target_run_simple_swift = make_simple_target_run()
787787
config.target_run_simple_swift_parameterized = make_simple_target_run(parameterized=True)
788+
config.target_run_simple_swiftgyb_parameterized = make_simple_target_run(gyb=True, parameterized=True)
788789
config.available_features.add('interpret')
789790

790791
target_specific_module_triple = config.variant_triple
@@ -1609,13 +1610,24 @@ if not kIsWindows:
16091610
.format(all_stdlib_path, libdispatch_path)) + config.target_run
16101611

16111612
if not getattr(config, 'target_run_simple_swift', None):
1612-
config.target_run_simple_swift_parameterized = \
1613-
(SubstituteCaptures('%%empty-directory(%%t) && '
1614-
'%s %s %%s \\1 -o %%t/a.out -module-name main && '
1615-
'%s %%t/a.out && '
1616-
'%s %%t/a.out' % (config.target_build_swift,
1617-
mcp_opt, config.target_codesign,
1618-
config.target_run)))
1613+
config.target_run_simple_swift_parameterized = SubstituteCaptures(
1614+
"%%empty-directory(%%t) && "
1615+
"%s %s %%s \\1 -o %%t/a.out -module-name main && "
1616+
"%s %%t/a.out && "
1617+
"%s %%t/a.out"
1618+
% (config.target_build_swift, mcp_opt, config.target_codesign, config.target_run)
1619+
)
1620+
config.target_run_simple_swiftgyb_parameterized = SubstituteCaptures(
1621+
"%%empty-directory(%%t) && "
1622+
"%%gyb %%s -o %%t/main.swift && "
1623+
"%%line-directive %%t/main.swift -- "
1624+
"%s %s %%t/main.swift \\1 -o %%t/a.out -module-name main && "
1625+
"%s %%t/a.out && "
1626+
"%%line-directive %%t/main.swift -- "
1627+
"%s %%t/a.out"
1628+
% (config.target_build_swift, mcp_opt, config.target_codesign, config.target_run)
1629+
)
1630+
16191631
config.target_run_simple_swift = (
16201632
'%%empty-directory(%%t) && '
16211633
'%s %s %%s -o %%t/a.out -module-name main && '
@@ -1688,8 +1700,11 @@ config.substitutions.append(('%target-swift-frontend\(mock-sdk:([^)]+)\)',
16881700
config.substitutions.append(('%target-swift-frontend', config.target_swift_frontend))
16891701

16901702

1703+
config.substitutions.append(('%target-run-simple-swiftgyb\(([^)]+)\)',
1704+
config.target_run_simple_swiftgyb_parameterized))
16911705
config.substitutions.append(('%target-run-simple-swiftgyb', config.target_run_simple_swiftgyb))
1692-
config.substitutions.append(('%target-run-simple-swift\(([^)]+)\)', config.target_run_simple_swift_parameterized))
1706+
config.substitutions.append(('%target-run-simple-swift\(([^)]+)\)',
1707+
config.target_run_simple_swift_parameterized))
16931708
config.substitutions.append(('%target-run-simple-swift', config.target_run_simple_swift))
16941709
config.substitutions.append(('%target-run-stdlib-swiftgyb', config.target_run_stdlib_swiftgyb))
16951710
config.substitutions.append(('%target-run-stdlib-swift', config.target_run_stdlib_swift))

0 commit comments

Comments
 (0)