ian-do
ian-do

Reputation: 31

Can not import TensorFlow for Swift in Xcode Playground

I'm trying to use Swift for TensorFlow and have followed the directions found here: https://github.com/tensorflow/swift/blob/master/Installation.md

When I go ahead and import TensorFlow as such within a Swift Playground file:

import TensorFlow

I get this error: "The active toolchain is not compatible with playgrounds. libswiftCore.dylib could not be loaded"

I was able to use Swift for TensorFlow within the REPL so I know it should work. Anyone have any ideas as to how to fix this issue? It clearly works as shown in this demonstration: https://www.youtube.com/watch?time_continue=819&v=Yze693W4MaU

Upvotes: 3

Views: 1375

Answers (4)

Collierton
Collierton

Reputation: 549

As a synthesis of answers above and my own experience now with the latest May 8, 2020 builds and comments from the Google Group, I find:

SUCCESS

  • Xcode Project Command-line (as @8bitmp3 answered above)
  • Command-line swiftc (using the -O argument. Note this is the compiler)

FAILURE

  • Xcode Playground macOS (as @ian-do originally asked)
  • Command-line swift (and confirmed by Google Group. Note this is the interpreter)

For those interested in the command-line success (macOS 10.15.4), install the toolchain from here, create the source code file inference.swift per this page, and execute the following:

# Test this command. Expected outcome is your existing Xcode app as show below
$ xcrun -f swift
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift

# Switch to the new TensorFlow toolchain you installed
# Mine was located at /Library/Developer/Toolchains/swift-tensorflow-RELEASE-0.9.xctoolchain/
# Open the Info.plist file, locate CFBundleIdentifier, and copy the string value
# Mine was com.google.swift.20200507
$ export TOOLCHAINS=com.google.swift.20200507

# Test the switch to your TensorFlow toolchain. Note the different result
$ xcrun -f swift
/Library/Developer/Toolchains/swift-tensorflow-RELEASE-0.9.xctoolchain/usr/bin/swift

# Compile your Swift for TensorFlow source code
$ cd <directory with .swift file>
$ swiftc -O -sdk `xcrun --show-sdk-path` inference.swift

# Run the program
$ ./inference 
[[0.68070436]]

Upvotes: 1

user12657810
user12657810

Reputation:

Get the latest stable toolchain (e.g. v 0.6), install it, then go to Xcode > File > New > Project > Macos - Command Line Tools (instead of Playground). Additionally, the December 23, 2019 development toolchain—and beyond—should not require switching to the Legacy Build System. More information in this discussion here.

Upvotes: 2

fabmilo
fabmilo

Reputation: 48330

I had the same issue and it turns out is the playground type.

From the Google Group reply:

enter image description here

Upvotes: 1

anayini
anayini

Reputation: 247

Was running into this as well. Answered in the Google Group - turns out you need to make sure you are creating a macOS playground and not an iOS playground.

Upvotes: 1

Related Questions