-
Notifications
You must be signed in to change notification settings - Fork 112
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
custom derivative doc prototype #2189
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,13 +79,81 @@ extern llvm::StringMap<std::function<llvm::Value *( | |
shadowHandlers; | ||
|
||
class DiffeGradientUtils; | ||
|
||
/// This is the entry point to register custom derivatives programmatically. | ||
/// It is more general than registering custom-derivatives in the llvm-ir module, at the cost of higher complexity. | ||
/// Examples on why it is more general include custom-derivatives for non-default activity cases | ||
/// (e.g. a function call where a pointer or a float scalar is marked as const). | ||
/// It also allows using Enzyme and LLVM analysis, e.g. activity analysis, differential use analysis, alias analysis. | ||
/// | ||
/// | ||
extern llvm::StringMap<std::pair< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you give a go explaining that there are two handlers here, an augmented forward pass handler and a reverse pass handler, explaining what they mean, and what the arguments to each are (if you don't know leave an empty space and we can fill it in on GH) |
||
std::function<bool(llvm::IRBuilder<> &, llvm::CallInst *, GradientUtils &, | ||
llvm::Value *&, llvm::Value *&, llvm::Value *&)>, | ||
std::function<void(llvm::IRBuilder<> &, llvm::CallInst *, | ||
DiffeGradientUtils &, llvm::Value *)>>> | ||
customCallHandlers; | ||
|
||
/// The StringMap allows looking up a (forward-mode) custom rule based on the mangled name of the function. | ||
/// The first argument is the IRBuilder, the third argument are gradientutils, both of which should be already | ||
/// available in the frontend. The second argument is the CallInst, this should be for a function call which will | ||
/// compute the forward-mode derivative, while taking into consideration which input arguments are active or const. | ||
/// The function returns true, if the custom rule was applied, and false otherwise (e.g. because the combination of | ||
/// activities is not yet supported). The last two arguments are ... | ||
/// | ||
/// Example: | ||
/// define double @my_pow(double %x, double %y) { | ||
/// %call = call double @llvm.pow(double %x, double %y) | ||
/// ret double %call | ||
/// } | ||
/// | ||
/// The custom rule for this function could be: | ||
/// customCallHandlers["my_pow"] = [](llvm::IRBuilder<> &Builder, llvm::CallInst *CI, GradientUtils &gutils, llvm::Value *&dcall, llvm::Value *&normalReturn, llvm::Value *&shadowReturn) { | ||
/// auto x = CI->getArgOperand(0); | ||
/// auto y = CI->getArgOperand(1); | ||
/// auto xprime = gutils.getNewFromOriginal(x); | ||
/// auto yprime = gutils.getNewFromOriginal(y); | ||
/// bool is_x_active = !gutils.isConstantValue(x); | ||
/// bool is_y_active = !gutils.isConstantValue(y); | ||
/// normalreturn = Builder.CreateCall(Intrinsic::pow, {x, y}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs the be passed in |
||
/// if (is_x_active) { | ||
/// auto ym1 = Builder.CreateFSub(y, ConstantFP::get(Type::getDoubleTy(CI->getContext()), 1.0)); | ||
/// auto pow = Builder.CreateCall(Intrinsic::pow, {x, ym1}); | ||
/// auto ypow = Builder.CreateFMul(y, pow); | ||
/// shadowReturn = Builder.CreateFMul(xprime, ypow); | ||
/// | ||
/// // if y were inactive, this would be conceptually equivalent to generating | ||
/// // define internal double @fwddiffetester(double %x, double %"x'", double %y) #1 { | ||
/// // %0 = fsub fast double %y, 1.000000e+00 | ||
/// // %1 = call fast double @llvm.pow.f64(double %x, double %0) | ||
/// // %2 = fmul fast double %y, %1 | ||
/// // %3 = fmul fast double %"x'", %2 | ||
/// // ret double %3 | ||
/// // } | ||
/// } | ||
/// if (is_y_active) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just for fun can you test out this code and confirm it works? |
||
/// auto pow = Builder.CreateCall(Intrinsic::pow, {x, y}); | ||
/// auto log = Builder.CreateCall(Intrinsic::log, {x}); | ||
/// auto logpow = Builder.CreateFMul(pow, log); | ||
/// auto ylogpow = Builder.CreateFMul(yprime, logpow); | ||
/// if (is_x_active) { | ||
/// shadowReturn = Builder.CreateFAdd(ylogpow, shadowReturn); | ||
/// } else { | ||
/// shadowReturn = ylogpow; | ||
/// } | ||
/// | ||
/// // if x was inactive, this would be conceptually equivalent to generating | ||
/// // define internal double @fwddiffetester.1(double %x, double %y, double %"y'") #1 { | ||
/// // %0 = call fast double @llvm.pow.f64(double %x, double %y) | ||
/// // %1 = call fast double @llvm.log.f64(double %x) | ||
/// // %2 = fmul fast double %0, %1 | ||
/// // %3 = fmul fast double %"y'", %2 | ||
/// // ret double %3 | ||
/// // } | ||
/// } | ||
/// // We covered all 2x2 combinations, so always return true | ||
/// return true; | ||
/// } | ||
extern llvm::StringMap< | ||
std::function<bool(llvm::IRBuilder<> &, llvm::CallInst *, GradientUtils &, | ||
llvm::Value *&, llvm::Value *&)>> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you refer to the doxygen variable of note in gradientutils.h