swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.58k stars 10.35k forks source link

Function incorrectly marked as not differentiable #75776

Open JaapWijnen opened 2 months ago

JaapWijnen commented 2 months ago

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 60bf038ce965551) Target: arm64-apple-macosx14.0

Additional information

No response

JaapWijnen commented 2 months ago

tagging @asl