Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 244a235

Browse files
authored
Initial compatibility with stock toolchain (#722)
* Adding missing _Differentiation and Numerics imports, temporarily gating off AnyLayer from stock toolchains. * Adding initial CMake support for stock toolchain. * Uncommenting the Numerics linking.
1 parent 6189d7a commit 244a235

11 files changed

+227
-3
lines changed

CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ set_target_properties(ArgumentParser PROPERTIES
148148
add_dependencies(ArgumentParser swift-argument-parser-install)
149149
add_dependencies(turbojpeg libjpeg-turbo-build)
150150

151+
option(ENABLE_SWIFT_NUMERICS
152+
"Enable integrating swift-numerics" YES)
153+
154+
include(CheckSwiftSourceCompiles)
155+
check_swift_source_compiles("struct S : KeyPathIterable { }" SWIFT_COMPILER_HAS_KEYPATHITERABLE_PROTOCOL)
156+
if(SWIFT_COMPILER_HAS_KEYPATHITERABLE_PROTOCOL)
157+
set(TENSORFLOW_USE_STANDARD_TOOLCHAIN_DEFAULT OFF)
158+
else()
159+
# if(CMAKE_Swift_COMPILER_VERSION VERSION_GREATER 5.3)
160+
message(WARNING "Swift compiler does not support KeyPathIterable protocol - assuming stock toolchain")
161+
set(TENSORFLOW_USE_STANDARD_TOOLCHAIN_DEFAULT ON)
162+
# endif()
163+
endif()
164+
165+
include(CMakeDependentOption)
166+
cmake_dependent_option(TENSORFLOW_USE_STANDARD_TOOLCHAIN
167+
"Experimental support to use a standard toolchain"
168+
${TENSORFLOW_USE_STANDARD_TOOLCHAIN_DEFAULT}
169+
"ENABLE_SWIFT_NUMERICS"
170+
NO)
171+
151172
add_subdirectory(Autoencoder)
152173
add_subdirectory(TensorBoard)
153174
add_subdirectory(Support)

Models/Text/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ add_library(TextModels
1616
WordSeg/SemiRing.swift)
1717
set_target_properties(TextModels PROPERTIES
1818
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
19+
target_compile_definitions(TextModels PRIVATE
20+
$<$<BOOL:${TENSORFLOW_USE_STANDARD_TOOLCHAIN}>:TENSORFLOW_USE_STANDARD_TOOLCHAIN>)
1921
target_compile_options(TextModels PRIVATE
2022
$<$<BOOL:${BUILD_TESTING}>:-enable-testing>)
2123
target_link_libraries(TextModels PUBLIC
2224
Checkpoints
2325
Datasets
26+
$<$<AND:$<BOOL:${ENABLE_SWIFT_NUMERICS}>,$<BOOL:${TENSORFLOW_USE_STANDARD_TOOLCHAIN}>>:Numerics>
2427
SwiftProtobuf)
2528

2629
install(TARGETS TextModels

Models/Text/WordSeg/Lattice.swift

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import _Differentiation
1516
import ModelSupport
1617
import TensorFlow
1718

Models/Text/WordSeg/Model.swift

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
import ModelSupport
2424
import TensorFlow
2525

26+
#if TENSORFLOW_USE_STANDARD_TOOLCHAIN
27+
import Numerics
28+
#endif
29+
2630
/// Types that can be optimized by an optimizer.
2731
///
2832
/// TODO: Consider promoting this into a public protocol in swift-apis?

Models/Text/WordSeg/SemiRing.swift

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import _Differentiation
16+
1517
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
1618
import Darwin
1719
#elseif os(Windows)

Package.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ let package = Package(
2727
.package(url: "https://github.com/apple/swift-protobuf.git", from: "1.10.0"),
2828
.package(url: "https://github.com/apple/swift-argument-parser", .branch("main")),
2929
.package(url: "https://github.com/google/swift-benchmark", from: "0.1.0"),
30+
.package(url: "https://github.com/apple/swift-numerics", .branch("main")),
3031
],
3132
targets: [
3233
.target(
@@ -35,12 +36,15 @@ let package = Package(
3536
.target(name: "Datasets", dependencies: ["ModelSupport"], path: "Datasets"),
3637
.target(name: "STBImage", path: "Support/STBImage"),
3738
.target(
38-
name: "ModelSupport", dependencies: ["STBImage"], path: "Support",
39-
exclude: ["STBImage"]),
39+
name: "ModelSupport",
40+
dependencies: ["STBImage", .product(name: "Numerics", package: "swift-numerics"),],
41+
path: "Support", exclude: ["STBImage"]),
4042
.target(name: "TensorBoard", dependencies: ["SwiftProtobuf", "ModelSupport", "TrainingLoop"], path: "TensorBoard"),
4143
.target(name: "ImageClassificationModels", path: "Models/ImageClassification"),
4244
.target(name: "VideoClassificationModels", path: "Models/Spatiotemporal"),
43-
.target(name: "TextModels", dependencies: ["Checkpoints", "Datasets", "SwiftProtobuf"], path: "Models/Text"),
45+
.target(name: "TextModels",
46+
dependencies: ["Checkpoints", "Datasets", "SwiftProtobuf", .product(name: "Numerics", package: "swift-numerics")],
47+
path: "Models/Text"),
4448
.target(name: "RecommendationModels", path: "Models/Recommendation"),
4549
.target(name: "TrainingLoop", dependencies: ["ModelSupport"], path: "TrainingLoop"),
4650
.target(

Support/AnyLayer.swift

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// TODO: Re-enable this for the stock toolchain when it can be realigned with VectorProtocol.
16+
#if !TENSORFLOW_USE_STANDARD_TOOLCHAIN
117
import TensorFlow
218
import _Differentiation
319

@@ -253,3 +269,4 @@ extension AnyLayer: Layer {
253269
return _callAsFunction(input)
254270
}
255271
}
272+
#endif

Support/AnyLayerTangentVector.swift

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// TODO: Re-enable this for the stock toolchain when it can be realigned with VectorProtocol.
16+
#if !TENSORFLOW_USE_STANDARD_TOOLCHAIN
117
import TensorFlow
218
import _Differentiation
319

@@ -824,3 +840,4 @@ extension AnyLayerTangentVector: ElementaryFunctions {
824840
return .init(box: x.box._root(n))
825841
}
826842
}
843+
#endif

Support/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ add_library(ModelSupport
2020
Text/WordSeg/Lexicon.swift)
2121
set_target_properties(ModelSupport PROPERTIES
2222
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
23+
target_compile_definitions(ModelSupport PRIVATE
24+
$<$<BOOL:${TENSORFLOW_USE_STANDARD_TOOLCHAIN}>:TENSORFLOW_USE_STANDARD_TOOLCHAIN>)
2325
target_compile_options(ModelSupport PRIVATE
2426
$<$<BOOL:${BUILD_TESTING}>:-enable-testing>)
2527
target_link_libraries(ModelSupport PUBLIC

Tests/SupportTests/AnyLayerTests.swift

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ import XCTest
1616
import ModelSupport
1717
import TensorFlow
1818

19+
// TODO: Re-enable these once AnyLayer can be aligned with the new VectorProtocol.
20+
#if TENSORFLOW_USE_STANDARD_TOOLCHAIN
21+
final class AnyLayerTests: XCTestCase {
22+
static var allTests = [(String, (XCTestCase) -> () -> Void)]()
23+
}
24+
#else
25+
1926
struct ElementaryFunctionsTests<WrapperTestType: XCTestCase, Reference: ElementaryFunctions, Test: ElementaryFunctions> {
2027
let createReference: ([Float]) -> Reference
2128
let referenceToTest: (Reference) -> Test
@@ -167,3 +174,4 @@ final class AnyLayerTests: XCTestCase {
167174
("testTangentVectorElementaryFunctions", testTangentVectorElementaryFunctions),
168175
]
169176
}
177+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2+
# file Copyright.txt or https://cmake.org/licensing for details.
3+
4+
#[=======================================================================[.rst:
5+
CheckSwiftSourceCompiles
6+
----------------------
7+
8+
Check if given Swift source compiles and links into an executable.
9+
10+
.. command:: check_swift_source_compiles
11+
12+
.. code-block:: cmake
13+
14+
check_swift_source_compiles(<code> <resultVar>
15+
[FAIL_REGEX <regex1> [<regex2>...]])
16+
17+
Check that the source supplied in ``<code>`` can be compiled as a Swift source
18+
file and linked as an executable. The result will be stored in the internal
19+
cache variable specified by ``<resultVar>``, with a boolean true value for
20+
success and boolean false for failure. If ``FAIL_REGEX`` is provided, then
21+
failure is determined by checking if anything in the output matches any of the
22+
specified regular expressions.
23+
24+
The underlying check is performed by the :command:`try_compile` command. The
25+
compile and link commands can be influenced by setting any of the following
26+
variables prior to calling ``check_swift_source_compiles()``:
27+
28+
``CMAKE_REQUIRED_FLAGS``
29+
Additional flags to pass to the compiler. Note that the contents of
30+
:variable:`CMAKE_Swift_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
31+
configuration-specific variable are automatically added to the compiler
32+
command before the contents of ``CMAKE_REQUIRED_FLAGS``.
33+
34+
``CMAKE_REQUIRED_DEFINITIONS``
35+
A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
36+
``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
37+
``<resultVar>`` will also be added automatically.
38+
39+
``CMAKE_REQUIRED_INCLUDES``
40+
A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
41+
the compiler. These will be the only header search paths used by
42+
``try_compile()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
43+
directory property will be ignored.
44+
45+
``CMAKE_REQUIRED_LINK_OPTIONS``
46+
A :ref:`;-list <CMake Language Lists>` of options to add to the link
47+
command (see :command:`try_compile` for further details).
48+
49+
``CMAKE_REQUIRED_LIBRARIES``
50+
A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
51+
command. These can be the name of system libraries or they can be
52+
:ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
53+
further details).
54+
55+
``CMAKE_REQUIRED_QUIET``
56+
If this variable evaluates to a boolean true value, all status messages
57+
associated with the check will be suppressed.
58+
59+
The check is only performed once, with the result cached in the variable
60+
named by ``<resultVar>``. Every subsequent CMake run will re-use this cached
61+
value rather than performing the check again, even if the ``<code>`` changes.
62+
In order to force the check to be re-evaluated, the variable named by
63+
``<resultVar>`` must be manually removed from the cache.
64+
65+
#]=======================================================================]
66+
67+
include_guard(GLOBAL)
68+
69+
macro(CHECK_Swift_SOURCE_COMPILES SOURCE VAR)
70+
if(NOT DEFINED "${VAR}")
71+
set(_FAIL_REGEX)
72+
set(_key)
73+
foreach(arg ${ARGN})
74+
if("${arg}" MATCHES "^(FAIL_REGEX)$")
75+
set(_key "${arg}")
76+
elseif(_key)
77+
list(APPEND _${_key} "${arg}")
78+
else()
79+
message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
80+
endif()
81+
endforeach()
82+
83+
set(MACRO_CHECK_FUNCTION_DEFINITIONS
84+
"-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
85+
if(CMAKE_REQUIRED_LINK_OPTIONS)
86+
set(CHECK_Swift_SOURCE_COMPILES_ADD_LINK_OPTIONS
87+
LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
88+
else()
89+
set(CHECK_Swift_SOURCE_COMPILES_ADD_LINK_OPTIONS)
90+
endif()
91+
if(CMAKE_REQUIRED_LIBRARIES)
92+
set(CHECK_Swift_SOURCE_COMPILES_ADD_LIBRARIES
93+
LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
94+
else()
95+
set(CHECK_Swift_SOURCE_COMPILES_ADD_LIBRARIES)
96+
endif()
97+
if(CMAKE_REQUIRED_INCLUDES)
98+
set(CHECK_Swift_SOURCE_COMPILES_ADD_INCLUDES
99+
"-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
100+
else()
101+
set(CHECK_Swift_SOURCE_COMPILES_ADD_INCLUDES)
102+
endif()
103+
file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.swift"
104+
"${SOURCE}\n")
105+
106+
if(NOT CMAKE_REQUIRED_QUIET)
107+
message(CHECK_START "Performing Test ${VAR}")
108+
endif()
109+
try_compile(${VAR}
110+
${CMAKE_BINARY_DIR}
111+
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.swift
112+
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
113+
${CHECK_Swift_SOURCE_COMPILES_ADD_LINK_OPTIONS}
114+
${CHECK_Swift_SOURCE_COMPILES_ADD_LIBRARIES}
115+
CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
116+
"${CHECK_Swift_SOURCE_COMPILES_ADD_INCLUDES}"
117+
OUTPUT_VARIABLE OUTPUT)
118+
119+
foreach(_regex ${_FAIL_REGEX})
120+
if("${OUTPUT}" MATCHES "${_regex}")
121+
set(${VAR} 0)
122+
endif()
123+
endforeach()
124+
125+
if(${VAR})
126+
set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
127+
if(NOT CMAKE_REQUIRED_QUIET)
128+
message(CHECK_PASS "Success")
129+
endif()
130+
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
131+
"Performing Swift SOURCE FILE Test ${VAR} succeeded with the following output:\n"
132+
"${OUTPUT}\n"
133+
"Source file was:\n${SOURCE}\n")
134+
else()
135+
if(NOT CMAKE_REQUIRED_QUIET)
136+
message(CHECK_FAIL "Failed")
137+
endif()
138+
set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
139+
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
140+
"Performing Swift SOURCE FILE Test ${VAR} failed with the following output:\n"
141+
"${OUTPUT}\n"
142+
"Source file was:\n${SOURCE}\n")
143+
endif()
144+
endif()
145+
endmacro()

0 commit comments

Comments
 (0)