swift icon indicating copy to clipboard operation
swift copied to clipboard

[6.1] [AutoDiff] Peel off @sendable implicit conversion when checking for unsupported differentiable function conversion

Open clackary opened this issue 10 months ago • 3 comments

  • Explanation: Peel off @sendable implicit conversion when checking for unsupported differentiable function conversion
  • Scope: Only affects autodiff code.
  • Issues: N/A
  • Original PRs: https://github.com/swiftlang/swift/pull/77869
  • Risk: Low.
  • Testing: Confirmed that a test package with the following snippet fails against the latest snapshot from release/6.1, but succeeds against the latest main snapshot. The sendable conformance is implicitly added by the test framework.
import Testing
import _Differentiation

@inlinable
@derivative(of: min)
public func _vjpMin<T: Comparable & Differentiable>(
    _ lhs: T,
    _ rhs: T
) -> (value: T, pullback: (T.TangentVector) -> (T.TangentVector, T.TangentVector)) {
    func pullback(_ tangentVector: T.TangentVector) -> (T.TangentVector, T.TangentVector) {
        if lhs <= rhs {
            return (tangentVector, .zero)
        }
        else {
            return (.zero, tangentVector)
        }
    }
    return (value: min(lhs, rhs), pullback: pullback)
}

@Test
func testMin() {
    @differentiable(reverse)
    func minContainer(_ lhs: Float, _ rhs: Float) -> Float {
        return min(lhs, rhs)
    }
    let vwgLessThan = valueWithGradient(at: 2.0, 3.0, of: min(_:_:))
    print(vwgLessThan.value == 2.0)
    print(vwgLessThan.gradient == (1.0, 0.0))
}
  • Reviewers: @asl @xedin

clackary avatar Feb 21 '25 17:02 clackary

cc @JaapWijnen

clackary avatar Feb 21 '25 17:02 clackary

@swift-ci Please test

clackary avatar Mar 10 '25 16:03 clackary

Ping @xedin...

Any concerns taking this for 6.1?

clackary avatar Mar 27 '25 16:03 clackary

Closing since we have an official 6.1 release.

clackary avatar Mar 31 '25 19:03 clackary