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

Error when differentiating Explicit template class #1510

Closed
a17brah opened this issue Oct 31, 2023 · 5 comments
Closed

Error when differentiating Explicit template class #1510

a17brah opened this issue Oct 31, 2023 · 5 comments

Comments

@a17brah
Copy link

a17brah commented Oct 31, 2023

I noticed a weird behaviour when used EnzymeAD with an explicit template class. If the method bump is defined in the cpp file, I get this error "declare dso_local noundef double @_ZNK8RiskViewIdE4bumpEdd(ptr noundef nonnull align 8 dereferenceable(304), double noundef, double noundef) local_unnamed_addr #7
Assertion failed: 0 && "attempting to differentiate function without definition" "

template<typename T>
class RiskView {
public:
    RiskView();

    RiskView(Array<T> &times, Array<T> &strikes, Array<T> &forwards, Matrix<T> &spreads);

    T bump(T x, T t) const;;

    void out();

private:
    bool isDefault_;
    Array<T> times_;
    Array<T> strikes_;
    Array<T> forwards_;
    Matrix<T> spreads_;
    std::vector<Array<T>> logMoneyness_;
    std::vector<Array<T>> spreadsVec_;
    std::vector<Interpolator<T, SmoothStepInterpolation, FlatExtrapolation>> interpolators_;
};

//EnzymeAD works when this is defined in the header
template<typename T>
T RiskView<T>::bump(T x, T t) const { //error if defined in cpp
    if (!isDefault_) {
        //find index in t then interpolate index-1, index=> give linear interpolation of that.
        int index = search_sorted<T>(t, times_.data(), times_.size(), true);
        //case index = 0 => t< times[0]
        if (index == 0) {
            T t_right = times_[0];
            T y_right = interpolators_[0].interpolate(x);
            return t * y_right / t_right;
        } else {
            T t_left = times_[index - 1];
            T t_right = times_[index];
            T y_left = interpolators_[index - 1].interpolate(x);;
            T y_right = interpolators_[index].interpolate(x);
            return y_left + (t - t_left) * (y_right - y_left) / (t_right - t_left);
        }
    }
    return 0.0;
}
@wsmoses
Copy link
Member

wsmoses commented Nov 1, 2023

Mind pasting the whole file (perhaps in an Enzyme explorer link enzyme.mit.edu/explorer)?

@a17brah
Copy link
Author

a17brah commented Nov 2, 2023

https://fwd.gymni.ch/CmHXUs. When implementing the f in a CPP file, the Enzyme tool crashes with the following error message:

[2/3] Building CXX object CMakeFiles/testEnzyme.dir/main.cpp.o
FAILED: CMakeFiles/testEnzyme.dir/main.cpp.o
/home/linuxbrew/.linuxbrew/bin/clang++ -std=gnu++17 -O3 -DNDEBUG -fcolor-diagnostics -fplugin=/home/linuxbrew/.linuxbrew/Cellar/enzyme/0.0.89/lib/ClangEnzyme-16.so -MD -MT CMakeFiles/testEnzyme.dir/main.cpp.o -MF CMakeFiles/testEnzyme.dir/main.cpp.o.d -o CMakeFiles/testEnzyme.dir/main.cpp.o -c /mnt/c/Users/alijm/CLionProjects/testEnzyme/main.cpp
error: Enzyme: No reverse pass found for _ZNK7MyClassIdE1fEd
at context: %3 = tail call noundef double @_ZNK7MyClassIdE1fEd(ptr noundef nonnull align 8 dereferenceable(8) %1, double noundef %0) #9

@wsmoses
Copy link
Member

wsmoses commented Nov 8, 2023

Ah sorry I didn't fully read your post properly. If you have the function in a different cpp file and Enzyme deduces it needs to be diffeerentiated (it detects this here), you need to enable multisource support with LLDEnzyme or an alternate mechanism.

@a17brah
Copy link
Author

a17brah commented Nov 9, 2023

So use LLDEnzyme flags instead of clang, or do I need to add anything else?

@ZuseZ4
Copy link
Member

ZuseZ4 commented Nov 9, 2023

We updated our docs here to describe the differences: https://enzyme.mit.edu/getting_started/UsingEnzyme/#differentiating-cc
It also contains a link to a cmake template which hopefully works for you, please let us know if not!

@a17brah a17brah closed this as completed Nov 10, 2023
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

3 participants