compressed_row_jacobian_writer.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: keir@google.com (Keir Mierle)
  30. #include "ceres/compressed_row_jacobian_writer.h"
  31. #include <algorithm>
  32. #include <iterator>
  33. #include <memory>
  34. #include <string>
  35. #include <utility>
  36. #include <vector>
  37. #include "ceres/casts.h"
  38. #include "ceres/compressed_row_sparse_matrix.h"
  39. #include "ceres/parameter_block.h"
  40. #include "ceres/program.h"
  41. #include "ceres/residual_block.h"
  42. #include "ceres/scratch_evaluate_preparer.h"
  43. namespace ceres::internal {
  44. void CompressedRowJacobianWriter::PopulateJacobianRowAndColumnBlockVectors(
  45. const Program* program, CompressedRowSparseMatrix* jacobian) {
  46. const auto& parameter_blocks = program->parameter_blocks();
  47. auto& col_blocks = *(jacobian->mutable_col_blocks());
  48. col_blocks.resize(parameter_blocks.size());
  49. int col_pos = 0;
  50. for (int i = 0; i < parameter_blocks.size(); ++i) {
  51. col_blocks[i].size = parameter_blocks[i]->TangentSize();
  52. col_blocks[i].position = col_pos;
  53. col_pos += col_blocks[i].size;
  54. }
  55. const auto& residual_blocks = program->residual_blocks();
  56. auto& row_blocks = *(jacobian->mutable_row_blocks());
  57. row_blocks.resize(residual_blocks.size());
  58. int row_pos = 0;
  59. for (int i = 0; i < residual_blocks.size(); ++i) {
  60. row_blocks[i].size = residual_blocks[i]->NumResiduals();
  61. row_blocks[i].position = row_pos;
  62. row_pos += row_blocks[i].size;
  63. }
  64. }
  65. void CompressedRowJacobianWriter::GetOrderedParameterBlocks(
  66. const Program* program,
  67. int residual_id,
  68. std::vector<std::pair<int, int>>* evaluated_jacobian_blocks) {
  69. auto residual_block = program->residual_blocks()[residual_id];
  70. const int num_parameter_blocks = residual_block->NumParameterBlocks();
  71. for (int j = 0; j < num_parameter_blocks; ++j) {
  72. auto parameter_block = residual_block->parameter_blocks()[j];
  73. if (!parameter_block->IsConstant()) {
  74. evaluated_jacobian_blocks->push_back(
  75. std::make_pair(parameter_block->index(), j));
  76. }
  77. }
  78. std::sort(evaluated_jacobian_blocks->begin(),
  79. evaluated_jacobian_blocks->end());
  80. }
  81. std::unique_ptr<SparseMatrix> CompressedRowJacobianWriter::CreateJacobian()
  82. const {
  83. const auto& residual_blocks = program_->residual_blocks();
  84. const int total_num_residuals = program_->NumResiduals();
  85. const int total_num_effective_parameters = program_->NumEffectiveParameters();
  86. // Count the number of jacobian nonzeros.
  87. //
  88. // We used an unsigned int here, so that we can compare it INT_MAX without
  89. // triggering overflow behaviour.
  90. unsigned int num_jacobian_nonzeros = total_num_effective_parameters;
  91. for (auto* residual_block : residual_blocks) {
  92. const int num_residuals = residual_block->NumResiduals();
  93. const int num_parameter_blocks = residual_block->NumParameterBlocks();
  94. for (int j = 0; j < num_parameter_blocks; ++j) {
  95. auto parameter_block = residual_block->parameter_blocks()[j];
  96. if (!parameter_block->IsConstant()) {
  97. num_jacobian_nonzeros += num_residuals * parameter_block->TangentSize();
  98. if (num_jacobian_nonzeros > std::numeric_limits<int>::max()) {
  99. LOG(ERROR) << "Unable to create Jacobian matrix: Too many entries in "
  100. "the Jacobian matrix. num_jacobian_nonzeros = "
  101. << num_jacobian_nonzeros;
  102. return nullptr;
  103. }
  104. }
  105. }
  106. }
  107. // Allocate storage for the jacobian with some extra space at the end.
  108. // Allocate more space than needed to store the jacobian so that when the LM
  109. // algorithm adds the diagonal, no reallocation is necessary. This reduces
  110. // peak memory usage significantly.
  111. auto jacobian = std::make_unique<CompressedRowSparseMatrix>(
  112. total_num_residuals,
  113. total_num_effective_parameters,
  114. static_cast<int>(num_jacobian_nonzeros));
  115. // At this stage, the CompressedRowSparseMatrix is an invalid state. But
  116. // this seems to be the only way to construct it without doing a memory
  117. // copy.
  118. int* rows = jacobian->mutable_rows();
  119. int* cols = jacobian->mutable_cols();
  120. int row_pos = 0;
  121. rows[0] = 0;
  122. for (auto* residual_block : residual_blocks) {
  123. const int num_parameter_blocks = residual_block->NumParameterBlocks();
  124. // Count the number of derivatives for a row of this residual block and
  125. // build a list of active parameter block indices.
  126. int num_derivatives = 0;
  127. std::vector<int> parameter_indices;
  128. for (int j = 0; j < num_parameter_blocks; ++j) {
  129. auto parameter_block = residual_block->parameter_blocks()[j];
  130. if (!parameter_block->IsConstant()) {
  131. parameter_indices.push_back(parameter_block->index());
  132. num_derivatives += parameter_block->TangentSize();
  133. }
  134. }
  135. // Sort the parameters by their position in the state vector.
  136. std::sort(parameter_indices.begin(), parameter_indices.end());
  137. if (adjacent_find(parameter_indices.begin(), parameter_indices.end()) !=
  138. parameter_indices.end()) {
  139. std::string parameter_block_description;
  140. for (int j = 0; j < num_parameter_blocks; ++j) {
  141. auto parameter_block = residual_block->parameter_blocks()[j];
  142. parameter_block_description += parameter_block->ToString() + "\n";
  143. }
  144. LOG(FATAL) << "Ceres internal error: "
  145. << "Duplicate parameter blocks detected in a cost function. "
  146. << "This should never happen. Please report this to "
  147. << "the Ceres developers.\n"
  148. << "Residual Block: " << residual_block->ToString() << "\n"
  149. << "Parameter Blocks: " << parameter_block_description;
  150. }
  151. // Update the row indices.
  152. const int num_residuals = residual_block->NumResiduals();
  153. for (int j = 0; j < num_residuals; ++j) {
  154. rows[row_pos + j + 1] = rows[row_pos + j] + num_derivatives;
  155. }
  156. // Iterate over parameter blocks in the order which they occur in the
  157. // parameter vector. This code mirrors that in Write(), where jacobian
  158. // values are updated.
  159. int col_pos = 0;
  160. for (int parameter_index : parameter_indices) {
  161. auto parameter_block = program_->parameter_blocks()[parameter_index];
  162. const int parameter_block_size = parameter_block->TangentSize();
  163. for (int r = 0; r < num_residuals; ++r) {
  164. // This is the position in the values array of the jacobian where this
  165. // row of the jacobian block should go.
  166. const int column_block_begin = rows[row_pos + r] + col_pos;
  167. for (int c = 0; c < parameter_block_size; ++c) {
  168. cols[column_block_begin + c] = parameter_block->delta_offset() + c;
  169. }
  170. }
  171. col_pos += parameter_block_size;
  172. }
  173. row_pos += num_residuals;
  174. }
  175. CHECK_EQ(num_jacobian_nonzeros - total_num_effective_parameters,
  176. rows[total_num_residuals]);
  177. PopulateJacobianRowAndColumnBlockVectors(program_, jacobian.get());
  178. return jacobian;
  179. }
  180. void CompressedRowJacobianWriter::Write(int residual_id,
  181. int residual_offset,
  182. double** jacobians,
  183. SparseMatrix* base_jacobian) {
  184. auto* jacobian = down_cast<CompressedRowSparseMatrix*>(base_jacobian);
  185. double* jacobian_values = jacobian->mutable_values();
  186. const int* jacobian_rows = jacobian->rows();
  187. auto residual_block = program_->residual_blocks()[residual_id];
  188. const int num_residuals = residual_block->NumResiduals();
  189. std::vector<std::pair<int, int>> evaluated_jacobian_blocks;
  190. GetOrderedParameterBlocks(program_, residual_id, &evaluated_jacobian_blocks);
  191. // Where in the current row does the jacobian for a parameter block begin.
  192. int col_pos = 0;
  193. // Iterate over the jacobian blocks in increasing order of their
  194. // positions in the reduced parameter vector.
  195. for (auto& evaluated_jacobian_block : evaluated_jacobian_blocks) {
  196. auto parameter_block =
  197. program_->parameter_blocks()[evaluated_jacobian_block.first];
  198. const int argument = evaluated_jacobian_block.second;
  199. const int parameter_block_size = parameter_block->TangentSize();
  200. // Copy one row of the jacobian block at a time.
  201. for (int r = 0; r < num_residuals; ++r) {
  202. // Position of the r^th row of the current jacobian block.
  203. const double* block_row_begin =
  204. jacobians[argument] + r * parameter_block_size;
  205. // Position in the values array of the jacobian where this
  206. // row of the jacobian block should go.
  207. double* column_block_begin =
  208. jacobian_values + jacobian_rows[residual_offset + r] + col_pos;
  209. std::copy(block_row_begin,
  210. block_row_begin + parameter_block_size,
  211. column_block_begin);
  212. }
  213. col_pos += parameter_block_size;
  214. }
  215. }
  216. } // namespace ceres::internal