Importing third-party packages before TensorFlow causes a runtime error #4
Description
Continuing our discussion from the group here.
Full background - I've just copied my comment directly from the group:
I've had some success in using third-party SPM packages by creating a dynamic library and linking to it when launching the REPL, however, it seems like the import order of TensorFlow vs other packages is important; importing the 3rd-party lib first causes a C++ runtime error in TensorFlow.
Here's some snippets:
Package.swift
import PackageDescription
let package = Package(
name: "TFExample",
products: [
.library(
name: "TFExample",
type: .dynamic, // allow use of this package and it's deps from the REPL
targets: ["TFExample"]
)
],
dependencies: [
.package(url: "https://github.com/ReactiveX/RxSwift.git", "4.0.0" ..< "5.0.0")
],
targets: [
.target(
name: "TFExample",
dependencies: ["RxSwift"]),
.testTarget(
name: "TFExampleTests",
dependencies: ["TFExample"]),
]
)
... then we just fetch dependencies and build with vanilla commands, then invoke the REPL:
Invocation
swift -I/usr/lib/swift/clang/include -I/usr/src/TFExample/.build/debug -L/usr/src/TFExample/.build/debug -lTFExample
At this point, I'm able to import RxSwift and TensorFlow in the REPL without errors in any order; however, when I actually interact with the packages, the incorrect import order does result in a runtime error:
Scenario 1 (OK)
1> import TensorFlow
2> import RxSwift
3> _ = Observable.from([1,2]).subscribe(onNext: { print($0) })
1
2
4> var x = Tensor([[1, 2], [3, 4]])
2018-04-27 17:13:12.514107: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
x: TensorFlow.Tensor<Double> = [[1.0, 2.0], [3.0, 4.0]]
Scenario 2 (runtime error)
1> import RxSwift
2> import TensorFlow
3> _ = Observable.from([1,2]).subscribe(onNext: { print($0) })
1
2
4> var x = Tensor([[1, 2], [3, 4]])
x: TensorFlow.Tensor<Double> =terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
The full process is outlined here if more detail is necessary: https://github.com/zachgrayio/swift-tensorflow/blob/example/package/README.md#run-with-dependencies-advanced