tensorflow / swift

Swift for TensorFlow
https://tensorflow.org/swift
Apache License 2.0
6.13k stars 606 forks source link

Can't run in macOS SwiftUI App #602

Open jonkan opened 3 years ago

jonkan commented 3 years ago

I'm encountering a runtime error if I try to use TensorFlow in an SwiftUI App:

dyld: Symbol not found: _$sSo12NSFileHandleC10FoundationE9readToEndAC4DataVSgyKF
  Referenced from: /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI
  Expected in: /Library/Developer/Toolchains/swift-tensorflow-RELEASE-0.12.xctoolchain/usr/lib/swift/macosx/libswiftFoundation.dylib
 in /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI

Setup:

Steps to reproduce:

func test() -> String { let x = Tensor([[1, 2], [3, 4]]) return String(describing: x + x) }


- Run (no need to call the function)

Also, if choosing `Command Line Tool` instead of `App`, everything works as expected.
porterchild commented 3 years ago

One thing you can try is to look at the arguments for a successful build, and try adding some of those arguments to the scheme of the unsuccessful build.

You can find the successful arguments by building as Command Line Tool (since that works), then going the the Report Navigator tab in Xcode, clicking on the build, then the "Export..." button at the top of the middle pane, which will give you all the build arguments. The export arguments might be interesting since you've got a dyld runtime linker issue. Maybe try pasting some of them in to Edit Scheme -> Arguments -> Environment Variables of your unsuccessful build.

I haven't seen that exact error before, but I've solved other dyld: Symbol not found errors with export DYLD_FRAMEWORK_PATH=path to my other built frameworks or export DYLD_LIBRARY_PATH=<path to toolchain>/usr/lib/swift/macosx/ depending on what Referenced from: and Expected in: said in the error.

jonkan commented 3 years ago

Thank you for your tips. I wasn't able to get anywhere though..

The error seems to indicate that SwiftUI wants some symbol that's missing from tensorflow's libswiftFoundation.dylib. Then I would've expected to find it in the "default" libswiftFoundation.dylib. But grep:ing for the symbol (as suggested here) showed nothing, which is weird: otool -tV /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.0/macosx/libswiftFoundation.dylib | grep '_$sSo12NSFileHandleC10FoundationE9readToEndAC4DataVSgyKF' Not really sure that's the "default" libswiftFoundation.dylib though, but it was my best guess from find.

Even weirder, I do find the symbol in: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftFoundation.dylib

I did try setting DYLD_LIBRARY_PATH to /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.0/macosx in the scheme environment variables, which gave me another error:

dyld: Symbol not found: _$s2os28getNullTerminatedUTF8Pointer_5usingSVSS_SpyypGSgztF
  Referenced from: /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI
  Expected in: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.0/macosx/libswiftos.dylib
 in /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI

But don't really know what's going on, or what I'm doing trying to troubleshoot this.. :P

It does seem to be some library version inconsistencies though. Found a post with a very similar issue and explanation in the swift forums. Can't really draw any conclusions from it though, is SwiftUI-support doomed?

BradLarson commented 3 years ago

If you try to use a toolchain from Swift.org to build your application and ignore the TensorFlow bits for now, does that work? There may have been some significant changes between when we last had a successful macOS toolchain build and upstream Swift.

If a Swift.org toolchain works to build your application, that would narrow it down to something wrong in the 0.12 macOS toolchain, which was still using the custom tensorflow branch of the compiler.

jonkan commented 3 years ago

Hmm, no..

Swift 5.3.3 Release

dyld: Symbol not found: _$sSo12NSFileHandleC10FoundationE9readToEndAC4DataVSgyKF
  Referenced from: /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI
  Expected in: /Library/Developer/Toolchains/swift-5.3.3-RELEASE.xctoolchain/usr/lib/swift/macosx/libswiftFoundation.dylib
 in /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI

Trunk Development (main)

dyld: Symbol not found: _$sSo12NSFileHandleC10FoundationE9readToEndAC4DataVSgyKF
  Referenced from: /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI
  Expected in: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-01-27-a.xctoolchain/usr/lib/swift/macosx/libswiftFoundation.dylib
 in /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI

Swift 5.4 Development

dyld: Symbol not found: _$sSo12NSFileHandleC10FoundationE9readToEndAC4DataVSgyKF
  Referenced from: /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI
  Expected in: /Library/Developer/Toolchains/swift-5.4-DEVELOPMENT-SNAPSHOT-2021-01-23-a.xctoolchain/usr/lib/swift/macosx/libswiftFoundation.dylib
 in /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI

Swift 5.3 Development

dyld: Symbol not found: _$sSo12NSFileHandleC10FoundationE9readToEndAC4DataVSgyKF
  Referenced from: /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI
  Expected in: /Library/Developer/Toolchains/swift-5.3-DEVELOPMENT-SNAPSHOT-2020-11-11-a.xctoolchain/usr/lib/swift/macosx/libswiftFoundation.dylib
 in /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI

Edit: Unchecking the "Disable Library Validation" does make it run for all of the above and the tensorflow 0.12 toolchain..??