-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[TF] Remove unbroadcast(to:)
and improve derivative performance.
#24408
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -210,7 +210,10 @@ extension Tensor where Scalar : TensorFlowFloatingPoint { | |
) -> (Tensor, (Tensor) -> (Tensor, Tensor)) { | ||
return (lhs + rhs, { | ||
[lhsShape = lhs.shapeTensor, rhsShape = rhs.shapeTensor] v in | ||
return (v.unbroadcast(toShape: lhsShape), v.unbroadcast(toShape: rhsShape)) | ||
let (lhsAxes, rhsAxes) = | ||
Raw.broadcastGradientArgs(s0: lhsShape, s1: rhsShape) | ||
return (v.sum(squeezingAxes: lhsAxes).reshaped(toShape: lhsShape), | ||
v.sum(squeezingAxes: rhsAxes).reshaped(toShape: rhsShape)) | ||
}) | ||
} | ||
|
||
|
@@ -220,30 +223,38 @@ extension Tensor where Scalar : TensorFlowFloatingPoint { | |
) -> (Tensor, (Tensor) -> (Tensor, Tensor)) { | ||
return (lhs - rhs, { | ||
[lhsShape = lhs.shapeTensor, rhsShape = rhs.shapeTensor] v in | ||
return (v.unbroadcast(toShape: lhsShape), | ||
-v.unbroadcast(toShape: rhsShape)) | ||
let (lhsAxes, rhsAxes) = | ||
Raw.broadcastGradientArgs(s0: lhsShape, s1: rhsShape) | ||
return (v.sum(squeezingAxes: lhsAxes).reshaped(toShape: lhsShape), | ||
-v.sum(squeezingAxes: rhsAxes).reshaped(toShape: rhsShape)) | ||
}) | ||
} | ||
|
||
@inlinable | ||
static func _vjpMultiply( | ||
lhs: Tensor, rhs: Tensor | ||
) -> (Tensor, (Tensor) -> (Tensor, Tensor)) { | ||
return (lhs * rhs, { | ||
[lhsShape = lhs.shapeTensor, rhsShape = rhs.shapeTensor] v in | ||
((rhs * v).unbroadcast(toShape: lhsShape), | ||
(lhs * v).unbroadcast(toShape: rhsShape)) | ||
return (lhs * rhs, { v in | ||
let (lhsShape, rhsShape) = (lhs.shapeTensor, rhs.shapeTensor) | ||
let (lhsAxes, rhsAxes) = | ||
Raw.broadcastGradientArgs(s0: lhsShape, s1: rhsShape) | ||
return ((rhs * v).sum(squeezingAxes: lhsAxes).reshaped(toShape: lhsShape), | ||
(lhs * v).sum(squeezingAxes: rhsAxes).reshaped(toShape: rhsShape)) | ||
}) | ||
} | ||
|
||
@inlinable | ||
static func _vjpDivide( | ||
lhs: Tensor, rhs: Tensor | ||
) -> (Tensor, (Tensor) -> (Tensor, Tensor)) { | ||
return (lhs / rhs, { | ||
[lhsShape = lhs.shapeTensor, rhsShape = rhs.shapeTensor] v in | ||
((v / rhs).unbroadcast(toShape: lhsShape), | ||
((-lhs) / rhs.squared() * v).unbroadcast(toShape: rhsShape)) | ||
return (lhs / rhs, { v in | ||
let (lhsShape, rhsShape) = (lhs.shapeTensor, rhs.shapeTensor) | ||
let (lhsAxes, rhsAxes) = | ||
Raw.broadcastGradientArgs(s0: lhsShape, s1: rhsShape) | ||
return ((v / rhs).sum(squeezingAxes: lhsAxes) | ||
.reshaped(toShape: lhsShape), | ||
(-lhs / rhs.squared() * v).sum(squeezingAxes: rhsAxes) | ||
.reshaped(toShape: rhsShape)) | ||
}) | ||
} | ||
} | ||
|
@@ -267,14 +278,14 @@ extension Tensor where Scalar : TensorFlowFloatingPoint { | |
static func _vjpSubtract( | ||
lhs: Tensor, rhs: Scalar | ||
) -> (Tensor, (Tensor) -> (Tensor, Scalar)) { | ||
return (lhs - rhs, { v in (v, 0 - v.sum().scalarized()) }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is some legacy code introduced in the early days when a |
||
return (lhs - rhs, { v in (v, -v.sum().scalarized()) }) | ||
} | ||
|
||
@inlinable | ||
static func _vjpSubtract( | ||
lhs: Scalar, rhs: Tensor | ||
) -> (Tensor, (Tensor) -> (Scalar, Tensor)) { | ||
return (lhs - rhs, { v in (v.sum().scalarized(), 0 - v) }) | ||
return (lhs - rhs, { v in (v.sum().scalarized(), -v) }) | ||
} | ||
|
||
@inlinable | ||
|
@@ -296,7 +307,7 @@ extension Tensor where Scalar : TensorFlowFloatingPoint { | |
lhs: Tensor, rhs: Scalar | ||
) -> (Tensor, (Tensor) -> (Tensor, Scalar)) { | ||
return (lhs / rhs, { v in | ||
(v / rhs, (v * (0 - lhs) / Tensor(rhs).squared()).sum().scalarized()) | ||
(v / rhs, (v * -lhs / Tensor(rhs).squared()).sum().scalarized()) | ||
}) | ||
} | ||
|
||
|
@@ -317,25 +328,30 @@ func _vjpMinMaxHelper<T : TensorFlowFloatingPoint>( | |
let denom = 1 + Tensor<T>(x .== y) | ||
let dfdx = vector * Tensor<T>(x .== originalValue) / denom | ||
let dfdy = vector * Tensor<T>(y .== originalValue) / denom | ||
return (dfdx.unbroadcast(like: x), dfdy.unbroadcast(like: y)) | ||
let (xShape, yShape) = (x.shapeTensor, y.shapeTensor) | ||
let (xAxes, yAxes) = Raw.broadcastGradientArgs(s0: xShape, s1: yShape) | ||
return (dfdx.sum(squeezingAxes: xAxes).reshaped(toShape: xShape), | ||
dfdy.sum(squeezingAxes: yAxes).reshaped(toShape: yShape)) | ||
} | ||
|
||
@inlinable | ||
func _vjpMax<T : TensorFlowFloatingPoint>( | ||
_ x: Tensor<T>, _ y: Tensor<T> | ||
) -> (Tensor<T>, (Tensor<T>) -> (Tensor<T>, Tensor<T>)) { | ||
let value = max(x, y) | ||
return (value, | ||
{ v in _vjpMinMaxHelper(x, y, originalValue: value, vector: v) }) | ||
return (value, { v in | ||
_vjpMinMaxHelper(x, y, originalValue: value, vector: v) | ||
}) | ||
} | ||
|
||
@inlinable | ||
func _vjpMin<T : TensorFlowFloatingPoint>( | ||
_ x: Tensor<T>, _ y: Tensor<T> | ||
) -> (Tensor<T>, (Tensor<T>) -> (Tensor<T>, Tensor<T>)) { | ||
let value = min(x, y) | ||
return (value, | ||
{ v in _vjpMinMaxHelper(x, y, originalValue: value, vector: v) }) | ||
return (value, { v in | ||
_vjpMinMaxHelper(x, y, originalValue: value, vector: v) | ||
}) | ||
} | ||
|
||
@inlinable | ||
|
@@ -344,8 +360,12 @@ func _vjpPow<T : TensorFlowFloatingPoint>( | |
) -> (Tensor<T>, (Tensor<T>) -> (Tensor<T>, Tensor<T>)) { | ||
let value = pow(x, y) | ||
return (value, { v in | ||
((v * y * pow(x, y-1)).unbroadcast(like: x), | ||
(v * log(x) * value).unbroadcast(like: y)) | ||
let (xShape, yShape) = (x.shapeTensor, y.shapeTensor) | ||
let (xAxes, yAxes) = Raw.broadcastGradientArgs(s0: xShape, s1: yShape) | ||
return ((v * y * pow(x, y-1)).sum(squeezingAxes: xAxes) | ||
.reshaped(toShape: xShape), | ||
(v * log(x) * value).sum(squeezingAxes: yAxes) | ||
.reshaped(toShape: yShape)) | ||
}) | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't looked too much, but I suspect that this extra reshape is not necessary. The lhsAxes should be sufficient to recover the original shape.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s what I tried initially (more specifically, ‘sum(alongAxes:)’) but it didn’t work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pschuh @rxwei the reshape is needed for handling dimensions with size 1. For example, say you do:
In this case, the broadcast indices for the gradient wrt to
y
will be[0]
and so we’ll do something like:Now, let
y
have shape[1, 5]
, which still broadcasts correctly for this example. The broadcast indices will now also be the same for the gradient (i.e.,[0]
). However, we need to do the reshape to recover the dimensions of size 1. Thus, the gradient needs to be computed as:Having said that, I have a working implementation of these changes that I had made as part of a future
swift-apis
PR. I’ll try to open a PR here for this ASAP, but haven’t gotten the chance yet because I’m traveling to ICLR this week.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make sure we don't regress in the future, could you add a quick test case in your other PR to
swift-apis
? :-)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap, I will go ahead and add that. Given that the merge already happened, is it ok to make this change after we move stdlib to swift-apis? I'll update the two PRs doing the move tonight.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Thanks!