Import Tensorflow failed in sw

Hi, I'm a compiler developer who recently have interested in swift for tensorflow (S4TF).
This is amazing project, thanks for the development team.

Anyway, currently I've setup the development environment including swift-compiler itself with Tensorflow module and debug flag by following the instruction in GitHub - apple/swift at tensorflow

I ran

swift/utils/build-script --enable-tensorflow --release-debuginfo

Then I called swift REPL and tried to import Tensorflow but it falied with the following error.

➜ cd {swift-source directory}
➜ ./build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/bin/swift
(swift) import Tensorflow

<REPL Input>:1:8: error: cannot load module 'TensorFlow' as 'Tensorflow'
import Tensorflow

I don't even know how to use swift-compiler itself but I'd like to use it as pure-compiler from shell with VSCode not Xcode if I could because I'm intended to use it for normal machine learning and compiler development, not application dev for apple products.
Here, I have questions

  • do S4TF developers mandatorily use Xcode or not?
  • how to debug swift-compiler itself or any document about it?
  • how to import Tensorflow module or to build the compiler can do?

Hello @tkclimb,

Welcome to the Swift forums :slight_smile:

Since this PR #27596 was merged, you need to pass the flag --enable-experimental-differentiable-programming to the build-script so that you enable AutoDiff capabilities in the compiler + stdlib. Could you please try to run the build-script with this flag?

I use mainly Xcode but I've read that people use as well vim or VS Code. You can find here integration scripts for vim. For developing with swift using VS Code check out this extention.

You can use lldb to debug the compiler. There is a a wrapper to launch lldb including some helpers relevant for the swift compiler that you can find within your build directory : swift-macosx-x86_64/utils/lldb/lldb-with-tools and you can use it like this:

./lldb-with-tools -- ./swift /tmp/test.swift

Another option for you to play around with S4TF is to download a Development Snapshot, for instance the one from Nov. 11th, 2019. Install it and from the shell you can do:

$> export TOOLCHAINS=com.google.swift.20191111
$> swift
Welcome to Swift version 5.1.1-dev (Swift e242a8825f).
Type :help for assistance.
  1> import TensorFlow
PlatformPOSIX::DoLoadImage
path: libswiftTensorFlow.dylib
PlatformPOSIX::DoLoadImage
path: @rpath/libswiftTensorFlow.dylib
PlatformPOSIX::DoLoadImage
path: libswiftDarwin.dylib

@Victor_Guerra Thank you, it seems great information.
I'll try your suggestion. Hopefully I want to do some contribution to this amazing compiler stack.

error: cannot load module 'TensorFlow' as 'Tensorflow' occurs because TensorFlow has been misspelled as Tensorflow.

Could you please try import TensorFlow instead?

  • do S4TF developers mandatorily use Xcode or not?

Swift for TensorFlow has macOS and Ubuntu toolchains that work just like toolchains from Swift.org - Download Swift. Xcode is not mandatory!

In fact, there are some known issues regarding Xcode and Swift for TensorFlow toolchains (mailing list discussion). It's not clear whether these issues can be fixed via changes to Xcode's open source dependencies.

If you'd like to start a project, SwiftPM works more reliably:

$ mkdir Example
$ cd Example
$ swift package init
# Edit sources to import and use TensorFlow.
$ vim Sources/Example/Example.swift
$ swift test

# macOS: generate .xcodeproj to open project in Xcode.
$ swift package generate-xcodeproj
$ open Example.xcodeproj
  • how to debug swift-compiler itself or any document about it?

There's a great Debugging the Swift Compiler document with useful information.

  • how to import Tensorflow module or to build the compiler can do?

It seems like you followed the right steps! Try import TensorFlow instead.

@dan-zheng
Oh, I see... This is a silly mistake...
Now, I confirmed importing TF correctly has done. Many Thanks!

How about what is the difference of putting "--enable-experimental-differentiable-programming" that previous answer mentioned?

1 Like

The build-script flag --enable-experimental-differentiable-programming enables standard library additions for differentiable programming, including the Differentiable protocol.

However, passing it explicitly isn't necessary, as the default value is true on both tensorflow branch and master branch (to ensure testing by CI).

I've been using this flag unnecessarily in my local builds then :( ... thanks @dan-zheng for the clarification and sorry @tkclimb for the misleading info.

1 Like