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
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions enzyme/Enzyme/CApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,21 @@ void EnzymeRegisterAllocationHandler(char *Name, CustomShadowAlloc AHandle,
};
}


/// This is the main entry point to register a custom derivative for language frontends.
ZuseZ4 marked this conversation as resolved.
Show resolved Hide resolved
/// It should be prefered over trying to register custom-derivatives in the llvm-ir module.
ZuseZ4 marked this conversation as resolved.
Show resolved Hide resolved
/// The main reason is that these rules can handle non-default activity cases, e.g.
/// a function call where a pointer or a float scalar is marked as const.
/// To get a better low-level understanding, the code in AdjointGenerator.h can be read.
///
/// This Function will only handle using ReverseMode AD (either split or combined).
Copy link
Member

Choose a reason for hiding this comment

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

currently this only supports split mode. Well kind of. A combined mode call will call the "forward pass" generator from here and then also the "reverse pass" generator as well. This is also the case for split mode as well. Of course with the full functionality of gradientutils/etc available, one could check if they are in combined mode by checking if gutils->mode == ReverseModeCombined.

Copy link
Member Author

@ZuseZ4 ZuseZ4 Dec 6, 2024

Choose a reason for hiding this comment

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

Thank you. Can you please also answer how the last three arguments should be used and how to get them?

/// As a high-level example, assume we want to register a custom derivative for a vector resize function.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is a good example (far too complicated, and I'm not confident your algirthm for the derivative is correct). Perhaps explain with a simple pow(x, y) example where x or y could be differentiable or not and you want to handle both easily.

Copy link
Member Author

@ZuseZ4 ZuseZ4 Dec 6, 2024

Choose a reason for hiding this comment

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

Can you please explain why it is not correct?

/// We pass the mangled name as first argument.
/// resizing is a simple enough to be handled in the fwd pass, so we just pass a nullptr as RevHandle.
/// For the forward pass, we will need to resize the shadow of the input (if duplicated), so the CI
/// is a CallInst of resize on the shadow argument. The IRBuilder B, the shadow argument, and gutils
/// should all be provided available in the frontend. The last three arguments ...
///
void EnzymeRegisterCallHandler(char *Name,
CustomAugmentedFunctionForward FwdHandle,
CustomFunctionReverse RevHandle) {
Expand All @@ -363,6 +378,13 @@ void EnzymeRegisterCallHandler(char *Name,
};
}

/// This is the main entry point to register a custom derivative for language frontends.
ZuseZ4 marked this conversation as resolved.
Show resolved Hide resolved
/// It should be prefered over trying to register custom-derivatives in the llvm-ir module.
/// The main reason is that these rules can handle non-default activity cases, e.g.
/// a function call where a pointer or a float scalar is marked as const.
/// To get a better low-level understanding, the code in AdjointGenerator.h can be read.
///
/// This Function will only handle using ForwardMode AD.
void EnzymeRegisterFwdCallHandler(char *Name, CustomFunctionForward FwdHandle) {
auto &pair = customFwdCallHandlers[Name];
pair = [=](IRBuilder<> &B, CallInst *CI, GradientUtils &gutils,
Expand Down
Loading