/* ---------------------------------------------------------------------------- * GTSAM Copyright 2010, Georgia Tech Research Corporation, * Atlanta, Georgia 30332-0415 * All Rights Reserved * Authors: Frank Dellaert, et al. (see THANKS for the full author list) * See LICENSE for the license information * -------------------------------------------------------------------------- */ /** * @file Expression-inl.h * @date September 18, 2014 * @author Frank Dellaert * @author Paul Furgale * @brief Internals for Expression.h, not for general consumption */ #pragma once #include #include #include #include #include #include #include // template meta-programming headers #include namespace MPL = boost::mpl::placeholders; #include class ExpressionFactorBinaryTest; // Forward declare for testing namespace gtsam { template class Expression; /** * Expressions are designed to write their derivatives into an already allocated * Jacobian of the correct size, of type VerticalBlockMatrix. * The JacobianMap provides a mapping from keys to the underlying blocks. */ class JacobianMap { const FastVector& keys_; VerticalBlockMatrix& Ab_; public: JacobianMap(const FastVector& keys, VerticalBlockMatrix& Ab) : keys_(keys), Ab_(Ab) { } /// Access via key VerticalBlockMatrix::Block operator()(Key key) { FastVector::const_iterator it = std::find(keys_.begin(), keys_.end(), key); DenseIndex block = it - keys_.begin(); return Ab_(block); } }; //----------------------------------------------------------------------------- /// Handle Leaf Case: reverseAD ends here, by writing a matrix into Jacobians template void handleLeafCase(const Eigen::Matrix& dTdA, JacobianMap& jacobians, Key key) { jacobians(key).block(0, 0) += dTdA; // block makes HUGE difference } /// Handle Leaf Case for Dynamic ROWS Matrix type (slower) template inline void handleLeafCase( const Eigen::Matrix& dTdA, JacobianMap& jacobians, Key key) { jacobians(key) += dTdA; } //----------------------------------------------------------------------------- /** * The ExecutionTrace class records a tree-structured expression's execution. * * The class looks a bit complicated but it is so for performance. * It is a tagged union that obviates the need to create * a ExecutionTrace subclass for Constants and Leaf Expressions. Instead * the key for the leaf is stored in the space normally used to store a * CallRecord*. Nothing is stored for a Constant. * * A full execution trace of a Binary(Unary(Binary(Leaf,Constant)),Leaf) would be: * Trace(Function) -> * BinaryRecord with two traces in it * trace1(Function) -> * UnaryRecord with one trace in it * trace1(Function) -> * BinaryRecord with two traces in it * trace1(Leaf) * trace2(Constant) * trace2(Leaf) * Hence, there are three Record structs, written to memory by traceExecution */ template class ExecutionTrace { static const int Dim = traits::dimension::value; enum { Constant, Leaf, Function } kind; union { Key key; CallRecord* ptr; } content; public: /// Pointer always starts out as a Constant ExecutionTrace() : kind(Constant) { } /// Change pointer to a Leaf Record void setLeaf(Key key) { kind = Leaf; content.key = key; } /// Take ownership of pointer to a Function Record void setFunction(CallRecord* record) { kind = Function; content.ptr = record; } /// Print void print(const std::string& indent = "") const { if (kind == Constant) std::cout << indent << "Constant" << std::endl; else if (kind == Leaf) std::cout << indent << "Leaf, key = " << content.key << std::endl; else if (kind == Function) { std::cout << indent << "Function" << std::endl; content.ptr->print(indent + " "); } } /// Return record pointer, quite unsafe, used only for testing template boost::optional record() { if (kind != Function) return boost::none; else { Record* p = dynamic_cast(content.ptr); return p ? boost::optional(p) : boost::none; } } /** * *** This is the main entry point for reverseAD, called from Expression *** * Called only once, either inserts I into Jacobians (Leaf) or starts AD (Function) */ typedef Eigen::Matrix JacobianTT; void startReverseAD(JacobianMap& jacobians) const { if (kind == Leaf) { // This branch will only be called on trivial Leaf expressions, i.e. Priors static const JacobianTT I = JacobianTT::Identity(); handleLeafCase(I, jacobians, content.key); } else if (kind == Function) // This is the more typical entry point, starting the AD pipeline // Inside the startReverseAD that the correctly dimensioned pipeline is chosen. content.ptr->startReverseAD(jacobians); } // Either add to Jacobians (Leaf) or propagate (Function) template void reverseAD(const Eigen::MatrixBase & dTdA, JacobianMap& jacobians) const { if (kind == Leaf) handleLeafCase(dTdA.eval(), jacobians, content.key); else if (kind == Function) content.ptr->reverseAD(dTdA.eval(), jacobians); } /// Define type so we can apply it as a meta-function typedef ExecutionTrace type; }; //----------------------------------------------------------------------------- /** * Expression node. The superclass for objects that do the heavy lifting * An Expression has a pointer to an ExpressionNode underneath * allowing Expressions to have polymorphic behaviour even though they * are passed by value. This is the same way boost::function works. * http://loki-lib.sourceforge.net/html/a00652.html */ template class ExpressionNode { protected: size_t traceSize_; /// Constructor, traceSize is size of the execution trace of expression rooted here ExpressionNode(size_t traceSize = 0) : traceSize_(traceSize) { } public: /// Destructor virtual ~ExpressionNode() { } /// Return keys that play in this expression as a set virtual std::set keys() const { std::set keys; return keys; } /// Return dimensions for each argument, as a map virtual void dims(std::map& map) const { } // Return size needed for memory buffer in traceExecution size_t traceSize() const { return traceSize_; } /// Return value virtual T value(const Values& values) const = 0; /// Construct an execution trace for reverse AD virtual T traceExecution(const Values& values, ExecutionTrace& trace, char* raw) const = 0; }; //----------------------------------------------------------------------------- /// Constant Expression template class ConstantExpression: public ExpressionNode { /// The constant value T constant_; /// Constructor with a value, yielding a constant ConstantExpression(const T& value) : constant_(value) { } friend class Expression ; public: /// Return value virtual T value(const Values& values) const { return constant_; } /// Construct an execution trace for reverse AD virtual T traceExecution(const Values& values, ExecutionTrace& trace, char* raw) const { return constant_; } }; //----------------------------------------------------------------------------- /// Leaf Expression template > class LeafExpression: public ExpressionNode { typedef ChartValue value_type; // perhaps this can be something else like a std::pair ?? /// The key into values Key key_; /// Constructor with a single key LeafExpression(Key key) : key_(key) { } // todo: do we need a virtual destructor here too? friend class Expression ; public: /// Return keys that play in this expression virtual std::set keys() const { std::set keys; keys.insert(key_); return keys; } /// Return dimensions for each argument virtual void dims(std::map& map) const { // get dimension from the chart; only works for fixed dimension charts map[key_] = traits::dimension::value; } /// Return value virtual const value_type& value(const Values& values) const { return dynamic_cast(values.at(key_)); } /// Construct an execution trace for reverse AD virtual const value_type& traceExecution(const Values& values, ExecutionTrace& trace, char* raw) const { trace.setLeaf(key_); return dynamic_cast(values.at(key_)); } }; //----------------------------------------------------------------------------- /// Leaf Expression, if no chart is given, assume default chart and value_type is just the plain value template class LeafExpression > : public ExpressionNode { typedef T value_type; /// The key into values Key key_; /// Constructor with a single key LeafExpression(Key key) : key_(key) { } // todo: do we need a virtual destructor here too? friend class Expression ; public: /// Return keys that play in this expression virtual std::set keys() const { std::set keys; keys.insert(key_); return keys; } /// Return dimensions for each argument virtual void dims(std::map& map) const { map[key_] = traits::dimension::value; } /// Return value virtual T value(const Values& values) const { return values.at(key_); } /// Construct an execution trace for reverse AD virtual T traceExecution(const Values& values, ExecutionTrace& trace, char* raw) const { trace.setLeaf(key_); return values.at(key_); } }; //----------------------------------------------------------------------------- // Below we use the "Class Composition" technique described in the book // C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost // and Beyond. Abrahams, David; Gurtovoy, Aleksey. Pearson Education. // to recursively generate a class, that will be the base for function nodes. // // The class generated, for three arguments A1, A2, and A3 will be // // struct Base1 : Argument, FunctionalBase { // ... storage related to A1 ... // ... methods that work on A1 ... // }; // // struct Base2 : Argument, Base1 { // ... storage related to A2 ... // ... methods that work on A2 and (recursively) on A1 ... // }; // // struct Base3 : Argument, Base2 { // ... storage related to A3 ... // ... methods that work on A3 and (recursively) on A2 and A1 ... // }; // // struct FunctionalNode : Base3 { // Provides convenience access to storage in hierarchy by using // static_cast &>(*this) // } // // All this magic happens when we generate the Base3 base class of FunctionalNode // by invoking boost::mpl::fold over the meta-function GenerateFunctionalNode // // Similarly, the inner Record struct will be // // struct Record1 : JacobianTrace, CallRecord::value> { // ... storage related to A1 ... // ... methods that work on A1 ... // }; // // struct Record2 : JacobianTrace, Record1 { // ... storage related to A2 ... // ... methods that work on A2 and (recursively) on A1 ... // }; // // struct Record3 : JacobianTrace, Record2 { // ... storage related to A3 ... // ... methods that work on A3 and (recursively) on A2 and A1 ... // }; // // struct Record : Record3 { // Provides convenience access to storage in hierarchy by using // static_cast &>(*this) // } // //----------------------------------------------------------------------------- /// meta-function to generate fixed-size JacobianTA type template struct Jacobian { typedef Eigen::Matrix::value, traits::dimension::value> type; }; /// meta-function to generate JacobianTA optional reference template struct OptionalJacobian { typedef Eigen::Matrix::value, traits::dimension::value> Jacobian; typedef boost::optional type; }; /** * Base case for recursive FunctionalNode class */ template struct FunctionalBase: ExpressionNode { static size_t const N = 0; // number of arguments struct Record { void print(const std::string& indent) const { } void startReverseAD(JacobianMap& jacobians) const { } template void reverseAD(const SomeMatrix & dFdT, JacobianMap& jacobians) const { } }; /// Construct an execution trace for reverse AD void trace(const Values& values, Record* record, char*& raw) const { // base case: does not do anything } }; /** * Building block for recursive FunctionalNode class * The integer argument N is to guarantee a unique type signature, * so we are guaranteed to be able to extract their values by static cast. */ template struct Argument { /// Expression that will generate value/derivatives for argument boost::shared_ptr > expression; }; /** * Building block for Recursive Record Class * Records the evaluation of a single argument in a functional expression */ template struct JacobianTrace { A value; ExecutionTrace trace; typename Jacobian::type dTdA; }; /** * Recursive Definition of Functional ExpressionNode */ template struct GenerateFunctionalNode: Argument, Base { static size_t const N = Base::N + 1; ///< Number of arguments in hierarchy typedef Argument This; ///< The storage we have direct access to /// Return keys that play in this expression virtual std::set keys() const { std::set keys = Base::keys(); std::set myKeys = This::expression->keys(); keys.insert(myKeys.begin(), myKeys.end()); return keys; } /// Return dimensions for each argument virtual void dims(std::map& map) const { Base::dims(map); This::expression->dims(map); } /// Recursive Record Class for Functional Expressions struct Record: JacobianTrace, Base::Record { typedef T return_type; typedef JacobianTrace This; /// Print to std::cout void print(const std::string& indent) const { Base::Record::print(indent); static const Eigen::IOFormat matlab(0, 1, " ", "; ", "", "", "[", "]"); std::cout << This::dTdA.format(matlab) << std::endl; This::trace.print(indent); } /// Start the reverse AD process void startReverseAD(JacobianMap& jacobians) const { Base::Record::startReverseAD(jacobians); This::trace.reverseAD(This::dTdA, jacobians); } /// Given df/dT, multiply in dT/dA and continue reverse AD process template void reverseAD(const Eigen::Matrix & dFdT, JacobianMap& jacobians) const { Base::Record::reverseAD(dFdT, jacobians); This::trace.reverseAD(dFdT * This::dTdA, jacobians); } }; /// Construct an execution trace for reverse AD void trace(const Values& values, Record* record, char*& raw) const { Base::trace(values, record, raw); // recurse // Write an Expression execution trace in record->trace // Iff Constant or Leaf, this will not write to raw, only to trace. // Iff the expression is functional, write all Records in raw buffer // Return value of type T is recorded in record->value record->Record::This::value = This::expression->traceExecution(values, record->Record::This::trace, raw); // raw is never modified by traceExecution, but if traceExecution has // written in the buffer, the next caller expects we advance the pointer raw += This::expression->traceSize(); } }; /** * Recursive GenerateFunctionalNode class Generator */ template struct FunctionalNode { /// The following typedef generates the recursively defined Base class typedef typename boost::mpl::fold, GenerateFunctionalNode >::type Base; /** * The type generated by this meta-function derives from Base * and adds access functions as well as the crucial [trace] function */ struct type: public Base { // Argument types and derived, note these are base 0 ! // These are currently not used - useful for Phoenix in future #ifdef EXPRESSIONS_PHOENIX typedef TYPES Arguments; typedef typename boost::mpl::transform >::type Jacobians; typedef typename boost::mpl::transform >::type Optionals; #endif /// Reset expression shared pointer template void reset(const boost::shared_ptr >& ptr) { static_cast &>(*this).expression = ptr; } /// Access Expression shared pointer template boost::shared_ptr > expression() const { return static_cast const &>(*this).expression; } /// Provide convenience access to Record storage and implement /// the virtual function based interface of CallRecord using the CallRecordImplementor struct Record: public internal::CallRecordImplementor::value>, public Base::Record { using Base::Record::print; using Base::Record::startReverseAD; using Base::Record::reverseAD; virtual ~Record() { } /// Access Value template const A& value() const { return static_cast const &>(*this).value; } /// Access Jacobian template typename Jacobian::type& jacobian() { return static_cast&>(*this).dTdA; } }; /// Construct an execution trace for reverse AD Record* trace(const Values& values, char* raw) const { // Create the record and advance the pointer Record* record = new (raw) Record(); raw = (char*) (record + 1); // Record the traces for all arguments // After this, the raw pointer is set to after what was written Base::trace(values, record, raw); // Return the record for this function evaluation return record; } }; }; //----------------------------------------------------------------------------- /// Unary Function Expression template class UnaryExpression: public FunctionalNode >::type { public: typedef boost::function::type)> Function; typedef typename FunctionalNode >::type Base; typedef typename Base::Record Record; private: Function function_; /// Constructor with a unary function f, and input argument e UnaryExpression(Function f, const Expression& e1) : function_(f) { this->template reset(e1.root()); ExpressionNode::traceSize_ = sizeof(Record) + e1.traceSize(); } friend class Expression ; public: /// Return value virtual T value(const Values& values) const { return function_(this->template expression()->value(values), boost::none); } /// Construct an execution trace for reverse AD virtual T traceExecution(const Values& values, ExecutionTrace& trace, char* raw) const { Record* record = Base::trace(values, raw); trace.setFunction(record); return function_(record->template value(), record->template jacobian()); } }; //----------------------------------------------------------------------------- /// Binary Expression template class BinaryExpression: public FunctionalNode >::type { public: typedef boost::function< T(const A1&, const A2&, typename OptionalJacobian::type, typename OptionalJacobian::type)> Function; typedef typename FunctionalNode >::type Base; typedef typename Base::Record Record; private: Function function_; /// Constructor with a ternary function f, and three input arguments BinaryExpression(Function f, const Expression& e1, const Expression& e2) : function_(f) { this->template reset(e1.root()); this->template reset(e2.root()); ExpressionNode::traceSize_ = // sizeof(Record) + e1.traceSize() + e2.traceSize(); } friend class Expression ; friend class ::ExpressionFactorBinaryTest; public: /// Return value virtual T value(const Values& values) const { using boost::none; return function_(this->template expression()->value(values), this->template expression()->value(values), none, none); } /// Construct an execution trace for reverse AD virtual T traceExecution(const Values& values, ExecutionTrace& trace, char* raw) const { Record* record = Base::trace(values, raw); trace.setFunction(record); return function_(record->template value(), record->template value(), record->template jacobian(), record->template jacobian()); } }; //----------------------------------------------------------------------------- /// Ternary Expression template class TernaryExpression: public FunctionalNode >::type { public: typedef boost::function< T(const A1&, const A2&, const A3&, typename OptionalJacobian::type, typename OptionalJacobian::type, typename OptionalJacobian::type)> Function; typedef typename FunctionalNode >::type Base; typedef typename Base::Record Record; private: Function function_; /// Constructor with a ternary function f, and three input arguments TernaryExpression(Function f, const Expression& e1, const Expression& e2, const Expression& e3) : function_(f) { this->template reset(e1.root()); this->template reset(e2.root()); this->template reset(e3.root()); ExpressionNode::traceSize_ = // sizeof(Record) + e1.traceSize() + e2.traceSize() + e3.traceSize(); } friend class Expression ; public: /// Return value virtual T value(const Values& values) const { using boost::none; return function_(this->template expression()->value(values), this->template expression()->value(values), this->template expression()->value(values), none, none, none); } /// Construct an execution trace for reverse AD virtual T traceExecution(const Values& values, ExecutionTrace& trace, char* raw) const { Record* record = Base::trace(values, raw); trace.setFunction(record); return function_( record->template value(), record->template value(), record->template value(), record->template jacobian(), record->template jacobian(), record->template jacobian()); } }; //----------------------------------------------------------------------------- }