Skip to content

[TF] Reimplement unbroadcast using on-host axis calculation for performance. #24907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2019
Merged
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
8 changes: 3 additions & 5 deletions stdlib/public/TensorFlow/Gradients.swift
Original file line number Diff line number Diff line change
Expand Up @@ -645,16 +645,14 @@ extension Tensor where Scalar : TensorFlowFloatingPoint {
func _vjpBroadcast(
toShape shape: Tensor<Int32>
) -> (Tensor, (Tensor) -> Tensor) {
return (broadcast(toShape: shape), { [origShape = self.shapeTensor] v in
return (broadcast(toShape: shape), { [origShape = shapeTensor] v in
v.unbroadcast(toShape: origShape)
})
}

@inlinable
func _vjpUnbroadcast(
toShape shape: Tensor<Int32>
) -> (Tensor, (Tensor) -> Tensor) {
return (unbroadcast(toShape: shape), { [origShape = self.shapeTensor] v in
func _vjpUnbroadcast(to shape: TensorShape) -> (Tensor, (Tensor) -> Tensor) {
return (unbroadcast(to: shape), { [origShape = shapeTensor] v in
v.broadcast(toShape: origShape)
})
}
Expand Down
50 changes: 34 additions & 16 deletions stdlib/public/TensorFlow/Ops.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1601,40 +1601,36 @@ public extension Tensor {
public extension Tensor {
@inlinable
@differentiable(wrt: self, vjp: _vjpBroadcast(toShape:)
where Scalar : TensorFlowFloatingPoint)
where Scalar : TensorFlowFloatingPoint)
func broadcast(toShape shape: Tensor<Int32>) -> Tensor {
return Raw.broadcastTo(self, shape: shape)
}

@inlinable
@differentiable(wrt: self where Scalar : TensorFlowFloatingPoint)
func broadcast(to shape: TensorShape) -> Tensor {
return broadcast(toShape: Tensor<Int32>({ shape.dimensions.map(Int32.init) }()))
return broadcast(
toShape: Tensor<Int32>({ shape.dimensions.map(Int32.init) }()))
}

/// Broadcast to the same shape as the specified `Tensor`.
/// - Precondition: The specified shape must be compatible for broadcasting.
@inlinable
@differentiable(wrt: self
where Scalar : TensorFlowFloatingPoint)
@differentiable(wrt: self where Scalar : TensorFlowFloatingPoint)
func broadcast<OtherScalar>(like other: Tensor<OtherScalar>) -> Tensor {
return broadcast(toShape: other.shapeTensor)
}
}

public extension Tensor where Scalar : Numeric {
@inlinable
@differentiable(wrt: self, vjp: _vjpUnbroadcast(toShape:)
where Scalar : TensorFlowFloatingPoint)
@differentiable(wrt: self where Scalar : TensorFlowFloatingPoint)
func unbroadcast(toShape otherShape: Tensor<Int32>) -> Tensor {
let rankDiff = (rankTensor - otherShape.scalarCountTensor).rankLifted()
let ones: Tensor<Int32> = Raw.fill(dims: rankDiff, value: Tensor<Int32>(1))
let paddedShape = ones ++ otherShape
let nonEqualIndices = paddedShape .!= shapeTensor
let broadcastIndices = Raw.where_(nonEqualIndices).flattened()
let unbroadcasted: Tensor = Raw.sum(
self, reductionIndices: Tensor<Int32>(broadcastIndices), keepDims: false)
return Raw.reshape(unbroadcasted, shape: otherShape)
// TODO: Simplify this once differentiating control flow is supported.
return unbroadcast(to: {
precondition(otherShape.rank == 1)
return TensorShape(otherShape.scalars.map(Int.init))
}())
}

@inlinable
Expand All @@ -1644,9 +1640,31 @@ public extension Tensor where Scalar : Numeric {
}

@inlinable
@differentiable(wrt: self where Scalar : TensorFlowFloatingPoint)
@differentiable(wrt: self, vjp: _vjpUnbroadcast(to:)
where Scalar : TensorFlowFloatingPoint)
func unbroadcast(to shape: TensorShape) -> Tensor {
return unbroadcast(toShape: Tensor<Int32>({ shape.dimensions.map(Int32.init) }()))
let dimensions = self.shape.dimensions
var otherDimensions = shape.dimensions
let rankDifference = dimensions.count - otherDimensions.count
precondition(rankDifference >= 0, """
The rank of 'self' must be greater than or equal to the number of \
dimensions in the destination shape
""")
if rankDifference > 0 {
otherDimensions.insert(
contentsOf: repeatElement(1, count: rankDifference),
at: 0
)
}
assert(dimensions.count == otherDimensions.count)
var axes: [Int] = []
axes.reserveCapacity(dimensions.count)
for (i, (dim, otherDim)) in zip(dimensions, otherDimensions).enumerated() {
if dim == otherDim { continue }
if otherDim == 1 { axes.append(i); continue }
preconditionFailure("Cannot unbroadcast \(self.shape) to \(shape)")
}
return sum(alongAxes: axes).reshaped(to: shape)
}

@inlinable
Expand Down