Skip to content

Conversation

marcrasi
Copy link

@marcrasi marcrasi commented Dec 12, 2019

This PR puts the generic signature of the original function in the DifferentiableAttribute, solving TF-1046.

This exposed 3 latent bugs, which I had to deal with:

@marcrasi marcrasi requested review from dan-zheng and rxwei December 12, 2019 05:55
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.

Nice!

// same as the derivative generic signature, then the requirements are
// satisfied. This check is necessary because the subsequent logic does not
// correctly handle polymorphic original functions.
// TODO(TF-1055): Can be removed after we have a robust solution for TF-1055.
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder what's the specific symptom of TF-1055?

Referencing the example:

sil_differentiability_witness @foo <T: A> { ... }

sil_differentiability_witness @foo <T: B> { ... }

sil @foo : <T> (T) -> T

sil @example
bb0:
  %1 = function_ref @foo : <T> (T) -> T
  %2 = differentiable_function %1
  %3 = differentiable_function_extract [vjp] %2
  ...

Currently, is the symptom that one differentiability witness is chosen at random during differentiable_function canonicalization?

Copy link
Author

Choose a reason for hiding this comment

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

You'd get an error that foo is not differentiable, even if the next instruction applies the vjp to something that conforms to A. However, I don't know if any swift code will actually generate SIL like that.

With this check here, I know of no swift code that causes any symptoms.

A symptom that you can get from real swift, if you remove the check here, is:

protocol P {
  @differentiable
  func f(_ x: Float) -> Float
}

extension P {
  @differentiable
  func f(_ x: Float) -> Float
}

struct S: P {}

=>

error: function is not differentiable because `Self: P` is not satisfied

(error from memory, don't have a compiler to test it on right now)

Copy link
Author

Choose a reason for hiding this comment

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

The reason that error happens is that the differentiability witness table for P.f has a Self: P constraint, and the substitution map that gets passed in to diagnoseUnsatisfiedRequirements doesn't have anything satisfying those constraints.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, thanks for the details! I wonder if this is a deficiency in the code around diagnoseUnsatisfiedRequirements (e.g. logic for computing the substitution map). I'll try to reproduce the issue after this patch lands.

@marcrasi
Copy link
Author

@swift-ci please test tensorflow

@marcrasi
Copy link
Author

All downstream repo tests passed, except swift-jupyter flaked. I'm rerunning that to make sure it's not a real failure. If that passes, then I'll merge this!

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