Skip to content
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

Add tensorflow/swift-apis as a SwiftPM dependency. #727

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dan-zheng
Copy link
Member

@dan-zheng dan-zheng commented Jan 7, 2021

Motivation

This enables building tensorflow/swift-models using stock toolchains from swift.org/download.

swift build will clone and build tensorflow/swift-apis as a regular SwiftPM dependency. Eventually, we would like to stop releasing custom toolchains bundled with pre-installed tensorflow/swift-apis.

Build instructions

It is possible to build tensorflow/swift-apis and dependencies like tensorflow/swift-models using stock toolchains by installing pre-built X10 libraries (currently available only for macOS and Windows).

After installing (e.g. to $HOME/Library on macOS), build with SwiftPM via the following:

$ swift build -Xcc -I$HOME/Library/tensorflow-2.4.0/usr/include -Xlinker -L$HOME/Library/tensorflow-2.4.0/usr/lib -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN

swift test is known not to work on macOS for tensorflow/swift-apis and dependencies due to SR-14008: Library not loaded: /usr/lib/swift/libswift_Differentiation.dylib.

Testing

Before merging, let's verify that swift build, swift run, and swift test works for swift.org/download toolchains across platforms:

$ swift run Benchmarks -Xcc -I$HOME/Library/tensorflow-2.4.0/usr/include -Xlinker -L$HOME/Library/tensorflow-2.4.0/usr/lib -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN
ld: warning: dylib (/Users/danielzheng/Library/tensorflow-2.4.0/usr/lib/libx10.dylib) was built for newer macOS version (10.15) than being linked (10.13)
ld: warning: dylib (/Users/danielzheng/Library/tensorflow-2.4.0/usr/lib/libx10.dylib) was built for newer macOS version (10.15) than being linked (10.13)
[2/2] Linking Benchmarks

* Build Completed!
dyld: Library not loaded: @rpath/libx10.dylib
Referenced from: /Users/danielzheng/swift-models/.build/x86_64-apple-macosx/debug/Benchmarks
Reason: image not found
[1]    78739 abort      swift run Benchmarks -Xcc -I$HOME/Library/tensorflow-2.4.0/usr/include
$ swift test -Xcc -I$HOME/Library/tensorflow-2.4.0/usr/include -Xlinker -L$HOME/Library/tensorflow-2.4.0/usr/lib -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN
2021-01-08 07:11:00.826 xctest[78981:2111298] The bundle “swift-modelsPackageTests.xctest” couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle.
2021-01-08 07:11:00.826 xctest[78981:2111298] (dlopen_preflight(/Users/danielzheng/swift-models/.build/x86_64-apple-macosx/debug/swift-modelsPackageTests.xctest/Contents/MacOS/swift-modelsPackageTests): Library not loaded: /usr/lib/swift/libswift_Differentiation.dylib
Referenced from: /Users/danielzheng/swift-models/.build/x86_64-apple-macosx/debug/swift-modelsPackageTests.xctest/Contents/MacOS/swift-modelsPackageTests
Reason: image not found)

This enables building tensorflow/swift-models using stock toolchains
from swift.org/download.
Copy link
Contributor

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The review comments are basically a styling nit.

.target(name: "ImageClassificationModels", path: "Models/ImageClassification"),
.target(name: "VideoClassificationModels", path: "Models/Spatiotemporal"),
.target(name: "TextModels",
name: "ModelSupport", dependencies: ["TensorFlow", "STBImage"], path: "Support",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While doing this, it might be nice to format this as:

.target(
  name: "ModelSupport",
  dependencies: [
    "TensorFlow",
    "STBImage",
  ],
  path: "Support"),

and similarly throughout. It makes it easier to read at a glance due to the one-item per line. It also makes future changes trivial to read.

@novakov-alexey-zz
Copy link

I can confirm the same run failure locally:

 swift-linear-regression % swift run -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN -Xcc -I/Library/tensorflow-2.4.0/usr/include -Xlinker -L/Library/tensorflow-2.4.0/usr/lib
dyld: Library not loaded: @rpath/libx10.dylib
  Referenced from: /Users/Alexey_Novakov/dev/git/swift-linear-regression/.build/x86_64-apple-macosx/debug/libTensorFlow.dylib
  Reason: image not found
zsh: abort      swift run -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN -Xcc  -Xlinker

@philipturner
Copy link

philipturner commented Feb 18, 2022

I can confirm the same run failure locally:

 swift-linear-regression % swift run -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN -Xcc -I/Library/tensorflow-2.4.0/usr/include -Xlinker -L/Library/tensorflow-2.4.0/usr/lib
dyld: Library not loaded: @rpath/libx10.dylib
  Referenced from: /Users/Alexey_Novakov/dev/git/swift-linear-regression/.build/x86_64-apple-macosx/debug/libTensorFlow.dylib
  Reason: image not found
zsh: abort      swift run -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN -Xcc  -Xlinker

That may be a different failure. _Differentiation should now load. You could try copying the libx10.dylib binary into the Swift build products folder after compiling. In addition, it should not be necessary to pass libx10 into the linker settings for swift build or swift test, at least on macOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants