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-15675] [AutoDiff] Derivative powVJP broken on Linux #57954

Closed
philipturner opened this issue Dec 31, 2021 · 0 comments
Closed

[SR-15675] [AutoDiff] Derivative powVJP broken on Linux #57954

philipturner opened this issue Dec 31, 2021 · 0 comments
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@philipturner
Copy link
Contributor

Previous ID SR-15675
Radar None
Original Reporter @philipturner
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AutoDiff
Assignee BradLarson (JIRA)
Priority Medium

md5: 8d335cddddc6ffa03f1af4f31fb6b979

Issue Description:

I tried this out on both Swift 5.5 and Swift 5.5.2 release, using a Colab VM (see Swift-Colab (https://github.com/philipturner/swift-colab) for how to replicate). I am patching the S4TF tutorial on "Sharp Edges in Differentiability", but finding the synthesized derivative powVJP always returns an incorrect result.

I am using the Differentiation (https://github.com/philipturner/differentiation) package, but files from swift/main haven't changed since Differentiation was last updated. On Xcode, the derivative produces the correct result (4.0, 2.772588722239781), but on Colab it produces (4.0, 2.825309782578104e-303) which is incorrect. This happens even when both are tested on Swift 5.5.0 (modify the import command for Colab to download "5.5" instead of "5.5.2", not "5.5.0").

I am not able to use Swift development snapshots because they break support for Swift on Colab. Furthermore, this requires importing "Differentiation" instead of "_Differentiation".

import Foundation

@usableFromInline
@derivative(of: pow)
func powVJP(_ base: Double, _ exponent: Double) -> (value: Double, pullback: (Double) -> (Double, Double)) {
  let output: Double = pow(base, exponent)
  func pullback(_ vector: Double) -> (Double, Double) {
    let baseDerivative = vector * (exponent * pow(base, exponent - 1))
    let exponentDerivative = vector * output * log(base)
    return (baseDerivative, exponentDerivative)
  }
  
  return (value: output, pullback: pullback)
}

let powGrad = gradient(at: Double(2), Double(2), of: pow)
print("pow gradient: ", powGrad, "which is", powGrad == (4.0, 2.772588722239781) ? "correct" : "incorrect")

This is related to the following, which indirectly reference this code:

https://bugs.swift.org/browse/SR-14240

#38850

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 in itself
Projects
None yet
Development

No branches or pull requests

1 participant