Skip to content

[AutoDiff] Create JVPEmitter #25954

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 31 commits into from
Jul 10, 2019
Merged

Conversation

bartchr808
Copy link
Contributor

@bartchr808 bartchr808 commented Jul 3, 2019

Creates a very barebones JVPEmitter which copies the body of the original function returning (original, undef), and creates the differential function with a body that returns an undef.

Apologies for the weird commit history, there was something wrong with my tensorflow branch on the fork of this repo that caused this. However, I fixed it now so future PRs won't be this ugly.

bartchr808 and others added 10 commits June 18, 2019 18:40
) (swiftlang#25778)

Xcode puts the bin/ and lib/ directories in a subdirectory based on
configuration; account for this when copying over compiler-rt for
non-host Apple platforms using one of the existing helpers in
build-script-impl.
`PointwiseMultiplicative` represents a ring over multiplication with
identity element "one" and a multiplicative inverse (reciprocal).

Conform `Differentiable` synthesized struct types to
`PointwiseMultiplicative` protocol if possible.

Enables revamping optimizers, which requires division
(i.e. multiplication with multiplicative inverse).
…swiftlang#25803)

Partial fix for `@differentiable` function thunk with output opaque
abstraction pattern. Avoid `AbstractionPattern` methods that crash for
opaque patterns.

Small step towards fixing TF-123.
Reproducers no longer crash, SIL verification fails instead.
Robust fix requires more work - see TF-123 for more details.
@bartchr808 bartchr808 added the tensorflow This is for "tensorflow" branch PRs. label Jul 3, 2019
@bartchr808 bartchr808 force-pushed the jvp-emitter branch 3 times, most recently from a56c4cc to 349db72 Compare July 3, 2019 16:33
@bartchr808 bartchr808 force-pushed the jvp-emitter branch 4 times, most recently from f4067ea to b7343a2 Compare July 3, 2019 23:05
rxwei and others added 5 commits July 8, 2019 12:44
…wiftlang#25967)

In VJPEmitter, if the original function call has substitutions, we `partial_apply` it with no arguments to specialize it. This `partial_apply` is not being released. JVP and VJP are being specialized the same way, but they are not being released either.

To fix this, we release the `@differentiable` function returned by `autodiff_function`, which will release the original and the associated functions tht are to be filled in later altogether. If the original function does not have substitutions, we retain the original function to balance out the release of the `@differentiable` function that comes later. As a result, `ADContext::promoteToDifferentiableFunction` no longer needs to retain the associated functions.

Example where the original function has substitutions:
```
f' = partial_apply f<...>()
f_diff = autodiff_function f'
release_value f_diff
```

Example where the original function does not have substitutions:
```
retain_value f
f_diff = autodiff_function f
release_value f_diff
```

Note: This makes the `autodiff_function` folding optimization no longer able to detect the pattern, but it is necessary. We can rewrite the optimization later.

This should fix [TF-621](https://bugs.swift.org/browse/TF-621).
@bartchr808 bartchr808 marked this pull request as ready for review July 9, 2019 15:02
@@ -5875,108 +6123,104 @@ ADContext::declareExternalAssociatedFunction(
return assocFn;
}

static SILFunction* createJVP(
static SILFunction *createEmptyVJP(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The git diff here is acting funny in the next several lines, may be hard to follow but I more so made a copy of createEmptyVJP and named it createEmptyJVP and removed the previous createJVP to more closely resemble how VJPEmitter looks like and JVPEmitter will become.

@bartchr808 bartchr808 requested review from rxwei and dan-zheng July 9, 2019 15:15
@bartchr808
Copy link
Contributor Author

@swift-ci please test tensorflow

@bartchr808
Copy link
Contributor Author

@swift-ci please test tensorflow

@bartchr808
Copy link
Contributor Author

@swift-ci please test tensorflow

2 similar comments
@bartchr808
Copy link
Contributor Author

@swift-ci please test tensorflow

@bartchr808
Copy link
Contributor Author

@swift-ci please test tensorflow

@rxwei rxwei merged commit a5bff23 into swiftlang:tensorflow Jul 10, 2019
bartchr808 added a commit that referenced this pull request Aug 20, 2019
This PR extends on the previous work here on the `JVPEmitter` (#25954) to make it be able to generate SIL code for the `differential` function, as well as more correctly implementing the body of the JVP.'

generates a valid JVP and differential. I have yet to add support for control flow and generics. This PR also renames `PullbackInfo` to `LinearMap` info since it has a lot of tangent mapping helpers that `JVPEmitter` can use when creating the differential.

This is still early, so the JVP/Differential emission only runs with the flag `-Xllvm -run-jvp-generation` when running Swift. With it, you can differentiate basic Float functions with `+ - / *` with no `var`s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tensorflow This is for "tensorflow" branch PRs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants