Skip to content

Commit 2d9111d

Browse files
authored
[AutoDiff] Enable control flow AD by default. (#25273)
Preemptively remove `-Xllvm -differentiation-enable-control-flow` flag. Note that limited control flow AD support is not yet robust: - Memory leaks: TF-552. - Incorrect zero gradients involving buffers + recursion: TF-554.
1 parent 5ac83e2 commit 2d9111d

File tree

8 files changed

+10
-38
lines changed

8 files changed

+10
-38
lines changed

lib/SILOptimizer/Mandatory/Differentiation.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ static llvm::cl::opt<bool> SkipFoldingAutoDiffFunctionExtraction(
6262
"differentiation-skip-folding-autodiff-function-extraction",
6363
llvm::cl::init(false));
6464

65-
/// This flag enables experimental differentiation of functions containing
66-
/// control flow. When this flag is true, adjoint generation is skipped and
67-
/// adjoint function references are replaced with undef. This is used for SIL
68-
/// testing purposes, specifically testing VJP generation before adjoint
69-
/// generation supports control flow.
70-
static llvm::cl::opt<bool> EnableControlFlow(
71-
"differentiation-enable-control-flow", llvm::cl::init(false));
72-
7365
//===----------------------------------------------------------------------===//
7466
// Helpers
7567
//===----------------------------------------------------------------------===//
@@ -1782,19 +1774,12 @@ static bool diagnoseUnsupportedControlFlow(ADContext &context,
17821774
diag::autodiff_loops_not_supported);
17831775
return true;
17841776
}
1785-
// Diagnose unsupported terminators.
1777+
// Diagnose unsupported branching terminators.
17861778
for (auto &bb : *original) {
17871779
auto *term = bb.getTerminator();
1788-
// Adjoint generation is not yet robust for control flow.
1789-
// If control flow is not enable, emit an error.
1790-
// Otherwise, `br` and `cond_br` instructions are supported terminators.
1791-
if (isa<BranchInst>(term) || isa<CondBranchInst>(term)) {
1792-
if (EnableControlFlow)
1793-
continue;
1794-
context.emitNondifferentiabilityError(
1795-
term, invoker, diag::autodiff_control_flow_not_supported);
1796-
return true;
1797-
}
1780+
// Supported terminators are: `br`, `cond_br`.
1781+
if (isa<BranchInst>(term) || isa<CondBranchInst>(term))
1782+
continue;
17981783
// If terminator is an unsupported branching terminator, emit an error.
17991784
if (term->isBranch()) {
18001785
context.emitNondifferentiabilityError(

test/AutoDiff/autodiff_diagnostics.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ func calls_grad_of_nested(_ x: Float) -> Float {
120120

121121
func if_else(_ x: Float, _ flag: Bool) -> Float {
122122
let y: Float
123-
// expected-error @+2 {{expression is not differentiable}}
124-
// expected-note @+1 {{differentiating control flow is not yet supported}}
125123
if flag {
126124
y = x + 1
127125
} else {

test/AutoDiff/control_flow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift-control-flow-differentiation
1+
// RUN: %target-run-simple-swift
22
// REQUIRES: executable_test
33

44
import StdlibUnittest

test/AutoDiff/control_flow_diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -emit-sil -verify -Xllvm -differentiation-enable-control-flow %s
1+
// RUN: %target-swift-frontend -emit-sil -verify %s
22

33
// Test supported `br` and `cond_br` terminators.
44

test/AutoDiff/control_flow_sil.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-swift-frontend -emit-sil -verify -Xllvm -differentiation-enable-control-flow %s | %FileCheck %s -check-prefix=CHECK-DATA-STRUCTURES
2-
// RUN: %target-swift-frontend -emit-sil -verify -Xllvm -differentiation-enable-control-flow %s | %FileCheck %s -check-prefix=CHECK-SIL
1+
// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s -check-prefix=CHECK-DATA-STRUCTURES
2+
// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s -check-prefix=CHECK-SIL
33

44
// TODO: Add adjoint SIL FileCheck tests.
55

test/AutoDiff/leakchecking.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift-control-flow-differentiation
1+
// RUN: %target-run-simple-swift
22
// REQUIRES: executable_test
33

44
// Test differentiation-related memory leaks.

test/TensorFlowRuntime/tensor_autodiff_control_flow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift-control-flow-differentiation
1+
// RUN: %target-run-simple-swift
22
// REQUIRES: executable_test
33
//
44
// FIXME(TF-326): Re-enable `-O` after deserialization failure fix.

test/lit.cfg

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,14 +1419,6 @@ if not getattr(config, 'target_run_simple_swift', None):
14191419
'%s %%t/a.out' % (config.target_build_swift,
14201420
mcp_opt, config.target_codesign,
14211421
config.target_run)))
1422-
# SWIFT_ENABLE_TENSORFLOW
1423-
# TODO: Remove when differentiation control flow support is robust.
1424-
config.target_run_simple_swift_control_flow_differentiation = (
1425-
'%%empty-directory(%%t) && '
1426-
'%s %s %%s -Xllvm -differentiation-enable-control-flow -o %%t/a.out %s -module-name main && '
1427-
'%s %%t/a.out &&'
1428-
'%s %%t/a.out'
1429-
% (config.target_build_swift, mcp_opt, swift_tensorflow_extra_options, config.target_codesign, config.target_run))
14301422
config.target_run_simple_swift = (
14311423
'%%empty-directory(%%t) && '
14321424
'%s %s %%s -o %%t/a.out %s -module-name main && '
@@ -1484,9 +1476,6 @@ config.substitutions.append(('%target-swift-frontend', config.target_swift_front
14841476

14851477

14861478
config.substitutions.append(('%target-run-simple-swiftgyb', config.target_run_simple_swiftgyb))
1487-
# SWIFT_ENABLE_TENSORFLOW
1488-
# TODO: Remove when differentiation control flow support is robust.
1489-
config.substitutions.append(('%target-run-simple-swift-control-flow-differentiation', config.target_run_simple_swift_control_flow_differentiation))
14901479
config.substitutions.append(('%target-run-simple-swift\(([^)]+)\)', config.target_run_simple_swift_parameterized))
14911480
config.substitutions.append(('%target-run-simple-swift', config.target_run_simple_swift))
14921481
config.substitutions.append(('%target-run-stdlib-swiftgyb', config.target_run_stdlib_swiftgyb))

0 commit comments

Comments
 (0)