-
Notifications
You must be signed in to change notification settings - Fork 5
Additional Enzyme examples and use cases (e.g DistributedGenerator) #54
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
Merged
Merged
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
ba3ea2a
Fix spacing in Enzyme basic examples.
nkoukpaizan 2904eff
SparseMatrix directory.
nkoukpaizan 8254ce2
Add preliminary Enzyme support
ashermancinelli 8b50a7e
Use better naming conventions
ashermancinelli be8f2e8
Apply suggestions from code review
ashermancinelli 3d608a3
More verbose tests for enzyme support.
pelesh 536865e
int --> double in Enzyme examples.
nkoukpaizan aa7b2c8
Minor name changes for Enzyme tests.
nkoukpaizan b508275
Separate scalar and vector examples using Enzyme.
nkoukpaizan 119b572
Jacobian of vector input function is diagonal. Using Enzyme in forwar…
nkoukpaizan 23dc868
Attempt to use classes in EnzymeLibCheck.
nkoukpaizan 0d08de1
Minor improvements in scalar and vector Enzyme standalone tests.
nkoukpaizan cc83e0f
Minor name change for Enzyme test files.
nkoukpaizan fa3c09a
Move Enzyme examples.
nkoukpaizan caeef0c
Minor edits to match develop
nkoukpaizan e999877
LinearAlgebra directory in examples and DenseTest.
nkoukpaizan c0694dd
Fix path of example using Enzyme for vectors and Jacobians.
nkoukpaizan ee0c9a9
Enzyme vector/Jacobian example.
nkoukpaizan 1a6260d
Cleanup Enzyme CMakeList.
nkoukpaizan e84ad62
Bug fix (type): columns in DenseMatrix.
nkoukpaizan 9294ed3
A very manual way to link GridKit targets with Enzyme executables. Ne…
nkoukpaizan 2d9200b
Fixed spacing in Enzyme examples
nkoukpaizan 88845a6
Basic usage of DenseMatrix in EnzymeVector example.
nkoukpaizan 2ff261e
Intdx-->IdxT
nkoukpaizan 991dca1
Method to convert Dense matriz to COO.
nkoukpaizan 7f5d49f
Use DenseMatrix to store the Jacobians in Enzyme vector example.
nkoukpaizan c918663
Typo fix in CMakeLists: mirco-->micro.
nkoukpaizan f11e460
Minor formatting.
nkoukpaizan 4b39fe8
pragma once for COO_Matrix.
nkoukpaizan 91e291c
Better way to ensure paths exist for INCLUDES in enzyme_add_executable.
nkoukpaizan 016fb74
Merge branch 'develop' into nicholson/enzyme-usecase
nkoukpaizan de6cc71
Functional use of -Wl to link GridKit libraries with Enzyme examples.
nkoukpaizan a25ed69
Remove extra debug statements from FindEnzyme.
nkoukpaizan 6b53a07
Progress on model autodiff (distributed generator case).
nkoukpaizan 4e48386
Attempts with DisctributedGenerator autodiff.
nkoukpaizan 7ba837e
Name input for COO_Matrix printMatrix method.
nkoukpaizan c7c9546
Bug fix: need to check abs(value) to sparsify DenseMatrix.
nkoukpaizan 7a42e5f
Working Enzyme derivative of DistributedGenerator with evalResidual c…
nkoukpaizan 0096cf2
Method to set DenseMatrix values from COO.
nkoukpaizan d7ba02d
Using DenseMatrix form of reference Jacobian to check the answer.
nkoukpaizan 6dab218
EnzymeLibCheck->EnzymeLibScalarCheck in preparation for a vector vers…
nkoukpaizan ef110ac
Seemingly working EnzymeStandaloneVectorCheck using std::vector<doubl…
nkoukpaizan 733b37d
EnzymePowerElectronicsCheck also works with std::vector<double> inste…
nkoukpaizan 30e1065
Bug fix: Need to pass output vector as a reference.
nkoukpaizan 2b3d79f
Remove unneeded enzyme_autodiff declaration.
nkoukpaizan 37166f7
EnzymeLibVectorCheck.
nkoukpaizan de8e4fc
Minor fixes to EnzymeLibVectorCheck.
nkoukpaizan 4711620
Correct way to specify library depencencies in enzyme_add_executable?
nkoukpaizan 4ac1e49
Merge branch 'develop' into nicholson/enzyme-usecase
nkoukpaizan be03069
ModelLib -> GridKit on this branch.
nkoukpaizan a385880
Merge 'develop' into 'nicholson/enzyme-usecase'.
nkoukpaizan 8329bf9
Better documentation of the DenseMatrix class.
nkoukpaizan 140c2b7
Rename a few variables in Enzyme examples.
nkoukpaizan fc974dc
Merge 'develop' into 'nicholson/enzyme-usecase'.
nkoukpaizan 356d8a5
powerelec --> power_elec in GRIDKIT::targets.
nkoukpaizan 495ab8c
Added comments to describe the Enzyme examples along with TODO items …
nkoukpaizan 4fc7f46
Fix braces in Enzyme examples.
nkoukpaizan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
add_subdirectory(Standalone) | ||
add_subdirectory(Library) | ||
add_subdirectory(PowerElectronics) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
enzyme_add_executable(NAME EnzymeLibCheck SOURCES main.cpp library.cpp) | ||
#install(TARGETS ${CMAKE_CURRENT_BINARY_DIR}/EnzymeLibCheck DESTINATION bin) | ||
|
||
add_test(NAME "EnzymeLibCheck" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/EnzymeLibCheck) | ||
add_subdirectory(Scalar) | ||
add_subdirectory(Vector) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
enzyme_add_executable( | ||
NAME EnzymeLibScalarCheck | ||
SOURCES EnzymeScalar.cpp ScalarModel.cpp | ||
) | ||
|
||
add_test(NAME "EnzymeLibScalarCheck" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/EnzymeLibScalarCheck) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <iostream> | ||
#include <limits> | ||
#include "ScalarModel.hpp" | ||
|
||
/** | ||
* @brief Example that computes the derivative of a library function | ||
* (implemented as the member function of a class and operating directly on class members) | ||
* by automatic differentiation via Enzyme. | ||
* | ||
* TODO: Convert this into a unit test. | ||
*/ | ||
|
||
int main() | ||
{ | ||
int fail = 0; | ||
ScalarModel scalar_model; | ||
double var = 5.0; | ||
scalar_model.setVariable(var); | ||
scalar_model.evalFunction(); | ||
scalar_model.evalDerivative(); | ||
double sq = scalar_model.getFunctionValue(); | ||
double dsq = scalar_model.getDerivativeValue(); | ||
|
||
std::cout << "x = " << var << ", x^2 = " << sq << ", d(x^2)/dx = " << dsq << "\n"; | ||
if (std::abs(dsq - 2.0*var) > std::numeric_limits<double>::epsilon()) | ||
{ | ||
fail++; | ||
std::cout << "Result incorrect\n"; | ||
} | ||
std::cout << "Status: " << fail << "\n"; | ||
return fail; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
int enzyme_dup; | ||
int enzyme_dupnoneed; | ||
int enzyme_out; | ||
int enzyme_const; | ||
|
||
template <typename return_type, typename ... T> | ||
return_type __enzyme_fwddiff(return_type*, int, T* ... ); | ||
|
||
template <typename return_type, typename T> | ||
return_type wrapper(T* obj) | ||
{ | ||
obj->evalFunction(); | ||
return obj->getFunctionValue(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "EnzymeWrapper.hpp" | ||
#include "ScalarModel.hpp" | ||
#include <iostream> | ||
|
||
ScalarModel::ScalarModel() | ||
{ | ||
} | ||
|
||
inline | ||
double ScalarModel::square(double x) | ||
{ | ||
return x * x; | ||
} | ||
|
||
void ScalarModel::setVariable(double x) | ||
{ | ||
x_ = x; | ||
} | ||
|
||
void ScalarModel::evalFunction() | ||
{ | ||
f_ = square(x_); | ||
} | ||
|
||
void ScalarModel::evalDerivative() | ||
{ | ||
ScalarModel d_scalar_model; | ||
d_scalar_model.setVariable(1.0); | ||
df_dx_ = __enzyme_fwddiff<double, ScalarModel>((double*)wrapper<double, ScalarModel>, enzyme_dup, this, &d_scalar_model); | ||
} | ||
|
||
double ScalarModel::getVariable() const | ||
{ | ||
return x_; | ||
} | ||
|
||
double ScalarModel::getFunctionValue() const | ||
{ | ||
return f_; | ||
} | ||
|
||
double ScalarModel::getDerivativeValue() const | ||
{ | ||
return df_dx_; | ||
} | ||
|
||
ScalarModel::~ScalarModel() | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
/** | ||
* @brief Class providing methods to evaluate a function and its derivative. | ||
* This is used to test automatic differentiation. | ||
*/ | ||
class ScalarModel | ||
{ | ||
private: | ||
double x_, f_, df_dx_; | ||
inline double square(double); | ||
|
||
public: | ||
ScalarModel(); | ||
void setVariable(double); | ||
void evalFunction(); | ||
void evalDerivative(); | ||
double getVariable() const; | ||
double getFunctionValue() const; | ||
double getDerivativeValue() const; | ||
~ScalarModel(); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
enzyme_add_executable( | ||
NAME EnzymeLibVectorCheck | ||
SOURCES EnzymeVector.cpp VectorModel.cpp | ||
LINK_LIBRARIES GRIDKIT::DenseMatrix | ||
) | ||
|
||
add_test(NAME "EnzymeLibVectorCheck" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/EnzymeLibVectorCheck) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <iostream> | ||
#include <limits> | ||
#include "VectorModel.hpp" | ||
|
||
/** | ||
* @brief Example that computes the Jacobian of a vector-valued residual | ||
* (implemented as the member function of a class and operating directly on class members) | ||
* by automatic differentiation via Enzyme. | ||
* | ||
* TODO: Convert this into a unit test. | ||
*/ | ||
|
||
inline | ||
double dsquare_ref_scalar(double x) | ||
{ | ||
return 2.0 * x; | ||
} | ||
|
||
// Reference Jacobian | ||
DenseMatrix dsquare_ref(std::vector<double> x, std::vector<double> y) | ||
{ | ||
DenseMatrix jac(x.size(), y.size()); | ||
for (int idy = 0; idy < y.size(); ++idy) | ||
{ | ||
for (int idx = 0; idx < x.size(); ++idx) | ||
{ | ||
if (idx == idy) | ||
jac.setValue(idx, idy, dsquare_ref_scalar(x[idx])); | ||
} | ||
} | ||
return jac; | ||
} | ||
|
||
int main() | ||
{ | ||
// Size and variable declarations | ||
constexpr int n = 10; | ||
std::vector<double> var(n); | ||
|
||
// Random input values | ||
srand(time(NULL)); | ||
for (int idx = 0; idx < var.size(); ++idx) | ||
{ | ||
var[idx] = rand(); | ||
} | ||
|
||
// Model | ||
VectorModel* vector_model = new VectorModel(n); | ||
vector_model->setVariable(var); | ||
vector_model->evalResidual(); | ||
vector_model->evalJacobian(); | ||
std::vector<double> var_temp = vector_model->getVariable(); | ||
std::vector<double> res = vector_model->getResidual(); | ||
DenseMatrix jac = vector_model->getJacobian(); | ||
|
||
// Reference Jacobian | ||
DenseMatrix jac_ref = dsquare_ref(var, res); | ||
|
||
// Check | ||
int fail = 0; | ||
bool verbose = true; | ||
for (int idy = 0; idy < res.size(); ++idy) | ||
{ | ||
for (int idx = 0; idx < var.size(); ++idx) | ||
{ | ||
if (std::abs(jac.getValue(idx, idy) - jac_ref.getValue(idx, idy)) > std::numeric_limits<double>::epsilon()) | ||
{ | ||
fail++; | ||
if (verbose) | ||
{ | ||
std::cout << "Result incorrect at line = " << idy << ", column = " << idx << "\n"; | ||
std::cout << "x = " << var_temp[idx] << ", x^2 = " << res[idx] << ", d(x^2)/dx = " << jac.getValue(idx, idy) << "\n"; | ||
} | ||
} | ||
} | ||
} | ||
if (verbose) | ||
{ | ||
jac.printMatrix("Autodiff Jacobian"); | ||
jac_ref.printMatrix("Reference Jacobian"); | ||
} | ||
std::cout << "Status: " << fail << "\n"; | ||
|
||
// Cleanup | ||
delete vector_model; | ||
|
||
return fail; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
int enzyme_dup; | ||
int enzyme_dupnoneed; | ||
int enzyme_out; | ||
int enzyme_const; | ||
|
||
template <typename T> | ||
std::vector<double> __enzyme_fwddiff(std::vector<double>*, int, T*, T*); | ||
|
||
template <typename T> | ||
std::vector<double> wrapper(T* obj) | ||
{ | ||
obj->evalResidual(); | ||
return obj->getResidual(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.