782 {
786 Vector::Zero(m),
787 std::vector<Matrix>(p, Matrix::Zero(m, m)),
788 std::vector<Vector>(p, Vector::Zero(m)),
789 Vector::Zero(p + 1),
790 Vector::Zero(p + 1)};
791 std::size_t entry_count = 0;
792 result.
visit([&](
double&) { ++entry_count; });
793 std::vector<linalg::ExactSum> exact(entry_count);
794 std::exception_ptr failure;
795 try {
796 [[maybe_unused]] const SerialEigenProducts serial_products;
798 Vector::Constant(d + 1, 1.0 / params.gradient_noise);
799 precision(0) = 1.0 / params.energy_noise;
800 for (const auto& [index, environment, prior_trace]:
803 Matrix::Zero(m, m),
804 Vector::Zero(m),
805 std::vector<Matrix>(p, Matrix::Zero(m, m)),
806 std::vector<Vector>(p, Vector::Zero(m)),
807 Vector::Zero(p + 1),
808 Vector::Zero(p + 1)};
809 auto cross =
crossRows(environment, params,
true);
810 cross[0] =
whitenRows(cross[0], transform.lower);
811 for (std::size_t parameter = 0;
812 parameter < transform.derivative.size(); ++parameter) {
813 cross[parameter + 1] =
814 whitenRows(cross[parameter + 1], transform.lower);
816 cross[parameter + 1], cross[0],
817 transform.derivative[parameter]);
818 }
819 contribution.prior_trace =
priorTrace(prior_trace, params);
823 const Vector weighted_target = precision.cwiseProduct(target);
824 const Matrix weighted_cross = precision.asDiagonal() * cross[0];
826 contribution.gram.data(), contribution.gram.outerStride(),
827 cross[0].data(), cross[0].outerStride(),
828 weighted_cross.data(), weighted_cross.outerStride(), m, m,
829 d + 1, 1.0, true, false);
830 contribution.rhs.noalias() +=
831 cross[0].transpose() * weighted_target;
832 contribution.target_norm(0) += target.dot(weighted_target);
833 for (Eigen::Index parameter = 0;
834 parameter < static_cast<Eigen::Index>(
species.size()) + 1;
835 ++parameter) {
836 Matrix term = Matrix::Zero(m, m);
838 term.data(), term.outerStride(),
839 cross[parameter + 1].data(),
840 cross[parameter + 1].outerStride(),
841 weighted_cross.data(), weighted_cross.outerStride(), m,
842 m, d + 1, 1.0, true, false);
843 contribution.dgram[parameter] += term + term.transpose();
844 contribution.drhs[parameter].noalias() +=
845 cross[parameter + 1].transpose() * weighted_target;
846 }
847 for (const bool gradient: {false, true}) {
848 const Eigen::Index parameter = p - (gradient ? 1 : 2);
849 const Eigen::Index offset = gradient ? 1 : 0;
850 const Eigen::Index count = gradient ? d : 1;
851 const auto block = cross[0].middleRows(offset, count);
852 const auto weighted_block =
853 weighted_cross.middleRows(offset, count);
855 contribution.dgram[parameter].data(),
856 contribution.dgram[parameter].outerStride(),
857 block.data(), block.outerStride(),
858 weighted_block.data(), weighted_block.outerStride(), m,
859 m, count, -1.0, true, false);
860 contribution.drhs[parameter].noalias() -=
861 block.transpose() *
862 weighted_target.segment(offset, count);
863 contribution.target_norm(parameter + 1) -=
864 target.segment(offset, count)
865 .dot(weighted_target.segment(offset, count));
866 }
867
868
869 std::size_t entry = 0;
870 contribution.visit([&](double value) {
871 if (!std::isfinite(value))
872 throw std::runtime_error(
873 "atomic fit statistics are not finite");
874 exact[entry++].add(value);
875 });
876 }
877 } catch (...) {
878 failure = std::current_exception();
879 }
880 shareFailure(
context(), failure);
881 constexpr std::size_t chunk = 4096;
882 for (std::size_t offset = 0; offset < exact.size(); offset += chunk)
884 exact.data() + offset,
885 static_cast<int>(std::min(chunk, exact.size() - offset)),
887 std::size_t entry = 0;
888 result.visit([&](double& value) { value = exact[entry++].value(); });
889 result.gram = result.gram.selfadjointView<Eigen::Lower>();
890 for (auto& derivative: result.dgram)
891 derivative = derivative.selfadjointView<Eigen::Lower>();
892 return result;
893 }
void addMatrixProduct(double *out, std::ptrdiff_t out_stride, const double *left, std::ptrdiff_t left_stride, const double *right, std::ptrdiff_t right_stride, std::ptrdiff_t rows, std::ptrdiff_t columns, std::ptrdiff_t depth, double scale, bool transpose_left, bool transpose_right)
void subtractProduct(Target &&out, const Left &left, const Right &right)
void exact_sum_bcast(ExactSum *sums, int count, const MpiContext &context)
void visit(Visitor &&visitor)
Vector priorTrace(const std::vector< PriorTracePair > &pairs, const Parameters ¶ms) const
std::vector< Matrix > crossRows(const Environment &cached, const Parameters ¶ms, bool derivatives, bool energy_only=false) const
std::vector< TrainingEnvironment > local_environments
static Matrix whitenRows(const Matrix &rows, const Matrix &lower)