-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff] Refine debug info emitted for adjoint buffers #62779
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c168585
Refine debug info emitted for adjoint buffers.
asl ce4acbd
Fix windows deficiencies in optimizations
asl f6657a1
Use human-readable scopes for adjoint buffer names
asl 37bf335
Another attempt to pacify windows bots as they produce different resu…
asl 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
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
79 changes: 79 additions & 0 deletions
79
test/AutoDiff/compiler_crashers_fixed/issue-62608-conflicting-debug-info.swift
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// RUN: %target-swift-frontend -emit-sil -O -g %s | %FileCheck %s | ||
|
||
// REQUIRES: swift_in_compiler | ||
|
||
// Fix for https://github.com/apple/swift/issues/62608 | ||
// We need to emit separate debug info location for different adjoint buffers | ||
// created for the single input variable | ||
|
||
import _Differentiation | ||
|
||
public extension Array { | ||
@inlinable | ||
@differentiable(reverse) | ||
mutating func update(at index: Int, byCalling closure: @differentiable(reverse) (inout Element) -> Void) where Element: Differentiable { | ||
closure(&self[index]) | ||
} | ||
} | ||
|
||
public func valueWithPullback<T>( | ||
at x: T, of f: @differentiable(reverse) (inout T) -> Void | ||
) -> (value: Void, pullback: (inout T.TangentVector) -> Void) { | ||
@differentiable(reverse) | ||
func nonInoutWrappingFunction(_ t: T) -> T { | ||
var t = t | ||
f(&t) | ||
return t | ||
} | ||
let nonInoutPullback = pullback(at: x, of: nonInoutWrappingFunction) | ||
return ((), { $0 = nonInoutPullback($0) }) | ||
} | ||
|
||
@inlinable | ||
public func pullback<T>( | ||
at x: T, of f: @differentiable(reverse) (inout T) -> Void | ||
) -> (inout T.TangentVector) -> Void { | ||
return valueWithPullback(at: x, of: f).pullback | ||
} | ||
|
||
// CHECK-LABEL: sil private @$s4main19testUpdateByCallingyyKF8fOfArrayL_5arraySdSaySdG_tFySdzcfU_TJpSUpSr : | ||
// CHECK: alloc_stack $Double, var, name "derivative of 'element' in scope at {{.*}} (scope #3)" | ||
// CHECK: debug_value %{{.*}} : $Builtin.FPIEEE64, var, (name "derivative of 'element' in scope at {{.*}} (scope #1)" | ||
|
||
public extension Array where Element: Differentiable { | ||
@inlinable | ||
@derivative(of: update(at:byCalling:)) | ||
mutating func vjpUpdate( | ||
at index: Int, | ||
byCalling closure: @differentiable(reverse) (inout Element) -> Void | ||
) | ||
-> | ||
(value: Void, pullback: (inout Self.TangentVector) -> Void) | ||
{ | ||
let closurePullback = pullback(at: self[index], of: closure) | ||
return (value: (), pullback: { closurePullback(&$0.base[index]) }) | ||
} | ||
} | ||
|
||
func testUpdateByCalling() throws { | ||
@differentiable(reverse) | ||
func fOfArray(array: [Double]) -> Double { | ||
var array = array | ||
var result = 0.0 | ||
for i in withoutDerivative(at: 0 ..< array.count) { | ||
array.update(at: i, byCalling: { (element: inout Double) in | ||
let initialElement = element | ||
for _ in withoutDerivative(at: 0 ..< i) { | ||
element *= initialElement | ||
} | ||
}) | ||
result += array[i] | ||
} | ||
return result | ||
} | ||
|
||
let array = [Double](repeating: 1.0, count: 3) | ||
let expectedGradientOfFOfArray = [1.0, 2.0, 3.0] | ||
let obtainedGradientOfFOfArray = gradient(at: array, of: fOfArray).base | ||
} | ||
|
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.
Why does this require
swift_in_compiler
?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.
The code is optimized on Windows differently due to lack of optimization passes written in Swift....