Skip to content

[AutoDiff] Destroy all pullback indirect results after adjoint accumulation. #27711

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 2 commits into from
Oct 16, 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
7 changes: 4 additions & 3 deletions lib/SILOptimizer/Mandatory/Differentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6955,7 +6955,6 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
auto tan = *allResultsIt++;
if (tan->getType().isAddress()) {
addToAdjointBuffer(bb, origArg, tan, loc);
builder.emitDestroyAddrAndFold(loc, tan);
} else {
if (origArg->getType().isAddress()) {
auto *tmpBuf = builder.createAllocStack(loc, tan->getType());
Expand All @@ -6971,9 +6970,11 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
}
}
}
// Deallocate pullback indirect results.
for (auto *alloc : reversed(pullbackIndirectResults))
// Destroy and deallocate pullback indirect results.
for (auto *alloc : reversed(pullbackIndirectResults)) {
builder.emitDestroyAddrAndFold(loc, alloc);
builder.createDeallocStack(loc, alloc);
}
}

/// Handle `struct` instruction.
Expand Down
40 changes: 22 additions & 18 deletions test/AutoDiff/superset_adjoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,34 @@ SupersetVJPTests.test("SubsetOfSubset") {
expectEqual(0, gradient(at: 0, in: { x in foo(x, 0, 0) }))
}

SupersetVJPTests.test("ApplySubset") {
// TF-914
@differentiable(wrt: x)
func foo<T: Differentiable>(_ x: T, _ y: T, apply: @differentiable (T, T) -> T) -> T {
return apply(x, y)
}
expectEqual(1, gradient(at: Float(0)) { x in foo(x, 0) { $0 + $1 } })
}

// FIXME: The expression `(+) as @differentiable (Float, @nondiff Float) -> Float)`
// forms a curry thunk of `Float.+` before conversion to @differentiable, and AD
// doesn't know how to differentiate the curry thunk, so it produces a
// "function is not differentiable" error.
// FIXME: Propagate wrt indices correctly so that this actually takes the
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was referring to lowering parameter indices to @nondiff parameters, which was done long ago.

// gradient wrt only the first parameter, as intended.
// SupersetVJPTests.test("CrossModule") {
// expectEqual(1, gradient(at: 1, 2, in: (+) as @differentiable (Float, @nondiff Float) -> Float))
// let grad = gradient(at: Float(1), Float(2), in: (+) as @differentiable (Float, @nondiff Float) -> Float)
// expectEqual(Float(1), grad)
// }

// FIXME: Unbreak this one.
//
// @differentiable(wrt: (.0, .1), vjp: dx_T)
// func x_T<T : Differentiable>(_ x: Float, _ y: T) -> Float {
// if x > 1000 {
// return x
// }
// return x
// }
// func dx_T<T>(_ x: Float, _ y: T) -> (Float, (Float) -> (Float, T.TangentVector)) {
// return (x_T(x, y), { seed in (x, y) })
// }
// SupersetVJPTests.test("IndirectResults") {
// expectEqual(3, gradient(at: 2) { x in x_T(x, Float(3)) })
// }
SupersetVJPTests.test("IndirectResults") {
@differentiable(wrt: (x, y), vjp: dx_T)
func x_T<T : Differentiable>(_ x: Float, _ y: T) -> Float {
if x > 1000 { return x }
return x
}
func dx_T<T : Differentiable>(_ x: Float, _ y: T) -> (Float, (Float) -> (Float, T.TangentVector)) {
return (x_T(x, y), { v in (x * v, .zero) })
}
expectEqual(2, gradient(at: 2) { x in x_T(x, Float(3)) })
}

runAllTests()