EnzymeAD / Enzyme.jl

Julia bindings for the Enzyme automatic differentiator
https://enzyme.mit.edu
MIT License
437 stars 62 forks source link

Using Julia's LLVM and Enzyme.jl's artifacts to differentiate C code #1367

Closed cgeoga closed 5 months ago

cgeoga commented 5 months ago

I have some C code I want to differentiate, and it seems like one of the easier ways to use Enzyme to do that is to use the artifacts from LLVM_full_jll.jl and Enzyme_jll.jl to set that up. I have set up a little bash function following the getting started docs for this that works on an Ubuntu LTS machine:

#!/bin/bash

_opt=~/.julia/artifacts/290d06a089f634a44dd3a8838acf3f08dbc77534/tools/opt
_cla=~/.julia/artifacts/290d06a089f634a44dd3a8838acf3f08dbc77534/tools/clang
_enz=~/.julia/artifacts/f7069537846d503d1aacae07adda0035a774b8e9/lib/LLVMEnzyme-15.so

"$_cla" $1 -S -emit-llvm -o in_tmp.ll -O2 -fno-vectorize -fno-unroll-loops -fno-slp-vectorize
"$_opt" in_tmp.ll -load-pass-plugin="$_enz" -passes=enzyme -o out_tmp.ll -S
"$_cla" out_tmp.ll -O3 -o "${2:-a.exe}"
rm -f in_tmp.ll out_tmp.ll

# Example use:
# bash enzyme_build.sh test.c

But: The docs mention passing the clang plugin ClangEnzyme-vXXX.so, and indicate that it is preferable to do this instead of the more manual process above. But I can't find that *.so file anywhere. When I build Enzyme from source with Julia's LLVM following the Enzyme.jl developer docs (using the official binary), I don't get a ClangEnzyme-vXXX.so anywhere. And I don't see it anywhere in the jll artifacts either. Does that tool still exist?

Also: If there is some easier way to set up Enzyme for C code, I would love to hear it. I would rather not build LLVM from source---I can't even git clone that repository! It seems like Enzyme.jl's packaging has already kindly done all the hard work of managing versions and stuff. But maybe I'm missing a more elegant option.

Once this is sorted out I'm very happy to make a PR to the docs explaining this stuff! Thanks so much in advance.

vchuravy commented 5 months ago

Since the Clang plugins is not necessary for Julia and bandwidth use might be precious to the user we intentionally disable the ClangPlugin in our recipe.

https://github.com/EnzymeAD/Enzyme/blob/757becc24a6d86f4f4554010619c761bb1cf86fa/.packaging/build_tarballs.jl#L63

You could use https://github.com/EnzymeAD/Enzyme.jl/blob/main/deps/build_local.jl to build a new version with the ClangPlugin enabled.

Caveat emptor. The LLVM build used here is only validated for Julia and we do not provide any guarantees or support for using it to build C/C++ code with it.

wsmoses commented 5 months ago

It does exist and is provided in spack,brew, etc packages.

Building Enzyme from source will build it by default if your llvm installation also built clang (but not if you don't have clang, since we need to build it against clang). If you explicitly request it to be built and Clang is not available build will fail.

In this case Julia just doesn't build it.

cgeoga commented 5 months ago

Amazing, thank you both.

Re: the caveat emptor, though, is this just a bad idea? Would I be better off just figuring out the LLVM+Clang source build -> Enzyme source build?

vchuravy commented 5 months ago

is this just a bad idea?

It's a medium bad idea, which you know is pretty good in the grand scheme of things.

There have recently been request for a Conda setup and there is Spack which we tend to recommend.

Overall I am hesitant with recommending it as a general solution.

cgeoga commented 5 months ago

Hah. I appreciate your candor. If Spack is your recommendation, I'll look into that and hold off on a docs PR somewhere until I have a handle on how to do that.

Thanks again for the help on this! I'm going to close the issue, but let me know if it should stay open and I'll reopen it.

wsmoses commented 5 months ago

I'll give a slightly differing viewpoint, I actually think the homebrew packages are easier (the homebrew team updates them within days every Enzyme release, whereas spack may have a longer delay to have a new version added).

vchuravy commented 5 months ago

Whereas spack may have longer delay to have a new version added

Yeah but that is up to us, since we are the Spack maintainers.

Homebrew is limiting in its OS support.

wsmoses commented 5 months ago

Indeed, relatedly we should figure out how to get our enzyme bot to automate it for us