Skip to content
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

[SR-12056] [AutoDiff] '@derivative(of:)' unable to find operator (confusing diagnostic) #54492

Open
rxwei opened this issue Jan 21, 2020 · 3 comments
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@rxwei
Copy link
Member

rxwei commented Jan 21, 2020

Previous ID SR-12056
Radar None
Original Reporter @rxwei
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AutoDiff
Assignee None
Priority Medium

md5: 6e93ab574cfcf1f80438e7cf183cc1f0

duplicates:

  • SR-13152 Add diagnostic for derivative registration instance/static method mismatch

Issue Description:

extension Int: Differentiable {
    public typealias TangentVector = Int
}

extension Int {
    @derivative(of: Int.+)
    @usableFromInline
    func derivativeOfAdd(_ x: Int, _ y: Int) -> (value: Int, differential: (Int, Int) -> Int) {
        (x + y, { dx, dy in dx + dy })
    }
}
main.swift:6:25: error: '+' is not defined in the current type context
    @derivative(of: Int.+)

The correct derivative should obviously be defined in the static type context. However, this diagnostic is not helpful and should be improved.

@rxwei
Copy link
Member Author

rxwei commented Jan 21, 2020

When I change the derivative function to a static function, the error becomes something else:

/Users/rxwei/Development/Swift/Incremental/Sources/Incremental/main.swift:6:6: error: function is not differentiable
    @derivative(of: +)
    ~^~~~~~~~~~~~~~~~~
<unknown>:0: note: when differentiating this function definition
<unknown>:0: note: missing return for differentiation

This is even worse.

@dan-zheng
Copy link
Collaborator

Derivative registration instance/static member mismatch is tracked by TF-1015.

The second issue in your comment above is new.

extension Int: Differentiable {
    public typealias TangentVector = Int
}

extension Int {
    @derivative(of: Int.+)
    @usableFromInline
    static func derivativeOfAdd(_ x: Int, _ y: Int) -> (value: Int, differential: (Int, Int) -> Int) {
        (x + y, { dx, dy in dx + dy })
    }
}
sr-12056.swift:6:6: error: function is not differentiable
    @derivative(of: Int.+)
    ~^~~~~~~~~~~~~~~~~~~~~
<unknown>:0: note: when differentiating this function definition
<unknown>:0: note: missing return for differentiation

This error occurs because only a JVP is registered for Int.+, no VJP. The @derivative attribute lowers to a differentiability witness (the public_external linkage may be wrong):

// differentiability witness for static Int.+ infix(_:_:)
sil_differentiability_witness public_external [serialized] [parameters 0 1] [results 0] @$sSi1poiyS2i_SitFZ : $@convention(method) (Int, Int, @thin Int.Type) -> Int {
  jvp: @AD__$sSi1poiyS2i_SitFZ__jvp_src_0_wrt_0_1 : $@convention(method) (Int, Int, @thin Int.Type) -> (Int, @owned @callee_guaranteed (Int, Int) -> Int)
}

When canonicalizing the differentiability witness created for Int.+, a VJP cannot be generated because Int.+ has no body, as an external function.


What behavior would you expect for this example? Here are possible improvements:

  • Add a note that reads: "register a VJP function into addition to a JVP function to avoid this error".

  • When a VJP cannot be generated (e.g. unsupported control flow or no return) but a JVP function is registered, emit an empty VJP function that calls fatalError with the message "No VJP function was registered". This behavior might be unexpected.

@rxwei
Copy link
Member Author

rxwei commented Jan 21, 2020

Since the error is coming out of the differentiation transform, it looks like the generated differentiability witnesses are falsely treated as the trigger for differentiation. I would expect an error that asks the user to "provide a derivative that produces a pullback".

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants