Skip to content

Refine the implicit wrt logic for @differentiable attribute #67174

@asl

Description

@asl

Originally posted by @rxwei in #66873 (comment)

I think there are two angles where we need to decide:

  • Whether non-wrt parameters should be treated as semantic results.
    • It seems that our conclusion was "no".
  • The default wrt inference behavior, i.e. the behavior when you apply @differentiable without specifying wrt:.
    • My current preference is to never infer wrt-ness on any inout argument. If the user explicitly specifies wrt: on an inout argument, we treat it as a semantic result.
    • Enforcing explicit wrt annotations has better clarity. We can always add inference rules in the future if there's a usability benefit, without breaking source code.
@differentiable // inferred as wrt: x, semantic result: formal result (no y)
func foo(x: Float, y: inout Float) -> Float

@differentiable // error: 'String' does not conform to 'Differentiable'
func foo(x: Float, y: inout Float) -> String

@differentiable // error: function does not have a differentiable return type
                // note: did you mean to differentiate wrt the inout parameter `y`?
                // fixit: insert `(wrt: y)`
func foo(x: Float, y: inout Float)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions