Skip to content
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

Enable recursive differentiation of nested calls #117

Merged
merged 1 commit into from
Apr 2, 2019

Conversation

efremale
Copy link
Contributor

Now, if a function f that is being differentiated contains a call to a function g, clad will proceed to try to differentiate g even if it has no
overload in custom_derivatives.

Example:

double f(double x) {
  return g(double x);
}

On clad::differentiate(f, 0), clad will proceed to differentiate g and so on, even if it is not in custom_derivatives namespace.

A new issue with forward mode calls was detected: #116, we should fix it. Some tests were disabled.

//cCHECK-NEXT: int _t0 = f_pow(arg, p - 1);
//cCHECK-NEXT: return _d_arg * _t0 + arg * (f_pow_darg0(arg, p - 1) * (_d_arg + _d_p - 0));
//cCHECK-NEXT: }
//cCHECK-NEXT: }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test is broken atm and was disabled.

Copy link
Owner

Choose a reason for hiding this comment

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

Could you prepend FIXME- and open an issue for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, the issue is #116

//cCHECK-NEXT: _d_x = _d_x * _t1 + x * (custom_derivatives::pow_darg0(2., y) * (0. + _d_y));
//cCHECK-NEXT: x *= _t1;
//cCHECK-NEXT: return _d_x;
//cCHECK-NEXT: }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test is broken atm and was disabled.

@efremale efremale force-pushed the nested-calls branch 3 times, most recently from 2891c31 to 05e83ac Compare March 29, 2019 16:54
/// Name of the base function to be differentiated. Can be different from
/// function->getNameAsString() when higher-order derivatives are computed.
std::string baseFunctionName = {};
/// Current derivative order to be computer.
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
/// Current derivative order to be computer.
/// Current derivative order to be computed.

@@ -424,9 +435,10 @@ namespace clad {
NamespaceDecl* enclosingNS = nullptr;
llvm::SaveAndRestore<DeclContext*> SaveContext(m_Sema.CurContext);
llvm::SaveAndRestore<Scope*> SaveScope(m_CurScope);
m_Sema.CurContext = m_Function->getDeclContext();
DeclContext* DC = const_cast<DeclContext*>(m_Function->getDeclContext());
Copy link
Owner

Choose a reason for hiding this comment

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

We should make m_Function non 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.

It is not really a solution, since we are assigning FD (const FunctionDecl* passed to ::Derive, which is indeed intended to be const, since we are not going to modify the original declaration) to m_Function. So that will just move the const_cast elsewhere.

It is more reasonable to remove constness from the DeclContext since we are indeed going to modify it (e.g. by putting new derivative into the original function's namespace).

@@ -1339,9 +1315,10 @@ namespace clad {
NamespaceDecl* enclosingNS = nullptr;
llvm::SaveAndRestore<DeclContext*> SaveContext(m_Sema.CurContext);
llvm::SaveAndRestore<Scope*> SaveScope(m_CurScope);
m_Sema.CurContext = m_Function->getDeclContext();
DeclContext* DC = const_cast<DeclContext*>(m_Function->getDeclContext());
Copy link
Owner

Choose a reason for hiding this comment

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

Likewise

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See above.

@@ -96,7 +96,7 @@ float f_const_args_func_6(const float x, const float y, const Vec &v) {

float f_const_helper(const float x) {
return x * x;
} // expected-warning 4 {{function 'f_const_helper' was not differentiated because it is not declared in namespace 'custom_derivatives'}}
}
Copy link
Owner

Choose a reason for hiding this comment

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

Nice!

int f_1(int x) {
printf("I am being run!\n"); //expected-warning{{attempted to differentiate unsupported statement, no changes applied}}
printf("I am being run!\n"); //expected-warning{{attempted to differentiate unsupported statement, no changes applied}} //expected-warning{{function 'printf' was not differentiated because clad failed to differentiate it and no suitable overload was found in namespace 'custom_derivatives'}}
Copy link
Owner

Choose a reason for hiding this comment

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

We should shorten the diagnostic and make it more accurate. What about “function ... was not differentiated because no definition is available and no suitable replacement ...”?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is not necessarily that there is no definition. There may be a definition but clad may fail to derive it (e.g. if it meets something it doesn't support).

Copy link
Owner

Choose a reason for hiding this comment

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

I think In that case we should diagnose that in a separate diagnostic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is not so easy to do at the moment and we'll need to work on some diagnostic infrastructure to propagate exact and clear errors from recursive calls to ::Derive.

Copy link
Owner

Choose a reason for hiding this comment

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

Ok, could you open an issue for that then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

/// A struct containing information about request to differentiate a function.
struct DiffRequest {
/// Function to be differentiated.
const clang::FunctionDecl* function = nullptr;
Copy link
Owner

Choose a reason for hiding this comment

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

We should probably either add m_ to all members or at least capitalize them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is wrong with request.function? It is the cleanest way IMO. It is just a struct carrying parameters.

Copy link
Owner

Choose a reason for hiding this comment

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

We seem to be using different pattern elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is questionable why are we using m_ thing in the first place. Maybe it makes some sense in classes where only a small part is exposed as a public interface. It doesn't make a lot of sense to use it on a config struct where members are intended to be assigned with something as a public interface.

Copy link
Owner

Choose a reason for hiding this comment

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

We should have accessors but that'd be an overkill for that. Let's use llvm's convention capitalizing the data members.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Accessors like request.setCurrentDerivativeOrder(request.getCurrentDerivativeOrder() + 1)? I don't think we should ever have that.

Now, if a function `f` that is being differentiated contains a call to a function `g`, clad will proceed to try to differentiate `g` even if it has no
overload in `custom_derivatives`.

Example:
```
double f(double x) {
  return g(double x);
}
```
On `clad::differentiate(f, 0)`, clad will proceed to differentiate `g` and so on, even if it is not in `custom_derivatives` namespace.
@vgvassilev vgvassilev merged commit 14dc444 into vgvassilev:master Apr 2, 2019
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.

2 participants