Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Patching to enable building swift-apis on current nightly Swift #1184

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ if(ENABLE_PYTHON_SUPPORT)
GIT_REPOSITORY
git://github.com/pvieito/PythonKit
GIT_TAG
master
6a05a15
Copy link
Author

Choose a reason for hiding this comment

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

6a05a15 is the last commit before CMake is removed.

CMAKE_ARGS
-D BUILD_SHARED_LIBS=YES
-D CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
Expand Down Expand Up @@ -189,7 +189,7 @@ if(NOT X10_FOUND AND NOT USE_BUNDLED_X10)
COMMAND
rm -rf <SOURCE_DIR>/bazel-bin # ${CMAKE_COMMAND} -E rm -Rrf <SOURCE_DIR>/bazel-bin
COMMAND
bazel build ${VISIBILITY_FLAGS} -c opt --define framework_shared_object=false //tensorflow/compiler/tf2xla/xla_tensor:x10 --nocheck_visibility
bazel build ${VISIBILITY_FLAGS} -c opt --define framework_shared_object=false //tensorflow:tensorflow //tensorflow/compiler/tf2xla/xla_tensor:x10 --nocheck_visibility
Copy link
Author

Choose a reason for hiding this comment

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

This enables one to extract a standalone X10 after a successful CMake build, so subsequent builds can be made with the Swift Package Manager.

Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem right. You should be able to do that irrespective. This is just building an additional label, which means that there is potentially a dependency that is missing?

Copy link
Author

Choose a reason for hiding this comment

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

Without this I think libtensorflow.so.xxx is not generated and I cannot copy them to a standalone /Library/usr/lib for a SPM build, following the current docs/Development.md. Interestingly this is not needed for the CMake build, which I don't know the reason.

Copy link
Contributor

Choose a reason for hiding this comment

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

Technically, the tensorflow core itself is not needed for the build, it is only needed at runtime.

