WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
adjoint_expression_graph.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <ranges>
6#include <utility>
7
8#include <Eigen/SparseCore>
10
14
15namespace slp::detail {
16
17/**
18 * This class is an adaptor type that performs value updates of an expression's
19 * adjoint graph.
20 */
22 public:
23 /**
24 * Generates the adjoint graph for the given expression.
25 *
26 * @param root The root node of the expression.
27 */
28 explicit AdjointExpressionGraph(const Variable& root)
29 : m_top_list{topological_sort(root.expr)} {
30 for (const auto& node : m_top_list) {
31 m_col_list.emplace_back(node->col);
32 }
33 }
34
35 /**
36 * Update the values of all nodes in this adjoint graph based on the values of
37 * their dependent nodes.
38 */
39 void update_values() { detail::update_values(m_top_list); }
40
41 /**
42 * Returns the variable's gradient tree.
43 *
44 * This function lazily allocates variables, so elements of the returned
45 * VariableMatrix will be empty if the corresponding element of wrt had no
46 * adjoint. Ensure Variable::expr isn't nullptr before calling member
47 * functions.
48 *
49 * @param wrt Variables with respect to which to compute the gradient.
50 * @return The variable's gradient tree.
51 */
53 // Read docs/algorithms.md#Reverse_accumulation_automatic_differentiation
54 // for background on reverse accumulation automatic differentiation.
55
56 if (m_top_list.empty()) {
58 }
59
60 // Set root node's adjoint to 1 since df/df is 1
61 m_top_list[0]->adjoint_expr = make_expression_ptr<ConstExpression>(1.0);
62
63 // df/dx = (df/dy)(dy/dx). The adjoint of x is equal to the adjoint of y
64 // multiplied by dy/dx. If there are multiple "paths" from the root node to
65 // variable; the variable's adjoint is the sum of each path's adjoint
66 // contribution.
67 for (auto& node : m_top_list) {
68 auto& lhs = node->args[0];
69 auto& rhs = node->args[1];
70
71 if (lhs != nullptr) {
72 lhs->adjoint_expr += node->grad_expr_l(lhs, rhs, node->adjoint_expr);
73 if (rhs != nullptr) {
74 rhs->adjoint_expr += node->grad_expr_r(lhs, rhs, node->adjoint_expr);
75 }
76 }
77 }
78
79 // Move gradient tree to return value
81 for (int row = 0; row < grad.rows(); ++row) {
82 grad[row] = Variable{std::move(wrt[row].expr->adjoint_expr)};
83 }
84
85 // Unlink adjoints to avoid circular references between them and their
86 // parent expressions. This ensures all expressions are returned to the free
87 // list.
88 for (auto& node : m_top_list) {
89 node->adjoint_expr = nullptr;
90 }
91
92 return grad;
93 }
94
95 /**
96 * Updates the adjoints in the expression graph (computes the gradient) then
97 * appends the adjoints of wrt to the sparse matrix triplets.
98 *
99 * @param triplets The sparse matrix triplets.
100 * @param row The row of wrt.
101 * @param wrt Vector of variables with respect to which to compute the
102 * Jacobian.
103 */
105 gch::small_vector<Eigen::Triplet<double>>& triplets, int row,
106 const VariableMatrix& wrt) const {
107 // Read docs/algorithms.md#Reverse_accumulation_automatic_differentiation
108 // for background on reverse accumulation automatic differentiation.
109
110 // If wrt has fewer nodes than graph, zero wrt's adjoints
111 if (static_cast<size_t>(wrt.rows()) < m_top_list.size()) {
112 for (const auto& elem : wrt) {
113 elem.expr->adjoint = 0.0;
114 }
115 }
116
117 if (m_top_list.empty()) {
118 return;
119 }
120
121 // Set root node's adjoint to 1 since df/df is 1
122 m_top_list[0]->adjoint = 1.0;
123
124 // Zero the rest of the adjoints
125 for (auto& node : m_top_list | std::views::drop(1)) {
126 node->adjoint = 0.0;
127 }
128
129 // df/dx = (df/dy)(dy/dx). The adjoint of x is equal to the adjoint of y
130 // multiplied by dy/dx. If there are multiple "paths" from the root node to
131 // variable; the variable's adjoint is the sum of each path's adjoint
132 // contribution.
133 for (const auto& node : m_top_list) {
134 auto& lhs = node->args[0];
135 auto& rhs = node->args[1];
136
137 if (lhs != nullptr) {
138 if (rhs != nullptr) {
139 lhs->adjoint += node->grad_l(lhs->val, rhs->val, node->adjoint);
140 rhs->adjoint += node->grad_r(lhs->val, rhs->val, node->adjoint);
141 } else {
142 lhs->adjoint += node->grad_l(lhs->val, 0.0, node->adjoint);
143 }
144 }
145 }
146
147 // If wrt has fewer nodes than graph, iterate over wrt
148 if (static_cast<size_t>(wrt.rows()) < m_top_list.size()) {
149 for (int col = 0; col < wrt.rows(); ++col) {
150 const auto& node = wrt[col].expr;
151
152 // Append adjoints of wrt to sparse matrix triplets
153 if (node->adjoint != 0.0) {
154 triplets.emplace_back(row, col, node->adjoint);
155 }
156 }
157 } else {
158 for (size_t i = 0; i < m_top_list.size(); ++i) {
159 const auto& col = m_col_list[i];
160 const auto& node = m_top_list[i];
161
162 // Append adjoints of wrt to sparse matrix triplets
163 if (col != -1 && node->adjoint != 0.0) {
164 triplets.emplace_back(row, col, node->adjoint);
165 }
166 }
167 }
168 }
169
170 private:
171 // Topological sort of graph from parent to child
173
174 // List that maps nodes to their respective column
175 gch::small_vector<int> m_col_list;
176};
177
178} // namespace slp::detail
An autodiff variable pointing to an expression node.
Definition variable.hpp:40
A matrix of autodiff variables.
Definition variable_matrix.hpp:29
int rows() const
Returns the number of rows in the matrix.
Definition variable_matrix.hpp:914
static constexpr empty_t empty
Designates an uninitialized VariableMatrix.
Definition variable_matrix.hpp:39
This class is an adaptor type that performs value updates of an expression's adjoint graph.
Definition adjoint_expression_graph.hpp:21
VariableMatrix generate_gradient_tree(const VariableMatrix &wrt) const
Returns the variable's gradient tree.
Definition adjoint_expression_graph.hpp:52
void update_values()
Update the values of all nodes in this adjoint graph based on the values of their dependent nodes.
Definition adjoint_expression_graph.hpp:39
AdjointExpressionGraph(const Variable &root)
Generates the adjoint graph for the given expression.
Definition adjoint_expression_graph.hpp:28
void append_adjoint_triplets(gch::small_vector< Eigen::Triplet< double > > &triplets, int row, const VariableMatrix &wrt) const
Updates the adjoints in the expression graph (computes the gradient) then appends the adjoints of wrt...
Definition adjoint_expression_graph.hpp:104
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1198
Definition expression_graph.hpp:11
gch::small_vector< Expression * > topological_sort(const ExpressionPtr &root)
Generate a topological sort of an expression graph from parent to child.
Definition expression_graph.hpp:20
void update_values(const gch::small_vector< Expression * > &list)
Update the values of all nodes in this graph based on the values of their dependent nodes.
Definition expression_graph.hpp:78
static ExpressionPtr make_expression_ptr(Args &&... args)
Creates an intrusive shared pointer to an expression from the global pool allocator.
Definition expression.hpp:48