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

[SR-15793] [AutoDiff] Incorrect behavior with derivatives #58070

Open
philipturner opened this issue Jan 31, 2022 · 3 comments
Open

[SR-15793] [AutoDiff] Incorrect behavior with derivatives #58070

philipturner opened this issue Jan 31, 2022 · 3 comments
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself declarations Feature: declarations func Feature → declarations: Functions methods Feature → functions: methods (member functions) mutating Feature → declarations → functions: Mutating functions swift 5.9 unexpected behavior Bug: Unexpected behavior or incorrect output

Comments

@philipturner
Copy link
Contributor

Previous ID SR-15793
Radar None
Original Reporter @philipturner
Type Bug
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 7bdb8a5d99f3bfcb51ba371fd5018a93

Issue Description:

Automatic differentiation gives incorrect results when differentiating a mutating function. It swizzles the components of the gradient. This function returns the following for derivatives:

  • Number being modified (x = 10): 1

  • Any parameters (y = 2, z = 3, w = 4): themselves, in order

import _Differentiation

extension Double {
  func addingThree(_ lhs: Self, _ mhs: Self, _ rhs: Self) -> Self {
    self + lhs + rhs
  }

  @derivative(of: addingThree)
  func _vjpAddingThree(
    _ lhs: Self,
    _ mhs: Self,
    _ rhs: Self
  ) -> (value: Self, pullback: (Self) -> (Self, Self, Self, Self)) {
    return (addingThree(lhs, mhs, rhs), { v in (v, lhs, mhs, rhs) })
  }

  mutating func addThree(_ lhs: Self, _ mhs: Self, _ rhs: Self) {
    self += lhs + mhs + rhs
  }

  @derivative(of: addThree)
  mutating func _vjpAddThree(
    _ lhs: Self,
    _ mhs: Self,
    _ rhs: Self
  ) -> (value: Void, pullback: (inout Self) -> (Self, Self, Self)) {
    addThree(lhs, mhs, rhs)
    return ((), { v in (lhs, mhs, rhs) })
  }
}

@differentiable(reverse)
func altAddingThree(_ x: Double, _ y: Double, _ z: Double, _ w: Double) -> Double {
  var output = x
  output.addThree(y, z, w)
  return output
}

assert((2, 3, 4) == gradient(at: 2, 3, 4, of: { 10.addingThree($0, $1, $2) }))

// fails
assert((2, 3, 4) == gradient(at: 2, 3, 4, of: { altAddingThree(10, $0, $1, $2) }))
input expected output
(x=10, y=2, z=3) (dx=1, dy=2, dz=3) (dx=1, dy=3, dz=4)
(y=2, x=10, z=3) (dy=2, dx=1, dz=3) (dy=3, dx=1, dz=4)
(y=2, z=3, x=10) (dy=2, dz=3, dx=1) (dy=3, dz=4, dx=1)
(y=2, z=3, w=4) (dy=2, dz=3, dw=4) (dy=3, dz=4, dw=2)
(x=10, y=2) (dx=1, dy=2) (dx=1, dy=3)
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@philipturner
Copy link
Contributor Author

Fix submitted as #58437

@BradLarson
Copy link
Contributor

This behavior is still present on top-of-tree Swift, and we need to investigate this further, so reopening to look into it.

@BradLarson BradLarson reopened this May 9, 2023
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself declarations Feature: declarations func Feature → declarations: Functions methods Feature → functions: methods (member functions) mutating Feature → declarations → functions: Mutating functions unexpected behavior Bug: Unexpected behavior or incorrect output swift 5.9 labels May 13, 2023
@jkshtj
Copy link
Contributor

jkshtj commented May 3, 2024

Issue still exists in 05/24 toolchain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself declarations Feature: declarations func Feature → declarations: Functions methods Feature → functions: methods (member functions) mutating Feature → declarations → functions: Mutating functions swift 5.9 unexpected behavior Bug: Unexpected behavior or incorrect output
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants