Skip to content

Commit 7498390

Browse files
authored
[AutoDiff] Add build-script flag for differentiable programming. (#27596)
Add `--enable-experimental-differentiable-programming` build-script flag. The build-script flag enables/disables standard library additions related to differentiable programming. This will allow official Swift releases to disable these additions. The build-script flag is on by default to ensure testing of differentiable programming standard library additions. An additional driver flag must be enabled to use differentiable programming features: #27446 Mirror of #27595, with minor changes specific to tensorflow branch.
1 parent 0848df8 commit 7498390

File tree

7 files changed

+57
-6
lines changed

7 files changed

+57
-6
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ option(SWIFT_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING
392392
"Build stdlibCore with exclusivity checking enabled"
393393
FALSE)
394394

395+
option(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING
396+
"Enable experimental Swift differentiable programming features"
397+
FALSE)
398+
395399
#
396400
# End of user-configurable options.
397401
#

cmake/modules/AddSwift.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ function(_add_variant_swift_compile_flags
422422
list(APPEND result "-D" "SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
423423
endif()
424424

425+
if(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING)
426+
list(APPEND result "-D" "SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING")
427+
endif()
428+
425429
set("${result_var_name}" "${result}" PARENT_SCOPE)
426430
endfunction()
427431

stdlib/public/core/CMakeLists.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ set(SWIFTLIB_ESSENTIAL_GYB_SOURCES
197197
UnsafeRawBufferPointer.swift.gyb
198198
)
199199

200+
# Compile differentiable programming sources only if enabled.
201+
set(SWIFTLIB_DIFFERENTIABLE_PROGRAMMING_SOURCES)
202+
if(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING)
203+
list(APPEND SWIFTLIB_DIFFERENTIABLE_PROGRAMMING_SOURCES
204+
Differentiable.swift
205+
# SWIFT_ENABLE_TENSORFLOW
206+
DifferentiationSupport.swift)
207+
message(STATUS "Differentiable programming standard library additions enabled.")
208+
endif()
209+
200210
# The complete list of sources in the core standard library. Includes
201211
# all the essential sources listed above.
202212
set(SWIFTLIB_SOURCES
@@ -206,9 +216,6 @@ set(SWIFTLIB_SOURCES
206216
CollectionDifference.swift
207217
CollectionOfOne.swift
208218
Diffing.swift
209-
Differentiable.swift
210-
# SWIFT_ENABLE_TENSORFLOW
211-
DifferentiationSupport.swift
212219
Mirror.swift
213220
PlaygroundDisplay.swift
214221
CommandLine.swift
@@ -218,6 +225,7 @@ set(SWIFTLIB_SOURCES
218225
VarArgs.swift
219226
Zip.swift
220227
"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"
228+
${SWIFTLIB_DIFFERENTIABLE_PROGRAMMING_SOURCES}
221229
)
222230

223231
set(SWIFTLIB_GYB_SOURCES

utils/build_swift/driver_arguments.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,14 @@ def create_argument_parser():
10291029
'Currently only armv7 and aarch64 are supported. '
10301030
'%(default)s is the default.')
10311031

1032+
# -------------------------------------------------------------------------
1033+
in_group('Experimental language features')
1034+
1035+
option('--enable-experimental-differentiable-programming', toggle_true,
1036+
default=True,
1037+
help='Enable experimental Swift differentiable programming language'
1038+
' features.')
1039+
10321040
# -------------------------------------------------------------------------
10331041
in_group('Unsupported options')
10341042

utils/build_swift/tests/expected_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
'distcc': False,
124124
'dry_run': False,
125125
'enable_asan': False,
126+
'enable_experimental_differentiable_programming': True,
126127
'enable_lsan': False,
127128
'enable_sanitize_coverage': False,
128129
'disable_guaranteed_normal_arguments': False,
@@ -459,6 +460,7 @@ class IgnoreOption(_BaseOption):
459460
EnableOption('--build-swift-stdlib-unittest-extra'),
460461
EnableOption('--distcc'),
461462
EnableOption('--enable-asan'),
463+
EnableOption('--enable-experimental-differentiable-programming'),
462464
EnableOption('--enable-lsan'),
463465
EnableOption('--enable-sanitize-coverage'),
464466
EnableOption('--enable-tsan'),

utils/swift_build_support/swift_build_support/products/swift.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def __init__(self, args, toolchain, source_dir, build_dir):
4141
# Add any exclusivity checking flags for stdlibcore.
4242
self.cmake_options.extend(self._stdlibcore_exclusivity_checking_flags)
4343

44+
# Add experimental differentiable programming flag.
45+
self.cmake_options.extend(
46+
self._enable_experimental_differentiable_programming)
47+
4448
@property
4549
def _runtime_sanitizer_flags(self):
4650
sanitizer_list = []
@@ -112,3 +116,8 @@ def _force_optimized_typechecker_flags(self):
112116
def _stdlibcore_exclusivity_checking_flags(self):
113117
return [('SWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING:BOOL',
114118
self.args.enable_stdlibcore_exclusivity_checking)]
119+
120+
@property
121+
def _enable_experimental_differentiable_programming(self):
122+
return [('SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING:BOOL',
123+
self.args.enable_experimental_differentiable_programming)]

utils/swift_build_support/tests/products/test_swift.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def setUp(self):
5757
benchmark_num_o_iterations=3,
5858
disable_guaranteed_normal_arguments=True,
5959
force_optimized_typechecker=False,
60-
enable_stdlibcore_exclusivity_checking=False)
60+
enable_stdlibcore_exclusivity_checking=False,
61+
enable_experimental_differentiable_programming=False)
6162

6263
# Setup shell
6364
shell.dry_run = True
@@ -87,7 +88,8 @@ def test_by_default_no_cmake_options(self):
8788
expected = [
8889
'-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE',
8990
'-DSWIFT_FORCE_OPTIMIZED_TYPECHECKER:BOOL=FALSE',
90-
'-DSWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING:BOOL=FALSE'
91+
'-DSWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING:BOOL=FALSE',
92+
'-DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING:BOOL=FALSE'
9193
]
9294
self.assertEqual(set(swift.cmake_options), set(expected))
9395

@@ -102,7 +104,8 @@ def test_swift_runtime_tsan(self):
102104
'-DSWIFT_RUNTIME_USE_SANITIZERS=Thread',
103105
'-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE',
104106
'-DSWIFT_FORCE_OPTIMIZED_TYPECHECKER:BOOL=FALSE',
105-
'-DSWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING:BOOL=FALSE'
107+
'-DSWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING:BOOL=FALSE',
108+
'-DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING:BOOL=FALSE'
106109
]
107110
self.assertEqual(set(swift.cmake_options), set(flags_set))
108111

@@ -302,3 +305,16 @@ def test_exclusivity_checking_flags(self):
302305
'TRUE'],
303306
[x for x in swift.cmake_options
304307
if 'SWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING' in x])
308+
309+
def test_experimental_differentiable_programming_flags(self):
310+
self.args.enable_experimental_differentiable_programming = True
311+
swift = Swift(
312+
args=self.args,
313+
toolchain=self.toolchain,
314+
source_dir='/path/to/src',
315+
build_dir='/path/to/build')
316+
self.assertEqual(
317+
['-DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING:BOOL='
318+
'TRUE'],
319+
[x for x in swift.cmake_options
320+
if 'DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING' in x])

0 commit comments

Comments
 (0)