Skip to content

Function incorrectly marked as not differentiable #75776

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

Closed
JaapWijnen opened this issue Aug 8, 2024 · 1 comment · Fixed by #77615
Closed

Function incorrectly marked as not differentiable #75776

JaapWijnen opened this issue Aug 8, 2024 · 1 comment · Fixed by #77615
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@JaapWijnen
Copy link
Contributor

JaapWijnen commented Aug 8, 2024

Description

The following code does not compile giving the following error diagnostic:

Function is not differentiable
Differentiated functions in '@inlinable' functions must be marked '@differentiable' or have a public '@derivative'; this is not possible with a closure, make a top-level function instead

This is incorrect however as the function itself is not a closure and is differentiable.

Reproduction

import _Differentiation

public struct Solution2<Thing: Differentiable & FloatingPoint>: Differentiable where Thing.TangentVector == Thing {
    @inlinable
    public static func optimization() -> Thing {
        var initial = Thing.zero
        
        let (_, delta) = valueWithGradient(at: initial, of: simulationWithLoss)
        
        initial.move(by: delta)
        
        return initial
    }
    
    @inlinable
    @differentiable(reverse)
    static func simulationWithLoss(input: Thing) -> Thing {
        return input // implementation
    }
}

Expected behavior

Compile without error

Environment

Apple Swift version 6.0-dev (LLVM 097782ee554d9fe, Swift 60bf038)
Target: arm64-apple-macosx14.0

Additional information

No response

@JaapWijnen JaapWijnen added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Aug 8, 2024
@JaapWijnen
Copy link
Contributor Author

tagging @asl

@asl asl added AutoDiff and removed triage needed This issue needs more specific labels labels Aug 8, 2024
kovdan01 added a commit to kovdan01/swift that referenced this issue Nov 14, 2024
Inside inlinable functions, we expect functions to either be explicitly
marked as differentiable or have a public explicit derivative defined.
This is obviously not possible for single and double curry thunks which
are a special case of `AutoClosureExpr`.

Instead of looking at the thunk itself, we unwrap it and look at the
function being wrapped. While the thunk itself and its differentiability
witness will not have public visibility, it's not an issue for the case
where the function being wrapped (and its witness) have public visibility.

Fixes swiftlang#54819
Fixes swiftlang#75776
kovdan01 added a commit to kovdan01/swift that referenced this issue Nov 14, 2024
Inside inlinable functions, we expect functions to either be explicitly
marked as differentiable or have a public explicit derivative defined.
This is obviously not possible for single and double curry thunks which
are a special case of `AutoClosureExpr`.

Instead of looking at the thunk itself, we unwrap it and look at the
function being wrapped. While the thunk itself and its differentiability
witness will not have public visibility, it's not an issue for the case
where the function being wrapped (and its witness) have public visibility.

Fixes swiftlang#54819
Fixes swiftlang#75776
kovdan01 added a commit to kovdan01/swift that referenced this issue Nov 14, 2024
Inside inlinable functions, we expect functions to either be explicitly
marked as differentiable or have a public explicit derivative defined.
This is obviously not possible for single and double curry thunks which
are a special case of `AutoClosureExpr`.

Instead of looking at the thunk itself, we unwrap it and look at the
function being wrapped. While the thunk itself and its differentiability
witness will not have public visibility, it's not an issue for the case
where the function being wrapped (and its witness) have public visibility.

Fixes swiftlang#54819
Fixes swiftlang#75776
kovdan01 added a commit to kovdan01/swift that referenced this issue Nov 14, 2024
Inside fragile functions, we expect function derivatives to be public, which
could be achieved by either explicitly marking the functions as differentiable
or having a public explicit derivative defined for them. This is obviously not
possible for single and double curry thunks which are a special case of
`AutoClosureExpr`.

Instead of looking at the thunk itself, we unwrap it and look at the
function being wrapped. While the thunk itself and its differentiability
witness will not have public visibility, it's not an issue for the case
where the function being wrapped (and its witness) have public visibility.

Fixes swiftlang#54819
Fixes swiftlang#75776
kovdan01 added a commit to kovdan01/swift that referenced this issue Nov 14, 2024
Inside fragile functions, we expect function derivatives to be public, which
could be achieved by either explicitly marking the functions as differentiable
or having a public explicit derivative defined for them. This is obviously not
possible for single and double curry thunks which are a special case of
`AutoClosureExpr`.

Instead of looking at the thunk itself, we unwrap it and look at the
function being wrapped. While the thunk itself and its differentiability
witness will not have public visibility, it's not an issue for the case
where the function being wrapped (and its witness) have public visibility.

Fixes swiftlang#54819
Fixes swiftlang#75776
@asl asl closed this as completed in 1a42a0c Feb 17, 2025
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants