The same gradient with the pair types of one length scale contracted together. type_group[t] is the group of pair type t, groups numbered from zero; the caller holds the lengths of a group equal. group_slot[g] is the pair type whose entry receives the derivative of group g, or -1 to drop it; every other length entry is zero.
730{
732 throw std::logic_error(
733 "gradient requires a conditioned molecular posterior");
734 auto& s = *impl_;
735 const bool grouped = !type_group.empty();
736 if (grouped && type_group.size() != static_cast<std::size_t>(s.types))
737 throw std::invalid_argument("one group per pair type is required");
738 const int groups = grouped ? static_cast<int>(group_slot.size()) : s.types;
739 V::ivector device_group, device_type;
740 if (grouped) {
741
742 std::vector<int> group_type(group_slot.size(), -1);
743 for (int t = 0; t < s.types; ++t) {
744 const int g = type_group[static_cast<std::size_t>(t)];
745 if (g < 0 || g >= groups)
746 throw std::invalid_argument("pair type group out of range");
747 if (group_type[static_cast<std::size_t>(g)] < 0)
748 group_type[static_cast<std::size_t>(g)] = t;
749 }
750 for (const int t: group_type)
751 if (t < 0) throw std::invalid_argument("empty pair type group");
752 device_group = V::ivector("gpr pair type group", type_group.size());
753 device_type = V::ivector("gpr group pair type", group_type.size());
754 auto gh = Kokkos::create_mirror_view(device_group);
755 auto th = Kokkos::create_mirror_view(device_type);
756 for (std::size_t t = 0; t < type_group.size(); ++t)
757 gh(t) = type_group[t];
758 for (std::size_t g = 0; g < group_type.size(); ++g)
759 th(g) = group_type[g];
760 detail::audited_deep_copy(s.exec, device_group, gh,
761 detail::CopyRole::ToExecution);
762 detail::audited_deep_copy(s.exec, device_type, th,
763 detail::CopyRole::ToExecution);
764 s.stats.input_bytes +=
765 (type_group.size() + group_type.size()) * sizeof(int);
766 }
767 const auto weight = s.posterior.objective_weight(data_term_scale);
768 V::vector
gradient(
"gpr kernel parameter contractions", groups + 2);
770 s.exec, s.training.rows(s.topology()), s.pair_index, s.hyper, s.nugget,
771 weight,
gradient, V::const_ivector(device_group),
772 V::const_ivector(device_type));
773 Eigen::VectorXd host;
775 s.stats.output_bytes += host.size() * sizeof(double);
776 Eigen::VectorXd result =
777 Eigen::VectorXd::Zero(s.types + 1 + (differentiable_noise > 0.0));
778 result(0) = host(0);
779 if (grouped) {
780 for (int g = 0; g < groups; ++g) {
781 const int slot = group_slot[static_cast<std::size_t>(g)];
782 if (slot >= s.types)
783 throw std::invalid_argument("group slot out of range");
784 if (slot >= 0) result(1 + slot) = host(2 + g);
785 }
786 } else {
787 result.segment(1, s.types) = host.tail(s.types);
788 }
789 if (differentiable_noise > 0.0) {
790 const int n = static_cast<int>(s.posterior.rows());
791 double trace = 0.0;
792 if (n > 0) {
793 constexpr int kTile = 256;
794 const int n_tiles = (n + kTile - 1) / kTile;
795 V::vector tiles(
796 Kokkos::view_alloc(s.exec, Kokkos::WithoutInitializing,
797 "gpr resident noise tiles"),
798 static_cast<std::size_t>(n_tiles));
799 V::vector scalar_out(
800 Kokkos::view_alloc(s.exec, Kokkos::WithoutInitializing,
801 "gpr resident noise scalar"),
802 1);
803 Kokkos::parallel_for(
804 "gpr resident noise contraction tiles",
805 Kokkos::RangePolicy<Exec>(s.exec, 0, n_tiles),
806 KOKKOS_LAMBDA(const int t) {
807 const int begin = t * kTile;
808 const int end = (begin + kTile < n) ? (begin + kTile) : n;
809 double sum = 0.0, corr = 0.0;
810 for (int i = begin; i < end; ++i) {
811 const double term = weight(i, i);
812 const double next = sum + term;
813 corr += Kokkos::abs(sum) >= Kokkos::abs(term)
814 ? (sum - next) + term
815 : (term - next) + sum;
816 sum = next;
817 }
818 tiles(t) = sum + corr;
819 });
820 Kokkos::parallel_for(
821 "gpr resident noise contraction fold",
822 Kokkos::RangePolicy<Exec>(s.exec, 0, 1),
823 KOKKOS_LAMBDA(const int) {
824 double sum = 0.0, corr = 0.0;
825 for (int t = 0; t < n_tiles; ++t) {
826 const double term = tiles(t);
827 const double next = sum + term;
828 corr += Kokkos::abs(sum) >= Kokkos::abs(term)
829 ? (sum - next) + term
830 : (term - next) + sum;
831 sum = next;
832 }
833 scalar_out(0) = sum + corr;
834 });
835 Kokkos::View<double, Kokkos::HostSpace,
836 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
837 host_trace(&trace);
838 detail::audited_deep_copy(s.exec, host_trace,
839 Kokkos::subview(scalar_out, 0),
840 detail::CopyRole::ToHost);
841 s.exec.fence("gpr resident noise contraction");
842 }
843 result(result.size() - 1) = differentiable_noise * trace;
844 s.stats.output_bytes += sizeof(double);
845 }
846 return result;
847}
Eigen::VectorXd gradient(double differentiable_noise=0.0, double data_term_scale=1.0)
void contract_kernel_derivatives(const ExecSpace &exec, const JointRows< ExecSpace > &rows, const PairTypeIndex< ExecSpace > &index, const JointHyper< ExecSpace > &hyper, double eta, const typename Views< ExecSpace >::const_matrix &W, const typename Views< ExecSpace >::vector &out, const typename Views< ExecSpace >::const_ivector &type_group={}, const typename Views< ExecSpace >::const_ivector &group_type={})