Skip to content

[AutoDiff] Remove 'differentiableFunction(from:)' and 'linearFunction(from:)' #35366

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
Jan 13, 2021

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Jan 12, 2021

Remove unused APIs differentiableFunction(from:) and linearFunction(from:). They were never official APIs, are not included in the initial proposal, and are unused by existing supported client libraries (SwiftFusion and S4TF). Most importantly, they block crucial optimizations on linear map closures (#34935) and would need nontrivial work in SILGen to support.

@rxwei rxwei requested review from dan-zheng and marcrasi January 12, 2021 09:35
@rxwei
Copy link
Contributor Author

rxwei commented Jan 12, 2021

@swift-ci please test

Copy link
Contributor

@dan-zheng dan-zheng left a comment

Choose a reason for hiding this comment

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

LGTM! I'm not familiar with existing use cases or reasons not to remove - my memory is that you added them as a cute idea.

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - ee216181050e4d9c9b5ca7056a581edd95e0ee65

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - ee216181050e4d9c9b5ca7056a581edd95e0ee65

…(from:)'.

Remove unused APIs `differentiableFunction(from:)` and `linearFunction(from:)`. They were never official APIs, are not included in the [initial proposal](https://github.com/rxwei/swift-evolution/blob/autodiff/proposals/0000-differentiable-programming.md#make-a-function-differentiable-using-derivative), and are unused by existing supported client libraries (SwiftFusion and S4TF). Most importantly, they block crucial optimizations on linear map closures (swiftlang#34935) and would need nontrivial work in SILGen to support.
@rxwei rxwei force-pushed the remove-differentiable-function-ctor branch from ee21618 to f79391b Compare January 12, 2021 19:59
@rxwei
Copy link
Contributor Author

rxwei commented Jan 12, 2021

@swift-ci please test

@rxwei rxwei merged commit c8cd24a into swiftlang:main Jan 13, 2021
@rxwei rxwei deleted the remove-differentiable-function-ctor branch January 13, 2021 00:09
@porterchild
Copy link

@rxwei @dan-zheng I was making use of differentiableFunction for checkpointing, is there a way to achieve gradient checkpointing without differentiableFunction? Like here

@porterchild
Copy link

porterchild commented Feb 18, 2021

Would something like this do it?

@differentiable
func makeRecomputedInGradient<T: Differentiable, U: Differentiable>(
    _ original: @escaping @differentiable(T) -> U
) -> @differentiable(T) -> U {
    
    var gradientHolder: U.TangentVector? = nil
    
    @differentiable
    func newFunc(_ t: T) -> U {
        var t = t
        t = t.withDerivative { $0 = pullback(at: t, in: original)(gradientHolder!) }
        var result = withoutDerivative(at: original(t))
        result = result.withDerivative{ gradientHolder = $0 }
        return result
    }
    
    return newFunc
}

Huh. That crashes the compiler...

@rxwei
Copy link
Contributor Author

rxwei commented Feb 18, 2021

func _original<T, R>(_ x: T, _ f: @differentiable (T) -> R) -> R {
  f(x)
}

@derivative(of: _original, wrt: x)
func _vjp<T, R>(
  _ x: T, _ f: @differentiable (T) -> R
) -> (value: R, pullback: (R.TangentVector) -> T.TangentVector) {
  (value: f(x), pullback: pullback(at: x, in: f))
}

func makeRecomputed<T, R>(
  _ f: @escaping @differentiable (T) -> R
) -> @differentiable (T) -> R {
  { x in _original(x, f) }
}

@porterchild
Copy link

Thanks Richard!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants