Skip to content

Conversation

@dan-zheng
Copy link
Contributor

@dan-zheng dan-zheng commented Nov 19, 2019

Propagate variedness from apply argument operands to apply inout arguments
(representing results). apply inout arguments are now correctly marked as
active, triggering non-differentiability errors.

Add ApplyInstBase::getInoutArguments for iterating over @inout and
@inout_aliasable arguments.

Add non-differentiability diagnostics and activity info tests.

Resolves TF-974.


Example:

@differentiable
func activeInoutArgNonactiveInitialResult(_ x: Float) -> Float {
  var result: Float = 1
  result += x // Active `inout` argument: `result`.
  return result
}
print(gradient(at: 3, in: activeInoutArgNonactiveInitialResult))

// Before: successful compilation. Prints 0.0.
// After: compile-time error.
//
//     tf-974.swift:1:2: error: function is not differentiable
//     @differentiable
//     ~^~~~~~~~~~~~~~
//     tf-974.swift:2:6: note: when differentiating this function definition
//     func activeInoutArgNonactiveInitialResult(_ x: Float) -> Float {
//          ^
//     tf-974.swift:4:10: note: cannot differentiate through 'inout' arguments
//       result += x // Active `inout` argument: `result`.
//              ^

Propagate variedness from `apply` argument operands to `apply` inout arguments
(representing results). `apply` inout arguments are now correctly marked as
active, triggering non-differentiability errors.

Add `ApplyInstBase::getInoutArguments` for iterating over `@inout` and
`@inout_aliasable` arguments.

Add non-differentiability diagnostics and activity info tests.

Resolves TF-974.
@dan-zheng dan-zheng added the tensorflow This is for "tensorflow" branch PRs. label Nov 19, 2019
OptionalTransformRange<IntRange<unsigned long>, OperandToInoutArgument>;
/// Returns all `@inout` and `@inout_aliasable` arguments passed to the
/// instruction.
InoutArgumentRange getInoutArguments() const {
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: getInoutArguments is currently defined in the "full application" case of ApplyInstBase. This required duplicating asImpl from the "partial application" ApplyInstBase superclass, which is a private function.

Other options:

  • Define getInoutArguments as a top-level function in Differentiation.cpp specialized for just ApplyInst. No problems there.
  • Move getInoutArguments to the "partial application" superclass ApplyInstBase. This requires more duplication because getArgumentsWithoutIndirectResults is defined only in the "full application" ApplyInstBase.

Optional<SILValue> operator()(unsigned long i) const {
auto paramInfo = inst.getSubstCalleeConv().getParameters()[i];
auto argument = inst.getArgumentsWithoutIndirectResults()[i];
if (paramInfo.isIndirectMutating())
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: @inout-ness is represented in SILParameterInfo. It's quite more complicated to get SILParameterInfo from just an argument Operand (need to use operand index to compute argument index) so I decided to iterate over argument indices.

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test tensorflow

@dan-zheng dan-zheng merged commit 958ecc6 into swiftlang:tensorflow Nov 19, 2019
@dan-zheng dan-zheng deleted the autodiff-fix-inout-activity branch November 19, 2019 22:19
dan-zheng added a commit to dan-zheng/open_spiel that referenced this pull request Nov 20, 2019
`Array.append` cannot be differentiated because differentiation does not yet
support function applications with `inout` arguments.

These latent bugs were uncovered in swiftlang/swift#28352,
which correctly diagnoses function applications with `inout` arguments.

https://bugs.swift.org/browse/TF-129 tracks `inout` argument support.
In the meantime, rewrite `Array.append` using `Array.+`.
dan-zheng added a commit to dan-zheng/open_spiel that referenced this pull request Nov 20, 2019
`mutating` functions like `Array.append` and `ActionValueCalculator.loss`
cannot be differentiated because differentiation does not yet support
function applications with `inout` arguments.

These latent bugs were uncovered in swiftlang/swift#28352,
which correctly diagnoses function applications with `inout` arguments.

https://bugs.swift.org/browse/TF-129 tracks `inout` argument support.
In the meantime, disable affected code and tests.
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.

3 participants