-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Ensure autodiff code does not ignore getSingleTerminatorOperands return value
#64200
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
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
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
59 changes: 59 additions & 0 deletions
59
test/AutoDiff/compiler_crashers_fixed/issue-63728-try-apply-activity.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,59 @@ | ||
| // RUN: not %target-swift-frontend -emit-sil -verify %s | ||
|
|
||
| // The testcase from https://github.com/apple/swift/issues/63728 is not valid | ||
| // (the function is not differentiable), however, it should not cause verifier errors | ||
| // Here the root case is lack of activity analysis for `try_apply` terminators | ||
|
|
||
| import _Differentiation | ||
|
|
||
| func a() throws { | ||
| let keyPaths = (readable: [String: KeyPath<T, Double>](), writable: [String: WritableKeyPath<T, Double>]()) | ||
| @differentiable(reverse) | ||
| func f(p: PAndT) -> Double { | ||
| var mutableP = p | ||
| let s = p.p.e | ||
| var sArray: [[Double]] = [] | ||
| sArray.append((s["a"]!.asArray()).map {$0.value}) | ||
| mutableP.s = w(mutableP.s, at: keyPaths.writable["a"]!, with: sArray[0][0]) | ||
| return mutableP.s[keyPath: keyPaths.writable["a"]!] | ||
| } | ||
| } | ||
|
|
||
| public struct S<I: SProtocol, D> { | ||
| public func asArray() -> [(index: I, value: D)] { | ||
| return [(index: I, value: D)]() | ||
| } | ||
| } | ||
| struct T: Differentiable {} | ||
| struct P: Differentiable { | ||
| public var e: F<Double, Double> | ||
| } | ||
|
|
||
| struct PAndT: Differentiable{ | ||
| @differentiable(reverse) public var p: P | ||
| @differentiable(reverse) public var s: T | ||
| } | ||
|
|
||
| public struct F<I: SProtocol, D>{ | ||
| public func asArray() -> [(index: I, value: D)] {return [(index: I, value: D)]()} | ||
| public subscript(_ name: String) -> S<I, D>? {get { return self.sGet(name) }} | ||
| func sGet(_ name: String) -> S<I, D>? { fatalError("") } | ||
| } | ||
|
|
||
| public protocol ZProtocol: Differentiable {var z: () -> TangentVector { get }} | ||
| public protocol SProtocol: Hashable {} | ||
|
|
||
| //extension S: Differentiable where D: ZProtocol, D.TangentVector == D {} | ||
|
|
||
| extension F: Differentiable where D: ZProtocol, D.TangentVector == D {} | ||
| public extension ZProtocol {var z: () -> TangentVector {{ Self.TangentVector.zero }}} | ||
|
|
||
| extension F: Equatable where I: Equatable, D: Equatable {} | ||
| //extension S: Equatable where I: Equatable, D: Equatable {} | ||
| extension Double: SProtocol, ZProtocol {} | ||
|
|
||
| @differentiable(reverse where O: Differentiable, M: ZProtocol) | ||
| func w<O, M>(_ o: O, at m: WritableKeyPath<O, M>, with v: M) -> O {return o} | ||
|
|
||
| @derivative(of: w) | ||
| func vjpw<O, M>(_ o: O, at m: WritableKeyPath<O, M>, with v: M) -> (value: O, pullback: (O.TangentVector) -> (O.TangentVector, M.TangentVector)) where O: Differentiable, M: ZProtocol{fatalError("")} |
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.
Should we do an early return here to reduce nesting?
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.
It seems we'd need to eventually handle
elsecase since it at least coverstry_applyinstruction among the others. The fix for activity calculation was pretty straightforward, but I will need to think how to proceed with adjoint propagation. I do not have a testcase for this right now, but hopefully @BradLarson will be able to distill some from their codebase.For now we just do not silently generate incorrect code :)