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

add floating-point check for the gradients #2084

Open
wants to merge 1 commit 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
6 changes: 6 additions & 0 deletions enzyme/Enzyme/Enzyme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2942,6 +2942,12 @@ class EnzymeBase {
bool run(Module &M) {
Logic.clear();

for (Function &F : M) {
if (F.getName().contains("enzyme_fp_check")) {
F.setLinkage(GlobalValue::LinkageTypes::LinkOnceODRLinkage);
}
}

for (Function &F : make_early_inc_range(M)) {
attributeKnownFunctions(F);
}
Expand Down
12 changes: 12 additions & 0 deletions enzyme/Enzyme/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ llvm::cl::opt<bool> EnzymeRuntimeError(
cl::desc("Emit Runtime errors instead of compile time ones"));
}


__attribute__((weak)) void enzyme_fp_check(double) {}

void ZeroMemory(llvm::IRBuilder<> &Builder, llvm::Type *T, llvm::Value *obj,
bool isTape) {
if (CustomZero) {
Expand Down Expand Up @@ -3025,6 +3028,15 @@ llvm::Value *SanitizeDerivatives(llvm::Value *val, llvm::Value *toset,
if (EnzymeSanitizeDerivatives)
return unwrap(EnzymeSanitizeDerivatives(wrap(val), wrap(toset),
wrap(&BuilderM), wrap(mask)));

if (((Value*)toset)->getType()->Type::isDoubleTy()) {
std::string fp_check_fnname = "enzyme_fp_check";
Module &M = *BuilderM.GetInsertBlock()->getParent()->getParent();
if (M.getFunction(fp_check_fnname)){
Function *F = cast<Function>(M.getFunction(fp_check_fnname));
BuilderM.CreateCall(F, toset);
}
}
return toset;
}

Expand Down
3 changes: 3 additions & 0 deletions enzyme/Enzyme/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ llvm::Value *CreateReAllocation(llvm::IRBuilder<> &B, llvm::Value *prev,

llvm::PointerType *getDefaultAnonymousTapeType(llvm::LLVMContext &C);


__attribute__((weak)) void enzyme_fp_check(double);

class GradientUtils;
extern llvm::StringMap<std::function<llvm::Value *(
llvm::IRBuilder<> &, llvm::CallInst *, llvm::ArrayRef<llvm::Value *>,
Expand Down