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

Clad fails to use proper pullbacks for functions with reference arguments #1065

Open
gojakuch opened this issue Aug 29, 2024 · 1 comment
Open

Comments

@gojakuch
Copy link
Collaborator

reproducer

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

#define show(x) std::cout << #x << ": " << x << "\n";

double fff(double &x) {
    return 0;
}

double fn(double u, double v) {
    double &w = u;
    fff(u);
    return v;
}

int main() {
    auto d_fn = clad::gradient(fn);
    double u = 3, v = 5;
    double du, dv;
    du = dv = 0;
    show(fn(u, v));
    d_fn.execute(u, v, &du, &dv);
    show(du);
    show(dv);
}

an important note, same should work out for constructors:

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

#define show(x) std::cout << #x << ": " << x << "\n";

class SafeTestClass {
    public:
    SafeTestClass() {};
    SafeTestClass(double &x) {
    }
};

namespace clad {
namespace custom_derivatives {
namespace class_functions {
    clad::ValueAndAdjoint<SafeTestClass, SafeTestClass>
    constructor_reverse_forw(clad::ConstructorReverseForwTag<SafeTestClass>, double& x, double *d_x) {
        return {SafeTestClass(x), SafeTestClass(*d_x)};
    }
    void constructor_pullback(SafeTestClass *c, double &x, SafeTestClass *d_c, double* d_x) {
        
    }
}}}

double fn(double u, double v) {
    double &w = u;
    SafeTestClass s3(w);
    return v;
}

int main() {
    auto d_fn = clad::gradient(fn);
    double u = 3, v = 5;
    double du, dv;
    du = dv = 0;
    show(fn(u, v));
    d_fn.execute(u, v, &du, &dv);
    show(du);
    show(dv);
}
@gojakuch
Copy link
Collaborator Author

gojakuch commented Oct 8, 2024

the first reproducer might actually be related to #1105

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

No branches or pull requests

1 participant