Skip to content
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

Can't create a custom tangent vector type with computed properties (for lazy materialization) #72229

Closed
GeorgeLyon opened this issue Mar 11, 2024 · 3 comments
Labels
AutoDiff compiler The Swift compiler itself feature A feature request or implementation improvement

Comments

@GeorgeLyon
Copy link

Description

Tangent vectors for aggregates (structs) can get really large, and there are many situations where you do not need to materialize all properties of the tangent vector in order to properly differentiate your code. Currently, there isn't a good way to do "lazy" materialization of a tangent vector because the compiler complains when a tangent vector's member is not a stored property (to be lazy, we'd need the property to be computed so that it can materialize itself on demand).

Here is an example that demonstrates the issue: https://github.com/GeorgeLyon/LazyTangentVectorIssue/blob/1f65e3333d0b6f303a60e43d358d2213e15925a0/Sources/DifferentiableSwiftExample/DifferentiableSwiftExample.swift

Reproduction

import _Differentiation

@differentiable(reverse)
public func foo(_ value: Foo) -> Double {
  return value.x + 3
}

public struct Foo: Differentiable {
  var x: Double

  public mutating func move(by offset: TangentVector) {
    self.x += offset.x
  }

  public struct TangentVector: Differentiable, AdditiveArithmetic {
    public var x: Double.TangentVector {
      get { _x }
      set { _x = newValue }
    }
    public var _x: Double.TangentVector
  }
}

Error:

error: function is not differentiable
 1 │ import _Differentiation
 2 │ 
 3 │ @differentiable(reverse)
   │  ╰─ error: function is not differentiable
 4 │ public func foo(_ value: Foo) -> Double {
   │             ╰─ note: when differentiating this function definition
 5 │   return value.x + 3
   │                ╰─ note: cannot differentiate access to property 'Foo.x' because 'Foo.TangentVector.x' is not a stored property
 6 │ }
 7 │ 

Expected behavior

This works.

Environment

Swift version 6.0-dev (LLVM ce41a43bba95b2b, Swift 1a84094)
Target: aarch64-unknown-linux-gnu
(swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a)

Additional information

No response

@GeorgeLyon GeorgeLyon added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 11, 2024
@jkshtj
Copy link
Contributor

jkshtj commented Mar 14, 2024

Hey @dan-zheng, we have recently been trying to implement optimizations to lazily materialize a massive zero tangent vector for one of our types. One of the things that we tried, was to make the properties of the tangent vector “computed”, upon which the compiler gave us the following diagnostic.

note: cannot differentiate access to property 'Foo.x' because 'Foo.TangentVector.x' is not a stored property

It looks like you put those diagnostics in place. I wanted to understand whether this limitation is an artificial one. From briefly looking into it, it seems like we simply do not support this case because the pullback code generation becomes more complicated for us? Is that right or is there a deeper reason for the existence of this limitation?

@jkshtj
Copy link
Contributor

jkshtj commented Mar 14, 2024

@rxwei @asl would you guys have any insights about this?

@asl asl added AutoDiff improvement and removed bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 14, 2024
@GeorgeLyon GeorgeLyon closed this as not planned Won't fix, can't repro, duplicate, stale Sep 14, 2024
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself feature A feature request or implementation labels Sep 15, 2024
@asl
Copy link
Contributor

asl commented Sep 16, 2024

@GeorgeLyon Why closing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoDiff compiler The Swift compiler itself feature A feature request or implementation improvement
Projects
None yet
Development

No branches or pull requests

4 participants