Blame include/ceres/dynamic_autodiff_cost_function.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
//         mierle@gmail.com (Keir Mierle)
Packit ea1746
Packit ea1746
#ifndef CERES_PUBLIC_DYNAMIC_AUTODIFF_COST_FUNCTION_H_
Packit ea1746
#define CERES_PUBLIC_DYNAMIC_AUTODIFF_COST_FUNCTION_H_
Packit ea1746
Packit ea1746
#include <cmath>
Packit ea1746
#include <numeric>
Packit ea1746
#include <vector>
Packit ea1746
Packit ea1746
#include "ceres/dynamic_cost_function.h"
Packit ea1746
#include "ceres/internal/scoped_ptr.h"
Packit ea1746
#include "ceres/jet.h"
Packit ea1746
#include "glog/logging.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
Packit ea1746
// This autodiff implementation differs from the one found in
Packit ea1746
// autodiff_cost_function.h by supporting autodiff on cost functions
Packit ea1746
// with variable numbers of parameters with variable sizes. With the
Packit ea1746
// other implementation, all the sizes (both the number of parameter
Packit ea1746
// blocks and the size of each block) must be fixed at compile time.
Packit ea1746
//
Packit ea1746
// The functor API differs slightly from the API for fixed size
Packit ea1746
// autodiff; the expected interface for the cost functors is:
Packit ea1746
//
Packit ea1746
//   struct MyCostFunctor {
Packit ea1746
//     template<typename T>
Packit ea1746
//     bool operator()(T const* const* parameters, T* residuals) const {
Packit ea1746
//       // Use parameters[i] to access the i'th parameter block.
Packit ea1746
//     }
Packit ea1746
//   }
Packit ea1746
//
Packit ea1746
// Since the sizing of the parameters is done at runtime, you must
Packit ea1746
// also specify the sizes after creating the dynamic autodiff cost
Packit ea1746
// function. For example:
Packit ea1746
//
Packit ea1746
//   DynamicAutoDiffCostFunction<MyCostFunctor, 3> cost_function(
Packit ea1746
//       new MyCostFunctor());
Packit ea1746
//   cost_function.AddParameterBlock(5);
Packit ea1746
//   cost_function.AddParameterBlock(10);
Packit ea1746
//   cost_function.SetNumResiduals(21);
Packit ea1746
//
Packit ea1746
// Under the hood, the implementation evaluates the cost function
Packit ea1746
// multiple times, computing a small set of the derivatives (four by
Packit ea1746
// default, controlled by the Stride template parameter) with each
Packit ea1746
// pass. There is a tradeoff with the size of the passes; you may want
Packit ea1746
// to experiment with the stride.
Packit ea1746
template <typename CostFunctor, int Stride = 4>
Packit ea1746
class DynamicAutoDiffCostFunction : public DynamicCostFunction {
Packit ea1746
 public:
Packit ea1746
  explicit DynamicAutoDiffCostFunction(CostFunctor* functor)
Packit ea1746
    : functor_(functor) {}
Packit ea1746
Packit ea1746
  virtual ~DynamicAutoDiffCostFunction() {}
Packit ea1746
Packit ea1746
  virtual bool Evaluate(double const* const* parameters,
Packit ea1746
                        double* residuals,
Packit ea1746
                        double** jacobians) const {
Packit ea1746
    CHECK_GT(num_residuals(), 0)
Packit ea1746
        << "You must call DynamicAutoDiffCostFunction::SetNumResiduals() "
Packit ea1746
        << "before DynamicAutoDiffCostFunction::Evaluate().";
Packit ea1746
Packit ea1746
    if (jacobians == NULL) {
Packit ea1746
      return (*functor_)(parameters, residuals);
Packit ea1746
    }
Packit ea1746
Packit ea1746
    // The difficulty with Jets, as implemented in Ceres, is that they were
Packit ea1746
    // originally designed for strictly compile-sized use. At this point, there
Packit ea1746
    // is a large body of code that assumes inside a cost functor it is
Packit ea1746
    // acceptable to do e.g. T(1.5) and get an appropriately sized jet back.
Packit ea1746
    //
Packit ea1746
    // Unfortunately, it is impossible to communicate the expected size of a
Packit ea1746
    // dynamically sized jet to the static instantiations that existing code
Packit ea1746
    // depends on.
Packit ea1746
    //
Packit ea1746
    // To work around this issue, the solution here is to evaluate the
Packit ea1746
    // jacobians in a series of passes, each one computing Stripe *
Packit ea1746
    // num_residuals() derivatives. This is done with small, fixed-size jets.
Packit ea1746
    const int num_parameter_blocks = parameter_block_sizes().size();
Packit ea1746
    const int num_parameters = std::accumulate(parameter_block_sizes().begin(),
Packit ea1746
                                               parameter_block_sizes().end(),
Packit ea1746
                                               0);
Packit ea1746
Packit ea1746
    // Allocate scratch space for the strided evaluation.
Packit ea1746
    std::vector<Jet<double, Stride> > input_jets(num_parameters);
Packit ea1746
    std::vector<Jet<double, Stride> > output_jets(num_residuals());
Packit ea1746
Packit ea1746
    // Make the parameter pack that is sent to the functor (reused).
Packit ea1746
    std::vector<Jet<double, Stride>* > jet_parameters(num_parameter_blocks,
Packit ea1746
        static_cast<Jet<double, Stride>* >(NULL));
Packit ea1746
    int num_active_parameters = 0;
Packit ea1746
Packit ea1746
    // To handle constant parameters between non-constant parameter blocks, the
Packit ea1746
    // start position --- a raw parameter index --- of each contiguous block of
Packit ea1746
    // non-constant parameters is recorded in start_derivative_section.
Packit ea1746
    std::vector<int> start_derivative_section;
Packit ea1746
    bool in_derivative_section = false;
Packit ea1746
    int parameter_cursor = 0;
Packit ea1746
Packit ea1746
    // Discover the derivative sections and set the parameter values.
Packit ea1746
    for (int i = 0; i < num_parameter_blocks; ++i) {
Packit ea1746
      jet_parameters[i] = &input_jets[parameter_cursor];
Packit ea1746
Packit ea1746
      const int parameter_block_size = parameter_block_sizes()[i];
Packit ea1746
      if (jacobians[i] != NULL) {
Packit ea1746
        if (!in_derivative_section) {
Packit ea1746
          start_derivative_section.push_back(parameter_cursor);
Packit ea1746
          in_derivative_section = true;
Packit ea1746
        }
Packit ea1746
Packit ea1746
        num_active_parameters += parameter_block_size;
Packit ea1746
      } else {
Packit ea1746
        in_derivative_section = false;
Packit ea1746
      }
Packit ea1746
Packit ea1746
      for (int j = 0; j < parameter_block_size; ++j, parameter_cursor++) {
Packit ea1746
        input_jets[parameter_cursor].a = parameters[i][j];
Packit ea1746
      }
Packit ea1746
    }
Packit ea1746
Packit ea1746
    // When `num_active_parameters % Stride != 0` then it can be the case
Packit ea1746
    // that `active_parameter_count < Stride` while parameter_cursor is less
Packit ea1746
    // than the total number of parameters and with no remaining non-constant
Packit ea1746
    // parameter blocks. Pushing parameter_cursor (the total number of
Packit ea1746
    // parameters) as a final entry to start_derivative_section is required
Packit ea1746
    // because if a constant parameter block is encountered after the
Packit ea1746
    // last non-constant block then current_derivative_section is incremented
Packit ea1746
    // and would otherwise index an invalid position in
Packit ea1746
    // start_derivative_section. Setting the final element to the total number
Packit ea1746
    // of parameters means that this can only happen at most once in the loop
Packit ea1746
    // below.
Packit ea1746
    start_derivative_section.push_back(parameter_cursor);
Packit ea1746
Packit ea1746
    // Evaluate all of the strides. Each stride is a chunk of the derivative to
Packit ea1746
    // evaluate, typically some size proportional to the size of the SIMD
Packit ea1746
    // registers of the CPU.
Packit ea1746
    int num_strides = static_cast<int>(ceil(num_active_parameters /
Packit ea1746
                                            static_cast<float>(Stride)));
Packit ea1746
Packit ea1746
    int current_derivative_section = 0;
Packit ea1746
    int current_derivative_section_cursor = 0;
Packit ea1746
Packit ea1746
    for (int pass = 0; pass < num_strides; ++pass) {
Packit ea1746
      // Set most of the jet components to zero, except for
Packit ea1746
      // non-constant #Stride parameters.
Packit ea1746
      const int initial_derivative_section = current_derivative_section;
Packit ea1746
      const int initial_derivative_section_cursor =
Packit ea1746
        current_derivative_section_cursor;
Packit ea1746
Packit ea1746
      int active_parameter_count = 0;
Packit ea1746
      parameter_cursor = 0;
Packit ea1746
Packit ea1746
      for (int i = 0; i < num_parameter_blocks; ++i) {
Packit ea1746
        for (int j = 0; j < parameter_block_sizes()[i];
Packit ea1746
             ++j, parameter_cursor++) {
Packit ea1746
          input_jets[parameter_cursor].v.setZero();
Packit ea1746
          if (active_parameter_count < Stride &&
Packit ea1746
              parameter_cursor >= (
Packit ea1746
                start_derivative_section[current_derivative_section] +
Packit ea1746
                current_derivative_section_cursor)) {
Packit ea1746
            if (jacobians[i] != NULL) {
Packit ea1746
              input_jets[parameter_cursor].v[active_parameter_count] = 1.0;
Packit ea1746
              ++active_parameter_count;
Packit ea1746
              ++current_derivative_section_cursor;
Packit ea1746
            } else {
Packit ea1746
              ++current_derivative_section;
Packit ea1746
              current_derivative_section_cursor = 0;
Packit ea1746
            }
Packit ea1746
          }
Packit ea1746
        }
Packit ea1746
      }
Packit ea1746
Packit ea1746
      if (!(*functor_)(&jet_parameters[0], &output_jets[0])) {
Packit ea1746
        return false;
Packit ea1746
      }
Packit ea1746
Packit ea1746
      // Copy the pieces of the jacobians into their final place.
Packit ea1746
      active_parameter_count = 0;
Packit ea1746
Packit ea1746
      current_derivative_section = initial_derivative_section;
Packit ea1746
      current_derivative_section_cursor = initial_derivative_section_cursor;
Packit ea1746
Packit ea1746
      for (int i = 0, parameter_cursor = 0; i < num_parameter_blocks; ++i) {
Packit ea1746
        for (int j = 0; j < parameter_block_sizes()[i];
Packit ea1746
             ++j, parameter_cursor++) {
Packit ea1746
          if (active_parameter_count < Stride &&
Packit ea1746
              parameter_cursor >= (
Packit ea1746
                start_derivative_section[current_derivative_section] +
Packit ea1746
                current_derivative_section_cursor)) {
Packit ea1746
            if (jacobians[i] != NULL) {
Packit ea1746
              for (int k = 0; k < num_residuals(); ++k) {
Packit ea1746
                jacobians[i][k * parameter_block_sizes()[i] + j] =
Packit ea1746
                    output_jets[k].v[active_parameter_count];
Packit ea1746
              }
Packit ea1746
              ++active_parameter_count;
Packit ea1746
              ++current_derivative_section_cursor;
Packit ea1746
            } else {
Packit ea1746
              ++current_derivative_section;
Packit ea1746
              current_derivative_section_cursor = 0;
Packit ea1746
            }
Packit ea1746
          }
Packit ea1746
        }
Packit ea1746
      }
Packit ea1746
Packit ea1746
      // Only copy the residuals over once (even though we compute them on
Packit ea1746
      // every loop).
Packit ea1746
      if (pass == num_strides - 1) {
Packit ea1746
        for (int k = 0; k < num_residuals(); ++k) {
Packit ea1746
          residuals[k] = output_jets[k].a;
Packit ea1746
        }
Packit ea1746
      }
Packit ea1746
    }
Packit ea1746
    return true;
Packit ea1746
  }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  internal::scoped_ptr<CostFunctor> functor_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_PUBLIC_DYNAMIC_AUTODIFF_COST_FUNCTION_H_