Skip to content

Conversation

@dan-zheng
Copy link
Contributor

Reverse-mode differentiation now supports apply instructions with multiple
active "semantic results" (formal results or inout parameters).

The "cannot differentiate through multiple results" non-differentiability error
is lifted.

Resolves TF-983.


Follow-up:

  • TF-1288: relax @differentiable and @derivative type-checking to allow
    original declarations with multiple semantic results.
  • Add forward-mode differentiation support for apply instructions with multiple
    active "semantic results".

Example:

import _Differentiation

// Test function returning a tuple of active results.
func tuple(_ x: Float, _ y: Float) -> (Float, Float) {
  return (x, y)
}
func multiply(_ x: Float, _ y: Float) -> Float {
  let z = tuple(x, y)
  // Note: both results (tuple elements) are active.
  return z.0 * z.1
}
print(valueWithGradient(at: 3, 4, in: multiply))
print(valueWithGradient(at: 5, 10, in: multiply))

// Test function with multiple `inout` parameters.
func swap<T>(_ x: inout T, _ y: inout T) {
  let tmp = x; x = y; y = tmp
}
func multiply_swap(_ x: Float, _ y: Float) -> Float {
  var tuple = (x, y)
  swap(&tuple.0, &tuple.1)
  return tuple.0 * tuple.1
}
print(valueWithGradient(at: 3, 4, in: multiply_swap))
print(valueWithGradient(at: 5, 10, in: multiply_swap))

Before:

$ swift test.swift
test.swift:25:40: error: function is not differentiable
print(valueWithGradient(at: 5, 10, in: multiply_swap))
                                       ^~~~~~~~~~~~~
test.swift:21:3: note: cannot differentiate through multiple results
  swap(&tuple.0, &tuple.1)
  ^
test.swift:13:40: error: function is not differentiable
print(valueWithGradient(at: 5, 10, in: multiply))
                                       ^~~~~~~~
test.swift:8:11: note: cannot differentiate through multiple results
  let z = tuple(x, y)
          ^

After:

$ swift test.swift
(value: 12.0, gradient: (4.0, 3.0))
(value: 50.0, gradient: (10.0, 5.0))
(value: 12.0, gradient: (4.0, 3.0))
(value: 50.0, gradient: (10.0, 5.0))

… in SIL.

Reverse-mode differentiation now supports `apply` instructions with multiple
active "semantic results" (formal results or `inout` parameters).

The "cannot differentiate through multiple results" non-differentiability error
is lifted.

Resolves TF-983.
// Add pullback parameter for the seed.
Optional<SILParameterInfo> inoutParam;
bool isWrtInoutParam = false;
// Add pullback parameters based on original result indices.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: the pullback type calculation logic here in VJPEmitter::createEmptyPullback() duplicates SILFunctionType::getAutoDiffDerivativeFunctionType. It should be deduped, I'll look into it soon.

@@ -1,7 +1,11 @@
// RUN: %target-run-simple-swift

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can add more tests regarding "multiple result indices" (e.g. differentiability witness SILGen tests) when landing TF-1288: relaxing @differentiable and @derivative type-checking to allow original declarations with multiple semantic results.

@dan-zheng dan-zheng requested review from marcrasi and rxwei June 30, 2020 21:16
@dan-zheng
Copy link
Contributor Author

@swift-ci Please smoke test

Copy link

@marcrasi marcrasi left a comment

Choose a reason for hiding this comment

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

Exciting!

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.

3 participants