COMMAND
bazel shutdown
INSTALL_COMMAND
Expand Down
2 changes: 1 addition & 1 deletion Documentation/X10/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct MyModel: Layer {
public var dense3 = Dense<Float>(inputSize: 4, outputSize: 4)
public var flatten = Flatten<Float>()

@differentiable
@differentiable(reverse)
public func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
let layer1 = dense1(input)
let layer2 = layer1.reshaped(to: [1, 4])
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Model: Layer {
var layer2 = Dense<Float>(inputSize: hiddenSize, outputSize: hiddenSize, activation: relu)
var layer3 = Dense<Float>(inputSize: hiddenSize, outputSize: 3, activation: identity)

@differentiable
@differentiable(reverse)
func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
return input.sequenced(through: layer1, layer2, layer3)
}
Expand Down
38 changes: 19 additions & 19 deletions Sources/TensorFlow/BackwardsCompatibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import _Differentiation
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - expected: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
@differentiable(wrt: (predicted, expected))
@differentiable(reverse, wrt: predicted)
@differentiable(reverse, wrt: (predicted, expected))
public func l1Loss<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>,
expected: Tensor<Scalar>
Expand All @@ -37,8 +37,8 @@ public func l1Loss<Scalar: TensorFlowFloatingPoint>(
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - expected: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
@differentiable(wrt: (predicted, expected))
@differentiable(reverse, wrt: predicted)
@differentiable(reverse, wrt: (predicted, expected))
public func l2Loss<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>,
expected: Tensor<Scalar>
Expand All @@ -51,8 +51,8 @@ public func l2Loss<Scalar: TensorFlowFloatingPoint>(
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - expected: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
@differentiable(wrt: (predicted, expected))
@differentiable(reverse, wrt: predicted)
@differentiable(reverse, wrt: (predicted, expected))
public func hingeLoss<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>,
expected: Tensor<Scalar>
Expand All @@ -65,8 +65,8 @@ public func hingeLoss<Scalar: TensorFlowFloatingPoint>(
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - expected: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
@differentiable(wrt: (predicted, expected))
@differentiable(reverse, wrt: predicted)
@differentiable(reverse, wrt: (predicted, expected))
public func squaredHingeLoss<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>,
expected: Tensor<Scalar>
Expand All @@ -79,8 +79,8 @@ public func squaredHingeLoss<Scalar: TensorFlowFloatingPoint>(
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - expected: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
@differentiable(wrt: (predicted, expected))
@differentiable(reverse, wrt: predicted)
@differentiable(reverse, wrt: (predicted, expected))
public func categoricalHingeLoss<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>,
expected: Tensor<Scalar>
Expand All @@ -94,8 +94,8 @@ public func categoricalHingeLoss<Scalar: TensorFlowFloatingPoint>(
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - expected: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
@differentiable(wrt: (predicted, expected))
@differentiable(reverse, wrt: predicted)
@differentiable(reverse, wrt: (predicted, expected))
public func logCoshLoss<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>,
expected: Tensor<Scalar>
Expand All @@ -108,8 +108,8 @@ public func logCoshLoss<Scalar: TensorFlowFloatingPoint>(
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - expected: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
@differentiable(wrt: (predicted, expected))
@differentiable(reverse, wrt: predicted)
@differentiable(reverse, wrt: (predicted, expected))
public func poissonLoss<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>,
expected: Tensor<Scalar>
Expand All @@ -123,8 +123,8 @@ public func poissonLoss<Scalar: TensorFlowFloatingPoint>(
/// - Parameters:
/// - predicted: Predicted outputs from a neural network.
/// - expected: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
@differentiable(wrt: (predicted, expected))
@differentiable(reverse, wrt: predicted)
@differentiable(reverse, wrt: (predicted, expected))
public func kullbackLeiblerDivergence<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>,
expected: Tensor<Scalar>
Expand All @@ -137,7 +137,7 @@ public func kullbackLeiblerDivergence<Scalar: TensorFlowFloatingPoint>(
/// - Parameters:
/// - logits: One-hot encoded outputs from a neural network.
/// - labels: Indices (zero-indexed) of the correct outputs.
@differentiable(wrt: logits)
@differentiable(reverse, wrt: logits)
public func softmaxCrossEntropy<Scalar: TensorFlowFloatingPoint>(
logits: Tensor<Scalar>,
probabilities: Tensor<Scalar>
Expand All @@ -149,8 +149,8 @@ public func softmaxCrossEntropy<Scalar: TensorFlowFloatingPoint>(
/// - Parameters:
/// - logits: The unscaled output of a neural network.
/// - labels: Integer values that correspond to the correct output.
@differentiable(wrt: logits)
@differentiable(wrt: (logits, labels))
@differentiable(reverse, wrt: logits)
@differentiable(reverse, wrt: (logits, labels))
public func sigmoidCrossEntropy<Scalar: TensorFlowFloatingPoint>(
logits: Tensor<Scalar>,
labels: Tensor<Scalar>
Expand Down
30 changes: 15 additions & 15 deletions Sources/TensorFlow/Core/DifferentialOperators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import _Differentiation
@inlinable
public func valueWithGradient<T, R>(
at x: T,
in f: @differentiable (T) -> Tensor<R>
in f: @differentiable(reverse) (T) -> Tensor<R>
) -> (value: Tensor<R>, gradient: T.TangentVector)
where T: Differentiable, R: TensorFlowFloatingPoint {
let (y, pullback) = valueWithPullback(at: x, in: f)
let (y, pullback) = valueWithPullback(at: x, of: f)
precondition(
y.rank == 0,
"""
Expand All @@ -40,10 +40,10 @@ where T: Differentiable, R: TensorFlowFloatingPoint {
public func valueWithGradient<T, U, R>(
at x: T,
_ y: U,
in f: @differentiable (T, U) -> Tensor<R>
in f: @differentiable(reverse) (T, U) -> Tensor<R>
) -> (value: Tensor<R>, gradient: (T.TangentVector, U.TangentVector))
where T: Differentiable, U: Differentiable, R: TensorFlowFloatingPoint {
let (y, pullback) = valueWithPullback(at: x, y, in: f)
let (y, pullback) = valueWithPullback(at: x, y, of: f)
precondition(
y.rank == 0,
"""
Expand All @@ -58,10 +58,10 @@ public func valueWithGradient<T, U, V, R>(
at x: T,
_ y: U,
_ z: V,
in f: @differentiable (T, U, V) -> Tensor<R>
in f: @differentiable(reverse) (T, U, V) -> Tensor<R>
) -> (value: Tensor<R>, gradient: (T.TangentVector, U.TangentVector, V.TangentVector))
where T: Differentiable, U: Differentiable, V: Differentiable, R: TensorFlowFloatingPoint {
let (y, pullback) = valueWithPullback(at: x, y, z, in: f)
let (y, pullback) = valueWithPullback(at: x, y, z, of: f)
precondition(y.rank == 0)
return (y, pullbackOfOneLikeY(y: y, pullback: pullback))
}
Expand All @@ -70,23 +70,23 @@ where T: Differentiable, U: Differentiable, V: Differentiable, R: TensorFlowFloa

@inlinable
public func valueWithGradient<T, R>(
of f: @escaping @differentiable (T) -> Tensor<R>
of f: @escaping @differentiable(reverse) (T) -> Tensor<R>
) -> (T) -> (value: Tensor<R>, gradient: T.TangentVector)
where T: Differentiable, R: TensorFlowFloatingPoint {
return { x in valueWithGradient(at: x, in: f) }
}

@inlinable
public func valueWithGradient<T, U, R>(
of f: @escaping @differentiable (T, U) -> Tensor<R>
of f: @escaping @differentiable(reverse) (T, U) -> Tensor<R>
) -> (T, U) -> (value: Tensor<R>, gradient: (T.TangentVector, U.TangentVector))
where T: Differentiable, U: Differentiable, R: TensorFlowFloatingPoint {
return { x, y in valueWithGradient(at: x, y, in: f) }
}

@inlinable
public func valueWithGradient<T, U, V, R>(
of f: @escaping @differentiable (T, U, V) -> Tensor<R>
of f: @escaping @differentiable(reverse) (T, U, V) -> Tensor<R>
) -> (T, U, V) -> (
value: Tensor<R>,
gradient: (T.TangentVector, U.TangentVector, V.TangentVector)
Expand All @@ -100,7 +100,7 @@ where T: Differentiable, U: Differentiable, V: Differentiable, R: TensorFlowFloa
@inlinable
public func gradient<T, R>(
at x: T,
in f: @differentiable (T) -> Tensor<R>
in f: @differentiable(reverse) (T) -> Tensor<R>
) -> T.TangentVector where T: Differentiable, R: TensorFlowFloatingPoint {
return valueWithGradient(at: x, in: f).1
}
Expand All @@ -109,7 +109,7 @@ public func gradient<T, R>(
public func gradient<T, U, R>(
at x: T,
_ y: U,
in f: @differentiable (T, U) -> Tensor<R>
in f: @differentiable(reverse) (T, U) -> Tensor<R>
) -> (T.TangentVector, U.TangentVector)
where T: Differentiable, U: Differentiable, R: TensorFlowFloatingPoint {
return valueWithGradient(at: x, y, in: f).1
Expand All @@ -120,7 +120,7 @@ public func gradient<T, U, V, R>(
at x: T,
_ y: U,
_ z: V,
in f: @differentiable (T, U, V) -> Tensor<R>
in f: @differentiable(reverse) (T, U, V) -> Tensor<R>
) -> (T.TangentVector, U.TangentVector, V.TangentVector)
where T: Differentiable, U: Differentiable, V: Differentiable, R: TensorFlowFloatingPoint {
return valueWithGradient(at: x, y, z, in: f).1
Expand All @@ -130,22 +130,22 @@ where T: Differentiable, U: Differentiable, V: Differentiable, R: TensorFlowFloa

@inlinable
public func gradient<T, R>(
of f: @escaping @differentiable (T) -> Tensor<R>
of f: @escaping @differentiable(reverse) (T) -> Tensor<R>
) -> (T) -> T.TangentVector where T: Differentiable, R: TensorFlowFloatingPoint {
return { x in gradient(at: x, in: f) }
}

@inlinable
public func gradient<T, U, R>(
of f: @escaping @differentiable (T, U) -> Tensor<R>
of f: @escaping @differentiable(reverse) (T, U) -> Tensor<R>
) -> (T, U) -> (T.TangentVector, U.TangentVector)
where T: Differentiable, U: Differentiable, R: TensorFlowFloatingPoint {
return { x, y in gradient(at: x, y, in: f) }
}

@inlinable
public func gradient<T, U, V, R>(
of f: @escaping @differentiable (T, U, V) -> Tensor<R>
of f: @escaping @differentiable(reverse) (T, U, V) -> Tensor<R>
) -> (T, U, V) -> (T.TangentVector, U.TangentVector, V.TangentVector)
where T: Differentiable, U: Differentiable, V: Differentiable, R: TensorFlowFloatingPoint {
return { x, y, z in gradient(at: x, y, z, in: f) }
Expand Down
2 changes: 1 addition & 1 deletion Sources/TensorFlow/Core/MixedPrecision.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ extension Tensor {

/// Promotes a scalar to a tensor with the same device and precision as the given tensor.
// TODO (SR-12968): Mark `tensor` with `@noDerivative` and remove custom vjp below.
@differentiable(where Scalar: TensorFlowFloatingPoint)
@differentiable(reverse where Scalar: TensorFlowFloatingPoint)
public init(_ value: Scalar, deviceAndPrecisionLike tensor: Tensor) {
let device = tensor.device
let tmp = Tensor(value, on: device)
Expand Down
28 changes: 14 additions & 14 deletions Sources/TensorFlow/Core/Tensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public struct Tensor<Scalar: TensorFlowScalar> {
@usableFromInline
internal var _isScalarZero = false

/// An internal workaround for SR-13263: debug info generation crash.
@usableFromInline
class SR13263Workaround {}
// /// An internal workaround for SR-13263: debug info generation crash.
// @usableFromInline
// class SR13263Workaround {}

/// An internal workaround for SR-13263: debug info generation crash.
internal var _sr13263Workaround: SR13263Workaround?
// /// An internal workaround for SR-13263: debug info generation crash.
// internal var _sr13263Workaround: SR13263Workaround?
Copy link
Contributor

Choose a reason for hiding this comment

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

Why comment this out? Is it no longer needed?

Copy link
Author

Choose a reason for hiding this comment

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

Leaving this as-is triggers a compiler crash, so I removed this workaround.

Copy link
Contributor

Choose a reason for hiding this comment

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

This was a workaround for an actual issue. I think that it would be better to verify if the underlying issue has been resolved or not.

Copy link
Contributor

Choose a reason for hiding this comment

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

The underlying issue has migrated in that you can now trigger the same crash by inserting this placeholder, rather than avoiding the crash by having it. I've seen this with other types. Unclear when the behavior inverted.

Copy link

@philipturner philipturner Jul 5, 2022

Choose a reason for hiding this comment

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

I'm not seeing this happen with or without SR13263Workaround commented out, on current Swift toolchains.

Nevermind. I reproduced the crash on recent nightly toolchains, using the reproducer from swiftlang/swift#55703.

Copy link

@philipturner philipturner Jul 5, 2022

Choose a reason for hiding this comment

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

I'm not seeing the crash affect S4TF, with or without SR13623Workaround commented out. I'm running swift build and swift test, and it's not activating the crash on recent toolchains. Am I doing this right?

export TENSORFLOW_USE_RELEASE_TOOLCHAIN=1
cd s4tf
swift build -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN \
  -c release -Xswiftc -g -Xcc -I${DESTDIR}/usr/include -Xlinker -L${DESTDIR}/usr/lib

rm -f .build/release/libx10.dylib
cp ${DESTDIR}/usr/lib/libx10.dylib .build/release/libx10.dylib

swift test -Xswiftc -DTENSORFLOW_USE_STANDARD_TOOLCHAIN \
    -c release -Xswiftc -g -Xcc -I${DESTDIR}/usr/include -Xlinker -L${DESTDIR}/usr/lib


@inlinable
public init(handle: TensorHandle<Scalar>) {
Expand Down Expand Up @@ -132,7 +132,7 @@ extension Tensor {
/// Reshape to scalar.
/// - Precondition: The tensor has exactly one scalar.
@inlinable
@differentiable(where Scalar: TensorFlowFloatingPoint)
@differentiable(reverse where Scalar: TensorFlowFloatingPoint)
public func scalarized() -> Scalar {
precondition(
shape.contiguousSize == 1,
Expand Down Expand Up @@ -174,7 +174,7 @@ extension Tensor {
return handle.makeHostCopy()
}

@differentiable(where Scalar: TensorFlowFloatingPoint)
@differentiable(reverse where Scalar: TensorFlowFloatingPoint)
public var scalars: [Scalar] {
if handle.backend == .XLA {
let (storage, _) = xlaTensor.fetchTensorValues(Scalar.self)
Expand Down Expand Up @@ -203,7 +203,7 @@ extension Tensor where Scalar: TensorFlowFloatingPoint {

extension Tensor {
/// Creates a 0-D tensor from a scalar value.
@differentiable(where Scalar: TensorFlowFloatingPoint)
@differentiable(reverse where Scalar: TensorFlowFloatingPoint)
public init(_ value: Scalar, on device: Device = .default) {
switch device.backend {
case .XLA:
Expand All @@ -227,7 +227,7 @@ extension Tensor where Scalar: TensorFlowFloatingPoint {
extension Tensor {
/// Creates a 1D tensor from scalars.
@inlinable
@differentiable(where Scalar: TensorFlowFloatingPoint)
@differentiable(reverse where Scalar: TensorFlowFloatingPoint)
public init(_ scalars: [Scalar], on device: Device = .default) {
self.init(shape: [scalars.count], scalars: scalars, on: device)
}
Expand All @@ -247,7 +247,7 @@ extension Tensor {
/// - scalars: The scalar contents of the tensor.
/// - Precondition: The product of the dimensions of the shape must equal the number of scalars.
@inlinable
@differentiable(where Scalar: TensorFlowFloatingPoint)
@differentiable(reverse where Scalar: TensorFlowFloatingPoint)
public init(shape: TensorShape, scalars: [Scalar], on device: Device = .default) {
precondition(
shape.contiguousSize == scalars.count,
Expand Down Expand Up @@ -628,7 +628,7 @@ extension Tensor: AdditiveArithmetic where Scalar: Numeric {
/// Adds two tensors and produces their sum.
/// - Note: `+` supports broadcasting.
@inlinable
@differentiable(where Scalar: TensorFlowFloatingPoint)
@differentiable(reverse where Scalar: TensorFlowFloatingPoint)
public static func + (lhs: Tensor, rhs: Tensor) -> Tensor {
if lhs._isScalarZero {
return rhs
Expand All @@ -641,7 +641,7 @@ extension Tensor: AdditiveArithmetic where Scalar: Numeric {
/// Subtracts one tensor from another and produces their difference.
/// - Note: `-` supports broadcasting.
@inlinable
@differentiable(where Scalar: TensorFlowFloatingPoint)
@differentiable(reverse where Scalar: TensorFlowFloatingPoint)
public static func - (lhs: Tensor, rhs: Tensor) -> Tensor {
if rhs._isScalarZero {
return lhs
Expand Down Expand Up @@ -745,7 +745,7 @@ public protocol TensorProtocol {
public protocol DifferentiableTensorProtocol:
TensorProtocol & Differentiable & EuclideanDifferentiable
where Scalar: TensorFlowFloatingPoint {
@differentiable(wrt: self)
@differentiable(reverse, wrt: self)
func annotate(_ annotation: String) -> Self
}

Expand Down Expand Up @@ -773,7 +773,7 @@ where Scalar: TensorFlowFloatingPoint {
///
/// - Parameter annotation: The annotation to be added.
/// - Returns: The annotated tensor.
@differentiable(wrt: self)
@differentiable(reverse, wrt: self)
public func annotate(_ annotation: String) -> Tensor<Scalar> {
switch handle.backend {
case .XLA:
Expand Down
Loading