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

custom derivative doc prototype #2189

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions enzyme/Enzyme/CApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ void EnzymeRegisterAllocationHandler(char *Name, CustomShadowAlloc AHandle,
};
}


/// This is the entry point to register reverse-mode custom derivatives programmatically.
/// A more detailed documentation is available in GradientUtils.h
Copy link
Member

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

void EnzymeRegisterCallHandler(char *Name,
CustomAugmentedFunctionForward FwdHandle,
CustomFunctionReverse RevHandle) {
Expand All @@ -363,6 +366,8 @@ void EnzymeRegisterCallHandler(char *Name,
};
}

/// This is the entry point to register forward-mode custom derivatives programmatically.
/// A more detailed documentation is available in GradientUtils.h
void EnzymeRegisterFwdCallHandler(char *Name, CustomFunctionForward FwdHandle) {
auto &pair = customFwdCallHandlers[Name];
pair = [=](IRBuilder<> &B, CallInst *CI, GradientUtils &gutils,
Expand Down
68 changes: 68 additions & 0 deletions enzyme/Enzyme/GradientUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Copy link
Member

Choose a reason for hiding this comment

The 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});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs the be passed in Value* args[2] = {x, y} or something iirc?

/// 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) {
Copy link
Member

Choose a reason for hiding this comment

The 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 *&)>>
Expand Down
Loading