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

Invalid code generated in the gradient when using operators #1087

Closed
gojakuch opened this issue Sep 10, 2024 · 1 comment · Fixed by #1088
Closed

Invalid code generated in the gradient when using operators #1087

gojakuch opened this issue Sep 10, 2024 · 1 comment · Fixed by #1088
Assignees

Comments

@gojakuch
Copy link
Collaborator

reproducer:

#include "clad/Differentiator/Differentiator.h"
#include <iostream>

namespace TN {
    template <typename T>
    struct Test2 {
        T operator()(T x) {
            return 2*x;
        }
        T operator[](T x) {
            return 4*x;
        }
    };
}

namespace clad {
namespace custom_derivatives {
namespace class_functions {
    template <typename T>
    void operator_subscript_pullback(::TN::Test2<T>* obj, T x, T d_u, ::TN::Test2<T>* d_obj, T* d_x) {
        (*d_x) += 4*d_u;
    }
}}}

double fn2(double x, double y) {
    TN::Test2<double> t;
    auto q = t[x];
    return q;
}

int main() {
  double dx, dy;
  auto df2 = clad::gradient(fn2);
  std::cout << "f2(3, 4)=" << fn2(3.0, 4.0) << '\n';
  dx = 0; dy = 0;
  df2.execute(3.0, 4.0, &dx, &dy);
  std::cout << "df/dx(3, 4)=" << dx << '\n';
  std::cout << "df/dy(3, 4)=" << dy << '\n';
}

generates code with the following line:

double q = operator[](x);

which is invalid and should be replaced with double q = t[x];

@gojakuch gojakuch self-assigned this Sep 10, 2024
@vgvassilev
Copy link
Owner

double q = operator[](x); is missing double q = t.operator[](x);. However, I agree it should appear the way you suggested.

gojakuch added a commit to gojakuch/clad that referenced this issue Oct 6, 2024
This commit fixes the way Clad generates code. Specifically,
it addresses the way operators appear in the generated code
in the reverse mode and the way nested name qualifiers are
built in both modes.

Fixes: vgvassilev#1050
Fixes: vgvassilev#1087
gojakuch added a commit to gojakuch/clad that referenced this issue Oct 7, 2024
This commit fixes the way Clad generates code. Specifically,
it addresses the way operators appear in the generated code
in the reverse mode and the way nested name qualifiers are
built in both modes.

Fixes: vgvassilev#1050
Fixes: vgvassilev#1087
gojakuch added a commit to gojakuch/clad that referenced this issue Oct 8, 2024
This commit fixes the way Clad generates code. Specifically,
it addresses the way operators appear in the generated code
in the reverse mode and the way nested name qualifiers are
built in both modes. This partially fixes vgvassilev#1050.

Fixes: vgvassilev#1087
gojakuch added a commit to gojakuch/clad that referenced this issue Oct 8, 2024
This commit fixes the way Clad generates code. Specifically,
it addresses the way operators appear in the generated code
in the reverse mode and the way nested name qualifiers are
built in both modes. This partially fixes vgvassilev#1050.

Fixes: vgvassilev#1087
gojakuch added a commit to gojakuch/clad that referenced this issue Oct 8, 2024
This commit fixes the way Clad generates code. Specifically,
it addresses the way operators appear in the generated code
in the reverse mode and the way nested name qualifiers are
built in both modes. This partially f ixes vgvassilev#1050.

Fixes: vgvassilev#1087
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 a pull request may close this issue.

2 participants