Skip to content

[AutoDiff] Enable control flow AD by default. #25273

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
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
23 changes: 4 additions & 19 deletions lib/SILOptimizer/Mandatory/Differentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ static llvm::cl::opt<bool> SkipFoldingAutoDiffFunctionExtraction(
"differentiation-skip-folding-autodiff-function-extraction",
llvm::cl::init(false));

/// This flag enables experimental differentiation of functions containing
/// control flow. When this flag is true, adjoint generation is skipped and
/// adjoint function references are replaced with undef. This is used for SIL
/// testing purposes, specifically testing VJP generation before adjoint
/// generation supports control flow.
static llvm::cl::opt<bool> EnableControlFlow(
"differentiation-enable-control-flow", llvm::cl::init(false));

//===----------------------------------------------------------------------===//
// Helpers
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -1783,19 +1775,12 @@ static bool diagnoseUnsupportedControlFlow(ADContext &context,
diag::autodiff_loops_not_supported);
return true;
}
// Diagnose unsupported terminators.
// Diagnose unsupported branching terminators.
for (auto &bb : *original) {
auto *term = bb.getTerminator();
// Adjoint generation is not yet robust for control flow.
// If control flow is not enable, emit an error.
// Otherwise, `br` and `cond_br` instructions are supported terminators.
if (isa<BranchInst>(term) || isa<CondBranchInst>(term)) {
if (EnableControlFlow)
continue;
context.emitNondifferentiabilityError(
term, invoker, diag::autodiff_control_flow_not_supported);
return true;
}
// Supported terminators are: `br`, `cond_br`.
if (isa<BranchInst>(term) || isa<CondBranchInst>(term))
continue;
// If terminator is an unsupported branching terminator, emit an error.
if (term->isBranch()) {
context.emitNondifferentiabilityError(
Expand Down
2 changes: 0 additions & 2 deletions test/AutoDiff/autodiff_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ func calls_grad_of_nested(_ x: Float) -> Float {

func if_else(_ x: Float, _ flag: Bool) -> Float {
let y: Float
// expected-error @+2 {{expression is not differentiable}}
// expected-note @+1 {{differentiating control flow is not yet supported}}
if flag {
y = x + 1
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/AutoDiff/control_flow.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift-control-flow-differentiation
// RUN: %target-run-simple-swift
// REQUIRES: executable_test

import StdlibUnittest
Expand Down
2 changes: 1 addition & 1 deletion test/AutoDiff/control_flow_diagnostics.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -emit-sil -verify -Xllvm -differentiation-enable-control-flow %s
// RUN: %target-swift-frontend -emit-sil -verify %s

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

Expand Down
4 changes: 2 additions & 2 deletions test/AutoDiff/control_flow_sil.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %target-swift-frontend -emit-sil -verify -Xllvm -differentiation-enable-control-flow %s | %FileCheck %s -check-prefix=CHECK-DATA-STRUCTURES
// RUN: %target-swift-frontend -emit-sil -verify -Xllvm -differentiation-enable-control-flow %s | %FileCheck %s -check-prefix=CHECK-SIL
// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s -check-prefix=CHECK-DATA-STRUCTURES
// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s -check-prefix=CHECK-SIL

// TODO: Add adjoint SIL FileCheck tests.

Expand Down
2 changes: 1 addition & 1 deletion test/AutoDiff/leakchecking.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift-control-flow-differentiation
// RUN: %target-run-simple-swift
// REQUIRES: executable_test

// Test differentiation-related memory leaks.
Expand Down
2 changes: 1 addition & 1 deletion test/TensorFlowRuntime/tensor_autodiff_control_flow.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift-control-flow-differentiation
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
//
// FIXME(TF-326): Re-enable `-O` after deserialization failure fix.
Expand Down
11 changes: 0 additions & 11 deletions test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1419,14 +1419,6 @@ if not getattr(config, 'target_run_simple_swift', None):
'%s %%t/a.out' % (config.target_build_swift,
mcp_opt, config.target_codesign,
config.target_run)))
# SWIFT_ENABLE_TENSORFLOW
# TODO: Remove when differentiation control flow support is robust.
config.target_run_simple_swift_control_flow_differentiation = (
'%%empty-directory(%%t) && '
'%s %s %%s -Xllvm -differentiation-enable-control-flow -o %%t/a.out %s -module-name main && '
'%s %%t/a.out &&'
'%s %%t/a.out'
% (config.target_build_swift, mcp_opt, swift_tensorflow_extra_options, config.target_codesign, config.target_run))
config.target_run_simple_swift = (
'%%empty-directory(%%t) && '
'%s %s %%s -o %%t/a.out %s -module-name main && '
Expand Down Expand Up @@ -1484,9 +1476,6 @@ config.substitutions.append(('%target-swift-frontend', config.target_swift_front


config.substitutions.append(('%target-run-simple-swiftgyb', config.target_run_simple_swiftgyb))
# SWIFT_ENABLE_TENSORFLOW
# TODO: Remove when differentiation control flow support is robust.
config.substitutions.append(('%target-run-simple-swift-control-flow-differentiation', config.target_run_simple_swift_control_flow_differentiation))
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))
Expand Down