Skip to content

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

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 1 commit into from
Dec 2, 2024

Conversation

asl
Copy link
Contributor

@asl asl commented Nov 29, 2024

No description provided.

unsupported differentiable function conversion
@asl asl requested a review from rxwei November 29, 2024 05:45
@asl asl requested review from hborla and xedin as code owners November 29, 2024 05:45
@asl
Copy link
Contributor Author

asl commented Nov 29, 2024

It is a bit non-trivial to craft a standalone testcase for this as it happens within Testing context, where such conversions are added by a Framework.

The testcase is essentially:

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() {
    let vwgLessThan = valueWithGradient(at: 2.0, 3.0, of: min(_:_:))
    print(vwgLessThan.value == 2.0)
    print(vwgLessThan.gradient == (1.0, 0.0))
}

Here Testing creates an implicit conversion for min from (Double, Double) -> Double to @sendable (Double, Double) -> Double`

@asl
Copy link
Contributor Author

asl commented Nov 29, 2024

@swift-ci please test

@asl
Copy link
Contributor Author

asl commented Nov 29, 2024

Tagging @JaapWijnen

@@ -6550,7 +6550,19 @@ maybeDiagnoseUnsupportedDifferentiableConversion(ConstraintSystem &cs,
maybeDiagnoseFunctionRef(unwrappedFnExpr);
return;
}
} else if (auto conv = dyn_cast<FunctionConversionExpr>(semanticExpr)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also ABISafeConversionExpr which does the same thing for l-value types, in case that matters in some other case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, thanks! I just do not have example in hand, but we'll see.

@asl asl merged commit 8e53ccd into main Dec 2, 2024
5 checks passed
@asl asl deleted the sendable-diff branch December 2, 2024 18:29
@asl asl added the AutoDiff label Dec 12, 2024
clackary pushed a commit to clackary/swift that referenced this pull request Feb 20, 2025
…nsupported differentiable function conversion (swiftlang#77869)

(cherry picked from commit 8e53ccd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants