Skip to content

Commit

Permalink
adapted to poseidon
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrantq committed Nov 12, 2024
1 parent 7eb617e commit 7145c49
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 177 deletions.
48 changes: 27 additions & 21 deletions enzyme/benchmarks/ReverseMode/ode/Makefile.make
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
# RUN: cd %S && LD_LIBRARY_PATH="%bldpath:$LD_LIBRARY_PATH" BENCH="%bench" BENCHLINK="%blink" LOAD="%loadEnzyme" make -B ode-unopt.ll ode-opt.ll ode.o results.txt VERBOSE=1 -f %s
# RUN: cd %S && LD_LIBRARY_PATH="%bldpath:$LD_LIBRARY_PATH" BENCH="%bench" LOAD="%loadEnzyme" LOADCLANG="%loadClangEnzyme" make -B tuned.exe VERBOSE=1 -f %s

.PHONY: clean

clean:
rm -f *.ll *.o results.txt
rm -f *.exe *.o results.txt ode.txt

ode-adept-unopt.ll: ode-adept.cpp
clang++ $(BENCH) $^ -O2 -fno-use-cxa-atexit -fno-vectorize -fno-slp-vectorize -ffast-math -fno-unroll-loops -o $@ -S -emit-llvm
#clang++ $(BENCH) $^ -O1 -Xclang -disable-llvm-passes -fno-use-cxa-atexit -fno-vectorize -fno-slp-vectorize -ffast-math -fno-unroll-loops -o $@ -S -emit-llvm
logger.exe: ode.cpp fp-logger.cpp
clang++ $(BENCH) $(LOADCLANG) ode.cpp fp-logger.cpp -O3 -mllvm -enzyme-inline -ffast-math -fno-finite-math-only -o $@ -DLOGGING

ode-unopt.ll: ode.cpp
clang++ $(BENCH) $^ -O2 -fno-use-cxa-atexit -fno-exceptions -fno-vectorize -fno-slp-vectorize -ffast-math -fno-unroll-loops -o $@ -S -emit-llvm
#clang++ $(BENCH) $^ -O1 -Xclang -disable-llvm-passes -fno-use-cxa-atexit -fno-exceptions -fno-vectorize -fno-slp-vectorize -ffast-math -fno-unroll-loops -o $@ -S -emit-llvm -Xclang -new-struct-path-tbaa

ode-raw.ll: ode-adept-unopt.ll ode-unopt.ll
opt ode-unopt.ll $(LOAD) -enzyme -o ode-enzyme.ll -S
llvm-link ode-adept-unopt.ll ode-enzyme.ll -o $@

%-opt.ll: %-raw.ll
opt $^ -o $@ -S
#opt $^ -O2 -o $@ -S

ode.o: ode-opt.ll
clang++ -O2 $^ -o $@ $(BENCHLINK) -lm

results.txt: ode.o
ode.txt: logger.exe
./$^ 1000000 | tee $@

tuned.exe: ode.cpp ode.txt
clang++ $(BENCH) $(LOADCLANG) ode.cpp -O3 -ffast-math -fno-finite-math-only -o $@ \
-mllvm -enzyme-inline \
-mllvm -enzyme-enable-fpopt \
-mllvm -fpopt-log-path=ode.txt \
-mllvm -fpopt-enable-solver \
-mllvm -fpopt-enable-pt \
-mllvm -fpopt-target-func-regex=foobar \
-mllvm -fpopt-comp-cost-budget=0 \
-mllvm -fpopt-num-samples=1024 \
-mllvm -fpopt-cost-dom-thres=0.0 \
-mllvm -fpopt-acc-dom-thres=0.0 \
-mllvm -enzyme-print-fpopt \
-mllvm -fpopt-show-table \
-mllvm -fpopt-cache-path=cache \
-mllvm -herbie-timeout=1000 \
-mllvm -herbie-num-threads=12 \
-mllvm --fpopt-cost-model-path=cm.csv

results.txt: tuned.exe
./$^ 1000000 | tee $@
40 changes: 40 additions & 0 deletions enzyme/benchmarks/ReverseMode/ode/cm.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
fneg,float,22
fadd,float,22
fsub,float,22
fmul,float,22
fdiv,float,22
fcmp,float,16
fpext_float_to_double,float,22
sin,float,100
cos,float,103
tan,float,246
exp,float,142
log,float,83
sqrt,float,33
expm1,float,106
log1p,float,110
cbrt,float,584
pow,float,159
fabs,float,20
hypot,float,608
fma,float,75
fneg,double,19
fadd,double,19
fsub,double,19
fmul,double,19
fdiv,double,28
fcmp,double,15
fptrunc_double_to_float,double,19
sin,double,829
cos,double,859
tan,double,998
exp,double,192
log,double,104
sqrt,double,52
expm1,double,75
log1p,double,118
cbrt,double,244
pow,double,225
fabs,double,20
hypot,double,382
fma,double,74
165 changes: 165 additions & 0 deletions enzyme/benchmarks/ReverseMode/ode/fp-logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <unordered_map>
#include <vector>

#include "fp-logger.hpp"

class ValueInfo {
public:
double minRes = std::numeric_limits<double>::max();
double maxRes = std::numeric_limits<double>::lowest();
std::vector<double> minOperands;
std::vector<double> maxOperands;
unsigned executions = 0;
double logSum = 0.0;
unsigned logCount = 0;

void update(double res, const double *operands, unsigned numOperands) {
minRes = std::min(minRes, res);
maxRes = std::max(maxRes, res);
if (minOperands.empty()) {
minOperands.resize(numOperands, std::numeric_limits<double>::max());
maxOperands.resize(numOperands, std::numeric_limits<double>::lowest());
}
for (unsigned i = 0; i < numOperands; ++i) {
minOperands[i] = std::min(minOperands[i], operands[i]);
maxOperands[i] = std::max(maxOperands[i], operands[i]);
}
++executions;

if (!std::isnan(res)) {
logSum += std::log1p(std::fabs(res));
++logCount;
}
}

double getGeometricAverage() const {
if (logCount == 0) {
return 0.;
}
return std::expm1(logSum / logCount);
}
};

class ErrorInfo {
public:
double minErr = std::numeric_limits<double>::max();
double maxErr = std::numeric_limits<double>::lowest();

void update(double err) {
minErr = std::min(minErr, err);
maxErr = std::max(maxErr, err);
}
};

class GradInfo {
public:
double logSum = 0.0;
unsigned count = 0;

void update(double grad) {
if (!std::isnan(grad)) {
logSum += std::log1p(std::fabs(grad));
++count;
}
}

double getGeometricAverage() const {
if (count == 0) {
return 0.;
}
return std::expm1(logSum / count);
}
};

class Logger {
private:
std::unordered_map<std::string, ValueInfo> valueInfo;
std::unordered_map<std::string, ErrorInfo> errorInfo;
std::unordered_map<std::string, GradInfo> gradInfo;

public:
void updateValue(const std::string &id, double res, unsigned numOperands,
const double *operands) {
auto &info = valueInfo.emplace(id, ValueInfo()).first->second;
info.update(res, operands, numOperands);
}

void updateError(const std::string &id, double err) {
auto &info = errorInfo.emplace(id, ErrorInfo()).first->second;
info.update(err);
}

void updateGrad(const std::string &id, double grad) {
auto &info = gradInfo.emplace(id, GradInfo()).first->second;
info.update(grad);
}

void print() const {
std::cout << std::scientific
<< std::setprecision(std::numeric_limits<double>::max_digits10);

for (const auto &pair : valueInfo) {
const auto &id = pair.first;
const auto &info = pair.second;
std::cout << "Value:" << id << "\n";
std::cout << "\tMinRes = " << info.minRes << "\n";
std::cout << "\tMaxRes = " << info.maxRes << "\n";
std::cout << "\tExecutions = " << info.executions << "\n";
std::cout << "\tGeometric Average = " << info.getGeometricAverage()
<< "\n";
for (unsigned i = 0; i < info.minOperands.size(); ++i) {
std::cout << "\tOperand[" << i << "] = [" << info.minOperands[i] << ", "
<< info.maxOperands[i] << "]\n";
}
}

for (const auto &pair : errorInfo) {
const auto &id = pair.first;
const auto &info = pair.second;
std::cout << "Error:" << id << "\n";
std::cout << "\tMinErr = " << info.minErr << "\n";
std::cout << "\tMaxErr = " << info.maxErr << "\n";
}

for (const auto &pair : gradInfo) {
const auto &id = pair.first;
const auto &info = pair.second;
std::cout << "Grad:" << id << "\n";
std::cout << "\tGrad = " << info.getGeometricAverage() << "\n";
}
}
};

Logger *logger = nullptr;

void initializeLogger() { logger = new Logger(); }

void destroyLogger() {
delete logger;
logger = nullptr;
}

void printLogger() { logger->print(); }

void enzymeLogError(const char *id, double err) {
assert(logger && "Logger is not initialized");
logger->updateError(id, err);
}

void enzymeLogGrad(const char *id, double grad) {
assert(logger && "Logger is not initialized");
logger->updateGrad(id, grad);
}

void enzymeLogValue(const char *id, double res, unsigned numOperands,
double *operands) {
assert(logger && "Logger is not initialized");
logger->updateValue(id, res, numOperands, operands);
}
8 changes: 8 additions & 0 deletions enzyme/benchmarks/ReverseMode/ode/fp-logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
void initializeLogger();
void destroyLogger();
void printLogger();

void enzymeLogError(const char *id, double err);
void enzymeLogGrad(const char *id, double grad);
void enzymeLogValue(const char *id, double res, unsigned numOperands,
double *operands);
101 changes: 0 additions & 101 deletions enzyme/benchmarks/ReverseMode/ode/ode-adept.cpp

This file was deleted.

Loading

0 comments on commit 7145c49

Please sign in to comment.