-
Notifications
You must be signed in to change notification settings - Fork 168
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
Implement new autodiff type based on tape representation #89
Open
supersega
wants to merge
17
commits into
autodiff:main
Choose a base branch
from
supersega:reverse-mode-tape-rep
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2a2566e
[autodiff/mixed] Implement base expression class for new mixed mode type
supersega 88114bb
[autodiff/mixed] Add expression tree node class which hold topology a…
supersega 9663649
[autodiff/mixed] Create tape based storage for expression tree nodes
supersega 7912624
[autodiff/mixed] Add variable which supports mixed type of automatic …
supersega 1f669b2
[autodiff/mixed] Implement meta function which help to select derivat…
supersega 1d2d962
[autodiff/mixed] Add algorithm which compute all first order derivatives
supersega 613707f
[autodiff/mixed] Implement parameter expression to be able to create …
supersega 2df418b
[autodiff/mixed] Implement binary expressions classes for differentia…
supersega 6c346a9
[autodiff/mixed] Implement unary expressions classes for differentiab…
supersega e4c2a20
[autodiff/mixed] Overload binary operators for mixed differentiable v…
supersega f4ae890
[autodiff/mixed] Create sinus function for mixed differentiable variable
supersega 7589f8d
[autodiff/mixed] Add Eigen support for first order mixed differentiab…
supersega f25f3d1
[autodiff/mixed] Calculate gradient w.r.t. Eigen vector
supersega 3e3a45d
[autodiff/mixed] Add alias headers for all folders in mixed
supersega a5fa2aa
[autodiff/mixed] Add mixed header file for autodiff
supersega 152d5a5
[autodiff/mixed] Add stream operator for mixed type variable
supersega d0af7aa
[autodiff/mixed] Add first example for autodiff variable
supersega 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 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,37 @@ | ||
// _ _ | ||
// _ _|_ _ _|o_|__|_ | ||
// (_||_||_(_)(_|| | | | ||
// | ||
// automatic differentiation made easier in C++ | ||
// https://github.com/autodiff/autodiff | ||
// | ||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>. | ||
// | ||
// Copyright (c) 2018-2019 Serhii Malyshev | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#pragma once | ||
|
||
// autodiff includes | ||
#include <autodiff/mixed/algorithms.hpp> | ||
#include <autodiff/mixed/core.hpp> | ||
#include <autodiff/mixed/expressions.hpp> | ||
#include <autodiff/mixed/functions.hpp> | ||
#include <autodiff/mixed/operators.hpp> |
This file contains 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,30 @@ | ||
// _ _ | ||
// _ _|_ _ _|o_|__|_ | ||
// (_||_||_(_)(_|| | | | ||
// | ||
// automatic differentiation made easier in C++ | ||
// https://github.com/autodiff/autodiff | ||
// | ||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>. | ||
// | ||
// Copyright (c) 2018-2019 Serhii Malyshev | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#include <autodiff/mixed/algorithms/first_order.hpp> | ||
This file contains 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,82 @@ | ||
// _ _ | ||
// _ _|_ _ _|o_|__|_ | ||
// (_||_||_(_)(_|| | | | ||
// | ||
// automatic differentiation made easier in C++ | ||
// https://github.com/autodiff/autodiff | ||
// | ||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>. | ||
// | ||
// Copyright (c) 2018-2019 Serhii Malyshev | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include <autodiff/mixed/core/node.hpp> | ||
#include <autodiff/mixed/core/variable.hpp> | ||
|
||
namespace autodiff::taperep | ||
{ | ||
namespace detail | ||
{ | ||
/// @brief Compute all first derivatives derivatives using reverse loop. | ||
/// @param y target variable which derivatives we want to compute. | ||
/// @return all derivatives of target variable in <code>std::vector</code> | ||
template<typename Var> | ||
auto all_first_derivatives(const Var& x) -> std::vector<double> | ||
{ | ||
const auto& nodes = x.tape().nodes(); | ||
const auto length = nodes.size(); | ||
|
||
std::vector<double> derivatives(length, 0.0); | ||
|
||
derivatives[x.index()] = 1.0; | ||
|
||
for (auto i = length; i > 0; i--) | ||
{ | ||
const auto& node = nodes[i - 1]; | ||
|
||
for (std::size_t j = 0; j < node.parents_count(); j++) | ||
derivatives[node.parent(j)] += node. template partial_derivative<first_order>(j) * derivatives[i - 1]; | ||
} | ||
|
||
return derivatives; | ||
} | ||
} | ||
|
||
/// @brief Compute all first derivatives derivatives using reverse loop. | ||
/// @param y Target variable which derivatives we want to compute. | ||
/// @return Function due to we can get derivatives by operator <code>()</code>. | ||
template<typename Var> | ||
auto first_derivatives(const Var& y) | ||
{ | ||
static_assert(traits::is_var_v<Var>, "Please, use 'first_derivatives' function only for var"); | ||
|
||
return [derivatives = detail::all_first_derivatives(y)](auto x) -> double | ||
{ | ||
static_assert(traits::is_var_v<decltype(x)>, | ||
"Looks like you want to get derivative for non var type, it is prevented"); | ||
|
||
return derivatives[x.index()]; | ||
}; | ||
} | ||
} // namespace autodiff::taperep |
This file contains 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,35 @@ | ||
// _ _ | ||
// _ _|_ _ _|o_|__|_ | ||
// (_||_||_(_)(_|| | | | ||
// | ||
// automatic differentiation made easier in C++ | ||
// https://github.com/autodiff/autodiff | ||
// | ||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>. | ||
// | ||
// Copyright (c) 2018-2019 Serhii Malyshev | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#pragma once | ||
|
||
#include <autodiff/mixed/core/meta.hpp> | ||
#include <autodiff/mixed/core/node.hpp> | ||
#include <autodiff/mixed/core/tape.hpp> | ||
#include <autodiff/mixed/core/variable.hpp> |
This file contains 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,69 @@ | ||
// _ _ | ||
// _ _|_ _ _|o_|__|_ | ||
// (_||_||_(_)(_|| | | | ||
// | ||
// automatic differentiation made easier in C++ | ||
// https://github.com/autodiff/autodiff | ||
// | ||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>. | ||
// | ||
// Copyright (c) 2018-2019 Serhii Malyshev | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#pragma once | ||
|
||
#include <array> | ||
#include <tuple> | ||
|
||
namespace autodiff::meta | ||
{ | ||
namespace detail | ||
{ | ||
/// @brief Meta function to generate tuple which contains derivatives array. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should just arrange about documentation style, and I will do ;) |
||
/// @return Tuple which contain array of derivatives weights. | ||
constexpr auto generate_tuple_of_derivatives_weights(std::index_sequence<0>) | ||
{ | ||
return std::tuple(std::array<double, 2>{ }); | ||
} | ||
|
||
/// @brief Meta function to generate tuple which contains derivatives arrays. | ||
/// @return Tuple which contain arrays of derivatives weights. | ||
template<std::size_t I, std::size_t... N> | ||
constexpr auto generate_tuple_of_derivatives_weights(std::index_sequence<I, N...>) | ||
{ | ||
constexpr auto cnt = sizeof...(N); | ||
return std::tuple_cat(generate_tuple_of_derivatives_weights(std::make_index_sequence<cnt>{}), | ||
std::tuple(std::array<double, cnt + 2>{})); | ||
} | ||
|
||
/// @brief Meta function to generate type of weights container | ||
/// @return Type of container. | ||
template<std::size_t N> | ||
struct tuple_of_derivatives_weights | ||
{ | ||
using type = decltype(generate_tuple_of_derivatives_weights(std::make_index_sequence<N>{})); | ||
}; | ||
} | ||
|
||
/// @brief Meta function to generate type of weights container | ||
/// @return Type of container | ||
template<std::size_t N> | ||
using tuple_of_derivatives_weights_t = typename detail::tuple_of_derivatives_weights<N>::type; | ||
} |
This file contains 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,94 @@ | ||
#pragma once | ||
|
||
// _ _ | ||
// _ _|_ _ _|o_|__|_ | ||
// (_||_||_(_)(_|| | | | ||
// | ||
// automatic differentiation made easier in C++ | ||
// https://github.com/autodiff/autodiff | ||
// | ||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>. | ||
// | ||
// Copyright (c) 2018-2019 Serhii Malyshev | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#include <array> | ||
#include <vector> | ||
#include <memory> | ||
|
||
#include <autodiff/mixed/expressions/expression.hpp> | ||
|
||
namespace autodiff::taperep | ||
{ | ||
/// @brief Struct which represent node of expression graph. | ||
/// @details Give access to topology of node. Makes enable to update children etc. | ||
/// Node holds topology(parent indices) and differential properties (expression) in separate members. | ||
/// @tparam N order of node. | ||
template<std::size_t N> | ||
struct node final | ||
{ | ||
static_assert(N > 0 && "bad idea to create node with zero order derivatives variable, use double or other trivial type"); | ||
static_assert(N <= 2 && "we support only 1st and 2nd order vars"); | ||
|
||
/// @brief Construct node from parents indices and underlying expression. | ||
/// @param parents parent indices (topological info). | ||
/// @param expr storage for values and derivatives. | ||
node(std::array<std::size_t, 2> parents, expression_ptr<N> expr) : | ||
m_parents { parents }, m_expression { expr } { } | ||
|
||
/// @brief access to class which holds differential props. | ||
/// @return node differential props. | ||
auto expression() const -> const expression_ptr<N>& | ||
{ | ||
return m_expression; | ||
} | ||
|
||
/// @brief Partial derivative at position i. | ||
/// @details For example if we work with first order derivatives | ||
/// i = 0 represent dnode / dleft, i = 1 represent dnode / dright. | ||
/// @tparam Order order of derivatives weights. | ||
/// @param i index. | ||
/// @return partial derivative of given order. | ||
template<std::size_t Order> | ||
auto partial_derivative(std::size_t i) const -> double | ||
{ | ||
return m_expression->template partial_derivatives<Order>()[i]; | ||
} | ||
|
||
/// @brief Parent index at position i. | ||
/// @param i index. | ||
/// @return Parent node index for given i. | ||
auto parent(std::size_t i) const -> std::size_t | ||
{ | ||
return m_parents[i]; | ||
} | ||
|
||
/// @brief Count of parents. | ||
/// @details For now always two. | ||
/// @return Number of parent nodes. | ||
auto parents_count() const -> std::size_t | ||
{ | ||
return 2; | ||
} | ||
private: | ||
std::array<std::size_t, 2> m_parents; ///< parents nodes indices | ||
expression_ptr<N> m_expression; ///< pointer to expression of node | ||
}; | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For file names, so far we have adopted a no-underline, no-caps convention, meaning, we would use
firstorder.hpp
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that
_
will help to reed file name, but if we prefer such naming as you mentioned I will change.