Skip to content

cblas_dgemm fails to compile #2842

@jeffpollock9

Description

@jeffpollock9

Hello.

I have tried to make a small example showing some issues when trying to use cblas_dgemm inside an objective function. I see there are various tests in Enzyme regarding using cblas_* stuff so I was hopeful this would work, perhaps it is a geniune Enzyme bug or I have mis-used?

Any help would be great.

I've written the code so you can compile with no defines, -DENZYME_ISSUE_1 (gradient wrt alpha in gemm) and -DENZYME_ISSUE_2 (gradient wrt values in matrix x).

// clang++ blas.cpp -O2 -std=c++23 -lblas -fplugin=/home/jeff/workspace/Enzyme/build/Enzyme/ClangEnzyme-20.so -DENZYME_ISSUE_(1|2)

#include <vector>
#include <cstdio>
#include <cblas.h>

template <typename Return, typename... T>
Return __enzyme_autodiff(void*, T...);

int enzyme_dup;
int enzyme_const;
int enzyme_out;


double objective(const double alpha, const double* x, const double* y, const int n) {
    std::vector<double> z(n * n);
    cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, alpha, x, n, y, n, 0.0, z.data(), n);
    double result = 0.0;
    for (int i = 0; i < n * n; ++i) {
        result += z[i];
    }
    return result;
}

int main() {
    const int n = 3;

    std::vector<double> x(n * n, 1.0);
    std::vector<double> y(n * n, 2.0);

    double alpha = 3.14;

    double v = objective(alpha, x.data(), y.data(), n);
    std::printf("v = %f\n", v);

    #ifdef ENZYME_ISSUE_1
    // fails with many lines
    double g = __enzyme_autodiff<double>((void*)objective, enzyme_out, alpha, enzyme_const, x.data(), enzyme_const, y.data(), enzyme_const, n);
    std::printf("g = %f\n", g);
    #endif

    #ifdef ENZYME_ISSUE_2
    // /usr/bin/ld: /tmp/blas-a3b7db.o: in function `diffe_Z9objectivedPKdS0_i':
    // blas.cpp:(.text+0x912): undefined reference to `cblas_dlascl'
    // clang++: error: linker command failed with exit code 1 (use -v to see invocation)
    std::vector<double> g(n * n);
    __enzyme_autodiff<void>((void*)objective, enzyme_const, alpha, enzyme_dup, x.data(), g.data(), enzyme_const, y.data(), enzyme_const, n);
    for (int i = 0; i < n * n; ++i) {
        std::printf("g[%i] = %f\n", i, g[i]);
    }
    #endif

    return 0;
}

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions