-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Sema] [AutoDiff] Derive @_fieldwiseProductSpace
when tangent/cotangent is Self
#21737
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
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,27 @@ | ||
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s | ||
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s --check-prefix=CHECK-AST | ||
dan-zheng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s --check-prefix=CHECK-SILGEN | ||
// RUN: %target-swift-frontend -emit-sil -verify %s | ||
|
||
public struct Foo : Differentiable { | ||
public var a: Float | ||
} | ||
|
||
// CHECK-LABEL: public struct Foo : Differentiable { | ||
// CHECK: @sil_stored @differentiable() | ||
// CHECK: public var a: Float { get set } | ||
// CHECK-AST-LABEL: public struct Foo : Differentiable { | ||
// CHECK-AST: @sil_stored @differentiable() | ||
// CHECK-AST: public var a: Float { get set } | ||
|
||
// CHECK-SILGEN-LABEL: // Foo.a.getter | ||
// CHECK-SILGEN: sil [transparent] [serialized] [differentiable source 0 wrt 0] @$s33derived_differentiable_properties3FooV1aSfvg : $@convention(method) (Foo) -> Float | ||
|
||
struct AdditiveTangentIsSelf : AdditiveArithmetic, Differentiable { | ||
var a: Float | ||
} | ||
let _: @autodiff (AdditiveTangentIsSelf) -> Float = { x in | ||
x.a + x.a | ||
} | ||
|
||
// CHECK-AST-LABEL: struct AdditiveTangentIsSelf : AdditiveArithmetic, Differentiable { | ||
// CHECK-AST-NOT: @differentiable | ||
// CHECK-AST: @_fieldwiseProductSpace typealias TangentVector = AdditiveTangentIsSelf | ||
// CHECK-AST: @_fieldwiseProductSpace typealias CotangentVector = AdditiveTangentIsSelf | ||
|
||
// CHECK-LABEL: // Foo.a.getter | ||
// CHECK: sil [transparent] [serialized] [differentiable source 0 wrt 0] @$s33derived_differentiable_properties3FooV1aSfvg : $@convention(method) (Foo) -> Float |
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.
Do you want to change this to a note?
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.
No, here's why. The
DifferentiationInvoker
infra keeps track of how each differentiation task is created. Only the diagnostic on the very bottom of the invocation stack should be an error.If expression
f3()
is not differentiable, we should not emit an error atf3()
because it's not the place where the user requested the derivative and the user should not be told to change the code here before knowing what's actually causing differentiation to occur. Instead, we emit notes along the invocation stack until we reach the user's differentiation request, which is always the bottom of the invocation stack.As to why this cannot be changed to a note today: We used to have
DifferentialOperator
as a case inDifferentiationInvoker
, and the differential operator was, as you know,#gradient
. When we switched over to generalized differentiability™, there is no more compiler-known differential operator -- function conversion to@autodiff
is the only formal way to trigger differentiation in expressions. We didn't add a case for function conversion inDifferentiationInvoker
, so the user request is not recorded, and there is no error at the user request. If we change this error to a note today, the pass pipeline would assume there's no error, keep running other passes on partially/incorrectly transformed code, and cause crashers or undefined runtime behavior.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.
PS: This definitely needs to be fixed. Let me know if you wanna make diagnostics better :)
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.
Thanks for the detailed info! Definitely interested in improving AD diagnostics sometime.